[clang-format][NFC] Fix a bug in setting type FunctionLBrace
authorOwen Pan <owenpiano@gmail.com>
Fri, 4 Feb 2022 05:23:41 +0000 (21:23 -0800)
committerOwen Pan <owenpiano@gmail.com>
Fri, 4 Feb 2022 19:36:30 +0000 (11:36 -0800)
The l_brace token in a macro definition should not be set to
TT_FunctionLBrace.

This patch could have fixed #42087.

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

clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp

index eb927b5..97a2cf3 100644 (file)
@@ -1577,7 +1577,8 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
         } else if (Style.BraceWrapping.AfterFunction) {
           addUnwrappedLine();
         }
-        FormatTok->setType(TT_FunctionLBrace);
+        if (!Line->InPPDirective)
+          FormatTok->setType(TT_FunctionLBrace);
         parseBlock();
         addUnwrappedLine();
         return;
index 1684e81..88deee9 100644 (file)
@@ -91,6 +91,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsEnums) {
   EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace);
 }
 
+TEST_F(TokenAnnotatorTest, UnderstandsLBracesInMacroDefinition) {
+  auto Tokens = annotate("#define BEGIN NS {");
+  EXPECT_EQ(Tokens.size(), 6u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang