Skip to content

Conversation

hnakamura5
Copy link
Contributor

Fixes #154634.
Allow inserting space before DAGArg's opener paren when nested.

@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2025

@llvm/pr-subscribers-clang-format

Author: Hirofumi Nakamura (hnakamura5)

Changes

Fixes #154634.
Allow inserting space before DAGArg's opener paren when nested.


Full diff: https://github.com/llvm/llvm-project/pull/155837.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+12-1)
  • (modified) clang/unittests/Format/FormatTestTableGen.cpp (+9-2)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a220de54f46bf..997fcfc2159dc 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1045,6 +1045,14 @@ class AnnotatingParser {
       }
     }
     // Parse the [DagArgList] part
+    return parseTableGenDAGArgList(Opener, BreakInside);
+  }
+
+  // DagArgList   ::=  "," DagArg [DagArgList]
+  // This parses SimpleValue 6's [DagArgList] part.
+  bool parseTableGenDAGArgList(FormatToken *Opener, bool BreakInside) {
+    ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
+    Contexts.back().IsTableGenDAGArgList = true;
     bool FirstDAGArgListElm = true;
     while (CurrentToken) {
       if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
@@ -1101,6 +1109,9 @@ class AnnotatingParser {
     // SimpleValue6 ::=  "(" DagArg [DagArgList] ")"
     if (Tok->is(tok::l_paren)) {
       Tok->setType(TT_TableGenDAGArgOpener);
+      // Nested DAGArg requires space before '(' as separator.
+      if (Contexts.back().IsTableGenDAGArgList)
+        Tok->SpacesRequiredBefore = 1;
       return parseTableGenDAGArgAndList(Tok);
     }
     // SimpleValue 9: Bang operator
@@ -2138,7 +2149,7 @@ class AnnotatingParser {
     // Whether the braces may mean concatenation instead of structure or array
     // literal.
     bool VerilogMayBeConcatenation = false;
-    bool IsTableGenDAGArg = false;
+    bool IsTableGenDAGArgList = false;
     bool IsTableGenBangOpe = false;
     bool IsTableGenCondOpe = false;
     enum {
diff --git a/clang/unittests/Format/FormatTestTableGen.cpp b/clang/unittests/Format/FormatTestTableGen.cpp
index 1c3d187de393c..33acd2435d289 100644
--- a/clang/unittests/Format/FormatTestTableGen.cpp
+++ b/clang/unittests/Format/FormatTestTableGen.cpp
@@ -185,11 +185,18 @@ TEST_F(FormatTestTableGen, SimpleValue6) {
                "      i32:$dst6,                                // dst6\n"
                "      i32:$dst7                                 // dst7\n"
                "  );\n"
-               "  let DAGArgBang = (!cast<SomeType>(\"Some\") i32:$src1,\n"
-               "      i32:$src2);\n"
+               "  let DAGArgBang =\n"
+               "      (!cast<SomeType>(\"Some\") i32:$src1, i32:$src2);\n"
+               "  let NestedDAGArg = ((DAGArg1 (v111 v112, v113), v12) v2,\n"
+               "      (DAGArg3 (v31 v32)));\n"
                "}");
 }
 
+TEST_F(FormatTestTableGen, SimpleValue6_NestedInPat) {
+  verifyFormat("def : Pat<(vec.vt (avg (vec.vt V128:$l), (vec.vt V128:$r))),\n"
+               "          (inst $l, $r)>;");
+}
+
 TEST_F(FormatTestTableGen, SimpleValue7) {
   verifyFormat("def SimpleValue7 { let Identifier = SimpleValue; }");
 }

Comment on lines +188 to +189
" let DAGArgBang =\n"
" (!cast<SomeType>(\"Some\") i32:$src1, i32:$src2);\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the line wrapping change (for better?) here now?

Comment on lines +190 to +191
" let NestedDAGArg = ((DAGArg1 (v111 v112, v113), v12) v2,\n"
" (DAGArg3 (v31 v32)));\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But here the line breaking is not after the =.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Clang format] [Tablegen] Formatting tablegen lacks whitespaces
4 participants