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