Skip to content

Commit 9fa2d74

Browse files
authored
[clang-format] Indent Verilog case statements with comments (#71353)
If a line contains a comment outside of (fake) parentheses, the part following it is indented according to `CurrentState.Indent`. A Verilog case label and the statement that follows are broken with mustBreakBefore. So the part that follows the case label needs some special handling. Previously, that variable was left out. So the indentation was wrong when there was a comment. old: ```Verilog case (data) 16'd0: result = // 10'b0111111111; endcase case (data) 16'd0: // // result = // 10'b0111111111; endcase ``` new: ```Verilog case (data) 16'd0: result = // 10'b0111111111; endcase case (data) 16'd0: // // result = // 10'b0111111111; endcase ```
1 parent 3af82b3 commit 9fa2d74

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
12081208

12091209
// Indentation of the statement following a Verilog case label is taken care
12101210
// of in moveStateToNextToken.
1211-
if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(Previous))
1211+
if (Style.isVerilog() && PreviousNonComment &&
1212+
Keywords.isVerilogEndOfLabel(*PreviousNonComment)) {
12121213
return State.FirstIndent;
1214+
}
12131215

12141216
if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths &&
12151217
State.Line->First->is(tok::kw_enum)) {
@@ -1612,6 +1614,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
16121614
State.NextToken->MustBreakBefore &&
16131615
Keywords.isVerilogEndOfLabel(Current)) {
16141616
State.FirstIndent += Style.IndentWidth;
1617+
CurrentState.Indent = State.FirstIndent;
16151618
}
16161619

16171620
unsigned Penalty =

clang/unittests/Format/FormatTestVerilog.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,20 @@ TEST_F(FormatTestVerilog, Case) {
344344
" longfunction( //\n"
345345
" arg);\n"
346346
"endcase");
347+
verifyFormat("case (data)\n"
348+
" 16'd0:\n"
349+
" //\n"
350+
" result = //\n"
351+
" 10'b0111111111;\n"
352+
"endcase");
353+
verifyFormat("case (data)\n"
354+
" 16'd0:\n"
355+
" //\n"
356+
"\n"
357+
" //\n"
358+
" result = //\n"
359+
" 10'b0111111111;\n"
360+
"endcase");
347361
Style = getDefaultStyle();
348362
Style.ContinuationIndentWidth = 1;
349363
verifyFormat("case (data)\n"

0 commit comments

Comments
 (0)