You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/code-quality/quick-start-code-analysis-for-c-cpp.md
+61-31Lines changed: 61 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
---
2
-
title: "Quickstart: Code Analysis for C/C++"
3
-
description: Run static analysis on C++ code in Visual Studio to detect common coding problems and defects.
4
-
ms.date: 11/04/2016
2
+
title: "Quickstart: Code analysis for C/C++"
3
+
description: "Run static analysis on C++ code in Visual Studio to detect common coding problems and defects."
4
+
ms.date: 04/08/2020
5
5
ms.topic: "conceptual"
6
6
helpviewer_keywords:
7
7
- "C/C++ code analysis"
8
-
- "code analysis,C/C++"
8
+
- "code analysis,C/C++"
9
9
---
10
10
# Quickstart: Code analysis for C/C++
11
11
12
-
You can improve the quality of your application by running code analysis regularly on C or C++ code. This can help you find common problems, violations of good programming practice, or defects that are difficult to discover through testing. Code analysis warnings differ from compiler errors and warnings because code analysis searches for specific code patterns that are valid but could still create issuesfor you or other people who use your code.
12
+
You can improve the quality of your application by running code analysis regularly on C or C++ code. Code analysis can help you find common problems and violations of good programming practice. And, it finds defects that are difficult to discover through testing. Its warnings differ from compiler errors and warnings: It searches for specific code patterns that are known to cause problems. That is, code that's valid, but could still create issues, either for you or for other people who use your code.
13
13
14
14
## Configure rule sets for a project
15
15
@@ -23,16 +23,58 @@ You can improve the quality of your application by running code analysis regular
23
23
24
24
### Standard C/C++ Rule Sets
25
25
26
-
Visual Studio includes two standard sets of rules for native code:
27
-
28
-
|Rule Set|Description|
29
-
|--------------|-----------------|
30
-
|Microsoft Native Minimum Recommended Rules|This rule set focuses on the most critical problems in your native code, including potential security holes and application crashes. You should include this rule set in any custom rule set you create for your native projects.|
31
-
|Microsoft Native Recommended Rules|This rule set covers a broad range of problems. It includes all the rules in Microsoft Native Minimum Recommended Rules.|
26
+
Visual Studio includes these standard sets of rules for native code:
27
+
28
+
| Rule Set | Description |
29
+
|--|--|
30
+
|**C++ Core Check Arithmetic Rules**| These rules enforce checks related to [arithmetic operations from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es-expressions-and-statements). |
31
+
|**C++ Core Check Bounds Rules**| These rules enforce the [Bounds profile of the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#probounds-bounds-safety-profile). |
32
+
|**C++ Core Check Class Rules**| These rules enforce checks related to [classes from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c-classes-and-class-hierarchies). |
33
+
|**C++ Core Check Concurrency Rules**| These rules enforce checks related to [concurrency from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cpcon-concurrency). |
34
+
|**C++ Core Check Const Rules**| These rules enforce [const-related checks from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#con-constants-and-immutability). |
35
+
|**C++ Core Check Declaration Rules**| These rules enforce checks related to [declarations from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i-interfaces). |
36
+
|**C++ Core Check Enum Rules**| These rules enforce [enum-related checks from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-enum). |
37
+
|**C++ Core Check Experimental Rules**| These rules collect some experimental checks. Eventually, we expect these checks to be moved to other rulesets or removed completely. |
38
+
|**C++ Core Check Function Rules**| These rules enforce checks related to [functions from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#f-functions). |
39
+
|**C++ Core Check GSL Rules**| These rules enforce checks related to the [Guidelines Support Library from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-gsl). |
40
+
|**C++ Core Check Lifetime Rules**| These rules enforce the [Lifetime profile of the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#prolifetime-lifetime-safety-profile). |
41
+
|**C++ Core Check Owner Pointer Rules**| These rules enforce resource-management checks related to [owner<T> from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management). |
42
+
|**C++ Core Check Raw Pointer Rules**| These rules enforce resource-management checks related to [raw pointers from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management). |
43
+
|**C++ Core Check Rules**| These rules enforce a subset of the checks from the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c-core-guidelines). Use this ruleset to include all of the C++ Core Check rules except the Enum and Experimental rulesets. |
44
+
|**C++ Core Check Shared Pointer Rules**| These rules enforce resource-management checks related to [types with shared pointer semantics from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management). |
45
+
|**C++ Core Check STL Rules**| These rules enforce checks related to the [C++ Standard Template Library (STL) from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-stdlib). |
46
+
|**C++ Core Check Style Rules**| These rules enforce checks related to use of [expressions and statements from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es-expressions-and-statements). |
47
+
|**C++ Core Check Type Rules**| These rules enforce the [Type profile of the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#prosafety-type-safety-profile). |
48
+
|**C++ Core Check Unique Pointer Rules**| These rules enforce resource-management checks related to types with [unique pointer semantics from the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management). |
49
+
|**Concurrency Check Rules**| These rules enforce a set of Win32 concurrency pattern checks in C++. |
50
+
|**Concurrency Rules**| Adds concurrency rules from C++ Core Guidelines to **Concurrency Check Rules**. |
51
+
|**Microsoft Native Minimum Rules**| These rules focus on the most critical problems in your native code, including potential security holes and application crashes. We recommend you include this rule set in any custom rule set you create for your native projects. |
52
+
|**Microsoft Native Recommended Rules**| These rules focus on the most critical and common problems in your native code. These problems include potential security holes and application crashes. We recommend you include this rule set in any custom rule set you create for your native projects. This ruleset is designed to work with Visual Studio Professional edition and higher. It includes all the rules in **Microsoft Native Minimum Rules**. |
53
+
54
+
Visual Studio includes these standard sets of rules for managed code:
55
+
56
+
| Rule Set | Description |
57
+
|--|--|
58
+
|**Microsoft Basic Correctness Rules**| These rules focus on logic errors and common mistakes made in the usage of framework APIs. Include this rule set to expand on the list of warnings reported by the minimum recommended rules. |
59
+
|**Microsoft Basic Design Guideline Rules**| These rules focus on enforcing best practices to make your code easy to understand and use. Include this rule set if your project includes library code or if you want to enforce best practices for easily maintainable code. |
60
+
|**Microsoft Extended Correctness Rules**| These rules expand on the basic correctness rules to maximize the reported logic and framework usage errors. Extra emphasis is placed on specific scenarios such as COM interop and mobile applications. Consider including this rule set if one of these scenarios applies to your project or to find additional problems in your project. |
61
+
|**Microsoft Extended Design Guideline Rules**| These rules expand on the basic design guideline rules to maximize the reported usability and maintainability issues. Extra emphasis is placed on naming guidelines. Consider including this rule set if your project includes library code or if you want to enforce the highest standards for writing maintainable code. |
62
+
|**Microsoft Globalization Rules**| These rules focus on problems that prevent data in your application from displaying correctly when used in different languages, locales, and cultures. Include this rule set if your application is localized and/or globalized. |
63
+
|**Microsoft Managed Minimum Rules**| These rules focus on the most critical problems in your code for which Code Analysis is the most accurate. These rules are small in number and they are intended only to run in limited Visual Studio editions. Use MinimumRecommendedRules.ruleset with other Visual Studio editions. |
64
+
|**Microsoft Managed Recommended Rules**| These rules focus on the most critical problems in your code. These problems include potential security holes, application crashes, and other important logic and design errors. We recommend you include this rule set in any custom rule set you create for your projects. |
65
+
|**Microsoft Mixed (C++ /CLR) Minimum Rules**| These rules focus on the most critical problems in your C++ projects that support the Common Language Runtime. These problems include potential security holes, application crashes, and other important logic and design errors. We recommend you include this rule set in any custom rule set you create for your C++ projects that support the Common Language Runtime. |
66
+
|**Microsoft Mixed (C++ /CLR) Recommended Rules**| These rules focus on the most common and critical problems in your C++ projects that support the Common Language Runtime. These problems include potential security holes, application crashes, and other important logic and design errors. This ruleset is designed for use in the Visual Studio Professional edition and higher. |
67
+
|**Microsoft Security Rules**| This rule set contains all Microsoft security rules. Include this rule set to maximize the number of potential security issues that are reported. |
68
+
69
+
To include every rule:
70
+
71
+
| Rule Set | Description |
72
+
|--|--|
73
+
|**Microsoft All Rules**| This rule set contains all rules. Running this rule set may result in a large number of warnings being reported. Use this rule set to get a comprehensive picture of all issues in your code. It can help you decide which of the more focused rule sets are most appropriate to run for your projects. |
32
74
33
75
## Run code analysis
34
76
35
-
On the Code Analysis page of the Project Properties page, you can configure code analysis to run each time you build your project. You can also run code analysis manually.
77
+
On the **Code Analysis** page of the Project Properties dialog, you can configure code analysis to run each time you build your project. You can also run code analysis manually.
36
78
37
79
To run code analysis on a solution:
38
80
@@ -50,33 +92,21 @@ To run code analysis on a file:
50
92
51
93
1. In the **Build** menu, choose **Run Code Analysis on File** or press **Ctrl+Shift+Alt+F7**.
52
94
53
-
The project or solution is compiled and code analysis runs. Results appear in the Error List.
95
+
The project or solution is compiled and code analysis runs. Results appear in the Error List window.
54
96
55
97
## Analyze and resolve code analysis warnings
56
98
57
-
To analyze a specific warning, choose the title of the warning in the Error List. The warning expands to display additional information about the issue. When possible, code analysis displays the line numbers and analysis logic that led to the warning. For detailed information about the warning, including possible solutions to the issue, choose the warning ID to display its corresponding online help topic.
58
-
59
-
When you select a warning, the line of code that caused the warning is highlighted in the Visual Studio code editor.
60
-
61
-
After you understand the problem, you can resolve it in your code. Then, rerun code analysis to make sure that the warning no longer appears in the Error List, and that your fix has not raised any new warnings.
62
-
63
-
## Suppress code analysis warnings
64
-
65
-
There are times when you might decide not to fix a code analysis warning. You might decide that resolving the warning requires too much recoding in relation to the probability that the issue will arise in any real-world implementation of your code. Or you might believe that the analysis that is used in the warning is inappropriate for the particular context. You can suppress individual warnings so that they no longer appear in the Error List.
66
-
67
-
### To suppress a warning
68
-
69
-
1. If the detailed information is not displayed, choose the title of the warning to expand it.
99
+
The Error List window lists the code analysis warnings found. The results are displayed in a table. If more information is available about a particular warning, the first column contains an expansion control. Choose it to expand the display for additional information about the issue. When possible, code analysis displays the line numbers and analysis logic that led to the warning.
70
100
71
-
1. Choose the **Actions** link at the bottom of the warning.
101
+
For detailed information about the warning, including possible solutions to the issue, choose the warning ID in the Code column to display its corresponding online help article.
72
102
73
-
1. Choose **Suppress Message** and then choose **In Source**.
103
+
Double-click a warning to move the cursor to the line of code that caused the warning in the Visual Studio code editor. Or, press Enter on the selected warning.
74
104
75
-
Suppressing a message inserts `#pragma warning (disable:[warning ID])` that suppresses the warning for the line of code.
105
+
After you understand the problem, you can resolve it in your code. Then, rerun code analysis to make sure that the warning no longer appears in the Error List.
76
106
77
107
## Create work items for code analysis warnings
78
108
79
-
You can use the work item tracking feature to log bugs from within Visual Studio. To use this feature, you must connect to an instance of Team Foundation Server.
109
+
You can use the work item tracking feature to log bugs from within Visual Studio. To use this feature, you must connect to an instance of Azure DevOps Server (formerly, Team Foundation Server).
80
110
81
111
### To create a work item for one or more C/C++ code warnings
82
112
@@ -92,7 +122,7 @@ You can use the work item tracking feature to log bugs from within Visual Studio
92
122
93
123
You can search long lists of warning messages and you can filter warnings in multi-project solutions.
94
124
95
-
-**To filter warnings by title or warning ID**: Enter the keyword in the search box.
125
+
-**To filter warnings by title or warning ID**: Enter the keyword in the Search Error List box.
96
126
97
127
-**To filter warnings by severity**: By default, code analysis messages are assigned a severity of **Warning**. You can assign the severity of one or more messages as **Error** in a custom rule set. On the **Severity** column of the **Error List**, choose the drop-down arrow and then the filter icon. Choose **Warning** or **Error** to display only the messages that are assigned the respective severity. Choose **Select All** to display all messages.
0 commit comments