[clang] 35f7dd6 - [clang-format][NFC] Fix a bug in setting type FunctionLBrace

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 4 11:36:50 PST 2022


Author: Owen Pan
Date: 2022-02-04T11:36:30-08:00
New Revision: 35f7dd601d33219fafa2c0d308e187df3e36847a

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

LOG: [clang-format][NFC] Fix a bug in setting type FunctionLBrace

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index eb927b5c49217..97a2cf367e808 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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;

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1684e815b0617..88deee974bbf5 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -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


        


More information about the cfe-commits mailing list