[clang] 9f616a4 - [clang-format][NFC] Group QualifierAlignment with other C++ passes

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 11 14:18:23 PST 2022


Author: owenca
Date: 2022-03-11T14:18:00-08:00
New Revision: 9f616a467fc710f084b7e57812a2ed64c214c2c6

URL: https://github.com/llvm/llvm-project/commit/9f616a467fc710f084b7e57812a2ed64c214c2c6
DIFF: https://github.com/llvm/llvm-project/commit/9f616a467fc710f084b7e57812a2ed64c214c2c6.diff

LOG: [clang-format][NFC] Group QualifierAlignment with other C++ passes

Also increases the initial size of Passes to 8 and move the
definition of BracesInserter up.

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

Added: 
    

Modified: 
    clang/lib/Format/Format.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 9e3eaf72e121e..4f9320f2b34e5 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1784,9 +1784,9 @@ FormatStyle::GetLanguageStyle(FormatStyle::LanguageKind Language) const {
 
 namespace {
 
-class BracesRemover : public TokenAnalyzer {
+class BracesInserter : public TokenAnalyzer {
 public:
-  BracesRemover(const Environment &Env, const FormatStyle &Style)
+  BracesInserter(const Environment &Env, const FormatStyle &Style)
       : TokenAnalyzer(Env, Style) {}
 
   std::pair<tooling::Replacements, unsigned>
@@ -1795,40 +1795,40 @@ class BracesRemover : public TokenAnalyzer {
           FormatTokenLexer &Tokens) override {
     AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
     tooling::Replacements Result;
-    removeBraces(AnnotatedLines, Result);
+    insertBraces(AnnotatedLines, Result);
     return {Result, 0};
   }
 
 private:
-  void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
+  void insertBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
                     tooling::Replacements &Result) {
     const auto &SourceMgr = Env.getSourceManager();
     for (AnnotatedLine *Line : Lines) {
-      removeBraces(Line->Children, Result);
+      insertBraces(Line->Children, Result);
       if (!Line->Affected)
         continue;
       for (FormatToken *Token = Line->First; Token && !Token->Finalized;
            Token = Token->Next) {
-        if (!Token->Optional)
+        if (Token->BraceCount == 0)
           continue;
-        assert(Token->isOneOf(tok::l_brace, tok::r_brace));
-        assert(Token->Next || Token == Line->Last);
-        const auto Start =
-            Token == Line->Last || (Token->Next->is(tok::kw_else) &&
-                                    Token->Next->NewlinesBefore > 0)
-                ? Token->WhitespaceRange.getBegin()
-                : Token->Tok.getLocation();
-        const auto Range =
-            CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
-        cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
+        std::string Brace;
+        if (Token->BraceCount < 0) {
+          assert(Token->BraceCount == -1);
+          Brace = '{';
+        } else {
+          Brace = '\n' + std::string(Token->BraceCount, '}');
+        }
+        Token->BraceCount = 0;
+        const auto Start = Token->Tok.getEndLoc();
+        cantFail(Result.add(tooling::Replacement(SourceMgr, Start, 0, Brace)));
       }
     }
   }
 };
 
-class BracesInserter : public TokenAnalyzer {
+class BracesRemover : public TokenAnalyzer {
 public:
-  BracesInserter(const Environment &Env, const FormatStyle &Style)
+  BracesRemover(const Environment &Env, const FormatStyle &Style)
       : TokenAnalyzer(Env, Style) {}
 
   std::pair<tooling::Replacements, unsigned>
@@ -1837,32 +1837,32 @@ class BracesInserter : public TokenAnalyzer {
           FormatTokenLexer &Tokens) override {
     AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
     tooling::Replacements Result;
-    insertBraces(AnnotatedLines, Result);
+    removeBraces(AnnotatedLines, Result);
     return {Result, 0};
   }
 
 private:
-  void insertBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
+  void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
                     tooling::Replacements &Result) {
     const auto &SourceMgr = Env.getSourceManager();
     for (AnnotatedLine *Line : Lines) {
-      insertBraces(Line->Children, Result);
+      removeBraces(Line->Children, Result);
       if (!Line->Affected)
         continue;
       for (FormatToken *Token = Line->First; Token && !Token->Finalized;
            Token = Token->Next) {
-        if (Token->BraceCount == 0)
+        if (!Token->Optional)
           continue;
-        std::string Brace;
-        if (Token->BraceCount < 0) {
-          assert(Token->BraceCount == -1);
-          Brace = '{';
-        } else {
-          Brace = '\n' + std::string(Token->BraceCount, '}');
-        }
-        Token->BraceCount = 0;
-        const auto Start = Token->Tok.getEndLoc();
-        cantFail(Result.add(tooling::Replacement(SourceMgr, Start, 0, Brace)));
+        assert(Token->isOneOf(tok::l_brace, tok::r_brace));
+        assert(Token->Next || Token == Line->Last);
+        const auto Start =
+            Token == Line->Last || (Token->Next->is(tok::kw_else) &&
+                                    Token->Next->NewlinesBefore > 0)
+                ? Token->WhitespaceRange.getBegin()
+                : Token->Tok.getLocation();
+        const auto Range =
+            CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
+        cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
       }
     }
   }
@@ -3169,18 +3169,18 @@ reformat(const FormatStyle &Style, StringRef Code,
   typedef std::function<std::pair<tooling::Replacements, unsigned>(
       const Environment &)>
       AnalyzerPass;
-  SmallVector<AnalyzerPass, 4> Passes;
-
-  if (Style.isCpp() && Style.QualifierAlignment != FormatStyle::QAS_Leave) {
-    Passes.emplace_back([&](const Environment &Env) {
-      return QualifierAlignmentFixer(Env, Expanded, Code, Ranges,
-                                     FirstStartColumn, NextStartColumn,
-                                     LastStartColumn, FileName)
-          .process();
-    });
-  }
+  SmallVector<AnalyzerPass, 8> Passes;
 
   if (Style.isCpp()) {
+    if (Style.QualifierAlignment != FormatStyle::QAS_Leave) {
+      Passes.emplace_back([&](const Environment &Env) {
+        return QualifierAlignmentFixer(Env, Expanded, Code, Ranges,
+                                       FirstStartColumn, NextStartColumn,
+                                       LastStartColumn, FileName)
+            .process();
+      });
+    }
+
     if (Style.InsertBraces)
       Passes.emplace_back([&](const Environment &Env) {
         return BracesInserter(Env, Expanded).process();


        


More information about the cfe-commits mailing list