Analyzing your C++ code

Static analysis of C++ code is a feature offered in VS2015 Community and the Premium editions of Visual Studio. Static analysis is a useful way to locate potential problems in your code, and provides a way to catch a wide range of problems early in the development cycle.

In this recipe, we will show you how to use Visual Studio's built-in static analysis tools.

Getting ready

Start Visual Studio, and create a new project using the Empty Project template under Visual C++, giving it a name of your choice.

How to do it…

For this project, perform the following steps:

  1. Right-click on the project, and select Properties.
  2. Navigate to Configuration Properties | General, change Configuration Type to Static Library (.lib), and click on OK.
  3. Add a new Header File to the project, and name it AnalyzeThis.h.
  4. Enter the following code in the body of the header file:
    class AnalyzeThis {
      public:
      int LookHere(int param);
    };
  5. Add a new C++ File to the project, and name it AnalyzeThis.cpp.
  6. Enter the following code to the body of the file:
    # include "AnalyzeThis.h"
    int AnalyzeThis::LookHere(int param)
    {
      int x;
      int y;
      if (param > 0) x = param;
      if (param < 0) y = param;
      return x + y;
    }
  7. Compile the project. There should be no errors or warnings.
  8. Right-click on the project and select Properties again. Select the Code Analysis group and ensure that Enable Code Analysis on Build is checked. Click on OK to close the window.
  9. Right-click on the solution in the Solution Explorer window and select Analysis | Run Code Analysis on Solution (Alt + F11). This option is also available in the menu by navigating to Build | Run Code Analysis on Solution.
  10. The Code Analysis results will be displayed, and it will show a single warning about the use of uninitialized memory, as shown in the following screenshot:
    How to do it…
  11. Double-click on the entry to receive more details. The reasons for the analysis warning will be shown, and the code where the warning occurs will be highlighted, as shown in the following screenshot:
    How to do it…
  12. To address these details, change the code in the LookHere method so that both x and y are initialized correctly with zero values.
  13. Rerun the analysis. No messages should be displayed.

How it works…

Visual Studio ships with a set of predefined rules to examine your project for common mistakes and poorly written code. In our example, the code may have compiled cleanly, but it could cause problems in operation, as the x and y variables are not initialized. Static code analysis seeks to find these types of mistakes earlier in the development cycle rather than waiting and hoping for them to be caught later by unit tests or the QA department.

There's more…

Selecting Active ruleset

The ruleset used by the analyzer can be modified to suit your preferences. The Rule Set setting, which can be accessed by going to Settings in the Code Analysis window, offers several choices based on the level of details required or the type of application being developed, as shown in the following screenshot:

Selecting Active ruleset

Improving C++ source navigation speed

Update 2 has added a new SQLite-based system that is designed to speed up source code navigation tools including Go To Definition and Find All References. To make sure it is enabled for your project, open the Options menu, then navigate to Text Editor | C/C++ | Advanced, and ensure the Enable New Database Engine option is set to True, as shown in the following screenshot:

Improving C++ source navigation speed

See also

  • The Using a custom ruleset recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.102.112