Skip to content

Commit 88e76b9

Browse files
authored
Merge pull request #2807 from MicrosoftDocs/master
4/13/2020 AM Publish
2 parents 0d61502 + 6399f86 commit 88e76b9

File tree

6 files changed

+281
-104
lines changed

6 files changed

+281
-104
lines changed

docs/code-quality/c26820.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ helpviewer_keywords: ["C26820"]
77
---
88
# C26820
99

10-
> Assigning by value when a const-reference would suffice, use const T& instead (p.9).
10+
> Assigning by value when a const-reference would suffice, use const auto& instead (p.9).
1111
1212
For more information, see [P.9: Don't waste time or space](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#p9-dont-waste-time-or-space) in the C++ Core Guidelines.
1313

@@ -31,13 +31,15 @@ This check covers non-obvious and easy-to-miss behavior when assigning a referen
3131
This sample shows a variable definition that makes a potentially expensive copy when assigned a reference:
3232

3333
```cpp
34-
MyClass& ref = ...;
35-
auto var = ref; // C26820 (`var` takes a copy of the object referred to by `ref`)
34+
const Object& MyClass::getRef() { ... }
35+
...
36+
auto ref = myclass.getRef(); // C26820 (`ref` takes a copy of the returned object)
3637
```
3738
3839
To resolve this issue, declare the variable by using `const auto&` instead:
3940
4041
```cpp
41-
MyClass& ref = ...;
42-
const auto& var = ref; // OK
42+
const Object& MyClass::getRef() { ... }
43+
...
44+
const auto& ref = myclass.getRef(); // OK
4345
```

docs/code-quality/demo-sample.md

Lines changed: 164 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Sample C++ project for code analysis
3-
ms.date: 11/04/2016
3+
description: "How to create a sample solution for use in the code analysis walkthrough for Microsoft C++ in Visual Studio."
4+
ms.date: 04/14/2020
45
ms.topic: sample
56
helpviewer_keywords:
67
- "demo sample [Visual Studio ALM]"
@@ -9,47 +10,97 @@ ms.assetid: 09e1b9f7-5916-4ed6-a001-5c2d7e710682
910
---
1011
# Sample C++ project for code analysis
1112

12-
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:
1314

14-
- A Visual Studio solution named CppDemo.
15+
- A Visual Studio solution named *CppDemo*.
1516

16-
- A static library project named CodeDefects.
17+
- A static library project named *CodeDefects*.
1718

18-
- A static library project named Annotations.
19+
- A static library project named *Annotations*.
1920

2021
The procedures also provide the code for the header and *.cpp* files for the static libraries.
2122

2223
## Create the CppDemo solution and the CodeDefects project
2324

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

26-
1. Change language filter to **C++**
29+
1. In the **Create a new project** dialog, change the language filter to **C++**.
30+
31+
1. Select **Windows Desktop Wizard** and choose the **Next** button.
32+
33+
1. On the **Configure your new project** page, in the **Project name** text box, enter *CodeDefects*.
34+
35+
1. In the **Solution name** text box, enter *CppDemo*.
36+
37+
1. Choose **Create**.
38+
39+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
40+
41+
1. Under **Additional options**, select **Empty project**.
42+
43+
1. Choose **OK** to create the solution and project.
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*.
2758

28-
1. Select **Empty Project** and click **Next**
59+
1. Choose **OK**.
2960

30-
1. In the **Project Name** text box, type **CodeDefects**
61+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
3162

32-
1. In the **Solution name** text box, type **CppDemo**
63+
1. Under **Additional options**, select **Empty project**.
3364

34-
1. Click **Create**
65+
1. Choose **OK** to create the solution and project.
3566

36-
## Configure the CodeDefects project as a static library
67+
::: moniker-end
3768

38-
1. In Solution Explorer, right-click **CodeDefects** and then click **Properties**.
69+
::: moniker range="vs-2015"
3970

40-
1. Expand **Configuration Properties** and then click **General**.
71+
1. Open Visual Studio. On the menu bar, choose **File** > **New** > **Project**.
4172

42-
1. In the **General** list, change **Configuration Type**, to **Static library (.lib)**.
73+
1. In the **New Project** dialog, select **Templates** > **Visual C++** > **Win32**.
4374

44-
1. In the **Advanced** list, change **Target File Extension** to **.lib**
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
4592

4693
## Add the header and source file to the CodeDefects project
4794

48-
1. In Solution Explorer, expand **CodeDefects**, right-click **Header Files**, click **Add**, and then click **New Item**.
95+
1. In Solution Explorer, expand **CodeDefects**.
4996

50-
1. In the **Add New Item** dialog box, click **Code**, and then click **Header File (.h)**.
97+
1. Right-click to open the context menu for **Header Files**. Choose **Add** > **New Item**.
5198

52-
1. In the **Name** box, type **Bug.h** and then click **Add**.
99+
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select **Header File (.h)**.
100+
101+
1. In the **Name** edit box, enter *Bug.h*, and then choose the **Add** button.
102+
103+
1. In the edit window for *Bug.h*, select and delete the contents.
53104

54105
1. Copy the following code and paste it into the *Bug.h* file in the editor.
55106

@@ -58,9 +109,8 @@ The procedures also provide the code for the header and *.cpp* files for the sta
58109

59110
#include <windows.h>
60111

61-
// These functions are consumed by the sample
62-
// but are not defined. This project cannot be linked!
63-
bool CheckDomain(LPCTSTR);
112+
// Function prototypes
113+
bool CheckDomain(wchar_t const *);
64114
HRESULT ReadUserAccount();
65115

66116
// These constants define the common sizes of the
@@ -69,24 +119,34 @@ 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 **Source Files**, point to **New**, and then click **New Item**.
122+
1. In Solution Explorer, right-click to open the context menu for **Source Files**. Choose **Add** > **New Item**.
73123

74-
1. In the **Add New Item** dialog box, click **C++ File (.cpp)**
124+
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
75125

76-
1. In the **Name** box, type **Bug.cpp** and then click **Add**.
126+
1. In the **Name** edit box, enter *Bug.cpp*, and then choose the **Add** button.
77127

78128
1. Copy the following code and paste it into the *Bug.cpp* file in the editor.
79129

80130
```cpp
81131
#include "Bug.h"
82132

83133
// the user account
84-
TCHAR g_userAccount[USER_ACCOUNT_LEN] = {};
134+
wchar_t g_userAccount[USER_ACCOUNT_LEN] = { L"domain\\user" };
85135
int len = 0;
86136

137+
bool CheckDomain(wchar_t const* domain)
138+
{
139+
return (wcsnlen_s(domain, USER_ACCOUNT_LEN) > 0);
140+
}
141+
142+
HRESULT ReadUserAccount()
143+
{
144+
return S_OK;
145+
}
146+
87147
bool ProcessDomain()
88148
{
89-
TCHAR* domain = new TCHAR[ACCOUNT_DOMAIN_LEN];
149+
wchar_t* domain = new wchar_t[ACCOUNT_DOMAIN_LEN];
90150
// ReadUserAccount gets a 'domain\user' input from
91151
//the user into the global 'g_userAccount'
92152
if (ReadUserAccount())
@@ -95,22 +155,22 @@ The procedures also provide the code for the header and *.cpp* files for the sta
95155
// character onto the 'domain' buffer
96156
for (len = 0; (len < ACCOUNT_DOMAIN_LEN) && (g_userAccount[len] != L'\0'); len++)
97157
{
98-
if (g_userAccount[len] == '\\')
158+
if (g_userAccount[len] == L'\\')
99159
{
100160
// Stops copying on the domain and user separator ('\')
101161
break;
102162
}
103163
domain[len] = g_userAccount[len];
104164
}
105-
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\'))
165+
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L'\\'))
106166
{
107167
// '\' was not found. Invalid domain\user string.
108168
delete[] domain;
109169
return false;
110170
}
111171
else
112172
{
113-
domain[len] = '\0';
173+
domain[len] = L'\0';
114174
}
115175
// Process domain string
116176
bool result = CheckDomain(domain);
@@ -133,31 +193,77 @@ The procedures also provide the code for the header and *.cpp* files for the sta
133193
}
134194
```
135195

136-
1. Click the **File** menu, and then click **Save All**.
196+
1. On the menu bar, choose **File** > **Save All**.
137197

138198
## Add the Annotations project and configure it as a static library
139199

140-
1. In Solution Explorer, click **CppDemo**, point to **Add**, and then click **New Project**.
200+
::: moniker range=">=vs-2019"
201+
202+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
203+
204+
1. In the **Add a new project** dialog box, select **Windows Desktop Wizard**, and then choose the **Next** button.
205+
206+
1. On the **Configure your new project** page, in the **Project name** text box, enter *Annotations*, and then choose **Create**.
207+
208+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
209+
210+
1. Under **Additional options**, select **Empty project**.
211+
212+
1. Choose **OK** to create the project.
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**.
141229

142-
1. In the **Add a new project** dialog box, Change language filter to **C++** and select **Empty Project** then click **Next**.
230+
1. Choose **OK** to create the project.
143231

144-
1. In the **Project name** text box, type **Annotations**, and then click **Create**.
232+
::: moniker-end
145233

146-
1. In Solution Explorer, right-click **Annotations** and then click **Properties**.
234+
::: moniker range="vs-2015"
147235

148-
1. Expand **Configuration Properties** and then click **General**.
236+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
149237

150-
1. In the **General** list, change **Configuration Type**, to and then click **Static library (.lib)**.
238+
1. In the **Add New Project** dialog, select **Visual C++** > **Win32**.
151239

152-
1. In the **Advanced** list, select the text in the column next to **Target File extension**, and then type **.lib**.
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
153255

154256
## Add the header file and source file to the Annotations project
155257

156-
1. In Solution Explorer, expand **Annotations**, right-click **Header Files**, click **Add**, and then click **New Item**.
258+
1. In Solution Explorer, expand **Annotations**.
259+
260+
1. Right-click to open the context menu for **Header Files** under **Annotations**. Choose **Add** > **New Item**.
157261

158-
1. In the **Add New Item** dialog box, click **Header File (.h)**.
262+
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select **Header File (.h)**.
159263

160-
1. In the **Name** box, type **annotations.h** and then click **Add**.
264+
1. In the **Name** edit box, enter *annotations.h*, and then choose the **Add** button.
265+
266+
1. In the edit window for *annotations.h*, select and delete the contents.
161267

162268
1. Copy the following code and paste it into the *annotations.h* file in the editor.
163269

@@ -176,16 +282,23 @@ The procedures also provide the code for the header and *.cpp* files for the sta
176282
_Ret_maybenull_ LinkedList* AllocateNode();
177283
```
178284

179-
1. In Solution Explorer, right-click **Source Files**, point to **New**, and then click **New Item**.
285+
1. In Solution Explorer, right-click to open the context menu for **Source Files** under **Annotations**. Choose **Add** > **New Item**.
180286

181-
1. In the **Add New Item** dialog box, click **Code** and then click **C++ File (.cpp)**
287+
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
182288

183-
1. In the **Name** box, type **annotations.cpp** and then click **Add**.
289+
1. In the **Name** edit box, enter *annotations.cpp*, and then choose the **Add** button.
184290

185291
1. Copy the following code and paste it into the *annotations.cpp* file in the editor.
186292

187293
```cpp
188294
#include "annotations.h"
295+
#include <malloc.h>
296+
297+
_Ret_maybenull_ LinkedList* AllocateNode()
298+
{
299+
LinkedList* result = static_cast<LinkedList*>(malloc(sizeof(LinkedList)));
300+
return result;
301+
}
189302

190303
LinkedList* AddTail(LinkedList* node, int value)
191304
{
@@ -205,6 +318,13 @@ The procedures also provide the code for the header and *.cpp* files for the sta
205318
}
206319
```
207320

208-
1. Click the **File** menu, and then click **Save All**.
321+
1. On the menu bar, choose **File** > **Save All**.
209322

210323
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. You can safely ignore this warning.
329+
330+
::: moniker-end

0 commit comments

Comments
 (0)