Skip to content

Commit be3204a

Browse files
authored
[clang-format] Ignore C++ keywords when formatting Verilog (#167984)
In the sample below, the `private` identifier is the name of the type, and the `try` identifier is the name of the variable. new ```SystemVerilog begin private try; end ``` old ```SystemVerilog begin private try ; end ```
1 parent 75c85ba commit be3204a

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,8 @@ FormatToken *FormatTokenLexer::getNextToken() {
14081408
FormatTok->Tok.setKind(tok::identifier);
14091409
} else if (Style.isTableGen() && !Keywords.isTableGenKeyword(*FormatTok)) {
14101410
FormatTok->Tok.setKind(tok::identifier);
1411+
} else if (Style.isVerilog() && Keywords.isVerilogIdentifier(*FormatTok)) {
1412+
FormatTok->Tok.setKind(tok::identifier);
14111413
}
14121414
} else if (const bool Greater = FormatTok->is(tok::greatergreater);
14131415
Greater || FormatTok->is(tok::lessless)) {

clang/unittests/Format/FormatTestVerilog.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ TEST_F(FormatTestVerilog, Declaration) {
423423
verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;");
424424
verifyFormat("wire (strong1, pull0) mynet, //\n"
425425
" mynet1 = enable;");
426+
427+
// The type or variable can be a C++ keyword.
428+
verifyFormat("private mynet;");
429+
verifyFormat("switch mynet;");
430+
verifyFormat("wire try;");
426431
}
427432

428433
TEST_F(FormatTestVerilog, Delay) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) {
29182918
EXPECT_EQ(Tokens[0]->TokenText, R"(\busa+index\
29192919
+)");
29202920
EXPECT_TOKEN(Tokens[1], tok::semi, TT_Unknown);
2921+
2922+
// A C++ keyword should be treated as an identifier.
2923+
Tokens = Annotate("volatile delete;");
2924+
ASSERT_EQ(Tokens.size(), 4u) << Tokens;
2925+
EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
2926+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
2927+
29212928
// An escaped newline should not be treated as an escaped identifier.
29222929
Tokens = Annotate("\\\n");
29232930
ASSERT_EQ(Tokens.size(), 1u) << Tokens;

0 commit comments

Comments
 (0)