[clang] caf2767 - [Clang][AST] Fixed BindingDecl AST-dump for tuple like structures

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 14 12:14:42 PDT 2022


Author: isuckatcs
Date: 2022-06-14T21:13:04+02:00
New Revision: caf2767c47c3808538c82b6ea2b24d8e95b11ee7

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

LOG: [Clang][AST] Fixed BindingDecl AST-dump for tuple like structures

The AST of a BindingDecl in case of tuple like structures wasn't
properly printed. For these bidnings there is information stored
in BindingDecl::getHoldingVar(), and this information was't
printed in the AST-dump.

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

Added: 
    

Modified: 
    clang/include/clang/AST/ASTNodeTraverser.h
    clang/unittests/AST/ASTTraverserTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h
index f2c5c01ac88d..093dc9a773c7 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -467,6 +467,10 @@ class ASTNodeTraverser
   void VisitBindingDecl(const BindingDecl *D) {
     if (Traversal == TK_IgnoreUnlessSpelledInSource)
       return;
+
+    if (const auto *V = D->getHoldingVar())
+      Visit(V);
+
     if (const auto *E = D->getBinding())
       Visit(E);
   }

diff  --git a/clang/unittests/AST/ASTTraverserTest.cpp b/clang/unittests/AST/ASTTraverserTest.cpp
index 9d32a3d4116d..3553c3012cd4 100644
--- a/clang/unittests/AST/ASTTraverserTest.cpp
+++ b/clang/unittests/AST/ASTTraverserTest.cpp
@@ -1157,6 +1157,46 @@ void decomposition()
     f = 42;
 }
 
+typedef __typeof(sizeof(int)) size_t;
+
+struct Pair
+{
+    int x, y;
+};
+
+// Note: these utilities are required to force binding to tuple like structure
+namespace std
+{
+    template <typename E>
+    struct tuple_size
+    {
+    };
+
+    template <>
+    struct tuple_size<Pair>
+    {
+        static constexpr size_t value = 2;
+    };
+
+    template <size_t I, class T>
+    struct tuple_element
+    {
+        using type = int;
+    };
+
+};
+
+template <size_t I>
+int &&get(Pair &&p);
+
+void decompTuple()
+{
+    Pair p{1, 2};
+    auto [a, b] = p;
+
+    a = 3;
+}
+
 )cpp",
                                        {"-std=c++20"});
 
@@ -1492,6 +1532,48 @@ DecompositionDecl ''
 |-BindingDecl 'f'
 |-BindingDecl 's'
 `-BindingDecl 't'
+)cpp");
+  }
+
+  {
+    auto FN = ast_matchers::match(
+        functionDecl(hasName("decompTuple"),
+                     hasDescendant(decompositionDecl().bind("decomp"))),
+        AST2->getASTContext());
+    EXPECT_EQ(FN.size(), 1u);
+
+    EXPECT_EQ(
+        dumpASTString(TK_AsIs, FN[0].getNodeAs<DecompositionDecl>("decomp")),
+        R"cpp(
+DecompositionDecl ''
+|-CXXConstructExpr
+| `-ImplicitCastExpr
+|   `-DeclRefExpr 'p'
+|-BindingDecl 'a'
+| |-VarDecl 'a'
+| | `-CallExpr
+| |   |-ImplicitCastExpr
+| |   | `-DeclRefExpr 'get'
+| |   `-ImplicitCastExpr
+| |     `-DeclRefExpr ''
+| `-DeclRefExpr 'a'
+`-BindingDecl 'b'
+  |-VarDecl 'b'
+  | `-CallExpr
+  |   |-ImplicitCastExpr
+  |   | `-DeclRefExpr 'get'
+  |   `-ImplicitCastExpr
+  |     `-DeclRefExpr ''
+  `-DeclRefExpr 'b'
+)cpp");
+
+    EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
+                            FN[0].getNodeAs<DecompositionDecl>("decomp")),
+              R"cpp(
+DecompositionDecl ''
+|-DeclRefExpr 'p'
+|-BindingDecl 'a'
+`-BindingDecl 'b'
 )cpp");
   }
 }


        


More information about the cfe-commits mailing list