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

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 22 03:41:33 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;
+      }
+    }
+
----------------
alexfh 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`.

Ah, right, I had tried it with `tok::kw_import` (and I missed the difference between this and your suggestion). Thanks for the clarification!

> > 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 don't think it would add a lot of overhead (one branch on a happy path) or hinder readability of the code a lot (`getPreviousNonComment()` vs `Prev`), but I also don't think it's super important here.


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


More information about the cfe-commits mailing list