blob: 27f6532bf5aca1be15726b76fb4cbb0f36f8e8a5 [file] [log] [blame]
Sam McCall489cc582019-09-03 11:35:50 +00001//===--- 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 "Logger.h"
12#include "clang/AST/ASTTypeTraits.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000013#include "clang/AST/Decl.h"
Sam McCall489cc582019-09-03 11:35:50 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/DeclarationName.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000018#include "clang/AST/Expr.h"
Ilya Biryukov27744572019-09-27 09:39:10 +000019#include "clang/AST/ExprCXX.h"
Nathan Ridgecbcd07a2020-01-21 16:55:43 -050020#include "clang/AST/ExprConcepts.h"
Sam McCall489cc582019-09-03 11:35:50 +000021#include "clang/AST/ExprObjC.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000022#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/PrettyPrinter.h"
24#include "clang/AST/RecursiveASTVisitor.h"
Sam McCall489cc582019-09-03 11:35:50 +000025#include "clang/AST/StmtVisitor.h"
Ilya Biryukov1d32da82019-10-01 10:02:23 +000026#include "clang/AST/TemplateBase.h"
Sam McCall489cc582019-09-03 11:35:50 +000027#include "clang/AST/Type.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000028#include "clang/AST/TypeLoc.h"
Sam McCall489cc582019-09-03 11:35:50 +000029#include "clang/AST/TypeLocVisitor.h"
Dmitri Gribenkocbaa3262020-01-16 15:57:28 +010030#include "clang/AST/TypeVisitor.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000031#include "clang/Basic/LangOptions.h"
Nathan Ridge16f47cf2019-12-15 20:42:25 -050032#include "clang/Basic/OperatorKinds.h"
Sam McCall489cc582019-09-03 11:35:50 +000033#include "clang/Basic/SourceLocation.h"
Haojian Wue326f522020-02-20 15:22:07 +010034#include "clang/Basic/Specifiers.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000035#include "llvm/ADT/STLExtras.h"
36#include "llvm/ADT/SmallVector.h"
Sam McCall489cc582019-09-03 11:35:50 +000037#include "llvm/Support/Casting.h"
38#include "llvm/Support/Compiler.h"
39#include "llvm/Support/raw_ostream.h"
Nathan Ridge31b7f0e2020-03-12 18:22:44 -040040#include <iterator>
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000041#include <utility>
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +010042#include <vector>
Sam McCall489cc582019-09-03 11:35:50 +000043
44namespace clang {
45namespace clangd {
46namespace {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000047using ast_type_traits::DynTypedNode;
Sam McCall489cc582019-09-03 11:35:50 +000048
Sam McCall80195e72019-09-03 13:54:27 +000049LLVM_ATTRIBUTE_UNUSED std::string
Sam McCall489cc582019-09-03 11:35:50 +000050nodeToString(const ast_type_traits::DynTypedNode &N) {
Benjamin Krameradcd0262020-01-28 20:23:46 +010051 std::string S = std::string(N.getNodeKind().asStringRef());
Sam McCall489cc582019-09-03 11:35:50 +000052 {
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
Nathan Ridgeecaa9362019-12-05 18:29:32 -050061// Given a dependent type and a member name, heuristically resolve the
62// name to one or more declarations.
63// The current heuristic is simply to look up the name in the primary
64// template. This is a heuristic because the template could potentially
65// have specializations that declare different members.
66// Multiple declarations could be returned if the name is overloaded
67// (e.g. an overloaded method in the primary template).
68// This heuristic will give the desired answer in many cases, e.g.
69// for a call to vector<T>::size().
Nathan Ridge16f47cf2019-12-15 20:42:25 -050070// The name to look up is provided in the form of a factory that takes
71// an ASTContext, because an ASTContext may be needed to obtain the
72// name (e.g. if it's an operator name), but the caller may not have
73// access to an ASTContext.
74std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
75 const Type *T,
76 llvm::function_ref<DeclarationName(ASTContext &)> NameFactory,
77 bool IsNonstaticMember) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -050078 if (!T)
79 return {};
Nathan Ridge31b7f0e2020-03-12 18:22:44 -040080 if (auto *ET = T->getAs<EnumType>()) {
81 auto Result =
82 ET->getDecl()->lookup(NameFactory(ET->getDecl()->getASTContext()));
83 return {Result.begin(), Result.end()};
84 }
Nathan Ridgeecaa9362019-12-05 18:29:32 -050085 if (auto *ICNT = T->getAs<InjectedClassNameType>()) {
86 T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
87 }
88 auto *TST = T->getAs<TemplateSpecializationType>();
89 if (!TST)
90 return {};
91 const ClassTemplateDecl *TD = dyn_cast_or_null<ClassTemplateDecl>(
92 TST->getTemplateName().getAsTemplateDecl());
93 if (!TD)
94 return {};
95 CXXRecordDecl *RD = TD->getTemplatedDecl();
96 if (!RD->hasDefinition())
97 return {};
98 RD = RD->getDefinition();
Nathan Ridge16f47cf2019-12-15 20:42:25 -050099 DeclarationName Name = NameFactory(RD->getASTContext());
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500100 return RD->lookupDependentName(Name, [=](const NamedDecl *D) {
101 return IsNonstaticMember ? D->isCXXInstanceMember()
102 : !D->isCXXInstanceMember();
103 });
104}
105
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500106// Given the type T of a dependent expression that appears of the LHS of a "->",
107// heuristically find a corresponding pointee type in whose scope we could look
108// up the name appearing on the RHS.
109const Type *getPointeeType(const Type *T) {
110 if (!T)
111 return nullptr;
112
113 if (T->isPointerType()) {
114 return T->getAs<PointerType>()->getPointeeType().getTypePtrOrNull();
115 }
116
117 // Try to handle smart pointer types.
118
119 // Look up operator-> in the primary template. If we find one, it's probably a
120 // smart pointer type.
121 auto ArrowOps = getMembersReferencedViaDependentName(
122 T,
123 [](ASTContext &Ctx) {
124 return Ctx.DeclarationNames.getCXXOperatorName(OO_Arrow);
125 },
126 /*IsNonStaticMember=*/true);
127 if (ArrowOps.empty())
128 return nullptr;
129
130 // Getting the return type of the found operator-> method decl isn't useful,
131 // because we discarded template arguments to perform lookup in the primary
132 // template scope, so the return type would just have the form U* where U is a
133 // template parameter type.
134 // Instead, just handle the common case where the smart pointer type has the
135 // form of SmartPtr<X, ...>, and assume X is the pointee type.
136 auto *TST = T->getAs<TemplateSpecializationType>();
137 if (!TST)
138 return nullptr;
139 if (TST->getNumArgs() == 0)
140 return nullptr;
141 const TemplateArgument &FirstArg = TST->getArg(0);
142 if (FirstArg.getKind() != TemplateArgument::Type)
143 return nullptr;
144 return FirstArg.getAsType().getTypePtrOrNull();
145}
146
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100147const NamedDecl *getTemplatePattern(const NamedDecl *D) {
148 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
Haojian Wue326f522020-02-20 15:22:07 +0100149 if (const auto *Result = CRD->getTemplateInstantiationPattern())
150 return Result;
151 // getTemplateInstantiationPattern returns null if the Specialization is
152 // incomplete (e.g. the type didn't need to be complete), fall back to the
153 // primary template.
154 if (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
155 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
156 return Spec->getSpecializedTemplate()->getTemplatedDecl();
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100157 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
158 return FD->getTemplateInstantiationPattern();
159 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
160 // Hmm: getTIP returns its arg if it's not an instantiation?!
161 VarDecl *T = VD->getTemplateInstantiationPattern();
162 return (T == D) ? nullptr : T;
163 } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
164 return ED->getInstantiatedFromMemberEnum();
165 } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
166 if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext()))
167 if (const DeclContext *ParentPat =
168 dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
169 for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName()))
170 if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind())
171 return BaseND;
172 } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
173 if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
174 if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
175 for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
176 return BaseECD;
177 }
178 }
179 }
180 return nullptr;
181}
182
Sam McCall489cc582019-09-03 11:35:50 +0000183// TargetFinder locates the entities that an AST node refers to.
184//
185// Typically this is (possibly) one declaration and (possibly) one type, but
186// may be more:
187// - for ambiguous nodes like OverloadExpr
188// - if we want to include e.g. both typedefs and the underlying type
189//
190// This is organized as a set of mutually recursive helpers for particular node
191// types, but for most nodes this is a short walk rather than a deep traversal.
192//
193// It's tempting to do e.g. typedef resolution as a second normalization step,
194// after finding the 'primary' decl etc. But we do this monolithically instead
195// because:
196// - normalization may require these traversals again (e.g. unwrapping a
197// typedef reveals a decltype which must be traversed)
198// - it doesn't simplify that much, e.g. the first stage must still be able
199// to yield multiple decls to handle OverloadExpr
200// - there are cases where it's required for correctness. e.g:
201// template<class X> using pvec = vector<x*>; pvec<int> x;
202// There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>`
203// and both are lossy. We must know upfront what the caller ultimately wants.
204//
205// FIXME: improve common dependent scope using name lookup in primary templates.
206// e.g. template<typename T> int foo() { return std::vector<T>().size(); }
207// formally size() is unresolved, but the primary template is a good guess.
208// This affects:
209// - DependentTemplateSpecializationType,
Sam McCall489cc582019-09-03 11:35:50 +0000210// - DependentNameType
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500211// - UnresolvedUsingValueDecl
212// - UnresolvedUsingTypenameDecl
Sam McCall489cc582019-09-03 11:35:50 +0000213struct TargetFinder {
214 using RelSet = DeclRelationSet;
215 using Rel = DeclRelation;
Sam McCall489cc582019-09-03 11:35:50 +0000216
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100217private:
218 llvm::SmallDenseMap<const NamedDecl *,
219 std::pair<RelSet, /*InsertionOrder*/ size_t>>
220 Decls;
221 RelSet Flags;
Sam McCall489cc582019-09-03 11:35:50 +0000222
223 template <typename T> void debug(T &Node, RelSet Flags) {
224 dlog("visit [{0}] {1}", Flags,
225 nodeToString(ast_type_traits::DynTypedNode::create(Node)));
226 }
227
Sam McCallf06f4392020-01-03 17:26:33 +0100228 void report(const NamedDecl *D, RelSet Flags) {
Sam McCall489cc582019-09-03 11:35:50 +0000229 dlog("--> [{0}] {1}", Flags,
230 nodeToString(ast_type_traits::DynTypedNode::create(*D)));
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100231 auto It = Decls.try_emplace(D, std::make_pair(Flags, Decls.size()));
232 // If already exists, update the flags.
233 if (!It.second)
234 It.first->second.first |= Flags;
Sam McCall489cc582019-09-03 11:35:50 +0000235 }
236
237public:
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100238 llvm::SmallVector<std::pair<const NamedDecl *, RelSet>, 1> takeDecls() const {
239 using ValTy = std::pair<const NamedDecl *, RelSet>;
240 llvm::SmallVector<ValTy, 1> Result;
241 Result.resize(Decls.size());
242 for (const auto &Elem : Decls)
243 Result[Elem.second.second] = {Elem.first, Elem.second.first};
244 return Result;
245 }
246
Sam McCallf06f4392020-01-03 17:26:33 +0100247 void add(const Decl *Dcl, RelSet Flags) {
Sam McCall9a5c4482020-01-29 02:01:42 +0100248 const NamedDecl *D = llvm::dyn_cast_or_null<NamedDecl>(Dcl);
Sam McCall489cc582019-09-03 11:35:50 +0000249 if (!D)
250 return;
251 debug(*D, Flags);
252 if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
253 D = UDD->getNominatedNamespaceAsWritten();
254
255 if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
256 add(TND->getUnderlyingType(), Flags | Rel::Underlying);
257 Flags |= Rel::Alias; // continue with the alias.
258 } else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
259 for (const UsingShadowDecl *S : UD->shadows())
260 add(S->getUnderlyingDecl(), Flags | Rel::Underlying);
261 Flags |= Rel::Alias; // continue with the alias.
262 } else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
263 add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
264 Flags |= Rel::Alias; // continue with the alias
265 } else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
266 // Include the using decl, but don't traverse it. This may end up
267 // including *all* shadows, which we don't want.
268 report(USD->getUsingDecl(), Flags | Rel::Alias);
269 // Shadow decls are synthetic and not themselves interesting.
270 // Record the underlying decl instead, if allowed.
271 D = USD->getTargetDecl();
272 Flags |= Rel::Underlying; // continue with the underlying decl.
273 }
274
275 if (const Decl *Pat = getTemplatePattern(D)) {
276 assert(Pat != D);
277 add(Pat, Flags | Rel::TemplatePattern);
278 // Now continue with the instantiation.
279 Flags |= Rel::TemplateInstantiation;
280 }
281
282 report(D, Flags);
283 }
284
285 void add(const Stmt *S, RelSet Flags) {
286 if (!S)
287 return;
288 debug(*S, Flags);
289 struct Visitor : public ConstStmtVisitor<Visitor> {
290 TargetFinder &Outer;
291 RelSet Flags;
292 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
293
Haojian Wu939544a2019-11-27 16:22:16 +0100294 void VisitCallExpr(const CallExpr *CE) {
295 Outer.add(CE->getCalleeDecl(), Flags);
296 }
Nathan Ridgecbcd07a2020-01-21 16:55:43 -0500297 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
298 Outer.add(E->getNamedConcept(), Flags);
299 }
Sam McCall489cc582019-09-03 11:35:50 +0000300 void VisitDeclRefExpr(const DeclRefExpr *DRE) {
301 const Decl *D = DRE->getDecl();
302 // UsingShadowDecl allows us to record the UsingDecl.
303 // getFoundDecl() returns the wrong thing in other cases (templates).
304 if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
305 D = USD;
306 Outer.add(D, Flags);
307 }
308 void VisitMemberExpr(const MemberExpr *ME) {
309 const Decl *D = ME->getMemberDecl();
310 if (auto *USD =
311 llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
312 D = USD;
313 Outer.add(D, Flags);
314 }
Ilya Biryukova160a0b2019-10-01 07:27:55 +0000315 void VisitOverloadExpr(const OverloadExpr *OE) {
316 for (auto *D : OE->decls())
317 Outer.add(D, Flags);
318 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500319 void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
320 Outer.add(SE->getPack(), Flags);
321 }
Sam McCall489cc582019-09-03 11:35:50 +0000322 void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
323 Outer.add(CCE->getConstructor(), Flags);
324 }
325 void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
326 for (const DesignatedInitExpr::Designator &D :
327 llvm::reverse(DIE->designators()))
328 if (D.isFieldDesignator()) {
329 Outer.add(D.getField(), Flags);
330 // We don't know which designator was intended, we assume the outer.
331 break;
332 }
333 }
Haojian Wua533b032020-04-08 11:03:50 +0200334 void VisitGotoStmt(const GotoStmt *Goto) {
335 if (auto *LabelDecl = Goto->getLabel())
336 Outer.add(LabelDecl, Flags);
337 }
338 void VisitLabelStmt(const LabelStmt *Label) {
339 if (auto *LabelDecl = Label->getDecl())
340 Outer.add(LabelDecl, Flags);
341 }
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500342 void
343 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
344 const Type *BaseType = E->getBaseType().getTypePtrOrNull();
345 if (E->isArrow()) {
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500346 BaseType = getPointeeType(BaseType);
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500347 }
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500348 for (const NamedDecl *D : getMembersReferencedViaDependentName(
349 BaseType, [E](ASTContext &) { return E->getMember(); },
350 /*IsNonstaticMember=*/true)) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500351 Outer.add(D, Flags);
352 }
353 }
354 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
355 for (const NamedDecl *D : getMembersReferencedViaDependentName(
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500356 E->getQualifier()->getAsType(),
357 [E](ASTContext &) { return E->getDeclName(); },
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500358 /*IsNonstaticMember=*/false)) {
359 Outer.add(D, Flags);
360 }
361 }
Sam McCall489cc582019-09-03 11:35:50 +0000362 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
363 Outer.add(OIRE->getDecl(), Flags);
364 }
365 void VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
366 Outer.add(OME->getMethodDecl(), Flags);
367 }
368 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
369 if (OPRE->isExplicitProperty())
370 Outer.add(OPRE->getExplicitProperty(), Flags);
371 else {
372 if (OPRE->isMessagingGetter())
373 Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
374 if (OPRE->isMessagingSetter())
375 Outer.add(OPRE->getImplicitPropertySetter(), Flags);
376 }
377 }
378 void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
379 Outer.add(OPE->getProtocol(), Flags);
380 }
Sam McCall4c5a4512020-01-10 10:40:30 +0100381 void VisitOpaqueValueExpr(const OpaqueValueExpr *OVE) {
382 Outer.add(OVE->getSourceExpr(), Flags);
383 }
384 void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
385 Outer.add(POE->getSyntacticForm(), Flags);
386 }
Sam McCall489cc582019-09-03 11:35:50 +0000387 };
388 Visitor(*this, Flags).Visit(S);
389 }
390
391 void add(QualType T, RelSet Flags) {
392 if (T.isNull())
393 return;
394 debug(T, Flags);
395 struct Visitor : public TypeVisitor<Visitor> {
396 TargetFinder &Outer;
397 RelSet Flags;
398 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
399
400 void VisitTagType(const TagType *TT) {
401 Outer.add(TT->getAsTagDecl(), Flags);
402 }
Haojian Wu5d4e8992020-01-21 15:28:06 +0100403
Haojian Wueaf0c892020-02-05 09:56:13 +0100404 void VisitElaboratedType(const ElaboratedType *ET) {
405 Outer.add(ET->desugar(), Flags);
406 }
407
Haojian Wu5d4e8992020-01-21 15:28:06 +0100408 void VisitInjectedClassNameType(const InjectedClassNameType *ICNT) {
409 Outer.add(ICNT->getDecl(), Flags);
410 }
411
Sam McCall489cc582019-09-03 11:35:50 +0000412 void VisitDecltypeType(const DecltypeType *DTT) {
413 Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
414 }
415 void VisitDeducedType(const DeducedType *DT) {
416 // FIXME: In practice this doesn't work: the AutoType you find inside
417 // TypeLoc never has a deduced type. https://llvm.org/PR42914
418 Outer.add(DT->getDeducedType(), Flags | Rel::Underlying);
419 }
Nathan Ridge6a69d3c2020-01-02 00:45:01 -0500420 void VisitDeducedTemplateSpecializationType(
421 const DeducedTemplateSpecializationType *DTST) {
422 // FIXME: This is a workaround for https://llvm.org/PR42914,
423 // which is causing DTST->getDeducedType() to be empty. We
424 // fall back to the template pattern and miss the instantiation
425 // even when it's known in principle. Once that bug is fixed,
426 // this method can be removed (the existing handling in
427 // VisitDeducedType() is sufficient).
428 if (auto *TD = DTST->getTemplateName().getAsTemplateDecl())
429 Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
430 }
Sam McCall489cc582019-09-03 11:35:50 +0000431 void VisitTypedefType(const TypedefType *TT) {
432 Outer.add(TT->getDecl(), Flags);
433 }
434 void
435 VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
436 // Have to handle these case-by-case.
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.
442 if (TST->isTypeAlias()) {
443 Outer.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 Outer.report(
447 TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
448 Flags | Rel::Alias | Rel::TemplatePattern);
449 }
450 // specializations of template template parameters aren't instantiated
451 // into decls, so they must refer to the parameter itself.
452 else if (const auto *Parm =
453 llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
454 TST->getTemplateName().getAsTemplateDecl()))
455 Outer.add(Parm, Flags);
456 // class template specializations have a (specialized) CXXRecordDecl.
457 else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
458 Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
459 else {
460 // fallback: the (un-specialized) declaration from primary template.
461 if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
462 Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
463 }
464 }
465 void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
466 Outer.add(TTPT->getDecl(), Flags);
467 }
468 void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
469 Outer.add(OIT->getDecl(), Flags);
470 }
471 void VisitObjCObjectType(const ObjCObjectType *OOT) {
472 // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so
473 // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo.
474 if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1)
475 Outer.add(OOT->getProtocol(0), Flags);
476 }
477 };
478 Visitor(*this, Flags).Visit(T.getTypePtr());
479 }
480
481 void add(const NestedNameSpecifier *NNS, RelSet Flags) {
482 if (!NNS)
483 return;
484 debug(*NNS, Flags);
485 switch (NNS->getKind()) {
486 case NestedNameSpecifier::Identifier:
487 return;
488 case NestedNameSpecifier::Namespace:
489 add(NNS->getAsNamespace(), Flags);
490 return;
491 case NestedNameSpecifier::NamespaceAlias:
492 add(NNS->getAsNamespaceAlias(), Flags);
493 return;
494 case NestedNameSpecifier::TypeSpec:
495 case NestedNameSpecifier::TypeSpecWithTemplate:
496 add(QualType(NNS->getAsType(), 0), Flags);
497 return;
498 case NestedNameSpecifier::Global:
499 // This should be TUDecl, but we can't get a pointer to it!
500 return;
501 case NestedNameSpecifier::Super:
502 add(NNS->getAsRecordDecl(), Flags);
503 return;
504 }
505 llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
506 }
507
508 void add(const CXXCtorInitializer *CCI, RelSet Flags) {
509 if (!CCI)
510 return;
511 debug(*CCI, Flags);
512
513 if (CCI->isAnyMemberInitializer())
514 add(CCI->getAnyMember(), Flags);
515 // Constructor calls contain a TypeLoc node, so we don't handle them here.
516 }
517};
518
519} // namespace
520
Sam McCallf06f4392020-01-03 17:26:33 +0100521llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000522allTargetDecls(const ast_type_traits::DynTypedNode &N) {
523 dlog("allTargetDecls({0})", nodeToString(N));
524 TargetFinder Finder;
525 DeclRelationSet Flags;
526 if (const Decl *D = N.get<Decl>())
527 Finder.add(D, Flags);
528 else if (const Stmt *S = N.get<Stmt>())
529 Finder.add(S, Flags);
530 else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
531 Finder.add(NNSL->getNestedNameSpecifier(), Flags);
532 else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
533 Finder.add(NNS, Flags);
534 else if (const TypeLoc *TL = N.get<TypeLoc>())
535 Finder.add(TL->getType(), Flags);
536 else if (const QualType *QT = N.get<QualType>())
537 Finder.add(*QT, Flags);
538 else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
539 Finder.add(CCI, Flags);
540
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100541 return Finder.takeDecls();
Sam McCall489cc582019-09-03 11:35:50 +0000542}
543
Sam McCallf06f4392020-01-03 17:26:33 +0100544llvm::SmallVector<const NamedDecl *, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000545targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) {
Sam McCallf06f4392020-01-03 17:26:33 +0100546 llvm::SmallVector<const NamedDecl *, 1> Result;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000547 for (const auto &Entry : allTargetDecls(N)) {
Sam McCall489cc582019-09-03 11:35:50 +0000548 if (!(Entry.second & ~Mask))
549 Result.push_back(Entry.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000550 }
Sam McCall489cc582019-09-03 11:35:50 +0000551 return Result;
552}
553
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000554llvm::SmallVector<const NamedDecl *, 1>
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100555explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000556 assert(!(Mask & (DeclRelation::TemplatePattern |
557 DeclRelation::TemplateInstantiation)) &&
Kazuaki Ishizakidd5571d2020-04-05 15:28:11 +0900558 "explicitReferenceTargets handles templates on its own");
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000559 auto Decls = allTargetDecls(N);
560
561 // We prefer to return template instantiation, but fallback to template
562 // pattern if instantiation is not available.
563 Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation;
564
565 llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
566 llvm::SmallVector<const NamedDecl *, 1> Targets;
567 bool SeenTemplateInstantiations = false;
568 for (auto &D : Decls) {
569 if (D.second & ~Mask)
570 continue;
571 if (D.second & DeclRelation::TemplatePattern) {
Sam McCallf06f4392020-01-03 17:26:33 +0100572 TemplatePatterns.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000573 continue;
574 }
575 if (D.second & DeclRelation::TemplateInstantiation)
576 SeenTemplateInstantiations = true;
Sam McCallf06f4392020-01-03 17:26:33 +0100577 Targets.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000578 }
579 if (!SeenTemplateInstantiations)
580 Targets.insert(Targets.end(), TemplatePatterns.begin(),
581 TemplatePatterns.end());
582 return Targets;
583}
584
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100585namespace {
Haojian Wu65f61c02019-10-18 12:07:19 +0000586llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000587 struct Visitor : ConstDeclVisitor<Visitor> {
Haojian Wu65f61c02019-10-18 12:07:19 +0000588 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000589
590 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000591 // We want to keep it as non-declaration references, as the
592 // "using namespace" declaration doesn't have a name.
593 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
594 D->getIdentLocation(),
595 /*IsDecl=*/false,
596 {D->getNominatedNamespaceAsWritten()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000597 }
598
599 void VisitUsingDecl(const UsingDecl *D) {
Kazuaki Ishizakib7ecf1c2020-01-04 10:28:41 -0500600 // "using ns::identifier;" is a non-declaration reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000601 Refs.push_back(
602 ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
603 explicitReferenceTargets(DynTypedNode::create(*D),
604 DeclRelation::Underlying)});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000605 }
606
607 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000608 // For namespace alias, "namespace Foo = Target;", we add two references.
609 // Add a declaration reference for Foo.
610 VisitNamedDecl(D);
611 // Add a non-declaration reference for Target.
612 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
613 D->getTargetNameLoc(),
614 /*IsDecl=*/false,
615 {D->getAliasedNamespace()}});
616 }
617
618 void VisitNamedDecl(const NamedDecl *ND) {
Kadir Cetinkaya9570f1e2020-01-21 15:15:06 +0100619 // We choose to ignore {Class, Function, Var, TypeAlias}TemplateDecls. As
620 // as their underlying decls, covering the same range, will be visited.
621 if (llvm::isa<ClassTemplateDecl>(ND) ||
622 llvm::isa<FunctionTemplateDecl>(ND) ||
623 llvm::isa<VarTemplateDecl>(ND) ||
624 llvm::isa<TypeAliasTemplateDecl>(ND))
625 return;
Haojian Wu65f61c02019-10-18 12:07:19 +0000626 // FIXME: decide on how to surface destructors when we need them.
627 if (llvm::isa<CXXDestructorDecl>(ND))
628 return;
Ilya Biryukov4c430a72019-10-28 14:41:06 +0100629 // Filter anonymous decls, name location will point outside the name token
630 // and the clients are not prepared to handle that.
631 if (ND->getDeclName().isIdentifier() &&
632 !ND->getDeclName().getAsIdentifierInfo())
633 return;
634 Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
635 ND->getLocation(),
636 /*IsDecl=*/true,
637 {ND}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000638 }
639 };
640
641 Visitor V;
642 V.Visit(D);
Haojian Wu65f61c02019-10-18 12:07:19 +0000643 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000644}
645
Haojian Wu65f61c02019-10-18 12:07:19 +0000646llvm::SmallVector<ReferenceLoc, 2> refInExpr(const Expr *E) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000647 struct Visitor : ConstStmtVisitor<Visitor> {
Sam McCallf4871ea2020-01-10 16:27:12 +0100648 // FIXME: handle more complicated cases: more ObjC, designated initializers.
Haojian Wu65f61c02019-10-18 12:07:19 +0000649 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000650
Nathan Ridgec6c5dbc2020-01-21 13:21:08 -0500651 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
652 Refs.push_back(ReferenceLoc{E->getNestedNameSpecifierLoc(),
653 E->getConceptNameLoc(),
654 /*IsDecl=*/false,
655 {E->getNamedConcept()}});
656 }
Nathan Ridged83541d2020-03-19 15:42:10 -0400657
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000658 void VisitDeclRefExpr(const DeclRefExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000659 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
660 E->getNameInfo().getLoc(),
661 /*IsDecl=*/false,
662 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000663 }
664
Nathan Ridged83541d2020-03-19 15:42:10 -0400665 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
666 Refs.push_back(ReferenceLoc{
667 E->getQualifierLoc(), E->getNameInfo().getLoc(), /*IsDecl=*/false,
668 explicitReferenceTargets(DynTypedNode::create(*E), {})});
669 }
670
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000671 void VisitMemberExpr(const MemberExpr *E) {
Kirill Bobyrev38bdb942020-01-21 05:33:39 +0100672 // Skip destructor calls to avoid duplication: TypeLoc within will be
673 // visited separately.
674 if (llvm::dyn_cast<CXXDestructorDecl>(E->getFoundDecl().getDecl()))
675 return;
Haojian Wu65f61c02019-10-18 12:07:19 +0000676 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
677 E->getMemberNameInfo().getLoc(),
678 /*IsDecl=*/false,
679 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000680 }
Ilya Biryukov27744572019-09-27 09:39:10 +0000681
Nathan Ridged83541d2020-03-19 15:42:10 -0400682 void
683 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
684 Refs.push_back(
685 ReferenceLoc{E->getQualifierLoc(), E->getMemberNameInfo().getLoc(),
686 /*IsDecl=*/false,
687 explicitReferenceTargets(DynTypedNode::create(*E), {})});
688 }
689
Ilya Biryukov27744572019-09-27 09:39:10 +0000690 void VisitOverloadExpr(const OverloadExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000691 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
692 E->getNameInfo().getLoc(),
693 /*IsDecl=*/false,
694 llvm::SmallVector<const NamedDecl *, 1>(
695 E->decls().begin(), E->decls().end())});
Ilya Biryukov27744572019-09-27 09:39:10 +0000696 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500697
698 void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
699 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
700 E->getPackLoc(),
701 /*IsDecl=*/false,
702 {E->getPack()}});
703 }
Sam McCallf4871ea2020-01-10 16:27:12 +0100704
705 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) {
706 Refs.push_back(ReferenceLoc{
707 NestedNameSpecifierLoc(), E->getLocation(),
708 /*IsDecl=*/false,
709 // Select the getter, setter, or @property depending on the call.
710 explicitReferenceTargets(DynTypedNode::create(*E), {})});
711 }
Kirill Bobyrev9f6d8de2020-02-10 11:53:17 +0100712
713 void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
714 for (const DesignatedInitExpr::Designator &D : DIE->designators()) {
715 if (!D.isFieldDesignator())
716 continue;
717 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
718 D.getFieldLoc(),
719 /*IsDecl=*/false,
720 {D.getField()}});
721 }
722 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000723 };
724
725 Visitor V;
726 V.Visit(E);
Haojian Wu65f61c02019-10-18 12:07:19 +0000727 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000728}
729
Haojian Wu65f61c02019-10-18 12:07:19 +0000730llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000731 struct Visitor : TypeLocVisitor<Visitor> {
732 llvm::Optional<ReferenceLoc> Ref;
733
734 void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
735 // We only know about qualifier, rest if filled by inner locations.
736 Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
737 // Fill in the qualifier.
738 if (!Ref)
739 return;
740 assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
741 Ref->Qualifier = L.getQualifierLoc();
742 }
743
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000744 void VisitTagTypeLoc(TagTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000745 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
746 L.getNameLoc(),
747 /*IsDecl=*/false,
748 {L.getDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000749 }
750
Ilya Biryukovc3835092019-09-27 10:55:53 +0000751 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000752 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
753 L.getNameLoc(),
754 /*IsDecl=*/false,
755 {L.getDecl()}};
Ilya Biryukovc3835092019-09-27 10:55:53 +0000756 }
757
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000758 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000759 // We must ensure template type aliases are included in results if they
760 // were written in the source code, e.g. in
761 // template <class T> using valias = vector<T>;
762 // ^valias<int> x;
763 // 'explicitReferenceTargets' will return:
764 // 1. valias with mask 'Alias'.
765 // 2. 'vector<int>' with mask 'Underlying'.
766 // we want to return only #1 in this case.
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000767 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000768 NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000769 explicitReferenceTargets(DynTypedNode::create(L.getType()),
770 DeclRelation::Alias)};
771 }
772 void VisitDeducedTemplateSpecializationTypeLoc(
773 DeducedTemplateSpecializationTypeLoc L) {
774 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000775 NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000776 explicitReferenceTargets(DynTypedNode::create(L.getType()),
777 DeclRelation::Alias)};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000778 }
779
Haojian Wuf651c402020-01-21 11:50:57 +0100780 void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
781 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
782 TL.getNameLoc(),
783 /*IsDecl=*/false,
784 {TL.getDecl()}};
785 }
786
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000787 void VisitDependentTemplateSpecializationTypeLoc(
788 DependentTemplateSpecializationTypeLoc L) {
789 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000790 L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100791 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000792 }
793
794 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
795 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000796 L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100797 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000798 }
799
800 void VisitTypedefTypeLoc(TypedefTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000801 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
802 L.getNameLoc(),
803 /*IsDecl=*/false,
804 {L.getTypedefNameDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000805 }
806 };
807
808 Visitor V;
809 V.Visit(L.getUnqualifiedLoc());
Haojian Wu65f61c02019-10-18 12:07:19 +0000810 if (!V.Ref)
811 return {};
812 return {*V.Ref};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000813}
814
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100815class ExplicitReferenceCollector
816 : public RecursiveASTVisitor<ExplicitReferenceCollector> {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000817public:
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100818 ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out)
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000819 : Out(Out) {
820 assert(Out);
821 }
822
823 bool VisitTypeLoc(TypeLoc TTL) {
824 if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
825 return true;
826 visitNode(DynTypedNode::create(TTL));
827 return true;
828 }
829
830 bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
831 // ElaboratedTypeLoc will reports information for its inner type loc.
832 // Otherwise we loose information about inner types loc's qualifier.
833 TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
834 TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
835 return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
836 }
837
838 bool VisitExpr(Expr *E) {
839 visitNode(DynTypedNode::create(*E));
840 return true;
841 }
842
Sam McCallf4871ea2020-01-10 16:27:12 +0100843 bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) {
844 visitNode(DynTypedNode::create(*OVE));
845 // Not clear why the source expression is skipped by default...
846 // FIXME: can we just make RecursiveASTVisitor do this?
847 return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr());
848 }
849
850 bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) {
851 visitNode(DynTypedNode::create(*POE));
852 // Traverse only the syntactic form to find the *written* references.
853 // (The semantic form also contains lots of duplication)
854 return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm());
855 }
856
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000857 // We re-define Traverse*, since there's no corresponding Visit*.
858 // TemplateArgumentLoc is the only way to get locations for references to
859 // template template parameters.
860 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
861 switch (A.getArgument().getKind()) {
862 case TemplateArgument::Template:
863 case TemplateArgument::TemplateExpansion:
864 reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
865 A.getTemplateNameLoc(),
Haojian Wu65f61c02019-10-18 12:07:19 +0000866 /*IsDecl=*/false,
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000867 {A.getArgument()
868 .getAsTemplateOrTemplatePattern()
869 .getAsTemplateDecl()}},
870 DynTypedNode::create(A.getArgument()));
871 break;
872 case TemplateArgument::Declaration:
873 break; // FIXME: can this actually happen in TemplateArgumentLoc?
874 case TemplateArgument::Integral:
875 case TemplateArgument::Null:
876 case TemplateArgument::NullPtr:
877 break; // no references.
878 case TemplateArgument::Pack:
879 case TemplateArgument::Type:
880 case TemplateArgument::Expression:
881 break; // Handled by VisitType and VisitExpression.
882 };
883 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
884 }
885
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000886 bool VisitDecl(Decl *D) {
887 visitNode(DynTypedNode::create(*D));
888 return true;
889 }
890
891 // We have to use Traverse* because there is no corresponding Visit*.
892 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
893 if (!L.getNestedNameSpecifier())
894 return true;
895 visitNode(DynTypedNode::create(L));
896 // Inner type is missing information about its qualifier, skip it.
897 if (auto TL = L.getTypeLoc())
898 TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
899 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
900 }
901
Haojian Wu13fc8992019-10-21 10:11:30 +0200902 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
903 visitNode(DynTypedNode::create(*Init));
904 return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
905 }
906
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000907private:
908 /// Obtain information about a reference directly defined in \p N. Does not
909 /// recurse into child nodes, e.g. do not expect references for constructor
910 /// initializers
911 ///
912 /// Any of the fields in the returned structure can be empty, but not all of
913 /// them, e.g.
914 /// - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
915 /// source location information may be missing,
916 /// - for dependent code, targets may be empty.
917 ///
918 /// (!) For the purposes of this function declarations are not considered to
919 /// be references. However, declarations can have references inside them,
920 /// e.g. 'namespace foo = std' references namespace 'std' and this
921 /// function will return the corresponding reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000922 llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000923 if (auto *D = N.get<Decl>())
924 return refInDecl(D);
925 if (auto *E = N.get<Expr>())
926 return refInExpr(E);
Ilya Biryukov733777a2019-10-31 11:58:57 +0100927 if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
928 // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
929 return {ReferenceLoc{
930 NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
931 explicitReferenceTargets(
932 DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
933 DeclRelation::Alias)}};
934 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000935 if (const TypeLoc *TL = N.get<TypeLoc>())
936 return refInTypeLoc(*TL);
937 if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
Haojian Wu13fc8992019-10-21 10:11:30 +0200938 // Other type initializers (e.g. base initializer) are handled by visiting
939 // the typeLoc.
940 if (CCI->isAnyMemberInitializer()) {
941 return {ReferenceLoc{NestedNameSpecifierLoc(),
942 CCI->getMemberLocation(),
943 /*IsDecl=*/false,
944 {CCI->getAnyMember()}}};
945 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000946 }
947 // We do not have location information for other nodes (QualType, etc)
Haojian Wu65f61c02019-10-18 12:07:19 +0000948 return {};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000949 }
950
951 void visitNode(DynTypedNode N) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000952 for (const auto &R : explicitReference(N))
953 reportReference(R, N);
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000954 }
955
956 void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000957 // Our promise is to return only references from the source code. If we lack
958 // location information, skip these nodes.
959 // Normally this should not happen in practice, unless there are bugs in the
960 // traversals or users started the traversal at an implicit node.
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000961 if (Ref.NameLoc.isInvalid()) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000962 dlog("invalid location at node {0}", nodeToString(N));
963 return;
964 }
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000965 Out(Ref);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000966 }
967
968 llvm::function_ref<void(ReferenceLoc)> Out;
969 /// TypeLocs starting at these locations must be skipped, see
970 /// TraverseElaboratedTypeSpecifierLoc for details.
971 llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
972};
973} // namespace
974
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000975void findExplicitReferences(const Stmt *S,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000976 llvm::function_ref<void(ReferenceLoc)> Out) {
977 assert(S);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100978 ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000979}
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000980void findExplicitReferences(const Decl *D,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000981 llvm::function_ref<void(ReferenceLoc)> Out) {
982 assert(D);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100983 ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000984}
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100985void findExplicitReferences(const ASTContext &AST,
986 llvm::function_ref<void(ReferenceLoc)> Out) {
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100987 ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST));
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100988}
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000989
Sam McCall489cc582019-09-03 11:35:50 +0000990llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
991 switch (R) {
992#define REL_CASE(X) \
993 case DeclRelation::X: \
994 return OS << #X;
995 REL_CASE(Alias);
996 REL_CASE(Underlying);
997 REL_CASE(TemplateInstantiation);
998 REL_CASE(TemplatePattern);
999#undef REL_CASE
Michael Liaode524032019-09-03 15:02:46 +00001000 }
Simon Pilgrim54b989c2019-09-03 13:05:13 +00001001 llvm_unreachable("Unhandled DeclRelation enum");
Sam McCall489cc582019-09-03 11:35:50 +00001002}
1003llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
1004 const char *Sep = "";
1005 for (unsigned I = 0; I < RS.S.size(); ++I) {
1006 if (RS.S.test(I)) {
1007 OS << Sep << static_cast<DeclRelation>(I);
1008 Sep = "|";
1009 }
1010 }
1011 return OS;
1012}
1013
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001014llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
1015 // note we cannot print R.NameLoc without a source manager.
1016 OS << "targets = {";
1017 bool First = true;
1018 for (const NamedDecl *T : R.Targets) {
1019 if (!First)
1020 OS << ", ";
1021 else
1022 First = false;
1023 OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T);
1024 }
1025 OS << "}";
1026 if (R.Qualifier) {
1027 OS << ", qualifier = '";
1028 R.Qualifier.getNestedNameSpecifier()->print(OS,
1029 PrintingPolicy(LangOptions()));
1030 OS << "'";
1031 }
Haojian Wu65f61c02019-10-18 12:07:19 +00001032 if (R.IsDecl)
1033 OS << ", decl";
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001034 return OS;
1035}
1036
Sam McCall489cc582019-09-03 11:35:50 +00001037} // namespace clangd
1038} // namespace clang