Filtering out issues using macros or files

As of Klocwork 2022.2, you can suppress macros, files, and directories using an external '.sconf' file. You can import the configuration files with kwcheck or kwciagent similar to other configuration files. You can also provide the .sconf file while running kwbuildproject with the --exclude-issues option, or import it from portal while running a connected project.

When using the --replace-path option with kwbuildproject, suppression uses the original path and not the one that has been replaced. Therefore if you are using issue suppression with the --replace-path option, we recommend that you use the path pattern. If you prefer to use the full path, use the original path, and not the new one.

You can suppress issues using multiple suppression configurations by adding all of them to the same .sconf file. You can also provide multiple .sconf files.

Restriction: When multiple macros are involved, defects would be reported relative to the parent macro (not the embedded macro). For example, in the following code, the defect MISRA.CAST.VOID_PTR_TO_INT.2012 would be reported for the check_assert() macro, and not for the MAP_FAILED macro.

#define MAP_FAILED (void *)-1
#define check_assert(expr) if (!expr) { abort(); }

void f(void *p)
{
	check_assert(p != MAP_FAILED);
}		

If you are using multiple or embedded macros and cannot suppress the parent macro, consider assigning the issue status manually in the Portal (for instance, by changing it to "Not a problem").

Sample .sconf file

Below is an example of a typical .sconf file you can pass through the methods above to suppress particular issues:

{ "issue_suppressions": [
{ "kind": "macro", "name": "MAP_FAILED", "issue_codes" : [ "MISRA.CAST.VOID_PTR_TO_INT.2012" ], "filter": true }
,
{ "kind": "file",
"name": "dir1/file.c",
"all_issue_codes": true,
"filter": true
}
]
}		

This suppression configuration suppresses issues in all files whose path contains the pattern "dir1/file.cpp". For example, /space/folder1/dir1/file.c or /space/folder1/folder2/dir1/file.cpp. You should provide either all_issue_codes = true/false or a list of issue codes with issue_codes to specify which issues you want to filter. If the filter is false for any particular issue, then the suppression is skipped.

{ "issue_suppressions": [
{ "kind": "macro", "name": "MAP_FAILED", "issue_codes" : [ "MISRA.CAST.VOID_PTR_TO_INT.2012" ], "filter": true }
,
{ "kind": "file",
"name": "dir1/file.cpp",
"all_issue_codes": false,
"filter": true
},
{ "kind": "directory",
"name": "dir2",
"all_issue_codes": true,
"filter": true
}
]
}			

When we provide suppression configuration for a directory 'dir', and also for a file in the directory 'dir/file.cpp', then the configuration for the file gets preference over the directory, no matter what the order is. For example:

{ "issue_suppressions": [ \{ "kind": "file", "name": "dir/file.cpp", "all_issue_codes": false, "filter": true }
,
{ "kind": "directory",
"name": "dir",
"all_issue_codes": true,
"filter": true
}
]
}			

For this example, it suppresses issues in all files with path containing pattern 'dir'. Since we set all_issue_codes to false for the file, it won't suppress any issues for files containing pattern dir/file.cpp.

As of Klocwork 2022.3, you can also enable optimization when suppressing issues in files and directories. When optimization is enabled, it skips the whole translation unit from that file, but only for path checkers. You can also add comments to the configuration (which are not printed anywhere). For example:

{ "issue_suppressions": [
	{ "kind": "file",
	  "name": "foo.cpp",
	  "all_issue_codes": true,
 	  "filter": true,
	  "allow_optimization": true,
	  "comment": "add comment here"
	}
   ]
}		

As of Klocwork 2022.4, issue suppression supports full path to the file or directory as well as path patterns. You can also use asterisks as wildcards (* for non recursive or ** recursive searches). See the following examples:

Example 1

Given the sample file path: /space/workspaces/cvs/ccvs.1.11.18/diff/diff.c

The pattern:

    diff  

includes any path that contain the text 'diff' in any part of the path.

The pattern:

**/ccvs.1.11.18/diff/diff.c   

returns a match, since the double-asterisk matches any number of paths (/space/workspaces/cvs in this case). Whereas the pattern:

*/ccvs.1.11.18/diff/diff.c

does not return a match as * can only match a single file or directory.

Lastly, the pattern:

**/diff/**

By leading with '**', it matches /space/workspaces/cvs/ccvs.1.11.18 and trailing it with '**' matches all files below the 'diff' directory.

Example 2

To match a specific file under the path: /space/workspaces/cvs/ccvs.1.11.18/ regardless of the minor version number, use the pattern:

/space/workspaces/cvs/ccvs.1.11.*/diff/diff.c

since the * matches the 18. Or, to match all files under the same path, use the pattern:

/space/workspaces/cvs/ccvs.1.11.**

Example 3

To match all files that start with 's' and that are under ccvs.1.11.18 and any intermediate directory, use the pattern:

**/ccvs.1.11.18/**/s*

Lastly, to match all files that have an 's' in them and are under the ccvs.1.11.18 and any intermediate directory, use the pattern:

**/ccvs.1.11.18/**/*s*