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 // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so 436 // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo. 437 if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1) 438 Outer.add(OOT->getProtocol(0), Flags); 439 } 440 }; 441 Visitor(*this, Flags).Visit(T.getTypePtr()); 442 } 443 444 void add(const NestedNameSpecifier *NNS, RelSet Flags) { 445 if (!NNS) 446 return; 447 debug(*NNS, Flags); 448 switch (NNS->getKind()) { 449 case NestedNameSpecifier::Namespace: 450 add(NNS->getAsNamespace(), Flags); 451 return; 452 case NestedNameSpecifier::NamespaceAlias: 453 add(NNS->getAsNamespaceAlias(), Flags); 454 return; 455 case NestedNameSpecifier::Identifier: 456 if (Resolver) { 457 add(QualType(Resolver->resolveNestedNameSpecifierToType(NNS), 0), 458 Flags); 459 } 460 return; 461 case NestedNameSpecifier::TypeSpec: 462 case NestedNameSpecifier::TypeSpecWithTemplate: 463 add(QualType(NNS->getAsType(), 0), Flags); 464 return; 465 case NestedNameSpecifier::Global: 466 // This should be TUDecl, but we can't get a pointer to it! 467 return; 468 case NestedNameSpecifier::Super: 469 add(NNS->getAsRecordDecl(), Flags); 470 return; 471 } 472 llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind"); 473 } 474 475 void add(const CXXCtorInitializer *CCI, RelSet Flags) { 476 if (!CCI) 477 return; 478 debug(*CCI, Flags); 479 480 if (CCI->isAnyMemberInitializer()) 481 add(CCI->getAnyMember(), Flags); 482 // Constructor calls contain a TypeLoc node, so we don't handle them here. 483 } 484 485 void add(const TemplateArgument &Arg, RelSet Flags) { 486 // Only used for template template arguments. 487 // For type and non-type template arguments, SelectionTree 488 // will hit a more specific node (e.g. a TypeLoc or a 489 // DeclRefExpr). 490 if (Arg.getKind() == TemplateArgument::Template || 491 Arg.getKind() == TemplateArgument::TemplateExpansion) { 492 if (TemplateDecl *TD = Arg.getAsTemplate().getAsTemplateDecl()) { 493 report(TD, Flags); 494 } 495 } 496 } 497 }; 498 499 } // namespace 500 501 llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1> 502 allTargetDecls(const DynTypedNode &N, const HeuristicResolver *Resolver) { 503 dlog("allTargetDecls({0})", nodeToString(N)); 504 TargetFinder Finder(Resolver); 505 DeclRelationSet Flags; 506 if (const Decl *D = N.get<Decl>()) 507 Finder.add(D, Flags); 508 else if (const Stmt *S = N.get<Stmt>()) 509 Finder.add(S, Flags); 510 else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>()) 511 Finder.add(NNSL->getNestedNameSpecifier(), Flags); 512 else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>()) 513 Finder.add(NNS, Flags); 514 else if (const TypeLoc *TL = N.get<TypeLoc>()) 515 Finder.add(TL->getType(), Flags); 516 else if (const QualType *QT = N.get<QualType>()) 517 Finder.add(*QT, Flags); 518 else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) 519 Finder.add(CCI, Flags); 520 else if (const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>()) 521 Finder.add(TAL->getArgument(), Flags); 522 else if (const CXXBaseSpecifier *CBS = N.get<CXXBaseSpecifier>()) 523 Finder.add(CBS->getTypeSourceInfo()->getType(), Flags); 524 return Finder.takeDecls(); 525 } 526 527 llvm::SmallVector<const NamedDecl *, 1> 528 targetDecl(const DynTypedNode &N, DeclRelationSet Mask, 529 const HeuristicResolver *Resolver) { 530 llvm::SmallVector<const NamedDecl *, 1> Result; 531 for (const auto &Entry : allTargetDecls(N, Resolver)) { 532 if (!(Entry.second & ~Mask)) 533 Result.push_back(Entry.first); 534 } 535 return Result; 536 } 537 538 llvm::SmallVector<const NamedDecl *, 1> 539 explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask, 540 const HeuristicResolver *Resolver) { 541 assert(!(Mask & (DeclRelation::TemplatePattern | 542 DeclRelation::TemplateInstantiation)) && 543 "explicitReferenceTargets handles templates on its own"); 544 auto Decls = allTargetDecls(N, Resolver); 545 546 // We prefer to return template instantiation, but fallback to template 547 // pattern if instantiation is not available. 548 Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation; 549 550 llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns; 551 llvm::SmallVector<const NamedDecl *, 1> Targets; 552 bool SeenTemplateInstantiations = false; 553 for (auto &D : Decls) { 554 if (D.second & ~Mask) 555 continue; 556 if (D.second & DeclRelation::TemplatePattern) { 557 TemplatePatterns.push_back(D.first); 558 continue; 559 } 560 if (D.second & DeclRelation::TemplateInstantiation) 561 SeenTemplateInstantiations = true; 562 Targets.push_back(D.first); 563 } 564 if (!SeenTemplateInstantiations) 565 Targets.insert(Targets.end(), TemplatePatterns.begin(), 566 TemplatePatterns.end()); 567 return Targets; 568 } 569 570 namespace { 571 llvm::SmallVector<ReferenceLoc> refInDecl(const Decl *D, 572 const HeuristicResolver *Resolver) { 573 struct Visitor : ConstDeclVisitor<Visitor> { 574 Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {} 575 576 const HeuristicResolver *Resolver; 577 llvm::SmallVector<ReferenceLoc> Refs; 578 579 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 580 // We want to keep it as non-declaration references, as the 581 // "using namespace" declaration doesn't have a name. 582 Refs.push_back(ReferenceLoc{D->getQualifierLoc(), 583 D->getIdentLocation(), 584 /*IsDecl=*/false, 585 {D->getNominatedNamespaceAsWritten()}}); 586 } 587 588 void VisitUsingDecl(const UsingDecl *D) { 589 // "using ns::identifier;" is a non-declaration reference. 590 Refs.push_back(ReferenceLoc{ 591 D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false, 592 explicitReferenceTargets(DynTypedNode::create(*D), 593 DeclRelation::Underlying, Resolver)}); 594 } 595 596 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { 597 // For namespace alias, "namespace Foo = Target;", we add two references. 598 // Add a declaration reference for Foo. 599 VisitNamedDecl(D); 600 // Add a non-declaration reference for Target. 601 Refs.push_back(ReferenceLoc{D->getQualifierLoc(), 602 D->getTargetNameLoc(), 603 /*IsDecl=*/false, 604 {D->getAliasedNamespace()}}); 605 } 606 607 void VisitNamedDecl(const NamedDecl *ND) { 608 // We choose to ignore {Class, Function, Var, TypeAlias}TemplateDecls. As 609 // as their underlying decls, covering the same range, will be visited. 610 if (llvm::isa<ClassTemplateDecl>(ND) || 611 llvm::isa<FunctionTemplateDecl>(ND) || 612 llvm::isa<VarTemplateDecl>(ND) || 613 llvm::isa<TypeAliasTemplateDecl>(ND)) 614 return; 615 // FIXME: decide on how to surface destructors when we need them. 616 if (llvm::isa<CXXDestructorDecl>(ND)) 617 return; 618 // Filter anonymous decls, name location will point outside the name token 619 // and the clients are not prepared to handle that. 620 if (ND->getDeclName().isIdentifier() && 621 !ND->getDeclName().getAsIdentifierInfo()) 622 return; 623 Refs.push_back(ReferenceLoc{getQualifierLoc(*ND), 624 ND->getLocation(), 625 /*IsDecl=*/true, 626 {ND}}); 627 } 628 629 void VisitCXXDeductionGuideDecl(const CXXDeductionGuideDecl *DG) { 630 // The class template name in a deduction guide targets the class 631 // template. 632 Refs.push_back(ReferenceLoc{DG->getQualifierLoc(), 633 DG->getNameInfo().getLoc(), 634 /*IsDecl=*/false, 635 {DG->getDeducedTemplate()}}); 636 } 637 638 void VisitObjCMethodDecl(const ObjCMethodDecl *OMD) { 639 // The name may have several tokens, we can only report the first. 640 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 641 OMD->getSelectorStartLoc(), 642 /*IsDecl=*/true, 643 {OMD}}); 644 } 645 646 void visitProtocolList( 647 llvm::iterator_range<ObjCProtocolList::iterator> Protocols, 648 llvm::iterator_range<const SourceLocation *> Locations) { 649 for (const auto &P : llvm::zip(Protocols, Locations)) { 650 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 651 std::get<1>(P), 652 /*IsDecl=*/false, 653 {std::get<0>(P)}}); 654 } 655 } 656 657 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *OID) { 658 if (OID->isThisDeclarationADefinition()) 659 visitProtocolList(OID->protocols(), OID->protocol_locs()); 660 Base::VisitObjCInterfaceDecl(OID); // Visit the interface's name. 661 } 662 663 void VisitObjCCategoryDecl(const ObjCCategoryDecl *OCD) { 664 visitProtocolList(OCD->protocols(), OCD->protocol_locs()); 665 // getLocation is the extended class's location, not the category's. 666 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 667 OCD->getLocation(), 668 /*IsDecl=*/false, 669 {OCD->getClassInterface()}}); 670 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 671 OCD->getCategoryNameLoc(), 672 /*IsDecl=*/true, 673 {OCD}}); 674 } 675 676 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *OCID) { 677 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 678 OCID->getLocation(), 679 /*IsDecl=*/false, 680 {OCID->getClassInterface()}}); 681 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 682 OCID->getCategoryNameLoc(), 683 /*IsDecl=*/true, 684 {OCID->getCategoryDecl()}}); 685 } 686 687 void VisitObjCProtocolDecl(const ObjCProtocolDecl *OPD) { 688 if (OPD->isThisDeclarationADefinition()) 689 visitProtocolList(OPD->protocols(), OPD->protocol_locs()); 690 Base::VisitObjCProtocolDecl(OPD); // Visit the protocol's name. 691 } 692 }; 693 694 Visitor V{Resolver}; 695 V.Visit(D); 696 return V.Refs; 697 } 698 699 llvm::SmallVector<ReferenceLoc> refInStmt(const Stmt *S, 700 const HeuristicResolver *Resolver) { 701 struct Visitor : ConstStmtVisitor<Visitor> { 702 Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {} 703 704 const HeuristicResolver *Resolver; 705 // FIXME: handle more complicated cases: more ObjC, designated initializers. 706 llvm::SmallVector<ReferenceLoc> Refs; 707 708 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) { 709 Refs.push_back(ReferenceLoc{E->getNestedNameSpecifierLoc(), 710 E->getConceptNameLoc(), 711 /*IsDecl=*/false, 712 {E->getNamedConcept()}}); 713 } 714 715 void VisitDeclRefExpr(const DeclRefExpr *E) { 716 Refs.push_back(ReferenceLoc{E->getQualifierLoc(), 717 E->getNameInfo().getLoc(), 718 /*IsDecl=*/false, 719 {E->getFoundDecl()}}); 720 } 721 722 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) { 723 Refs.push_back(ReferenceLoc{ 724 E->getQualifierLoc(), E->getNameInfo().getLoc(), /*IsDecl=*/false, 725 explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)}); 726 } 727 728 void VisitMemberExpr(const MemberExpr *E) { 729 // Skip destructor calls to avoid duplication: TypeLoc within will be 730 // visited separately. 731 if (llvm::isa<CXXDestructorDecl>(E->getFoundDecl().getDecl())) 732 return; 733 Refs.push_back(ReferenceLoc{E->getQualifierLoc(), 734 E->getMemberNameInfo().getLoc(), 735 /*IsDecl=*/false, 736 {E->getFoundDecl()}}); 737 } 738 739 void 740 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { 741 Refs.push_back(ReferenceLoc{ 742 E->getQualifierLoc(), E->getMemberNameInfo().getLoc(), 743 /*IsDecl=*/false, 744 explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)}); 745 } 746 747 void VisitOverloadExpr(const OverloadExpr *E) { 748 Refs.push_back(ReferenceLoc{E->getQualifierLoc(), 749 E->getNameInfo().getLoc(), 750 /*IsDecl=*/false, 751 llvm::SmallVector<const NamedDecl *, 1>( 752 E->decls().begin(), E->decls().end())}); 753 } 754 755 void VisitSizeOfPackExpr(const SizeOfPackExpr *E) { 756 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 757 E->getPackLoc(), 758 /*IsDecl=*/false, 759 {E->getPack()}}); 760 } 761 762 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) { 763 Refs.push_back(ReferenceLoc{ 764 NestedNameSpecifierLoc(), E->getLocation(), 765 /*IsDecl=*/false, 766 // Select the getter, setter, or @property depending on the call. 767 explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)}); 768 } 769 770 void VisitObjCMessageExpr(const ObjCMessageExpr *E) { 771 // The name may have several tokens, we can only report the first. 772 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 773 E->getSelectorStartLoc(), 774 /*IsDecl=*/false, 775 {E->getMethodDecl()}}); 776 } 777 778 void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) { 779 for (const DesignatedInitExpr::Designator &D : DIE->designators()) { 780 if (!D.isFieldDesignator()) 781 continue; 782 783 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 784 D.getFieldLoc(), 785 /*IsDecl=*/false, 786 {D.getField()}}); 787 } 788 } 789 790 void VisitGotoStmt(const GotoStmt *GS) { 791 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 792 GS->getLabelLoc(), 793 /*IsDecl=*/false, 794 {GS->getLabel()}}); 795 } 796 797 void VisitLabelStmt(const LabelStmt *LS) { 798 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), 799 LS->getIdentLoc(), 800 /*IsDecl=*/true, 801 {LS->getDecl()}}); 802 } 803 }; 804 805 Visitor V{Resolver}; 806 V.Visit(S); 807 return V.Refs; 808 } 809 810 llvm::SmallVector<ReferenceLoc> 811 refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) { 812 struct Visitor : TypeLocVisitor<Visitor> { 813 Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {} 814 815 const HeuristicResolver *Resolver; 816 llvm::Optional<ReferenceLoc> Ref; 817 818 void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) { 819 // We only know about qualifier, rest if filled by inner locations. 820 Visit(L.getNamedTypeLoc().getUnqualifiedLoc()); 821 // Fill in the qualifier. 822 if (!Ref) 823 return; 824 assert(!Ref->Qualifier.hasQualifier() && "qualifier already set"); 825 Ref->Qualifier = L.getQualifierLoc(); 826 } 827 828 void VisitTagTypeLoc(TagTypeLoc L) { 829 Ref = ReferenceLoc{NestedNameSpecifierLoc(), 830 L.getNameLoc(), 831 /*IsDecl=*/false, 832 {L.getDecl()}}; 833 } 834 835 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) { 836 Ref = ReferenceLoc{NestedNameSpecifierLoc(), 837 L.getNameLoc(), 838 /*IsDecl=*/false, 839 {L.getDecl()}}; 840 } 841 842 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) { 843 // We must ensure template type aliases are included in results if they 844 // were written in the source code, e.g. in 845 // template <class T> using valias = vector<T>; 846 // ^valias<int> x; 847 // 'explicitReferenceTargets' will return: 848 // 1. valias with mask 'Alias'. 849 // 2. 'vector<int>' with mask 'Underlying'. 850 // we want to return only #1 in this case. 851 Ref = ReferenceLoc{ 852 NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false, 853 explicitReferenceTargets(DynTypedNode::create(L.getType()), 854 DeclRelation::Alias, Resolver)}; 855 } 856 void VisitDeducedTemplateSpecializationTypeLoc( 857 DeducedTemplateSpecializationTypeLoc L) { 858 Ref = ReferenceLoc{ 859 NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false, 860 explicitReferenceTargets(DynTypedNode::create(L.getType()), 861 DeclRelation::Alias, Resolver)}; 862 } 863 864 void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 865 Ref = ReferenceLoc{NestedNameSpecifierLoc(), 866 TL.getNameLoc(), 867 /*IsDecl=*/false, 868 {TL.getDecl()}}; 869 } 870 871 void VisitDependentTemplateSpecializationTypeLoc( 872 DependentTemplateSpecializationTypeLoc L) { 873 Ref = ReferenceLoc{L.getQualifierLoc(), L.getTemplateNameLoc(), 874 /*IsDecl=*/false, 875 explicitReferenceTargets( 876 DynTypedNode::create(L.getType()), {}, Resolver)}; 877 } 878 879 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) { 880 Ref = ReferenceLoc{L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false, 881 explicitReferenceTargets( 882 DynTypedNode::create(L.getType()), {}, Resolver)}; 883 } 884 885 void VisitTypedefTypeLoc(TypedefTypeLoc L) { 886 Ref = ReferenceLoc{NestedNameSpecifierLoc(), 887 L.getNameLoc(), 888 /*IsDecl=*/false, 889 {L.getTypedefNameDecl()}}; 890 } 891 892 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc L) { 893 Ref = ReferenceLoc{NestedNameSpecifierLoc(), 894 L.getNameLoc(), 895 /*IsDecl=*/false, 896 {L.getIFaceDecl()}}; 897 } 898 899 // FIXME: add references to protocols in ObjCObjectTypeLoc and maybe 900 // ObjCObjectPointerTypeLoc. 901 }; 902 903 Visitor V{Resolver}; 904 V.Visit(L.getUnqualifiedLoc()); 905 if (!V.Ref) 906 return {}; 907 return {*V.Ref}; 908 } 909 910 class ExplicitReferenceCollector 911 : public RecursiveASTVisitor<ExplicitReferenceCollector> { 912 public: 913 ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out, 914 const HeuristicResolver *Resolver) 915 : Out(Out), Resolver(Resolver) { 916 assert(Out); 917 } 918 919 bool VisitTypeLoc(TypeLoc TTL) { 920 if (TypeLocsToSkip.count(TTL.getBeginLoc())) 921 return true; 922 visitNode(DynTypedNode::create(TTL)); 923 return true; 924 } 925 926 bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) { 927 // ElaboratedTypeLoc will reports information for its inner type loc. 928 // Otherwise we loose information about inner types loc's qualifier. 929 TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc(); 930 TypeLocsToSkip.insert(Inner.getBeginLoc()); 931 return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L); 932 } 933 934 bool VisitStmt(Stmt *S) { 935 visitNode(DynTypedNode::create(*S)); 936 return true; 937 } 938 939 bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) { 940 visitNode(DynTypedNode::create(*OVE)); 941 // Not clear why the source expression is skipped by default... 942 // FIXME: can we just make RecursiveASTVisitor do this? 943 return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr()); 944 } 945 946 bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) { 947 visitNode(DynTypedNode::create(*POE)); 948 // Traverse only the syntactic form to find the *written* references. 949 // (The semantic form also contains lots of duplication) 950 return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm()); 951 } 952 953 // We re-define Traverse*, since there's no corresponding Visit*. 954 // TemplateArgumentLoc is the only way to get locations for references to 955 // template template parameters. 956 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) { 957 switch (A.getArgument().getKind()) { 958 case TemplateArgument::Template: 959 case TemplateArgument::TemplateExpansion: 960 reportReference(ReferenceLoc{A.getTemplateQualifierLoc(), 961 A.getTemplateNameLoc(), 962 /*IsDecl=*/false, 963 {A.getArgument() 964 .getAsTemplateOrTemplatePattern() 965 .getAsTemplateDecl()}}, 966 DynTypedNode::create(A.getArgument())); 967 break; 968 case TemplateArgument::Declaration: 969 break; // FIXME: can this actually happen in TemplateArgumentLoc? 970 case TemplateArgument::Integral: 971 case TemplateArgument::Null: 972 case TemplateArgument::NullPtr: 973 break; // no references. 974 case TemplateArgument::Pack: 975 case TemplateArgument::Type: 976 case TemplateArgument::Expression: 977 break; // Handled by VisitType and VisitExpression. 978 }; 979 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A); 980 } 981 982 bool VisitDecl(Decl *D) { 983 visitNode(DynTypedNode::create(*D)); 984 return true; 985 } 986 987 // We have to use Traverse* because there is no corresponding Visit*. 988 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) { 989 if (!L.getNestedNameSpecifier()) 990 return true; 991 visitNode(DynTypedNode::create(L)); 992 // Inner type is missing information about its qualifier, skip it. 993 if (auto TL = L.getTypeLoc()) 994 TypeLocsToSkip.insert(TL.getBeginLoc()); 995 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L); 996 } 997 998 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { 999 visitNode(DynTypedNode::create(*Init)); 1000 return RecursiveASTVisitor::TraverseConstructorInitializer(Init); 1001 } 1002 1003 private: 1004 /// Obtain information about a reference directly defined in \p N. Does not 1005 /// recurse into child nodes, e.g. do not expect references for constructor 1006 /// initializers 1007 /// 1008 /// Any of the fields in the returned structure can be empty, but not all of 1009 /// them, e.g. 1010 /// - for implicitly generated nodes (e.g. MemberExpr from range-based-for), 1011 /// source location information may be missing, 1012 /// - for dependent code, targets may be empty. 1013 /// 1014 /// (!) For the purposes of this function declarations are not considered to 1015 /// be references. However, declarations can have references inside them, 1016 /// e.g. 'namespace foo = std' references namespace 'std' and this 1017 /// function will return the corresponding reference. 1018 llvm::SmallVector<ReferenceLoc> explicitReference(DynTypedNode N) { 1019 if (auto *D = N.get<Decl>()) 1020 return refInDecl(D, Resolver); 1021 if (auto *S = N.get<Stmt>()) 1022 return refInStmt(S, Resolver); 1023 if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) { 1024 // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases. 1025 return {ReferenceLoc{ 1026 NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false, 1027 explicitReferenceTargets( 1028 DynTypedNode::create(*NNSL->getNestedNameSpecifier()), 1029 DeclRelation::Alias, Resolver)}}; 1030 } 1031 if (const TypeLoc *TL = N.get<TypeLoc>()) 1032 return refInTypeLoc(*TL, Resolver); 1033 if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) { 1034 // Other type initializers (e.g. base initializer) are handled by visiting 1035 // the typeLoc. 1036 if (CCI->isAnyMemberInitializer()) { 1037 return {ReferenceLoc{NestedNameSpecifierLoc(), 1038 CCI->getMemberLocation(), 1039 /*IsDecl=*/false, 1040 {CCI->getAnyMember()}}}; 1041 } 1042 } 1043 // We do not have location information for other nodes (QualType, etc) 1044 return {}; 1045 } 1046 1047 void visitNode(DynTypedNode N) { 1048 for (auto &R : explicitReference(N)) 1049 reportReference(std::move(R), N); 1050 } 1051 1052 void reportReference(ReferenceLoc &&Ref, DynTypedNode N) { 1053 // Strip null targets that can arise from invalid code. 1054 // (This avoids having to check for null everywhere we insert) 1055 llvm::erase_value(Ref.Targets, nullptr); 1056 // Our promise is to return only references from the source code. If we lack 1057 // location information, skip these nodes. 1058 // Normally this should not happen in practice, unless there are bugs in the 1059 // traversals or users started the traversal at an implicit node. 1060 if (Ref.NameLoc.isInvalid()) { 1061 dlog("invalid location at node {0}", nodeToString(N)); 1062 return; 1063 } 1064 Out(Ref); 1065 } 1066 1067 llvm::function_ref<void(ReferenceLoc)> Out; 1068 const HeuristicResolver *Resolver; 1069 /// TypeLocs starting at these locations must be skipped, see 1070 /// TraverseElaboratedTypeSpecifierLoc for details. 1071 llvm::DenseSet<SourceLocation> TypeLocsToSkip; 1072 }; 1073 } // namespace 1074 1075 void findExplicitReferences(const Stmt *S, 1076 llvm::function_ref<void(ReferenceLoc)> Out, 1077 const HeuristicResolver *Resolver) { 1078 assert(S); 1079 ExplicitReferenceCollector(Out, Resolver).TraverseStmt(const_cast<Stmt *>(S)); 1080 } 1081 void findExplicitReferences(const Decl *D, 1082 llvm::function_ref<void(ReferenceLoc)> Out, 1083 const HeuristicResolver *Resolver) { 1084 assert(D); 1085 ExplicitReferenceCollector(Out, Resolver).TraverseDecl(const_cast<Decl *>(D)); 1086 } 1087 void findExplicitReferences(const ASTContext &AST, 1088 llvm::function_ref<void(ReferenceLoc)> Out, 1089 const HeuristicResolver *Resolver) { 1090 ExplicitReferenceCollector(Out, Resolver) 1091 .TraverseAST(const_cast<ASTContext &>(AST)); 1092 } 1093 1094 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) { 1095 switch (R) { 1096 #define REL_CASE(X) \ 1097 case DeclRelation::X: \ 1098 return OS << #X; 1099 REL_CASE(Alias); 1100 REL_CASE(Underlying); 1101 REL_CASE(TemplateInstantiation); 1102 REL_CASE(TemplatePattern); 1103 #undef REL_CASE 1104 } 1105 llvm_unreachable("Unhandled DeclRelation enum"); 1106 } 1107 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) { 1108 const char *Sep = ""; 1109 for (unsigned I = 0; I < RS.S.size(); ++I) { 1110 if (RS.S.test(I)) { 1111 OS << Sep << static_cast<DeclRelation>(I); 1112 Sep = "|"; 1113 } 1114 } 1115 return OS; 1116 } 1117 1118 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) { 1119 // note we cannot print R.NameLoc without a source manager. 1120 OS << "targets = {"; 1121 bool First = true; 1122 for (const NamedDecl *T : R.Targets) { 1123 if (!First) 1124 OS << ", "; 1125 else 1126 First = false; 1127 OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T); 1128 } 1129 OS << "}"; 1130 if (R.Qualifier) { 1131 OS << ", qualifier = '"; 1132 R.Qualifier.getNestedNameSpecifier()->print(OS, 1133 PrintingPolicy(LangOptions())); 1134 OS << "'"; 1135 } 1136 if (R.IsDecl) 1137 OS << ", decl"; 1138 return OS; 1139 } 1140 1141 } // namespace clangd 1142 } // namespace clang 1143