Skip to content

Commit 60e1206

Browse files
committed
[clang-format] Handle Verilog attributes
Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D128709
1 parent c887194 commit 60e1206

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4067,6 +4067,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
40674067
Keywords.isWordLike(Left))) {
40684068
return false;
40694069
}
4070+
// Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
4071+
if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
4072+
return true;
40704073
}
40714074
if (Left.is(TT_ImplicitStringLiteral))
40724075
return Right.hasWhitespaceBefore();

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,23 @@ void UnwrappedLineParser::parseStructuralElement(
14711471
addUnwrappedLine();
14721472
return;
14731473
}
1474+
1475+
if (Style.isVerilog()) {
1476+
// Skip things that can exist before keywords like 'if' and 'case'.
1477+
while (true) {
1478+
if (FormatTok->isOneOf(Keywords.kw_priority, Keywords.kw_unique,
1479+
Keywords.kw_unique0)) {
1480+
nextToken();
1481+
} else if (FormatTok->is(tok::l_paren) &&
1482+
Tokens->peekNextToken()->is(tok::star)) {
1483+
parseParens();
1484+
} else {
1485+
break;
1486+
}
1487+
}
1488+
}
1489+
1490+
// Tokens that only make sense at the beginning of a line.
14741491
switch (FormatTok->Tok.getKind()) {
14751492
case tok::kw_asm:
14761493
nextToken();

clang/unittests/Format/FormatTestVerilog.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ TEST_F(FormatTestVerilog, Hierarchy) {
295295
TEST_F(FormatTestVerilog, If) {
296296
verifyFormat("if (x)\n"
297297
" x = x;");
298+
verifyFormat("unique if (x)\n"
299+
" x = x;");
300+
verifyFormat("unique0 if (x)\n"
301+
" x = x;");
302+
verifyFormat("priority if (x)\n"
303+
" x = x;");
298304
verifyFormat("if (x)\n"
299305
" x = x;\n"
300306
"x = x;");
@@ -357,6 +363,14 @@ TEST_F(FormatTestVerilog, If) {
357363
" x = {x};\n"
358364
"else\n"
359365
" {x} = {x};");
366+
367+
// With attributes.
368+
verifyFormat("(* x *) if (x)\n"
369+
" x = x;");
370+
verifyFormat("(* x = \"x\" *) if (x)\n"
371+
" x = x;");
372+
verifyFormat("(* x, x = \"x\" *) if (x)\n"
373+
" x = x;");
360374
}
361375

362376
TEST_F(FormatTestVerilog, Operators) {

0 commit comments

Comments
 (0)