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