Examples of setting a metrics threshold
The following table describes and shows the syntax of some typical metrics thresholds to include in a Klocwork report. Remember that you can change the text of the Name field (which describes the metrics threshold).
Example 1
Rule intent: At the file level, show the percentage of comment lines. If less than five percent of lines are comments, issue an error. If less than 10% but more than five percent of lines are comments, issue a warning.
Rule syntax: Name;Kind;Expression;Error threshold;Warning threshold
Percent of comments;FILE;LINESCOMM/LOC_FILE*100;<5;<10
Example 2
Rule intent: Show the number of calls to unique functions. If there are 20 or more, issue an error. If there are 15 to 19, issue a warning.
Rule syntax: Name;Kind;Expression;Error threshold;Warning threshold
Number of unique calls (UCT);FUNCTION,CLASS-METHOD;NOCALLS;20;15
Example 3
The following example shows three stages. The source code file shows what you begin with:
/* function main */
main(){
printf("Hello world\n");
}
hello(){
// tbd
}
test(int a){
return 1;
}
Change the threshold in the Metrics thresholds configuration file:
Empty function;FUNCTION;NOOPUSED;>1;==0
After your .mconf file is imported and analyzed, the default kwcheck output will be:
<path>/example2.c:6 METRICS.E.Empty_function Analyze Empty function: main 2>1