Skip to content

Commit da5e11c

Browse files
committed
[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
1 parent 2955cc1 commit da5e11c

File tree

9 files changed

+58
-0
lines changed

9 files changed

+58
-0
lines changed

clang/include/clang/AST/ASTConcept.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ class ConceptReference {
215215
}
216216

217217
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
218+
void dump() const;
219+
void dump(llvm::raw_ostream &) const;
218220
};
219221

220222
/// Models the abbreviated syntax to constrain a template type parameter:

clang/include/clang/AST/ASTFwd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Attr;
3434
#define ATTR(A) class A##Attr;
3535
#include "clang/Basic/AttrList.inc"
3636
class ObjCProtocolLoc;
37+
class ConceptReference;
3738

3839
} // end namespace clang
3940

clang/include/clang/AST/ASTNodeTraverser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ class ASTNodeTraverser
252252
});
253253
}
254254

255+
void Visit(const ConceptReference *R) {
256+
getNodeDelegate().AddChild([=] { getNodeDelegate().Visit(R); });
257+
}
258+
255259
void Visit(const APValue &Value, QualType Ty) {
256260
getNodeDelegate().AddChild([=] { getNodeDelegate().Visit(Value, Ty); });
257261
}
@@ -288,6 +292,8 @@ class ASTNodeTraverser
288292
Visit(C);
289293
else if (const auto *T = N.get<TemplateArgument>())
290294
Visit(*T);
295+
else if (const auto *CR = N.get<ConceptReference>())
296+
Visit(CR);
291297
}
292298

293299
void dumpDeclContext(const DeclContext *DC) {

clang/include/clang/AST/ASTTypeTraits.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class ASTNodeKind {
163163
#define ATTR(A) NKI_##A##Attr,
164164
#include "clang/Basic/AttrList.inc"
165165
NKI_ObjCProtocolLoc,
166+
NKI_ConceptReference,
166167
NKI_NumberOfKinds
167168
};
168169

@@ -222,6 +223,7 @@ KIND_TO_KIND_ID(OMPClause)
222223
KIND_TO_KIND_ID(Attr)
223224
KIND_TO_KIND_ID(ObjCProtocolLoc)
224225
KIND_TO_KIND_ID(CXXBaseSpecifier)
226+
KIND_TO_KIND_ID(ConceptReference)
225227
#define DECL(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED##Decl)
226228
#include "clang/AST/DeclNodes.inc"
227229
#define STMT(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED)
@@ -582,6 +584,10 @@ template <>
582584
struct DynTypedNode::BaseConverter<ObjCProtocolLoc, void>
583585
: public ValueConverter<ObjCProtocolLoc> {};
584586

587+
template <>
588+
struct DynTypedNode::BaseConverter<ConceptReference, void>
589+
: public PtrConverter<ConceptReference> {};
590+
585591
// The only operation we allow on unsupported types is \c get.
586592
// This allows to conveniently use \c DynTypedNode when having an arbitrary
587593
// AST node that is not supported, but prevents misuse - a user cannot create

clang/include/clang/AST/TextNodeDumper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class TextNodeDumper
189189

190190
void Visit(const GenericSelectionExpr::ConstAssociation &A);
191191

192+
void Visit(const ConceptReference *);
193+
192194
void Visit(const concepts::Requirement *R);
193195

194196
void Visit(const APValue &Value, QualType Ty);
@@ -204,6 +206,7 @@ class TextNodeDumper
204206
void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C);
205207
void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK);
206208
void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS);
209+
void dumpConceptReference(const ConceptReference *R);
207210

208211
void dumpDeclRef(const Decl *D, StringRef Label = {});
209212

