Skip to content

Commit 25bc16e

Browse files
author
Colin Robertson
committed
Fix for 2017 and 2015, too
1 parent 48554a2 commit 25bc16e

File tree

2 files changed

+171
-11
lines changed

2 files changed

+171
-11
lines changed

docs/code-quality/demo-sample.md

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ms.assetid: 09e1b9f7-5916-4ed6-a001-5c2d7e710682
1010
---
1111
# Sample C++ project for code analysis
1212

13-
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:
1414

1515
- A Visual Studio solution named *CppDemo*.
1616

@@ -22,13 +22,15 @@ The procedures also provide the code for the header and *.cpp* files for the sta
2222

2323
## Create the CppDemo solution and the CodeDefects project
2424

25+
::: moniker range=">=vs-2019"
26+
2527
1. Open Visual Studio and select **Create a new project**
2628

2729
1. In the **Create a new project** dialog, change the language filter to **C++**.
2830

2931
1. Select **Windows Desktop Wizard** and choose the **Next** button.
3032

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*.
3234

3335
1. In the **Solution name** text box, enter *CppDemo*.
3436

@@ -40,6 +42,54 @@ The procedures also provide the code for the header and *.cpp* files for the sta
4042

4143
1. Choose **OK** to create the solution and project.
4244

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+
4393
## Add the header and source file to the CodeDefects project
4494

4595
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
69119
const int ACCOUNT_DOMAIN_LEN = 128;
70120
```
71121

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**.
73123

74124
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
75125

@@ -147,6 +197,8 @@ The procedures also provide the code for the header and *.cpp* files for the sta
147197

148198
## Add the Annotations project and configure it as a static library
149199

200+
::: moniker range=">=vs-2019"
201+
150202
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
151203

152204
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
159211

160212
1. Choose **OK** to create the project.
161213

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+
162256
## Add the header file and source file to the Annotations project
163257

164258
1. In Solution Explorer, expand **Annotations**.
165259

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**.
167261

168262
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select **Header File (.h)**.
169263

@@ -188,7 +282,7 @@ The procedures also provide the code for the header and *.cpp* files for the sta
188282
_Ret_maybenull_ LinkedList* AllocateNode();
189283
```
190284

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**.
192286

193287
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
194288

@@ -227,3 +321,10 @@ The procedures also provide the code for the header and *.cpp* files for the sta
227321
1. On the menu bar, choose **File** > **Save All**.
228322

229323
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.
329+
330+
::: moniker-end

docs/code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ In this walkthrough, you'll:
2121
- A copy of the [CppDemo Sample](../code-quality/demo-sample.md).
2222
- Basic understanding of C/C++.
2323

24+
## Run code analysis on native code
25+
2426
### To run code defect analysis on native code
2527

28+
::: moniker range=">=vs-2019"
29+
2630
1. Open the CppDemo solution in Visual Studio.
2731

2832
The CppDemo solution now populates **Solution Explorer**.
@@ -45,6 +49,37 @@ In this walkthrough, you'll:
4549

4650
Code analysis warnings are displayed in the **Error List** window.
4751

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+
4883
### To analyze code defect warnings
4984

5085
1. On the **View** menu, choose **Error List**.
@@ -70,16 +105,20 @@ In this walkthrough, you'll:
70105
1. Correct this warning by testing for equality. Your code should look similar to the following code:
71106

72107
```cpp
73-
if ((len == ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\'))
108+
if ((len == ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L'\\'))
74109
```
75110

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.
77112

78113
1. Rebuild the CodeDefects project.
79114

80115
The project builds without any warnings or errors.
81116

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"
83122

84123
1. In Solution Explorer, select the Annotations project.
85124

@@ -91,6 +130,24 @@ In this walkthrough, you'll:
91130

92131
1. Change the **Enable Code Analysis on Build** property to **Yes**. Choose **OK** to save your changes.
93132

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+
94151
1. Rebuild the Annotations project.
95152

96153
1. On the **Build** menu, choose **Run Code Analysis on Annotations**.
@@ -119,6 +176,8 @@ In this walkthrough, you'll:
119176
120177
The project builds without any warnings or errors.
121178
179+
## Use source code annotation to discover more issues
180+
122181
### To use source code annotation
123182
124183
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:
127186
_Ret_maybenull_ LinkedList* AddTail(_Maybenull_ LinkedList* node, int value)
128187
```
129188

130-
1. On the **Build** menu, choose **Run Code Analysis on Annotations**.
189+
1. On the **Build** menu, choose **Run Code Analysis on Solution**.
131190

132191
1. In the **Error List**, double-click the following warning:
133192

134193
C6011: Dereferencing NULL pointer 'node'.
135194

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.
137196

138197
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:
139198

@@ -144,7 +203,7 @@ In this walkthrough, you'll:
144203
}
145204
```
146205
147-
1. On the **Build** menu, choose **Run Code Analysis on Annotations**.
206+
1. On the **Build** menu, choose **Run Code Analysis on Solution**.
148207
149208
The project now builds without any warnings or errors.
150209

0 commit comments

Comments
 (0)