[clang] da5e11c - [AST] Support ConceptReference in DynTypedNode, add dump().

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 4 04:56:36 PDT 2023


Author: Sam McCall
Date: 2023-09-04T13:56:15+02:00
New Revision: da5e11cd1ce0754a123ff0bf6f3ce4a41be14355

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

LOG: [AST] Support ConceptReference in DynTypedNode, add dump().

The dump() is not actually included recursively in any other nodes' dump,
as this is too verbose (similar to NNS) but useful in its own right.

It's unfortunate to not have the actual tests yet, but the DynTypedNode
tests are matcher-based and adding matchers is a larger task than
DynTypedNode support (but can't be done first).

(I've got a clangd change stacked on this that uses DynTypedNode and
dump(), and both work. I'll send a change for matchers next).

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

Added: 
    

Modified: 
    clang/include/clang/AST/ASTConcept.h
    clang/include/clang/AST/ASTFwd.h
    clang/include/clang/AST/ASTNodeTraverser.h
    clang/include/clang/AST/ASTTypeTraits.h
    clang/include/clang/AST/TextNodeDumper.h
    clang/lib/AST/ASTDumper.cpp
    clang/lib/AST/ASTTypeTraits.cpp
    clang/lib/AST/TextNodeDumper.cpp
    clang/unittests/AST/ASTTypeTraitsTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/ASTConcept.h b/clang/include/clang/AST/ASTConcept.h
index d40d3636287e7c0..5f9aa41d3e6cf50 100644
--- a/clang/include/clang/AST/ASTConcept.h
+++ b/clang/include/clang/AST/ASTConcept.h
@@ -215,6 +215,8 @@ class ConceptReference {
   }
 
   void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
+  void dump() const;
+  void dump(llvm::raw_ostream &) const;
 };
 
 /// Models the abbreviated syntax to constrain a template type parameter:

diff  --git a/clang/include/clang/AST/ASTFwd.h b/clang/include/clang/AST/ASTFwd.h
index f84b3238e32b52e..8823663386ea1bb 100644
--- a/clang/include/clang/AST/ASTFwd.h
+++ b/clang/include/clang/AST/ASTFwd.h
@@ -34,6 +34,7 @@ class Attr;
 #define ATTR(A) class A##Attr;
 #include "clang/Basic/AttrList.inc"
 class ObjCProtocolLoc;
+class ConceptReference;
 
 } // end namespace clang
 

diff  --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h
index 5ffce930b440ff4..1151a756ff377b6 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -252,6 +252,10 @@ class ASTNodeTraverser
     });
   }
 
+  void Visit(const ConceptReference *R) {
+    getNodeDelegate().AddChild([=] { getNodeDelegate().Visit(R); });
+  }
+
   void Visit(const APValue &Value, QualType Ty) {
     getNodeDelegate().AddChild([=] { getNodeDelegate().Visit(Value, Ty); });
   }
@@ -288,6 +292,8 @@ class ASTNodeTraverser
       Visit(C);
     else if (const auto *T = N.get<TemplateArgument>())
       Visit(*T);
+    else if (const auto *CR = N.get<ConceptReference>())
+      Visit(CR);
   }
 
   void dumpDeclContext(const DeclContext *DC) {

diff  --git a/clang/include/clang/AST/ASTTypeTraits.h b/clang/include/clang/AST/ASTTypeTraits.h
index 78661823ca858c2..3988a15971db5d9 100644
--- a/clang/include/clang/AST/ASTTypeTraits.h
+++ b/clang/include/clang/AST/ASTTypeTraits.h
@@ -163,6 +163,7 @@ class ASTNodeKind {
 #define ATTR(A) NKI_##A##Attr,
 #include "clang/Basic/AttrList.inc"
     NKI_ObjCProtocolLoc,
+    NKI_ConceptReference,
     NKI_NumberOfKinds
   };
 
@@ -222,6 +223,7 @@ KIND_TO_KIND_ID(OMPClause)
 KIND_TO_KIND_ID(Attr)
 KIND_TO_KIND_ID(ObjCProtocolLoc)
 KIND_TO_KIND_ID(CXXBaseSpecifier)
+KIND_TO_KIND_ID(ConceptReference)
 #define DECL(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED##Decl)
 #include "clang/AST/DeclNodes.inc"
 #define STMT(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED)
@@ -582,6 +584,10 @@ template <>
 struct DynTypedNode::BaseConverter<ObjCProtocolLoc, void>
     : public ValueConverter<ObjCProtocolLoc> {};
 
+template <>
+struct DynTypedNode::BaseConverter<ConceptReference, void>
+    : public PtrConverter<ConceptReference> {};
+
 // The only operation we allow on unsupported types is \c get.
 // This allows to conveniently use \c DynTypedNode when having an arbitrary
 // AST node that is not supported, but prevents misuse - a user cannot create

diff  --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h
index 9871a7fd4fbd025..2f4ed082a0c7ad4 100644
--- a/clang/include/clang/AST/TextNodeDumper.h
+++ b/clang/include/clang/AST/TextNodeDumper.h
@@ -189,6 +189,8 @@ class TextNodeDumper
 
   void Visit(const GenericSelectionExpr::ConstAssociation &A);
 
+  void Visit(const ConceptReference *);
+
   void Visit(const concepts::Requirement *R);
 
   void Visit(const APValue &Value, QualType Ty);
@@ -204,6 +206,7 @@ class TextNodeDumper
   void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C);
   void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK);
   void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS);
