blob: a9aa25805be4de118956213fd27d7027ad3de6d6 [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"
Sam McCallad97ccf2020-04-28 17:49:17 +020011#include "support/Logger.h"
Sam McCall489cc582019-09-03 11:35:50 +000012#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 Ridge98d763a2020-07-08 02:43:38 -040061// Helper function for getMembersReferencedViaDependentName()
Nathan Ridge9946dcd2020-07-08 02:51:34 -040062// which takes a possibly-dependent type `T` and heuristically
Nathan Ridge98d763a2020-07-08 02:43:38 -040063// resolves it to a CXXRecordDecl in which we can try name lookup.
64CXXRecordDecl *resolveTypeToRecordDecl(const Type *T) {
65 assert(T);
Nathan Ridge9946dcd2020-07-08 02:51:34 -040066
67 if (const auto *RT = T->getAs<RecordType>())
68 return dyn_cast<CXXRecordDecl>(RT->getDecl());
69
70 if (const auto *ICNT = T->getAs<InjectedClassNameType>())
Nathan Ridge98d763a2020-07-08 02:43:38 -040071 T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
Nathan Ridge9946dcd2020-07-08 02:51:34 -040072 if (!T)
73 return nullptr;
74
Nathan Ridge98d763a2020-07-08 02:43:38 -040075 const auto *TST = T->getAs<TemplateSpecializationType>();
76 if (!TST)
77 return nullptr;
Nathan Ridge9946dcd2020-07-08 02:51:34 -040078
Nathan Ridge98d763a2020-07-08 02:43:38 -040079 const ClassTemplateDecl *TD = dyn_cast_or_null<ClassTemplateDecl>(
80 TST->getTemplateName().getAsTemplateDecl());
81 if (!TD)
82 return nullptr;
Nathan Ridge9946dcd2020-07-08 02:51:34 -040083
Nathan Ridge98d763a2020-07-08 02:43:38 -040084 return TD->getTemplatedDecl();
85}
86
Nathan Ridge9946dcd2020-07-08 02:51:34 -040087// Given a tag-decl type and a member name, heuristically resolve the
Nathan Ridgeecaa9362019-12-05 18:29:32 -050088// 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().
Nathan Ridge16f47cf2019-12-15 20:42:25 -050096// 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.
100std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
101 const Type *T,
102 llvm::function_ref<DeclarationName(ASTContext &)> NameFactory,
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400103 llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500104 if (!T)
105 return {};
Nathan Ridge31b7f0e2020-03-12 18:22:44 -0400106 if (auto *ET = T->getAs<EnumType>()) {
107 auto Result =
108 ET->getDecl()->lookup(NameFactory(ET->getDecl()->getASTContext()));
109 return {Result.begin(), Result.end()};
110 }
Nathan Ridge98d763a2020-07-08 02:43:38 -0400111 if (auto *RD = resolveTypeToRecordDecl(T)) {
112 if (!RD->hasDefinition())
113 return {};
114 RD = RD->getDefinition();
115 DeclarationName Name = NameFactory(RD->getASTContext());
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400116 return RD->lookupDependentName(Name, Filter);
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500117 }
Nathan Ridge98d763a2020-07-08 02:43:38 -0400118 return {};
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500119}
120
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400121const auto NonStaticFilter = [](const NamedDecl *D) {
122 return D->isCXXInstanceMember();
123};
124const auto StaticFilter = [](const NamedDecl *D) {
125 return !D->isCXXInstanceMember();
126};
127const auto ValueFilter = [](const NamedDecl *D) { return isa<ValueDecl>(D); };
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400128const auto TypeFilter = [](const NamedDecl *D) { return isa<TypeDecl>(D); };
129const auto TemplateFilter = [](const NamedDecl *D) {
130 return isa<TemplateDecl>(D);
131};
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400132
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.
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500136const 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 },
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400153 NonStaticFilter);
Nathan Ridge16f47cf2019-12-15 20:42:25 -0500154 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
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400174// Forward declaration, needed as this function is mutually recursive
Haojian Wucd4e8d72020-07-29 13:58:12 +0200175// with resolveExprToDecls.
176const Type *resolveExprToType(const Expr *E);
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400177
Haojian Wucd4e8d72020-07-29 13:58:12 +0200178// Try to heuristically resolve a possibly-dependent expression `E` to one
Nathan Ridge98d763a2020-07-08 02:43:38 -0400179// or more declarations that it likely references.
Haojian Wucd4e8d72020-07-29 13:58:12 +0200180std::vector<const NamedDecl *> resolveExprToDecls(const Expr *E) {
Nathan Ridge98d763a2020-07-08 02:43:38 -0400181 if (const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(E)) {
182 const Type *BaseType = ME->getBaseType().getTypePtrOrNull();
183 if (ME->isArrow()) {
184 BaseType = getPointeeType(BaseType);
185 }
Haojian Wu3ad01812020-07-21 12:11:52 +0200186 if (!BaseType)
187 return {};
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400188 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) {
Haojian Wucd4e8d72020-07-29 13:58:12 +0200194 BaseType = resolveExprToType(Base);
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400195 }
196 }
Nathan Ridge98d763a2020-07-08 02:43:38 -0400197 return getMembersReferencedViaDependentName(
198 BaseType, [ME](ASTContext &) { return ME->getMember(); },
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400199 NonStaticFilter);
Nathan Ridge98d763a2020-07-08 02:43:38 -0400200 }
201 if (const auto *RE = dyn_cast<DependentScopeDeclRefExpr>(E)) {
202 return getMembersReferencedViaDependentName(
203 RE->getQualifier()->getAsType(),
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400204 [RE](ASTContext &) { return RE->getDeclName(); }, StaticFilter);
Nathan Ridge98d763a2020-07-08 02:43:38 -0400205 }
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400206 if (const auto *CE = dyn_cast<CallExpr>(E)) {
Haojian Wucd4e8d72020-07-29 13:58:12 +0200207 const auto *CalleeType = resolveExprToType(CE->getCallee());
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400208 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 }
Haojian Wucd4e8d72020-07-29 13:58:12 +0200219 if (const auto *ME = dyn_cast<MemberExpr>(E))
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400220 return {ME->getMemberDecl()};
Haojian Wucd4e8d72020-07-29 13:58:12 +0200221 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
222 return {DRE->getFoundDecl()};
Nathan Ridge98d763a2020-07-08 02:43:38 -0400223 return {};
224}
225
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400226const Type *resolveDeclsToType(const std::vector<const NamedDecl *> &Decls) {
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400227 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();
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400231 }
232 if (const auto *VD = dyn_cast<ValueDecl>(Decls[0])) {
Nathan Ridge9946dcd2020-07-08 02:51:34 -0400233 return VD->getType().getTypePtrOrNull();
234 }
235 return nullptr;
236}
237
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400238// Try to heuristically resolve the type of a possibly-dependent expression `E`.
239const 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.
245const 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
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100265const NamedDecl *getTemplatePattern(const NamedDecl *D) {
266 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
Haojian Wue326f522020-02-20 15:22:07 +0100267 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();
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100275 } 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
Sam McCall489cc582019-09-03 11:35:50 +0000301// 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.
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400324// We currently handle several dependent constructs, but some others remain to
325// be handled:
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500326// - UnresolvedUsingTypenameDecl
Sam McCall489cc582019-09-03 11:35:50 +0000327struct TargetFinder {
328 using RelSet = DeclRelationSet;
329 using Rel = DeclRelation;
Sam McCall489cc582019-09-03 11:35:50 +0000330
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100331private:
332 llvm::SmallDenseMap<const NamedDecl *,
333 std::pair<RelSet, /*InsertionOrder*/ size_t>>
334 Decls;
335 RelSet Flags;
Sam McCall489cc582019-09-03 11:35:50 +0000336
337 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
Sam McCallf06f4392020-01-03 17:26:33 +0100342 void report(const NamedDecl *D, RelSet Flags) {
Sam McCall489cc582019-09-03 11:35:50 +0000343 dlog("--> [{0}] {1}", Flags,
344 nodeToString(ast_type_traits::DynTypedNode::create(*D)));
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100345 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;
Sam McCall489cc582019-09-03 11:35:50 +0000349 }
350
351public:
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100352 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
Sam McCallf06f4392020-01-03 17:26:33 +0100361 void add(const Decl *Dcl, RelSet Flags) {
Sam McCall9a5c4482020-01-29 02:01:42 +0100362 const NamedDecl *D = llvm::dyn_cast_or_null<NamedDecl>(Dcl);
Sam McCall489cc582019-09-03 11:35:50 +0000363 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)) {
Haojian Wuf24649b2020-10-07 10:01:04 +0200373 // no Underlying as this is a non-renaming alias.
Sam McCall489cc582019-09-03 11:35:50 +0000374 for (const UsingShadowDecl *S : UD->shadows())
Haojian Wuf24649b2020-10-07 10:01:04 +0200375 add(S->getUnderlyingDecl(), Flags);
Sam McCall489cc582019-09-03 11:35:50 +0000376 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
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400380 } 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)) {
Haojian Wuf24649b2020-10-07 10:01:04 +0200386 add(Target, Flags); // no Underlying as this is a non-renaming alias
Nathan Ridgee33ec9d2020-03-12 19:27:18 -0400387 }
388 Flags |= Rel::Alias; // continue with the alias
Sam McCall489cc582019-09-03 11:35:50 +0000389 } 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();
Nathan Ridge100dbd12020-07-19 02:26:49 -0400396 } else if (const auto *DG = dyn_cast<CXXDeductionGuideDecl>(D)) {
397 D = DG->getDeducedTemplate();
David Goldmancb29c332020-07-09 14:29:15 -0400398 } 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();
Sam McCall489cc582019-09-03 11:35:50 +0000410 }
David Goldmancb29c332020-07-09 14:29:15 -0400411 if (!D)
412 return;
Sam McCall489cc582019-09-03 11:35:50 +0000413
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
424 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
Haojian Wu939544a2019-11-27 16:22:16 +0100433 void VisitCallExpr(const CallExpr *CE) {
434 Outer.add(CE->getCalleeDecl(), Flags);
435 }
Nathan Ridgecbcd07a2020-01-21 16:55:43 -0500436 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
437 Outer.add(E->getNamedConcept(), Flags);
438 }
Sam McCall489cc582019-09-03 11:35:50 +0000439 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 }
Ilya Biryukova160a0b2019-10-01 07:27:55 +0000454 void VisitOverloadExpr(const OverloadExpr *OE) {
455 for (auto *D : OE->decls())
456 Outer.add(D, Flags);
457 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500458 void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
459 Outer.add(SE->getPack(), Flags);
460 }
Sam McCall489cc582019-09-03 11:35:50 +0000461 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 }
Haojian Wua533b032020-04-08 11:03:50 +0200473 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 }
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500481 void
482 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
Haojian Wucd4e8d72020-07-29 13:58:12 +0200483 for (const NamedDecl *D : resolveExprToDecls(E)) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500484 Outer.add(D, Flags);
485 }
486 }
487 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
Haojian Wucd4e8d72020-07-29 13:58:12 +0200488 for (const NamedDecl *D : resolveExprToDecls(E)) {
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500489 Outer.add(D, Flags);
490 }
491 }
Sam McCall489cc582019-09-03 11:35:50 +0000492 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 }
Sam McCall4c5a4512020-01-10 10:40:30 +0100511 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 }
Haojian Wu5191f702020-07-25 21:52:33 +0200517 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 }
Sam McCall489cc582019-09-03 11:35:50 +0000523 };
524 Visitor(*this, Flags).Visit(S);
525 }
526
527 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 }
Haojian Wu5d4e8992020-01-21 15:28:06 +0100539
Haojian Wueaf0c892020-02-05 09:56:13 +0100540 void VisitElaboratedType(const ElaboratedType *ET) {
541 Outer.add(ET->desugar(), Flags);
542 }
543
Haojian Wu5d4e8992020-01-21 15:28:06 +0100544 void VisitInjectedClassNameType(const InjectedClassNameType *ICNT) {
545 Outer.add(ICNT->getDecl(), Flags);
546 }
547
Sam McCall489cc582019-09-03 11:35:50 +0000548 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 }
Nathan Ridge6a69d3c2020-01-02 00:45:01 -0500556 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 }
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400567 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 }
Sam McCall489cc582019-09-03 11:35:50 +0000584 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
634 void add(const NestedNameSpecifier *NNS, RelSet Flags) {
635 if (!NNS)
636 return;
637 debug(*NNS, Flags);
638 switch (NNS->getKind()) {
Sam McCall489cc582019-09-03 11:35:50 +0000639 case NestedNameSpecifier::Namespace:
640 add(NNS->getAsNamespace(), Flags);
641 return;
642 case NestedNameSpecifier::NamespaceAlias:
643 add(NNS->getAsNamespaceAlias(), Flags);
644 return;
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400645 case NestedNameSpecifier::Identifier:
Sam McCall489cc582019-09-03 11:35:50 +0000646 case NestedNameSpecifier::TypeSpec:
647 case NestedNameSpecifier::TypeSpecWithTemplate:
Nathan Ridge1b962fd2020-09-29 03:19:59 -0400648 add(QualType(resolveNestedNameSpecifierToType(NNS), 0), Flags);
Sam McCall489cc582019-09-03 11:35:50 +0000649 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
660 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 }
Nathan Ridge70d583a2020-08-09 20:37:43 -0400669
670 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 }
Sam McCall489cc582019-09-03 11:35:50 +0000682};
683
684} // namespace
685
Sam McCallf06f4392020-01-03 17:26:33 +0100686llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000687allTargetDecls(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);
Nathan Ridge70d583a2020-08-09 20:37:43 -0400705 else if (const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>())
706 Finder.add(TAL->getArgument(), Flags);
Sam McCall489cc582019-09-03 11:35:50 +0000707
Kadir Cetinkayad54d71b2020-01-16 11:37:58 +0100708 return Finder.takeDecls();
Sam McCall489cc582019-09-03 11:35:50 +0000709}
710
Sam McCallf06f4392020-01-03 17:26:33 +0100711llvm::SmallVector<const NamedDecl *, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000712targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) {
Sam McCallf06f4392020-01-03 17:26:33 +0100713 llvm::SmallVector<const NamedDecl *, 1> Result;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000714 for (const auto &Entry : allTargetDecls(N)) {
Sam McCall489cc582019-09-03 11:35:50 +0000715 if (!(Entry.second & ~Mask))
716 Result.push_back(Entry.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000717 }
Sam McCall489cc582019-09-03 11:35:50 +0000718 return Result;
719}
720
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000721llvm::SmallVector<const NamedDecl *, 1>
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100722explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000723 assert(!(Mask & (DeclRelation::TemplatePattern |
724 DeclRelation::TemplateInstantiation)) &&
Kazuaki Ishizakidd5571d2020-04-05 15:28:11 +0900725 "explicitReferenceTargets handles templates on its own");
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000726 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) {
Sam McCallf06f4392020-01-03 17:26:33 +0100739 TemplatePatterns.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000740 continue;
741 }
742 if (D.second & DeclRelation::TemplateInstantiation)
743 SeenTemplateInstantiations = true;
Sam McCallf06f4392020-01-03 17:26:33 +0100744 Targets.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000745 }
746 if (!SeenTemplateInstantiations)
747 Targets.insert(Targets.end(), TemplatePatterns.begin(),
748 TemplatePatterns.end());
749 return Targets;
750}
751
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100752namespace {
Haojian Wu65f61c02019-10-18 12:07:19 +0000753llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000754 struct Visitor : ConstDeclVisitor<Visitor> {
Haojian Wu65f61c02019-10-18 12:07:19 +0000755 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000756
757 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000758 // 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()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000764 }
765
766 void VisitUsingDecl(const UsingDecl *D) {
Kazuaki Ishizakib7ecf1c2020-01-04 10:28:41 -0500767 // "using ns::identifier;" is a non-declaration reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000768 Refs.push_back(
769 ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
770 explicitReferenceTargets(DynTypedNode::create(*D),
771 DeclRelation::Underlying)});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000772 }
773
774 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000775 // 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) {
Kadir Cetinkaya9570f1e2020-01-21 15:15:06 +0100786 // 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;
Haojian Wu65f61c02019-10-18 12:07:19 +0000793 // FIXME: decide on how to surface destructors when we need them.
794 if (llvm::isa<CXXDestructorDecl>(ND))
795 return;
Ilya Biryukov4c430a72019-10-28 14:41:06 +0100796 // 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}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000805 }
Nathan Ridge100dbd12020-07-19 02:26:49 -0400806
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 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000815 };
816
817 Visitor V;
818 V.Visit(D);
Haojian Wu65f61c02019-10-18 12:07:19 +0000819 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000820}
821
Sam McCallb0d94962020-05-19 00:45:27 +0200822llvm::SmallVector<ReferenceLoc, 2> refInStmt(const Stmt *S) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000823 struct Visitor : ConstStmtVisitor<Visitor> {
Sam McCallf4871ea2020-01-10 16:27:12 +0100824 // FIXME: handle more complicated cases: more ObjC, designated initializers.
Haojian Wu65f61c02019-10-18 12:07:19 +0000825 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000826
Nathan Ridgec6c5dbc2020-01-21 13:21:08 -0500827 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
828 Refs.push_back(ReferenceLoc{E->getNestedNameSpecifierLoc(),
829 E->getConceptNameLoc(),
830 /*IsDecl=*/false,
831 {E->getNamedConcept()}});
832 }
Nathan Ridged83541d2020-03-19 15:42:10 -0400833
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000834 void VisitDeclRefExpr(const DeclRefExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000835 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
836 E->getNameInfo().getLoc(),
837 /*IsDecl=*/false,
838 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000839 }
840
Nathan Ridged83541d2020-03-19 15:42:10 -0400841 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
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000847 void VisitMemberExpr(const MemberExpr *E) {
Kirill Bobyrev38bdb942020-01-21 05:33:39 +0100848 // Skip destructor calls to avoid duplication: TypeLoc within will be
849 // visited separately.
850 if (llvm::dyn_cast<CXXDestructorDecl>(E->getFoundDecl().getDecl()))
851 return;
Haojian Wu65f61c02019-10-18 12:07:19 +0000852 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
853 E->getMemberNameInfo().getLoc(),
854 /*IsDecl=*/false,
855 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000856 }
Ilya Biryukov27744572019-09-27 09:39:10 +0000857
Nathan Ridged83541d2020-03-19 15:42:10 -0400858 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
Ilya Biryukov27744572019-09-27 09:39:10 +0000866 void VisitOverloadExpr(const OverloadExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000867 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())});
Ilya Biryukov27744572019-09-27 09:39:10 +0000872 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500873
874 void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
875 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
876 E->getPackLoc(),
877 /*IsDecl=*/false,
878 {E->getPack()}});
879 }
Sam McCallf4871ea2020-01-10 16:27:12 +0100880
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 }
Kirill Bobyrev9f6d8de2020-02-10 11:53:17 +0100888
889 void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
890 for (const DesignatedInitExpr::Designator &D : DIE->designators()) {
891 if (!D.isFieldDesignator())
892 continue;
Haojian Wu6a78c552020-04-15 00:16:10 +0200893
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)});
Kirill Bobyrev9f6d8de2020-02-10 11:53:17 +0100899 }
900 }
Sam McCallb0d94962020-05-19 00:45:27 +0200901
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 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000916 };
917
918 Visitor V;
Sam McCallb0d94962020-05-19 00:45:27 +0200919 V.Visit(S);
Haojian Wu65f61c02019-10-18 12:07:19 +0000920 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000921}
922
Haojian Wu65f61c02019-10-18 12:07:19 +0000923llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000924 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
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000937 void VisitTagTypeLoc(TagTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000938 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
939 L.getNameLoc(),
940 /*IsDecl=*/false,
941 {L.getDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000942 }
943
Ilya Biryukovc3835092019-09-27 10:55:53 +0000944 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000945 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
946 L.getNameLoc(),
947 /*IsDecl=*/false,
948 {L.getDecl()}};
Ilya Biryukovc3835092019-09-27 10:55:53 +0000949 }
950
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000951 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000952 // 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.
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000960 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000961 NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000962 explicitReferenceTargets(DynTypedNode::create(L.getType()),
963 DeclRelation::Alias)};
964 }
965 void VisitDeducedTemplateSpecializationTypeLoc(
966 DeducedTemplateSpecializationTypeLoc L) {
967 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000968 NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000969 explicitReferenceTargets(DynTypedNode::create(L.getType()),
970 DeclRelation::Alias)};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000971 }
972
Haojian Wuf651c402020-01-21 11:50:57 +0100973 void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
974 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
975 TL.getNameLoc(),
976 /*IsDecl=*/false,
977 {TL.getDecl()}};
978 }
979
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000980 void VisitDependentTemplateSpecializationTypeLoc(
981 DependentTemplateSpecializationTypeLoc L) {
982 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000983 L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100984 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000985 }
986
987 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
988 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000989 L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100990 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000991 }
992
993 void VisitTypedefTypeLoc(TypedefTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000994 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
995 L.getNameLoc(),
996 /*IsDecl=*/false,
997 {L.getTypedefNameDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000998 }
999 };
1000
1001 Visitor V;
1002 V.Visit(L.getUnqualifiedLoc());
Haojian Wu65f61c02019-10-18 12:07:19 +00001003 if (!V.Ref)
1004 return {};
1005 return {*V.Ref};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001006}
1007
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +01001008class ExplicitReferenceCollector
1009 : public RecursiveASTVisitor<ExplicitReferenceCollector> {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001010public:
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +01001011 ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out)
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001012 : Out(Out) {
1013 assert(Out);
1014 }
1015
1016 bool VisitTypeLoc(TypeLoc TTL) {
Mikhail Maltsev78194112020-10-20 16:21:38 +01001017 if (TypeLocsToSkip.count(TTL.getBeginLoc()))
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001018 return true;
1019 visitNode(DynTypedNode::create(TTL));
1020 return true;
1021 }
1022
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();
Mikhail Maltsev78194112020-10-20 16:21:38 +01001027 TypeLocsToSkip.insert(Inner.getBeginLoc());
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001028 return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
1029 }
1030
Sam McCallb0d94962020-05-19 00:45:27 +02001031 bool VisitStmt(Stmt *S) {
1032 visitNode(DynTypedNode::create(*S));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001033 return true;
1034 }
1035
Sam McCallf4871ea2020-01-10 16:27:12 +01001036 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
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
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001050 // 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.
1053 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
Haojian Wu7d1ee632020-04-22 13:33:02 +02001054 llvm::SmallVector<const NamedDecl *, 1> Targets;
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001055 switch (A.getArgument().getKind()) {
1056 case TemplateArgument::Template:
1057 case TemplateArgument::TemplateExpansion:
Haojian Wu7d1ee632020-04-22 13:33:02 +02001058 if (const auto *D = A.getArgument()
1059 .getAsTemplateOrTemplatePattern()
1060 .getAsTemplateDecl())
1061 Targets.push_back(D);
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001062 reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
1063 A.getTemplateNameLoc(),
Haojian Wu7d1ee632020-04-22 13:33:02 +02001064 /*IsDecl=*/false, Targets},
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001065 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
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001081 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*.
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())
Mikhail Maltsev78194112020-10-20 16:21:38 +01001093 TypeLocsToSkip.insert(TL.getBeginLoc());
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001094 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
1095 }
1096
Haojian Wu13fc8992019-10-21 10:11:30 +02001097 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
1098 visitNode(DynTypedNode::create(*Init));
1099 return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
1100 }
1101
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001102private:
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.
Haojian Wu65f61c02019-10-18 12:07:19 +00001117 llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001118 if (auto *D = N.get<Decl>())
1119 return refInDecl(D);
Sam McCallb0d94962020-05-19 00:45:27 +02001120 if (auto *S = N.get<Stmt>())
1121 return refInStmt(S);
Ilya Biryukov733777a2019-10-31 11:58:57 +01001122 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 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001130 if (const TypeLoc *TL = N.get<TypeLoc>())
1131 return refInTypeLoc(*TL);
1132 if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
Haojian Wu13fc8992019-10-21 10:11:30 +02001133 // 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 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001141 }
1142 // We do not have location information for other nodes (QualType, etc)
Haojian Wu65f61c02019-10-18 12:07:19 +00001143 return {};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001144 }
1145
1146 void visitNode(DynTypedNode N) {
Haojian Wu65f61c02019-10-18 12:07:19 +00001147 for (const auto &R : explicitReference(N))
1148 reportReference(R, N);
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001149 }
1150
1151 void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001152 // 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.
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001156 if (Ref.NameLoc.isInvalid()) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001157 dlog("invalid location at node {0}", nodeToString(N));
1158 return;
1159 }
Ilya Biryukov1d32da82019-10-01 10:02:23 +00001160 Out(Ref);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001161 }
1162
1163 llvm::function_ref<void(ReferenceLoc)> Out;
1164 /// TypeLocs starting at these locations must be skipped, see
1165 /// TraverseElaboratedTypeSpecifierLoc for details.
Mikhail Maltsev78194112020-10-20 16:21:38 +01001166 llvm::DenseSet<SourceLocation> TypeLocsToSkip;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001167};
1168} // namespace
1169
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +00001170void findExplicitReferences(const Stmt *S,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001171 llvm::function_ref<void(ReferenceLoc)> Out) {
1172 assert(S);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +01001173 ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001174}
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +00001175void findExplicitReferences(const Decl *D,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001176 llvm::function_ref<void(ReferenceLoc)> Out) {
1177 assert(D);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +01001178 ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001179}
Ilya Biryukov87e0cb42019-11-05 19:06:12 +01001180void findExplicitReferences(const ASTContext &AST,
1181 llvm::function_ref<void(ReferenceLoc)> Out) {
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +01001182 ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST));
Ilya Biryukov87e0cb42019-11-05 19:06:12 +01001183}
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001184
Sam McCall489cc582019-09-03 11:35:50 +00001185llvm::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
Michael Liaode524032019-09-03 15:02:46 +00001195 }
Simon Pilgrim54b989c2019-09-03 13:05:13 +00001196 llvm_unreachable("Unhandled DeclRelation enum");
Sam McCall489cc582019-09-03 11:35:50 +00001197}
1198llvm::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
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001209llvm::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 }
Haojian Wu65f61c02019-10-18 12:07:19 +00001227 if (R.IsDecl)
1228 OS << ", decl";
Ilya Biryukovf96d2e12019-09-25 12:40:22 +00001229 return OS;
1230}
1231
Sam McCall489cc582019-09-03 11:35:50 +00001232} // namespace clangd
1233} // namespace clang