Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 1 | //===- IndexingContext.cpp - Indexing context data ------------------------===// |
| 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/ASTContext.h" |
| 13 | #include "clang/AST/DeclTemplate.h" |
| 14 | #include "clang/AST/DeclObjC.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using namespace index; |
| 19 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 20 | static bool isGeneratedDecl(const Decl *D) { |
| 21 | if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) { |
| 22 | return attr->getGeneratedDeclaration(); |
| 23 | } |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | bool IndexingContext::shouldIndex(const Decl *D) { |
| 28 | return !isGeneratedDecl(D); |
| 29 | } |
| 30 | |
Alex Lorenz | a352ba0 | 2017-04-24 14:04:58 +0000 | [diff] [blame] | 31 | const LangOptions &IndexingContext::getLangOpts() const { |
| 32 | return Ctx->getLangOpts(); |
| 33 | } |
| 34 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 35 | bool IndexingContext::shouldIndexFunctionLocalSymbols() const { |
| 36 | return IndexOpts.IndexFunctionLocals; |
| 37 | } |
| 38 | |
| 39 | bool IndexingContext::handleDecl(const Decl *D, |
| 40 | SymbolRoleSet Roles, |
| 41 | ArrayRef<SymbolRelation> Relations) { |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 42 | return handleDecl(D, D->getLocation(), Roles, Relations); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool IndexingContext::handleDecl(const Decl *D, SourceLocation Loc, |
| 46 | SymbolRoleSet Roles, |
| 47 | ArrayRef<SymbolRelation> Relations, |
| 48 | const DeclContext *DC) { |
| 49 | if (!DC) |
| 50 | DC = D->getDeclContext(); |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 51 | |
| 52 | const Decl *OrigD = D; |
| 53 | if (isa<ObjCPropertyImplDecl>(D)) { |
| 54 | D = cast<ObjCPropertyImplDecl>(D)->getPropertyDecl(); |
| 55 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 56 | return handleDeclOccurrence(D, Loc, /*IsRef=*/false, cast<Decl>(DC), |
| 57 | Roles, Relations, |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 58 | nullptr, OrigD, DC); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, |
| 62 | const NamedDecl *Parent, |
| 63 | const DeclContext *DC, |
| 64 | SymbolRoleSet Roles, |
| 65 | ArrayRef<SymbolRelation> Relations, |
| 66 | const Expr *RefE, |
| 67 | const Decl *RefD) { |
Argyrios Kyrtzidis | 6d1a15b2 | 2017-02-26 05:37:56 +0000 | [diff] [blame] | 68 | if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D)) |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 69 | return true; |
| 70 | |
| 71 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D)) |
| 72 | return true; |
| 73 | |
| 74 | return handleDeclOccurrence(D, Loc, /*IsRef=*/true, Parent, Roles, Relations, |
| 75 | RefE, RefD, DC); |
| 76 | } |
| 77 | |
| 78 | bool IndexingContext::importedModule(const ImportDecl *ImportD) { |
Argyrios Kyrtzidis | 113387e | 2016-02-29 07:56:07 +0000 | [diff] [blame] | 79 | SourceLocation Loc; |
| 80 | auto IdLocs = ImportD->getIdentifierLocs(); |
| 81 | if (!IdLocs.empty()) |
| 82 | Loc = IdLocs.front(); |
| 83 | else |
| 84 | Loc = ImportD->getLocation(); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 85 | SourceManager &SM = Ctx->getSourceManager(); |
| 86 | Loc = SM.getFileLoc(Loc); |
| 87 | if (Loc.isInvalid()) |
| 88 | return true; |
| 89 | |
| 90 | FileID FID; |
| 91 | unsigned Offset; |
| 92 | std::tie(FID, Offset) = SM.getDecomposedLoc(Loc); |
| 93 | if (FID.isInvalid()) |
| 94 | return true; |
| 95 | |
| 96 | bool Invalid = false; |
| 97 | const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid); |
| 98 | if (Invalid || !SEntry.isFile()) |
| 99 | return true; |
| 100 | |
| 101 | if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) { |
| 102 | switch (IndexOpts.SystemSymbolFilter) { |
| 103 | case IndexingOptions::SystemSymbolFilterKind::None: |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 104 | return true; |
Ben Langmuir | 6aed6b4 | 2016-07-14 18:51:55 +0000 | [diff] [blame] | 105 | case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly: |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 106 | case IndexingOptions::SystemSymbolFilterKind::All: |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
Ben Langmuir | 6aed6b4 | 2016-07-14 18:51:55 +0000 | [diff] [blame] | 111 | SymbolRoleSet Roles = (unsigned)SymbolRole::Declaration; |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 112 | if (ImportD->isImplicit()) |
| 113 | Roles |= (unsigned)SymbolRole::Implicit; |
| 114 | |
| 115 | return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset); |
| 116 | } |
| 117 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 118 | bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) { |
| 119 | TemplateSpecializationKind TKind = TSK_Undeclared; |
| 120 | if (const ClassTemplateSpecializationDecl * |
| 121 | SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 122 | TKind = SD->getSpecializationKind(); |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 123 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 124 | TKind = FD->getTemplateSpecializationKind(); |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 125 | } else if (auto *VD = dyn_cast<VarDecl>(D)) { |
| 126 | TKind = VD->getTemplateSpecializationKind(); |
Alex Lorenz | 2109d43 | 2017-05-23 16:23:28 +0000 | [diff] [blame] | 127 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 128 | if (RD->getInstantiatedFromMemberClass()) |
| 129 | TKind = RD->getTemplateSpecializationKind(); |
Alex Lorenz | 9d60520 | 2017-05-23 16:35:50 +0000 | [diff] [blame^] | 130 | } else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
| 131 | if (ED->getInstantiatedFromMemberEnum()) |
| 132 | TKind = ED->getTemplateSpecializationKind(); |
Alex Lorenz | fcbae3a | 2017-05-23 16:27:42 +0000 | [diff] [blame] | 133 | } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) { |
Alex Lorenz | 73e27a6 | 2017-05-23 16:25:06 +0000 | [diff] [blame] | 134 | if (const auto *Parent = dyn_cast<Decl>(D->getDeclContext())) |
| 135 | return isTemplateImplicitInstantiation(Parent); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 136 | } |
| 137 | switch (TKind) { |
| 138 | case TSK_Undeclared: |
| 139 | case TSK_ExplicitSpecialization: |
| 140 | return false; |
| 141 | case TSK_ImplicitInstantiation: |
| 142 | case TSK_ExplicitInstantiationDeclaration: |
| 143 | case TSK_ExplicitInstantiationDefinition: |
| 144 | return true; |
| 145 | } |
Saleem Abdulrasool | 9b0ac33 | 2016-02-15 00:36:52 +0000 | [diff] [blame] | 146 | llvm_unreachable("invalid TemplateSpecializationKind"); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) { |
| 150 | if (isa<ObjCInterfaceDecl>(D)) |
| 151 | return false; |
| 152 | if (isa<ObjCCategoryDecl>(D)) |
| 153 | return false; |
| 154 | if (isa<ObjCIvarDecl>(D)) |
| 155 | return false; |
| 156 | if (isa<ObjCMethodDecl>(D)) |
| 157 | return false; |
| 158 | if (isa<ImportDecl>(D)) |
| 159 | return false; |
| 160 | return true; |
| 161 | } |
| 162 | |
Alex Lorenz | 73e27a6 | 2017-05-23 16:25:06 +0000 | [diff] [blame] | 163 | static const CXXRecordDecl * |
| 164 | getDeclContextForTemplateInstationPattern(const Decl *D) { |
| 165 | if (const auto *CTSD = |
| 166 | dyn_cast<ClassTemplateSpecializationDecl>(D->getDeclContext())) |
| 167 | return CTSD->getTemplateInstantiationPattern(); |
| 168 | else if (const auto *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext())) |
| 169 | return RD->getInstantiatedFromMemberClass(); |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 173 | static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) { |
| 174 | if (const ClassTemplateSpecializationDecl * |
| 175 | SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 176 | return SD->getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 177 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 178 | return FD->getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 179 | } else if (auto *VD = dyn_cast<VarDecl>(D)) { |
| 180 | return VD->getTemplateInstantiationPattern(); |
Alex Lorenz | 2109d43 | 2017-05-23 16:23:28 +0000 | [diff] [blame] | 181 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 182 | return RD->getInstantiatedFromMemberClass(); |
Alex Lorenz | 9d60520 | 2017-05-23 16:35:50 +0000 | [diff] [blame^] | 183 | } else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
| 184 | return ED->getInstantiatedFromMemberEnum(); |
Alex Lorenz | fcbae3a | 2017-05-23 16:27:42 +0000 | [diff] [blame] | 185 | } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) { |
| 186 | const auto *ND = cast<NamedDecl>(D); |
Alex Lorenz | 73e27a6 | 2017-05-23 16:25:06 +0000 | [diff] [blame] | 187 | if (const CXXRecordDecl *Pattern = |
Alex Lorenz | fcbae3a | 2017-05-23 16:27:42 +0000 | [diff] [blame] | 188 | getDeclContextForTemplateInstationPattern(ND)) { |
| 189 | for (const NamedDecl *BaseND : Pattern->lookup(ND->getDeclName())) { |
| 190 | if (BaseND->isImplicit()) |
Alex Lorenz | 048c8a9 | 2017-05-15 14:26:22 +0000 | [diff] [blame] | 191 | continue; |
Alex Lorenz | fcbae3a | 2017-05-23 16:27:42 +0000 | [diff] [blame] | 192 | if (BaseND->getKind() == ND->getKind()) |
| 193 | return BaseND; |
Alex Lorenz | 048c8a9 | 2017-05-15 14:26:22 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 196 | } |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Argyrios Kyrtzidis | 66c49f7 | 2016-03-31 20:18:22 +0000 | [diff] [blame] | 200 | static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, ASTContext &Ctx) { |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 201 | if (auto VD = dyn_cast<VarDecl>(D)) |
| 202 | return VD->isThisDeclarationADefinition(Ctx); |
| 203 | |
| 204 | if (auto FD = dyn_cast<FunctionDecl>(D)) |
| 205 | return FD->isThisDeclarationADefinition(); |
| 206 | |
| 207 | if (auto TD = dyn_cast<TagDecl>(D)) |
| 208 | return TD->isThisDeclarationADefinition(); |
| 209 | |
| 210 | if (auto MD = dyn_cast<ObjCMethodDecl>(D)) |
Argyrios Kyrtzidis | 66c49f7 | 2016-03-31 20:18:22 +0000 | [diff] [blame] | 211 | return MD->isThisDeclarationADefinition() || isa<ObjCImplDecl>(ContainerDC); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 212 | |
| 213 | if (isa<TypedefNameDecl>(D) || |
| 214 | isa<EnumConstantDecl>(D) || |
| 215 | isa<FieldDecl>(D) || |
| 216 | isa<MSPropertyDecl>(D) || |
| 217 | isa<ObjCImplDecl>(D) || |
| 218 | isa<ObjCPropertyImplDecl>(D)) |
| 219 | return true; |
| 220 | |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | static const Decl *adjustParent(const Decl *Parent) { |
| 225 | if (!Parent) |
| 226 | return nullptr; |
| 227 | for (;; Parent = cast<Decl>(Parent->getDeclContext())) { |
| 228 | if (isa<TranslationUnitDecl>(Parent)) |
| 229 | return nullptr; |
| 230 | if (isa<LinkageSpecDecl>(Parent) || isa<BlockDecl>(Parent)) |
| 231 | continue; |
| 232 | if (auto NS = dyn_cast<NamespaceDecl>(Parent)) { |
| 233 | if (NS->isAnonymousNamespace()) |
| 234 | continue; |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 235 | } else if (auto RD = dyn_cast<RecordDecl>(Parent)) { |
| 236 | if (RD->isAnonymousStructOrUnion()) |
| 237 | continue; |
| 238 | } else if (auto FD = dyn_cast<FieldDecl>(Parent)) { |
| 239 | if (FD->getDeclName().isEmpty()) |
| 240 | continue; |
| 241 | } |
| 242 | return Parent; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static const Decl *getCanonicalDecl(const Decl *D) { |
| 247 | D = D->getCanonicalDecl(); |
| 248 | if (auto TD = dyn_cast<TemplateDecl>(D)) { |
| 249 | D = TD->getTemplatedDecl(); |
| 250 | assert(D->isCanonicalDecl()); |
| 251 | } |
| 252 | |
| 253 | return D; |
| 254 | } |
| 255 | |
Argyrios Kyrtzidis | a9876ca | 2017-03-23 16:34:47 +0000 | [diff] [blame] | 256 | static bool shouldReportOccurrenceForSystemDeclOnlyMode( |
| 257 | bool IsRef, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations) { |
| 258 | if (!IsRef) |
| 259 | return true; |
| 260 | |
| 261 | auto acceptForRelation = [](SymbolRoleSet roles) -> bool { |
| 262 | bool accept = false; |
| 263 | applyForEachSymbolRoleInterruptible(roles, [&accept](SymbolRole r) -> bool { |
| 264 | switch (r) { |
| 265 | case SymbolRole::RelationChildOf: |
| 266 | case SymbolRole::RelationBaseOf: |
| 267 | case SymbolRole::RelationOverrideOf: |
| 268 | case SymbolRole::RelationExtendedBy: |
| 269 | case SymbolRole::RelationAccessorOf: |
| 270 | case SymbolRole::RelationIBTypeOf: |
| 271 | accept = true; |
| 272 | return false; |
| 273 | case SymbolRole::Declaration: |
| 274 | case SymbolRole::Definition: |
| 275 | case SymbolRole::Reference: |
| 276 | case SymbolRole::Read: |
| 277 | case SymbolRole::Write: |
| 278 | case SymbolRole::Call: |
| 279 | case SymbolRole::Dynamic: |
| 280 | case SymbolRole::AddressOf: |
| 281 | case SymbolRole::Implicit: |
| 282 | case SymbolRole::RelationReceivedBy: |
| 283 | case SymbolRole::RelationCalledBy: |
| 284 | case SymbolRole::RelationContainedBy: |
Alex Lorenz | f6071c3 | 2017-04-20 10:43:22 +0000 | [diff] [blame] | 285 | case SymbolRole::RelationSpecializationOf: |
Argyrios Kyrtzidis | a9876ca | 2017-03-23 16:34:47 +0000 | [diff] [blame] | 286 | return true; |
| 287 | } |
Simon Pilgrim | dfbf049 | 2017-03-24 16:59:14 +0000 | [diff] [blame] | 288 | llvm_unreachable("Unsupported SymbolRole value!"); |
Argyrios Kyrtzidis | a9876ca | 2017-03-23 16:34:47 +0000 | [diff] [blame] | 289 | }); |
| 290 | return accept; |
| 291 | }; |
| 292 | |
| 293 | for (auto &Rel : Relations) { |
| 294 | if (acceptForRelation(Rel.Roles)) |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 301 | bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc, |
| 302 | bool IsRef, const Decl *Parent, |
| 303 | SymbolRoleSet Roles, |
| 304 | ArrayRef<SymbolRelation> Relations, |
| 305 | const Expr *OrigE, |
| 306 | const Decl *OrigD, |
| 307 | const DeclContext *ContainerDC) { |
| 308 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 309 | return true; |
| 310 | if (!isa<NamedDecl>(D) || |
| 311 | (cast<NamedDecl>(D)->getDeclName().isEmpty() && |
| 312 | !isa<TagDecl>(D) && !isa<ObjCCategoryDecl>(D))) |
| 313 | return true; |
| 314 | |
| 315 | SourceManager &SM = Ctx->getSourceManager(); |
| 316 | Loc = SM.getFileLoc(Loc); |
| 317 | if (Loc.isInvalid()) |
| 318 | return true; |
| 319 | |
| 320 | FileID FID; |
| 321 | unsigned Offset; |
| 322 | std::tie(FID, Offset) = SM.getDecomposedLoc(Loc); |
| 323 | if (FID.isInvalid()) |
| 324 | return true; |
| 325 | |
| 326 | bool Invalid = false; |
| 327 | const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid); |
| 328 | if (Invalid || !SEntry.isFile()) |
| 329 | return true; |
| 330 | |
| 331 | if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) { |
| 332 | switch (IndexOpts.SystemSymbolFilter) { |
| 333 | case IndexingOptions::SystemSymbolFilterKind::None: |
| 334 | return true; |
| 335 | case IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly: |
Argyrios Kyrtzidis | a9876ca | 2017-03-23 16:34:47 +0000 | [diff] [blame] | 336 | if (!shouldReportOccurrenceForSystemDeclOnlyMode(IsRef, Roles, Relations)) |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 337 | return true; |
| 338 | break; |
| 339 | case IndexingOptions::SystemSymbolFilterKind::All: |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (isTemplateImplicitInstantiation(D)) { |
| 345 | if (!IsRef) |
| 346 | return true; |
| 347 | D = adjustTemplateImplicitInstantiation(D); |
| 348 | if (!D) |
| 349 | return true; |
| 350 | assert(!isTemplateImplicitInstantiation(D)); |
| 351 | } |
| 352 | |
| 353 | if (!OrigD) |
| 354 | OrigD = D; |
| 355 | |
| 356 | if (IsRef) |
| 357 | Roles |= (unsigned)SymbolRole::Reference; |
Argyrios Kyrtzidis | 7479048 | 2017-02-17 04:49:41 +0000 | [diff] [blame] | 358 | else if (isDeclADefinition(OrigD, ContainerDC, *Ctx)) |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 359 | Roles |= (unsigned)SymbolRole::Definition; |
| 360 | else |
| 361 | Roles |= (unsigned)SymbolRole::Declaration; |
| 362 | |
| 363 | D = getCanonicalDecl(D); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 364 | Parent = adjustParent(Parent); |
| 365 | if (Parent) |
| 366 | Parent = getCanonicalDecl(Parent); |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 367 | |
| 368 | SmallVector<SymbolRelation, 6> FinalRelations; |
| 369 | FinalRelations.reserve(Relations.size()+1); |
| 370 | |
| 371 | auto addRelation = [&](SymbolRelation Rel) { |
| 372 | auto It = std::find_if(FinalRelations.begin(), FinalRelations.end(), |
| 373 | [&](SymbolRelation Elem)->bool { |
| 374 | return Elem.RelatedSymbol == Rel.RelatedSymbol; |
| 375 | }); |
| 376 | if (It != FinalRelations.end()) { |
| 377 | It->Roles |= Rel.Roles; |
| 378 | } else { |
| 379 | FinalRelations.push_back(Rel); |
| 380 | } |
| 381 | Roles |= Rel.Roles; |
| 382 | }; |
| 383 | |
Argyrios Kyrtzidis | df60aa8 | 2017-01-11 20:51:10 +0000 | [diff] [blame] | 384 | if (Parent) { |
Argyrios Kyrtzidis | 6d1a15b2 | 2017-02-26 05:37:56 +0000 | [diff] [blame] | 385 | if (IsRef || (!isa<ParmVarDecl>(D) && isFunctionLocalSymbol(D))) { |
Argyrios Kyrtzidis | de0f508 | 2017-01-11 21:01:07 +0000 | [diff] [blame] | 386 | addRelation(SymbolRelation{ |
| 387 | (unsigned)SymbolRole::RelationContainedBy, |
| 388 | Parent |
| 389 | }); |
Argyrios Kyrtzidis | 6d1a15b2 | 2017-02-26 05:37:56 +0000 | [diff] [blame] | 390 | } else { |
Argyrios Kyrtzidis | de0f508 | 2017-01-11 21:01:07 +0000 | [diff] [blame] | 391 | addRelation(SymbolRelation{ |
| 392 | (unsigned)SymbolRole::RelationChildOf, |
| 393 | Parent |
| 394 | }); |
Argyrios Kyrtzidis | df60aa8 | 2017-01-11 20:51:10 +0000 | [diff] [blame] | 395 | } |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 396 | } |
Argyrios Kyrtzidis | df60aa8 | 2017-01-11 20:51:10 +0000 | [diff] [blame] | 397 | |
Argyrios Kyrtzidis | f4fb85b | 2016-02-12 23:10:59 +0000 | [diff] [blame] | 398 | for (auto &Rel : Relations) { |
| 399 | addRelation(SymbolRelation(Rel.Roles, |
| 400 | Rel.RelatedSymbol->getCanonicalDecl())); |
| 401 | } |
| 402 | |
| 403 | IndexDataConsumer::ASTNodeInfo Node{ OrigE, OrigD, Parent, ContainerDC }; |
| 404 | return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, FID, Offset, |
| 405 | Node); |
| 406 | } |