>
>
Predefined PVS_STUDIO macro


Predefined PVS_STUDIO macro

Among the numerous filtration and message suppression methods of PVS-Studio analyzer is the PVS_STUDIO predefined macro.

The first case when it might come in handy is when one wants to prevent some code from getting in the analyzer for a check. For example, the analyzer generates a diagnostic message for the following code:

  int rawArray[5];
  rawArray[-1] = 0;

However, if you will 'wrap' it using this macro the message will not be generated.

  int rawArray[5];
#ifndef PVS_STUDIO
  rawArray[-1] = 0;
#endif

The PVS_STUDIO macro is automatically inserted while checking the code from the IDE. But if you are using PVS-Studio from the command line, the macro will not be passed by default to the analyzer and this should be done manually.

The second case is the override of the default and custom macros. For example, for the following code a warning will be generated about dereference of a potentially null pointer:

char *st = (char*)malloc(10);
TEST_MACRO(st != NULL);
st[0] = '\0'; //V522

To tell the analyzer that the execution of the program gets interrupted with certain conditions, you can override the macro in the following way:

#ifdef PVS_STUDIO
#undef TEST_MACRO
#define TEST_MACRO(expr) if (!(expr)) throw "PVS-Studio";
#endif

char *st = (char*)malloc(10);
TEST_MACRO(st != NULL);
st[0] = '\0';

This method allows to remove the analyzer warnings on the code checked using different libraries, as well as on any other macros that are used for debugging and testing.

See the discussion "Mark variable as not NULL after BOOST_REQUIRE in PVS-Studio" on StackOverflow.com site.

PVS_STUDIO macro will be automatically substituted when checking code from IDE. If you use the code check from the command line, macro is not passed to the analyzer by default, and it should be done manually.