[clang] ec90bc0 - [clang-format][NFC] Clean up the unwrapped line parser

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 02:59:31 PDT 2022


Author: owenca
Date: 2022-06-07T02:58:57-07:00
New Revision: ec90bc0ea4386849f9eea54f44d25b49d4673cd6

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

LOG: [clang-format][NFC] Clean up the unwrapped line parser

Change the signatures of parseBlock(), parseLevel(), and
parseStructuralElement() to support combining else and if when
removing braces. See #55663.

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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/lib/Format/UnwrappedLineParser.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 30bdbdfb2c702..f581005e8c3ca 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -395,7 +395,7 @@ void UnwrappedLineParser::parseFile() {
   if (Style.Language == FormatStyle::LK_TextProto)
     parseBracedList();
   else
-    parseLevel(/*OpeningBrace=*/nullptr, /*CanContainBracedList=*/true);
+    parseLevel();
   // Make sure to format the remaining tokens.
   //
   // LK_TextProto is special since its top-level is parsed as the body of a
@@ -469,12 +469,13 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
 /// \param CanContainBracedList If the content can contain (at any level) a
 /// braced list.
 /// \param NextLBracesType The type for left brace found in this level.
+/// \param IfKind The if statement kind in the level.
 /// \returns true if a simple block of if/else/for/while, or false otherwise.
 /// (A simple block has a single statement.)
 bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
                                      bool CanContainBracedList,
-                                     IfStmtKind *IfKind,
-                                     TokenType NextLBracesType) {
+                                     TokenType NextLBracesType,
+                                     IfStmtKind *IfKind) {
   auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace
                                   ? TT_BracedListLBrace
                                   : TT_Unknown;
@@ -484,6 +485,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
   bool HasLabel = false;
   unsigned StatementCount = 0;
   bool SwitchLabelEncountered = false;
+
   do {
     if (FormatTok->getType() == TT_AttributeMacro) {
       nextToken();
@@ -495,9 +497,9 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
     else if (FormatTok->getType() == TT_MacroBlockEnd)
       kind = tok::r_brace;
 
-    auto ParseDefault = [this, OpeningBrace, IfKind, NextLevelLBracesType,
+    auto ParseDefault = [this, OpeningBrace, NextLevelLBracesType, IfKind,
                          &HasDoWhile, &HasLabel, &StatementCount] {
-      parseStructuralElement(IfKind, !OpeningBrace, NextLevelLBracesType,
+      parseStructuralElement(!OpeningBrace, NextLevelLBracesType, IfKind,
                              HasDoWhile ? nullptr : &HasDoWhile,
                              HasLabel ? nullptr : &HasLabel);
       ++StatementCount;
@@ -524,7 +526,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
         continue;
       }
       parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
-                 /*MunchSemi=*/true, /*KeepBraces=*/true,
+                 /*MunchSemi=*/true, /*KeepBraces=*/true, /*IfKind=*/nullptr,
                  /*UnindentWhitesmithsBraces=*/false, CanContainBracedList,
                  NextLBracesType);
       ++StatementCount;
@@ -593,6 +595,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
       break;
     }
   } while (!eof());
+
   return false;
 }
 
@@ -815,10 +818,12 @@ bool UnwrappedLineParser::mightFitOnOneLine(
   return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
 }
 
-UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
-    bool MustBeDeclaration, unsigned AddLevels, bool MunchSemi, bool KeepBraces,
-    bool UnindentWhitesmithsBraces, bool CanContainBracedList,
-    TokenType NextLBracesType) {
+void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels,
+                                     bool MunchSemi, bool KeepBraces,
+                                     IfStmtKind *IfKind,
+                                     bool UnindentWhitesmithsBraces,
+                                     bool CanContainBracedList,
+                                     TokenType NextLBracesType) {
   assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
          "'{' or macro block token expected");
   FormatToken *Tok = FormatTok;
@@ -859,18 +864,17 @@ UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
   if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths)
     Line->Level += AddLevels;
 
-  IfStmtKind IfKind = IfStmtKind::NotIf;
   const bool SimpleBlock =
-      parseLevel(Tok, CanContainBracedList, &IfKind, NextLBracesType);
+      parseLevel(Tok, CanContainBracedList, NextLBracesType, IfKind);
 
   if (eof())
-    return IfKind;
+    return;
 
   if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
                  : !FormatTok->is(tok::r_brace)) {
     Line->Level = InitialLevel;
     FormatTok->setBlockKind(BK_Block);
-    return IfKind;
+    return;
   }
 
   auto RemoveBraces = [=]() mutable {
@@ -935,8 +939,6 @@ UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
           CurrentLines->size() - 1;
     }
   }
-
-  return IfKind;
 }
 
 static bool isGoogScope(const UnwrappedLine &Line) {
@@ -1010,8 +1012,7 @@ void UnwrappedLineParser::parseChildBlock(
     ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
                                             /*MustBeDeclaration=*/false);
     Line->Level += SkipIndent ? 0 : 1;
-    parseLevel(OpeningBrace, CanContainBracedList, /*IfKind=*/nullptr,
-               NextLBracesType);
+    parseLevel(OpeningBrace, CanContainBracedList, NextLBracesType);
     flushComments(isOnNewLine(*FormatTok));
     Line->Level -= SkipIndent ? 0 : 1;
   }
