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