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.
Start Visual Studio, and create a new project using the Empty Project template under Visual C++, giving it a name of your choice.
For this project, perform the following steps:
AnalyzeThis.h
.class AnalyzeThis { public: int LookHere(int param); };
AnalyzeThis.cpp
.# 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; }
LookHere
method so that both x
and y
are initialized correctly with zero values.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.
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:
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:
18.191.102.112