blob: fb3001fca94cb50782be81af8ae1d7b04dc2062a [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"
Sam McCall489cc582019-09-03 11:35:50 +000020#include "clang/AST/ExprObjC.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000021#include "clang/AST/NestedNameSpecifier.h"
22#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/RecursiveASTVisitor.h"
Sam McCall489cc582019-09-03 11:35:50 +000024#include "clang/AST/StmtVisitor.h"
Ilya Biryukov1d32da82019-10-01 10:02:23 +000025#include "clang/AST/TemplateBase.h"
Sam McCall489cc582019-09-03 11:35:50 +000026#include "clang/AST/Type.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000027#include "clang/AST/TypeLoc.h"
Sam McCall489cc582019-09-03 11:35:50 +000028#include "clang/AST/TypeLocVisitor.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000029#include "clang/Basic/LangOptions.h"
Nathan Ridge16f47cf2019-12-15 20:42:25 -050030#include "clang/Basic/OperatorKinds.h"
Sam McCall489cc582019-09-03 11:35:50 +000031#include "clang/Basic/SourceLocation.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000032#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sam McCall489cc582019-09-03 11:35:50 +000034#include "llvm/Support/Casting.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/raw_ostream.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000037#include <utility>
Sam McCall489cc582019-09-03 11:35:50 +000038
39namespace clang {
40namespace clangd {
41namespace {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000042using ast_type_traits::DynTypedNode;
Sam McCall489cc582019-09-03 11:35:50 +000043
Sam McCall80195e72019-09-03 13:54:27 +000044LLVM_ATTRIBUTE_UNUSED std::string
Sam McCall489cc582019-09-03 11:35:50 +000045nodeToString(const ast_type_traits::DynTypedNode &N) {
46 std::string S = N.getNodeKind().asStringRef();
47 {
48 llvm::raw_string_ostream OS(S);
49 OS << ": ";
50 N.print(OS, PrintingPolicy(LangOptions()));
51 }
52 std::replace(S.begin(), S.end(), '\n', ' ');
53 return S;
54}
55
Nathan Ridgeecaa9362019-12-05 18:29:32 -050056// Given a dependent type and a member name, heuristically resolve the
57// name to one or more declarations.
58// The current heuristic is simply to look up the name in the primary
59// template. This is a heuristic because the template could potentially
60// have specializations that declare different members.
61// Multiple declarations could be returned if the name is overloaded
62// (e.g. an overloaded method in the primary template).
63// This heuristic will give the desired answer in many cases, e.g.
64// for a call to vector<T>::size().
Nathan Ridge16f47cf2019-12-15 20:42:25 -050065// The name to look up is provided in the form of a factory that takes
66// an ASTContext, because an ASTContext may be needed to obtain the
67// name (e.g. if it's an operator name), but the caller may not have
68// access to an ASTContext.
69std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
70 const Type *T,
71 llvm::function_ref<DeclarationName(ASTContext &)> NameFactory,
72 bool IsNonstaticMember) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -050073 if (!T)
74 return {};
75 if (auto *ICNT = T->getAs<InjectedClassNameType>()) {
76 T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
77 }
78 auto *TST = T->getAs<TemplateSpecializationType>();
79 if (!TST)
80 return {};
81 const ClassTemplateDecl *TD = dyn_cast_or_null<ClassTemplateDecl>(
82 TST->getTemplateName().getAsTemplateDecl());
83 if (!TD)
84 return {};
85 CXXRecordDecl *RD = TD->getTemplatedDecl();
86 if (!RD->hasDefinition())
87 return {};
88 RD = RD->getDefinition();
Nathan Ridge16f47cf2019-12-15 20:42:25 -050089 DeclarationName Name = NameFactory(RD->getASTContext());
Nathan Ridgeecaa9362019-12-05 18:29:32 -050090 return RD->lookupDependentName(Name, [=](const NamedDecl *D) {
91 return IsNonstaticMember ? D->isCXXInstanceMember()
92 : !D->isCXXInstanceMember();
93 });
94}
95
Nathan Ridge16f47cf2019-12-15 20:42:25 -050096// Given the type T of a dependent expression that appears of the LHS of a "->",
97// heuristically find a corresponding pointee type in whose scope we could look
98// up the name appearing on the RHS.
99const Type *getPointeeType(const Type *T) {
100 if (!T)
101 return nullptr;
102
103 if (T->isPointerType()) {
104 return T->getAs<PointerType>()->getPointeeType().getTypePtrOrNull();
105 }
106
107 // Try to handle smart pointer types.
108
109 // Look up operator-> in the primary template. If we find one, it's probably a
110 // smart pointer type.
111 auto ArrowOps = getMembersReferencedViaDependentName(
112 T,
113 [](ASTContext &Ctx) {
114 return Ctx.DeclarationNames.getCXXOperatorName(OO_Arrow);
115 },
116 /*IsNonStaticMember=*/true);
117 if (ArrowOps.empty())
118 return nullptr;
119
120 // Getting the return type of the found operator-> method decl isn't useful,
121 // because we discarded template arguments to perform lookup in the primary
122 // template scope, so the return type would just have the form U* where U is a
123 // template parameter type.
124 // Instead, just handle the common case where the smart pointer type has the
125 // form of SmartPtr<X, ...>, and assume X is the pointee type.
126 auto *TST = T->getAs<TemplateSpecializationType>();
127 if (!TST)
128 return nullptr;
129 if (TST->getNumArgs() == 0)
130 return nullptr;
131 const TemplateArgument &FirstArg = TST->getArg(0);
132 if (FirstArg.getKind() != TemplateArgument::Type)
133 return nullptr;
134 return FirstArg.getAsType().getTypePtrOrNull();
135}
136
Sam McCall489cc582019-09-03 11:35:50 +0000137// TargetFinder locates the entities that an AST node refers to.
138//
139// Typically this is (possibly) one declaration and (possibly) one type, but
140// may be more:
141// - for ambiguous nodes like OverloadExpr
142// - if we want to include e.g. both typedefs and the underlying type
143//
144// This is organized as a set of mutually recursive helpers for particular node
145// types, but for most nodes this is a short walk rather than a deep traversal.
146//
147// It's tempting to do e.g. typedef resolution as a second normalization step,
148// after finding the 'primary' decl etc. But we do this monolithically instead
149// because:
150// - normalization may require these traversals again (e.g. unwrapping a
151// typedef reveals a decltype which must be traversed)
152// - it doesn't simplify that much, e.g. the first stage must still be able
153// to yield multiple decls to handle OverloadExpr
154// - there are cases where it's required for correctness. e.g:
155// template<class X> using pvec = vector<x*>; pvec<int> x;
156// There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>`
157// and both are lossy. We must know upfront what the caller ultimately wants.
158//
159// FIXME: improve common dependent scope using name lookup in primary templates.
160// e.g. template<typename T> int foo() { return std::vector<T>().size(); }
161// formally size() is unresolved, but the primary template is a good guess.
162// This affects:
163// - DependentTemplateSpecializationType,
Sam McCall489cc582019-09-03 11:35:50 +0000164// - DependentNameType
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500165// - UnresolvedUsingValueDecl
166// - UnresolvedUsingTypenameDecl
Sam McCall489cc582019-09-03 11:35:50 +0000167struct TargetFinder {
168 using RelSet = DeclRelationSet;
169 using Rel = DeclRelation;
Sam McCallf06f4392020-01-03 17:26:33 +0100170 llvm::SmallDenseMap<const NamedDecl *, RelSet> Decls;
Sam McCall489cc582019-09-03 11:35:50 +0000171 RelSet Flags;
172
Sam McCallf06f4392020-01-03 17:26:33 +0100173 static const NamedDecl *getTemplatePattern(const NamedDecl *D) {
Sam McCall489cc582019-09-03 11:35:50 +0000174 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
175 return CRD->getTemplateInstantiationPattern();
176 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
177 return FD->getTemplateInstantiationPattern();
178 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
179 // Hmm: getTIP returns its arg if it's not an instantiation?!
180 VarDecl *T = VD->getTemplateInstantiationPattern();
181 return (T == D) ? nullptr : T;
182 } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
183 return ED->getInstantiatedFromMemberEnum();
184 } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
Sam McCallf06f4392020-01-03 17:26:33 +0100185 if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext()))
186 if (const DeclContext *ParentPat =
187 dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
188 for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName()))
189 if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind())
190 return BaseND;
Sam McCall489cc582019-09-03 11:35:50 +0000191 } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
192 if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
193 if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
194 for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
195 return BaseECD;
196 }
197 }
198 }
199 return nullptr;
200 }
201
202 template <typename T> void debug(T &Node, RelSet Flags) {
203 dlog("visit [{0}] {1}", Flags,
204 nodeToString(ast_type_traits::DynTypedNode::create(Node)));
205 }
206
Sam McCallf06f4392020-01-03 17:26:33 +0100207 void report(const NamedDecl *D, RelSet Flags) {
Sam McCall489cc582019-09-03 11:35:50 +0000208 dlog("--> [{0}] {1}", Flags,
209 nodeToString(ast_type_traits::DynTypedNode::create(*D)));
210 Decls[D] |= Flags;
211 }
212
213public:
Sam McCallf06f4392020-01-03 17:26:33 +0100214 void add(const Decl *Dcl, RelSet Flags) {
215 const NamedDecl *D = llvm::dyn_cast<NamedDecl>(Dcl);
Sam McCall489cc582019-09-03 11:35:50 +0000216 if (!D)
217 return;
218 debug(*D, Flags);
219 if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
220 D = UDD->getNominatedNamespaceAsWritten();
221
222 if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
223 add(TND->getUnderlyingType(), Flags | Rel::Underlying);
224 Flags |= Rel::Alias; // continue with the alias.
225 } else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
226 for (const UsingShadowDecl *S : UD->shadows())
227 add(S->getUnderlyingDecl(), Flags | Rel::Underlying);
228 Flags |= Rel::Alias; // continue with the alias.
229 } else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
230 add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
231 Flags |= Rel::Alias; // continue with the alias
232 } else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
233 // Include the using decl, but don't traverse it. This may end up
234 // including *all* shadows, which we don't want.
235 report(USD->getUsingDecl(), Flags | Rel::Alias);
236 // Shadow decls are synthetic and not themselves interesting.
237 // Record the underlying decl instead, if allowed.
238 D = USD->getTargetDecl();
239 Flags |= Rel::Underlying; // continue with the underlying decl.
240 }
241
242 if (const Decl *Pat = getTemplatePattern(D)) {
243 assert(Pat != D);
244 add(Pat, Flags | Rel::TemplatePattern);
245 // Now continue with the instantiation.
246 Flags |= Rel::TemplateInstantiation;
247 }
248
249 report(D, Flags);
250 }
251
252 void add(const Stmt *S, RelSet Flags) {
253 if (!S)
254 return;
255 debug(*S, Flags);
256 struct Visitor : public ConstStmtVisitor<Visitor> {
257 TargetFinder &Outer;
258 RelSet Flags;
259 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
260
Haojian Wu939544a2019-11-27 16:22:16 +0100261 void VisitCallExpr(const CallExpr *CE) {
262 Outer.add(CE->getCalleeDecl(), Flags);
263 }
Sam McCall489cc582019-09-03 11:35:50 +0000264 void VisitDeclRefExpr(const DeclRefExpr *DRE) {
265 const Decl *D = DRE->getDecl();
266 // UsingShadowDecl allows us to record the UsingDecl.
267 // getFoundDecl() returns the wrong thing in other cases (templates).
268 if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
269 D = USD;
270 Outer.add(D, Flags);
271 }
272 void VisitMemberExpr(const MemberExpr *ME) {
273 const Decl *D = ME->getMemberDecl();
274 if (auto *USD =
275 llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
276 D = USD;
277 Outer.add(D, Flags);
278 }
Ilya Biryukova160a0b2019-10-01 07:27:55 +0000279 void VisitOverloadExpr(const OverloadExpr *OE) {
280 for (auto *D : OE->decls())
281 Outer.add(D, Flags);
282 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500283 void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
284 Outer.add(SE->getPack(), Flags);
285 }
Sam McCall489cc582019-09-03 11:35:50 +0000286 void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
287 Outer.add(CCE->getConstructor(), Flags);
288 }
289 void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
290 for (const DesignatedInitExpr::Designator &D :
291 llvm::reverse(DIE->designators()))
292 if (D.isFieldDesignator()) {
293 Outer.add(D.getField(), Flags);
294 // We don't know which designator was intended, we assume the outer.
295 break;
296 }
297 }
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500298 void
299 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
300 const Type *BaseType = E->getBaseType().getTypePtrOrNull();
301 if (E->isArrow()) {
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500302 BaseType = getPointeeType(BaseType);
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500303 }
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500304 for (const NamedDecl *D : getMembersReferencedViaDependentName(
305 BaseType, [E](ASTContext &) { return E->getMember(); },
306 /*IsNonstaticMember=*/true)) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500307 Outer.add(D, Flags);
308 }
309 }
310 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
311 for (const NamedDecl *D : getMembersReferencedViaDependentName(
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500312 E->getQualifier()->getAsType(),
313 [E](ASTContext &) { return E->getDeclName(); },
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500314 /*IsNonstaticMember=*/false)) {
315 Outer.add(D, Flags);
316 }
317 }
Sam McCall489cc582019-09-03 11:35:50 +0000318 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
319 Outer.add(OIRE->getDecl(), Flags);
320 }
321 void VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
322 Outer.add(OME->getMethodDecl(), Flags);
323 }
324 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
325 if (OPRE->isExplicitProperty())
326 Outer.add(OPRE->getExplicitProperty(), Flags);
327 else {
328 if (OPRE->isMessagingGetter())
329 Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
330 if (OPRE->isMessagingSetter())
331 Outer.add(OPRE->getImplicitPropertySetter(), Flags);
332 }
333 }
334 void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
335 Outer.add(OPE->getProtocol(), Flags);
336 }
337 };
338 Visitor(*this, Flags).Visit(S);
339 }
340
341 void add(QualType T, RelSet Flags) {
342 if (T.isNull())
343 return;
344 debug(T, Flags);
345 struct Visitor : public TypeVisitor<Visitor> {
346 TargetFinder &Outer;
347 RelSet Flags;
348 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
349
350 void VisitTagType(const TagType *TT) {
351 Outer.add(TT->getAsTagDecl(), Flags);
352 }
353 void VisitDecltypeType(const DecltypeType *DTT) {
354 Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
355 }
356 void VisitDeducedType(const DeducedType *DT) {
357 // FIXME: In practice this doesn't work: the AutoType you find inside
358 // TypeLoc never has a deduced type. https://llvm.org/PR42914
359 Outer.add(DT->getDeducedType(), Flags | Rel::Underlying);
360 }
361 void VisitTypedefType(const TypedefType *TT) {
362 Outer.add(TT->getDecl(), Flags);
363 }
364 void
365 VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
366 // Have to handle these case-by-case.
367
368 // templated type aliases: there's no specialized/instantiated using
369 // decl to point to. So try to find a decl for the underlying type
370 // (after substitution), and failing that point to the (templated) using
371 // decl.
372 if (TST->isTypeAlias()) {
373 Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
374 // Don't *traverse* the alias, which would result in traversing the
375 // template of the underlying type.
376 Outer.report(
377 TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
378 Flags | Rel::Alias | Rel::TemplatePattern);
379 }
380 // specializations of template template parameters aren't instantiated
381 // into decls, so they must refer to the parameter itself.
382 else if (const auto *Parm =
383 llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
384 TST->getTemplateName().getAsTemplateDecl()))
385 Outer.add(Parm, Flags);
386 // class template specializations have a (specialized) CXXRecordDecl.
387 else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
388 Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
389 else {
390 // fallback: the (un-specialized) declaration from primary template.
391 if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
392 Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
393 }
394 }
395 void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
396 Outer.add(TTPT->getDecl(), Flags);
397 }
398 void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
399 Outer.add(OIT->getDecl(), Flags);
400 }
401 void VisitObjCObjectType(const ObjCObjectType *OOT) {
402 // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so
403 // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo.
404 if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1)
405 Outer.add(OOT->getProtocol(0), Flags);
406 }
407 };
408 Visitor(*this, Flags).Visit(T.getTypePtr());
409 }
410
411 void add(const NestedNameSpecifier *NNS, RelSet Flags) {
412 if (!NNS)
413 return;
414 debug(*NNS, Flags);
415 switch (NNS->getKind()) {
416 case NestedNameSpecifier::Identifier:
417 return;
418 case NestedNameSpecifier::Namespace:
419 add(NNS->getAsNamespace(), Flags);
420 return;
421 case NestedNameSpecifier::NamespaceAlias:
422 add(NNS->getAsNamespaceAlias(), Flags);
423 return;
424 case NestedNameSpecifier::TypeSpec:
425 case NestedNameSpecifier::TypeSpecWithTemplate:
426 add(QualType(NNS->getAsType(), 0), Flags);
427 return;
428 case NestedNameSpecifier::Global:
429 // This should be TUDecl, but we can't get a pointer to it!
430 return;
431 case NestedNameSpecifier::Super:
432 add(NNS->getAsRecordDecl(), Flags);
433 return;
434 }
435 llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
436 }
437
438 void add(const CXXCtorInitializer *CCI, RelSet Flags) {
439 if (!CCI)
440 return;
441 debug(*CCI, Flags);
442
443 if (CCI->isAnyMemberInitializer())
444 add(CCI->getAnyMember(), Flags);
445 // Constructor calls contain a TypeLoc node, so we don't handle them here.
446 }
447};
448
449} // namespace
450
Sam McCallf06f4392020-01-03 17:26:33 +0100451llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000452allTargetDecls(const ast_type_traits::DynTypedNode &N) {
453 dlog("allTargetDecls({0})", nodeToString(N));
454 TargetFinder Finder;
455 DeclRelationSet Flags;
456 if (const Decl *D = N.get<Decl>())
457 Finder.add(D, Flags);
458 else if (const Stmt *S = N.get<Stmt>())
459 Finder.add(S, Flags);
460 else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
461 Finder.add(NNSL->getNestedNameSpecifier(), Flags);
462 else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
463 Finder.add(NNS, Flags);
464 else if (const TypeLoc *TL = N.get<TypeLoc>())
465 Finder.add(TL->getType(), Flags);
466 else if (const QualType *QT = N.get<QualType>())
467 Finder.add(*QT, Flags);
468 else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
469 Finder.add(CCI, Flags);
470
471 return {Finder.Decls.begin(), Finder.Decls.end()};
472}
473
Sam McCallf06f4392020-01-03 17:26:33 +0100474llvm::SmallVector<const NamedDecl *, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000475targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) {
Sam McCallf06f4392020-01-03 17:26:33 +0100476 llvm::SmallVector<const NamedDecl *, 1> Result;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000477 for (const auto &Entry : allTargetDecls(N)) {
Sam McCall489cc582019-09-03 11:35:50 +0000478 if (!(Entry.second & ~Mask))
479 Result.push_back(Entry.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000480 }
Sam McCall489cc582019-09-03 11:35:50 +0000481 return Result;
482}
483
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000484llvm::SmallVector<const NamedDecl *, 1>
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100485explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000486 assert(!(Mask & (DeclRelation::TemplatePattern |
487 DeclRelation::TemplateInstantiation)) &&
488 "explicitRefenceTargets handles templates on its own");
489 auto Decls = allTargetDecls(N);
490
491 // We prefer to return template instantiation, but fallback to template
492 // pattern if instantiation is not available.
493 Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation;
494
495 llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
496 llvm::SmallVector<const NamedDecl *, 1> Targets;
497 bool SeenTemplateInstantiations = false;
498 for (auto &D : Decls) {
499 if (D.second & ~Mask)
500 continue;
501 if (D.second & DeclRelation::TemplatePattern) {
Sam McCallf06f4392020-01-03 17:26:33 +0100502 TemplatePatterns.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000503 continue;
504 }
505 if (D.second & DeclRelation::TemplateInstantiation)
506 SeenTemplateInstantiations = true;
Sam McCallf06f4392020-01-03 17:26:33 +0100507 Targets.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000508 }
509 if (!SeenTemplateInstantiations)
510 Targets.insert(Targets.end(), TemplatePatterns.begin(),
511 TemplatePatterns.end());
512 return Targets;
513}
514
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100515namespace {
Haojian Wu65f61c02019-10-18 12:07:19 +0000516llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000517 struct Visitor : ConstDeclVisitor<Visitor> {
Haojian Wu65f61c02019-10-18 12:07:19 +0000518 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000519
520 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000521 // We want to keep it as non-declaration references, as the
522 // "using namespace" declaration doesn't have a name.
523 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
524 D->getIdentLocation(),
525 /*IsDecl=*/false,
526 {D->getNominatedNamespaceAsWritten()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000527 }
528
529 void VisitUsingDecl(const UsingDecl *D) {
Kazuaki Ishizakib7ecf1c2020-01-04 10:28:41 -0500530 // "using ns::identifier;" is a non-declaration reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000531 Refs.push_back(
532 ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
533 explicitReferenceTargets(DynTypedNode::create(*D),
534 DeclRelation::Underlying)});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000535 }
536
537 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000538 // For namespace alias, "namespace Foo = Target;", we add two references.
539 // Add a declaration reference for Foo.
540 VisitNamedDecl(D);
541 // Add a non-declaration reference for Target.
542 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
543 D->getTargetNameLoc(),
544 /*IsDecl=*/false,
545 {D->getAliasedNamespace()}});
546 }
547
548 void VisitNamedDecl(const NamedDecl *ND) {
549 // FIXME: decide on how to surface destructors when we need them.
550 if (llvm::isa<CXXDestructorDecl>(ND))
551 return;
Ilya Biryukov4c430a72019-10-28 14:41:06 +0100552 // Filter anonymous decls, name location will point outside the name token
553 // and the clients are not prepared to handle that.
554 if (ND->getDeclName().isIdentifier() &&
555 !ND->getDeclName().getAsIdentifierInfo())
556 return;
557 Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
558 ND->getLocation(),
559 /*IsDecl=*/true,
560 {ND}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000561 }
562 };
563
564 Visitor V;
565 V.Visit(D);
Haojian Wu65f61c02019-10-18 12:07:19 +0000566 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000567}
568
Haojian Wu65f61c02019-10-18 12:07:19 +0000569llvm::SmallVector<ReferenceLoc, 2> refInExpr(const Expr *E) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000570 struct Visitor : ConstStmtVisitor<Visitor> {
571 // FIXME: handle more complicated cases, e.g. ObjC, designated initializers.
Haojian Wu65f61c02019-10-18 12:07:19 +0000572 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000573
574 void VisitDeclRefExpr(const DeclRefExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000575 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
576 E->getNameInfo().getLoc(),
577 /*IsDecl=*/false,
578 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000579 }
580
581 void VisitMemberExpr(const MemberExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000582 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
583 E->getMemberNameInfo().getLoc(),
584 /*IsDecl=*/false,
585 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000586 }
Ilya Biryukov27744572019-09-27 09:39:10 +0000587
588 void VisitOverloadExpr(const OverloadExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000589 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
590 E->getNameInfo().getLoc(),
591 /*IsDecl=*/false,
592 llvm::SmallVector<const NamedDecl *, 1>(
593 E->decls().begin(), E->decls().end())});
Ilya Biryukov27744572019-09-27 09:39:10 +0000594 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500595
596 void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
597 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
598 E->getPackLoc(),
599 /*IsDecl=*/false,
600 {E->getPack()}});
601 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000602 };
603
604 Visitor V;
605 V.Visit(E);
Haojian Wu65f61c02019-10-18 12:07:19 +0000606 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000607}
608
Haojian Wu65f61c02019-10-18 12:07:19 +0000609llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000610 struct Visitor : TypeLocVisitor<Visitor> {
611 llvm::Optional<ReferenceLoc> Ref;
612
613 void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
614 // We only know about qualifier, rest if filled by inner locations.
615 Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
616 // Fill in the qualifier.
617 if (!Ref)
618 return;
619 assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
620 Ref->Qualifier = L.getQualifierLoc();
621 }
622
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000623 void VisitTagTypeLoc(TagTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000624 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
625 L.getNameLoc(),
626 /*IsDecl=*/false,
627 {L.getDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000628 }
629
Ilya Biryukovc3835092019-09-27 10:55:53 +0000630 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000631 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
632 L.getNameLoc(),
633 /*IsDecl=*/false,
634 {L.getDecl()}};
Ilya Biryukovc3835092019-09-27 10:55:53 +0000635 }
636
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000637 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000638 // We must ensure template type aliases are included in results if they
639 // were written in the source code, e.g. in
640 // template <class T> using valias = vector<T>;
641 // ^valias<int> x;
642 // 'explicitReferenceTargets' will return:
643 // 1. valias with mask 'Alias'.
644 // 2. 'vector<int>' with mask 'Underlying'.
645 // we want to return only #1 in this case.
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000646 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000647 NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000648 explicitReferenceTargets(DynTypedNode::create(L.getType()),
649 DeclRelation::Alias)};
650 }
651 void VisitDeducedTemplateSpecializationTypeLoc(
652 DeducedTemplateSpecializationTypeLoc L) {
653 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000654 NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000655 explicitReferenceTargets(DynTypedNode::create(L.getType()),
656 DeclRelation::Alias)};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000657 }
658
659 void VisitDependentTemplateSpecializationTypeLoc(
660 DependentTemplateSpecializationTypeLoc L) {
661 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000662 L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100663 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000664 }
665
666 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
667 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000668 L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100669 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000670 }
671
672 void VisitTypedefTypeLoc(TypedefTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000673 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
674 L.getNameLoc(),
675 /*IsDecl=*/false,
676 {L.getTypedefNameDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000677 }
678 };
679
680 Visitor V;
681 V.Visit(L.getUnqualifiedLoc());
Haojian Wu65f61c02019-10-18 12:07:19 +0000682 if (!V.Ref)
683 return {};
684 return {*V.Ref};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000685}
686
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100687class ExplicitReferenceCollector
688 : public RecursiveASTVisitor<ExplicitReferenceCollector> {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000689public:
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100690 ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out)
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000691 : Out(Out) {
692 assert(Out);
693 }
694
695 bool VisitTypeLoc(TypeLoc TTL) {
696 if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
697 return true;
698 visitNode(DynTypedNode::create(TTL));
699 return true;
700 }
701
702 bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
703 // ElaboratedTypeLoc will reports information for its inner type loc.
704 // Otherwise we loose information about inner types loc's qualifier.
705 TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
706 TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
707 return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
708 }
709
710 bool VisitExpr(Expr *E) {
711 visitNode(DynTypedNode::create(*E));
712 return true;
713 }
714
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000715 // We re-define Traverse*, since there's no corresponding Visit*.
716 // TemplateArgumentLoc is the only way to get locations for references to
717 // template template parameters.
718 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
719 switch (A.getArgument().getKind()) {
720 case TemplateArgument::Template:
721 case TemplateArgument::TemplateExpansion:
722 reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
723 A.getTemplateNameLoc(),
Haojian Wu65f61c02019-10-18 12:07:19 +0000724 /*IsDecl=*/false,
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000725 {A.getArgument()
726 .getAsTemplateOrTemplatePattern()
727 .getAsTemplateDecl()}},
728 DynTypedNode::create(A.getArgument()));
729 break;
730 case TemplateArgument::Declaration:
731 break; // FIXME: can this actually happen in TemplateArgumentLoc?
732 case TemplateArgument::Integral:
733 case TemplateArgument::Null:
734 case TemplateArgument::NullPtr:
735 break; // no references.
736 case TemplateArgument::Pack:
737 case TemplateArgument::Type:
738 case TemplateArgument::Expression:
739 break; // Handled by VisitType and VisitExpression.
740 };
741 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
742 }
743
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000744 bool VisitDecl(Decl *D) {
745 visitNode(DynTypedNode::create(*D));
746 return true;
747 }
748
749 // We have to use Traverse* because there is no corresponding Visit*.
750 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
751 if (!L.getNestedNameSpecifier())
752 return true;
753 visitNode(DynTypedNode::create(L));
754 // Inner type is missing information about its qualifier, skip it.
755 if (auto TL = L.getTypeLoc())
756 TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
757 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
758 }
759
Haojian Wu13fc8992019-10-21 10:11:30 +0200760 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
761 visitNode(DynTypedNode::create(*Init));
762 return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
763 }
764
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000765private:
766 /// Obtain information about a reference directly defined in \p N. Does not
767 /// recurse into child nodes, e.g. do not expect references for constructor
768 /// initializers
769 ///
770 /// Any of the fields in the returned structure can be empty, but not all of
771 /// them, e.g.
772 /// - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
773 /// source location information may be missing,
774 /// - for dependent code, targets may be empty.
775 ///
776 /// (!) For the purposes of this function declarations are not considered to
777 /// be references. However, declarations can have references inside them,
778 /// e.g. 'namespace foo = std' references namespace 'std' and this
779 /// function will return the corresponding reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000780 llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000781 if (auto *D = N.get<Decl>())
782 return refInDecl(D);
783 if (auto *E = N.get<Expr>())
784 return refInExpr(E);
Ilya Biryukov733777a2019-10-31 11:58:57 +0100785 if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
786 // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
787 return {ReferenceLoc{
788 NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
789 explicitReferenceTargets(
790 DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
791 DeclRelation::Alias)}};
792 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000793 if (const TypeLoc *TL = N.get<TypeLoc>())
794 return refInTypeLoc(*TL);
795 if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
Haojian Wu13fc8992019-10-21 10:11:30 +0200796 // Other type initializers (e.g. base initializer) are handled by visiting
797 // the typeLoc.
798 if (CCI->isAnyMemberInitializer()) {
799 return {ReferenceLoc{NestedNameSpecifierLoc(),
800 CCI->getMemberLocation(),
801 /*IsDecl=*/false,
802 {CCI->getAnyMember()}}};
803 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000804 }
805 // We do not have location information for other nodes (QualType, etc)
Haojian Wu65f61c02019-10-18 12:07:19 +0000806 return {};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000807 }
808
809 void visitNode(DynTypedNode N) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000810 for (const auto &R : explicitReference(N))
811 reportReference(R, N);
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000812 }
813
814 void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000815 // Our promise is to return only references from the source code. If we lack
816 // location information, skip these nodes.
817 // Normally this should not happen in practice, unless there are bugs in the
818 // traversals or users started the traversal at an implicit node.
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000819 if (Ref.NameLoc.isInvalid()) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000820 dlog("invalid location at node {0}", nodeToString(N));
821 return;
822 }
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000823 Out(Ref);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000824 }
825
826 llvm::function_ref<void(ReferenceLoc)> Out;
827 /// TypeLocs starting at these locations must be skipped, see
828 /// TraverseElaboratedTypeSpecifierLoc for details.
829 llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
830};
831} // namespace
832
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000833void findExplicitReferences(const Stmt *S,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000834 llvm::function_ref<void(ReferenceLoc)> Out) {
835 assert(S);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100836 ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000837}
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000838void findExplicitReferences(const Decl *D,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000839 llvm::function_ref<void(ReferenceLoc)> Out) {
840 assert(D);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100841 ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000842}
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100843void findExplicitReferences(const ASTContext &AST,
844 llvm::function_ref<void(ReferenceLoc)> Out) {
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100845 ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST));
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100846}
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000847
Sam McCall489cc582019-09-03 11:35:50 +0000848llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
849 switch (R) {
850#define REL_CASE(X) \
851 case DeclRelation::X: \
852 return OS << #X;
853 REL_CASE(Alias);
854 REL_CASE(Underlying);
855 REL_CASE(TemplateInstantiation);
856 REL_CASE(TemplatePattern);
857#undef REL_CASE
Michael Liaode524032019-09-03 15:02:46 +0000858 }
Simon Pilgrim54b989c2019-09-03 13:05:13 +0000859 llvm_unreachable("Unhandled DeclRelation enum");
Sam McCall489cc582019-09-03 11:35:50 +0000860}
861llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
862 const char *Sep = "";
863 for (unsigned I = 0; I < RS.S.size(); ++I) {
864 if (RS.S.test(I)) {
865 OS << Sep << static_cast<DeclRelation>(I);
866 Sep = "|";
867 }
868 }
869 return OS;
870}
871
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000872llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
873 // note we cannot print R.NameLoc without a source manager.
874 OS << "targets = {";
875 bool First = true;
876 for (const NamedDecl *T : R.Targets) {
877 if (!First)
878 OS << ", ";
879 else
880 First = false;
881 OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T);
882 }
883 OS << "}";
884 if (R.Qualifier) {
885 OS << ", qualifier = '";
886 R.Qualifier.getNestedNameSpecifier()->print(OS,
887 PrintingPolicy(LangOptions()));
888 OS << "'";
889 }
Haojian Wu65f61c02019-10-18 12:07:19 +0000890 if (R.IsDecl)
891 OS << ", decl";
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000892 return OS;
893}
894
Sam McCall489cc582019-09-03 11:35:50 +0000895} // namespace clangd
896} // namespace clang