Configuring the Visual Studio compiler as static analyzer

It is possible to use the Visual Studio compiler as a code checker (static analyzer) for C, C++ and C#. This means that the compiler warning level is set to maximum and any compiler warnings are stored in the quality database as violations. This makes it possible to track and reduce compiler warnings over time in a controlled manner.

Configuration

A default configuration for compiler warnings is provided by TICS in the configuration directory (in cfg/compilerwarnings). For C/C++, the analyzer VC is used, for C#, Csc.

By default, all warnings are reported (the maximum Visual Studio warning level is used).

Overriding the defaults

To configure warnings, one must specify the rule ID both in IMPL.txt and RULES.txt (just as for coding standard rules).

Example IMPL.txt

C++4057	VC	C4057
C4057	VC	C4057
C++4746	VC	C4746
C4746	VC	C4746

Here, the first three colums are relevant. The fourth column is not (yet) used. (For the moment, it is recommended to repeat the rule ID here.) The first three columns specify the rule ID (Visual Studio warning code), the language for which it should override the defaults and the analyzer tool 'VC'.

Example RULES.txt

C++4127	5	Visual Studio Severe Compiler Warning	A conditional expression should not be constant.
C4127	5	Visual Studio Severe Compiler Warning	A conditional expression should not be constant.
C++4706	7	Visual Studio Compiler Warning		Avoid side-effects in conditional expressions.
C4706	7	Visual Studio Compiler Warning		Avoid side-effects in conditional expressions.

Here, all aspects of the warning can be modified except for its ID (i.e., level, category and synopsis).

Suppressing unwanted warnings

As mentioned previously, by default, all warnings are enabled (the maximum Visual Studio warning level is used). It is not possible to disable warnings via the IMPL.txt since all warnings are implicitly enabled.

To disable unwanted warnings, one must use the Visual Studio pragmas for controlling warnings.

Example

#pragma warning(disable: 4057 4746)
or
#pragma warning(disable: 4057)
#pragma warning(disable: 4746)
to disable warnings C4057 and C4746.

Note the absence of the 'C' prefix in the #pragma!

It is possible to globally disable warnings by placing such pragmas in the PREDEFINES-VC.txt.

Warning reference

See the MSDN C/C++ Building Reference for a list of all compiler warnings.

Guiding compilation

Sometimes it is necessary to guide the C# compilation since the Visual Studio C# project build integration is not perfect. To help find the required .NET assemblies, additional search directories can be specified via an environment variable TICS_NET_ASSEMBLY_PATH. The syntax is similar to other search path variables: a list of directories separated by semicolons (';).

Example

set TICS_NET_ASSEMBLY_PATH=C:\app\tiobe\product\11.2.0\client_2\odp.net\bin\2.x;C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies;C:\Program Files (x86)\SunGard\AvantGard\4.4\Browser\Quantum;C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win32_x86