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