[clang-tools-extra] r296864 - [clang-tidy] Format code around applied fixes

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 3 03:16:35 PST 2017


Author: alexfh
Date: Fri Mar  3 05:16:34 2017
New Revision: 296864

URL: http://llvm.org/viewvc/llvm-project?rev=296864&view=rev
Log:
[clang-tidy] Format code around applied fixes

Summary:
Add -format option (disabled by default for now) to trigger formatting
of replacements.

Reviewers: ioeric

Reviewed By: ioeric

Subscribers: kimgr, malcolm.parsons, JDevlieghere, cfe-commits

Differential Revision: https://reviews.llvm.org/D30564

Added:
    clang-tools-extra/trunk/test/clang-tidy/readability-braces-around-statements-format.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
    clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/index.rst
    clang-tools-extra/trunk/test/clang-tidy/clean-up-code.cpp

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=296864&r1=296863&r2=296864&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Fri Mar  3 05:16:34 2017
@@ -183,7 +183,6 @@ public:
   }
 
   void Finish() {
-    // FIXME: Run clang-format on changes.
     if (ApplyFixes && TotalFixes > 0) {
       Rewriter Rewrite(SourceMgr, LangOpts);
       for (const auto &FileAndReplacements : FileReplacements) {
@@ -197,19 +196,28 @@ public:
           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<Replacements> CleanReplacements =
+        llvm::Expected<tooling::Replacements> 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)) {
+        if (llvm::Expected<tooling::Replacements> FormattedReplacements =
+                format::formatReplacements(Code, *Replacements, *Style)) {
+          Replacements = std::move(FormattedReplacements);
+          if (!Replacements)
+            llvm_unreachable("!Replacements");
+        } else {
+          llvm::errs() << llvm::toString(FormattedReplacements.takeError())
+                       << ". Skipping formatting.\n";
+        }
+        if (!tooling::applyAllReplacements(Replacements.get(), Rewrite)) {
           llvm::errs() << "Can't apply replacements for file " << File << "\n";
         }
       }

Modified: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp?rev=296864&r1=296863&r2=296864&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Fri Mar  3 05:16:34 2017
@@ -60,7 +60,7 @@ appearance in the list. Globs without '-
 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,20 @@ well.
 )"),
                                cl::init(false), cl::cat(ClangTidyCategory));
 
-static cl::opt<std::string> FormatStyle("style", cl::desc(R"(
-Fallback style for reformatting after inserting fixes
-if there is no clang-format config file found.
+static cl::opt<std::string> FormatStyle("format-style", cl::desc(R"(
+Style for formatting code around applied fixes:
+  - 'none' (default) turns off formatting
+  - 'file' (literally 'file', not a placeholder)
+    uses .clang-format file in the closest parent
+    directory
+  - '{ <json> }' specifies options inline, e.g.
+    -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
+  - 'llvm', 'google', 'webkit', 'mozilla'
+See clang-format documentation for the up-to-date
+information about formatting styles and options.
 )"),
-                                        cl::init("llvm"),
-                                        cl::cat(ClangTidyCategory));
+                                   cl::init("none"),
+                                   cl::cat(ClangTidyCategory));
 
 static cl::opt<bool> ListChecks("list-checks", cl::desc(R"(
 List all enabled checks and exit. Use with
@@ -189,7 +197,7 @@ code with clang-apply-replacements.
                                         cl::cat(ClangTidyCategory));
 
 static cl::opt<bool> 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.

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=296864&r1=296863&r2=296864&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Fri Mar  3 05:16:34 2017
@@ -87,6 +87,9 @@ Improvements to clang-tidy
   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
 -----------------------------
 

Modified: clang-tools-extra/trunk/docs/clang-tidy/index.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/index.rst?rev=296864&r1=296863&r2=296864&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/index.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/index.rst Fri Mar  3 05:16:34 2017
@@ -158,6 +158,17 @@ An overview of all the command-line opti
                                    errors were found. If compiler errors have
                                    attached fix-its, clang-tidy will apply them as
                                    well.
+    -format-style=<string>       -
+                                   Style for formatting code around applied fixes:
+                                     - 'none' (default) turns off formatting
+                                     - 'file' (literally 'file', not a placeholder)
+                                       uses .clang-format file in the closest parent
+                                       directory
+                                     - '{ <json> }' specifies options inline, e.g.
+                                       -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
+                                     - 'llvm', 'google', 'webkit', 'mozilla'
+                                   See clang-format documentation for the up-to-date
+                                   information about formatting styles and options.
     -header-filter=<string>      -
                                    Regular expression matching the names of the
                                    headers to output diagnostics from. Diagnostics
@@ -179,6 +190,11 @@ An overview of all the command-line opti
                                    List all enabled checks and exit. Use with
                                    -checks=* to list all available checks.
     -p=<string>                  - 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=<string>              -
                                    Fallback style for reformatting after inserting fixes
                                    if there is no clang-format config file found.
@@ -558,10 +574,10 @@ and once with the directive prefix set t
 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

Modified: clang-tools-extra/trunk/test/clang-tidy/clean-up-code.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/clean-up-code.cpp?rev=296864&r1=296863&r2=296864&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/clean-up-code.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/clean-up-code.cpp Fri Mar  3 05:16:34 2017
@@ -1,4 +1,6 @@
 // RUN: %check_clang_tidy %s misc-unused-using-decls %t
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t -- -format-style=none --
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t -- -format-style=llvm --
 namespace a { class A {}; }
 namespace b {
 using a::A;

Added: clang-tools-extra/trunk/test/clang-tidy/readability-braces-around-statements-format.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-braces-around-statements-format.cpp?rev=296864&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-braces-around-statements-format.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-braces-around-statements-format.cpp Fri Mar  3 05:16:34 2017
@@ -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: {{^}}   }
+}




More information about the cfe-commits mailing list