Skip to content

Commit

Permalink
[clang-format] Handle Verilog struct literals
Browse files Browse the repository at this point in the history
Previously `isVerilogIdentifier` was mistaking the apostrophe used in
struct literals as an identifier.  It is fixed.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D147329
  • Loading branch information
eywdck2l committed Apr 1, 2023
1 parent 92b2be3 commit feb585e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions clang/lib/Format/Format.cpp
Expand Up @@ -1489,6 +1489,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
break;
case FormatStyle::LK_Verilog:
LLVMStyle.IndentCaseLabels = true;
LLVMStyle.SpacesInContainerLiterals = false;
break;
default:
break;
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Format/FormatToken.h
Expand Up @@ -1722,8 +1722,9 @@ struct AdditionalKeywords {
case tok::kw_while:
return false;
case tok::identifier:
return VerilogExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
VerilogExtraKeywords.end();
return isWordLike(Tok) &&
VerilogExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
VerilogExtraKeywords.end();
default:
// getIdentifierInfo returns non-null for both identifiers and keywords.
return Tok.Tok.getIdentifierInfo();
Expand Down
19 changes: 19 additions & 0 deletions clang/unittests/Format/FormatTestVerilog.cpp
Expand Up @@ -916,6 +916,25 @@ TEST_F(FormatTestVerilog, Streaming) {
verifyFormat("{<<byte{j}} = x;");
}

TEST_F(FormatTestVerilog, StructLiteral) {
verifyFormat("c = '{0, 0.0};");
verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};");
verifyFormat("c = '{a: 0, b: 0.0};");
verifyFormat("c = '{a: 0, b: 0.0, default: 0};");
verifyFormat("c = ab'{a: 0, b: 0.0};");
verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};");
verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};");
verifyFormat("d = {int: 1, shortreal: 1.0};");
verifyFormat("d = ab'{int: 1, shortreal: 1.0};");
verifyFormat("c = '{default: 0};");
auto Style = getDefaultStyle();
Style.SpacesInContainerLiterals = true;
verifyFormat("c = '{a : 0, b : 0.0};", Style);
verifyFormat("c = '{a : 0, b : 0.0, default : 0};", Style);
verifyFormat("c = ab'{a : 0, b : 0.0};", Style);
verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};", Style);
}

TEST_F(FormatTestVerilog, StructuredProcedure) {
// Blocks should be indented correctly.
verifyFormat("initial begin\n"
Expand Down

0 comments on commit feb585e

Please sign in to comment.