Skip to content

Repo sync for protected branch #4958

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 15 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"searchScope": [
"C++"
],
"feedback_system": "GitHub",
"feedback_system": "Standard",
"feedback_github_repo": "MicrosoftDocs/cpp-docs",
"feedback_product_url": "https://developercommunity.visualstudio.com/cpp/",
"feedback_help_link_url": "https://learn.microsoft.com/en-us/answers/tags/314/cpp",
Expand Down
106 changes: 106 additions & 0 deletions docs/ide/include-diagnostics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: "C++ Include Diagnostics"
description: "Learn how to use #include Diagnostics in Visual Studio to analyze how often something from an include file is used and how an #include impacts build time."
ms.date: 10/10/2023
ms.topic: "how-to"
f1_keywords: ["include diagnostics"]
helpviewer_keywords: ["include diagnostics"]
---
# C++ Include Diagnostics in Visual Studio

Starting with Visual Studio 17.8, Visual Studio helps you analyze your C++ `#include` files:

- Displays how often, and where, something from each header file is used.
- Displays the build time for each `#include` file--which helps you identify opportunities to optimize your build time.

## Enable C++ Include Diagnostics and CodeLens

The C++ Include Diagnostics feature is off by default. To turn it on, right-click in the code editor to bring up the context menu, and choose **Include Directives** > **Turn #include Diagnostics On**.

:::image type="complex" source="media/vs2022-enable-include-diagnostics.png" alt-text="A screenshot of the context menu that appears when you right-click in the code editor area.":::
The context menu shows the include directives option highlighted, which reveals two options: Sort # include directives and turn # include diagnostics on.
:::image-end:::

Information about your `#include` files is displayed via CodeLens, which is off by default. To turn on the relevant CodeLens settings, navigate to **Tools** > **Options** > **Text Editor** > **All Languages** > **CodeLens** and confirm both **Show C++ #include references** and **Show C++ compilation times** are enabled.

:::image type="complex" source="media/vs2022-enable-code-lens-for-includes.png" alt-text="A screenshot of the options window.":::
The options window is set to Text Editor > All Languages > CodeLens. The Show C++ # include references and Show C++ compilation times options are highlighted.
:::image-end:::

## View `#include` references

To try out Include Diagnostics, create a new C++ console project. Replace the contents of the main `.cpp` file with the following code:

```cpp
#include <iostream>
#include <vector>

// a function that takes a vector of integers and prints them out
void print(std::vector<int> &vec)
{
for (int i : vec)
{
std::cout << i << std::endl;
}
std::cout << std::endl;
}

// a function that takes a vector of integers and adds 10 to each element of the vector and store the result in a new vector
std::vector<int> add10(std::vector<int>& vec)
{
std::vector<int> newVec;
for (int i : vec)
{
newVec.push_back(i + 10);
}
return newVec;
}

int main()
{
std::vector<int> vec = { 7, 5, 16, 8 };

print(vec);
auto newVec = add10(vec);
print(newVec);
}
```

When C++ Include Diagnostics are turned on, the number of times code from a header file is referenced in the current code file is displayed above the header file. It looks like this for the previous code sample:

```cpp
6 references
#include <iostream>
5 references
#include <vector>
```

In the code editor, select **5 references** above `#include <vector>` and a summary of locations where code from `<vector>` is used in this file is displayed:

:::image type="complex" source="media/visual-studio-2022-codelens-include-references.png" alt-text="A screenshot of the C++ Include Diagnostics context window showing where code from the vector header file is used.":::
The C++ Include Diagnostics context window shows that there are five places in the code where code from the vector header file is used in the current code file. For example, it's used twice on the definition of the add10 function, as a return value and parameter. It's used on line 17 in the declaration of newVec, and so on.
:::image-end:::

Select an item to go to its location in your code.

## View `#include` build time

To see the build time for each file you `#include`, first build using Build Insights.

Turn on Build Insights from the main menu bar by selecting **Build** > **Run Build Insights on Solution** > **Build**. After the build completes, a window appears to list the build times for the various files that are compiled. Return to the source code window, and the build time for each `#include` file is displayed in CodeLens. It looks similar to this:

```cpp
6 references | Build: 0.3560s
#include <iostream>
5 references | Build 0.0360s
#include <vector>
```

