llvm-project
1228 строк · 47.7 Кб
1//===--- FindTarget.cpp - What does an AST node refer to? -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "FindTarget.h"
10#include "AST.h"
11#include "HeuristicResolver.h"
12#include "support/Logger.h"
13#include "clang/AST/ASTConcept.h"
14#include "clang/AST/ASTTypeTraits.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclBase.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclVisitor.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/AST/ExprConcepts.h"
24#include "clang/AST/ExprObjC.h"
25#include "clang/AST/NestedNameSpecifier.h"
26#include "clang/AST/PrettyPrinter.h"
27#include "clang/AST/RecursiveASTVisitor.h"
28#include "clang/AST/StmtVisitor.h"
29#include "clang/AST/TemplateBase.h"
30#include "clang/AST/Type.h"
31#include "clang/AST/TypeLoc.h"
32#include "clang/AST/TypeLocVisitor.h"
33#include "clang/AST/TypeVisitor.h"
34#include "clang/Basic/LangOptions.h"
35#include "clang/Basic/SourceLocation.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/Specifiers.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"
44#include <iterator>
45#include <string>
46#include <utility>
47#include <vector>
48
49namespace clang {
50namespace clangd {
51namespace {
52
53LLVM_ATTRIBUTE_UNUSED std::string nodeToString(const DynTypedNode &N) {
54std::string S = std::string(N.getNodeKind().asStringRef());
55{
56llvm::raw_string_ostream OS(S);
57OS << ": ";
58N.print(OS, PrintingPolicy(LangOptions()));
59}
60std::replace(S.begin(), S.end(), '\n', ' ');
61return S;
62}
63
64const NamedDecl *getTemplatePattern(const NamedDecl *D) {
65if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
66if (const auto *Result = CRD->getTemplateInstantiationPattern())
67return Result;
68// getTemplateInstantiationPattern returns null if the Specialization is
69// incomplete (e.g. the type didn't need to be complete), fall back to the
70// primary template.
71if (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
72if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
73return Spec->getSpecializedTemplate()->getTemplatedDecl();
74} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
75return FD->getTemplateInstantiationPattern();
76} else if (auto *VD = dyn_cast<VarDecl>(D)) {
77// Hmm: getTIP returns its arg if it's not an instantiation?!
78VarDecl *T = VD->getTemplateInstantiationPattern();
79return (T == D) ? nullptr : T;
80} else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
81return ED->getInstantiatedFromMemberEnum();
82} else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
83if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext()))
84if (const DeclContext *ParentPat =
85dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
86for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName()))
87if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind())
88return BaseND;
89} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
90if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
91if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
92for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
93return BaseECD;
94}
95}
96}
97return nullptr;
98}
99
100// Returns true if the `TypedefNameDecl` should not be reported.
101bool shouldSkipTypedef(const TypedefNameDecl *TD) {
102// These should be treated as keywords rather than decls - the typedef is an
103// odd implementation detail.
104if (TD == TD->getASTContext().getObjCInstanceTypeDecl() ||
105TD == TD->getASTContext().getObjCIdDecl())
106return true;
107return false;
108}
109
110// TargetFinder locates the entities that an AST node refers to.
111//
112// Typically this is (possibly) one declaration and (possibly) one type, but
113// may be more:
114// - for ambiguous nodes like OverloadExpr
115// - if we want to include e.g. both typedefs and the underlying type
116//
117// This is organized as a set of mutually recursive helpers for particular node
118// types, but for most nodes this is a short walk rather than a deep traversal.
119//
120// It's tempting to do e.g. typedef resolution as a second normalization step,
121// after finding the 'primary' decl etc. But we do this monolithically instead
122// because:
123// - normalization may require these traversals again (e.g. unwrapping a
124// typedef reveals a decltype which must be traversed)
125// - it doesn't simplify that much, e.g. the first stage must still be able
126// to yield multiple decls to handle OverloadExpr
127// - there are cases where it's required for correctness. e.g:
128// template<class X> using pvec = vector<x*>; pvec<int> x;
129// There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>`
130// and both are lossy. We must know upfront what the caller ultimately wants.
131struct TargetFinder {
132using RelSet = DeclRelationSet;
133using Rel = DeclRelation;
134
135private:
136const HeuristicResolver *Resolver;
137llvm::SmallDenseMap<const NamedDecl *,
138std::pair<RelSet, /*InsertionOrder*/ size_t>>
139Decls;
140llvm::SmallDenseMap<const Decl *, RelSet> Seen;
141RelSet Flags;
142
143template <typename T> void debug(T &Node, RelSet Flags) {
144dlog("visit [{0}] {1}", Flags, nodeToString(DynTypedNode::create(Node)));
145}
146
147void report(const NamedDecl *D, RelSet Flags) {
148dlog("--> [{0}] {1}", Flags, nodeToString(DynTypedNode::create(*D)));
149auto It = Decls.try_emplace(D, std::make_pair(Flags, Decls.size()));
150// If already exists, update the flags.
151if (!It.second)
152It.first->second.first |= Flags;
153}
154
155public:
156TargetFinder(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
157
158llvm::SmallVector<std::pair<const NamedDecl *, RelSet>, 1> takeDecls() const {
159using ValTy = std::pair<const NamedDecl *, RelSet>;
160llvm::SmallVector<ValTy, 1> Result;
161Result.resize(Decls.size());
162for (const auto &Elem : Decls)
163Result[Elem.second.second] = {Elem.first, Elem.second.first};
164return Result;
165}
166
167void add(const Decl *Dcl, RelSet Flags) {
168const NamedDecl *D = llvm::dyn_cast_or_null<NamedDecl>(Dcl);
169if (!D)
170return;
171debug(*D, Flags);
172
173// Avoid recursion (which can arise in the presence of heuristic
174// resolution of dependent names) by exiting early if we have
175// already seen this decl with all flags in Flags.
176auto Res = Seen.try_emplace(D);
177if (!Res.second && Res.first->second.contains(Flags))
178return;
179Res.first->second |= Flags;
180
181if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
182D = UDD->getNominatedNamespaceAsWritten();
183
184if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
185add(TND->getUnderlyingType(), Flags | Rel::Underlying);
186Flags |= Rel::Alias; // continue with the alias.
187} else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
188// no Underlying as this is a non-renaming alias.
189for (const UsingShadowDecl *S : UD->shadows())
190add(S->getUnderlyingDecl(), Flags);
191Flags |= Rel::Alias; // continue with the alias.
192} else if (const UsingEnumDecl *UED = dyn_cast<UsingEnumDecl>(D)) {
193// UsingEnumDecl is not an alias at all, just a reference.
194D = UED->getEnumDecl();
195} else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
196add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
197Flags |= Rel::Alias; // continue with the alias
198} else if (const UnresolvedUsingValueDecl *UUVD =
199dyn_cast<UnresolvedUsingValueDecl>(D)) {
200if (Resolver) {
201for (const NamedDecl *Target : Resolver->resolveUsingValueDecl(UUVD)) {
202add(Target, Flags); // no Underlying as this is a non-renaming alias
203}
204}
205Flags |= Rel::Alias; // continue with the alias
206} else if (isa<UnresolvedUsingTypenameDecl>(D)) {
207// FIXME: improve common dependent scope using name lookup in primary
208// templates.
209Flags |= Rel::Alias;
210} else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
211// Include the introducing UsingDecl, but don't traverse it. This may end
212// up including *all* shadows, which we don't want.
213// Don't apply this logic to UsingEnumDecl, which can't easily be
214// conflated with the aliases it introduces.
215if (llvm::isa<UsingDecl>(USD->getIntroducer()))
216report(USD->getIntroducer(), Flags | Rel::Alias);
217// Shadow decls are synthetic and not themselves interesting.
218// Record the underlying decl instead, if allowed.
219D = USD->getTargetDecl();
220} else if (const auto *DG = dyn_cast<CXXDeductionGuideDecl>(D)) {
221D = DG->getDeducedTemplate();
222} else if (const ObjCImplementationDecl *IID =
223dyn_cast<ObjCImplementationDecl>(D)) {
224// Treat ObjC{Interface,Implementation}Decl as if they were a decl/def
225// pair as long as the interface isn't implicit.
226if (const auto *CID = IID->getClassInterface())
227if (const auto *DD = CID->getDefinition())
228if (!DD->isImplicitInterfaceDecl())
229D = DD;
230} else if (const ObjCCategoryImplDecl *CID =
231dyn_cast<ObjCCategoryImplDecl>(D)) {
232// Treat ObjC{Category,CategoryImpl}Decl as if they were a decl/def pair.
233D = CID->getCategoryDecl();
234}
235if (!D)
236return;
237
238if (const Decl *Pat = getTemplatePattern(D)) {
239assert(Pat != D);
240add(Pat, Flags | Rel::TemplatePattern);
241// Now continue with the instantiation.
242Flags |= Rel::TemplateInstantiation;
243}
244
245report(D, Flags);
246}
247
248void add(const Stmt *S, RelSet Flags) {
249if (!S)
250return;
251debug(*S, Flags);
252struct Visitor : public ConstStmtVisitor<Visitor> {
253TargetFinder &Outer;
254RelSet Flags;
255Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
256
257void VisitCallExpr(const CallExpr *CE) {
258Outer.add(CE->getCalleeDecl(), Flags);
259}
260void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
261Outer.add(E->getConceptReference(), Flags);
262}
263void VisitDeclRefExpr(const DeclRefExpr *DRE) {
264const Decl *D = DRE->getDecl();
265// UsingShadowDecl allows us to record the UsingDecl.
266// getFoundDecl() returns the wrong thing in other cases (templates).
267if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
268D = USD;
269Outer.add(D, Flags);
270}
271void VisitMemberExpr(const MemberExpr *ME) {
272const Decl *D = ME->getMemberDecl();
273if (auto *USD =
274llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
275D = USD;
276Outer.add(D, Flags);
277}
278void VisitOverloadExpr(const OverloadExpr *OE) {
279for (auto *D : OE->decls())
280Outer.add(D, Flags);
281}
282void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
283Outer.add(SE->getPack(), Flags);
284}
285void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
286Outer.add(CCE->getConstructor(), Flags);
287}
288void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
289for (const DesignatedInitExpr::Designator &D :
290llvm::reverse(DIE->designators()))
291if (D.isFieldDesignator()) {
292Outer.add(D.getFieldDecl(), Flags);
293// We don't know which designator was intended, we assume the outer.
294break;
295}
296}
297void VisitGotoStmt(const GotoStmt *Goto) {
298if (auto *LabelDecl = Goto->getLabel())
299Outer.add(LabelDecl, Flags);
300}
301void VisitLabelStmt(const LabelStmt *Label) {
302if (auto *LabelDecl = Label->getDecl())
303Outer.add(LabelDecl, Flags);
304}
305void
306VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
307if (Outer.Resolver) {
308for (const NamedDecl *D : Outer.Resolver->resolveMemberExpr(E)) {
309Outer.add(D, Flags);
310}
311}
312}
313void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
314if (Outer.Resolver) {
315for (const NamedDecl *D : Outer.Resolver->resolveDeclRefExpr(E)) {
316Outer.add(D, Flags);
317}
318}
319}
320void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
321Outer.add(OIRE->getDecl(), Flags);
322}
323void VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
324Outer.add(OME->getMethodDecl(), Flags);
325}
326void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
327if (OPRE->isExplicitProperty())
328Outer.add(OPRE->getExplicitProperty(), Flags);
329else {
330if (OPRE->isMessagingGetter())
331Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
332if (OPRE->isMessagingSetter())
333Outer.add(OPRE->getImplicitPropertySetter(), Flags);
334}
335}
336void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
337Outer.add(OPE->getProtocol(), Flags);
338}
339void VisitOpaqueValueExpr(const OpaqueValueExpr *OVE) {
340Outer.add(OVE->getSourceExpr(), Flags);
341}
342void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
343Outer.add(POE->getSyntacticForm(), Flags);
344}
345void VisitCXXNewExpr(const CXXNewExpr *CNE) {
346Outer.add(CNE->getOperatorNew(), Flags);
347}
348void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE) {
349Outer.add(CDE->getOperatorDelete(), Flags);
350}
351void
352VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *RBO) {
353Outer.add(RBO->getDecomposedForm().InnerBinOp, Flags);
354}
355};
356Visitor(*this, Flags).Visit(S);
357}
358
359void add(QualType T, RelSet Flags) {
360if (T.isNull())
361return;
362debug(T, Flags);
363struct Visitor : public TypeVisitor<Visitor> {
364TargetFinder &Outer;
365RelSet Flags;
366Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
367
368void VisitTagType(const TagType *TT) {
369Outer.add(TT->getAsTagDecl(), Flags);
370}
371
372void VisitElaboratedType(const ElaboratedType *ET) {
373Outer.add(ET->desugar(), Flags);
374}
375
376void VisitUsingType(const UsingType *ET) {
377Outer.add(ET->getFoundDecl(), Flags);
378}
379
380void VisitInjectedClassNameType(const InjectedClassNameType *ICNT) {
381Outer.add(ICNT->getDecl(), Flags);
382}
383
384void VisitDecltypeType(const DecltypeType *DTT) {
385Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
386}
387void VisitDeducedType(const DeducedType *DT) {
388// FIXME: In practice this doesn't work: the AutoType you find inside
389// TypeLoc never has a deduced type. https://llvm.org/PR42914
390Outer.add(DT->getDeducedType(), Flags);
391}
392void VisitUnresolvedUsingType(const UnresolvedUsingType *UUT) {
393Outer.add(UUT->getDecl(), Flags);
394}
395void VisitDeducedTemplateSpecializationType(
396const DeducedTemplateSpecializationType *DTST) {
397if (const auto *USD = DTST->getTemplateName().getAsUsingShadowDecl())
398Outer.add(USD, Flags);
399
400// FIXME: This is a workaround for https://llvm.org/PR42914,
401// which is causing DTST->getDeducedType() to be empty. We
402// fall back to the template pattern and miss the instantiation
403// even when it's known in principle. Once that bug is fixed,
404// the following code can be removed (the existing handling in
405// VisitDeducedType() is sufficient).
406if (auto *TD = DTST->getTemplateName().getAsTemplateDecl())
407Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
408}
409void VisitDependentNameType(const DependentNameType *DNT) {
410if (Outer.Resolver) {
411for (const NamedDecl *ND :
412Outer.Resolver->resolveDependentNameType(DNT)) {
413Outer.add(ND, Flags);
414}
415}
416}
417void VisitDependentTemplateSpecializationType(
418const DependentTemplateSpecializationType *DTST) {
419if (Outer.Resolver) {
420for (const NamedDecl *ND :
421Outer.Resolver->resolveTemplateSpecializationType(DTST)) {
422Outer.add(ND, Flags);
423}
424}
425}
426void VisitTypedefType(const TypedefType *TT) {
427if (shouldSkipTypedef(TT->getDecl()))
428return;
429Outer.add(TT->getDecl(), Flags);
430}
431void
432VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
433// Have to handle these case-by-case.
434
435if (const auto *UTN = TST->getTemplateName().getAsUsingShadowDecl())
436Outer.add(UTN, Flags);
437
438// templated type aliases: there's no specialized/instantiated using
439// decl to point to. So try to find a decl for the underlying type
440// (after substitution), and failing that point to the (templated) using
441// decl.
442if (TST->isTypeAlias()) {
443Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
444// Don't *traverse* the alias, which would result in traversing the
445// template of the underlying type.
446
447TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl();
448// Builtin templates e.g. __make_integer_seq, __type_pack_element
449// are such that they don't have alias *decls*. Even then, we still
450// traverse their desugared *types* so that instantiated decls are
451// collected.
452if (llvm::isa<BuiltinTemplateDecl>(TD))
453return;
454Outer.report(TD->getTemplatedDecl(),
455Flags | Rel::Alias | Rel::TemplatePattern);
456}
457// specializations of template template parameters aren't instantiated
458// into decls, so they must refer to the parameter itself.
459else if (const auto *Parm =
460llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
461TST->getTemplateName().getAsTemplateDecl()))
462Outer.add(Parm, Flags);
463// class template specializations have a (specialized) CXXRecordDecl.
464else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
465Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
466else {
467// fallback: the (un-specialized) declaration from primary template.
468if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
469Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
470}
471}
472void
473VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *STTPT) {
474Outer.add(STTPT->getReplacementType(), Flags);
475}
476void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
477Outer.add(TTPT->getDecl(), Flags);
478}
479void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
480Outer.add(OIT->getDecl(), Flags);
481}
482};
483Visitor(*this, Flags).Visit(T.getTypePtr());
484}
485
486void add(const NestedNameSpecifier *NNS, RelSet Flags) {
487if (!NNS)
488return;
489debug(*NNS, Flags);
490switch (NNS->getKind()) {
491case NestedNameSpecifier::Namespace:
492add(NNS->getAsNamespace(), Flags);
493return;
494case NestedNameSpecifier::NamespaceAlias:
495add(NNS->getAsNamespaceAlias(), Flags);
496return;
497case NestedNameSpecifier::Identifier:
498if (Resolver) {
499add(QualType(Resolver->resolveNestedNameSpecifierToType(NNS), 0),
500Flags);
501}
502return;
503case NestedNameSpecifier::TypeSpec:
504case NestedNameSpecifier::TypeSpecWithTemplate:
505add(QualType(NNS->getAsType(), 0), Flags);
506return;
507case NestedNameSpecifier::Global:
508// This should be TUDecl, but we can't get a pointer to it!
509return;
510case NestedNameSpecifier::Super:
511add(NNS->getAsRecordDecl(), Flags);
512return;
513}
514llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
515}
516
517void add(const CXXCtorInitializer *CCI, RelSet Flags) {
518if (!CCI)
519return;
520debug(*CCI, Flags);
521
522if (CCI->isAnyMemberInitializer())
523add(CCI->getAnyMember(), Flags);
524// Constructor calls contain a TypeLoc node, so we don't handle them here.
525}
526
527void add(const TemplateArgument &Arg, RelSet Flags) {
528// Only used for template template arguments.
529// For type and non-type template arguments, SelectionTree
530// will hit a more specific node (e.g. a TypeLoc or a
531// DeclRefExpr).
532if (Arg.getKind() == TemplateArgument::Template ||
533Arg.getKind() == TemplateArgument::TemplateExpansion) {
534if (TemplateDecl *TD =
535Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) {
536report(TD, Flags);
537}
538if (const auto *USD =
539Arg.getAsTemplateOrTemplatePattern().getAsUsingShadowDecl())
540add(USD, Flags);
541}
542}
543
544void add(const ConceptReference *CR, RelSet Flags) {
545add(CR->getNamedConcept(), Flags);
546}
547};
548
549} // namespace
550
551llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
552allTargetDecls(const DynTypedNode &N, const HeuristicResolver *Resolver) {
553dlog("allTargetDecls({0})", nodeToString(N));
554TargetFinder Finder(Resolver);
555DeclRelationSet Flags;
556if (const Decl *D = N.get<Decl>())
557Finder.add(D, Flags);
558else if (const Stmt *S = N.get<Stmt>())
559Finder.add(S, Flags);
560else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
561Finder.add(NNSL->getNestedNameSpecifier(), Flags);
562else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
563Finder.add(NNS, Flags);
564else if (const TypeLoc *TL = N.get<TypeLoc>())
565Finder.add(TL->getType(), Flags);
566else if (const QualType *QT = N.get<QualType>())
567Finder.add(*QT, Flags);
568else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
569Finder.add(CCI, Flags);
570else if (const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>())
571Finder.add(TAL->getArgument(), Flags);
572else if (const CXXBaseSpecifier *CBS = N.get<CXXBaseSpecifier>())
573Finder.add(CBS->getTypeSourceInfo()->getType(), Flags);
574else if (const ObjCProtocolLoc *PL = N.get<ObjCProtocolLoc>())
575Finder.add(PL->getProtocol(), Flags);
576else if (const ConceptReference *CR = N.get<ConceptReference>())
577Finder.add(CR, Flags);
578return Finder.takeDecls();
579}
580
581llvm::SmallVector<const NamedDecl *, 1>
582targetDecl(const DynTypedNode &N, DeclRelationSet Mask,
583const HeuristicResolver *Resolver) {
584llvm::SmallVector<const NamedDecl *, 1> Result;
585for (const auto &Entry : allTargetDecls(N, Resolver)) {
586if (!(Entry.second & ~Mask))
587Result.push_back(Entry.first);
588}
589return Result;
590}
591
592llvm::SmallVector<const NamedDecl *, 1>
593explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask,
594const HeuristicResolver *Resolver) {
595assert(!(Mask & (DeclRelation::TemplatePattern |
596DeclRelation::TemplateInstantiation)) &&
597"explicitReferenceTargets handles templates on its own");
598auto Decls = allTargetDecls(N, Resolver);
599
600// We prefer to return template instantiation, but fallback to template
601// pattern if instantiation is not available.
602Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation;
603
604llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
605llvm::SmallVector<const NamedDecl *, 1> Targets;
606bool SeenTemplateInstantiations = false;
607for (auto &D : Decls) {
608if (D.second & ~Mask)
609continue;
610if (D.second & DeclRelation::TemplatePattern) {
611TemplatePatterns.push_back(D.first);
612continue;
613}
614if (D.second & DeclRelation::TemplateInstantiation)
615SeenTemplateInstantiations = true;
616Targets.push_back(D.first);
617}
618if (!SeenTemplateInstantiations)
619Targets.insert(Targets.end(), TemplatePatterns.begin(),
620TemplatePatterns.end());
621return Targets;
622}
623
624namespace {
625llvm::SmallVector<ReferenceLoc> refInDecl(const Decl *D,
626const HeuristicResolver *Resolver) {
627struct Visitor : ConstDeclVisitor<Visitor> {
628Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
629
630const HeuristicResolver *Resolver;
631llvm::SmallVector<ReferenceLoc> Refs;
632
633void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
634// We want to keep it as non-declaration references, as the
635// "using namespace" declaration doesn't have a name.
636Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
637D->getIdentLocation(),
638/*IsDecl=*/false,
639{D->getNominatedNamespaceAsWritten()}});
640}
641
642void VisitUsingDecl(const UsingDecl *D) {
643// "using ns::identifier;" is a non-declaration reference.
644Refs.push_back(ReferenceLoc{
645D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
646explicitReferenceTargets(DynTypedNode::create(*D),
647DeclRelation::Underlying, Resolver)});
648}
649
650void VisitUsingEnumDecl(const UsingEnumDecl *D) {
651// "using enum ns::E" is a non-declaration reference.
652// The reference is covered by the embedded typeloc.
653// Don't use the default VisitNamedDecl, which would report a declaration.
654}
655
656void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
657// For namespace alias, "namespace Foo = Target;", we add two references.
658// Add a declaration reference for Foo.
659VisitNamedDecl(D);
660// Add a non-declaration reference for Target.
661Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
662D->getTargetNameLoc(),
663/*IsDecl=*/false,
664{D->getAliasedNamespace()}});
665}
666
667void VisitNamedDecl(const NamedDecl *ND) {
668// We choose to ignore {Class, Function, Var, TypeAlias}TemplateDecls. As
669// as their underlying decls, covering the same range, will be visited.
670if (llvm::isa<ClassTemplateDecl>(ND) ||
671llvm::isa<FunctionTemplateDecl>(ND) ||
672llvm::isa<VarTemplateDecl>(ND) ||
673llvm::isa<TypeAliasTemplateDecl>(ND))
674return;
675// FIXME: decide on how to surface destructors when we need them.
676if (llvm::isa<CXXDestructorDecl>(ND))
677return;
678// Filter anonymous decls, name location will point outside the name token
679// and the clients are not prepared to handle that.
680if (ND->getDeclName().isIdentifier() &&
681!ND->getDeclName().getAsIdentifierInfo())
682return;
683Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
684ND->getLocation(),
685/*IsDecl=*/true,
686{ND}});
687}
688
689void VisitCXXDeductionGuideDecl(const CXXDeductionGuideDecl *DG) {
690// The class template name in a deduction guide targets the class
691// template.
692Refs.push_back(ReferenceLoc{DG->getQualifierLoc(),
693DG->getNameInfo().getLoc(),
694/*IsDecl=*/false,
695{DG->getDeducedTemplate()}});
696}
697
698void VisitObjCMethodDecl(const ObjCMethodDecl *OMD) {
699// The name may have several tokens, we can only report the first.
700Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
701OMD->getSelectorStartLoc(),
702/*IsDecl=*/true,
703{OMD}});
704}
705
706void VisitObjCCategoryDecl(const ObjCCategoryDecl *OCD) {
707// getLocation is the extended class's location, not the category's.
708Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
709OCD->getLocation(),
710/*IsDecl=*/false,
711{OCD->getClassInterface()}});
712Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
713OCD->getCategoryNameLoc(),
714/*IsDecl=*/true,
715{OCD}});
716}
717
718void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *OCID) {
719Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
720OCID->getLocation(),
721/*IsDecl=*/false,
722{OCID->getClassInterface()}});
723Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
724OCID->getCategoryNameLoc(),
725/*IsDecl=*/false,
726{OCID->getCategoryDecl()}});
727Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
728OCID->getCategoryNameLoc(),
729/*IsDecl=*/true,
730{OCID}});
731}
732
733void VisitObjCImplementationDecl(const ObjCImplementationDecl *OIMD) {
734Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
735OIMD->getLocation(),
736/*IsDecl=*/false,
737{OIMD->getClassInterface()}});
738Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
739OIMD->getLocation(),
740/*IsDecl=*/true,
741{OIMD}});
742}
743};
744
745Visitor V{Resolver};
746V.Visit(D);
747return V.Refs;
748}
749
750llvm::SmallVector<ReferenceLoc> refInStmt(const Stmt *S,
751const HeuristicResolver *Resolver) {
752struct Visitor : ConstStmtVisitor<Visitor> {
753Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
754
755const HeuristicResolver *Resolver;
756// FIXME: handle more complicated cases: more ObjC, designated initializers.
757llvm::SmallVector<ReferenceLoc> Refs;
758
759void VisitDeclRefExpr(const DeclRefExpr *E) {
760Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
761E->getNameInfo().getLoc(),
762/*IsDecl=*/false,
763{E->getFoundDecl()}});
764}
765
766void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
767Refs.push_back(ReferenceLoc{
768E->getQualifierLoc(), E->getNameInfo().getLoc(), /*IsDecl=*/false,
769explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)});
770}
771
772void VisitMemberExpr(const MemberExpr *E) {
773// Skip destructor calls to avoid duplication: TypeLoc within will be
774// visited separately.
775if (llvm::isa<CXXDestructorDecl>(E->getFoundDecl().getDecl()))
776return;
777Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
778E->getMemberNameInfo().getLoc(),
779/*IsDecl=*/false,
780{E->getFoundDecl()}});
781}
782
783void
784VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
785Refs.push_back(ReferenceLoc{
786E->getQualifierLoc(), E->getMemberNameInfo().getLoc(),
787/*IsDecl=*/false,
788explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)});
789}
790
791void VisitOverloadExpr(const OverloadExpr *E) {
792Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
793E->getNameInfo().getLoc(),
794/*IsDecl=*/false,
795llvm::SmallVector<const NamedDecl *, 1>(
796E->decls().begin(), E->decls().end())});
797}
798
799void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
800Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
801E->getPackLoc(),
802/*IsDecl=*/false,
803{E->getPack()}});
804}
805
806void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) {
807Refs.push_back(ReferenceLoc{
808NestedNameSpecifierLoc(), E->getLocation(),
809/*IsDecl=*/false,
810// Select the getter, setter, or @property depending on the call.
811explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)});
812}
813
814void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
815Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
816OIRE->getLocation(),
817/*IsDecl=*/false,
818{OIRE->getDecl()}});
819}
820
821void VisitObjCMessageExpr(const ObjCMessageExpr *E) {
822// The name may have several tokens, we can only report the first.
823Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
824E->getSelectorStartLoc(),
825/*IsDecl=*/false,
826{E->getMethodDecl()}});
827}
828
829void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
830for (const DesignatedInitExpr::Designator &D : DIE->designators()) {
831if (!D.isFieldDesignator())
832continue;
833
834Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
835D.getFieldLoc(),
836/*IsDecl=*/false,
837{D.getFieldDecl()}});
838}
839}
840
841void VisitGotoStmt(const GotoStmt *GS) {
842Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
843GS->getLabelLoc(),
844/*IsDecl=*/false,
845{GS->getLabel()}});
846}
847
848void VisitLabelStmt(const LabelStmt *LS) {
849Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
850LS->getIdentLoc(),
851/*IsDecl=*/true,
852{LS->getDecl()}});
853}
854};
855
856Visitor V{Resolver};
857V.Visit(S);
858return V.Refs;
859}
860
861llvm::SmallVector<ReferenceLoc>
862refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) {
863struct Visitor : TypeLocVisitor<Visitor> {
864Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
865
866const HeuristicResolver *Resolver;
867llvm::SmallVector<ReferenceLoc> Refs;
868
869void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
870// We only know about qualifier, rest if filled by inner locations.
871size_t InitialSize = Refs.size();
872Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
873size_t NewSize = Refs.size();
874// Add qualifier for the newly-added refs.
875for (unsigned I = InitialSize; I < NewSize; ++I) {
876ReferenceLoc *Ref = &Refs[I];
877// Fill in the qualifier.
878assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
879Ref->Qualifier = L.getQualifierLoc();
880}
881}
882
883void VisitUsingTypeLoc(UsingTypeLoc L) {
884Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
885L.getLocalSourceRange().getBegin(),
886/*IsDecl=*/false,
887{L.getFoundDecl()}});
888}
889
890void VisitTagTypeLoc(TagTypeLoc L) {
891Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
892L.getNameLoc(),
893/*IsDecl=*/false,
894{L.getDecl()}});
895}
896
897void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
898Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
899L.getNameLoc(),
900/*IsDecl=*/false,
901{L.getDecl()}});
902}
903
904void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
905// We must ensure template type aliases are included in results if they
906// were written in the source code, e.g. in
907// template <class T> using valias = vector<T>;
908// ^valias<int> x;
909// 'explicitReferenceTargets' will return:
910// 1. valias with mask 'Alias'.
911// 2. 'vector<int>' with mask 'Underlying'.
912// we want to return only #1 in this case.
913Refs.push_back(ReferenceLoc{
914NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
915explicitReferenceTargets(DynTypedNode::create(L.getType()),
916DeclRelation::Alias, Resolver)});
917}
918void VisitDeducedTemplateSpecializationTypeLoc(
919DeducedTemplateSpecializationTypeLoc L) {
920Refs.push_back(ReferenceLoc{
921NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
922explicitReferenceTargets(DynTypedNode::create(L.getType()),
923DeclRelation::Alias, Resolver)});
924}
925
926void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
927Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
928TL.getNameLoc(),
929/*IsDecl=*/false,
930{TL.getDecl()}});
931}
932
933void VisitDependentTemplateSpecializationTypeLoc(
934DependentTemplateSpecializationTypeLoc L) {
935Refs.push_back(
936ReferenceLoc{L.getQualifierLoc(), L.getTemplateNameLoc(),
937/*IsDecl=*/false,
938explicitReferenceTargets(
939DynTypedNode::create(L.getType()), {}, Resolver)});
940}
941
942void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
943Refs.push_back(
944ReferenceLoc{L.getQualifierLoc(), L.getNameLoc(),
945/*IsDecl=*/false,
946explicitReferenceTargets(
947DynTypedNode::create(L.getType()), {}, Resolver)});
948}
949
950void VisitTypedefTypeLoc(TypedefTypeLoc L) {
951if (shouldSkipTypedef(L.getTypedefNameDecl()))
952return;
953Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
954L.getNameLoc(),
955/*IsDecl=*/false,
956{L.getTypedefNameDecl()}});
957}
958
959void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc L) {
960Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
961L.getNameLoc(),
962/*IsDecl=*/false,
963{L.getIFaceDecl()}});
964}
965};
966
967Visitor V{Resolver};
968V.Visit(L.getUnqualifiedLoc());
969return V.Refs;
970}
971
972class ExplicitReferenceCollector
973: public RecursiveASTVisitor<ExplicitReferenceCollector> {
974public:
975ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out,
976const HeuristicResolver *Resolver)
977: Out(Out), Resolver(Resolver) {
978assert(Out);
979}
980
981bool VisitTypeLoc(TypeLoc TTL) {
982if (TypeLocsToSkip.count(TTL.getBeginLoc()))
983return true;
984visitNode(DynTypedNode::create(TTL));
985return true;
986}
987
988bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
989// ElaboratedTypeLoc will reports information for its inner type loc.
990// Otherwise we loose information about inner types loc's qualifier.
991TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
992if (L.getBeginLoc() == Inner.getBeginLoc())
993return RecursiveASTVisitor::TraverseTypeLoc(Inner);
994else
995TypeLocsToSkip.insert(Inner.getBeginLoc());
996return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
997}
998
999bool VisitStmt(Stmt *S) {
1000visitNode(DynTypedNode::create(*S));
1001return true;
1002}
1003
1004bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) {
1005visitNode(DynTypedNode::create(*OVE));
1006// Not clear why the source expression is skipped by default...
1007// FIXME: can we just make RecursiveASTVisitor do this?
1008return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr());
1009}
1010
1011bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) {
1012visitNode(DynTypedNode::create(*POE));
1013// Traverse only the syntactic form to find the *written* references.
1014// (The semantic form also contains lots of duplication)
1015return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm());
1016}
1017
1018// We re-define Traverse*, since there's no corresponding Visit*.
1019// TemplateArgumentLoc is the only way to get locations for references to
1020// template template parameters.
1021bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
1022switch (A.getArgument().getKind()) {
1023case TemplateArgument::Template:
1024case TemplateArgument::TemplateExpansion:
1025reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
1026A.getTemplateNameLoc(),
1027/*IsDecl=*/false,
1028{A.getArgument()
1029.getAsTemplateOrTemplatePattern()
1030.getAsTemplateDecl()}},
1031DynTypedNode::create(A.getArgument()));
1032break;
1033case TemplateArgument::Declaration:
1034break; // FIXME: can this actually happen in TemplateArgumentLoc?
1035case TemplateArgument::Integral:
1036case TemplateArgument::Null:
1037case TemplateArgument::NullPtr:
1038break; // no references.
1039case TemplateArgument::Pack:
1040case TemplateArgument::Type:
1041case TemplateArgument::Expression:
1042case TemplateArgument::StructuralValue:
1043break; // Handled by VisitType and VisitExpression.
1044};
1045return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
1046}
1047
1048bool VisitDecl(Decl *D) {
1049visitNode(DynTypedNode::create(*D));
1050return true;
1051}
1052
1053// We have to use Traverse* because there is no corresponding Visit*.
1054bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
1055if (!L.getNestedNameSpecifier())
1056return true;
1057visitNode(DynTypedNode::create(L));
1058// Inner type is missing information about its qualifier, skip it.
1059if (auto TL = L.getTypeLoc())
1060TypeLocsToSkip.insert(TL.getBeginLoc());
1061return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
1062}
1063
1064bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) {
1065visitNode(DynTypedNode::create(ProtocolLoc));
1066return true;
1067}
1068
1069bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
1070visitNode(DynTypedNode::create(*Init));
1071return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
1072}
1073
1074bool VisitConceptReference(const ConceptReference *CR) {
1075visitNode(DynTypedNode::create(*CR));
1076return true;
1077}
1078
1079private:
1080/// Obtain information about a reference directly defined in \p N. Does not
1081/// recurse into child nodes, e.g. do not expect references for constructor
1082/// initializers
1083///
1084/// Any of the fields in the returned structure can be empty, but not all of
1085/// them, e.g.
1086/// - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
1087/// source location information may be missing,
1088/// - for dependent code, targets may be empty.
1089///
1090/// (!) For the purposes of this function declarations are not considered to
1091/// be references. However, declarations can have references inside them,
1092/// e.g. 'namespace foo = std' references namespace 'std' and this
1093/// function will return the corresponding reference.
1094llvm::SmallVector<ReferenceLoc> explicitReference(DynTypedNode N) {
1095if (auto *D = N.get<Decl>())
1096return refInDecl(D, Resolver);
1097if (auto *S = N.get<Stmt>())
1098return refInStmt(S, Resolver);
1099if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
1100// (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
1101return {ReferenceLoc{
1102NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
1103explicitReferenceTargets(
1104DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
1105DeclRelation::Alias, Resolver)}};
1106}
1107if (const TypeLoc *TL = N.get<TypeLoc>())
1108return refInTypeLoc(*TL, Resolver);
1109if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
1110// Other type initializers (e.g. base initializer) are handled by visiting
1111// the typeLoc.
1112if (CCI->isAnyMemberInitializer()) {
1113return {ReferenceLoc{NestedNameSpecifierLoc(),
1114CCI->getMemberLocation(),
1115/*IsDecl=*/false,
1116{CCI->getAnyMember()}}};
1117}
1118}
1119if (const ObjCProtocolLoc *PL = N.get<ObjCProtocolLoc>())
1120return {ReferenceLoc{NestedNameSpecifierLoc(),
1121PL->getLocation(),
1122/*IsDecl=*/false,
1123{PL->getProtocol()}}};
1124if (const ConceptReference *CR = N.get<ConceptReference>())
1125return {ReferenceLoc{CR->getNestedNameSpecifierLoc(),
1126CR->getConceptNameLoc(),
1127/*IsDecl=*/false,
1128{CR->getNamedConcept()}}};
1129
1130// We do not have location information for other nodes (QualType, etc)
1131return {};
1132}
1133
1134void visitNode(DynTypedNode N) {
1135for (auto &R : explicitReference(N))
1136reportReference(std::move(R), N);
1137}
1138
1139void reportReference(ReferenceLoc &&Ref, DynTypedNode N) {
1140// Strip null targets that can arise from invalid code.
1141// (This avoids having to check for null everywhere we insert)
1142llvm::erase(Ref.Targets, nullptr);
1143// Our promise is to return only references from the source code. If we lack
1144// location information, skip these nodes.
1145// Normally this should not happen in practice, unless there are bugs in the
1146// traversals or users started the traversal at an implicit node.
1147if (Ref.NameLoc.isInvalid()) {
1148dlog("invalid location at node {0}", nodeToString(N));
1149return;
1150}
1151Out(Ref);
1152}
1153
1154llvm::function_ref<void(ReferenceLoc)> Out;
1155const HeuristicResolver *Resolver;
1156/// TypeLocs starting at these locations must be skipped, see
1157/// TraverseElaboratedTypeSpecifierLoc for details.
1158llvm::DenseSet<SourceLocation> TypeLocsToSkip;
1159};
1160} // namespace
1161
1162void findExplicitReferences(const Stmt *S,
1163llvm::function_ref<void(ReferenceLoc)> Out,
1164const HeuristicResolver *Resolver) {
1165assert(S);
1166ExplicitReferenceCollector(Out, Resolver).TraverseStmt(const_cast<Stmt *>(S));
1167}
1168void findExplicitReferences(const Decl *D,
1169llvm::function_ref<void(ReferenceLoc)> Out,
1170const HeuristicResolver *Resolver) {
1171assert(D);
1172ExplicitReferenceCollector(Out, Resolver).TraverseDecl(const_cast<Decl *>(D));
1173}
1174void findExplicitReferences(const ASTContext &AST,
1175llvm::function_ref<void(ReferenceLoc)> Out,
1176const HeuristicResolver *Resolver) {
1177ExplicitReferenceCollector(Out, Resolver)
1178.TraverseAST(const_cast<ASTContext &>(AST));
1179}
1180
1181llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
1182switch (R) {
1183#define REL_CASE(X) \
1184case DeclRelation::X: \
1185return OS << #X;
1186REL_CASE(Alias);
1187REL_CASE(Underlying);
1188REL_CASE(TemplateInstantiation);
1189REL_CASE(TemplatePattern);
1190#undef REL_CASE
1191}
1192llvm_unreachable("Unhandled DeclRelation enum");
1193}
1194llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
1195const char *Sep = "";
1196for (unsigned I = 0; I < RS.S.size(); ++I) {
1197if (RS.S.test(I)) {
1198OS << Sep << static_cast<DeclRelation>(I);
1199Sep = "|";
1200}
1201}
1202return OS;
1203}
1204
1205llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
1206// note we cannot print R.NameLoc without a source manager.
1207OS << "targets = {";
1208llvm::SmallVector<std::string> Targets;
1209for (const NamedDecl *T : R.Targets) {
1210llvm::raw_string_ostream Target(Targets.emplace_back());
1211Target << printQualifiedName(*T) << printTemplateSpecializationArgs(*T);
1212}
1213llvm::sort(Targets);
1214OS << llvm::join(Targets, ", ");
1215OS << "}";
1216if (R.Qualifier) {
1217OS << ", qualifier = '";
1218R.Qualifier.getNestedNameSpecifier()->print(OS,
1219PrintingPolicy(LangOptions()));
1220OS << "'";
1221}
1222if (R.IsDecl)
1223OS << ", decl";
1224return OS;
1225}
1226
1227} // namespace clangd
1228} // namespace clang
1229