Despite the fact that our analyzer is stated to be one of the simplest tools in installing and everyday usage (everything is working "out-of-the-box" and does not require any unintuitive tweaks), some users lacks flexibility of some of the diagnostic rules. Unfortunately, usability and flexibility is if not totally opposite, but sometimes those features conflict with each other. In this note, we shall be talking about additional mechanism of tuning some diagnostic rules, which can be useful for some of our valuable users.
It was already possible to manipulate the behavior of a few diagnostic rules using special comments. You can read about it from their description in documentation. For instance, //-V610_LEFT_SIGN_OFF for V610 rule.
However, all these settings was about false alarm suppression. New settings now allows users to hint analyzer where additional errors may be found.
Syntax of new mechanism is as follows: to activate addition to diagnostic, user should use //+V comment. We have chosen this format because it is similar to the widely used comment to false alarm suppression: //+V. New format to make an addition to diagnostic is simple:
//+V<Number of rule>, <Key>:<Value>, ... , <Key>:<Value>
User can arrange parameters in any order in relation to each other. In a few diagnostics, there may be a possibility to add values with the same key, but with different values. In almost any diagnostic, some parameters in addition may be absent.
User may write an addition in:
Restriction - user should write the rule in one line.
Examples:
//+V128,function:N128_write_it,non_memsize:2,class:N128_T
//+V576,function:foo,format_arg:1,ellipsis_arg:3,namespace:X,class:Y
At the time when article was written, we have implemented new additions for two diagnostic rules: V128 and V576. In future, there may be new additions to other rules.
V128 rule finds all the places where variable of memsize type is written into file or is read from file. Full description of rule is present in the link above. One user asked to extend V128 with addition of possibility of creating a warning on custom functions.
Addition format is as follows: just above function prototype (or near its realization, or in standard header file) user should add a special comment. Let us start with the usage example:
//+V128, function:write, non_memsize:2
void write(string name, char);
void write(string name, int32);
void write(string name, int64);
foo()
{
write("zz", array.size()); // warning V128
}
Format:
Warning level in case of user functions is always first.
At last, here is full usage example:
//Warns when in method C of class B
//from A namespace memsize-type value
//is put as a second or third argument.
//+V128,namespace:A,class:B,function:C,non_memsize:3,non_memsize:2
V576 rule finds all the places where argument in printf()-like function is not corresponding with its desired type in format string. Potential user asked us to extend this diagnostic rule to the case of user-defined functions. He suggested that analyzer would print a warning message in case of somewhere an argument does not correspond with format string. It is assumed that formatting principle is equal to the one in printf() function.
Again, no sooner said than done.
Addition format is alike to the one of V128 diagnostics. User should write a comment of special kind near function prototype (or near its implementation, or in standard header file). Again, let us start with the usage example:
//+V576, function:Mylog, format_arg:1, ellipsis_arg:2
Mylog("%f", time(NULL)); // warning V576
Format:
At last, here is full usage example:
//Warn when in C method of class B from A namespace
//arguments, counting from third one, does not
//correspond to the format line in the second argument
//+V576,namespace:A,class:B,function:C,format_arg:2,ellipsis_arg:3
Our tool is constantly improving; it always gains new possibilities and other decisions features. I hope we also stressed that we love our clients and value their wishes. On PVS-Studio license purchase, you will also gain qualified support and realization of features that will allow you to gain maximum profit from static code analysis.