clang-tools  10.0.0git
FindTarget.cpp
Go to the documentation of this file.
1 //===--- FindTarget.cpp - What does an AST node refer to? -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FindTarget.h"
10 #include "AST.h"
11 #include "Logger.h"
12 #include "clang/AST/ASTTypeTraits.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclTemplate.h"
16 #include "clang/AST/DeclVisitor.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/ExprObjC.h"
21 #include "clang/AST/NestedNameSpecifier.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "clang/AST/RecursiveASTVisitor.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TemplateBase.h"
26 #include "clang/AST/Type.h"
27 #include "clang/AST/TypeLoc.h"
28 #include "clang/AST/TypeLocVisitor.h"
29 #include "clang/Basic/LangOptions.h"
30 #include "clang/Basic/OperatorKinds.h"
31 #include "clang/Basic/SourceLocation.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <utility>
38 
39 namespace clang {
40 namespace clangd {
41 namespace {
42 using ast_type_traits::DynTypedNode;
43 
44 LLVM_ATTRIBUTE_UNUSED std::string
45 nodeToString(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 
56 // 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().
65 // 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.
69 std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
70  const Type *T,
71  llvm::function_ref<DeclarationName(ASTContext &)> NameFactory,
72  bool IsNonstaticMember) {
73  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();
89  DeclarationName Name = NameFactory(RD->getASTContext());
90  return RD->lookupDependentName(Name, [=](const NamedDecl *D) {
91  return IsNonstaticMember ? D->isCXXInstanceMember()
92  : !D->isCXXInstanceMember();
93  });
94 }
95 
96 // 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.
99 const 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 
137 // 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,
164 // - DependentNameType
165 // - UnresolvedUsingValueDecl
166 // - UnresolvedUsingTypenameDecl
167 struct TargetFinder {
168  using RelSet = DeclRelationSet;
169  using Rel = DeclRelation;
170  llvm::SmallDenseMap<const NamedDecl *, RelSet> Decls;
171  RelSet Flags;
172 
173  static const NamedDecl *getTemplatePattern(const NamedDecl *D) {
174  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)) {
185  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;
191  } 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 
207  void report(const NamedDecl *D, RelSet Flags) {
208  dlog("--> [{0}] {1}", Flags,
209  nodeToString(ast_type_traits::DynTypedNode::create(*D)));
210  Decls[D] |= Flags;
211  }
212 
213 public:
214  void add(const Decl *Dcl, RelSet Flags) {
215  const NamedDecl *D = llvm::dyn_cast<NamedDecl>(Dcl);
216  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.
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 
261  void VisitCallExpr(const CallExpr *CE) {
262  Outer.add(CE->getCalleeDecl(), Flags);
263  }
264  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  }
279  void VisitOverloadExpr(const OverloadExpr *OE) {
280  for (auto *D : OE->decls())
281  Outer.add(D, Flags);
282  }
283  void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
284  Outer.add(SE->getPack(), Flags);
285  }
286  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  }
298  void
299  VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
300  const Type *BaseType = E->getBaseType().getTypePtrOrNull();
301  if (E->isArrow()) {
302  BaseType = getPointeeType(BaseType);
303  }
304  for (const NamedDecl *D : getMembersReferencedViaDependentName(
305  BaseType, [E](ASTContext &) { return E->getMember(); },
306  /*IsNonstaticMember=*/true)) {
307  Outer.add(D, Flags);
308  }
309  }
310  void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
311  for (const NamedDecl *D : getMembersReferencedViaDependentName(
312  E->getQualifier()->getAsType(),
313  [E](ASTContext &) { return E->getDeclName(); },
314  /*IsNonstaticMember=*/false)) {
315  Outer.add(D, Flags);
316  }
317  }
318  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  void VisitOpaqueValueExpr(const OpaqueValueExpr *OVE) {
338  Outer.add(OVE->getSourceExpr(), Flags);
339  }
340  void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
341  Outer.add(POE->getSyntacticForm(), Flags);
342  }
343  };
344  Visitor(*this, Flags).Visit(S);
345  }
346 
347  void add(QualType T, RelSet Flags) {
348  if (T.isNull())
349  return;
350  debug(T, Flags);
351  struct Visitor : public TypeVisitor<Visitor> {
352  TargetFinder &Outer;
353  RelSet Flags;
354  Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
355 
356  void VisitTagType(const TagType *TT) {
357  Outer.add(TT->getAsTagDecl(), Flags);
358  }
359  void VisitDecltypeType(const DecltypeType *DTT) {
360  Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
361  }
362  void VisitDeducedType(const DeducedType *DT) {
363  // FIXME: In practice this doesn't work: the AutoType you find inside
364  // TypeLoc never has a deduced type. https://llvm.org/PR42914
365  Outer.add(DT->getDeducedType(), Flags | Rel::Underlying);
366  }
367  void VisitDeducedTemplateSpecializationType(
368  const DeducedTemplateSpecializationType *DTST) {
369  // FIXME: This is a workaround for https://llvm.org/PR42914,
370  // which is causing DTST->getDeducedType() to be empty. We
371  // fall back to the template pattern and miss the instantiation
372  // even when it's known in principle. Once that bug is fixed,
373  // this method can be removed (the existing handling in
374  // VisitDeducedType() is sufficient).
375  if (auto *TD = DTST->getTemplateName().getAsTemplateDecl())
376  Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
377  }
378  void VisitTypedefType(const TypedefType *TT) {
379  Outer.add(TT->getDecl(), Flags);
380  }
381  void
382  VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
383  // Have to handle these case-by-case.
384 
385  // templated type aliases: there's no specialized/instantiated using
386  // decl to point to. So try to find a decl for the underlying type
387  // (after substitution), and failing that point to the (templated) using
388  // decl.
389  if (TST->isTypeAlias()) {
390  Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
391  // Don't *traverse* the alias, which would result in traversing the
392  // template of the underlying type.
393  Outer.report(
394  TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
395  Flags | Rel::Alias | Rel::TemplatePattern);
396  }
397  // specializations of template template parameters aren't instantiated
398  // into decls, so they must refer to the parameter itself.
399  else if (const auto *Parm =
400  llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
401  TST->getTemplateName().getAsTemplateDecl()))
402  Outer.add(Parm, Flags);
403  // class template specializations have a (specialized) CXXRecordDecl.
404  else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
405  Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
406  else {
407  // fallback: the (un-specialized) declaration from primary template.
408  if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
409  Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
410  }
411  }
412  void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
413  Outer.add(TTPT->getDecl(), Flags);
414  }
415  void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
416  Outer.add(OIT->getDecl(), Flags);
417  }
418  void VisitObjCObjectType(const ObjCObjectType *OOT) {
419  // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so
420  // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo.
421  if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1)
422  Outer.add(OOT->getProtocol(0), Flags);
423  }
424  };
425  Visitor(*this, Flags).Visit(T.getTypePtr());
426  }
427 
428  void add(const NestedNameSpecifier *NNS, RelSet Flags) {
429  if (!NNS)
430  return;
431  debug(*NNS, Flags);
432  switch (NNS->getKind()) {
434  return;
436  add(NNS->getAsNamespace(), Flags);
437  return;
438  case NestedNameSpecifier::NamespaceAlias:
439  add(NNS->getAsNamespaceAlias(), Flags);
440  return;
441  case NestedNameSpecifier::TypeSpec:
442  case NestedNameSpecifier::TypeSpecWithTemplate:
443  add(QualType(NNS->getAsType(), 0), Flags);
444  return;
445  case NestedNameSpecifier::Global:
446  // This should be TUDecl, but we can't get a pointer to it!
447  return;
448  case NestedNameSpecifier::Super:
449  add(NNS->getAsRecordDecl(), Flags);
450  return;
451  }
452  llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
453  }
454 
455  void add(const CXXCtorInitializer *CCI, RelSet Flags) {
456  if (!CCI)
457  return;
458  debug(*CCI, Flags);
459 
460  if (CCI->isAnyMemberInitializer())
461  add(CCI->getAnyMember(), Flags);
462  // Constructor calls contain a TypeLoc node, so we don't handle them here.
463  }
464 };
465 
466 } // namespace
467 
468 llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
469 allTargetDecls(const ast_type_traits::DynTypedNode &N) {
470  dlog("allTargetDecls({0})", nodeToString(N));
471  TargetFinder Finder;
473  if (const Decl *D = N.get<Decl>())
474  Finder.add(D, Flags);
475  else if (const Stmt *S = N.get<Stmt>())
476  Finder.add(S, Flags);
477  else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
478  Finder.add(NNSL->getNestedNameSpecifier(), Flags);
479  else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
480  Finder.add(NNS, Flags);
481  else if (const TypeLoc *TL = N.get<TypeLoc>())
482  Finder.add(TL->getType(), Flags);
483  else if (const QualType *QT = N.get<QualType>())
484  Finder.add(*QT, Flags);
485  else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
486  Finder.add(CCI, Flags);
487 
488  return {Finder.Decls.begin(), Finder.Decls.end()};
489 }
490 
491 llvm::SmallVector<const NamedDecl *, 1>
492 targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) {
493  llvm::SmallVector<const NamedDecl *, 1> Result;
494  for (const auto &Entry : allTargetDecls(N)) {
495  if (!(Entry.second & ~Mask))
496  Result.push_back(Entry.first);
497  }
498  return Result;
499 }
500 
501 llvm::SmallVector<const NamedDecl *, 1>
503  assert(!(Mask & (DeclRelation::TemplatePattern |
505  "explicitRefenceTargets handles templates on its own");
506  auto Decls = allTargetDecls(N);
507 
508  // We prefer to return template instantiation, but fallback to template
509  // pattern if instantiation is not available.
511 
512  llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
513  llvm::SmallVector<const NamedDecl *, 1> Targets;
514  bool SeenTemplateInstantiations = false;
515  for (auto &D : Decls) {
516  if (D.second & ~Mask)
517  continue;
518  if (D.second & DeclRelation::TemplatePattern) {
519  TemplatePatterns.push_back(D.first);
520  continue;
521  }
523  SeenTemplateInstantiations = true;
524  Targets.push_back(D.first);
525  }
526  if (!SeenTemplateInstantiations)
527  Targets.insert(Targets.end(), TemplatePatterns.begin(),
528  TemplatePatterns.end());
529  return Targets;
530 }
531 
532 namespace {
533 llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) {
534  struct Visitor : ConstDeclVisitor<Visitor> {
535  llvm::SmallVector<ReferenceLoc, 2> Refs;
536 
537  void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
538  // We want to keep it as non-declaration references, as the
539  // "using namespace" declaration doesn't have a name.
540  Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
541  D->getIdentLocation(),
542  /*IsDecl=*/false,
543  {D->getNominatedNamespaceAsWritten()}});
544  }
545 
546  void VisitUsingDecl(const UsingDecl *D) {
547  // "using ns::identifier;" is a non-declaration reference.
548  Refs.push_back(
549  ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
550  explicitReferenceTargets(DynTypedNode::create(*D),
552  }
553 
554  void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
555  // For namespace alias, "namespace Foo = Target;", we add two references.
556  // Add a declaration reference for Foo.
557  VisitNamedDecl(D);
558  // Add a non-declaration reference for Target.
559  Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
560  D->getTargetNameLoc(),
561  /*IsDecl=*/false,
562  {D->getAliasedNamespace()}});
563  }
564 
565  void VisitNamedDecl(const NamedDecl *ND) {
566  // FIXME: decide on how to surface destructors when we need them.
567  if (llvm::isa<CXXDestructorDecl>(ND))
568  return;
569  // Filter anonymous decls, name location will point outside the name token
570  // and the clients are not prepared to handle that.
571  if (ND->getDeclName().isIdentifier() &&
572  !ND->getDeclName().getAsIdentifierInfo())
573  return;
574  Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
575  ND->getLocation(),
576  /*IsDecl=*/true,
577  {ND}});
578  }
579  };
580 
581  Visitor V;
582  V.Visit(D);
583  return V.Refs;
584 }
585 
586 llvm::SmallVector<ReferenceLoc, 2> refInExpr(const Expr *E) {
587  struct Visitor : ConstStmtVisitor<Visitor> {
588  // FIXME: handle more complicated cases, e.g. ObjC, designated initializers.
589  llvm::SmallVector<ReferenceLoc, 2> Refs;
590 
591  void VisitDeclRefExpr(const DeclRefExpr *E) {
592  Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
593  E->getNameInfo().getLoc(),
594  /*IsDecl=*/false,
595  {E->getFoundDecl()}});
596  }
597 
598  void VisitMemberExpr(const MemberExpr *E) {
599  Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
600  E->getMemberNameInfo().getLoc(),
601  /*IsDecl=*/false,
602  {E->getFoundDecl()}});
603  }
604 
605  void VisitOverloadExpr(const OverloadExpr *E) {
606  Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
607  E->getNameInfo().getLoc(),
608  /*IsDecl=*/false,
609  llvm::SmallVector<const NamedDecl *, 1>(
610  E->decls().begin(), E->decls().end())});
611  }
612 
613  void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
614  Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
615  E->getPackLoc(),
616  /*IsDecl=*/false,
617  {E->getPack()}});
618  }
619  };
620 
621  Visitor V;
622  V.Visit(E);
623  return V.Refs;
624 }
625 
626 llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) {
627  struct Visitor : TypeLocVisitor<Visitor> {
628  llvm::Optional<ReferenceLoc> Ref;
629 
630  void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
631  // We only know about qualifier, rest if filled by inner locations.
632  Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
633  // Fill in the qualifier.
634  if (!Ref)
635  return;
636  assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
637  Ref->Qualifier = L.getQualifierLoc();
638  }
639 
640  void VisitTagTypeLoc(TagTypeLoc L) {
641  Ref = ReferenceLoc{NestedNameSpecifierLoc(),
642  L.getNameLoc(),
643  /*IsDecl=*/false,
644  {L.getDecl()}};
645  }
646 
647  void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
648  Ref = ReferenceLoc{NestedNameSpecifierLoc(),
649  L.getNameLoc(),
650  /*IsDecl=*/false,
651  {L.getDecl()}};
652  }
653 
654  void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
655  // We must ensure template type aliases are included in results if they
656  // were written in the source code, e.g. in
657  // template <class T> using valias = vector<T>;
658  // ^valias<int> x;
659  // 'explicitReferenceTargets' will return:
660  // 1. valias with mask 'Alias'.
661  // 2. 'vector<int>' with mask 'Underlying'.
662  // we want to return only #1 in this case.
663  Ref = ReferenceLoc{
664  NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
665  explicitReferenceTargets(DynTypedNode::create(L.getType()),
667  }
668  void VisitDeducedTemplateSpecializationTypeLoc(
669  DeducedTemplateSpecializationTypeLoc L) {
670  Ref = ReferenceLoc{
671  NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
672  explicitReferenceTargets(DynTypedNode::create(L.getType()),
674  }
675 
676  void VisitDependentTemplateSpecializationTypeLoc(
677  DependentTemplateSpecializationTypeLoc L) {
678  Ref = ReferenceLoc{
679  L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
680  explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
681  }
682 
683  void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
684  Ref = ReferenceLoc{
685  L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
686  explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
687  }
688 
689  void VisitTypedefTypeLoc(TypedefTypeLoc L) {
690  Ref = ReferenceLoc{NestedNameSpecifierLoc(),
691  L.getNameLoc(),
692  /*IsDecl=*/false,
693  {L.getTypedefNameDecl()}};
694  }
695  };
696 
697  Visitor V;
698  V.Visit(L.getUnqualifiedLoc());
699  if (!V.Ref)
700  return {};
701  return {*V.Ref};
702 }
703 
704 class ExplicitReferenceCollector
705  : public RecursiveASTVisitor<ExplicitReferenceCollector> {
706 public:
707  ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out)
708  : Out(Out) {
709  assert(Out);
710  }
711 
712  bool VisitTypeLoc(TypeLoc TTL) {
713  if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
714  return true;
715  visitNode(DynTypedNode::create(TTL));
716  return true;
717  }
718 
719  bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
720  // ElaboratedTypeLoc will reports information for its inner type loc.
721  // Otherwise we loose information about inner types loc's qualifier.
722  TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
723  TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
724  return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
725  }
726 
727  bool VisitExpr(Expr *E) {
728  visitNode(DynTypedNode::create(*E));
729  return true;
730  }
731 
732  // We re-define Traverse*, since there's no corresponding Visit*.
733  // TemplateArgumentLoc is the only way to get locations for references to
734  // template template parameters.
735  bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
736  switch (A.getArgument().getKind()) {
737  case TemplateArgument::Template:
738  case TemplateArgument::TemplateExpansion:
739  reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
740  A.getTemplateNameLoc(),
741  /*IsDecl=*/false,
742  {A.getArgument()
743  .getAsTemplateOrTemplatePattern()
744  .getAsTemplateDecl()}},
745  DynTypedNode::create(A.getArgument()));
746  break;
748  break; // FIXME: can this actually happen in TemplateArgumentLoc?
749  case TemplateArgument::Integral:
751  case TemplateArgument::NullPtr:
752  break; // no references.
753  case TemplateArgument::Pack:
755  case TemplateArgument::Expression:
756  break; // Handled by VisitType and VisitExpression.
757  };
758  return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
759  }
760 
761  bool VisitDecl(Decl *D) {
762  visitNode(DynTypedNode::create(*D));
763  return true;
764  }
765 
766  // We have to use Traverse* because there is no corresponding Visit*.
767  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
768  if (!L.getNestedNameSpecifier())
769  return true;
770  visitNode(DynTypedNode::create(L));
771  // Inner type is missing information about its qualifier, skip it.
772  if (auto TL = L.getTypeLoc())
773  TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
774  return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
775  }
776 
777  bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
778  visitNode(DynTypedNode::create(*Init));
779  return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
780  }
781 
782 private:
783  /// Obtain information about a reference directly defined in \p N. Does not
784  /// recurse into child nodes, e.g. do not expect references for constructor
785  /// initializers
786  ///
787  /// Any of the fields in the returned structure can be empty, but not all of
788  /// them, e.g.
789  /// - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
790  /// source location information may be missing,
791  /// - for dependent code, targets may be empty.
792  ///
793  /// (!) For the purposes of this function declarations are not considered to
794  /// be references. However, declarations can have references inside them,
795  /// e.g. 'namespace foo = std' references namespace 'std' and this
796  /// function will return the corresponding reference.
797  llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) {
798  if (auto *D = N.get<Decl>())
799  return refInDecl(D);
800  if (auto *E = N.get<Expr>())
801  return refInExpr(E);
802  if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
803  // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
804  return {ReferenceLoc{
805  NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
807  DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
809  }
810  if (const TypeLoc *TL = N.get<TypeLoc>())
811  return refInTypeLoc(*TL);
812  if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
813  // Other type initializers (e.g. base initializer) are handled by visiting
814  // the typeLoc.
815  if (CCI->isAnyMemberInitializer()) {
816  return {ReferenceLoc{NestedNameSpecifierLoc(),
817  CCI->getMemberLocation(),
818  /*IsDecl=*/false,
819  {CCI->getAnyMember()}}};
820  }
821  }
822  // We do not have location information for other nodes (QualType, etc)
823  return {};
824  }
825 
826  void visitNode(DynTypedNode N) {
827  for (const auto &R : explicitReference(N))
828  reportReference(R, N);
829  }
830 
831  void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
832  // Our promise is to return only references from the source code. If we lack
833  // location information, skip these nodes.
834  // Normally this should not happen in practice, unless there are bugs in the
835  // traversals or users started the traversal at an implicit node.
836  if (Ref.NameLoc.isInvalid()) {
837  dlog("invalid location at node {0}", nodeToString(N));
838  return;
839  }
840  Out(Ref);
841  }
842 
843  llvm::function_ref<void(ReferenceLoc)> Out;
844  /// TypeLocs starting at these locations must be skipped, see
845  /// TraverseElaboratedTypeSpecifierLoc for details.
846  llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
847 };
848 } // namespace
849 
850 void findExplicitReferences(const Stmt *S,
851  llvm::function_ref<void(ReferenceLoc)> Out) {
852  assert(S);
853  ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S));
854 }
856  llvm::function_ref<void(ReferenceLoc)> Out) {
857  assert(D);
858  ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D));
859 }
860 void findExplicitReferences(const ASTContext &AST,
861  llvm::function_ref<void(ReferenceLoc)> Out) {
862  ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST));
863 }
864 
865 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
866  switch (R) {
867 #define REL_CASE(X) \
868  case DeclRelation::X: \
869  return OS << #X;
870  REL_CASE(Alias);
874 #undef REL_CASE
875  }
876  llvm_unreachable("Unhandled DeclRelation enum");
877 }
878 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
879  const char *Sep = "";
880  for (unsigned I = 0; I < RS.S.size(); ++I) {
881  if (RS.S.test(I)) {
882  OS << Sep << static_cast<DeclRelation>(I);
883  Sep = "|";
884  }
885  }
886  return OS;
887 }
888 
889 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
890  // note we cannot print R.NameLoc without a source manager.
891  OS << "targets = {";
892  bool First = true;
893  for (const NamedDecl *T : R.Targets) {
894  if (!First)
895  OS << ", ";
896  else
897  First = false;
899  }
900  OS << "}";
901  if (R.Qualifier) {
902  OS << ", qualifier = '";
903  R.Qualifier.getNestedNameSpecifier()->print(OS,
904  PrintingPolicy(LangOptions()));
905  OS << "'";
906  }
907  if (R.IsDecl)
908  OS << ", decl";
909  return OS;
910 }
911 
912 } // namespace clangd
913 } // namespace clang
QualType DeducedType
Definition: AST.cpp:414
const FunctionDecl * Decl
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
Definition: AST.cpp:169
Information about a reference written in the source code, independent of the actual AST node that thi...
Definition: FindTarget.h:119
const Node * Parent
llvm::SmallVector< std::pair< const NamedDecl *, DeclRelationSet >, 1 > allTargetDecls(const ast_type_traits::DynTypedNode &N)
Similar to targetDecl(), however instead of applying a filter, all possible decls are returned along ...
Definition: FindTarget.cpp:469
This is the pattern the template specialization was instantiated from.
#define REL_CASE(X)
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
NestedNameSpecifierLoc getQualifierLoc(const NamedDecl &ND)
Returns a nested name specifier loc of ND if it was present in the source, e.g.
Definition: AST.cpp:188
std::string printTemplateSpecializationArgs(const NamedDecl &ND)
Prints template arguments of a decl as written in the source code, including enclosing &#39;<&#39; and &#39;>&#39;...
Definition: AST.cpp:249
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
Definition: TestIndex.cpp:30
This declaration is an alias that was referred to.
Context Ctx
bool IsDecl
True if the reference is a declaration or definition;.
Definition: FindTarget.h:125
SourceLocation NameLoc
Start location of the last name part, i.e. &#39;foo&#39; in &#39;ns::foo<int>&#39;.
Definition: FindTarget.h:123
llvm::SmallVector< const NamedDecl *, 1 > explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask)
Definition: FindTarget.cpp:502
This is the template instantiation that was referred to.
#define dlog(...)
Definition: Logger.h:72
static constexpr llvm::StringLiteral Name
llvm::SmallVector< const NamedDecl *, 1 > Targets
A list of targets referenced by this name.
Definition: FindTarget.h:131
RelSet Flags
Definition: FindTarget.cpp:171
llvm::SmallVector< const NamedDecl *, 1 > targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask)
targetDecl() finds the declaration referred to by an AST node.
Definition: FindTarget.cpp:492
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void findExplicitReferences(const Stmt *S, llvm::function_ref< void(ReferenceLoc)> Out)
Recursively traverse S and report all references explicitly written in the code.
Definition: FindTarget.cpp:850
This is the underlying declaration for an alias, decltype etc.
llvm::SmallDenseMap< const NamedDecl *, RelSet > Decls
Definition: FindTarget.cpp:170
const Expr * E
RefSlab Refs
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
NestedNameSpecifierLoc Qualifier
Contains qualifier written in the code, if any, e.g. &#39;ns::&#39; for &#39;ns::foo&#39;.
Definition: FindTarget.h:121
NodeType Type