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