Skip to content

[clang-format] Recognize TableGen paste operator on separate line #133722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,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
Expand Down
11 changes: 9 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4853,9 +4853,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
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/FormatTestTableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,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);
Expand Down
Loading