The latest insiders release of the C++ extension is here, bringing clang-tidy support to VS Code! Clang-tidy is a clang-based C++ linter tool that detects common errors in your code, like style violations and bugs that can be deduced via static analysis. Clang-tidy integration was one of our top asks on GitHub, so we’re excited to announce that it’s ready for you to try!
Getting started
Do I need to install clang-tidy?
Nope! Clang-tidy now comes bundled with the C++ extension. But if you already have clang-tidy installed (and it’s on your environment’s path), the C++ extension will use that one instead. You can point the C++ extension to any clang-tidy binary by editing the C++ extension’s Clang Tidy: Path setting.
How do I run clang-tidy in VS Code?
To manually run clang-tidy, open the Command Palette (Ctrl + Shift + P) and type “Run Code Analysis.” You can run clang-tidy on the active file, on all open files, or on the entire workspace.
If there are certain workspace files or folders that you don’t want to run clang-tidy on, add them to the Clang Tidy: Exclude setting (C_Cpp.codeAnalysis.exclude in settings.json).
You can also set clang-tidy to run automatically whenever you open or save a file. To turn on automatic code analysis, open your project settings by selecting “Preferences: Open Settings (UI)” from the Command Palette (Ctrl + Shift + P).
Then search for “code analysis” to find all the clang-tidy settings and set Clang Tidy: Enabled to true.
Note: You can set clang-tidy settings globally (in the User tab) or at the workspace level.
You can check if clang-tidy is running by looking for the fire icon in the blue status bar at the bottom of the window.
To pause or cancel clang-tidy, click on the fire icon and then choose an option from the drop down.
How do I configure clang-tidy checks and check options?
If you have a .clang-tidy configuration file in your project directory, the C++ extension will honor the checks and options defined in that file. If you have multiple .clang-tidy configuration files in your workspace, clang-tidy will use the configuration file closest to the source file by searching up the path in its parent directories. Alternatively, you can specify a clang-tidy configuration with the Clang Tidy: Config setting. Clang Tidy: Config accepts checks and check options as a YAML/JSON-formatted string.
If a source file doesn’t have a .clang-tidy configuration file in any of its parent directories, and the Clang Tidy: Config property is left empty, then the fallback configuration (defined in C_Cpp.codeAnalysis.clangTidy.fallbackConfig) will be used for that file.
You can enable and disable more checks by using the Clang Tidy > Checks: Enabled and Clang Tidy > Checks: Disabled settings. The checks defined in these settings are run in addition to the checks in the .clang-tidy file.
Selecting Add Item shows a list of all clang-tidy checks.
You can add as many checks as you’d like to both the Clang Tidy > Checks: Enabled and Clang Tidy > Checks: Disabled settings.
How do I pass command line args to clang-tidy?
If you typically pass command line options to clang-tidy, you can specify those in the Clang Tidy: Args property. The Clang Tidy: Args setting takes precedence over the equivalent Clang Tidy settings in the editor (such as Clang Tidy > Checks: Enabled and Clang Tidy > Checks: Disabled).
Viewing check results in the editor
The results of clang-tidy checks (warnings and errors) are displayed in the Problems panel and as squiggles beneath relevant sections of code.
Clicking on the problem in the Problems panel takes you to the issue in your source file. To clear the code analysis squiggles, click on the Quick Actions lightbulb, and then select Clear code analysis squiggles.
Clang-tidy settings
Here’s a list of new VS Code settings to help you configure clang-tidy for your workspace.
Setting name | Type | Default value | Description |
C_Cpp.codeAnalysis.clangTidy.enabled
|
Boolean | False | When true, clang-tidy will be enabled and run automatically if C_Cpp.codeAnalysis.runAutomatically is set to true
|
C_Cpp.codeAnalysis.runAutomatically | Boolean | True | When true, runs clang-tidy on file open and file save.
|
C_Cpp.codeAnalysis.clangTidy.path | String | Null (empty) | The full path to the clang-tidy executable. |
C_Cpp.codeAnalysis.clangTidy.checks.enabled | Array of strings | Null (empty) | List of enabled clang-tidy checks. The values are appended to the Checks in a .clang-tidy file or C_Cpp › Code Analysis › Clang Tidy: Config. |
C_Cpp.codeAnalysis.clangTidy.checks.disabled | Array of strings | Null (empty) | List of disabled clang-tidy checks. The values are appended to the Checks in a .clang-tidy file or C_Cpp › Code Analysis › Clang Tidy: Config, if any. |
C_Cpp.codeAnalysis.clangTidy.config | String | Null (empty) | Specifies a clang-tidy configuration in YAML/JSON format: {Checks: ‘-*,clang-analyzer-*’, CheckOptions: [{key: x, value: y}]}. When empty, any found .clang-tidy files will be used. |
C_Cpp.codeAnalysis.clangTidy.fallbackConfig | String | Null (empty) | Specifies a clang-tidy configuration in YAML/JSON format to be used as a fallback when C_Cpp › Code Analysis › Clang Tidy: Config is not set and no .clang-tidy file is found. |
C_Cpp.codeAnalysis.clangTidy.args | Array of strings | Null (empty) | Command line arguments to pass to clang-tidy. These take precedence over the equivalent C_Cpp.codeAnalysis.clangTidy.* settings. |
C_Cpp.codeAnalysis.exclude | String | Null (empty) | Configure glob patterns for excluding folders and files for code analysis. |
C_Cpp.codeAnalysis.clangTidy.headerFilter | String | Null (empty) | A POSIX extended regular expression (ERE) matching the names of the headers to output diagnostics from. It overrides the HeaderFilterRegex option in a .clang-tidy file, if any. |
C_Cpp.codeAnalysis.maxConcurrentThreads
(User level only) |
Int | Null (empty) | The maximum number of concurrent threads to use for code analysis. The default of null (empty) uses half the value inherited from C_Cpp: Max Concurrent Threads. |
C_Cpp.codeAnalysis.maxMemory
(User level only) |
Int | Null (empty) | Fewer code analysis processes will run concurrently after this memory usage (in MB) is exceeded. The default of null (empty) uses the value inherited from C_Cpp: Max Memory. |
Coming soon
Hang tight! Applying suggested fixes in the editor is coming soon.
What do you think?
Download the C++ extension for Visual Studio Code today and join our Insiders program to access this initial release of clang-tidy integration. To join our Insiders program, go to File > Preferences > Settings and under Extensions > C/C++, change the “C_Cpp: Update Channel” to “Insiders.” Then under Features > Extensions, enable “Extensions: Auto Update.”
If you run into any issues, or have any suggestions, please report them in the Issues section of our GitHub repository.
We can be reached via the comments below or in email at visualcpp@microsoft.com. You can also find our team on Twitter at @VisualC.
This is not just related to VSCode but to VS too: Is there a trivial way to disable code analysis on headers included from vcpkg and generally to exclude third party libraries and headers even such that are included in the source itself?