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