Index: clang-tidy/ClangTidy.cpp =================================================================== --- clang-tidy/ClangTidy.cpp +++ clang-tidy/ClangTidy.cpp @@ -183,7 +183,6 @@ } void Finish() { - // FIXME: Run clang-format on changes. if (ApplyFixes && TotalFixes > 0) { Rewriter Rewrite(SourceMgr, LangOpts); for (const auto &FileAndReplacements : FileReplacements) { @@ -197,19 +196,24 @@ continue; } StringRef Code = Buffer.get()->getBuffer(); - auto Style = format::getStyle("file", File, FormatStyle); + auto Style = format::getStyle(FormatStyle, File, "none"); if (!Style) { llvm::errs() << llvm::toString(Style.takeError()) << "\n"; continue; } - llvm::Expected CleanReplacements = + llvm::Expected Replacements = format::cleanupAroundReplacements(Code, FileAndReplacements.second, *Style); - if (!CleanReplacements) { - llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n"; + if (!Replacements) { + llvm::errs() << llvm::toString(Replacements.takeError()) << "\n"; continue; } - if (!tooling::applyAllReplacements(CleanReplacements.get(), Rewrite)) { + Replacements = format::formatReplacements(Code, *Replacements, *Style); + if (!Replacements) { + llvm::errs() << llvm::toString(Replacements.takeError()) << "\n"; + continue; + } + if (!tooling::applyAllReplacements(Replacements.get(), Rewrite)) { llvm::errs() << "Can't apply replacements for file " << File << "\n"; } } Index: clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tidy/tool/ClangTidyMain.cpp +++ clang-tidy/tool/ClangTidyMain.cpp @@ -60,7 +60,7 @@ prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled -checks. This option's value is appended to the +checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), @@ -120,12 +120,12 @@ )"), cl::init(false), cl::cat(ClangTidyCategory)); -static cl::opt FormatStyle("style", cl::desc(R"( -Fallback style for reformatting after inserting fixes -if there is no clang-format config file found. +static cl::opt FormatStyle("format-style", cl::desc(R"( +Style for formatting code around applied fixes. +'none' turns off formatting. )"), - cl::init("llvm"), - cl::cat(ClangTidyCategory)); + cl::init("none"), + cl::cat(ClangTidyCategory)); static cl::opt ListChecks("list-checks", cl::desc(R"( List all enabled checks and exit. Use with @@ -189,7 +189,7 @@ cl::cat(ClangTidyCategory)); static cl::opt Quiet("quiet", cl::desc(R"( -Run clang-tidy in quiet mode. This suppresses +Run clang-tidy in quiet mode. This suppresses printing statistics about ignored warnings and warnings treated as errors if the respective options are specified. Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -87,6 +87,9 @@ Finds and replaces explicit calls to the constructor in a return statement by a braced initializer list so that the return type is not needlessly repeated. +- Support clang-formatting of the code around applied fixes (``-format-style`` + command-line option). + Improvements to include-fixer ----------------------------- Index: docs/clang-tidy/index.rst =================================================================== --- docs/clang-tidy/index.rst +++ docs/clang-tidy/index.rst @@ -158,6 +158,9 @@ errors were found. If compiler errors have attached fix-its, clang-tidy will apply them as well. + -format-style= - + Style for formatting code around applied fixes. + 'none' turns off formatting. -header-filter= - Regular expression matching the names of the headers to output diagnostics from. Diagnostics @@ -179,6 +182,11 @@ List all enabled checks and exit. Use with -checks=* to list all available checks. -p= - Build path + -quiet - + Run clang-tidy in quiet mode. This suppresses + printing statistics about ignored warnings and + warnings treated as errors if the respective + options are specified. -style= - Fallback style for reformatting after inserting fixes if there is no clang-format config file found. @@ -558,10 +566,10 @@ against the fixed code (i.e., the code after generated fix-its are applied). In particular, ``CHECK-FIXES:`` can be used to check that code was not modified by fix-its, by checking that it is present -unchanged in the fixed code. The full set of `FileCheck`_ directives +unchanged in the fixed code. The full set of `FileCheck`_ directives is available (e.g., ``CHECK-MESSAGES-SAME:``, ``CHECK-MESSAGES-NOT:``), though typically the basic ``CHECK`` forms (``CHECK-MESSAGES`` and ``CHECK-FIXES``) -are sufficient for clang-tidy tests. Note that the `FileCheck`_ +are sufficient for clang-tidy tests. Note that the `FileCheck`_ documentation mostly assumes the default prefix (``CHECK``), and hence describes the directive as ``CHECK:``, ``CHECK-SAME:``, ``CHECK-NOT:``, etc. Replace ``CHECK`` by either ``CHECK-FIXES`` or ``CHECK-MESSAGES`` for Index: test/clang-tidy/readability-braces-around-statements-format.cpp =================================================================== --- /dev/null +++ test/clang-tidy/readability-braces-around-statements-format.cpp @@ -0,0 +1,33 @@ +// RUN: %check_clang_tidy %s readability-braces-around-statements %t -- -format-style="{IndentWidth: 3}" -- + +void do_something(const char *) {} + +bool cond(const char *) { + return false; +} + +void test() { + if (cond("if0") /*comment*/) do_something("same-line"); + // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: statement should be inside braces + // CHECK-FIXES: {{^}} if (cond("if0") /*comment*/) {{{$}} + // CHECK-FIXES-NEXT: {{^}} do_something("same-line");{{$}} + // CHECK-FIXES-NEXT: {{^}} }{{$}} + + if (1) while (2) if (3) for (;;) do ; while(false) /**/;/**/ + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should be inside braces + // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces + // CHECK-MESSAGES: :[[@LINE-3]]:26: warning: statement should be inside braces + // CHECK-MESSAGES: :[[@LINE-4]]:35: warning: statement should be inside braces + // CHECK-MESSAGES: :[[@LINE-5]]:38: warning: statement should be inside braces + // CHECK-FIXES: {{^}} if (1) {{{$}} + // CHECK-FIXES-NEXT: {{^}} while (2) { + // CHECK-FIXES-NEXT: {{^}} if (3) { + // CHECK-FIXES-NEXT: {{^}} for (;;) { + // CHECK-FIXES-NEXT: {{^}} do { + // CHECK-FIXES-NEXT: {{^}} ; + // CHECK-FIXES-NEXT: {{^}} } while (false) /**/; /**/ + // CHECK-FIXES-NEXT: {{^}} } + // CHECK-FIXES-NEXT: {{^}} } + // CHECK-FIXES-NEXT: {{^}} } + // CHECK-FIXES-NEXT: {{^}} } +}