Skip to content

Commit 43177bf

Browse files
authored
[clang-format] Recognize Verilog followed-by operators (#165594)
When formatting Verilog code, the program changes the hash to `kw_verilogHash` and the backtick to `tok::hash`. The developer did not take that into account when writing the part for recognizing the `#-#` and `#=#` operators. The part did not work. The program would add a space within the operator. after ```SystemVerilog ##[0 : 5] done #-# always !rst; ``` before ```SystemVerilog ##[0 : 5] done #- #always !rst; ```
1 parent 9ed6be8 commit 43177bf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,21 @@ void FormatTokenLexer::tryMergePreviousTokens() {
318318
{tok::equal, tok::greater},
319319
{tok::star, tok::greater},
320320
{tok::pipeequal, tok::greater},
321-
{tok::pipe, tok::arrow},
322-
{tok::hash, tok::minus, tok::hash},
323-
{tok::hash, tok::equal, tok::hash}},
321+
{tok::pipe, tok::arrow}},
324322
TT_BinaryOperator) ||
325323
Tokens.back()->is(tok::arrow)) {
326324
Tokens.back()->ForcedPrecedence = prec::Comma;
327325
return;
328326
}
327+
if (Tokens.size() >= 3 &&
328+
Tokens[Tokens.size() - 3]->is(Keywords.kw_verilogHash) &&
329+
Tokens[Tokens.size() - 2]->isOneOf(tok::minus, tok::equal) &&
330+
Tokens[Tokens.size() - 1]->is(Keywords.kw_verilogHash) &&
331+
tryMergeTokens(3, TT_BinaryOperator)) {
332+
Tokens.back()->setFinalizedType(TT_BinaryOperator);
333+
Tokens.back()->ForcedPrecedence = prec::Comma;
334+
return;
335+
}
329336
} else if (Style.isTableGen()) {
330337
// TableGen's Multi line string starts with [{
331338
if (tryMergeTokens({tok::l_square, tok::l_brace},

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) {
26862686
// precedence.
26872687
std::pair<prec::Level, std::string> JoinedBinary[] = {
26882688
{prec::Comma, "->"}, {prec::Comma, "<->"},
2689+
{prec::Comma, "#-#"}, {prec::Comma, "#=#"},
26892690
{prec::Assignment, "+="}, {prec::Assignment, "-="},
26902691
{prec::Assignment, "*="}, {prec::Assignment, "/="},
26912692
{prec::Assignment, "%="}, {prec::Assignment, "&="},

0 commit comments

Comments
 (0)