clang/lib/AST/ASTDumper.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "clang/AST/ASTDumper.h"
15+
#include "clang/AST/ASTConcept.h"
1516
#include "clang/AST/ASTContext.h"
1617
#include "clang/AST/DeclLookups.h"
1718
#include "clang/AST/JSONNodeDumper.h"
@@ -333,3 +334,17 @@ LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS,
333334
Context.getDiagnostics().getShowColors());
334335
Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy));
335336
}
337+
338+
//===----------------------------------------------------------------------===//
339+
// ConceptReference method implementations
340+
//===----------------------------------------------------------------------===//
341+
342+
LLVM_DUMP_METHOD void ConceptReference::dump() const {
343+
dump(llvm::errs());
344+
}
345+
346+
LLVM_DUMP_METHOD void ConceptReference::dump(raw_ostream &OS) const {
347+
auto &Ctx = getNamedConcept()->getASTContext();
348+
ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
349+
P.Visit(this);
350+
}

clang/lib/AST/ASTTypeTraits.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "clang/AST/ASTTypeTraits.h"
16+
#include "clang/AST/ASTConcept.h"
1617
#include "clang/AST/ASTContext.h"
1718
#include "clang/AST/Attr.h"
1819
#include "clang/AST/DeclCXX.h"
@@ -54,6 +55,7 @@ const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
5455
#define ATTR(A) {NKI_Attr, #A "Attr"},
5556
#include "clang/Basic/AttrList.inc"
5657
{NKI_None, "ObjCProtocolLoc"},
58+
{NKI_None, "ConceptReference"},
5759
};
5860

5961
bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
@@ -210,6 +212,8 @@ void DynTypedNode::print(llvm::raw_ostream &OS,
210212
A->printPretty(OS, PP);
211213
else if (const ObjCProtocolLoc *P = get<ObjCProtocolLoc>())
212214
P->getProtocol()->print(OS, PP);
215+
else if (const ConceptReference *C = get<ConceptReference>())
216+
C->print(OS, PP);
213217
else
214218
OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
215219
}
@@ -222,6 +226,8 @@ void DynTypedNode::dump(llvm::raw_ostream &OS,
222226
S->dump(OS, Context);
223227
else if (const Type *T = get<Type>())
224228
T->dump(OS, Context);
229+
else if (const ConceptReference *C = get<ConceptReference>())
230+
C->dump(OS);
225231
else
226232
OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
227233
}
@@ -247,5 +253,7 @@ SourceRange DynTypedNode::getSourceRange() const {
247253
return A->getRange();
248254
if (const ObjCProtocolLoc *P = get<ObjCProtocolLoc>())
249255
return P->getSourceRange();
256+
if (const ConceptReference *C = get<ConceptReference>())
257+
return C->getSourceRange();
250258
return SourceRange();
251259
}

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ void TextNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
371371
OS << " selected";
372372
}
373373

374+
void TextNodeDumper::Visit(const ConceptReference *R) {
375+
if (!R) {
376+
ColorScope Color(OS, ShowColors, NullColor);
377+
OS << "<<<NULL>>> ConceptReference";
378+
return;
379+
}
380+
381+
OS << "ConceptReference";
382+
dumpPointer(R);
383+
dumpSourceRange(R->getSourceRange());
384+
OS << ' ';
385+
dumpBareDeclRef(R->getNamedConcept());
386+
}
387+
374388
void TextNodeDumper::Visit(const concepts::Requirement *R) {
375389
if (!R) {
376390
ColorScope Color(OS, ShowColors, NullColor);

clang/unittests/AST/ASTTypeTraitsTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ TEST(ASTNodeKind, Name) {
166166
VERIFY_NAME(QualType);
167167
VERIFY_NAME(TypeLoc);
168168
VERIFY_NAME(CXXCtorInitializer);
169+
VERIFY_NAME(ConceptReference);
169170
VERIFY_NAME(NestedNameSpecifier);
170171
VERIFY_NAME(Decl);
171172
VERIFY_NAME(CXXRecordDecl);
@@ -209,6 +210,8 @@ TEST(DynTypedNode, AttrSourceRange) {
209210
ast_matchers::attr()));
210211
}
211212

213+
// FIXME: add tests for ConceptReference once we add an ASTMatcher.
214+
212215
TEST(DynTypedNode, DeclDump) {
213216
DumpVerifier Verifier;
214217
Verifier.expectSubstring("FunctionDecl");

0 commit comments

Comments
 (0)