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.

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").

Available configurations

The .sconf file supports the following suppression configurations:

  • kind: macro, file, or directory
    The type of suppression to be applied
    T

  • name
    If the kind of suppression is a:

    • macro: the name of the macro to apply the suppression configuration

    • file or directory: the relative or absolute path (or path pattern) of the file or directory to apply the suppression configuration

  • all_issue_codes: true/false or issue_codes
    To specify which issues you want to filter, provide either all_issue_codes = true/false or a list of issue codes using issue_codes

  • filter: true/false
    If filter is false for any particular issue, suppression is skipped (see Example 1)

  • allow_optimization: true/false
    If allow_optimization is true, the translation unit from that file is skipped for path checkers only (see Example 3)

  • comment
    Enter any relevant comment(s) about the configuration

Using path patterns

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 recursive or non-recursive searches.

All files and folders that match a specified pattern will be suppressed and you should be cognizant of similar file or folder names in your project when defining the pattern.

Example 1

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

diff  

This includes the text diff in any part of the path.

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

This returns a match, since a double asterisk matches any number of paths (in this case, /space/workspaces/cvs).

*/ccvs.1.11.18/diff/diff.c

This does not return a match, since a single asterisk matches only a single file or directory.

**/diff/**

The double asterisk at the start matches /space/workspaces/cvs/ccvs.1.11.18 and the double asterisk at the end matches all files after the diff directory .

Example 2

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

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

This matches a specific file under the path /space/workspaces/cvs/ccvs.1.11.18/ regardless of the minor version number, since * matches the text 18.

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

This matches all files under the same path.

Example 3

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

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

This matches all files that start with the character s under ccvs.1.11.18 and any intermediate directory.

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

This matches all files that contain the character s under ccvs.1.11.18 and any intermediate directory.

Example 4

Given the file path /a/b*c/d/:

/a/b!*c/d/

The escaped asterisk !* is used to search for a path contains the * character, since asterisks can be characters in path specifications in Windows and Linux.

Example 5

Given the file path /a/b!c/d/:

/a/b!!c/d/

The escaped exclamation point !! is used to search for a path that contains the ! character.

Example 6

Given the file path /a/b!*c/d/:

/a/b!!!*c/d/

Since the folder path contains a contiguous string of characters, each unique character in the path must be escaped with an exclamation point.

Given the file path /**/sub!*folder/test.c:

/!*!*/sub!!!*folder/test.c

Each asterisk in the contiguous string must each be escaped with an exclamation point.

Example 7

Given the file path /space/*!*!words/file.cpp:

/space/*/file.cpp

This returns a match, since the wild card * matches both its character value and any search term within the current path level.

Using .sconf files

Example 1

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

Copy
{ "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 configuration suppresses issues in all files whose path contains the pattern "dir1/file.c". 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, as shown next for the pattern "dir1/myfile.cpp", then the suppression is skipped.

Copy
{ "issue_suppressions": [
    { "kind": "macro",
      "name": "INFINITE_LOOP",
      "issue_codes": ["INFINITE_LOOP.MACRO"],
      "filter": true
    },
    { "kind": "file",
      "name": "dir1/myfile.cpp",
      "all_issue_codes": false,
      "filter": true
    },
    { "kind": "directory",
      "name": "dir2",
      "all_issue_codes": true,
      "filter": true
    }
  ]
}

Example 2

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:

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

This configuration 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 the pattern dir/file.cpp.

Example 3

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:

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