[clang] 6bf66d8 - [clang-format] Indent Verilog struct literal on new line

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 29 08:39:11 PDT 2023


Author: sstwcw
Date: 2023-06-29T15:38:44Z
New Revision: 6bf66d839f1386c06e19a3621c02c8fc6a14f94f

URL: https://github.com/llvm/llvm-project/commit/6bf66d839f1386c06e19a3621c02c8fc6a14f94f
DIFF: https://github.com/llvm/llvm-project/commit/6bf66d839f1386c06e19a3621c02c8fc6a14f94f.diff

LOG: [clang-format] Indent Verilog struct literal on new line

Before:
```
c = //
'{default: 0};
```

After:
```
c = //
    '{default: 0};
```

If the line has to be broken, the continuation part should be
indented.  Before this fix, it was not the case if the continuation
part was a struct literal.  The rule that caused the problem was added
in 783bac6b.  It was intended for aligning the field labels in
ProtoBuf.  The type `TT_DictLiteral` was only for colons back then, so
the program didn't have to check whether the token was a colon when it
was already type `TT_DictLiteral`.  Now the type applies to more
things including the braces enclosing a dictionary literal.  In
Verilog, struct literals start with a quote.  The quote is regarded as
an identifier by the program.  So the rule for aligning the fields in
ProtoBuf applied to this situation by mistake.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D152623

Added: 
    

Modified: 
    clang/lib/Format/ContinuationIndenter.cpp
    clang/unittests/Format/FormatTestVerilog.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 13b853ea9e2267..74d82db043e67f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1173,7 +1173,15 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
   }
   if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
     return State.Stack[State.Stack.size() - 2].LastSpace;
+  // Field labels in a nested type should be aligned to the brace. For example
+  // in ProtoBuf:
+  //   optional int32 b = 2 [(foo_options) = {aaaaaaaaaaaaaaaaaaa: 123,
+  //                                          bbbbbbbbbbbbbbbbbbbbbbbb:"baz"}];
+  // For Verilog, a quote following a brace is treated as an identifier.  And
+  // Both braces and colons get annotated as TT_DictLiteral.  So we have to
+  // check.
   if (Current.is(tok::identifier) && Current.Next &&
+      (!Style.isVerilog() || Current.Next->is(tok::colon)) &&
       (Current.Next->is(TT_DictLiteral) ||
        ((Style.Language == FormatStyle::LK_Proto ||
          Style.Language == FormatStyle::LK_TextProto) &&

diff  --git a/clang/unittests/Format/FormatTestVerilog.cpp b/clang/unittests/Format/FormatTestVerilog.cpp
index 5c94d5afbac960..45839aea54ff0d 100644
--- a/clang/unittests/Format/FormatTestVerilog.cpp
+++ b/clang/unittests/Format/FormatTestVerilog.cpp
@@ -1172,6 +1172,15 @@ TEST_F(FormatTestVerilog, StructLiteral) {
   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);
+
+  // It should be indented correctly when the line has to break.
+  verifyFormat("c = //\n"
+               "    '{default: 0};");
+  Style = getDefaultStyle();
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("c = //\n"
+               "  '{default: 0};",
+               Style);
 }
 
 TEST_F(FormatTestVerilog, StructuredProcedure) {


        


More information about the cfe-commits mailing list