+  void dumpConceptReference(const ConceptReference *R);
 
   void dumpDeclRef(const Decl *D, StringRef Label = {});
 

diff  --git a/clang/lib/AST/ASTDumper.cpp b/clang/lib/AST/ASTDumper.cpp
index 9900efb5a48dfc0..cc9a84eecaadba6 100644
--- a/clang/lib/AST/ASTDumper.cpp
+++ b/clang/lib/AST/ASTDumper.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTDumper.h"
+#include "clang/AST/ASTConcept.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclLookups.h"
 #include "clang/AST/JSONNodeDumper.h"
@@ -333,3 +334,17 @@ LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS,
                    Context.getDiagnostics().getShowColors());
   Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy));
 }
+
+//===----------------------------------------------------------------------===//
+// ConceptReference method implementations
+//===----------------------------------------------------------------------===//
+
+LLVM_DUMP_METHOD void ConceptReference::dump() const {
+  dump(llvm::errs());
+}
+
+LLVM_DUMP_METHOD void ConceptReference::dump(raw_ostream &OS) const {
+  auto &Ctx = getNamedConcept()->getASTContext();
+  ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
+  P.Visit(this);
+}

diff  --git a/clang/lib/AST/ASTTypeTraits.cpp b/clang/lib/AST/ASTTypeTraits.cpp
index fb9fe39e7778d8d..4c7496c699beffd 100644
--- a/clang/lib/AST/ASTTypeTraits.cpp
+++ b/clang/lib/AST/ASTTypeTraits.cpp
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/ASTConcept.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/DeclCXX.h"
@@ -54,6 +55,7 @@ const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
 #define ATTR(A) {NKI_Attr, #A "Attr"},
 #include "clang/Basic/AttrList.inc"
     {NKI_None, "ObjCProtocolLoc"},
+    {NKI_None, "ConceptReference"},
 };
 
 bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
@@ -210,6 +212,8 @@ void DynTypedNode::print(llvm::raw_ostream &OS,
     A->printPretty(OS, PP);
   else if (const ObjCProtocolLoc *P = get<ObjCProtocolLoc>())
     P->getProtocol()->print(OS, PP);
+  else if (const ConceptReference *C = get<ConceptReference>())
+    C->print(OS, PP);
   else
     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
 }
@@ -222,6 +226,8 @@ void DynTypedNode::dump(llvm::raw_ostream &OS,
     S->dump(OS, Context);
   else if (const Type *T = get<Type>())
     T->dump(OS, Context);
+  else if (const ConceptReference *C = get<ConceptReference>())
+    C->dump(OS);
   else
     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
 }
@@ -247,5 +253,7 @@ SourceRange DynTypedNode::getSourceRange() const {
     return A->getRange();
   if (const ObjCProtocolLoc *P = get<ObjCProtocolLoc>())
     return P->getSourceRange();
+  if (const ConceptReference *C = get<ConceptReference>())
+    return C->getSourceRange();
   return SourceRange();
 }

diff  --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index 593f7c4412d9964..c1a7ab10133d584 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -371,6 +371,20 @@ void TextNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
     OS << " selected";
 }
 
+void TextNodeDumper::Visit(const ConceptReference *R) {
+  if (!R) {
+    ColorScope Color(OS, ShowColors, NullColor);
+    OS << "<<<NULL>>> ConceptReference";
+    return;
+  }
+
+  OS << "ConceptReference";
+  dumpPointer(R);
+  dumpSourceRange(R->getSourceRange());
+  OS << ' ';
+  dumpBareDeclRef(R->getNamedConcept());
+}
+
 void TextNodeDumper::Visit(const concepts::Requirement *R) {
   if (!R) {
     ColorScope Color(OS, ShowColors, NullColor);

diff  --git a/clang/unittests/AST/ASTTypeTraitsTest.cpp b/clang/unittests/AST/ASTTypeTraitsTest.cpp
index dcea32d1f1c0829..b8f58b0cc6dd70f 100644
--- a/clang/unittests/AST/ASTTypeTraitsTest.cpp
+++ b/clang/unittests/AST/ASTTypeTraitsTest.cpp
@@ -166,6 +166,7 @@ TEST(ASTNodeKind, Name) {
   VERIFY_NAME(QualType);
   VERIFY_NAME(TypeLoc);
   VERIFY_NAME(CXXCtorInitializer);
+  VERIFY_NAME(ConceptReference);
   VERIFY_NAME(NestedNameSpecifier);
   VERIFY_NAME(Decl);
   VERIFY_NAME(CXXRecordDecl);
@@ -209,6 +210,8 @@ TEST(DynTypedNode, AttrSourceRange) {
                              ast_matchers::attr()));
 }
 
+// FIXME: add tests for ConceptReference once we add an ASTMatcher.
+
 TEST(DynTypedNode, DeclDump) {
   DumpVerifier Verifier;
   Verifier.expectSubstring("FunctionDecl");


        


More information about the cfe-commits mailing list