Using fbkb_tags
If you are using the classic engine, you can use symbols to define which tag(s) are used by Klocwork within the <klocwork>/config/kb/cpp/fbkb_tags.h file. If a kw_tag is defined, then the knowledge base file that has kw_tag in the line with #! is used.
If a kw_tag is already defined in the file that you would like to import in your project, then you need to define the symbol(s) associated with this kw_tag or add a new one in the file. You can define the symbol in several ways: using gcc command, in the #define in your source code, or in an 'external_config.txt' file.
Example 1: Using gcc or defining it directly in your source file
Let's say we want to use <klocwork>/config/kb/cpp/linux.kb and the head of your file has the '#! linux' token. This means that the ‘linux’ kw_tag needs to be defined for this KB file to be enabled. Open the 'fbkb_tags.h' file and you will see the following definition for the kw_tag:
#ifdef __linux__ #kw_tag linux #endif
You can see that this ‘kw_tag linux’ is protected by the definition of the symbol __linux__ . In other words, the ‘kw_tag linux’ is only defined if the __linux__ symbol is defined in your project. If the __linux__ symbol is not already defined in your project, you can define it in several ways:
- Using kwinject, adding the option gcc -D__linux__=1 ... enables the build specification to pass this information to Klocwork.
- You can add the following directly to the top of your source file:
#define __linux__ 1
You don't need to regenerate the build specification because the analysis engine finds it during its pre-processing stage.
#define __linux__ 1
Next, import this file into your Klocwork project and you can analyze as usual (for example, with --url if you are using kwbuildproject).
Example 2: Add the compiler option to the Klocwork build command
There are two ways you can add the compiler option to the Klocwork build command, depending on what command you prefer to use:
- If you are using kwbuildproject, run the following command:
kwbuildproject --add-compiler-options "--KW_CLASSIC_BEGIN -D__linux__=1 --KW_CLASSIC_END --KW_MODERN_BEGIN --kw_tags=linux --KW_MODERN_END" ...
- If you are using kwcheck, create a file called external_config.txt containing the following:
additional_compiler_options=--KW_CLASSIC_BEGIN -D__linux__=1 --KW_CLASSIC_END --KW_MODERN_BEGIN --kw_tags=linux --KW_MODERN_END
Lastly, copy the external_config.txt file you created into your .kwps directory. Adding the compiler option as above makes the knowledge base available to both the classic and the new analysis engine.