[clang] 5b81158 - Revert "[clang-format] Handle attributes before case label."

Jorge Gorbe Moya via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 21 14:39:30 PDT 2022


Author: Jorge Gorbe Moya
Date: 2022-03-21T14:39:14-07:00
New Revision: 5b811586758808ce3335272d5b41852cf87666c7

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

LOG: Revert "[clang-format] Handle attributes before case label."

This reverts commit 596fa2d90044841c33b9a0e6b17406c2a45077a2.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index bef8ed54fab8a..36205b8ee18cd 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -480,10 +480,6 @@ bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace,
   unsigned StatementCount = 0;
   bool SwitchLabelEncountered = false;
   do {
-    if (FormatTok->getType() == TT_AttributeMacro) {
-      nextToken();
-      continue;
-    }
     tok::TokenKind kind = FormatTok->Tok.getKind();
     if (FormatTok->getType() == TT_MacroBlockBegin)
       kind = tok::l_brace;
@@ -573,8 +569,6 @@ bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace,
         parseCSharpAttribute();
         break;
       }
-      if (handleCppAttributes())
-        break;
       LLVM_FALLTHROUGH;
     default:
       ParseDefault();
@@ -1403,11 +1397,9 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
     // e.g. "default void f() {}" in a Java interface.
     break;
   case tok::kw_case:
-    if (Style.isJavaScript() && Line->MustBeDeclaration) {
+    if (Style.isJavaScript() && Line->MustBeDeclaration)
       // 'case: string' field declaration.
-      nextToken();
       break;
-    }
     parseCaseLabel();
     return;
   case tok::kw_try:
@@ -1828,12 +1820,6 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
     case tok::kw_new:
       parseNew();
       break;
-    case tok::kw_case:
-      if (Style.isJavaScript() && Line->MustBeDeclaration)
-        // 'case: string' field declaration.
-        break;
-      parseCaseLabel();
-      break;
     default:
       nextToken();
       break;
@@ -2402,24 +2388,17 @@ static void markOptionalBraces(FormatToken *LeftBrace) {
   RightBrace->Optional = true;
 }
 
-void UnwrappedLineParser::handleAttributes() {
-  // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
-  if (FormatTok->is(TT_AttributeMacro))
-    nextToken();
-  handleCppAttributes();
-}
-
-bool UnwrappedLineParser::handleCppAttributes() {
-  // Handle [[likely]] / [[unlikely]] attributes.
-  if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) {
-    parseSquare();
-    return true;
-  }
-  return false;
-}
-
 FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
                                                   bool KeepBraces) {
+  auto HandleAttributes = [this]() {
+    // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
+    if (FormatTok->is(TT_AttributeMacro))
+      nextToken();
+    // Handle [[likely]] / [[unlikely]] attributes.
+    if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute())
+      parseSquare();
+  };
+
   assert(FormatTok->is(tok::kw_if) && "'if' expected");
   nextToken();
   if (FormatTok->is(tok::exclaim))
@@ -2432,7 +2411,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
     if (FormatTok->is(tok::l_paren))
       parseParens();
   }
-  handleAttributes();
+  HandleAttributes();
 
   bool NeedsUnwrappedLine = false;
   keepAncestorBraces();
@@ -2469,7 +2448,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
       Kind = IfStmtKind::IfElse;
     }
     nextToken();
-    handleAttributes();
+    HandleAttributes();
     if (FormatTok->is(tok::l_brace)) {
       ElseLeftBrace = FormatTok;
       CompoundStatementIndenter Indenter(this, Style, Line->Level);

diff  --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index 798bae24ad075..5cc01398a5457 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -121,8 +121,6 @@ class UnwrappedLineParser {
   void parseSquare(bool LambdaIntroducer = false);
   void keepAncestorBraces();
   void parseUnbracedBody(bool CheckEOF = false);
-  void handleAttributes();
-  bool handleCppAttributes();
   FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false);
   void parseTryCatch();
   void parseForOrWhileLoop();

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e36a267c01f4b..539e9c22767ea 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -2609,52 +2609,6 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
                "}",
                getLLVMStyleWithColumns(34));
 
-  verifyFormat("switch (a) {\n"
-               "[[likely]] case 1:\n"
-               "  return;\n"
-               "}");
-  verifyFormat("switch (a) {\n"
-               "[[likely]] [[other::likely]] case 1:\n"
-               "  return;\n"
-               "}");
-  verifyFormat("switch (x) {\n"
-               "case 1:\n"
-               "  return;\n"
-               "[[likely]] case 2:\n"
-               "  return;\n"
-               "}");
-  verifyFormat("switch (a) {\n"
-               "case 1:\n"
-               "[[likely]] case 2:\n"
-               "  return;\n"
-               "}");
-  FormatStyle Attributes = getLLVMStyle();
-  Attributes.AttributeMacros.push_back("LIKELY");
-  Attributes.AttributeMacros.push_back("OTHER_LIKELY");
-  verifyFormat("switch (a) {\n"
-               "LIKELY case b:\n"
-               "  return;\n"
-               "}",
-               Attributes);
-  verifyFormat("switch (a) {\n"
-               "LIKELY OTHER_LIKELY() case b:\n"
-               "  return;\n"
-               "}",
-               Attributes);
-  verifyFormat("switch (a) {\n"
-               "case 1:\n"
-               "  return;\n"
-               "LIKELY case 2:\n"
-               "  return;\n"
-               "}",
-               Attributes);
-  verifyFormat("switch (a) {\n"
-               "case 1:\n"
-               "LIKELY case 2:\n"
-               "  return;\n"
-               "}",
-               Attributes);
-
   FormatStyle Style = getLLVMStyle();
   Style.IndentCaseLabels = true;
   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;


        


More information about the cfe-commits mailing list