Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 1 | //===--- FindTarget.cpp - What does an AST node refer to? -----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "FindTarget.h" |
| 10 | #include "AST.h" |
| 11 | #include "Logger.h" |
| 12 | #include "clang/AST/ASTTypeTraits.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 13 | #include "clang/AST/Decl.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/DeclVisitor.h" |
| 17 | #include "clang/AST/DeclarationName.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Ilya Biryukov | 2774457 | 2019-09-27 09:39:10 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 21 | #include "clang/AST/NestedNameSpecifier.h" |
| 22 | #include "clang/AST/PrettyPrinter.h" |
| 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtVisitor.h" |
Ilya Biryukov | 1d32da8 | 2019-10-01 10:02:23 +0000 | [diff] [blame] | 25 | #include "clang/AST/TemplateBase.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 26 | #include "clang/AST/Type.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLocVisitor.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 29 | #include "clang/Basic/LangOptions.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceLocation.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
| 32 | #include "llvm/ADT/SmallVector.h" |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Casting.h" |
| 34 | #include "llvm/Support/Compiler.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 36 | #include <utility> |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 37 | |
| 38 | namespace clang { |
| 39 | namespace clangd { |
| 40 | namespace { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 41 | using ast_type_traits::DynTypedNode; |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 42 | |
Sam McCall | 80195e7 | 2019-09-03 13:54:27 +0000 | [diff] [blame] | 43 | LLVM_ATTRIBUTE_UNUSED std::string |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 44 | nodeToString(const ast_type_traits::DynTypedNode &N) { |
| 45 | std::string S = N.getNodeKind().asStringRef(); |
| 46 | { |
| 47 | llvm::raw_string_ostream OS(S); |
| 48 | OS << ": "; |
| 49 | N.print(OS, PrintingPolicy(LangOptions())); |
| 50 | } |
| 51 | std::replace(S.begin(), S.end(), '\n', ' '); |
| 52 | return S; |
| 53 | } |
| 54 | |
Nathan Ridge | ecaa936 | 2019-12-05 18:29:32 -0500 | [diff] [blame] | 55 | // Given a dependent type and a member name, heuristically resolve the |
| 56 | // name to one or more declarations. |
| 57 | // The current heuristic is simply to look up the name in the primary |
| 58 | // template. This is a heuristic because the template could potentially |
| 59 | // have specializations that declare different members. |
| 60 | // Multiple declarations could be returned if the name is overloaded |
| 61 | // (e.g. an overloaded method in the primary template). |
| 62 | // This heuristic will give the desired answer in many cases, e.g. |
| 63 | // for a call to vector<T>::size(). |
| 64 | std::vector<const NamedDecl *> |
| 65 | getMembersReferencedViaDependentName(const Type *T, const DeclarationName &Name, |
| 66 | bool IsNonstaticMember) { |
| 67 | if (!T) |
| 68 | return {}; |
| 69 | if (auto *ICNT = T->getAs<InjectedClassNameType>()) { |
| 70 | T = ICNT->getInjectedSpecializationType().getTypePtrOrNull(); |
| 71 | } |
| 72 | auto *TST = T->getAs<TemplateSpecializationType>(); |
| 73 | if (!TST) |
| 74 | return {}; |
| 75 | const ClassTemplateDecl *TD = dyn_cast_or_null<ClassTemplateDecl>( |
| 76 | TST->getTemplateName().getAsTemplateDecl()); |
| 77 | if (!TD) |
| 78 | return {}; |
| 79 | CXXRecordDecl *RD = TD->getTemplatedDecl(); |
| 80 | if (!RD->hasDefinition()) |
| 81 | return {}; |
| 82 | RD = RD->getDefinition(); |
| 83 | return RD->lookupDependentName(Name, [=](const NamedDecl *D) { |
| 84 | return IsNonstaticMember ? D->isCXXInstanceMember() |
| 85 | : !D->isCXXInstanceMember(); |
| 86 | }); |
| 87 | } |
| 88 | |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 89 | // TargetFinder locates the entities that an AST node refers to. |
| 90 | // |
| 91 | // Typically this is (possibly) one declaration and (possibly) one type, but |
| 92 | // may be more: |
| 93 | // - for ambiguous nodes like OverloadExpr |
| 94 | // - if we want to include e.g. both typedefs and the underlying type |
| 95 | // |
| 96 | // This is organized as a set of mutually recursive helpers for particular node |
| 97 | // types, but for most nodes this is a short walk rather than a deep traversal. |
| 98 | // |
| 99 | // It's tempting to do e.g. typedef resolution as a second normalization step, |
| 100 | // after finding the 'primary' decl etc. But we do this monolithically instead |
| 101 | // because: |
| 102 | // - normalization may require these traversals again (e.g. unwrapping a |
| 103 | // typedef reveals a decltype which must be traversed) |
| 104 | // - it doesn't simplify that much, e.g. the first stage must still be able |
| 105 | // to yield multiple decls to handle OverloadExpr |
| 106 | // - there are cases where it's required for correctness. e.g: |
| 107 | // template<class X> using pvec = vector<x*>; pvec<int> x; |
| 108 | // There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>` |
| 109 | // and both are lossy. We must know upfront what the caller ultimately wants. |
| 110 | // |
| 111 | // FIXME: improve common dependent scope using name lookup in primary templates. |
| 112 | // e.g. template<typename T> int foo() { return std::vector<T>().size(); } |
| 113 | // formally size() is unresolved, but the primary template is a good guess. |
| 114 | // This affects: |
| 115 | // - DependentTemplateSpecializationType, |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 116 | // - DependentNameType |
Nathan Ridge | ecaa936 | 2019-12-05 18:29:32 -0500 | [diff] [blame] | 117 | // - UnresolvedUsingValueDecl |
| 118 | // - UnresolvedUsingTypenameDecl |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 119 | struct TargetFinder { |
| 120 | using RelSet = DeclRelationSet; |
| 121 | using Rel = DeclRelation; |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 122 | llvm::SmallDenseMap<const NamedDecl *, RelSet> Decls; |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 123 | RelSet Flags; |
| 124 | |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 125 | static const NamedDecl *getTemplatePattern(const NamedDecl *D) { |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 126 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 127 | return CRD->getTemplateInstantiationPattern(); |
| 128 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 129 | return FD->getTemplateInstantiationPattern(); |
| 130 | } else if (auto *VD = dyn_cast<VarDecl>(D)) { |
| 131 | // Hmm: getTIP returns its arg if it's not an instantiation?! |
| 132 | VarDecl *T = VD->getTemplateInstantiationPattern(); |
| 133 | return (T == D) ? nullptr : T; |
| 134 | } else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
| 135 | return ED->getInstantiatedFromMemberEnum(); |
| 136 | } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) { |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 137 | if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext())) |
| 138 | if (const DeclContext *ParentPat = |
| 139 | dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent))) |
| 140 | for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName())) |
| 141 | if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind()) |
| 142 | return BaseND; |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 143 | } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) { |
| 144 | if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) { |
| 145 | if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) { |
| 146 | for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName())) |
| 147 | return BaseECD; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return nullptr; |
| 152 | } |
| 153 | |
| 154 | template <typename T> void debug(T &Node, RelSet Flags) { |
| 155 | dlog("visit [{0}] {1}", Flags, |
| 156 | nodeToString(ast_type_traits::DynTypedNode::create(Node))); |
| 157 | } |
| 158 | |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 159 | void report(const NamedDecl *D, RelSet Flags) { |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 160 | dlog("--> [{0}] {1}", Flags, |
| 161 | nodeToString(ast_type_traits::DynTypedNode::create(*D))); |
| 162 | Decls[D] |= Flags; |
| 163 | } |
| 164 | |
| 165 | public: |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 166 | void add(const Decl *Dcl, RelSet Flags) { |
| 167 | const NamedDecl *D = llvm::dyn_cast<NamedDecl>(Dcl); |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 168 | if (!D) |
| 169 | return; |
| 170 | debug(*D, Flags); |
| 171 | if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D)) |
| 172 | D = UDD->getNominatedNamespaceAsWritten(); |
| 173 | |
| 174 | if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 175 | add(TND->getUnderlyingType(), Flags | Rel::Underlying); |
| 176 | Flags |= Rel::Alias; // continue with the alias. |
| 177 | } else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 178 | for (const UsingShadowDecl *S : UD->shadows()) |
| 179 | add(S->getUnderlyingDecl(), Flags | Rel::Underlying); |
| 180 | Flags |= Rel::Alias; // continue with the alias. |
| 181 | } else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) { |
| 182 | add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying); |
| 183 | Flags |= Rel::Alias; // continue with the alias |
| 184 | } else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) { |
| 185 | // Include the using decl, but don't traverse it. This may end up |
| 186 | // including *all* shadows, which we don't want. |
| 187 | report(USD->getUsingDecl(), Flags | Rel::Alias); |
| 188 | // Shadow decls are synthetic and not themselves interesting. |
| 189 | // Record the underlying decl instead, if allowed. |
| 190 | D = USD->getTargetDecl(); |
| 191 | Flags |= Rel::Underlying; // continue with the underlying decl. |
| 192 | } |
| 193 | |
| 194 | if (const Decl *Pat = getTemplatePattern(D)) { |
| 195 | assert(Pat != D); |
| 196 | add(Pat, Flags | Rel::TemplatePattern); |
| 197 | // Now continue with the instantiation. |
| 198 | Flags |= Rel::TemplateInstantiation; |
| 199 | } |
| 200 | |
| 201 | report(D, Flags); |
| 202 | } |
| 203 | |
| 204 | void add(const Stmt *S, RelSet Flags) { |
| 205 | if (!S) |
| 206 | return; |
| 207 | debug(*S, Flags); |
| 208 | struct Visitor : public ConstStmtVisitor<Visitor> { |
| 209 | TargetFinder &Outer; |
| 210 | RelSet Flags; |
| 211 | Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {} |
| 212 | |
Haojian Wu | 939544a | 2019-11-27 16:22:16 +0100 | [diff] [blame] | 213 | void VisitCallExpr(const CallExpr *CE) { |
| 214 | Outer.add(CE->getCalleeDecl(), Flags); |
| 215 | } |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 216 | void VisitDeclRefExpr(const DeclRefExpr *DRE) { |
| 217 | const Decl *D = DRE->getDecl(); |
| 218 | // UsingShadowDecl allows us to record the UsingDecl. |
| 219 | // getFoundDecl() returns the wrong thing in other cases (templates). |
| 220 | if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl())) |
| 221 | D = USD; |
| 222 | Outer.add(D, Flags); |
| 223 | } |
| 224 | void VisitMemberExpr(const MemberExpr *ME) { |
| 225 | const Decl *D = ME->getMemberDecl(); |
| 226 | if (auto *USD = |
| 227 | llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl())) |
| 228 | D = USD; |
| 229 | Outer.add(D, Flags); |
| 230 | } |
Ilya Biryukov | a160a0b | 2019-10-01 07:27:55 +0000 | [diff] [blame] | 231 | void VisitOverloadExpr(const OverloadExpr *OE) { |
| 232 | for (auto *D : OE->decls()) |
| 233 | Outer.add(D, Flags); |
| 234 | } |
Nathan Ridge | 1a4ee4c | 2019-12-05 14:27:23 -0500 | [diff] [blame] | 235 | void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) { |
| 236 | Outer.add(SE->getPack(), Flags); |
| 237 | } |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 238 | void VisitCXXConstructExpr(const CXXConstructExpr *CCE) { |
| 239 | Outer.add(CCE->getConstructor(), Flags); |
| 240 | } |
| 241 | void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) { |
| 242 | for (const DesignatedInitExpr::Designator &D : |
| 243 | llvm::reverse(DIE->designators())) |
| 244 | if (D.isFieldDesignator()) { |
| 245 | Outer.add(D.getField(), Flags); |
| 246 | // We don't know which designator was intended, we assume the outer. |
| 247 | break; |
| 248 | } |
| 249 | } |
Nathan Ridge | ecaa936 | 2019-12-05 18:29:32 -0500 | [diff] [blame] | 250 | void |
| 251 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
| 252 | const Type *BaseType = E->getBaseType().getTypePtrOrNull(); |
| 253 | if (E->isArrow()) { |
| 254 | // FIXME: Handle smart pointer types by looking up operator-> |
| 255 | // in the primary template. |
| 256 | if (!BaseType || !BaseType->isPointerType()) { |
| 257 | return; |
| 258 | } |
| 259 | BaseType = BaseType->getAs<PointerType>() |
| 260 | ->getPointeeType() |
| 261 | .getTypePtrOrNull(); |
| 262 | } |
| 263 | for (const NamedDecl *D : |
| 264 | getMembersReferencedViaDependentName(BaseType, E->getMember(), |
| 265 | /*IsNonstaticMember=*/true)) { |
| 266 | Outer.add(D, Flags); |
| 267 | } |
| 268 | } |
| 269 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) { |
| 270 | for (const NamedDecl *D : getMembersReferencedViaDependentName( |
| 271 | E->getQualifier()->getAsType(), E->getDeclName(), |
| 272 | /*IsNonstaticMember=*/false)) { |
| 273 | Outer.add(D, Flags); |
| 274 | } |
| 275 | } |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 276 | void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) { |
| 277 | Outer.add(OIRE->getDecl(), Flags); |
| 278 | } |
| 279 | void VisitObjCMessageExpr(const ObjCMessageExpr *OME) { |
| 280 | Outer.add(OME->getMethodDecl(), Flags); |
| 281 | } |
| 282 | void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) { |
| 283 | if (OPRE->isExplicitProperty()) |
| 284 | Outer.add(OPRE->getExplicitProperty(), Flags); |
| 285 | else { |
| 286 | if (OPRE->isMessagingGetter()) |
| 287 | Outer.add(OPRE->getImplicitPropertyGetter(), Flags); |
| 288 | if (OPRE->isMessagingSetter()) |
| 289 | Outer.add(OPRE->getImplicitPropertySetter(), Flags); |
| 290 | } |
| 291 | } |
| 292 | void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) { |
| 293 | Outer.add(OPE->getProtocol(), Flags); |
| 294 | } |
| 295 | }; |
| 296 | Visitor(*this, Flags).Visit(S); |
| 297 | } |
| 298 | |
| 299 | void add(QualType T, RelSet Flags) { |
| 300 | if (T.isNull()) |
| 301 | return; |
| 302 | debug(T, Flags); |
| 303 | struct Visitor : public TypeVisitor<Visitor> { |
| 304 | TargetFinder &Outer; |
| 305 | RelSet Flags; |
| 306 | Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {} |
| 307 | |
| 308 | void VisitTagType(const TagType *TT) { |
| 309 | Outer.add(TT->getAsTagDecl(), Flags); |
| 310 | } |
| 311 | void VisitDecltypeType(const DecltypeType *DTT) { |
| 312 | Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying); |
| 313 | } |
| 314 | void VisitDeducedType(const DeducedType *DT) { |
| 315 | // FIXME: In practice this doesn't work: the AutoType you find inside |
| 316 | // TypeLoc never has a deduced type. https://llvm.org/PR42914 |
| 317 | Outer.add(DT->getDeducedType(), Flags | Rel::Underlying); |
| 318 | } |
| 319 | void VisitTypedefType(const TypedefType *TT) { |
| 320 | Outer.add(TT->getDecl(), Flags); |
| 321 | } |
| 322 | void |
| 323 | VisitTemplateSpecializationType(const TemplateSpecializationType *TST) { |
| 324 | // Have to handle these case-by-case. |
| 325 | |
| 326 | // templated type aliases: there's no specialized/instantiated using |
| 327 | // decl to point to. So try to find a decl for the underlying type |
| 328 | // (after substitution), and failing that point to the (templated) using |
| 329 | // decl. |
| 330 | if (TST->isTypeAlias()) { |
| 331 | Outer.add(TST->getAliasedType(), Flags | Rel::Underlying); |
| 332 | // Don't *traverse* the alias, which would result in traversing the |
| 333 | // template of the underlying type. |
| 334 | Outer.report( |
| 335 | TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(), |
| 336 | Flags | Rel::Alias | Rel::TemplatePattern); |
| 337 | } |
| 338 | // specializations of template template parameters aren't instantiated |
| 339 | // into decls, so they must refer to the parameter itself. |
| 340 | else if (const auto *Parm = |
| 341 | llvm::dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 342 | TST->getTemplateName().getAsTemplateDecl())) |
| 343 | Outer.add(Parm, Flags); |
| 344 | // class template specializations have a (specialized) CXXRecordDecl. |
| 345 | else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl()) |
| 346 | Outer.add(RD, Flags); // add(Decl) will despecialize if needed. |
| 347 | else { |
| 348 | // fallback: the (un-specialized) declaration from primary template. |
| 349 | if (auto *TD = TST->getTemplateName().getAsTemplateDecl()) |
| 350 | Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern); |
| 351 | } |
| 352 | } |
| 353 | void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) { |
| 354 | Outer.add(TTPT->getDecl(), Flags); |
| 355 | } |
| 356 | void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) { |
| 357 | Outer.add(OIT->getDecl(), Flags); |
| 358 | } |
| 359 | void VisitObjCObjectType(const ObjCObjectType *OOT) { |
| 360 | // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so |
| 361 | // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo. |
| 362 | if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1) |
| 363 | Outer.add(OOT->getProtocol(0), Flags); |
| 364 | } |
| 365 | }; |
| 366 | Visitor(*this, Flags).Visit(T.getTypePtr()); |
| 367 | } |
| 368 | |
| 369 | void add(const NestedNameSpecifier *NNS, RelSet Flags) { |
| 370 | if (!NNS) |
| 371 | return; |
| 372 | debug(*NNS, Flags); |
| 373 | switch (NNS->getKind()) { |
| 374 | case NestedNameSpecifier::Identifier: |
| 375 | return; |
| 376 | case NestedNameSpecifier::Namespace: |
| 377 | add(NNS->getAsNamespace(), Flags); |
| 378 | return; |
| 379 | case NestedNameSpecifier::NamespaceAlias: |
| 380 | add(NNS->getAsNamespaceAlias(), Flags); |
| 381 | return; |
| 382 | case NestedNameSpecifier::TypeSpec: |
| 383 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 384 | add(QualType(NNS->getAsType(), 0), Flags); |
| 385 | return; |
| 386 | case NestedNameSpecifier::Global: |
| 387 | // This should be TUDecl, but we can't get a pointer to it! |
| 388 | return; |
| 389 | case NestedNameSpecifier::Super: |
| 390 | add(NNS->getAsRecordDecl(), Flags); |
| 391 | return; |
| 392 | } |
| 393 | llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind"); |
| 394 | } |
| 395 | |
| 396 | void add(const CXXCtorInitializer *CCI, RelSet Flags) { |
| 397 | if (!CCI) |
| 398 | return; |
| 399 | debug(*CCI, Flags); |
| 400 | |
| 401 | if (CCI->isAnyMemberInitializer()) |
| 402 | add(CCI->getAnyMember(), Flags); |
| 403 | // Constructor calls contain a TypeLoc node, so we don't handle them here. |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | } // namespace |
| 408 | |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 409 | llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1> |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 410 | allTargetDecls(const ast_type_traits::DynTypedNode &N) { |
| 411 | dlog("allTargetDecls({0})", nodeToString(N)); |
| 412 | TargetFinder Finder; |
| 413 | DeclRelationSet Flags; |
| 414 | if (const Decl *D = N.get<Decl>()) |
| 415 | Finder.add(D, Flags); |
| 416 | else if (const Stmt *S = N.get<Stmt>()) |
| 417 | Finder.add(S, Flags); |
| 418 | else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>()) |
| 419 | Finder.add(NNSL->getNestedNameSpecifier(), Flags); |
| 420 | else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>()) |
| 421 | Finder.add(NNS, Flags); |
| 422 | else if (const TypeLoc *TL = N.get<TypeLoc>()) |
| 423 | Finder.add(TL->getType(), Flags); |
| 424 | else if (const QualType *QT = N.get<QualType>()) |
| 425 | Finder.add(*QT, Flags); |
| 426 | else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) |
| 427 | Finder.add(CCI, Flags); |
| 428 | |
| 429 | return {Finder.Decls.begin(), Finder.Decls.end()}; |
| 430 | } |
| 431 | |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 432 | llvm::SmallVector<const NamedDecl *, 1> |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 433 | targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) { |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 434 | llvm::SmallVector<const NamedDecl *, 1> Result; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 435 | for (const auto &Entry : allTargetDecls(N)) { |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 436 | if (!(Entry.second & ~Mask)) |
| 437 | Result.push_back(Entry.first); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 438 | } |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 439 | return Result; |
| 440 | } |
| 441 | |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 442 | llvm::SmallVector<const NamedDecl *, 1> |
Kadir Cetinkaya | ac3f9e4 | 2019-12-17 12:13:28 +0100 | [diff] [blame] | 443 | explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 444 | assert(!(Mask & (DeclRelation::TemplatePattern | |
| 445 | DeclRelation::TemplateInstantiation)) && |
| 446 | "explicitRefenceTargets handles templates on its own"); |
| 447 | auto Decls = allTargetDecls(N); |
| 448 | |
| 449 | // We prefer to return template instantiation, but fallback to template |
| 450 | // pattern if instantiation is not available. |
| 451 | Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation; |
| 452 | |
| 453 | llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns; |
| 454 | llvm::SmallVector<const NamedDecl *, 1> Targets; |
| 455 | bool SeenTemplateInstantiations = false; |
| 456 | for (auto &D : Decls) { |
| 457 | if (D.second & ~Mask) |
| 458 | continue; |
| 459 | if (D.second & DeclRelation::TemplatePattern) { |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 460 | TemplatePatterns.push_back(D.first); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 461 | continue; |
| 462 | } |
| 463 | if (D.second & DeclRelation::TemplateInstantiation) |
| 464 | SeenTemplateInstantiations = true; |
Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 465 | Targets.push_back(D.first); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 466 | } |
| 467 | if (!SeenTemplateInstantiations) |
| 468 | Targets.insert(Targets.end(), TemplatePatterns.begin(), |
| 469 | TemplatePatterns.end()); |
| 470 | return Targets; |
| 471 | } |
| 472 | |
Kadir Cetinkaya | ac3f9e4 | 2019-12-17 12:13:28 +0100 | [diff] [blame] | 473 | namespace { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 474 | llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 475 | struct Visitor : ConstDeclVisitor<Visitor> { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 476 | llvm::SmallVector<ReferenceLoc, 2> Refs; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 477 | |
| 478 | void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 479 | // We want to keep it as non-declaration references, as the |
| 480 | // "using namespace" declaration doesn't have a name. |
| 481 | Refs.push_back(ReferenceLoc{D->getQualifierLoc(), |
| 482 | D->getIdentLocation(), |
| 483 | /*IsDecl=*/false, |
| 484 | {D->getNominatedNamespaceAsWritten()}}); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void VisitUsingDecl(const UsingDecl *D) { |
Kazuaki Ishizaki | b7ecf1c | 2020-01-04 10:28:41 -0500 | [diff] [blame] | 488 | // "using ns::identifier;" is a non-declaration reference. |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 489 | Refs.push_back( |
| 490 | ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false, |
| 491 | explicitReferenceTargets(DynTypedNode::create(*D), |
| 492 | DeclRelation::Underlying)}); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 496 | // For namespace alias, "namespace Foo = Target;", we add two references. |
| 497 | // Add a declaration reference for Foo. |
| 498 | VisitNamedDecl(D); |
| 499 | // Add a non-declaration reference for Target. |
| 500 | Refs.push_back(ReferenceLoc{D->getQualifierLoc(), |
| 501 | D->getTargetNameLoc(), |
| 502 | /*IsDecl=*/false, |
| 503 | {D->getAliasedNamespace()}}); |
| 504 | } |
| 505 | |
| 506 | void VisitNamedDecl(const NamedDecl *ND) { |
| 507 | // FIXME: decide on how to surface destructors when we need them. |
| 508 | if (llvm::isa<CXXDestructorDecl>(ND)) |
| 509 | return; |
Ilya Biryukov | 4c430a7 | 2019-10-28 14:41:06 +0100 | [diff] [blame] | 510 | // Filter anonymous decls, name location will point outside the name token |
| 511 | // and the clients are not prepared to handle that. |
| 512 | if (ND->getDeclName().isIdentifier() && |
| 513 | !ND->getDeclName().getAsIdentifierInfo()) |
| 514 | return; |
| 515 | Refs.push_back(ReferenceLoc{getQualifierLoc(*ND), |
| 516 | ND->getLocation(), |
| 517 | /*IsDecl=*/true, |
| 518 | {ND}}); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 519 | } |
| 520 | }; |
| 521 | |
| 522 | Visitor V; |
| 523 | V.Visit(D); |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 524 | return V.Refs; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 527 | llvm::SmallVector<ReferenceLoc, 2> refInExpr(const Expr *E) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 528 | struct Visitor : ConstStmtVisitor<Visitor> { |
| 529 | // FIXME: handle more complicated cases, e.g. ObjC, designated initializers. |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 530 | llvm::SmallVector<ReferenceLoc, 2> Refs; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 531 | |
| 532 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 533 | Refs.push_back(ReferenceLoc{E->getQualifierLoc(), |
| 534 | E->getNameInfo().getLoc(), |
| 535 | /*IsDecl=*/false, |
| 536 | {E->getFoundDecl()}}); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void VisitMemberExpr(const MemberExpr *E) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 540 | Refs.push_back(ReferenceLoc{E->getQualifierLoc(), |
| 541 | E->getMemberNameInfo().getLoc(), |
| 542 | /*IsDecl=*/false, |
| 543 | {E->getFoundDecl()}}); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 544 | } |
Ilya Biryukov | 2774457 | 2019-09-27 09:39:10 +0000 | [diff] [blame] | 545 | |
| 546 | void VisitOverloadExpr(const OverloadExpr *E) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 547 | Refs.push_back(ReferenceLoc{E->getQualifierLoc(), |
| 548 | E->getNameInfo().getLoc(), |
| 549 | /*IsDecl=*/false, |
| 550 | llvm::SmallVector<const NamedDecl *, 1>( |
| 551 | E->decls().begin(), E->decls().end())}); |
Ilya Biryukov | 2774457 | 2019-09-27 09:39:10 +0000 | [diff] [blame] | 552 | } |
Nathan Ridge | 1a4ee4c | 2019-12-05 14:27:23 -0500 | [diff] [blame] | 553 | |
| 554 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 555 | Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), |
| 556 | E->getPackLoc(), |
| 557 | /*IsDecl=*/false, |
| 558 | {E->getPack()}}); |
| 559 | } |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 560 | }; |
| 561 | |
| 562 | Visitor V; |
| 563 | V.Visit(E); |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 564 | return V.Refs; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 567 | llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 568 | struct Visitor : TypeLocVisitor<Visitor> { |
| 569 | llvm::Optional<ReferenceLoc> Ref; |
| 570 | |
| 571 | void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) { |
| 572 | // We only know about qualifier, rest if filled by inner locations. |
| 573 | Visit(L.getNamedTypeLoc().getUnqualifiedLoc()); |
| 574 | // Fill in the qualifier. |
| 575 | if (!Ref) |
| 576 | return; |
| 577 | assert(!Ref->Qualifier.hasQualifier() && "qualifier already set"); |
| 578 | Ref->Qualifier = L.getQualifierLoc(); |
| 579 | } |
| 580 | |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 581 | void VisitTagTypeLoc(TagTypeLoc L) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 582 | Ref = ReferenceLoc{NestedNameSpecifierLoc(), |
| 583 | L.getNameLoc(), |
| 584 | /*IsDecl=*/false, |
| 585 | {L.getDecl()}}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Ilya Biryukov | c383509 | 2019-09-27 10:55:53 +0000 | [diff] [blame] | 588 | void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 589 | Ref = ReferenceLoc{NestedNameSpecifierLoc(), |
| 590 | L.getNameLoc(), |
| 591 | /*IsDecl=*/false, |
| 592 | {L.getDecl()}}; |
Ilya Biryukov | c383509 | 2019-09-27 10:55:53 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 595 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) { |
Ilya Biryukov | 4ae2381 | 2019-09-27 17:55:46 +0000 | [diff] [blame] | 596 | // We must ensure template type aliases are included in results if they |
| 597 | // were written in the source code, e.g. in |
| 598 | // template <class T> using valias = vector<T>; |
| 599 | // ^valias<int> x; |
| 600 | // 'explicitReferenceTargets' will return: |
| 601 | // 1. valias with mask 'Alias'. |
| 602 | // 2. 'vector<int>' with mask 'Underlying'. |
| 603 | // we want to return only #1 in this case. |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 604 | Ref = ReferenceLoc{ |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 605 | NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false, |
Ilya Biryukov | 4ae2381 | 2019-09-27 17:55:46 +0000 | [diff] [blame] | 606 | explicitReferenceTargets(DynTypedNode::create(L.getType()), |
| 607 | DeclRelation::Alias)}; |
| 608 | } |
| 609 | void VisitDeducedTemplateSpecializationTypeLoc( |
| 610 | DeducedTemplateSpecializationTypeLoc L) { |
| 611 | Ref = ReferenceLoc{ |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 612 | NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false, |
Ilya Biryukov | 4ae2381 | 2019-09-27 17:55:46 +0000 | [diff] [blame] | 613 | explicitReferenceTargets(DynTypedNode::create(L.getType()), |
| 614 | DeclRelation::Alias)}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | void VisitDependentTemplateSpecializationTypeLoc( |
| 618 | DependentTemplateSpecializationTypeLoc L) { |
| 619 | Ref = ReferenceLoc{ |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 620 | L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false, |
Sam McCall | acc4ffb | 2020-01-02 17:59:10 +0100 | [diff] [blame] | 621 | explicitReferenceTargets(DynTypedNode::create(L.getType()), {})}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void VisitDependentNameTypeLoc(DependentNameTypeLoc L) { |
| 625 | Ref = ReferenceLoc{ |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 626 | L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false, |
Sam McCall | acc4ffb | 2020-01-02 17:59:10 +0100 | [diff] [blame] | 627 | explicitReferenceTargets(DynTypedNode::create(L.getType()), {})}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void VisitTypedefTypeLoc(TypedefTypeLoc L) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 631 | Ref = ReferenceLoc{NestedNameSpecifierLoc(), |
| 632 | L.getNameLoc(), |
| 633 | /*IsDecl=*/false, |
| 634 | {L.getTypedefNameDecl()}}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 635 | } |
| 636 | }; |
| 637 | |
| 638 | Visitor V; |
| 639 | V.Visit(L.getUnqualifiedLoc()); |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 640 | if (!V.Ref) |
| 641 | return {}; |
| 642 | return {*V.Ref}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 645 | class ExplicitReferenceCollector |
| 646 | : public RecursiveASTVisitor<ExplicitReferenceCollector> { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 647 | public: |
Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 648 | ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out) |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 649 | : Out(Out) { |
| 650 | assert(Out); |
| 651 | } |
| 652 | |
| 653 | bool VisitTypeLoc(TypeLoc TTL) { |
| 654 | if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding())) |
| 655 | return true; |
| 656 | visitNode(DynTypedNode::create(TTL)); |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) { |
| 661 | // ElaboratedTypeLoc will reports information for its inner type loc. |
| 662 | // Otherwise we loose information about inner types loc's qualifier. |
| 663 | TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc(); |
| 664 | TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding()); |
| 665 | return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L); |
| 666 | } |
| 667 | |
| 668 | bool VisitExpr(Expr *E) { |
| 669 | visitNode(DynTypedNode::create(*E)); |
| 670 | return true; |
| 671 | } |
| 672 | |
Ilya Biryukov | 1d32da8 | 2019-10-01 10:02:23 +0000 | [diff] [blame] | 673 | // We re-define Traverse*, since there's no corresponding Visit*. |
| 674 | // TemplateArgumentLoc is the only way to get locations for references to |
| 675 | // template template parameters. |
| 676 | bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) { |
| 677 | switch (A.getArgument().getKind()) { |
| 678 | case TemplateArgument::Template: |
| 679 | case TemplateArgument::TemplateExpansion: |
| 680 | reportReference(ReferenceLoc{A.getTemplateQualifierLoc(), |
| 681 | A.getTemplateNameLoc(), |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 682 | /*IsDecl=*/false, |
Ilya Biryukov | 1d32da8 | 2019-10-01 10:02:23 +0000 | [diff] [blame] | 683 | {A.getArgument() |
| 684 | .getAsTemplateOrTemplatePattern() |
| 685 | .getAsTemplateDecl()}}, |
| 686 | DynTypedNode::create(A.getArgument())); |
| 687 | break; |
| 688 | case TemplateArgument::Declaration: |
| 689 | break; // FIXME: can this actually happen in TemplateArgumentLoc? |
| 690 | case TemplateArgument::Integral: |
| 691 | case TemplateArgument::Null: |
| 692 | case TemplateArgument::NullPtr: |
| 693 | break; // no references. |
| 694 | case TemplateArgument::Pack: |
| 695 | case TemplateArgument::Type: |
| 696 | case TemplateArgument::Expression: |
| 697 | break; // Handled by VisitType and VisitExpression. |
| 698 | }; |
| 699 | return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A); |
| 700 | } |
| 701 | |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 702 | bool VisitDecl(Decl *D) { |
| 703 | visitNode(DynTypedNode::create(*D)); |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | // We have to use Traverse* because there is no corresponding Visit*. |
| 708 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) { |
| 709 | if (!L.getNestedNameSpecifier()) |
| 710 | return true; |
| 711 | visitNode(DynTypedNode::create(L)); |
| 712 | // Inner type is missing information about its qualifier, skip it. |
| 713 | if (auto TL = L.getTypeLoc()) |
| 714 | TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding()); |
| 715 | return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L); |
| 716 | } |
| 717 | |
Haojian Wu | 13fc899 | 2019-10-21 10:11:30 +0200 | [diff] [blame] | 718 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { |
| 719 | visitNode(DynTypedNode::create(*Init)); |
| 720 | return RecursiveASTVisitor::TraverseConstructorInitializer(Init); |
| 721 | } |
| 722 | |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 723 | private: |
| 724 | /// Obtain information about a reference directly defined in \p N. Does not |
| 725 | /// recurse into child nodes, e.g. do not expect references for constructor |
| 726 | /// initializers |
| 727 | /// |
| 728 | /// Any of the fields in the returned structure can be empty, but not all of |
| 729 | /// them, e.g. |
| 730 | /// - for implicitly generated nodes (e.g. MemberExpr from range-based-for), |
| 731 | /// source location information may be missing, |
| 732 | /// - for dependent code, targets may be empty. |
| 733 | /// |
| 734 | /// (!) For the purposes of this function declarations are not considered to |
| 735 | /// be references. However, declarations can have references inside them, |
| 736 | /// e.g. 'namespace foo = std' references namespace 'std' and this |
| 737 | /// function will return the corresponding reference. |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 738 | llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 739 | if (auto *D = N.get<Decl>()) |
| 740 | return refInDecl(D); |
| 741 | if (auto *E = N.get<Expr>()) |
| 742 | return refInExpr(E); |
Ilya Biryukov | 733777a | 2019-10-31 11:58:57 +0100 | [diff] [blame] | 743 | if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) { |
| 744 | // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases. |
| 745 | return {ReferenceLoc{ |
| 746 | NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false, |
| 747 | explicitReferenceTargets( |
| 748 | DynTypedNode::create(*NNSL->getNestedNameSpecifier()), |
| 749 | DeclRelation::Alias)}}; |
| 750 | } |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 751 | if (const TypeLoc *TL = N.get<TypeLoc>()) |
| 752 | return refInTypeLoc(*TL); |
| 753 | if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) { |
Haojian Wu | 13fc899 | 2019-10-21 10:11:30 +0200 | [diff] [blame] | 754 | // Other type initializers (e.g. base initializer) are handled by visiting |
| 755 | // the typeLoc. |
| 756 | if (CCI->isAnyMemberInitializer()) { |
| 757 | return {ReferenceLoc{NestedNameSpecifierLoc(), |
| 758 | CCI->getMemberLocation(), |
| 759 | /*IsDecl=*/false, |
| 760 | {CCI->getAnyMember()}}}; |
| 761 | } |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 762 | } |
| 763 | // We do not have location information for other nodes (QualType, etc) |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 764 | return {}; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void visitNode(DynTypedNode N) { |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 768 | for (const auto &R : explicitReference(N)) |
| 769 | reportReference(R, N); |
Ilya Biryukov | 1d32da8 | 2019-10-01 10:02:23 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void reportReference(const ReferenceLoc &Ref, DynTypedNode N) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 773 | // Our promise is to return only references from the source code. If we lack |
| 774 | // location information, skip these nodes. |
| 775 | // Normally this should not happen in practice, unless there are bugs in the |
| 776 | // traversals or users started the traversal at an implicit node. |
Ilya Biryukov | 1d32da8 | 2019-10-01 10:02:23 +0000 | [diff] [blame] | 777 | if (Ref.NameLoc.isInvalid()) { |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 778 | dlog("invalid location at node {0}", nodeToString(N)); |
| 779 | return; |
| 780 | } |
Ilya Biryukov | 1d32da8 | 2019-10-01 10:02:23 +0000 | [diff] [blame] | 781 | Out(Ref); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | llvm::function_ref<void(ReferenceLoc)> Out; |
| 785 | /// TypeLocs starting at these locations must be skipped, see |
| 786 | /// TraverseElaboratedTypeSpecifierLoc for details. |
| 787 | llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip; |
| 788 | }; |
| 789 | } // namespace |
| 790 | |
Kadir Cetinkaya | 007e4fe | 2019-09-25 15:44:26 +0000 | [diff] [blame] | 791 | void findExplicitReferences(const Stmt *S, |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 792 | llvm::function_ref<void(ReferenceLoc)> Out) { |
| 793 | assert(S); |
Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 794 | ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S)); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 795 | } |
Kadir Cetinkaya | 007e4fe | 2019-09-25 15:44:26 +0000 | [diff] [blame] | 796 | void findExplicitReferences(const Decl *D, |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 797 | llvm::function_ref<void(ReferenceLoc)> Out) { |
| 798 | assert(D); |
Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 799 | ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D)); |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 800 | } |
Ilya Biryukov | 87e0cb4 | 2019-11-05 19:06:12 +0100 | [diff] [blame] | 801 | void findExplicitReferences(const ASTContext &AST, |
| 802 | llvm::function_ref<void(ReferenceLoc)> Out) { |
Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 803 | ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST)); |
Ilya Biryukov | 87e0cb4 | 2019-11-05 19:06:12 +0100 | [diff] [blame] | 804 | } |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 805 | |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 806 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) { |
| 807 | switch (R) { |
| 808 | #define REL_CASE(X) \ |
| 809 | case DeclRelation::X: \ |
| 810 | return OS << #X; |
| 811 | REL_CASE(Alias); |
| 812 | REL_CASE(Underlying); |
| 813 | REL_CASE(TemplateInstantiation); |
| 814 | REL_CASE(TemplatePattern); |
| 815 | #undef REL_CASE |
Michael Liao | de52403 | 2019-09-03 15:02:46 +0000 | [diff] [blame] | 816 | } |
Simon Pilgrim | 54b989c | 2019-09-03 13:05:13 +0000 | [diff] [blame] | 817 | llvm_unreachable("Unhandled DeclRelation enum"); |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 818 | } |
| 819 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) { |
| 820 | const char *Sep = ""; |
| 821 | for (unsigned I = 0; I < RS.S.size(); ++I) { |
| 822 | if (RS.S.test(I)) { |
| 823 | OS << Sep << static_cast<DeclRelation>(I); |
| 824 | Sep = "|"; |
| 825 | } |
| 826 | } |
| 827 | return OS; |
| 828 | } |
| 829 | |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 830 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) { |
| 831 | // note we cannot print R.NameLoc without a source manager. |
| 832 | OS << "targets = {"; |
| 833 | bool First = true; |
| 834 | for (const NamedDecl *T : R.Targets) { |
| 835 | if (!First) |
| 836 | OS << ", "; |
| 837 | else |
| 838 | First = false; |
| 839 | OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T); |
| 840 | } |
| 841 | OS << "}"; |
| 842 | if (R.Qualifier) { |
| 843 | OS << ", qualifier = '"; |
| 844 | R.Qualifier.getNestedNameSpecifier()->print(OS, |
| 845 | PrintingPolicy(LangOptions())); |
| 846 | OS << "'"; |
| 847 | } |
Haojian Wu | 65f61c0 | 2019-10-18 12:07:19 +0000 | [diff] [blame] | 848 | if (R.IsDecl) |
| 849 | OS << ", decl"; |
Ilya Biryukov | f96d2e1 | 2019-09-25 12:40:22 +0000 | [diff] [blame] | 850 | return OS; |
| 851 | } |
| 852 | |
Sam McCall | 489cc58 | 2019-09-03 11:35:50 +0000 | [diff] [blame] | 853 | } // namespace clangd |
| 854 | } // namespace clang |