Skip to content

Commit 2c82c53

Browse files
authored
Merge pull request #5186 from MicrosoftDocs/main
2/5/2024 AM Publish
2 parents 3c30349 + ea24951 commit 2c82c53

6 files changed

+89
-1
lines changed

docs/build/reference/execution-charset-set-execution-character-set.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ You can use the **`/execution-charset`** option to specify an execution characte
2828

2929
By default, Visual Studio detects a byte-order mark to determine if the source file is in an encoded Unicode format, for example, UTF-16 or UTF-8. If no byte-order mark is found, it assumes that the source file is encoded in the current user code page, unless you used the **`/source-charset`** or **`/utf-8`** option to specify a character set name or code page. Visual Studio allows you to save your C++ source code in any of several character encodings. For information about source and execution character sets, see [Character sets](../../cpp/character-sets.md) in the language documentation.
3030

31-
If you want to set both the source character set and the execution character set to UTF-8, you can use the **`/utf-8*`** compiler option as a shortcut. It's equivalent to **`/source-charset:utf-8 /execution-charset:utf-8`** on the command line. Any of these options also enables the **`/validate-charset`** option by default.
31+
If you want to set both the source character set and the execution character set to UTF-8, you can use the **`/utf-8`** compiler option as a shortcut. It's equivalent to **`/source-charset:utf-8 /execution-charset:utf-8`** on the command line. Any of these options also enables the **`/validate-charset`** option by default.
3232

3333
### To set this compiler option in the Visual Studio development environment
3434

docs/ide/media/linter-settings.png

109 KB
Loading
Loading
Loading

docs/ide/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ items:
44
items:
55
- name: Read and understand C++ code
66
href: ../ide/read-and-understand-code-cpp.md
7+
- name: Visualize macro expansions
8+
href: ../ide/visualize-macro-expansion.md
79
- name: Edit and refactor C++ code
810
href: ../ide/writing-and-refactoring-code-cpp.md
911
- name: Navigate C++ code

docs/ide/visualize-macro-expansion.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: "Visualize C/C++ macro expansion"
3+
description: "Learn how to use Visual Studio to visualize C/C++ macro expansion."
4+
ms.date: 02/02/2024
5+
ms.topic: "how-to"
6+
f1_keywords: ["macro expansion", "macro visualization"]
7+
helpviewer_keywords: ["macro expansion", "macro visualization"]
8+
---
9+
# Visualize C/C++ macro expansion
10+
11+
Long macros can be difficult to read. Visual Studio can now expand C and C++ macros: you can get a copy on the clipboard of what the expanded macro looks like, replace the macro inline with its expansion, and step-by-step expand a macro so you can see what it looks like at each stage of expansion. This article allows you to experiment with these features.
12+
13+
## Prerequisites
14+
15+
- Visual Studio version 17.5 or later
16+
17+
### Create the sample
18+
19+
1. Start Visual Studio 2022, version 17.5 or later, and create a C++ Console app.
20+
1. Replace the default code with:
21+
22+
```cpp
23+
#include <iostream>
24+
25+
#define MASS 10.0
26+
#define ACCELERATION 20.0
27+
#define SPEED 5.0
28+
#define TIME 2.0
29+
#define DISTANCE() (SPEED * TIME)
30+
#define FORCE()(MASS * ACCELERATION)
31+
#define WORK()(FORCE() * DISTANCE())
32+
#define POWER()(WORK()/TIME)
33+
34+
int main()
35+
{
36+
std::cout << "Distance: " << DISTANCE() << std::endl;
37+
std::cout << "Force: " << FORCE() << std::endl;
38+
std::cout << "Work: " << WORK() << std::endl;
39+
std::cout << "Power: " << POWER() << std::endl;
40+
}
41+
```
42+
43+
## Copy an expanded macro
44+
45+
You can inspect a macro's expanded value, even when several preprocessor steps are involved, by using the following steps:
46+
47+
1. Place the cursor on the `POWER` a macro in the sample.
48+
1. As you hover over the macro, options appear to **Copy**, **Expand Inline**, **Visualize Expansion**, and **Search Online**:
49+
50+
:::image type="complex" source="media/visual-studio-2022-hover-macro.png" alt-text="Screenshot of the macro window, showing the POWER macro expansion.":::
51+
The macro window is open on POWER to show that it expands to (((10.0 * 20.0) * (5.0 * 2.0)) / 2.0). Options to copy, expand inline, visual expansion, and search online appear at the bottom of the window.
52+
:::image-end:::
53+
54+
1. Choose **Copy**.
55+
1. Create a comment following the `POWER` line and choose paste (`Ctrl+V`). The expansion of the macro, as a comment near your macro, looks like: ```// (((10.0 * 20.0)* (5.0 * 2.0)) / 2.0)```.
56+
57+
## Expand a macro inline
58+
59+
Use the following steps to expand a macro inline, which replaces the macro with its expansion:
60+
61+
1. Place the cursor on the `POWER` macro in the previous example.
62+
1. As you hover over the macro, options appear to **Copy**, **Expand Inline**, **Visualize Expansion**, and **Search Online**
63+
1. Choose **Expand Inline**. The `POWER()` macro is replaced with its expanded value: ```std::cout << "Power: " << (((10.0 * 20.0) * (5.0 * 2.0)) / 2.0) << std::endl;```
64+
65+
## Visualize macro expansion
66+
67+
You can expand a macro one step at a time. This is useful when there are nested macros and you want to see the expansion step-by-step. To visualize the macro expansion for the `WORK` macro, use the following steps:
68+
69+
1. Place the cursor on the `WORK` macro in the previous example.
70+
1. As you hover over the macro, options appear to **Copy**, **Expand Inline**, **Visualize Expansion**, and **Search Online**.
71+
1. Choose **Visualize Expansion**.
72+
1. The macro expansion window appears. The first expansion of the `WORK` macro is visible: `(FORCE() * DISTANCE())`:
73+
74+
:::image type="complex" source="media/visual-studio-2022-work-macro-expansion.png" alt-text="Screenshot of the macro expansion window, which allows you to step through the WORK macro expansion one step at a time.":::
75+
The macro visualization window is open on FORCE to show that it initially expands to (FORCE()*DISTANCE()). There are single angle brackets in the window for moving forwards and backwards a single expansion at a time. The double angle brackets fully expand or fully undo the macro expansion.
76+
:::image-end:::
77+
78+
1. Click the right angle bracket to expand the `FORCE` macro. The window now shows the `FORCE` macro expansion: `(MASS * ACCELERATION) * DISTANCE()`.
79+
1. Click the right angle bracket to expand another step. The window now shows the `FORCE` macro expansion: `((10.0 * ACCELERATION) * DISTANCE())`.
80+
81+
Continue to expand the macro to see the full expansion of the `WORK` macro, which is: ```((10.0 * 20.0) * (5.0 * 2.0))```.
82+
You can use the double angle brackets to fully expand the macro, or to reverse the expansion to the first level of expansion.
83+
84+
## See also
85+
86+
[View UE macros in Visual Studio](/visualstudio/gamedev/unreal/get-started/vs-tools-unreal-quickstart#view-ue-macros-in-visual-studio)

0 commit comments

Comments
 (0)