Skip to content

Repo sync for protected branch #4979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 8, 2024
Merged
60 changes: 60 additions & 0 deletions docs/code-quality/c6392.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
description: "Learn more about: Warning C6392"
title: Warning C6392
ms.date: 03/06/2024
f1_keywords: ["C6392", "STREAM_OUTPUT_VOID_PTR", "__STREAM_OUTPUT_VOID_PTR"]
helpviewer_keywords: ["C6392"]
---
# Warning C6392

> This expression writes the value of the pointer to the stream. If this is intentional, add an explicit cast to 'void *'

This rule was added in Visual Studio 2022 17.8.

## Remarks

C++ supports wide character streams such as `std::wostringstream`, and nonwide character streams such as `std::ostringstream`. Trying to print a wide string to a nonwide stream calls the `void*` overload of `operator<<`. This overload prints the address of the wide string instead of the value.

Code analysis name: `STREAM_OUTPUT_VOID_PTR`

## Example

The following code snippet prints the value of the pointer to the standard output instead of the string `"Pear"`:

```cpp
#include <iostream>

int main() {
std::cout << L"Pear\n"; // Warning: C6392
}
```

There are multiple ways to fix this error. If printing the pointer value is unintended, use a nonwide string:

```cpp
#include <iostream>

int main() {
std::cout << "Pear\n"; // No warning.
}
```

Alternatively, use a wide stream:

```cpp
#include <iostream>

int main() {
std::wcout << L"Pear\n"; // No warning.
}
```

If the behavior is intentional, make the intention explicit and silence the warning by using an explicit cast:

```cpp
#include <iostream>

int main() {
std::cout << static_cast<void*>(L"Pear\n"); // No warning.
}
```
2 changes: 2 additions & 0 deletions docs/code-quality/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ items:
href: ../code-quality/c6389.md
- name: Warning C6390
href: ../code-quality/c6390.md
- name: Warning C6392
href: ../code-quality/c6392.md
- name: Warning C6393
href: ../code-quality/c6393.md
- name: Warning C6394
Expand Down
26 changes: 13 additions & 13 deletions docs/ide/visualize-macro-expansion.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: "Visualize C/C++ macro expansion"
description: "Learn how to use Visual Studio to visualize C/C++ macro expansion."
ms.date: 02/02/2024
ms.date: 03/07/2024
ms.topic: "how-to"
f1_keywords: ["macro expansion", "macro visualization"]
helpviewer_keywords: ["macro expansion", "macro visualization"]
---
# Visualize C/C++ macro expansion

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.
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 to see what it looks like at each stage of expansion. In this article, you experiment with all of these features.

## Prerequisites

Expand All @@ -33,42 +33,42 @@ Long macros can be difficult to read. Visual Studio can now expand C and C++ mac

int main()
{
std::cout << "Distance: " << DISTANCE() << std::endl;
std::cout << "Force: " << FORCE() << std::endl;
std::cout << "Work: " << WORK() << std::endl;
std::cout << "Power: " << POWER() << std::endl;
std::cout << "Distance: " << DISTANCE() << std::endl;
std::cout << "Force: " << FORCE() << std::endl;
std::cout << "Work: " << WORK() << std::endl;
std::cout << "Power: " << POWER() << std::endl;
}
```

## Copy an expanded macro

You can inspect a macro's expanded value, even when several preprocessor steps are involved, by using the following steps:

1. Place the cursor on the `POWER` a macro in the sample.
1. Place the cursor on the `POWER` macro inside `main()` in the example.
1. As you hover over the macro, options appear to **Copy**, **Expand Inline**, **Visualize Expansion**, and **Search Online**:

:::image type="complex" source="media/visual-studio-2022-hover-macro.png" alt-text="Screenshot of the macro window, showing the POWER macro expansion.":::
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.
:::image-end:::

1. Choose **Copy**.
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)```.
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).` The keyboard shortcut for this action is CTRL+M, CTRL+C.

## Expand a macro inline

Use the following steps to expand a macro inline, which replaces the macro with its expansion:

1. Place the cursor on the `POWER` macro in the previous example.
1. Place the cursor on the `POWER` macro inside `main()` in the example.
1. As you hover over the macro, options appear to **Copy**, **Expand Inline**, **Visualize Expansion**, and **Search Online**
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;```
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;```. The keyboard shortcut for this action is CTRL+M, CTRL+I.

## Visualize macro expansion

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:

1. Place the cursor on the `WORK` macro in the previous example.
1. Place the cursor on the `WORK` macro inside `main()` in the example.
1. As you hover over the macro, options appear to **Copy**, **Expand Inline**, **Visualize Expansion**, and **Search Online**.
1. Choose **Visualize Expansion**.
1. Choose **Visualize Expansion**. The keyboard shortcut for this action is CTRL+M followed by CTRL+V.
1. The macro expansion window appears. The first expansion of the `WORK` macro is visible: `(FORCE() * DISTANCE())`:

:::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.":::
Expand Down