@@ -839,8 +839,13 @@ FormatToken *UnwrappedLineParser::parseBlock(
839
839
}
840
840
};
841
841
842
+ // Whether this is a Verilog-specific block that has a special header like a
843
+ // module.
844
+ const bool VerilogHierarchy =
845
+ Style.isVerilog () && Keywords.isVerilogHierarchy (*FormatTok);
842
846
assert ((FormatTok->isOneOf (tok::l_brace, TT_MacroBlockBegin) ||
843
- (Style.isVerilog () && Keywords.isVerilogBegin (*FormatTok))) &&
847
+ (Style.isVerilog () &&
848
+ (Keywords.isVerilogBegin (*FormatTok) || VerilogHierarchy))) &&
844
849
" '{' or macro block token expected" );
845
850
FormatToken *Tok = FormatTok;
846
851
const bool FollowedByComment = Tokens->peekNextToken ()->is (tok::comment);
@@ -850,14 +855,20 @@ FormatToken *UnwrappedLineParser::parseBlock(
850
855
851
856
// For Whitesmiths mode, jump to the next level prior to skipping over the
852
857
// braces.
853
- if (AddLevels > 0 && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
858
+ if (!VerilogHierarchy && AddLevels > 0 &&
859
+ Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
854
860
++Line->Level ;
861
+ }
855
862
856
863
size_t PPStartHash = computePPHash ();
857
864
858
865
const unsigned InitialLevel = Line->Level ;
859
- nextToken (/* LevelDifference=*/ AddLevels);
860
- HandleVerilogBlockLabel ();
866
+ if (VerilogHierarchy) {
867
+ AddLevels += parseVerilogHierarchyHeader ();
868
+ } else {
869
+ nextToken (/* LevelDifference=*/ AddLevels);
870
+ HandleVerilogBlockLabel ();
871
+ }
861
872
862
873
// Bail out if there are too many levels. Otherwise, the stack might overflow.
863
874
if (Line->Level > 300 )
@@ -1552,7 +1563,14 @@ void UnwrappedLineParser::parseStructuralElement(
1552
1563
return ;
1553
1564
case tok::kw_extern:
1554
1565
nextToken ();
1555
- if (FormatTok->is (tok::string_literal)) {
1566
+ if (Style.isVerilog ()) {
1567
+ // In Verilog and extern module declaration looks like a start of module.
1568
+ // But there is no body and endmodule. So we handle it separately.
1569
+ if (Keywords.isVerilogHierarchy (*FormatTok)) {
1570
+ parseVerilogHierarchyHeader ();
1571
+ return ;
1572
+ }
1573
+ } else if (FormatTok->is (tok::string_literal)) {
1556
1574
nextToken ();
1557
1575
if (FormatTok->is (tok::l_brace)) {
1558
1576
if (Style.BraceWrapping .AfterExternBlock )
@@ -1751,9 +1769,15 @@ void UnwrappedLineParser::parseStructuralElement(
1751
1769
parseEnum ();
1752
1770
}
1753
1771
break ;
1772
+ case tok::kw_class:
1773
+ if (Style.isVerilog ()) {
1774
+ parseBlock ();
1775
+ addUnwrappedLine ();
1776
+ return ;
1777
+ }
1778
+ LLVM_FALLTHROUGH;
1754
1779
case tok::kw_struct:
1755
1780
case tok::kw_union:
1756
- case tok::kw_class:
1757
1781
if (parseStructLike ())
1758
1782
return ;
1759
1783
break ;
@@ -1889,7 +1913,8 @@ void UnwrappedLineParser::parseStructuralElement(
1889
1913
}
1890
1914
1891
1915
if (Style.isVerilog ()) {
1892
- if (Keywords.isVerilogBegin (*FormatTok)) {
1916
+ if (Keywords.isVerilogBegin (*FormatTok) ||
1917
+ Keywords.isVerilogHierarchy (*FormatTok)) {
1893
1918
parseBlock ();
1894
1919
addUnwrappedLine ();
1895
1920
return ;
@@ -3998,6 +4023,137 @@ void UnwrappedLineParser::parseStatementMacro() {
3998
4023
addUnwrappedLine ();
3999
4024
}
4000
4025
4026
+ void UnwrappedLineParser::parseVerilogHierarchyIdentifier () {
4027
+ // consume things like a::`b.c[d:e] or a::*
4028
+ while (true ) {
4029
+ if (FormatTok->isOneOf (tok::star, tok::period, tok::periodstar,
4030
+ tok::coloncolon, tok::hash) ||
4031
+ Keywords.isVerilogIdentifier (*FormatTok)) {
4032
+ nextToken ();
4033
+ } else if (FormatTok->is (tok::l_square)) {
4034
+ parseSquare ();
4035
+ } else {
4036
+ break ;
4037
+ }
4038
+ }
4039
+ }
4040
+
4041
+ void UnwrappedLineParser::parseVerilogSensitivityList () {
4042
+ assert (FormatTok->is (tok::at));
4043
+ nextToken ();
4044
+ // A block event expression has 2 at signs.
4045
+ if (FormatTok->is (tok::at))
4046
+ nextToken ();
4047
+ switch (FormatTok->Tok .getKind ()) {
4048
+ case tok::star:
4049
+ nextToken ();
4050
+ break ;
4051
+ case tok::l_paren:
4052
+ parseParens ();
4053
+ break ;
4054
+ default :
4055
+ parseVerilogHierarchyIdentifier ();
4056
+ break ;
4057
+ }
4058
+ }
4059
+
4060
+ unsigned UnwrappedLineParser::parseVerilogHierarchyHeader () {
4061
+ unsigned AddLevels = 0 ;
4062
+
4063
+ if (FormatTok->is (Keywords.kw_clocking )) {
4064
+ nextToken ();
4065
+ if (Keywords.isVerilogIdentifier (*FormatTok))
4066
+ nextToken ();
4067
+ if (FormatTok->is (tok::at))
4068
+ parseVerilogSensitivityList ();
4069
+ if (FormatTok->is (tok::semi))
4070
+ nextToken ();
4071
+ } else if (FormatTok->isOneOf (tok::kw_case, Keywords.kw_casex ,
4072
+ Keywords.kw_casez , Keywords.kw_randcase ,
4073
+ Keywords.kw_randsequence )) {
4074
+ if (Style.IndentCaseLabels )
4075
+ ++AddLevels;
4076
+ nextToken ();
4077
+ if (FormatTok->is (tok::l_paren))
4078
+ parseParens ();
4079
+ if (FormatTok->isOneOf (Keywords.kw_inside , Keywords.kw_matches ))
4080
+ nextToken ();
4081
+ // The case header has no semicolon.
4082
+ } else {
4083
+ // "module" etc.
4084
+ nextToken ();
4085
+ // all the words like the name of the module and specifiers like
4086
+ // "automatic" and the width of function return type
4087
+ while (true ) {
4088
+ if (FormatTok->is (tok::l_square)) {
4089
+ auto Prev = FormatTok->getPreviousNonComment ();
4090
+ if (Prev && Keywords.isVerilogIdentifier (*Prev))
4091
+ Prev->setFinalizedType (TT_VerilogDimensionedTypeName);
4092
+ parseSquare ();
4093
+ } else if (Keywords.isVerilogIdentifier (*FormatTok) ||
4094
+ FormatTok->isOneOf (Keywords.kw_automatic , tok::kw_static)) {
4095
+ nextToken ();
4096
+ } else {
4097
+ break ;
4098
+ }
4099
+ }
4100
+
4101
+ auto NewLine = [this ]() {
4102
+ addUnwrappedLine ();
4103
+ Line->IsContinuation = true ;
4104
+ };
4105
+
4106
+ // package imports
4107
+ while (FormatTok->is (Keywords.kw_import )) {
4108
+ NewLine ();
4109
+ nextToken ();
4110
+ parseVerilogHierarchyIdentifier ();
4111
+ if (FormatTok->is (tok::semi))
4112
+ nextToken ();
4113
+ }
4114
+
4115
+ // parameters and ports
4116
+ if (FormatTok->is (Keywords.kw_verilogHash )) {
4117
+ NewLine ();
4118
+ nextToken ();
4119
+ if (FormatTok->is (tok::l_paren))
4120
+ parseParens ();
4121
+ }
4122
+ if (FormatTok->is (tok::l_paren)) {
4123
+ NewLine ();
4124
+ parseParens ();
4125
+ }
4126
+
4127
+ // extends and implements
4128
+ if (FormatTok->is (Keywords.kw_extends )) {
4129
+ NewLine ();
4130
+ nextToken ();
4131
+ parseVerilogHierarchyIdentifier ();
4132
+ if (FormatTok->is (tok::l_paren))
4133
+ parseParens ();
4134
+ }
4135
+ if (FormatTok->is (Keywords.kw_implements )) {
4136
+ NewLine ();
4137
+ do {
4138
+ nextToken ();
4139
+ parseVerilogHierarchyIdentifier ();
4140
+ } while (FormatTok->is (tok::comma));
4141
+ }
4142
+
4143
+ // Coverage event for cover groups.
4144
+ if (FormatTok->is (tok::at)) {
4145
+ NewLine ();
4146
+ parseVerilogSensitivityList ();
4147
+ }
4148
+
4149
+ if (FormatTok->is (tok::semi))
4150
+ nextToken (/* LevelDifference=*/ 1 );
4151
+ addUnwrappedLine ();
4152
+ }
4153
+
4154
+ return AddLevels;
4155
+ }
4156
+
4001
4157
LLVM_ATTRIBUTE_UNUSED static void printDebugInfo (const UnwrappedLine &Line,
4002
4158
StringRef Prefix = " " ) {
4003
4159
llvm::dbgs () << Prefix << " Line(" << Line.Level
@@ -4035,6 +4191,7 @@ void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4035
4191
Line->Tokens .clear ();
4036
4192
Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex ;
4037
4193
Line->FirstStartColumn = 0 ;
4194
+ Line->IsContinuation = false ;
4038
4195
4039
4196
if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4040
4197
--Line->Level ;
0 commit comments