{"id":18485,"date":"2018-03-13T11:13:51","date_gmt":"2018-03-13T18:13:51","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=18485"},"modified":"2022-08-11T18:43:47","modified_gmt":"2022-08-11T18:43:47","slug":"clangformat-support-in-visual-studio-2017-15-7-preview-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/clangformat-support-in-visual-studio-2017-15-7-preview-1\/","title":{"rendered":"ClangFormat Support in Visual Studio 2017"},"content":{"rendered":"<p><div class=\"alert alert-info\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Content outdated<\/strong><\/p> For up-to-date documentation see <a class=\"Hyperlink SCXW24361506 BCX8\" href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/reference\/options-text-editor-c-cpp-formatting?view=vs-2022\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW24361506 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW24361506 BCX8\" data-ccp-charstyle=\"Hyperlink\">Options, Text Editor, C\/C++, Formatting &#8211; Visual Studio (Windows)<\/span><\/span><\/a>.<\/div>Visual Studio 2017 15.7 Preview 1 comes with built-in <a href=\"https:\/\/clang.llvm.org\/docs\/ClangFormat.html\" target=\"_blank\" rel=\"noopener\">ClangFormat <\/a>support for C++ developers. Specifically, we ship version 5.0 of the clang-format.exe. ClangFormat is a utility that makes it easy to style and format your code based on a set of rules that can be configured in a .clang-format or _clang-format file. This file should be present in your source code directory tree, and applies formatting conventions to all code files at the same level or in any sub-folders. It is easily shareable via source control, so you can enforce coding conventions across your whole development team.<\/p>\n<p>Note that Visual Studio also supports <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/create-portable-custom-editor-options\" target=\"_blank\" rel=\"noopener\">EditorConfig<\/a>, which works in a similar way. ClangFormat, however, has a <a href=\"https:\/\/clang.llvm.org\/docs\/ClangFormatStyleOptions.html\" target=\"_blank\" rel=\"noopener\">much larger variety of style options<\/a> than EditorConfig, including some very C++ specific rules that can be set, and it is already used by C++ developers today.<\/p>\n<p><figure id=\"attachment_18495\" aria-labelledby=\"figcaption_attachment_18495\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/ClangFormat-File.png\"><img decoding=\"async\" class=\"size-full wp-image-18495\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/ClangFormat-File.png\" alt=\".clang-format file example\" width=\"454\" height=\"362\" \/><\/a><figcaption id=\"figcaption_attachment_18495\" class=\"wp-caption-text\">.clang-format file example<\/figcaption><\/figure><\/p>\n<p>You can use multiple ClangFormat files to apply different styling rules at different locations in your codebase, if desired, by placing files in different folders. Any given code file will be styled by the ClangFormat file closest to it in the directory tree. We run the ClangFormat utility in the background to do the formatting, so you get all of ClangFormat\u2019s features right away.<\/p>\n<p>We also provide a way to enforce the built-in formatting conventions that ClangFormat ships with (LLVM, Google, Chromium, Mozilla, or WebKit) without requiring a separate ClangFormat file. This is the same as having the file there with the <em>-style<\/em> switch set to one of these conventions. This can be configured in Tools &gt; Options.<\/p>\n<p>Using the .clang-format file in the screenshot above, here is a before and after representation of a code sample when <em>Format Document<\/em> is invoked on it:<\/p>\n<p><figure id=\"attachment_18505\" aria-labelledby=\"figcaption_attachment_18505\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Code-before.png\"><img decoding=\"async\" class=\"size-full wp-image-18505\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Code-before.png\" alt=\"\" width=\"1280\" height=\"679\" \/><\/a><figcaption id=\"figcaption_attachment_18505\" class=\"wp-caption-text\">Code before the Format Document operation<\/figcaption><\/figure><\/p>\n<p><figure id=\"attachment_18515\" aria-labelledby=\"figcaption_attachment_18515\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/code-after.png\"><img decoding=\"async\" class=\"size-full wp-image-18515\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/code-after.png\" alt=\"\" width=\"1280\" height=\"680\" \/><\/a><figcaption id=\"figcaption_attachment_18515\" class=\"wp-caption-text\">Code after the Format Document operation<\/figcaption><\/figure><\/p>\n<p>Some of the noticeable changes here include the maximum column width (now set to 50), and the presence of the opening curly brace on the same line as the header preceding it.<\/p>\n<h3>How to get started<\/h3>\n<p>If you already have a .clang-format or _clang-format file in your codebase, you will notice Visual Studio uses it for formatting immediately, as soon as you make an edit in your code that would trigger a formatting operation. ClangFormat also runs if you run the <strong>Format Document<\/strong> (Ctrl+K, Ctrl+D) or <strong>Format Selection<\/strong> (Ctrl + K, Ctrl + F) commands from the Edit menu or keyboard shortcuts manually in Visual Studio.<\/p>\n<p>If you don\u2019t already have a sample file to get started with, there is a handy <a href=\"https:\/\/clangformat.com\/\" target=\"_blank\" rel=\"noopener\">website <\/a>that features an interactive .clang-format file builder and guide to ClangFormat styling options in general. For the complete list of ClangFormat style options, visit the <a href=\"https:\/\/clang.llvm.org\/docs\/ClangFormatStyleOptions.html\" target=\"_blank\" rel=\"noopener\">official documentation<\/a>.<\/p>\n<h3>Tools &gt; Options configuration<\/h3>\n<p>There are a number of configurations you can make in Tools &gt; Options to control your ClangFormat experience. You can find them at <em>Tools &gt; Options &gt; Text Editor &gt; C\/C++ &gt; Formatting<\/em>.<\/p>\n<p><figure id=\"attachment_18525\" aria-labelledby=\"figcaption_attachment_18525\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/ClangFormat-Options.png\"><img decoding=\"async\" class=\"size-full wp-image-18525\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/ClangFormat-Options.png\" alt=\"ClangFormat Options\" width=\"744\" height=\"487\" \/><\/a><figcaption id=\"figcaption_attachment_18525\" class=\"wp-caption-text\">ClangFormat settings in Tools &gt; Options<\/figcaption><\/figure><\/p>\n<p>By default, Visual Studio will use any existing ClangFormat file in your source tree for formatting operations. To turn this off, you can uncheck <em>Enable ClangFormat support<\/em>. By default, ClangFormat will also emulate Visual Studio formatting, reacting to characters like braces ({}) and semicolons to format as you type. You can change this to have it only use ClangFormat for explicit <em>Format Document<\/em> and <em>Format Selection<\/em> commands by choosing <em>Run ClangFormat only for manually invoked formatting commands<\/em>.<\/p>\n<p>The Default formatting style allows you to control your formatting experience if you do not have a .clang-format or _clang-format file in your source tree. We will always prefer that file if it exists, but if we cannot find it, we will use the formatting convention selected in that option. By default, Visual Studio is selected, meaning we do what Visual Studio usually does for formatting and do not run ClangFormat at all. However, you can also specify LLVM, Google, Chromium, Mozilla, or WebKit coding conventions as well. These function the same as having a ClangFormat file with its style property set to one of these options, with no other alterations, so in this case we\u2019re running ClangFormat with just that setting on. So, if you\u2019re using one of these default styles and are not planning on deviating from its rules, make the selection in Tools &gt; Options and you\u2019re good to go without the ClangFormat file.<\/p>\n<h3>Give us feedback<\/h3>\n<p>This feature is currently out in Preview. Please try it out and let us know if you run into any issues by reporting them via <em>Help &gt; Send Feedback &gt; Report A Problem<\/em> from inside the Visual Studio IDE. You can view active issues, comment on them and upvote them on <a href=\"http:\/\/developercommunity.visualstudio.com\/index.html\" target=\"_blank\" rel=\"noopener\">Developer Community<\/a>.<\/p>\n<p><strong>Post Changelog<\/strong>\n&#8211; March 14, 2018: added note mentioning what version of clang-format.exe we ship.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2017 15.7 Preview 1 comes with built-in ClangFormat support for C++ developers. Specifically, we ship version 5.0 of the clang-format.exe. ClangFormat is a utility that makes it easy to style and format your code based on a set of rules that can be configured in a .clang-format or _clang-format file. This file should [&hellip;]<\/p>\n","protected":false},"author":1063,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-18485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Visual Studio 2017 15.7 Preview 1 comes with built-in ClangFormat support for C++ developers. Specifically, we ship version 5.0 of the clang-format.exe. ClangFormat is a utility that makes it easy to style and format your code based on a set of rules that can be configured in a .clang-format or _clang-format file. This file should [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/18485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/1063"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=18485"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/18485\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=18485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=18485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=18485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}