This topic explains how you can perform static code analysis to identify code that does not comply with a preconfigured or customized set of static analysis rules. Sections include:

Incremental Static Analysis

C/C++test is optimized to minimize the impact on your development process. By reusing information collected in previous analysis runs, it can reduce the analysis time in subsequent runs. The information is stored in the .cpptest folder in your solution. To ensure optimal performance, avoid removing the .cpptest folder or deleting the contents of the folder.

Analyzing Headers

C++test does not directly analyze headers unless they are included by a source file under test. See How do I analyze header files/what files are analyzed? for details.

Analyzing Template Functions

C++test does perform static analysis of instantiated function templates and instantiated members of class templates. See Support for C++ Template Functions for details.

Running Static Code Analysis

The general procedure for performing static code analysis on one or more files is as follows:

  1. Select or create a Test Configuration with your preferred static code analysis settings.
  2. Start the test using the preferred Test Configuration.
  3. Review and respond to the results.
  4. (Optional) Fine-tune static code analysis settings as needed.

Configuring Batch-Mode Analysis with cpptestcli

Regularly-schedule batch-mode coding standard analysis should simply execute a built-in or custom Test Configuration that analyzes your project using the coding standard rules important to your team. For example:

  • cpptestcli -solution “C:\temp\*.sln” –config team://CodingStandardsAnalysis -publish
See Testing from the Command Line Interface for more details on configuring batch-mode tests.

Detecting Duplicated Code with Static Analysis

By identifying and removing duplicate code, you make your code more concise, more readable, and easier to maintain. It can detect similar code fragments that were introduced during the development process (for example by copy-paste mistakes).  It is especially useful for large projects, where manual duplication detection is tedious and ineffective.

To detect duplicated code, run the built-in "Find Duplicated Code" Test Configuration or a custom Test Configuration that includes the desired rules from the Code Duplication Detection category.

You can customize the level of code similarity that is used to determine whether two code fragments are reported as duplicates. By configuring rule properties, you can ignore variable names, string literals, number literals, and boolean literals. All text flow differences (like tabs, spaces, line breaks and comments) are always ignored.

For more details, see the rule descriptions for specific rules in the Code Duplication Detection category.

Analyzing an MSBuild-based Project

C/C++test ships with a script that allows you to quickly reconfigure your MSBuild project to generate .bdf files. This script supports both Visual Studio and CMake-generated MSBuild projects. The script creates a backup of the target .vcxproj file and then modifies it to enable the requested Parasoft features on all build targets.

C/C++test includes a Python script launcher to run the included tools, which can be found at:

<CPPTEST_INSTALL_DIR>/engine/bin/cpptestpy.exe
The MSBuild integration script is in the following location:

<CPPTEST_INSTALL_DIR>/integration/msbuild/msbuild_cpptest.py

To analyze an MSBuild-based project:

  1. Setup the environmental variables.

Be sure to not use double quotes after = which will cause incorrect string interpretation.  

    1. Setup the CPPTEST_INSTALL_DIR variable.

      set CPPTEST_HOME=<CPPTEST_INSTALL_DIR>
  1. (CMake only) Generate your MSBuild files using CMake. 

    cd <PROJECT_DIR>
    mkdir build
    cd build
    cmake ..
  2. Run the integration script. 

    <cpptestpy.exe> <msbuild_cpptest.py> -b -f project.vcxproj
  3. Build the project using MSBuild.

    msbuild -t:Rebuild project.sln

    A .bdf file will be generated in your current working directory. 

  4.  To launch static analysis, import the .bdf file. For details, see Testing from the Command Line Interface.


  • No labels