13#include "clang/AST/ASTTypeTraits.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclBase.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/DeclarationName.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/ExprConcepts.h"
23#include "clang/AST/ExprObjC.h"
24#include "clang/AST/NestedNameSpecifier.h"
25#include "clang/AST/PrettyPrinter.h"
26#include "clang/AST/RecursiveASTVisitor.h"
27#include "clang/AST/StmtVisitor.h"
28#include "clang/AST/TemplateBase.h"
29#include "clang/AST/Type.h"
30#include "clang/AST/TypeLoc.h"
31#include "clang/AST/TypeLocVisitor.h"
32#include "clang/AST/TypeVisitor.h"
33#include "clang/Basic/LangOptions.h"
34#include "clang/Basic/SourceLocation.h"
35#include "clang/Basic/SourceManager.h"
36#include "clang/Basic/Specifiers.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/ADT/SmallVector.h"
39#include "llvm/ADT/StringExtras.h"
40#include "llvm/Support/Casting.h"
41#include "llvm/Support/Compiler.h"
42#include "llvm/Support/raw_ostream.h"
52LLVM_ATTRIBUTE_UNUSED std::string nodeToString(
const DynTypedNode &N) {
53 std::string S = std::string(N.getNodeKind().asStringRef());
55 llvm::raw_string_ostream
OS(S);
57 N.print(
OS, PrintingPolicy(LangOptions()));
59 std::replace(S.begin(), S.end(),
'\n',
' ');
63const NamedDecl *getTemplatePattern(
const NamedDecl *D) {
64 if (
const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
65 if (
const auto *Result = CRD->getTemplateInstantiationPattern())
70 if (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
71 if (
const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
72 return Spec->getSpecializedTemplate()->getTemplatedDecl();
73 }
else if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
74 return FD->getTemplateInstantiationPattern();
75 }
else if (
auto *VD = dyn_cast<VarDecl>(D)) {
77 VarDecl *T = VD->getTemplateInstantiationPattern();
78 return (T == D) ? nullptr : T;
79 }
else if (
const auto *ED = dyn_cast<EnumDecl>(D)) {
80 return ED->getInstantiatedFromMemberEnum();
81 }
else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
82 if (
const auto *
Parent = llvm::dyn_cast<NamedDecl>(
D->getDeclContext()))
83 if (
const DeclContext *ParentPat =
84 dyn_cast_or_null<DeclContext>(getTemplatePattern(
Parent)))
85 for (
const NamedDecl *BaseND : ParentPat->lookup(
D->getDeclName()))
86 if (!BaseND->isImplicit() && BaseND->getKind() ==
D->getKind())
88 }
else if (
const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
89 if (
const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
90 if (
const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
91 for (
const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
100bool shouldSkipTypedef(
const TypedefNameDecl *TD) {
103 if (TD == TD->getASTContext().getObjCInstanceTypeDecl() ||
104 TD == TD->getASTContext().getObjCIdDecl())
131 using RelSet = DeclRelationSet;
135 const HeuristicResolver *Resolver;
136 llvm::SmallDenseMap<
const NamedDecl *,
137 std::pair<RelSet,
size_t>>
139 llvm::SmallDenseMap<const Decl *, RelSet> Seen;
142 template <
typename T>
void debug(T &Node, RelSet Flags) {
143 dlog(
"visit [{0}] {1}", Flags, nodeToString(DynTypedNode::create(Node)));
146 void report(
const NamedDecl *D, RelSet Flags) {
147 dlog(
"--> [{0}] {1}", Flags, nodeToString(DynTypedNode::create(*D)));
148 auto It = Decls.try_emplace(D, std::make_pair(Flags, Decls.size()));
151 It.first->second.first |=
Flags;
155 TargetFinder(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
157 llvm::SmallVector<std::pair<const NamedDecl *, RelSet>, 1> takeDecls()
const {
158 using ValTy = std::pair<const NamedDecl *, RelSet>;
159 llvm::SmallVector<ValTy, 1> Result;
160 Result.resize(Decls.size());
161 for (
const auto &Elem : Decls)
162 Result[Elem.second.second] = {Elem.first, Elem.second.first};
166 void add(
const Decl *Dcl, RelSet Flags) {
167 const NamedDecl *
D = llvm::dyn_cast_or_null<NamedDecl>(Dcl);
175 auto Res = Seen.try_emplace(D);
176 if (!Res.second && Res.first->second.contains(Flags))
178 Res.first->second |=
Flags;
180 if (
const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
181 D = UDD->getNominatedNamespaceAsWritten();
183 if (
const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
184 add(TND->getUnderlyingType(), Flags | Rel::Underlying);
186 }
else if (
const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
188 for (
const UsingShadowDecl *S : UD->shadows())
189 add(S->getUnderlyingDecl(), Flags);
191 }
else if (
const UsingEnumDecl *UED = dyn_cast<UsingEnumDecl>(D)) {
193 D = UED->getEnumDecl();
194 }
else if (
const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
195 add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
197 }
else if (
const UnresolvedUsingValueDecl *UUVD =
198 dyn_cast<UnresolvedUsingValueDecl>(D)) {
200 for (
const NamedDecl *Target : Resolver->resolveUsingValueDecl(UUVD)) {
205 }
else if (isa<UnresolvedUsingTypenameDecl>(D)) {
209 }
else if (
const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
214 if (llvm::isa<UsingDecl>(USD->getIntroducer()))
215 report(USD->getIntroducer(), Flags | Rel::Alias);
218 D = USD->getTargetDecl();
219 }
else if (
const auto *DG = dyn_cast<CXXDeductionGuideDecl>(D)) {
220 D = DG->getDeducedTemplate();
221 }
else if (
const ObjCImplementationDecl *IID =
222 dyn_cast<ObjCImplementationDecl>(D)) {
225 if (
const auto *CID = IID->getClassInterface())
226 if (
const auto *DD = CID->getDefinition())
227 if (!DD->isImplicitInterfaceDecl())
229 }
else if (
const ObjCCategoryImplDecl *CID =
230 dyn_cast<ObjCCategoryImplDecl>(D)) {
232 D = CID->getCategoryDecl();
237 if (
const Decl *Pat = getTemplatePattern(D)) {
239 add(Pat, Flags | Rel::TemplatePattern);
241 Flags |= Rel::TemplateInstantiation;
247 void add(
const Stmt *S, RelSet Flags) {
251 struct Visitor :
public ConstStmtVisitor<Visitor> {
254 Visitor(TargetFinder &Outer, RelSet
Flags) : Outer(Outer),
Flags(
Flags) {}
256 void VisitCallExpr(
const CallExpr *
CE) {
257 Outer.add(
CE->getCalleeDecl(),
Flags);
259 void VisitConceptSpecializationExpr(
const ConceptSpecializationExpr *
E) {
260 Outer.add(
E->getNamedConcept(),
Flags);
262 void VisitDeclRefExpr(
const DeclRefExpr *DRE) {
263 const Decl *
D = DRE->getDecl();
266 if (
auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
270 void VisitMemberExpr(
const MemberExpr *ME) {
271 const Decl *
D = ME->getMemberDecl();
273 llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
277 void VisitOverloadExpr(
const OverloadExpr *OE) {
278 for (
auto *D : OE->decls())
281 void VisitSizeOfPackExpr(
const SizeOfPackExpr *SE) {
282 Outer.add(SE->getPack(),
Flags);
284 void VisitCXXConstructExpr(
const CXXConstructExpr *CCE) {
285 Outer.add(CCE->getConstructor(),
Flags);
287 void VisitDesignatedInitExpr(
const DesignatedInitExpr *DIE) {
288 for (
const DesignatedInitExpr::Designator &D :
289 llvm::reverse(DIE->designators()))
290 if (
D.isFieldDesignator()) {
291 Outer.add(
D.getFieldDecl(),
Flags);
296 void VisitGotoStmt(
const GotoStmt *Goto) {
297 if (
auto *LabelDecl = Goto->getLabel())
298 Outer.add(LabelDecl,
Flags);
300 void VisitLabelStmt(
const LabelStmt *
Label) {
301 if (
auto *LabelDecl =
Label->getDecl())
302 Outer.add(LabelDecl,
Flags);
305 VisitCXXDependentScopeMemberExpr(
const CXXDependentScopeMemberExpr *
E) {
306 if (Outer.Resolver) {
307 for (
const NamedDecl *D : Outer.Resolver->resolveMemberExpr(
E)) {
312 void VisitDependentScopeDeclRefExpr(
const DependentScopeDeclRefExpr *
E) {
313 if (Outer.Resolver) {
314 for (
const NamedDecl *D : Outer.Resolver->resolveDeclRefExpr(
E)) {
319 void VisitObjCIvarRefExpr(
const ObjCIvarRefExpr *OIRE) {
320 Outer.add(OIRE->getDecl(),
Flags);
322 void VisitObjCMessageExpr(
const ObjCMessageExpr *OME) {
323 Outer.add(OME->getMethodDecl(),
Flags);
325 void VisitObjCPropertyRefExpr(
const ObjCPropertyRefExpr *OPRE) {
326 if (OPRE->isExplicitProperty())
327 Outer.add(OPRE->getExplicitProperty(),
Flags);
329 if (OPRE->isMessagingGetter())
330 Outer.add(OPRE->getImplicitPropertyGetter(),
Flags);
331 if (OPRE->isMessagingSetter())
332 Outer.add(OPRE->getImplicitPropertySetter(),
Flags);
335 void VisitObjCProtocolExpr(
const ObjCProtocolExpr *OPE) {
336 Outer.add(OPE->getProtocol(),
Flags);
338 void VisitOpaqueValueExpr(
const OpaqueValueExpr *OVE) {
339 Outer.add(OVE->getSourceExpr(),
Flags);
341 void VisitPseudoObjectExpr(
const PseudoObjectExpr *POE) {
342 Outer.add(POE->getSyntacticForm(),
Flags);
344 void VisitCXXNewExpr(
const CXXNewExpr *CNE) {
345 Outer.add(CNE->getOperatorNew(),
Flags);
347 void VisitCXXDeleteExpr(
const CXXDeleteExpr *CDE) {
348 Outer.add(CDE->getOperatorDelete(),
Flags);
351 Visitor(*
this, Flags).Visit(S);
354 void add(QualType T, RelSet Flags) {
358 struct Visitor :
public TypeVisitor<Visitor> {
361 Visitor(TargetFinder &Outer, RelSet
Flags) : Outer(Outer),
Flags(
Flags) {}
363 void VisitTagType(
const TagType *TT) {
364 Outer.add(TT->getAsTagDecl(),
Flags);
367 void VisitElaboratedType(
const ElaboratedType *ET) {
368 Outer.add(ET->desugar(),
Flags);
371 void VisitUsingType(
const UsingType *ET) {
372 Outer.add(ET->getFoundDecl(),
Flags);
375 void VisitInjectedClassNameType(
const InjectedClassNameType *ICNT) {
376 Outer.add(ICNT->getDecl(),
Flags);
379 void VisitDecltypeType(
const DecltypeType *DTT) {
380 Outer.add(DTT->getUnderlyingType(),
Flags | Rel::Underlying);
385 Outer.add(DT->getDeducedType(),
Flags);
387 void VisitUnresolvedUsingType(
const UnresolvedUsingType *UUT) {
388 Outer.add(UUT->getDecl(),
Flags);
390 void VisitDeducedTemplateSpecializationType(
391 const DeducedTemplateSpecializationType *DTST) {
392 if (
const auto *USD = DTST->getTemplateName().getAsUsingShadowDecl())
393 Outer.add(USD,
Flags);
401 if (
auto *TD = DTST->getTemplateName().getAsTemplateDecl())
402 Outer.add(TD->getTemplatedDecl(),
Flags | Rel::TemplatePattern);
404 void VisitDependentNameType(
const DependentNameType *DNT) {
405 if (Outer.Resolver) {
406 for (
const NamedDecl *ND :
407 Outer.Resolver->resolveDependentNameType(DNT)) {
408 Outer.add(ND,
Flags);
412 void VisitDependentTemplateSpecializationType(
413 const DependentTemplateSpecializationType *DTST) {
414 if (Outer.Resolver) {
415 for (
const NamedDecl *ND :
416 Outer.Resolver->resolveTemplateSpecializationType(DTST)) {
417 Outer.add(ND,
Flags);
421 void VisitTypedefType(
const TypedefType *TT) {
422 if (shouldSkipTypedef(TT->getDecl()))
424 Outer.add(TT->getDecl(),
Flags);
427 VisitTemplateSpecializationType(
const TemplateSpecializationType *TST) {
430 if (
const auto *UTN = TST->getTemplateName().getAsUsingShadowDecl())
431 Outer.add(UTN,
Flags);
437 if (TST->isTypeAlias()) {
438 Outer.add(TST->getAliasedType(),
Flags | Rel::Underlying);
442 TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
443 Flags | Rel::Alias | Rel::TemplatePattern);
447 else if (
const auto *
Parm =
448 llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
449 TST->getTemplateName().getAsTemplateDecl()))
452 else if (
const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
453 Outer.add(RD,
Flags);
456 if (
auto *TD = TST->getTemplateName().getAsTemplateDecl())
457 Outer.add(TD->getTemplatedDecl(),
Flags | Rel::TemplatePattern);
461 VisitSubstTemplateTypeParmType(
const SubstTemplateTypeParmType *STTPT) {
462 Outer.add(STTPT->getReplacementType(),
Flags);
464 void VisitTemplateTypeParmType(
const TemplateTypeParmType *TTPT) {
465 Outer.add(TTPT->getDecl(),
Flags);
467 void VisitObjCInterfaceType(
const ObjCInterfaceType *OIT) {
468 Outer.add(OIT->getDecl(),
Flags);
471 Visitor(*
this, Flags).Visit(T.getTypePtr());
474 void add(
const NestedNameSpecifier *NNS, RelSet Flags) {
478 switch (NNS->getKind()) {
479 case NestedNameSpecifier::Namespace:
480 add(NNS->getAsNamespace(), Flags);
482 case NestedNameSpecifier::NamespaceAlias:
483 add(NNS->getAsNamespaceAlias(), Flags);
485 case NestedNameSpecifier::Identifier:
487 add(QualType(Resolver->resolveNestedNameSpecifierToType(NNS), 0),
491 case NestedNameSpecifier::TypeSpec:
492 case NestedNameSpecifier::TypeSpecWithTemplate:
493 add(QualType(NNS->getAsType(), 0), Flags);
495 case NestedNameSpecifier::Global:
498 case NestedNameSpecifier::Super:
499 add(NNS->getAsRecordDecl(), Flags);
502 llvm_unreachable(
"unhandled NestedNameSpecifier::SpecifierKind");
505 void add(
const CXXCtorInitializer *CCI, RelSet Flags) {
510 if (CCI->isAnyMemberInitializer())
511 add(CCI->getAnyMember(), Flags);
515 void add(
const TemplateArgument &Arg, RelSet Flags) {
520 if (Arg.getKind() == TemplateArgument::Template ||
521 Arg.getKind() == TemplateArgument::TemplateExpansion) {
522 if (TemplateDecl *TD =
523 Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) {
526 if (
const auto *USD =
527 Arg.getAsTemplateOrTemplatePattern().getAsUsingShadowDecl())
535llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
537 dlog(
"allTargetDecls({0})", nodeToString(N));
538 TargetFinder Finder(Resolver);
541 Finder.add(D,
Flags);
542 else if (
const Stmt *S = N.get<Stmt>())
543 Finder.add(S,
Flags);
544 else if (
const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
545 Finder.add(NNSL->getNestedNameSpecifier(),
Flags);
546 else if (
const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
547 Finder.add(NNS,
Flags);
548 else if (
const TypeLoc *TL = N.get<TypeLoc>())
549 Finder.add(TL->getType(),
Flags);
550 else if (
const QualType *QT = N.get<QualType>())
551 Finder.add(*QT,
Flags);
552 else if (
const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
553 Finder.add(CCI,
Flags);
554 else if (
const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>())
555 Finder.add(TAL->getArgument(),
Flags);
556 else if (
const CXXBaseSpecifier *CBS = N.get<CXXBaseSpecifier>())
557 Finder.add(CBS->getTypeSourceInfo()->getType(),
Flags);
558 else if (
const ObjCProtocolLoc *PL = N.get<ObjCProtocolLoc>())
559 Finder.add(PL->getProtocol(),
Flags);
560 return Finder.takeDecls();
563llvm::SmallVector<const NamedDecl *, 1>
566 llvm::SmallVector<const NamedDecl *, 1> Result;
568 if (!(
Entry.second & ~Mask))
569 Result.push_back(
Entry.first);
574llvm::SmallVector<const NamedDecl *, 1>
579 "explicitReferenceTargets handles templates on its own");
586 llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
587 llvm::SmallVector<const NamedDecl *, 1> Targets;
588 bool SeenTemplateInstantiations =
false;
589 for (
auto &D : Decls) {
590 if (D.second & ~Mask)
593 TemplatePatterns.push_back(D.first);
597 SeenTemplateInstantiations =
true;
598 Targets.push_back(D.first);
600 if (!SeenTemplateInstantiations)
601 Targets.insert(Targets.end(), TemplatePatterns.begin(),
602 TemplatePatterns.end());
607llvm::SmallVector<ReferenceLoc> refInDecl(
const Decl *D,
608 const HeuristicResolver *Resolver) {
609 struct Visitor : ConstDeclVisitor<Visitor> {
610 Visitor(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
612 const HeuristicResolver *Resolver;
613 llvm::SmallVector<ReferenceLoc> Refs;
615 void VisitUsingDirectiveDecl(
const UsingDirectiveDecl *D) {
618 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
619 D->getIdentLocation(),
621 {D->getNominatedNamespaceAsWritten()}});
624 void VisitUsingDecl(
const UsingDecl *D) {
626 Refs.push_back(ReferenceLoc{
627 D->getQualifierLoc(),
D->getLocation(),
false,
629 DeclRelation::Underlying, Resolver)});
632 void VisitUsingEnumDecl(
const UsingEnumDecl *D) {
638 void VisitNamespaceAliasDecl(
const NamespaceAliasDecl *D) {
643 Refs.push_back(ReferenceLoc{
D->getQualifierLoc(),
644 D->getTargetNameLoc(),
646 {D->getAliasedNamespace()}});
649 void VisitNamedDecl(
const NamedDecl *ND) {
652 if (llvm::isa<ClassTemplateDecl>(ND) ||
653 llvm::isa<FunctionTemplateDecl>(ND) ||
654 llvm::isa<VarTemplateDecl>(ND) ||
655 llvm::isa<TypeAliasTemplateDecl>(ND))
658 if (llvm::isa<CXXDestructorDecl>(ND))
662 if (ND->getDeclName().isIdentifier() &&
663 !ND->getDeclName().getAsIdentifierInfo())
671 void VisitCXXDeductionGuideDecl(
const CXXDeductionGuideDecl *DG) {
674 Refs.push_back(ReferenceLoc{DG->getQualifierLoc(),
675 DG->getNameInfo().getLoc(),
677 {DG->getDeducedTemplate()}});
680 void VisitObjCMethodDecl(
const ObjCMethodDecl *OMD) {
682 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
683 OMD->getSelectorStartLoc(),
688 void VisitObjCCategoryDecl(
const ObjCCategoryDecl *OCD) {
690 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
693 {OCD->getClassInterface()}});
694 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
695 OCD->getCategoryNameLoc(),
700 void VisitObjCCategoryImplDecl(
const ObjCCategoryImplDecl *OCID) {
701 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
704 {OCID->getClassInterface()}});
705 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
706 OCID->getCategoryNameLoc(),
708 {OCID->getCategoryDecl()}});
717llvm::SmallVector<ReferenceLoc> refInStmt(
const Stmt *S,
718 const HeuristicResolver *Resolver) {
719 struct Visitor : ConstStmtVisitor<Visitor> {
720 Visitor(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
722 const HeuristicResolver *Resolver;
724 llvm::SmallVector<ReferenceLoc> Refs;
726 void VisitConceptSpecializationExpr(
const ConceptSpecializationExpr *
E) {
727 Refs.push_back(ReferenceLoc{
E->getNestedNameSpecifierLoc(),
728 E->getConceptNameLoc(),
730 {E->getNamedConcept()}});
733 void VisitDeclRefExpr(
const DeclRefExpr *
E) {
734 Refs.push_back(ReferenceLoc{
E->getQualifierLoc(),
735 E->getNameInfo().getLoc(),
737 {E->getFoundDecl()}});
740 void VisitDependentScopeDeclRefExpr(
const DependentScopeDeclRefExpr *
E) {
741 Refs.push_back(ReferenceLoc{
742 E->getQualifierLoc(),
E->getNameInfo().getLoc(),
false,
746 void VisitMemberExpr(
const MemberExpr *
E) {
749 if (llvm::isa<CXXDestructorDecl>(
E->getFoundDecl().getDecl()))
751 Refs.push_back(ReferenceLoc{
E->getQualifierLoc(),
752 E->getMemberNameInfo().getLoc(),
754 {E->getFoundDecl()}});
758 VisitCXXDependentScopeMemberExpr(
const CXXDependentScopeMemberExpr *
E) {
759 Refs.push_back(ReferenceLoc{
760 E->getQualifierLoc(),
E->getMemberNameInfo().getLoc(),
765 void VisitOverloadExpr(
const OverloadExpr *
E) {
766 Refs.push_back(ReferenceLoc{
E->getQualifierLoc(),
767 E->getNameInfo().getLoc(),
769 llvm::SmallVector<const NamedDecl *, 1>(
770 E->decls().begin(),
E->decls().end())});
773 void VisitSizeOfPackExpr(
const SizeOfPackExpr *
E) {
774 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
780 void VisitObjCPropertyRefExpr(
const ObjCPropertyRefExpr *
E) {
781 Refs.push_back(ReferenceLoc{
782 NestedNameSpecifierLoc(),
E->getLocation(),
788 void VisitObjCIvarRefExpr(
const ObjCIvarRefExpr *OIRE) {
789 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
795 void VisitObjCMessageExpr(
const ObjCMessageExpr *
E) {
797 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
798 E->getSelectorStartLoc(),
800 {
E->getMethodDecl()}});
803 void VisitDesignatedInitExpr(
const DesignatedInitExpr *DIE) {
804 for (
const DesignatedInitExpr::Designator &D : DIE->designators()) {
805 if (!
D.isFieldDesignator())
808 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
811 {
D.getFieldDecl()}});
815 void VisitGotoStmt(
const GotoStmt *GS) {
816 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
822 void VisitLabelStmt(
const LabelStmt *LS) {
823 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
835llvm::SmallVector<ReferenceLoc>
836refInTypeLoc(TypeLoc L,
const HeuristicResolver *Resolver) {
837 struct Visitor : TypeLocVisitor<Visitor> {
838 Visitor(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
840 const HeuristicResolver *Resolver;
841 llvm::SmallVector<ReferenceLoc> Refs;
843 void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
845 size_t InitialSize = Refs.size();
846 Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
847 size_t NewSize = Refs.size();
849 for (
unsigned I = InitialSize; I < NewSize; ++I) {
850 ReferenceLoc *Ref = &Refs[I];
852 assert(!Ref->Qualifier.hasQualifier() &&
"qualifier already set");
853 Ref->Qualifier = L.getQualifierLoc();
857 void VisitUsingTypeLoc(UsingTypeLoc L) {
858 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
859 L.getLocalSourceRange().getBegin(),
861 {L.getFoundDecl()}});
864 void VisitTagTypeLoc(TagTypeLoc L) {
865 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
871 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
872 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
878 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
887 Refs.push_back(ReferenceLoc{
888 NestedNameSpecifierLoc(), L.getTemplateNameLoc(),
false,
890 DeclRelation::Alias, Resolver)});
892 void VisitDeducedTemplateSpecializationTypeLoc(
893 DeducedTemplateSpecializationTypeLoc L) {
894 Refs.push_back(ReferenceLoc{
895 NestedNameSpecifierLoc(), L.getNameLoc(),
false,
897 DeclRelation::Alias, Resolver)});
900 void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
901 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
907 void VisitDependentTemplateSpecializationTypeLoc(
908 DependentTemplateSpecializationTypeLoc L) {
910 ReferenceLoc{L.getQualifierLoc(), L.getTemplateNameLoc(),
913 DynTypedNode::create(L.getType()), {}, Resolver)});
916 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
918 ReferenceLoc{L.getQualifierLoc(), L.getNameLoc(),
921 DynTypedNode::create(L.getType()), {}, Resolver)});
924 void VisitTypedefTypeLoc(TypedefTypeLoc L) {
925 if (shouldSkipTypedef(L.getTypedefNameDecl()))
927 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
930 {L.getTypedefNameDecl()}});
933 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc L) {
934 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
937 {L.getIFaceDecl()}});
942 V.Visit(L.getUnqualifiedLoc());
946class ExplicitReferenceCollector
949 ExplicitReferenceCollector(llvm::function_ref<
void(ReferenceLoc)> Out,
950 const HeuristicResolver *Resolver)
951 :
Out(
Out), Resolver(Resolver) {
955 bool VisitTypeLoc(TypeLoc TTL) {
956 if (TypeLocsToSkip.count(TTL.getBeginLoc()))
958 visitNode(DynTypedNode::create(TTL));
962 bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
965 TypeLoc
Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
966 if (L.getBeginLoc() ==
Inner.getBeginLoc())
967 return RecursiveASTVisitor::TraverseTypeLoc(
Inner);
969 TypeLocsToSkip.insert(
Inner.getBeginLoc());
970 return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
973 bool VisitStmt(Stmt *S) {
974 visitNode(DynTypedNode::create(*S));
978 bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) {
979 visitNode(DynTypedNode::create(*OVE));
982 return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr());
985 bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) {
986 visitNode(DynTypedNode::create(*POE));
989 return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm());
995 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
996 switch (
A.getArgument().getKind()) {
997 case TemplateArgument::Template:
998 case TemplateArgument::TemplateExpansion:
999 reportReference(ReferenceLoc{
A.getTemplateQualifierLoc(),
1000 A.getTemplateNameLoc(),
1003 .getAsTemplateOrTemplatePattern()
1004 .getAsTemplateDecl()}},
1005 DynTypedNode::create(
A.getArgument()));
1007 case TemplateArgument::Declaration:
1009 case TemplateArgument::Integral:
1010 case TemplateArgument::Null:
1011 case TemplateArgument::NullPtr:
1013 case TemplateArgument::Pack:
1014 case TemplateArgument::Type:
1015 case TemplateArgument::Expression:
1018 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
1021 bool VisitDecl(
Decl *D) {
1022 visitNode(DynTypedNode::create(*D));
1027 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
1028 if (!L.getNestedNameSpecifier())
1030 visitNode(DynTypedNode::create(L));
1032 if (
auto TL = L.getTypeLoc())
1033 TypeLocsToSkip.insert(TL.getBeginLoc());
1034 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
1037 bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) {
1038 visitNode(DynTypedNode::create(ProtocolLoc));
1042 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
1043 visitNode(DynTypedNode::create(*Init));
1044 return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
1047 bool TraverseTypeConstraint(
const TypeConstraint *TC) {
1051 Out(ReferenceLoc{TC->getNestedNameSpecifierLoc(),
1052 TC->getConceptNameLoc(),
1054 {TC->getNamedConcept()}});
1055 return RecursiveASTVisitor::TraverseTypeConstraint(TC);
1073 llvm::SmallVector<ReferenceLoc> explicitReference(DynTypedNode N) {
1074 if (
auto *D = N.get<
Decl>())
1075 return refInDecl(D, Resolver);
1076 if (
auto *S = N.get<Stmt>())
1077 return refInStmt(S, Resolver);
1078 if (
auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
1080 return {ReferenceLoc{
1081 NNSL->getPrefix(), NNSL->getLocalBeginLoc(),
false,
1083 DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
1086 if (
const TypeLoc *TL = N.get<TypeLoc>())
1087 return refInTypeLoc(*TL, Resolver);
1088 if (
const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
1091 if (CCI->isAnyMemberInitializer()) {
1092 return {ReferenceLoc{NestedNameSpecifierLoc(),
1093 CCI->getMemberLocation(),
1095 {CCI->getAnyMember()}}};
1098 if (
const ObjCProtocolLoc *PL = N.get<ObjCProtocolLoc>())
1099 return {ReferenceLoc{NestedNameSpecifierLoc(),
1102 {PL->getProtocol()}}};
1108 void visitNode(DynTypedNode N) {
1109 for (
auto &R : explicitReference(N))
1110 reportReference(std::move(R), N);
1113 void reportReference(ReferenceLoc &&Ref, DynTypedNode N) {
1116 llvm::erase_value(Ref.Targets,
nullptr);
1121 if (Ref.NameLoc.isInvalid()) {
1122 dlog(
"invalid location at node {0}", nodeToString(N));
1128 llvm::function_ref<void(ReferenceLoc)>
Out;
1129 const HeuristicResolver *Resolver;
1132 llvm::DenseSet<SourceLocation> TypeLocsToSkip;
1140 ExplicitReferenceCollector(
Out, Resolver).TraverseStmt(
const_cast<Stmt *
>(S));
1146 ExplicitReferenceCollector(
Out, Resolver).TraverseDecl(
const_cast<Decl *
>(D));
1151 ExplicitReferenceCollector(
Out, Resolver)
1152 .TraverseAST(
const_cast<ASTContext &
>(
AST));
1157#define REL_CASE(X) \
1158 case DeclRelation::X: \
1166 llvm_unreachable(
"Unhandled DeclRelation enum");
1169 const char *Sep =
"";
1170 for (
unsigned I = 0; I < RS.S.size(); ++I) {
1172 OS << Sep << static_cast<DeclRelation>(I);
1181 OS <<
"targets = {";
1182 llvm::SmallVector<std::string> Targets;
1183 for (
const NamedDecl *T : R.
Targets) {
1184 llvm::raw_string_ostream Target(Targets.emplace_back());
1187 llvm::sort(Targets);
1188 OS << llvm::join(Targets,
", ");
1191 OS <<
", qualifier = '";
1193 PrintingPolicy(LangOptions()));
const FunctionDecl * Decl
std::pair< Context, Canceler > Inner
CompiledFragmentImpl & Out
llvm::raw_string_ostream OS
std::string printTemplateSpecializationArgs(const NamedDecl &ND)
Prints template arguments of a decl as written in the source code, including enclosing '<' and '>',...
llvm::SmallVector< std::pair< const NamedDecl *, DeclRelationSet >, 1 > allTargetDecls(const DynTypedNode &N, const HeuristicResolver *Resolver)
Similar to targetDecl(), however instead of applying a filter, all possible decls are returned along ...
llvm::SmallVector< const NamedDecl *, 1 > explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask, const HeuristicResolver *Resolver)
Find declarations explicitly referenced in the source code defined by N.
void findExplicitReferences(const Stmt *S, llvm::function_ref< void(ReferenceLoc)> Out, const HeuristicResolver *Resolver)
Recursively traverse S and report all references explicitly written in the code.
NestedNameSpecifierLoc getQualifierLoc(const NamedDecl &ND)
Returns a nested name specifier loc of ND if it was present in the source, e.g.
llvm::SmallVector< const NamedDecl *, 1 > targetDecl(const DynTypedNode &N, DeclRelationSet Mask, const HeuristicResolver *Resolver)
targetDecl() finds the declaration referred to by an AST node.
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
@ Underlying
This is the underlying declaration for a renaming-alias, decltype etc.
@ TemplatePattern
This is the pattern the template specialization was instantiated from.
@ TemplateInstantiation
This is the template instantiation that was referred to.
@ Alias
This declaration is an alias that was referred to.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Information about a reference written in the source code, independent of the actual AST node that thi...
NestedNameSpecifierLoc Qualifier
Contains qualifier written in the code, if any, e.g. 'ns::' for 'ns::foo'.
bool IsDecl
True if the reference is a declaration or definition;.
llvm::SmallVector< const NamedDecl *, 1 > Targets
A list of targets referenced by this name.