[clang] [clang-format] Don't split "DPI"/"DPI-C" in Verilog imports (PR #66951)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 21 05:34:19 PDT 2023


================
@@ -2270,7 +2270,18 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
     if (State.Stack.back().IsInsideObjCArrayLiteral)
       return nullptr;
 
+    // The "DPI"/"DPI-C" in SystemVerilog direct programming interface imports
+    // cannot be split, e.g.
+    // `import "DPI" function foo();`
     StringRef Text = Current.TokenText;
+    if (Style.isVerilog()) {
+      const FormatToken *Prev = Current.getPreviousNonComment();
+      if (Prev && Prev == State.Line->getFirstNonComment() &&
+          Prev->TokenText == "import") {
+        return nullptr;
+      }
+    }
+
----------------
owenca wrote:

> `isOneOf` won't work here, since the token has the type of `identifier` rather than a keyword:

Have you tried it? It does work because not only is `import` a `tok::identifier`, it's also a `Keywords.kw_import`.

> We should leave `Current.getPreviousNonComment()` to handle comments in the middle of the statement (something like `import /*"DPI"*/ "DPI-C" ...` comes to mind).

I was aware of that, but we usually don't call `getPreviousNonComment()` unless a comment before a token makes sense in practice. Otherwise, we would have to write ugly and inefficient code to handle things like the following:
```
/* outer l_square */ [ /* inner l_square */ [ /* attribute */ unlikely /* inner r_square */ ] /* outer r_square */ ] // comment
```

> I'd suggest to address known cases with targeted exemptions to avoid surprises in random places.

I think this is also relevant to (at least) C++ import statements, e.g.:
```
`import "clang/include/clang/Format/Format.h";`
```
I still prefer that we make a general fix here but will leave it to @sstwcw.

https://github.com/llvm/llvm-project/pull/66951


More information about the cfe-commits mailing list