If you have an `#include` directive that is used infrequently, but significantly impacts your compile time, this tool can help you identify it.

In this article, you've seen how to turn on C++ Include Diagnostics and CodeLens, and how to use C++ Include Diagnostics to analyze how often something from an include file is used and how an `#include` impacts build time.

## See also

[C/C++ Include Cleanup overview](include-cleanup-overview.md)\
[Include Cleanup messages](include-cleanup-messages.md)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/ide/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ items:
href: ../ide/include-cleanup-config.md
- name: Include Cleanup messages
href: ../ide/include-cleanup-messages.md
- name: C++ Include Diagnostics
href: include-diagnostics.md
- name: Set your C++ coding preferences
href: ../ide/how-to-set-preferences.md
- name: Collaborate using Live Share for C++
Expand Down
2 changes: 1 addition & 1 deletion docs/overview/visual-cpp-language-conformance.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ For details on conformance improvements, see [C++ conformance improvements in Vi
| &emsp;[`P1099R5 Using enum`](https://wg21.link/P1099R5) | VS 2019 16.4 <sup>[20](#note_20)</sup> |
| &emsp;[`P1186R3 When do you actually use <=>`](https://wg21.link/P1186R3) | VS 2019 16.4 <sup>[20](#note_20)</sup> |
| &emsp;[`P1630R1 Spaceship needs a tune-up`](https://wg21.link/P1630R1) | VS 2019 16.4 <sup>[20](#note_20)</sup> |
| &emsp;[`P0306R4 Adding __VA_OPT__ for comma omission and comma deletion`](https://wg21.link/P0306R4) | VS 2019 16.5 <sup>[20](#note_20)</sup> |
| &emsp;[`P0306R4 Adding __VA_OPT__ for comma omission and comma deletion`](https://wg21.link/P0306R4) | VS 2019 16.5. To provide better backward compatibility, `__VA_OPT__` is enabled under `/Zc:preprocessor` across all language versions. |
| &emsp;[`P0614R1 Range-based for-loops with initializers`](https://wg21.link/P0614R1) | VS 2019 16.5 <sup>[20](#note_20)</sup> |
| &emsp;[`P0683R1 Default member initializers for bit-fields`](https://wg21.link/P0683R1) | VS 2019 16.5 <sup>[20](#note_20)</sup> |
| &emsp;[`P1002R1 try-catch blocks in constexpr functions`](https://wg21.link/P1002R1) | VS 2019 16.5 <sup>[20](#note_20)</sup> |
Expand Down
6 changes: 4 additions & 2 deletions docs/overview/what-s-new-for-visual-cpp-in-visual-studio.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "What's new for C++ in Visual Studio"
description: "The new features and fixes in the Microsoft C/C++ compiler and tools in Visual Studio."
ms.date: 02/07/2024
ms.date: 02/21/2024
ms.service: "visual-cpp"
ms.subservice: "ide"
ms.custom: intro-whats-new
Expand Down Expand Up @@ -37,7 +37,9 @@ Briefly, some of the new features are:
`<mdspan>` per [P0009R18](https://wg21.link/P0009R18) and subsequent wording changes that were applied to the C++23 Standard.
Also, `format()` can format pointers per [P2510R3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2510r3.pdf).

For a comprehensive summary of new C++ features in Visual Studio 17.9, see [What’s New for C++ Developers in Visual Studio 2022 17.9](https://devblogs.microsoft.com/cppblog/whats-new-for-cpp-developers-in-visual-studio-2022-17-9/).
For a comprehensive summary of new C++ features in Visual Studio 17.9, see [MSVC Backend Updates since Visual Studio 2022 version 17.3](https://devblogs.microsoft.com/cppblog/msvc-backend-updates-since-visual-studio-2022-version-17-3/).

For a summary of C++ compiler backend improvements since Visual Studio 17.3 until now, see [What's new in the Visual C++ compiler backend](https://devblogs.microsoft.com/cppblog/whats-new-in-the-visual-cpp-compiler-backend/).

For a summary of new features in the Visual Studio 17.9 IDE, see [Visual Studio 2022 version 17.9 Release Notes](/visualstudio/releases/2022/release-notes).

Expand Down