[clang] 240e29c - [clang-format][NFC] Turn on some code-changing options one by one

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 15 19:48:34 PST 2022


Author: Owen Pan
Date: 2022-12-15T19:48:26-08:00
New Revision: 240e29c5015d246de7fb5e4421aa93042fada59b

URL: https://github.com/llvm/llvm-project/commit/240e29c5015d246de7fb5e4421aa93042fada59b
DIFF: https://github.com/llvm/llvm-project/commit/240e29c5015d246de7fb5e4421aa93042fada59b.diff

LOG: [clang-format][NFC] Turn on some code-changing options one by one

For the code-changing options InsertBraces, RemoveBracesLLVM, and
RemoveSemicolon, turn the option on only when running the token
analyzer pass for it. This improves the run-time and avoids
interference from other options.

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

Added: 
    

Modified: 
    clang/lib/Format/Format.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c1e382a681c2f..c553a5bf2a3e9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1907,9 +1907,7 @@ namespace {
 class BracesInserter : public TokenAnalyzer {
 public:
   BracesInserter(const Environment &Env, const FormatStyle &Style)
-      : TokenAnalyzer(Env, Style) {
-    this->Style.RemoveBracesLLVM = false;
-  }
+      : TokenAnalyzer(Env, Style) {}
 
   std::pair<tooling::Replacements, unsigned>
   analyze(TokenAnnotator &Annotator,
@@ -1962,9 +1960,7 @@ class BracesInserter : public TokenAnalyzer {
 class BracesRemover : public TokenAnalyzer {
 public:
   BracesRemover(const Environment &Env, const FormatStyle &Style)
-      : TokenAnalyzer(Env, Style) {
-    this->Style.InsertBraces = false;
-  }
+      : TokenAnalyzer(Env, Style) {}
 
   std::pair<tooling::Replacements, unsigned>
   analyze(TokenAnnotator &Annotator,
@@ -3347,6 +3343,9 @@ reformat(const FormatStyle &Style, StringRef Code,
   FormatStyle Expanded = Style;
   expandPresetsBraceWrapping(Expanded);
   expandPresetsSpaceBeforeParens(Expanded);
+  Expanded.InsertBraces = false;
+  Expanded.RemoveBracesLLVM = false;
+  Expanded.RemoveSemicolon = false;
   switch (Expanded.RequiresClausePosition) {
   case FormatStyle::RCPS_SingleLine:
   case FormatStyle::RCPS_WithPreceding:
@@ -3403,20 +3402,26 @@ reformat(const FormatStyle &Style, StringRef Code,
     }
 
     if (Style.InsertBraces) {
+      FormatStyle S = Expanded;
+      S.InsertBraces = true;
       Passes.emplace_back([&](const Environment &Env) {
-        return BracesInserter(Env, Expanded).process(/*SkipAnnotation=*/true);
+        return BracesInserter(Env, S).process(/*SkipAnnotation=*/true);
       });
     }
 
     if (Style.RemoveBracesLLVM) {
+      FormatStyle S = Expanded;
+      S.RemoveBracesLLVM = true;
       Passes.emplace_back([&](const Environment &Env) {
-        return BracesRemover(Env, Expanded).process(/*SkipAnnotation=*/true);
+        return BracesRemover(Env, S).process(/*SkipAnnotation=*/true);
       });
     }
 
     if (Style.RemoveSemicolon) {
+      FormatStyle S = Expanded;
+      S.RemoveSemicolon = true;
       Passes.emplace_back([&](const Environment &Env) {
-        return SemiRemover(Env, Expanded).process(/*SkipAnnotation=*/true);
+        return SemiRemover(Env, S).process(/*SkipAnnotation=*/true);
       });
     }
 


        


More information about the cfe-commits mailing list