12#include "clang/AST/ASTConcept.h"
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 "clang/Sema/HeuristicResolver.h"
38#include "llvm/ADT/STLExtras.h"
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/Compiler.h"
43#include "llvm/Support/raw_ostream.h"
53LLVM_ATTRIBUTE_UNUSED std::string nodeToString(
const DynTypedNode &N) {
54 std::string S = std::string(N.getNodeKind().asStringRef());
56 llvm::raw_string_ostream OS(S);
58 N.print(OS, PrintingPolicy(LangOptions()));
60 llvm::replace(S,
'\n',
' ');
64const NamedDecl *getTemplatePattern(
const NamedDecl *D) {
65 if (
const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
66 if (
const auto *Result = CRD->getTemplateInstantiationPattern())
71 if (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
72 if (
const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
73 return Spec->getSpecializedTemplate()->getTemplatedDecl();
74 }
else if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
75 return FD->getTemplateInstantiationPattern();
76 }
else if (
auto *VD = dyn_cast<VarDecl>(D)) {
78 VarDecl *
T = VD->getTemplateInstantiationPattern();
79 return (T == D) ? nullptr :
T;
80 }
else if (
const auto *ED = dyn_cast<EnumDecl>(D)) {
81 return ED->getInstantiatedFromMemberEnum();
82 }
else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
83 if (
const auto *Parent = llvm::dyn_cast<NamedDecl>(
D->getDeclContext()))
84 if (
const DeclContext *ParentPat =
85 dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
86 for (
const NamedDecl *BaseND : ParentPat->lookup(
D->getDeclName()))
87 if (!BaseND->isImplicit() && BaseND->getKind() ==
D->getKind())
89 }
else if (
const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
90 if (
const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
91 if (
const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
92 for (
const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
101bool shouldSkipTypedef(
const TypedefNameDecl *TD) {
104 if (TD == TD->getASTContext().getObjCInstanceTypeDecl() ||
105 TD == TD->getASTContext().getObjCIdDecl())
132 using RelSet = DeclRelationSet;
136 const HeuristicResolver *Resolver;
137 llvm::SmallDenseMap<
const NamedDecl *,
138 std::pair<RelSet,
size_t>>
140 llvm::SmallDenseMap<const Decl *, RelSet> Seen;
143 template <
typename T>
void debug(T &Node, RelSet Flags) {
144 dlog(
"visit [{0}] {1}", Flags, nodeToString(DynTypedNode::create(Node)));
147 void report(
const NamedDecl *D, RelSet Flags) {
148 dlog(
"--> [{0}] {1}", Flags, nodeToString(DynTypedNode::create(*D)));
149 auto It = Decls.try_emplace(D, std::make_pair(Flags, Decls.size()));
152 It.first->second.first |= Flags;
156 TargetFinder(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
158 llvm::SmallVector<std::pair<const NamedDecl *, RelSet>, 1> takeDecls()
const {
159 using ValTy = std::pair<const NamedDecl *, RelSet>;
160 llvm::SmallVector<ValTy, 1> Result;
161 Result.resize(Decls.size());
162 for (
const auto &Elem : Decls)
163 Result[Elem.second.second] = {Elem.first, Elem.second.first};
167 void add(
const Decl *Dcl, RelSet Flags) {
168 const NamedDecl *
D = llvm::dyn_cast_or_null<NamedDecl>(Dcl);
176 auto Res = Seen.try_emplace(D);
177 if (!Res.second && Res.first->second.contains(Flags))
179 Res.first->second |= Flags;
181 if (
const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
182 D = UDD->getNominatedNamespaceAsWritten();
184 if (
const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
185 add(TND->getUnderlyingType(), Flags | Rel::Underlying);
187 }
else if (
const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
189 for (
const UsingShadowDecl *S : UD->shadows())
190 add(S->getUnderlyingDecl(), Flags);
192 }
else if (
const UsingEnumDecl *UED = dyn_cast<UsingEnumDecl>(D)) {
194 D = UED->getEnumDecl();
195 }
else if (
const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
196 add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
198 }
else if (
const UnresolvedUsingValueDecl *UUVD =
199 dyn_cast<UnresolvedUsingValueDecl>(D)) {
201 for (
const NamedDecl *Target : Resolver->resolveUsingValueDecl(UUVD)) {
206 }
else if (isa<UnresolvedUsingTypenameDecl>(D)) {
210 }
else if (
const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
215 if (llvm::isa<UsingDecl>(USD->getIntroducer()))
216 report(USD->getIntroducer(), Flags | Rel::Alias);
219 D = USD->getTargetDecl();
220 }
else if (
const auto *DG = dyn_cast<CXXDeductionGuideDecl>(D)) {
221 D = DG->getDeducedTemplate();
222 }
else if (
const ObjCImplementationDecl *IID =
223 dyn_cast<ObjCImplementationDecl>(D)) {
226 if (
const auto *CID = IID->getClassInterface())
227 if (
const auto *DD = CID->getDefinition())
228 if (!DD->isImplicitInterfaceDecl())
230 }
else if (
const ObjCCategoryImplDecl *CID =
231 dyn_cast<ObjCCategoryImplDecl>(D)) {
233 D = CID->getCategoryDecl();
238 if (
const Decl *Pat = getTemplatePattern(D)) {
240 add(Pat, Flags | Rel::TemplatePattern);
242 Flags |= Rel::TemplateInstantiation;
248 void add(
const Stmt *S, RelSet Flags) {
252 struct Visitor :
public ConstStmtVisitor<Visitor> {
255 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
257 void VisitCallExpr(
const CallExpr *CE) {
258 Outer.add(CE->getCalleeDecl(), Flags);
260 void VisitConceptSpecializationExpr(
const ConceptSpecializationExpr *E) {
261 Outer.add(E->getConceptReference(), Flags);
263 void VisitDeclRefExpr(
const DeclRefExpr *DRE) {
264 const Decl *
D = DRE->getDecl();
267 if (
auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
271 void VisitMemberExpr(
const MemberExpr *ME) {
272 const Decl *
D = ME->getMemberDecl();
274 llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
278 void VisitOverloadExpr(
const OverloadExpr *OE) {
279 for (
auto *D : OE->decls())
282 void VisitSizeOfPackExpr(
const SizeOfPackExpr *SE) {
283 Outer.add(SE->getPack(), Flags);
285 void VisitCXXConstructExpr(
const CXXConstructExpr *CCE) {
286 Outer.add(CCE->getConstructor(), Flags);
288 void VisitDesignatedInitExpr(
const DesignatedInitExpr *DIE) {
289 for (
const DesignatedInitExpr::Designator &D :
290 llvm::reverse(DIE->designators()))
291 if (
D.isFieldDesignator()) {
292 Outer.add(
D.getFieldDecl(), Flags);
297 void VisitGotoStmt(
const GotoStmt *Goto) {
298 if (
auto *LabelDecl = Goto->getLabel())
299 Outer.add(LabelDecl, Flags);
301 void VisitLabelStmt(
const LabelStmt *Label) {
302 if (
auto *LabelDecl =
Label->getDecl())
303 Outer.add(LabelDecl, Flags);
306 VisitCXXDependentScopeMemberExpr(
const CXXDependentScopeMemberExpr *E) {
307 if (Outer.Resolver) {
308 for (
const NamedDecl *D : Outer.Resolver->resolveMemberExpr(E)) {
313 void VisitDependentScopeDeclRefExpr(
const DependentScopeDeclRefExpr *E) {
314 if (Outer.Resolver) {
315 for (
const NamedDecl *D : Outer.Resolver->resolveDeclRefExpr(E)) {
320 void VisitObjCIvarRefExpr(
const ObjCIvarRefExpr *OIRE) {
321 Outer.add(OIRE->getDecl(), Flags);
323 void VisitObjCMessageExpr(
const ObjCMessageExpr *OME) {
324 Outer.add(OME->getMethodDecl(), Flags);
326 void VisitObjCPropertyRefExpr(
const ObjCPropertyRefExpr *OPRE) {
327 if (OPRE->isExplicitProperty())
328 Outer.add(OPRE->getExplicitProperty(), Flags);
330 if (OPRE->isMessagingGetter())
331 Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
332 if (OPRE->isMessagingSetter())
333 Outer.add(OPRE->getImplicitPropertySetter(), Flags);
336 void VisitObjCProtocolExpr(
const ObjCProtocolExpr *OPE) {
337 Outer.add(OPE->getProtocol(), Flags);
339 void VisitOpaqueValueExpr(
const OpaqueValueExpr *OVE) {
340 Outer.add(OVE->getSourceExpr(), Flags);
342 void VisitPseudoObjectExpr(
const PseudoObjectExpr *POE) {
343 Outer.add(POE->getSyntacticForm(), Flags);
345 void VisitCXXNewExpr(
const CXXNewExpr *CNE) {
346 Outer.add(CNE->getOperatorNew(), Flags);
348 void VisitCXXDeleteExpr(
const CXXDeleteExpr *CDE) {
349 Outer.add(CDE->getOperatorDelete(), Flags);
352 VisitCXXRewrittenBinaryOperator(
const CXXRewrittenBinaryOperator *RBO) {
353 Outer.add(RBO->getDecomposedForm().InnerBinOp, Flags);
356 Visitor(*
this, Flags).Visit(S);
359 void add(QualType T, RelSet Flags) {
363 struct Visitor :
public TypeVisitor<Visitor> {
366 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
368 void VisitTagType(
const TagType *TT) {
369 Outer.add(cast<TagType>(TT)->getOriginalDecl(), Flags);
372 void VisitUsingType(
const UsingType *ET) {
373 Outer.add(ET->getDecl(), Flags);
376 void VisitDecltypeType(
const DecltypeType *DTT) {
377 Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
379 void VisitDeducedType(
const DeducedType *DT) {
382 Outer.add(DT->getDeducedType(), Flags);
384 void VisitUnresolvedUsingType(
const UnresolvedUsingType *UUT) {
385 Outer.add(UUT->getDecl(), Flags);
387 void VisitDeducedTemplateSpecializationType(
388 const DeducedTemplateSpecializationType *DTST) {
389 if (
const auto *USD = DTST->getTemplateName().getAsUsingShadowDecl())
390 Outer.add(USD, Flags);
398 if (
auto *TD = DTST->getTemplateName().getAsTemplateDecl())
399 Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
401 void VisitDependentNameType(
const DependentNameType *DNT) {
402 if (Outer.Resolver) {
403 for (
const NamedDecl *ND :
404 Outer.Resolver->resolveDependentNameType(DNT)) {
405 Outer.add(ND, Flags);
409 void VisitTypedefType(
const TypedefType *TT) {
410 if (shouldSkipTypedef(TT->getDecl()))
412 Outer.add(TT->getDecl(), Flags);
415 VisitTemplateSpecializationType(
const TemplateSpecializationType *TST) {
418 if (
const auto *UTN = TST->getTemplateName().getAsUsingShadowDecl())
419 Outer.add(UTN, Flags);
425 if (TST->isTypeAlias()) {
426 Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
430 TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl();
435 if (llvm::isa<BuiltinTemplateDecl>(TD))
437 Outer.report(TD->getTemplatedDecl(),
438 Flags | Rel::Alias | Rel::TemplatePattern);
442 else if (
const auto *Parm =
443 llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
444 TST->getTemplateName().getAsTemplateDecl()))
445 Outer.add(Parm, Flags);
447 else if (
const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
448 Outer.add(RD, Flags);
449 else if (
auto *TD = TST->getTemplateName().getAsTemplateDecl())
451 Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
452 else if (Outer.Resolver)
453 for (
const NamedDecl *ND :
454 Outer.Resolver->resolveTemplateSpecializationType(TST))
455 Outer.add(ND, Flags);
458 VisitSubstTemplateTypeParmType(
const SubstTemplateTypeParmType *STTPT) {
459 Outer.add(STTPT->getReplacementType(), Flags);
461 void VisitTemplateTypeParmType(
const TemplateTypeParmType *TTPT) {
462 Outer.add(TTPT->getDecl(), Flags);
464 void VisitObjCInterfaceType(
const ObjCInterfaceType *OIT) {
465 Outer.add(OIT->getDecl(), Flags);
468 Visitor(*
this, Flags).Visit(
T.getTypePtr());
471 void add(NestedNameSpecifier NNS, RelSet Flags) {
475 switch (NNS.getKind()) {
476 case NestedNameSpecifier::Kind::Namespace:
477 add(NNS.getAsNamespaceAndPrefix().Namespace, Flags);
479 case NestedNameSpecifier::Kind::Type:
480 add(QualType(NNS.getAsType(), 0), Flags);
482 case NestedNameSpecifier::Kind::Global:
485 case NestedNameSpecifier::Kind::MicrosoftSuper:
486 add(NNS.getAsMicrosoftSuper(), Flags);
488 case NestedNameSpecifier::Kind::Null:
489 llvm_unreachable(
"unexpected null nested name specifier");
491 llvm_unreachable(
"unhandled NestedNameSpecifier::Kind");
494 void add(
const CXXCtorInitializer *CCI, RelSet Flags) {
499 if (CCI->isAnyMemberInitializer())
500 add(CCI->getAnyMember(), Flags);
504 void add(
const TemplateArgument &Arg, RelSet Flags) {
509 if (Arg.getKind() == TemplateArgument::Template ||
510 Arg.getKind() == TemplateArgument::TemplateExpansion) {
511 if (TemplateDecl *TD =
512 Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) {
515 if (
const auto *USD =
516 Arg.getAsTemplateOrTemplatePattern().getAsUsingShadowDecl())
521 void add(
const ConceptReference *CR, RelSet Flags) {
522 add(CR->getNamedConcept(), Flags);
528llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
530 dlog(
"allTargetDecls({0})", nodeToString(N));
531 TargetFinder Finder(Resolver);
533 if (
const Decl *D = N.get<Decl>())
534 Finder.add(D, Flags);
535 else if (
const Stmt *S = N.get<Stmt>())
536 Finder.add(S, Flags);
537 else if (
const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
538 Finder.add(NNSL->getNestedNameSpecifier(), Flags);
539 else if (
const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
540 Finder.add(*NNS, Flags);
541 else if (
const TypeLoc *TL = N.get<TypeLoc>())
542 Finder.add(TL->getType(), Flags);
543 else if (
const QualType *QT = N.get<QualType>())
544 Finder.add(*QT, Flags);
545 else if (
const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
546 Finder.add(CCI, Flags);
547 else if (
const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>())
548 Finder.add(TAL->getArgument(), Flags);
549 else if (
const CXXBaseSpecifier *CBS = N.get<CXXBaseSpecifier>())
550 Finder.add(CBS->getTypeSourceInfo()->getType(), Flags);
551 else if (
const ObjCProtocolLoc *PL = N.get<ObjCProtocolLoc>())
552 Finder.add(PL->getProtocol(), Flags);
553 else if (
const ConceptReference *CR = N.get<ConceptReference>())
554 Finder.add(CR, Flags);
555 return Finder.takeDecls();
558llvm::SmallVector<const NamedDecl *, 1>
560 const HeuristicResolver *Resolver) {
561 llvm::SmallVector<const NamedDecl *, 1> Result;
563 if (!(
Entry.second & ~Mask))
564 Result.push_back(
Entry.first);
569llvm::SmallVector<const NamedDecl *, 1>
571 const HeuristicResolver *Resolver) {
574 "explicitReferenceTargets handles templates on its own");
581 llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
582 llvm::SmallVector<const NamedDecl *, 1> Targets;
583 bool SeenTemplateInstantiations =
false;
584 for (
auto &D : Decls) {
585 if (D.second & ~Mask)
588 TemplatePatterns.push_back(D.first);
592 SeenTemplateInstantiations =
true;
593 Targets.push_back(D.first);
595 if (!SeenTemplateInstantiations)
596 Targets.insert(Targets.end(), TemplatePatterns.begin(),
597 TemplatePatterns.end());
602llvm::SmallVector<ReferenceLoc> refInDecl(
const Decl *D,
603 const HeuristicResolver *Resolver) {
604 struct Visitor : ConstDeclVisitor<Visitor> {
605 Visitor(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
607 const HeuristicResolver *Resolver;
608 llvm::SmallVector<ReferenceLoc> Refs;
610 void VisitUsingDirectiveDecl(
const UsingDirectiveDecl *D) {
613 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
614 D->getIdentLocation(),
616 {D->getNominatedNamespaceAsWritten()}});
619 void VisitUsingDecl(
const UsingDecl *D) {
621 Refs.push_back(ReferenceLoc{
622 D->getQualifierLoc(),
D->getLocation(),
false,
624 DeclRelation::Underlying, Resolver)});
627 void VisitUsingEnumDecl(
const UsingEnumDecl *D) {
633 void VisitNamespaceAliasDecl(
const NamespaceAliasDecl *D) {
638 Refs.push_back(ReferenceLoc{
D->getQualifierLoc(),
639 D->getTargetNameLoc(),
641 {
D->getAliasedNamespace()}});
644 void VisitNamedDecl(
const NamedDecl *ND) {
647 if (llvm::isa<ClassTemplateDecl>(ND) ||
648 llvm::isa<FunctionTemplateDecl>(ND) ||
649 llvm::isa<VarTemplateDecl>(ND) ||
650 llvm::isa<TypeAliasTemplateDecl>(ND))
653 if (llvm::isa<CXXDestructorDecl>(ND))
657 if (ND->getDeclName().isIdentifier() &&
658 !ND->getDeclName().getAsIdentifierInfo())
666 void VisitCXXDeductionGuideDecl(
const CXXDeductionGuideDecl *DG) {
669 Refs.push_back(ReferenceLoc{DG->getQualifierLoc(),
670 DG->getNameInfo().getLoc(),
672 {DG->getDeducedTemplate()}});
675 void VisitObjCMethodDecl(
const ObjCMethodDecl *OMD) {
677 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
678 OMD->getSelectorStartLoc(),
683 void VisitObjCCategoryDecl(
const ObjCCategoryDecl *OCD) {
685 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
688 {OCD->getClassInterface()}});
689 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
690 OCD->getCategoryNameLoc(),
695 void VisitObjCCategoryImplDecl(
const ObjCCategoryImplDecl *OCID) {
696 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
699 {OCID->getClassInterface()}});
700 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
701 OCID->getCategoryNameLoc(),
703 {OCID->getCategoryDecl()}});
704 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
705 OCID->getCategoryNameLoc(),
710 void VisitObjCImplementationDecl(
const ObjCImplementationDecl *OIMD) {
711 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
714 {OIMD->getClassInterface()}});
715 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
727llvm::SmallVector<ReferenceLoc> refInStmt(
const Stmt *S,
728 const HeuristicResolver *Resolver) {
729 struct Visitor : ConstStmtVisitor<Visitor> {
730 Visitor(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
732 const HeuristicResolver *Resolver;
734 llvm::SmallVector<ReferenceLoc> Refs;
736 void VisitDeclRefExpr(
const DeclRefExpr *E) {
737 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
738 E->getNameInfo().getLoc(),
740 {E->getFoundDecl()}});
743 void VisitDependentScopeDeclRefExpr(
const DependentScopeDeclRefExpr *E) {
744 Refs.push_back(ReferenceLoc{
745 E->getQualifierLoc(), E->getNameInfo().getLoc(),
false,
749 void VisitMemberExpr(
const MemberExpr *E) {
752 if (llvm::isa<CXXDestructorDecl>(E->getFoundDecl().getDecl()))
754 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
755 E->getMemberNameInfo().getLoc(),
757 {E->getFoundDecl()}});
761 VisitCXXDependentScopeMemberExpr(
const CXXDependentScopeMemberExpr *E) {
762 Refs.push_back(ReferenceLoc{
763 E->getQualifierLoc(), E->getMemberNameInfo().getLoc(),
768 void VisitOverloadExpr(
const OverloadExpr *E) {
769 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
770 E->getNameInfo().getLoc(),
772 llvm::SmallVector<const NamedDecl *, 1>(
773 E->decls().begin(), E->decls().end())});
776 void VisitSizeOfPackExpr(
const SizeOfPackExpr *E) {
777 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
783 void VisitObjCPropertyRefExpr(
const ObjCPropertyRefExpr *E) {
784 Refs.push_back(ReferenceLoc{
785 NestedNameSpecifierLoc(), E->getLocation(),
791 void VisitObjCIvarRefExpr(
const ObjCIvarRefExpr *OIRE) {
792 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
798 void VisitObjCMessageExpr(
const ObjCMessageExpr *E) {
800 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
801 E->getSelectorStartLoc(),
803 {E->getMethodDecl()}});
806 void VisitDesignatedInitExpr(
const DesignatedInitExpr *DIE) {
807 for (
const DesignatedInitExpr::Designator &D : DIE->designators()) {
808 if (!
D.isFieldDesignator())
811 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
814 {
D.getFieldDecl()}});
818 void VisitGotoStmt(
const GotoStmt *GS) {
819 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
825 void VisitLabelStmt(
const LabelStmt *LS) {
826 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
838llvm::SmallVector<ReferenceLoc>
839refInTypeLoc(TypeLoc L,
const HeuristicResolver *Resolver) {
840 struct Visitor : TypeLocVisitor<Visitor> {
841 Visitor(
const HeuristicResolver *Resolver) : Resolver(Resolver) {}
843 const HeuristicResolver *Resolver;
844 llvm::SmallVector<ReferenceLoc> Refs;
846 void VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc L) {
847 Refs.push_back(ReferenceLoc{L.getQualifierLoc(),
848 L.getLocalSourceRange().getBegin(),
853 void VisitUsingTypeLoc(UsingTypeLoc L) {
854 Refs.push_back(ReferenceLoc{L.getQualifierLoc(),
855 L.getLocalSourceRange().getBegin(),
860 void VisitTagTypeLoc(TagTypeLoc L) {
861 Refs.push_back(ReferenceLoc{L.getQualifierLoc(),
864 {L.getOriginalDecl()}});
867 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
868 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
874 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
883 Refs.push_back(ReferenceLoc{
884 L.getQualifierLoc(), L.getTemplateNameLoc(),
false,
886 DeclRelation::Alias, Resolver)});
888 void VisitDeducedTemplateSpecializationTypeLoc(
889 DeducedTemplateSpecializationTypeLoc L) {
890 Refs.push_back(ReferenceLoc{
891 L.getQualifierLoc(), L.getNameLoc(),
false,
893 DeclRelation::Alias, Resolver)});
896 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
898 ReferenceLoc{L.getQualifierLoc(), L.getNameLoc(),
901 DynTypedNode::create(L.getType()), {}, Resolver)});
904 void VisitTypedefTypeLoc(TypedefTypeLoc L) {
905 if (shouldSkipTypedef(L.getDecl()))
907 Refs.push_back(ReferenceLoc{L.getQualifierLoc(),
913 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc L) {
914 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
917 {L.getIFaceDecl()}});
922 V.Visit(L.getUnqualifiedLoc());
926class ExplicitReferenceCollector
927 :
public RecursiveASTVisitor<ExplicitReferenceCollector> {
929 ExplicitReferenceCollector(llvm::function_ref<
void(ReferenceLoc)> Out,
930 const HeuristicResolver *Resolver)
931 : Out(Out), Resolver(Resolver) {
935 bool VisitTypeLoc(TypeLoc TTL) {
936 if (TypeLocsToSkip.count(TTL.getBeginLoc()))
938 visitNode(DynTypedNode::create(TTL));
942 bool VisitStmt(Stmt *S) {
943 visitNode(DynTypedNode::create(*S));
947 bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) {
948 visitNode(DynTypedNode::create(*OVE));
951 return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr());
954 bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) {
955 visitNode(DynTypedNode::create(*POE));
958 return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm());
964 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
965 switch (
A.getArgument().getKind()) {
966 case TemplateArgument::Template:
967 case TemplateArgument::TemplateExpansion:
968 reportReference(ReferenceLoc{
A.getTemplateQualifierLoc(),
969 A.getTemplateNameLoc(),
972 .getAsTemplateOrTemplatePattern()
973 .getAsTemplateDecl()}},
974 DynTypedNode::create(
A.getArgument()));
976 case TemplateArgument::Declaration:
978 case TemplateArgument::Integral:
979 case TemplateArgument::Null:
980 case TemplateArgument::NullPtr:
982 case TemplateArgument::Pack:
983 case TemplateArgument::Type:
984 case TemplateArgument::Expression:
985 case TemplateArgument::StructuralValue:
988 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
991 bool VisitDecl(Decl *D) {
992 visitNode(DynTypedNode::create(*D));
997 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
998 if (!L.getNestedNameSpecifier())
1000 visitNode(DynTypedNode::create(L));
1002 if (
auto TL = L.getAsTypeLoc())
1003 TypeLocsToSkip.insert(TL.getBeginLoc());
1004 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
1007 bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) {
1008 visitNode(DynTypedNode::create(ProtocolLoc));
1012 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
1013 visitNode(DynTypedNode::create(*Init));
1014 return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
1017 bool VisitConceptReference(
const ConceptReference *CR) {
1018 visitNode(DynTypedNode::create(*CR));
1037 llvm::SmallVector<ReferenceLoc> explicitReference(DynTypedNode N) {
1038 if (
auto *D = N.get<Decl>())
1039 return refInDecl(D, Resolver);
1040 if (
auto *S = N.get<Stmt>())
1041 return refInStmt(S, Resolver);
1042 if (
auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
1044 NestedNameSpecifierLoc Qualifier;
1045 SourceLocation NameLoc;
1046 if (
auto TL = NNSL->getAsTypeLoc()) {
1047 Qualifier = TL.getPrefix();
1048 NameLoc = TL.getNonPrefixBeginLoc();
1050 Qualifier = NNSL->getAsNamespaceAndPrefix().Prefix;
1051 NameLoc = NNSL->getLocalBeginLoc();
1054 ReferenceLoc{Qualifier, NameLoc,
false,
1056 DynTypedNode::create(NNSL->getNestedNameSpecifier()),
1059 if (
const TypeLoc *TL = N.get<TypeLoc>())
1060 return refInTypeLoc(*TL, Resolver);
1061 if (
const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
1064 if (CCI->isAnyMemberInitializer()) {
1065 return {ReferenceLoc{NestedNameSpecifierLoc(),
1066 CCI->getMemberLocation(),
1068 {CCI->getAnyMember()}}};
1071 if (
const ObjCProtocolLoc *PL = N.get<ObjCProtocolLoc>())
1072 return {ReferenceLoc{NestedNameSpecifierLoc(),
1075 {PL->getProtocol()}}};
1076 if (
const ConceptReference *CR = N.get<ConceptReference>())
1077 return {ReferenceLoc{CR->getNestedNameSpecifierLoc(),
1078 CR->getConceptNameLoc(),
1080 {CR->getNamedConcept()}}};
1086 void visitNode(DynTypedNode N) {
1087 for (
auto &R : explicitReference(N))
1088 reportReference(std::move(R), N);
1091 void reportReference(ReferenceLoc &&Ref, DynTypedNode N) {
1094 llvm::erase(Ref.Targets,
nullptr);
1099 if (Ref.NameLoc.isInvalid()) {
1100 dlog(
"invalid location at node {0}", nodeToString(N));
1106 llvm::function_ref<void(ReferenceLoc)> Out;
1107 const HeuristicResolver *Resolver;
1110 llvm::DenseSet<SourceLocation> TypeLocsToSkip;
1116 const HeuristicResolver *Resolver) {
1118 ExplicitReferenceCollector(Out, Resolver).TraverseStmt(
const_cast<Stmt *
>(S));
1122 const HeuristicResolver *Resolver) {
1124 ExplicitReferenceCollector(Out, Resolver).TraverseDecl(
const_cast<Decl *
>(D));
1128 const HeuristicResolver *Resolver) {
1129 ExplicitReferenceCollector(Out, Resolver)
1130 .TraverseAST(
const_cast<ASTContext &
>(
AST));
1135#define REL_CASE(X) \
1136 case DeclRelation::X: \
1144 llvm_unreachable(
"Unhandled DeclRelation enum");
1146llvm::raw_ostream &
operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
1147 const char *Sep =
"";
1148 for (
unsigned I = 0; I < RS.S.size(); ++I) {
1150 OS << Sep << static_cast<DeclRelation>(I);
1159 OS <<
"targets = {";
1160 llvm::SmallVector<std::string> Targets;
1161 for (
const NamedDecl *T : R.
Targets) {
1162 llvm::raw_string_ostream Target(Targets.emplace_back());
1165 llvm::sort(Targets);
1166 OS << llvm::join(Targets,
", ");
1169 OS <<
", qualifier = '";
1170 R.
Qualifier.getNestedNameSpecifier().print(OS,
1171 PrintingPolicy(LangOptions()));
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
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.