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