Abdullah  Kozey

Abdullah Kozey

1626258120

Customized Warning Levels and Code Analysis for External Headers

If you have tried to keep your code clean by selecting strict compiler warning levels or code analysis rulesets, you likely know how frustrating it can be to see these warnings for headers that are not part of your project. To alleviate this, we’ve made it easy to mark headers as external to your project in the latest preview Visual Studio 2019. This is something we have been working on for a while with help and feedback from the community. Some of you may remember our previous post, Broken Warnings Theory, when we added experimental external header support to the Microsoft C++ Compiler. I’m happy to announce that, thanks to this feedback, external headers are now officially supported by the compiler, code analysis, and integrated into the IDE.

These external headers can have their own compiler warning level, code analysis, and template diagnostics settings. This leaves you free to choose stricter settings for your project’s code to enforce code quality without getting bogged down with warnings from headers that are beyond your control.

Adding External Headers to Your Projects

You can find a new “External Include Directories” property for your projects under “VC++ Directories” which can be used to designate any include directory as containing external headers. This external include directory will be added to the include search path as normal, but every header that in that directory or a subdirectory of it will be treated as external. From a compilation standpoint, you won’t notice any differences, but you can choose a different set of warning levels and other analysis settings for headers in these directories.

Customizing Warning Levels External Headers

You can customize the way external headers are treated in the project properties “C/C++ > External Includes” tab.

Customizing Code Analysis Settings for External Headers

Being able to mark certain headers as external to the project will make Code Analysis much easier to use. The examples below show some of the ways you can leverage this new feature to get the most out of Code Analysis.

Migration from undocumented CAExcludePath to /external:* and /analyze:external* options
Using /analyze:external:ruleset with /external:*
Bonus: Even Better Code Analysis Performance

#c++ #code analysis #code

Customized Warning Levels and Code Analysis for External Headers