Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 1 | //===- IndexDecl.cpp - Indexing declarations ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "IndexingContext.h" |
| 11 | #include "clang/Index/IndexDataConsumer.h" |
| 12 | #include "clang/AST/DeclVisitor.h" |
| 13 | |
| 14 | using namespace clang; |
| 15 | using namespace index; |
| 16 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 17 | #define TRY_DECL(D,CALL_EXPR) \ |
| 18 | do { \ |
| 19 | if (!IndexCtx.shouldIndex(D)) return true; \ |
| 20 | if (!CALL_EXPR) \ |
| 21 | return false; \ |
| 22 | } while (0) |
| 23 | |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 24 | #define TRY_TO(CALL_EXPR) \ |
| 25 | do { \ |
| 26 | if (!CALL_EXPR) \ |
| 27 | return false; \ |
| 28 | } while (0) |
| 29 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> { |
| 33 | IndexingContext &IndexCtx; |
| 34 | |
| 35 | public: |
| 36 | explicit IndexingDeclVisitor(IndexingContext &indexCtx) |
| 37 | : IndexCtx(indexCtx) { } |
| 38 | |
| 39 | bool Handled = true; |
| 40 | |
| 41 | bool VisitDecl(const Decl *D) { |
| 42 | Handled = false; |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | /// \brief Returns true if the given method has been defined explicitly by the |
| 47 | /// user. |
| 48 | static bool hasUserDefined(const ObjCMethodDecl *D, |
| 49 | const ObjCImplDecl *Container) { |
| 50 | const ObjCMethodDecl *MD = Container->getMethod(D->getSelector(), |
| 51 | D->isInstanceMethod()); |
| 52 | return MD && !MD->isImplicit() && MD->isThisDeclarationADefinition(); |
| 53 | } |
| 54 | |
Alex Lorenz | d43f75b | 2017-05-12 16:32:26 +0000 | [diff] [blame] | 55 | void handleTemplateArgumentLoc(const TemplateArgumentLoc &TALoc, |
| 56 | const NamedDecl *Parent, |
| 57 | const DeclContext *DC) { |
| 58 | const TemplateArgumentLocInfo &LocInfo = TALoc.getLocInfo(); |
| 59 | switch (TALoc.getArgument().getKind()) { |
| 60 | case TemplateArgument::Expression: |
| 61 | IndexCtx.indexBody(LocInfo.getAsExpr(), Parent, DC); |
| 62 | break; |
| 63 | case TemplateArgument::Type: |
| 64 | IndexCtx.indexTypeSourceInfo(LocInfo.getAsTypeSourceInfo(), Parent, DC); |
| 65 | break; |
Alex Lorenz | 80cb549 | 2017-05-22 16:50:54 +0000 | [diff] [blame] | 66 | case TemplateArgument::Template: |
| 67 | case TemplateArgument::TemplateExpansion: |
| 68 | IndexCtx.indexNestedNameSpecifierLoc(TALoc.getTemplateQualifierLoc(), |
| 69 | Parent, DC); |
| 70 | if (const TemplateDecl *TD = TALoc.getArgument() |
| 71 | .getAsTemplateOrTemplatePattern() |
| 72 | .getAsTemplateDecl()) { |
| 73 | if (const NamedDecl *TTD = TD->getTemplatedDecl()) |
| 74 | IndexCtx.handleReference(TTD, TALoc.getTemplateNameLoc(), Parent, DC); |
| 75 | } |
| 76 | break; |
Alex Lorenz | d43f75b | 2017-05-12 16:32:26 +0000 | [diff] [blame] | 77 | default: |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 82 | void handleDeclarator(const DeclaratorDecl *D, |
Argyrios Kyrtzidis | de0f508 | 2017-01-11 21:01:07 +0000 | [diff] [blame] | 83 | const NamedDecl *Parent = nullptr, |
| 84 | bool isIBType = false) { |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 85 | if (!Parent) Parent = D; |
| 86 | |
Argyrios Kyrtzidis | de0f508 | 2017-01-11 21:01:07 +0000 | [diff] [blame] | 87 | IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent, |
| 88 | Parent->getLexicalDeclContext(), |
| 89 | /*isBase=*/false, isIBType); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 90 | IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent); |
| 91 | if (IndexCtx.shouldIndexFunctionLocalSymbols()) { |
| 92 | // Only index parameters in definitions, parameters in declarations are |
| 93 | // not useful. |
| 94 | if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) { |
| 95 | auto *DC = Parm->getDeclContext(); |
| 96 | if (auto *FD = dyn_cast<FunctionDecl>(DC)) { |
| 97 | if (FD->isThisDeclarationADefinition()) |
| 98 | IndexCtx.handleDecl(Parm); |
| 99 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(DC)) { |
| 100 | if (MD->isThisDeclarationADefinition()) |
| 101 | IndexCtx.handleDecl(Parm); |
| 102 | } else { |
| 103 | IndexCtx.handleDecl(Parm); |
| 104 | } |
| 105 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 106 | if (FD->isThisDeclarationADefinition()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 107 | for (auto PI : FD->parameters()) { |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 108 | IndexCtx.handleDecl(PI); |
| 109 | } |
| 110 | } |
| 111 | } |
Alex Lorenz | 365cb47 | 2017-05-22 15:17:44 +0000 | [diff] [blame] | 112 | } else { |
| 113 | // Index the default parameter value for function definitions. |
| 114 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 115 | if (FD->isThisDeclarationADefinition()) { |
| 116 | for (const auto *PV : FD->parameters()) { |
| 117 | if (PV->hasDefaultArg() && !PV->hasUninstantiatedDefaultArg() && |
| 118 | !PV->hasUnparsedDefaultArg()) |
| 119 | IndexCtx.indexBody(PV->getDefaultArg(), D); |
| 120 | } |
| 121 | } |
| 122 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 126 | bool handleObjCMethod(const ObjCMethodDecl *D, |
| 127 | const ObjCPropertyDecl *AssociatedProp = nullptr) { |
| 128 | SmallVector<SymbolRelation, 4> Relations; |
| 129 | SmallVector<const ObjCMethodDecl*, 4> Overriden; |
| 130 | |
| 131 | D->getOverriddenMethods(Overriden); |
| 132 | for(auto overridden: Overriden) { |
| 133 | Relations.emplace_back((unsigned) SymbolRole::RelationOverrideOf, |
| 134 | overridden); |
| 135 | } |
| 136 | if (AssociatedProp) |
| 137 | Relations.emplace_back((unsigned)SymbolRole::RelationAccessorOf, |
| 138 | AssociatedProp); |
| 139 | |
Argyrios Kyrtzidis | aee15fb | 2017-01-26 02:11:50 +0000 | [diff] [blame] | 140 | // getLocation() returns beginning token of a method declaration, but for |
| 141 | // indexing purposes we want to point to the base name. |
| 142 | SourceLocation MethodLoc = D->getSelectorStartLoc(); |
| 143 | if (MethodLoc.isInvalid()) |
| 144 | MethodLoc = D->getLocation(); |
| 145 | |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 146 | SourceLocation AttrLoc; |
| 147 | |
| 148 | // check for (getter=/setter=) |
| 149 | if (AssociatedProp) { |
| 150 | bool isGetter = !D->param_size(); |
| 151 | AttrLoc = isGetter ? |
| 152 | AssociatedProp->getGetterNameLoc(): |
| 153 | AssociatedProp->getSetterNameLoc(); |
| 154 | } |
| 155 | |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 156 | SymbolRoleSet Roles = (SymbolRoleSet)SymbolRole::Dynamic; |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 157 | if (D->isImplicit()) { |
| 158 | if (AttrLoc.isValid()) { |
| 159 | MethodLoc = AttrLoc; |
| 160 | } else { |
| 161 | Roles |= (SymbolRoleSet)SymbolRole::Implicit; |
| 162 | } |
| 163 | } else if (AttrLoc.isValid()) { |
| 164 | IndexCtx.handleReference(D, AttrLoc, cast<NamedDecl>(D->getDeclContext()), |
| 165 | D->getDeclContext(), 0); |
| 166 | } |
| 167 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 168 | TRY_DECL(D, IndexCtx.handleDecl(D, MethodLoc, Roles, Relations)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 169 | IndexCtx.indexTypeSourceInfo(D->getReturnTypeSourceInfo(), D); |
Argyrios Kyrtzidis | de0f508 | 2017-01-11 21:01:07 +0000 | [diff] [blame] | 170 | bool hasIBActionAndFirst = D->hasAttr<IBActionAttr>(); |
| 171 | for (const auto *I : D->parameters()) { |
| 172 | handleDeclarator(I, D, /*isIBType=*/hasIBActionAndFirst); |
| 173 | hasIBActionAndFirst = false; |
| 174 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 175 | |
| 176 | if (D->isThisDeclarationADefinition()) { |
| 177 | const Stmt *Body = D->getBody(); |
| 178 | if (Body) { |
| 179 | IndexCtx.indexBody(Body, D, D); |
| 180 | } |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 185 | /// Gather the declarations which the given declaration \D overrides in a |
| 186 | /// pseudo-override manner. |
| 187 | /// |
| 188 | /// Pseudo-overrides occur when a class template specialization declares |
| 189 | /// a declaration that has the same name as a similar declaration in the |
| 190 | /// non-specialized template. |
| 191 | void |
| 192 | gatherTemplatePseudoOverrides(const NamedDecl *D, |
| 193 | SmallVectorImpl<SymbolRelation> &Relations) { |
| 194 | if (!IndexCtx.getLangOpts().CPlusPlus) |
| 195 | return; |
| 196 | const auto *CTSD = |
| 197 | dyn_cast<ClassTemplateSpecializationDecl>(D->getLexicalDeclContext()); |
| 198 | if (!CTSD) |
| 199 | return; |
| 200 | llvm::PointerUnion<ClassTemplateDecl *, |
| 201 | ClassTemplatePartialSpecializationDecl *> |
| 202 | Template = CTSD->getSpecializedTemplateOrPartial(); |
| 203 | if (const auto *CTD = Template.dyn_cast<ClassTemplateDecl *>()) { |
| 204 | const CXXRecordDecl *Pattern = CTD->getTemplatedDecl(); |
| 205 | bool TypeOverride = isa<TypeDecl>(D); |
| 206 | for (const NamedDecl *ND : Pattern->lookup(D->getDeclName())) { |
| 207 | if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 208 | ND = CTD->getTemplatedDecl(); |
| 209 | if (ND->isImplicit()) |
| 210 | continue; |
| 211 | // Types can override other types. |
| 212 | if (!TypeOverride) { |
| 213 | if (ND->getKind() != D->getKind()) |
| 214 | continue; |
| 215 | } else if (!isa<TypeDecl>(ND)) |
| 216 | continue; |
| 217 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { |
| 218 | const auto *DFD = cast<FunctionDecl>(D); |
| 219 | // Function overrides are approximated using the number of parameters. |
| 220 | if (FD->getStorageClass() != DFD->getStorageClass() || |
| 221 | FD->getNumParams() != DFD->getNumParams()) |
| 222 | continue; |
| 223 | } |
| 224 | Relations.emplace_back( |
Alex Lorenz | 9e2fea1 | 2017-05-04 10:42:06 +0000 | [diff] [blame] | 225 | SymbolRoleSet(SymbolRole::RelationSpecializationOf), ND); |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 230 | bool VisitFunctionDecl(const FunctionDecl *D) { |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 231 | SymbolRoleSet Roles{}; |
| 232 | SmallVector<SymbolRelation, 4> Relations; |
| 233 | if (auto *CXXMD = dyn_cast<CXXMethodDecl>(D)) { |
| 234 | if (CXXMD->isVirtual()) |
| 235 | Roles |= (unsigned)SymbolRole::Dynamic; |
| 236 | for (auto I = CXXMD->begin_overridden_methods(), |
| 237 | E = CXXMD->end_overridden_methods(); I != E; ++I) { |
| 238 | Relations.emplace_back((unsigned)SymbolRole::RelationOverrideOf, *I); |
| 239 | } |
| 240 | } |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 241 | gatherTemplatePseudoOverrides(D, Relations); |
Alex Lorenz | 5bf767c | 2017-04-25 14:22:29 +0000 | [diff] [blame] | 242 | if (const auto *Base = D->getPrimaryTemplate()) |
| 243 | Relations.push_back( |
| 244 | SymbolRelation(SymbolRoleSet(SymbolRole::RelationSpecializationOf), |
| 245 | Base->getTemplatedDecl())); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 246 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 247 | TRY_DECL(D, IndexCtx.handleDecl(D, Roles, Relations)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 248 | handleDeclarator(D); |
| 249 | |
| 250 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) { |
Argyrios Kyrtzidis | 573624a | 2017-03-17 23:41:59 +0000 | [diff] [blame] | 251 | IndexCtx.handleReference(Ctor->getParent(), Ctor->getLocation(), |
| 252 | Ctor->getParent(), Ctor->getDeclContext()); |
| 253 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 254 | // Constructor initializers. |
| 255 | for (const auto *Init : Ctor->inits()) { |
| 256 | if (Init->isWritten()) { |
| 257 | IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D); |
| 258 | if (const FieldDecl *Member = Init->getAnyMember()) |
| 259 | IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D, |
| 260 | (unsigned)SymbolRole::Write); |
| 261 | IndexCtx.indexBody(Init->getInit(), D, D); |
| 262 | } |
| 263 | } |
Argyrios Kyrtzidis | 573624a | 2017-03-17 23:41:59 +0000 | [diff] [blame] | 264 | } else if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(D)) { |
| 265 | if (auto TypeNameInfo = Dtor->getNameInfo().getNamedTypeInfo()) { |
| 266 | IndexCtx.handleReference(Dtor->getParent(), |
| 267 | TypeNameInfo->getTypeLoc().getLocStart(), |
| 268 | Dtor->getParent(), Dtor->getDeclContext()); |
| 269 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 270 | } |
Alex Lorenz | d43f75b | 2017-05-12 16:32:26 +0000 | [diff] [blame] | 271 | // Template specialization arguments. |
| 272 | if (const ASTTemplateArgumentListInfo *TemplateArgInfo = |
| 273 | D->getTemplateSpecializationArgsAsWritten()) { |
| 274 | for (const auto &Arg : TemplateArgInfo->arguments()) |
| 275 | handleTemplateArgumentLoc(Arg, D, D->getLexicalDeclContext()); |
| 276 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 277 | |
| 278 | if (D->isThisDeclarationADefinition()) { |
| 279 | const Stmt *Body = D->getBody(); |
| 280 | if (Body) { |
| 281 | IndexCtx.indexBody(Body, D, D); |
| 282 | } |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | bool VisitVarDecl(const VarDecl *D) { |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 288 | SmallVector<SymbolRelation, 4> Relations; |
| 289 | gatherTemplatePseudoOverrides(D, Relations); |
| 290 | TRY_DECL(D, IndexCtx.handleDecl(D, SymbolRoleSet(), Relations)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 291 | handleDeclarator(D); |
| 292 | IndexCtx.indexBody(D->getInit(), D); |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool VisitFieldDecl(const FieldDecl *D) { |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 297 | SmallVector<SymbolRelation, 4> Relations; |
| 298 | gatherTemplatePseudoOverrides(D, Relations); |
| 299 | TRY_DECL(D, IndexCtx.handleDecl(D, SymbolRoleSet(), Relations)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 300 | handleDeclarator(D); |
| 301 | if (D->isBitField()) |
| 302 | IndexCtx.indexBody(D->getBitWidth(), D); |
| 303 | else if (D->hasInClassInitializer()) |
| 304 | IndexCtx.indexBody(D->getInClassInitializer(), D); |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | bool VisitObjCIvarDecl(const ObjCIvarDecl *D) { |
| 309 | if (D->getSynthesize()) { |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 310 | // handled in VisitObjCPropertyImplDecl |
| 311 | return true; |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 312 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 313 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 314 | handleDeclarator(D); |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | bool VisitMSPropertyDecl(const MSPropertyDecl *D) { |
| 319 | handleDeclarator(D); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | bool VisitEnumConstantDecl(const EnumConstantDecl *D) { |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 324 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 325 | IndexCtx.indexBody(D->getInitExpr(), D); |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | bool VisitTypedefNameDecl(const TypedefNameDecl *D) { |
Argyrios Kyrtzidis | f932612 | 2017-04-21 05:42:40 +0000 | [diff] [blame] | 330 | if (!D->isTransparentTag()) { |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 331 | SmallVector<SymbolRelation, 4> Relations; |
| 332 | gatherTemplatePseudoOverrides(D, Relations); |
| 333 | TRY_DECL(D, IndexCtx.handleDecl(D, SymbolRoleSet(), Relations)); |
Argyrios Kyrtzidis | f932612 | 2017-04-21 05:42:40 +0000 | [diff] [blame] | 334 | IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D); |
| 335 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
| 339 | bool VisitTagDecl(const TagDecl *D) { |
| 340 | // Non-free standing tags are handled in indexTypeSourceInfo. |
| 341 | if (D->isFreeStanding()) { |
| 342 | if (D->isThisDeclarationADefinition()) { |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 343 | SmallVector<SymbolRelation, 4> Relations; |
| 344 | gatherTemplatePseudoOverrides(D, Relations); |
| 345 | IndexCtx.indexTagDecl(D, Relations); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 346 | } else { |
| 347 | auto *Parent = dyn_cast<NamedDecl>(D->getDeclContext()); |
| 348 | return IndexCtx.handleReference(D, D->getLocation(), Parent, |
| 349 | D->getLexicalDeclContext(), |
| 350 | SymbolRoleSet()); |
| 351 | } |
| 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 356 | bool handleReferencedProtocols(const ObjCProtocolList &ProtList, |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 357 | const ObjCContainerDecl *ContD, |
| 358 | SourceLocation SuperLoc) { |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 359 | ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin(); |
| 360 | for (ObjCInterfaceDecl::protocol_iterator |
| 361 | I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) { |
| 362 | SourceLocation Loc = *LI; |
| 363 | ObjCProtocolDecl *PD = *I; |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 364 | SymbolRoleSet roles{}; |
| 365 | if (Loc == SuperLoc) |
| 366 | roles |= (SymbolRoleSet)SymbolRole::Implicit; |
| 367 | TRY_TO(IndexCtx.handleReference(PD, Loc, ContD, ContD, roles, |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 368 | SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, ContD})); |
| 369 | } |
| 370 | return true; |
| 371 | } |
| 372 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 373 | bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) { |
| 374 | if (D->isThisDeclarationADefinition()) { |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 375 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 376 | SourceLocation SuperLoc = D->getSuperClassLoc(); |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 377 | if (auto *SuperD = D->getSuperClass()) { |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 378 | bool hasSuperTypedef = false; |
| 379 | if (auto *TInfo = D->getSuperClassTInfo()) { |
| 380 | if (auto *TT = TInfo->getType()->getAs<TypedefType>()) { |
| 381 | if (auto *TD = TT->getDecl()) { |
| 382 | hasSuperTypedef = true; |
| 383 | TRY_TO(IndexCtx.handleReference(TD, SuperLoc, D, D, |
| 384 | SymbolRoleSet())); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | SymbolRoleSet superRoles{}; |
| 389 | if (hasSuperTypedef) |
| 390 | superRoles |= (SymbolRoleSet)SymbolRole::Implicit; |
| 391 | TRY_TO(IndexCtx.handleReference(SuperD, SuperLoc, D, D, superRoles, |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 392 | SymbolRelation{(unsigned)SymbolRole::RelationBaseOf, D})); |
| 393 | } |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 394 | TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D, |
| 395 | SuperLoc)); |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 396 | TRY_TO(IndexCtx.indexDeclContext(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 397 | } else { |
Argyrios Kyrtzidis | 542f38f | 2016-03-09 02:12:46 +0000 | [diff] [blame] | 398 | return IndexCtx.handleReference(D, D->getLocation(), nullptr, |
| 399 | D->getDeclContext(), SymbolRoleSet()); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) { |
| 405 | if (D->isThisDeclarationADefinition()) { |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 406 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 407 | TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D, |
| 408 | /*superLoc=*/SourceLocation())); |
Argyrios Kyrtzidis | 34a0c1a | 2016-03-03 05:33:54 +0000 | [diff] [blame] | 409 | TRY_TO(IndexCtx.indexDeclContext(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 410 | } else { |
Argyrios Kyrtzidis | 542f38f | 2016-03-09 02:12:46 +0000 | [diff] [blame] | 411 | return IndexCtx.handleReference(D, D->getLocation(), nullptr, |
| 412 | D->getDeclContext(), SymbolRoleSet()); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 413 | } |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) { |
| 418 | const ObjCInterfaceDecl *Class = D->getClassInterface(); |
| 419 | if (!Class) |
| 420 | return true; |
| 421 | |
| 422 | if (Class->isImplicitInterfaceDecl()) |
| 423 | IndexCtx.handleDecl(Class); |
| 424 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 425 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 426 | |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 427 | // Visit implicit @synthesize property implementations first as their |
| 428 | // location is reported at the name of the @implementation block. This |
| 429 | // serves no purpose other than to simplify the FileCheck-based tests. |
| 430 | for (const auto *I : D->property_impls()) { |
| 431 | if (I->getLocation().isInvalid()) |
| 432 | IndexCtx.indexDecl(I); |
| 433 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 434 | for (const auto *I : D->decls()) { |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 435 | if (!isa<ObjCPropertyImplDecl>(I) || |
| 436 | cast<ObjCPropertyImplDecl>(I)->getLocation().isValid()) |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 437 | IndexCtx.indexDecl(I); |
| 438 | } |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) { |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 444 | if (!IndexCtx.shouldIndex(D)) |
| 445 | return true; |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 446 | const ObjCInterfaceDecl *C = D->getClassInterface(); |
Argyrios Kyrtzidis | 3800431 | 2017-01-11 21:08:31 +0000 | [diff] [blame] | 447 | if (!C) |
| 448 | return true; |
| 449 | TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D, SymbolRoleSet(), |
| 450 | SymbolRelation{ |
| 451 | (unsigned)SymbolRole::RelationExtendedBy, D |
| 452 | })); |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 453 | SourceLocation CategoryLoc = D->getCategoryNameLoc(); |
| 454 | if (!CategoryLoc.isValid()) |
| 455 | CategoryLoc = D->getLocation(); |
| 456 | TRY_TO(IndexCtx.handleDecl(D, CategoryLoc)); |
Argyrios Kyrtzidis | 266cfa3 | 2017-03-04 17:54:53 +0000 | [diff] [blame] | 457 | TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D, |
| 458 | /*superLoc=*/SourceLocation())); |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 459 | TRY_TO(IndexCtx.indexDeclContext(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 460 | return true; |
| 461 | } |
| 462 | |
| 463 | bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) { |
| 464 | const ObjCCategoryDecl *Cat = D->getCategoryDecl(); |
| 465 | if (!Cat) |
| 466 | return true; |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 467 | const ObjCInterfaceDecl *C = D->getClassInterface(); |
| 468 | if (C) |
| 469 | TRY_TO(IndexCtx.handleReference(C, D->getLocation(), D, D, |
| 470 | SymbolRoleSet())); |
| 471 | SourceLocation CategoryLoc = D->getCategoryNameLoc(); |
| 472 | if (!CategoryLoc.isValid()) |
| 473 | CategoryLoc = D->getLocation(); |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 474 | TRY_DECL(D, IndexCtx.handleDecl(D, CategoryLoc)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 475 | IndexCtx.indexDeclContext(D); |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | bool VisitObjCMethodDecl(const ObjCMethodDecl *D) { |
| 480 | // Methods associated with a property, even user-declared ones, are |
| 481 | // handled when we handle the property. |
| 482 | if (D->isPropertyAccessor()) |
| 483 | return true; |
| 484 | |
| 485 | handleObjCMethod(D); |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { |
| 490 | if (ObjCMethodDecl *MD = D->getGetterMethodDecl()) |
| 491 | if (MD->getLexicalDeclContext() == D->getLexicalDeclContext()) |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 492 | handleObjCMethod(MD, D); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 493 | if (ObjCMethodDecl *MD = D->getSetterMethodDecl()) |
| 494 | if (MD->getLexicalDeclContext() == D->getLexicalDeclContext()) |
Argyrios Kyrtzidis | 806faaf | 2016-10-25 21:11:22 +0000 | [diff] [blame] | 495 | handleObjCMethod(MD, D); |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 496 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | de0f508 | 2017-01-11 21:01:07 +0000 | [diff] [blame] | 497 | if (IBOutletCollectionAttr *attr = D->getAttr<IBOutletCollectionAttr>()) |
| 498 | IndexCtx.indexTypeSourceInfo(attr->getInterfaceLoc(), D, |
| 499 | D->getLexicalDeclContext(), false, true); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 500 | IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D); |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { |
| 505 | ObjCPropertyDecl *PD = D->getPropertyDecl(); |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 506 | auto *Container = cast<ObjCImplDecl>(D->getDeclContext()); |
| 507 | SourceLocation Loc = D->getLocation(); |
| 508 | SymbolRoleSet Roles = 0; |
| 509 | SmallVector<SymbolRelation, 1> Relations; |
| 510 | |
| 511 | if (ObjCIvarDecl *ID = D->getPropertyIvarDecl()) |
| 512 | Relations.push_back({(SymbolRoleSet)SymbolRole::RelationAccessorOf, ID}); |
| 513 | if (Loc.isInvalid()) { |
| 514 | Loc = Container->getLocation(); |
| 515 | Roles |= (SymbolRoleSet)SymbolRole::Implicit; |
| 516 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 517 | TRY_DECL(D, IndexCtx.handleDecl(D, Loc, Roles, Relations)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 518 | |
| 519 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 520 | return true; |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 521 | |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 522 | assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize); |
Argyrios Kyrtzidis | e594707 | 2017-04-27 15:05:18 +0000 | [diff] [blame] | 523 | SymbolRoleSet AccessorMethodRoles = |
| 524 | SymbolRoleSet(SymbolRole::Dynamic) | SymbolRoleSet(SymbolRole::Implicit); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 525 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) { |
| 526 | if (MD->isPropertyAccessor() && |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 527 | !hasUserDefined(MD, Container)) |
Argyrios Kyrtzidis | e594707 | 2017-04-27 15:05:18 +0000 | [diff] [blame] | 528 | IndexCtx.handleDecl(MD, Loc, AccessorMethodRoles, {}, Container); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 529 | } |
| 530 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) { |
| 531 | if (MD->isPropertyAccessor() && |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 532 | !hasUserDefined(MD, Container)) |
Argyrios Kyrtzidis | e594707 | 2017-04-27 15:05:18 +0000 | [diff] [blame] | 533 | IndexCtx.handleDecl(MD, Loc, AccessorMethodRoles, {}, Container); |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 534 | } |
| 535 | if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) { |
| 536 | if (IvarD->getSynthesize()) { |
| 537 | // For synthesized ivars, use the location of its name in the |
| 538 | // corresponding @synthesize. If there isn't one, use the containing |
| 539 | // @implementation's location, rather than the property's location, |
| 540 | // otherwise the header file containing the @interface will have different |
| 541 | // indexing contents based on whether the @implementation was present or |
| 542 | // not in the translation unit. |
| 543 | SymbolRoleSet IvarRoles = 0; |
| 544 | SourceLocation IvarLoc = D->getPropertyIvarDeclLoc(); |
| 545 | if (D->getLocation().isInvalid()) { |
| 546 | IvarLoc = Container->getLocation(); |
| 547 | IvarRoles = (SymbolRoleSet)SymbolRole::Implicit; |
| 548 | } else if (D->getLocation() == IvarLoc) { |
| 549 | IvarRoles = (SymbolRoleSet)SymbolRole::Implicit; |
| 550 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 551 | TRY_DECL(IvarD, IndexCtx.handleDecl(IvarD, IvarLoc, IvarRoles)); |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 552 | } else { |
| 553 | IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), nullptr, |
| 554 | D->getDeclContext(), SymbolRoleSet()); |
| 555 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 556 | } |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | bool VisitNamespaceDecl(const NamespaceDecl *D) { |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 561 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 562 | IndexCtx.indexDeclContext(D); |
| 563 | return true; |
| 564 | } |
| 565 | |
Alex Lorenz | 4ab5193 | 2017-05-15 10:56:31 +0000 | [diff] [blame] | 566 | bool VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { |
| 567 | TRY_DECL(D, IndexCtx.handleDecl(D)); |
| 568 | IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D); |
| 569 | IndexCtx.handleReference(D->getAliasedNamespace(), D->getTargetNameLoc(), D, |
| 570 | D->getLexicalDeclContext()); |
| 571 | return true; |
| 572 | } |
| 573 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 574 | bool VisitUsingDecl(const UsingDecl *D) { |
| 575 | const DeclContext *DC = D->getDeclContext()->getRedeclContext(); |
| 576 | const NamedDecl *Parent = dyn_cast<NamedDecl>(DC); |
| 577 | |
| 578 | IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent, |
| 579 | D->getLexicalDeclContext()); |
| 580 | for (const auto *I : D->shadows()) |
| 581 | IndexCtx.handleReference(I->getUnderlyingDecl(), D->getLocation(), Parent, |
| 582 | D->getLexicalDeclContext(), SymbolRoleSet()); |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { |
| 587 | const DeclContext *DC = D->getDeclContext()->getRedeclContext(); |
| 588 | const NamedDecl *Parent = dyn_cast<NamedDecl>(DC); |
| 589 | |
Alex Lorenz | d65b3e4 | 2017-05-22 14:39:39 +0000 | [diff] [blame] | 590 | // NNS for the local 'using namespace' directives is visited by the body |
| 591 | // visitor. |
| 592 | if (!D->getParentFunctionOrMethod()) |
| 593 | IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent, |
| 594 | D->getLexicalDeclContext()); |
| 595 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 596 | return IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(), |
| 597 | D->getLocation(), Parent, |
| 598 | D->getLexicalDeclContext(), |
| 599 | SymbolRoleSet()); |
| 600 | } |
| 601 | |
| 602 | bool VisitClassTemplateSpecializationDecl(const |
| 603 | ClassTemplateSpecializationDecl *D) { |
| 604 | // FIXME: Notify subsequent callbacks if info comes from implicit |
| 605 | // instantiation. |
Alex Lorenz | f6071c3 | 2017-04-20 10:43:22 +0000 | [diff] [blame] | 606 | if (D->isThisDeclarationADefinition()) { |
| 607 | llvm::PointerUnion<ClassTemplateDecl *, |
| 608 | ClassTemplatePartialSpecializationDecl *> |
| 609 | Template = D->getSpecializedTemplateOrPartial(); |
| 610 | const Decl *SpecializationOf = |
| 611 | Template.is<ClassTemplateDecl *>() |
| 612 | ? (Decl *)Template.get<ClassTemplateDecl *>() |
| 613 | : Template.get<ClassTemplatePartialSpecializationDecl *>(); |
| 614 | IndexCtx.indexTagDecl( |
| 615 | D, SymbolRelation(SymbolRoleSet(SymbolRole::RelationSpecializationOf), |
| 616 | SpecializationOf)); |
| 617 | } |
Alex Lorenz | f41b21e | 2017-04-25 15:13:42 +0000 | [diff] [blame] | 618 | if (TypeSourceInfo *TSI = D->getTypeAsWritten()) |
| 619 | IndexCtx.indexTypeSourceInfo(TSI, /*Parent=*/nullptr, |
| 620 | D->getLexicalDeclContext()); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
Alex Lorenz | 80cb549 | 2017-05-22 16:50:54 +0000 | [diff] [blame] | 624 | static bool shouldIndexTemplateParameterDefaultValue(const NamedDecl *D) { |
| 625 | if (!D) |
| 626 | return false; |
| 627 | // We want to index the template parameters only once when indexing the |
| 628 | // canonical declaration. |
| 629 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 630 | return FD->getCanonicalDecl() == FD; |
| 631 | else if (const auto *TD = dyn_cast<TagDecl>(D)) |
| 632 | return TD->getCanonicalDecl() == TD; |
| 633 | else if (const auto *VD = dyn_cast<VarDecl>(D)) |
| 634 | return VD->getCanonicalDecl() == VD; |
| 635 | return true; |
| 636 | } |
| 637 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 638 | bool VisitTemplateDecl(const TemplateDecl *D) { |
| 639 | // FIXME: Template parameters. |
Alex Lorenz | 80cb549 | 2017-05-22 16:50:54 +0000 | [diff] [blame] | 640 | |
| 641 | // Index the default values for the template parameters. |
| 642 | const NamedDecl *Parent = D->getTemplatedDecl(); |
| 643 | if (D->getTemplateParameters() && |
| 644 | shouldIndexTemplateParameterDefaultValue(Parent)) { |
| 645 | const TemplateParameterList *Params = D->getTemplateParameters(); |
| 646 | for (const NamedDecl *TP : *Params) { |
| 647 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(TP)) { |
| 648 | if (TTP->hasDefaultArgument()) |
| 649 | IndexCtx.indexTypeSourceInfo(TTP->getDefaultArgumentInfo(), Parent); |
| 650 | } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TP)) { |
| 651 | if (NTTP->hasDefaultArgument()) |
| 652 | IndexCtx.indexBody(NTTP->getDefaultArgument(), Parent); |
| 653 | } else if (const auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(TP)) { |
| 654 | if (TTPD->hasDefaultArgument()) |
| 655 | handleTemplateArgumentLoc(TTPD->getDefaultArgument(), Parent, |
| 656 | /*DC=*/nullptr); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 661 | return Visit(D->getTemplatedDecl()); |
| 662 | } |
| 663 | |
| 664 | bool VisitFriendDecl(const FriendDecl *D) { |
| 665 | if (auto ND = D->getFriendDecl()) { |
| 666 | // FIXME: Ignore a class template in a dependent context, these are not |
| 667 | // linked properly with their redeclarations, ending up with duplicate |
| 668 | // USRs. |
| 669 | // See comment "Friend templates are visible in fairly strange ways." in |
| 670 | // SemaTemplate.cpp which precedes code that prevents the friend template |
| 671 | // from becoming visible from the enclosing context. |
| 672 | if (isa<ClassTemplateDecl>(ND) && D->getDeclContext()->isDependentContext()) |
| 673 | return true; |
| 674 | return Visit(ND); |
| 675 | } |
| 676 | if (auto Ty = D->getFriendType()) { |
| 677 | IndexCtx.indexTypeSourceInfo(Ty, cast<NamedDecl>(D->getDeclContext())); |
| 678 | } |
| 679 | return true; |
| 680 | } |
| 681 | |
| 682 | bool VisitImportDecl(const ImportDecl *D) { |
| 683 | return IndexCtx.importedModule(D); |
| 684 | } |
| 685 | }; |
| 686 | |
| 687 | } // anonymous namespace |
| 688 | |
| 689 | bool IndexingContext::indexDecl(const Decl *D) { |
| 690 | if (D->isImplicit() && shouldIgnoreIfImplicit(D)) |
| 691 | return true; |
| 692 | |
| 693 | if (isTemplateImplicitInstantiation(D)) |
| 694 | return true; |
| 695 | |
| 696 | IndexingDeclVisitor Visitor(*this); |
| 697 | bool ShouldContinue = Visitor.Visit(D); |
| 698 | if (!ShouldContinue) |
| 699 | return false; |
| 700 | |
| 701 | if (!Visitor.Handled && isa<DeclContext>(D)) |
| 702 | return indexDeclContext(cast<DeclContext>(D)); |
| 703 | |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | bool IndexingContext::indexDeclContext(const DeclContext *DC) { |
| 708 | for (const auto *I : DC->decls()) |
| 709 | if (!indexDecl(I)) |
| 710 | return false; |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | bool IndexingContext::indexTopLevelDecl(const Decl *D) { |
| 715 | if (D->getLocation().isInvalid()) |
| 716 | return true; |
| 717 | |
| 718 | if (isa<ObjCMethodDecl>(D)) |
| 719 | return true; // Wait for the objc container. |
| 720 | |
| 721 | return indexDecl(D); |
| 722 | } |
| 723 | |
| 724 | bool IndexingContext::indexDeclGroupRef(DeclGroupRef DG) { |
| 725 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 726 | if (!indexTopLevelDecl(*I)) |
| 727 | return false; |
| 728 | return true; |
| 729 | } |