@@ -1414,9 +1415,9 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
   }
 }
 
-void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
-                                                 bool IsTopLevel,
+void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel,
                                                  TokenType NextLBracesType,
+                                                 IfStmtKind *IfKind,
                                                  bool *HasDoWhile,
                                                  bool *HasLabel) {
   if (Style.Language == FormatStyle::LK_TableGen &&
@@ -2543,8 +2544,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
   if (FormatTok->is(tok::kw_consteval)) {
     nextToken();
   } else {
-    if (Style.RemoveBracesLLVM)
-      KeepIfBraces = KeepBraces;
+    KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
     if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
       nextToken();
     if (FormatTok->is(tok::l_paren))
@@ -2562,8 +2562,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
     FormatTok->setFinalizedType(TT_ControlStatementLBrace);
     IfLeftBrace = FormatTok;
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    IfBlockKind = parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
-                             /*MunchSemi=*/true, KeepIfBraces);
+    parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
+               /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
     if (Style.BraceWrapping.BeforeElse)
       addUnwrappedLine();
     else
@@ -2595,9 +2595,9 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
       FormatTok->setFinalizedType(TT_ElseLBrace);
       ElseLeftBrace = FormatTok;
       CompoundStatementIndenter Indenter(this, Style, Line->Level);
-      const IfStmtKind ElseBlockKind =
-          parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
-                     /*MunchSemi=*/true, KeepElseBraces);
+      IfStmtKind ElseBlockKind;
+      parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
+                 /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
       if ((ElseBlockKind == IfStmtKind::IfOnly ||
            ElseBlockKind == IfStmtKind::IfElseIf) &&
           FormatTok->is(tok::kw_else)) {
@@ -2626,8 +2626,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
       parseUnbracedBody(/*CheckEOF=*/true);
     }
   } else {
-    if (Style.RemoveBracesLLVM)
-      KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
+    KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
     if (NeedsUnwrappedLine)
       addUnwrappedLine();
   }
@@ -2796,7 +2795,8 @@ void UnwrappedLineParser::parseNamespace() {
       ++Line->Level;
 
     parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
-               /*KeepBraces=*/true, ManageWhitesmithsBraces);
+               /*KeepBraces=*/true, /*IfKind=*/nullptr,
+               ManageWhitesmithsBraces);
 
     // Munch the semicolon after a namespace. This is more common than one would
     // think. Putting the semicolon into its own line is very ugly.
@@ -3612,7 +3612,7 @@ void UnwrappedLineParser::parseJavaEnumBody() {
   }
 
   // Parse the class body after the enum's ";" if any.
-  parseLevel(OpeningBrace, /*CanContainBracedList=*/true);
+  parseLevel(OpeningBrace);
   nextToken();
   --Line->Level;
   addUnwrappedLine();

diff  --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index 38f83a896e1d6..74d9719d38d79 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -92,16 +92,18 @@ class UnwrappedLineParser {
   void reset();
   void parseFile();
   bool precededByCommentOrPPDirective() const;
-  bool parseLevel(const FormatToken *OpeningBrace, bool CanContainBracedList,
-                  IfStmtKind *IfKind = nullptr,
-                  TokenType NextLBracesType = TT_Unknown);
+  bool parseLevel(const FormatToken *OpeningBrace = nullptr,
+                  bool CanContainBracedList = true,
+                  TokenType NextLBracesType = TT_Unknown,
+                  IfStmtKind *IfKind = nullptr);
   bool mightFitOnOneLine(UnwrappedLine &Line,
                          const FormatToken *OpeningBrace = nullptr) const;
-  IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
-                        bool MunchSemi = true, bool KeepBraces = true,
-                        bool UnindentWhitesmithsBraces = false,
-                        bool CanContainBracedList = true,
-                        TokenType NextLBracesType = TT_Unknown);
+  void parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
+                  bool MunchSemi = true, bool KeepBraces = true,
+                  IfStmtKind *IfKind = nullptr,
+                  bool UnindentWhitesmithsBraces = false,
+                  bool CanContainBracedList = true,
+                  TokenType NextLBracesType = TT_Unknown);
   void parseChildBlock(bool CanContainBracedList = true,
                        TokenType NextLBracesType = TT_Unknown);
   void parsePPDirective();
@@ -112,9 +114,9 @@ class UnwrappedLineParser {
   void parsePPEndIf();
   void parsePPUnknown();
   void readTokenWithJavaScriptASI();
-  void parseStructuralElement(IfStmtKind *IfKind = nullptr,
-                              bool IsTopLevel = false,
+  void parseStructuralElement(bool IsTopLevel = false,
                               TokenType NextLBracesType = TT_Unknown,
+                              IfStmtKind *IfKind = nullptr,
                               bool *HasDoWhile = nullptr,
                               bool *HasLabel = nullptr);
   bool tryToParseBracedList();


        


More information about the cfe-commits mailing list