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
This following procedures show you how to create the sample for [Walkthrough: Analyze C/C++ code for defects](../code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md). The procedures create:
13
+
The following procedures show you how to create the sample for [Walkthrough: Analyze C/C++ code for defects](../code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md). The procedures create:
14
14
15
15
- A Visual Studio solution named *CppDemo*.
16
16
@@ -22,13 +22,15 @@ The procedures also provide the code for the header and *.cpp* files for the sta
22
22
23
23
## Create the CppDemo solution and the CodeDefects project
24
24
25
+
::: moniker range=">=vs-2019"
26
+
25
27
1. Open Visual Studio and select **Create a new project**
26
28
27
29
1. In the **Create a new project** dialog, change the language filter to **C++**.
28
30
29
31
1. Select **Windows Desktop Wizard** and choose the **Next** button.
30
32
31
-
1. On the **Configure your new project** page, in the **Project name** text box, enter **CodeDefects**.
33
+
1. On the **Configure your new project** page, in the **Project name** text box, enter *CodeDefects*.
32
34
33
35
1. In the **Solution name** text box, enter *CppDemo*.
34
36
@@ -40,6 +42,54 @@ The procedures also provide the code for the header and *.cpp* files for the sta
40
42
41
43
1. Choose **OK** to create the solution and project.
42
44
45
+
::: moniker-end
46
+
47
+
::: moniker range="vs-2017"
48
+
49
+
1. Open Visual Studio. On the menu bar, choose **File** > **New** > **Project**.
50
+
51
+
1. In the **New Project** dialog, select **Visual C++** > **Windows Desktop**.
52
+
53
+
1. Select **Windows Desktop Wizard**.
54
+
55
+
1. In the **Name** text box, enter *CodeDefects*.
56
+
57
+
1. In the **Solution name** text box, enter *CppDemo*.
58
+
59
+
1. Choose **OK**.
60
+
61
+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
62
+
63
+
1. Under **Additional options**, select **Empty project**.
64
+
65
+
1. Choose **OK** to create the solution and project.
66
+
67
+
::: moniker-end
68
+
69
+
::: moniker range="vs-2015"
70
+
71
+
1. Open Visual Studio. On the menu bar, choose **File** > **New** > **Project**.
72
+
73
+
1. In the **New Project** dialog, select **Templates** > **Visual C++** > **Win32**.
74
+
75
+
1. Select **Win32 Console Application**.
76
+
77
+
1. In the **Name** text box, enter *CodeDefects*.
78
+
79
+
1. In the **Solution name** text box, enter *CppDemo*.
80
+
81
+
1. Choose **OK**.
82
+
83
+
1. In the **Win32 Application Wizard** dialog, choose the **Next** button.
84
+
85
+
1. Change the **Application type** to **Static library**.
86
+
87
+
1. Under **Additional options**, unselect **Precompiled header**.
88
+
89
+
1. Choose **Finish** to create the solution and project.
90
+
91
+
::: moniker-end
92
+
43
93
## Add the header and source file to the CodeDefects project
44
94
45
95
1. In Solution Explorer, expand **CodeDefects**.
@@ -69,7 +119,7 @@ The procedures also provide the code for the header and *.cpp* files for the sta
69
119
const int ACCOUNT_DOMAIN_LEN = 128;
70
120
```
71
121
72
-
1. In Solution Explorer, right-click to open the context menu for **Source Files**. Choose **Add** > **New Item**.
122
+
1. In Solution Explorer, right-click to open the context menu for **Source Files**. Choose **Add** > **New Item**.
73
123
74
124
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
75
125
@@ -147,6 +197,8 @@ The procedures also provide the code for the header and *.cpp* files for the sta
147
197
148
198
## Add the Annotations project and configure it as a static library
149
199
200
+
::: moniker range=">=vs-2019"
201
+
150
202
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
151
203
152
204
1. In the **Add a new project** dialog box, select **Windows Desktop Wizard**, and then choose the **Next** button.
@@ -159,11 +211,53 @@ The procedures also provide the code for the header and *.cpp* files for the sta
159
211
160
212
1. Choose **OK** to create the project.
161
213
214
+
::: moniker-end
215
+
216
+
::: moniker range="vs-2017"
217
+
218
+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
219
+
220
+
1. In the **Add New Project** dialog, select **Visual C++** > **Windows Desktop**.
221
+
222
+
1. Select **Windows Desktop Wizard**.
223
+
224
+
1. In the **Name** text box, enter *Annotations*, and then choose **OK**.
225
+
226
+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
227
+
228
+
1. Under **Additional options**, select **Empty project**.
229
+
230
+
1. Choose **OK** to create the project.
231
+
232
+
::: moniker-end
233
+
234
+
::: moniker range="vs-2015"
235
+
236
+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
237
+
238
+
1. In the **Add New Project** dialog, select **Visual C++** > **Win32**.
239
+
240
+
1. Select **Win32 Console Application**.
241
+
242
+
1. In the **Name** text box, enter *Annotations*.
243
+
244
+
1. Choose **OK**.
245
+
246
+
1. In the **Win32 Application Wizard** dialog, choose the **Next** button.
247
+
248
+
1. Change the **Application type** to **Static library**.
249
+
250
+
1. Under **Additional options**, unselect **Precompiled header**.
251
+
252
+
1. Choose **Finish** to create the project.
253
+
254
+
::: moniker-end
255
+
162
256
## Add the header file and source file to the Annotations project
163
257
164
258
1. In Solution Explorer, expand **Annotations**.
165
259
166
-
1. Right-click to open the context menu for **Header Files**. Choose **Add** > **New Item**.
260
+
1. Right-click to open the context menu for **Header Files** under **Annotations**. Choose **Add** > **New Item**.
167
261
168
262
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select **Header File (.h)**.
169
263
@@ -188,7 +282,7 @@ The procedures also provide the code for the header and *.cpp* files for the sta
188
282
_Ret_maybenull_ LinkedList* AllocateNode();
189
283
```
190
284
191
-
1. In Solution Explorer, right-click to open the context menu for **Source Files**. Choose **Add** > **New Item**.
285
+
1. In Solution Explorer, right-click to open the context menu for **Source Files** under **Annotations**. Choose **Add** > **New Item**.
192
286
193
287
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
194
288
@@ -227,3 +321,10 @@ The procedures also provide the code for the header and *.cpp* files for the sta
227
321
1. On the menu bar, choose **File** > **Save All**.
228
322
229
323
The solution is now complete and should build without errors.
324
+
325
+
::: moniker range="vs-2017"
326
+
327
+
> [!NOTE]
328
+
> In Visual Studio 2017, you may see a spurious warning `E1097 unknown attribute "no_init_all"` in the IntelliSense engine. This warning may safely be ignored.
Copy file name to clipboardExpand all lines: docs/code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md
+65-6Lines changed: 65 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -21,8 +21,12 @@ In this walkthrough, you'll:
21
21
- A copy of the [CppDemo Sample](../code-quality/demo-sample.md).
22
22
- Basic understanding of C/C++.
23
23
24
+
## Run code analysis on native code
25
+
24
26
### To run code defect analysis on native code
25
27
28
+
::: moniker range=">=vs-2019"
29
+
26
30
1. Open the CppDemo solution in Visual Studio.
27
31
28
32
The CppDemo solution now populates **Solution Explorer**.
@@ -45,6 +49,37 @@ In this walkthrough, you'll:
45
49
46
50
Code analysis warnings are displayed in the **Error List** window.
47
51
52
+
::: moniker-end
53
+
54
+
::: moniker range="<=vs-2017"
55
+
56
+
1. Open the CppDemo solution in Visual Studio.
57
+
58
+
The CppDemo solution now populates **Solution Explorer**.
59
+
60
+
1. On the **Build** menu, choose **Rebuild Solution**.
61
+
62
+
The solution builds without any errors or warnings.
63
+
64
+
> [!NOTE]
65
+
> In Visual Studio 2017, you may see a spurious warning `E1097 unknown attribute "no_init_all"` in the IntelliSense engine. This warning may safely be ignored.
66
+
67
+
1. In **Solution Explorer**, select the CodeDefects project.
68
+
69
+
1. On the **Project** menu, choose **Properties**.
70
+
71
+
The **CodeDefects Property Pages** dialog box is displayed.
72
+
73
+
1. Select the **Code Analysis** property page.
74
+
75
+
1. Select the **Enable Code Analysis on Build** check box. Choose **OK** to save your changes.
76
+
77
+
1. Rebuild the CodeDefects project.
78
+
79
+
Code analysis warnings are displayed in the **Error List** window.
80
+
81
+
::: moniker-end
82
+
48
83
### To analyze code defect warnings
49
84
50
85
1. On the **View** menu, choose **Error List**.
@@ -70,16 +105,20 @@ In this walkthrough, you'll:
70
105
1. Correct this warning by testing for equality. Your code should look similar to the following code:
71
106
72
107
```cpp
73
-
if ((len == ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\'))
108
+
if ((len == ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L'\\'))
74
109
```
75
110
76
-
1. Correct the remaining two C6001 errors in the **Error List** by initializing `i` and `j` to 0.
111
+
1. Correct the remaining C6001 warnings in the **Error List** by initializing `i` and `j` to 0.
77
112
78
113
1. Rebuild the CodeDefects project.
79
114
80
115
The project builds without any warnings or errors.
81
116
82
-
### To correct the source code annotation warnings in annotation.c
117
+
## Correct source code annotation warnings
118
+
119
+
### To enable the source code annotation warnings in annotation.c
120
+
121
+
::: moniker range=">=vs-2019"
83
122
84
123
1. In Solution Explorer, select the Annotations project.
85
124
@@ -91,6 +130,24 @@ In this walkthrough, you'll:
91
130
92
131
1. Change the **Enable Code Analysis on Build** property to **Yes**. Choose **OK** to save your changes.
93
132
133
+
::: moniker-end
134
+
135
+
::: moniker range="<=vs-2017"
136
+
137
+
1. In Solution Explorer, select the Annotations project.
138
+
139
+
1. On the **Project** menu, choose **Properties**.
140
+
141
+
The **Annotations Property Pages** dialog box is displayed.
142
+
143
+
1. Select the **Code Analysis** property page.
144
+
145
+
1. Select the **Enable Code Analysis on Build** check box. Choose **OK** to save your changes.
146
+
147
+
::: moniker-end
148
+
149
+
### To correct the source code annotation warnings in annotation.c
150
+
94
151
1. Rebuild the Annotations project.
95
152
96
153
1. On the **Build** menu, choose **Run Code Analysis on Annotations**.
@@ -119,6 +176,8 @@ In this walkthrough, you'll:
119
176
120
177
The project builds without any warnings or errors.
121
178
179
+
## Use source code annotation to discover more issues
180
+
122
181
### To use source code annotation
123
182
124
183
1. Annotate formal parameters and return value of the function `AddTail` to indicate the pointer values may be null:
@@ -127,13 +186,13 @@ In this walkthrough, you'll:
127
186
_Ret_maybenull_ LinkedList* AddTail(_Maybenull_ LinkedList* node, int value)
128
187
```
129
188
130
-
1. On the **Build** menu, choose **Run Code Analysis on Annotations**.
189
+
1. On the **Build** menu, choose **Run Code Analysis on Solution**.
131
190
132
191
1. In the **Error List**, double-click the following warning:
133
192
134
193
C6011: Dereferencing NULL pointer 'node'.
135
194
136
-
This warning indicates that the node passed into the function might be null. Double-click it to go to the location in the editor where the warning was raised.
195
+
This warning indicates that the node passed into the function might be null.
137
196
138
197
1. To correct this warning, use an 'if' statement at the beginning of the function to test the passed in value. Your code should resemble the following code:
139
198
@@ -144,7 +203,7 @@ In this walkthrough, you'll:
144
203
}
145
204
```
146
205
147
-
1. On the **Build** menu, choose **Run Code Analysis on Annotations**.
206
+
1. On the **Build** menu, choose **Run Code Analysis on Solution**.
148
207
149
208
The project now builds without any warnings or errors.
0 commit comments