[clang] f7617f7 - [clang-format] Recognize TableGen paste operator on separate line (#133722)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 10 05:52:06 PDT 2025
Author: sstwcw
Date: 2025-04-10T12:52:02Z
New Revision: f7617f7f909102080f1a0cee46f8ca75ec8d14ff
URL: https://github.com/llvm/llvm-project/commit/f7617f7f909102080f1a0cee46f8ca75ec8d14ff
DIFF: https://github.com/llvm/llvm-project/commit/f7617f7f909102080f1a0cee46f8ca75ec8d14ff.diff
LOG: [clang-format] Recognize TableGen paste operator on separate line (#133722)
Formatting this piece of code made the program crash.
```
class TypedVecListRegOperand<RegisterClass Reg, int lanes, string eltsize>
: RegisterOperand<Reg, "printTypedVectorList<" # lanes # ", '"
# eltsize # "'>">;
```
The line starting with the `#` was treated as a separate preprocessor
directive line. Then the code dereferenced a null pointer when it tried
to continue parsing the first line that did not end in a semicolon.
Now the 2 problems are fixed.
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTestTableGen.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 46cd009da281e..d184c23123c34 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -948,6 +948,8 @@ class AnnotatingParser {
HashTok->setType(TT_Unknown);
if (!parseTableGenValue(ParseNameMode))
return false;
+ if (!CurrentToken)
+ return true;
}
// In name mode, '{' is regarded as the end of the value.
// See TGParser::ParseValue in TGParser.cpp
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index b49e80b0b89c8..60f4f30623baa 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -4842,9 +4842,16 @@ void UnwrappedLineParser::readToken(int LevelDifference) {
PreviousWasComment = FormatTok->is(tok::comment);
while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
- (!Style.isVerilog() ||
- Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
FirstNonCommentOnLine) {
+ // In Verilog, the backtick is used for macro invocations. In TableGen,
+ // the single hash is used for the paste operator.
+ const auto *Next = Tokens->peekNextToken();
+ if ((Style.isVerilog() && !Keywords.isVerilogPPDirective(*Next)) ||
+ (Style.isTableGen() &&
+ !Next->isOneOf(tok::kw_else, tok::pp_define, tok::pp_ifdef,
+ tok::pp_ifndef, tok::pp_endif))) {
+ break;
+ }
distributeComments(Comments, FormatTok);
Comments.clear();
// If there is an unfinished unwrapped line, we flush the preprocessor
diff --git a/clang/unittests/Format/FormatTestTableGen.cpp b/clang/unittests/Format/FormatTestTableGen.cpp
index 92377c31f2e91..1c3d187de393c 100644
--- a/clang/unittests/Format/FormatTestTableGen.cpp
+++ b/clang/unittests/Format/FormatTestTableGen.cpp
@@ -218,6 +218,13 @@ TEST_F(FormatTestTableGen, PasteOperator) {
" string Z = [\"Traring\", \"Paste\", \"Traring\", \"Paste\",\n"
" \"Traring\", \"Paste\"]#;\n"
"}");
+
+ verifyFormat("def x#x {}", "def x\n"
+ "#x {}");
+ verifyFormat("def x#x {}", "def x\n"
+ "#\n"
+ "x {}");
+ verifyFormat("def x#x");
}
TEST_F(FormatTestTableGen, ClassDefinition) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 38dc10a08f640..2c7319ccefec2 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2836,6 +2836,28 @@ TEST_F(TokenAnnotatorTest, UnderstandTableGenTokens) {
Tokens = Annotate("!cond");
EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TableGenCondOperator);
+ // The paste operator should not be treated as a preprocessor directive even
+ // if it is on a separate line.
+ Tokens = Annotate("def x\n"
+ "#embed {}");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
+ EXPECT_EQ(Tokens[1]->Next, Tokens[2]);
+ Tokens = Annotate("def x\n"
+ "#define x\n"
+ "#embed {}");
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
+ EXPECT_EQ(Tokens[1]->Next, Tokens[5]);
+ Tokens = Annotate("def x\n"
+ "#ifdef x\n"
+ "#else\n"
+ "#endif\n"
+ "#embed {}");
+ ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+ EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
+ EXPECT_EQ(Tokens[1]->Next, Tokens[9]);
+
auto AnnotateValue = [this, &Style](StringRef Code) {
// Values are annotated only in specific context.
auto Result = annotate(("def X { let V = " + Code + "; }").str(), Style);
More information about the cfe-commits
mailing list