Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- CIndexHigh.cpp - Higher level API functions ------------------------===// |
| 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 "CXTranslationUnit.h" |
| 12 | #include "CIndexDiagnostic.h" |
| 13 | |
| 14 | #include "clang/Frontend/ASTUnit.h" |
| 15 | #include "clang/AST/DeclObjC.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using namespace cxindex; |
| 19 | using namespace cxcursor; |
| 20 | |
| 21 | const char *IndexingContext::StrAdapter::toCStr(StringRef Str) { |
| 22 | if (Str.empty()) |
| 23 | return ""; |
| 24 | if (Str.data()[Str.size()] == '\0') |
| 25 | return Str.data(); |
| 26 | Scratch += Str; |
| 27 | Scratch.push_back('\0'); |
| 28 | return Scratch.data() + (Scratch.size() - Str.size() - 1); |
| 29 | } |
| 30 | |
| 31 | void IndexingContext::setASTContext(ASTContext &ctx) { |
| 32 | Ctx = &ctx; |
| 33 | static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx); |
| 34 | } |
| 35 | |
| 36 | void IndexingContext::ppIncludedFile(SourceLocation hashLoc, |
| 37 | StringRef filename, |
| 38 | const FileEntry *File, |
| 39 | bool isImport, bool isAngled) { |
| 40 | if (!CB.ppIncludedFile) |
| 41 | return; |
| 42 | |
| 43 | StrAdapter SA(this); |
| 44 | CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), |
| 45 | SA.toCStr(filename), |
| 46 | getIndexFile(File), |
| 47 | isImport, isAngled }; |
| 48 | CB.ppIncludedFile(ClientData, &Info); |
| 49 | } |
| 50 | |
| 51 | void IndexingContext::ppMacroDefined(SourceLocation Loc, StringRef Name, |
| 52 | SourceLocation DefBegin, unsigned Length, |
| 53 | const void *OpaqueMacro) { |
| 54 | if (!CB.ppMacroDefined) |
| 55 | return; |
| 56 | |
| 57 | StrAdapter SA(this); |
| 58 | CXIdxMacroInfo MacroInfo = { getIndexLoc(Loc), SA.toCStr(Name) }; |
| 59 | CXIdxMacroDefinedInfo Info = { &MacroInfo, |
| 60 | getIndexLoc(DefBegin), Length }; |
| 61 | CXIdxMacro idxMacro = CB.ppMacroDefined(ClientData, &Info); |
| 62 | MacroMap[OpaqueMacro] = idxMacro; |
| 63 | } |
| 64 | |
| 65 | void IndexingContext::ppMacroUndefined(SourceLocation Loc, StringRef Name, |
| 66 | const void *OpaqueMacro) { |
| 67 | if (!CB.ppMacroUndefined) |
| 68 | return; |
| 69 | |
| 70 | StrAdapter SA(this); |
| 71 | CXIdxMacroUndefinedInfo Info = { getIndexLoc(Loc), |
| 72 | SA.toCStr(Name), 0 }; |
| 73 | CB.ppMacroUndefined(ClientData, &Info); |
| 74 | } |
| 75 | |
| 76 | void IndexingContext::ppMacroExpanded(SourceLocation Loc, StringRef Name, |
| 77 | const void *OpaqueMacro) { |
| 78 | if (!CB.ppMacroExpanded) |
| 79 | return; |
| 80 | |
| 81 | StrAdapter SA(this); |
| 82 | CXIdxMacroExpandedInfo Info = { getIndexLoc(Loc), |
| 83 | SA.toCStr(Name), 0 }; |
| 84 | CB.ppMacroExpanded(ClientData, &Info); |
| 85 | } |
| 86 | |
| 87 | void IndexingContext::invokeStartedTranslationUnit() { |
| 88 | CXIdxContainer idxCont = 0; |
| 89 | if (CB.startedTranslationUnit) |
| 90 | idxCont = CB.startedTranslationUnit(ClientData, 0); |
| 91 | addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); |
| 92 | } |
| 93 | |
| 94 | void IndexingContext::invokeFinishedTranslationUnit() { |
| 95 | invokeEndedContainer(Ctx->getTranslationUnitDecl()); |
| 96 | } |
| 97 | |
| 98 | void IndexingContext::handleDiagnostic(const StoredDiagnostic &StoredDiag) { |
| 99 | if (!CB.diagnostic) |
| 100 | return; |
| 101 | |
| 102 | CXStoredDiagnostic CXDiag(StoredDiag, Ctx->getLangOptions()); |
| 103 | CB.diagnostic(ClientData, &CXDiag, 0); |
| 104 | } |
| 105 | |
| 106 | void IndexingContext::handleFunction(const FunctionDecl *D) { |
| 107 | StrAdapter SA(this); |
| 108 | |
| 109 | if (D->isFirstDeclaration()) { |
| 110 | CXIdxEntity idxEntity = 0; |
| 111 | if (CB.indexFunction) { |
| 112 | CXIdxEntityInfo EntityInfo; |
| 113 | CXIdxIndexedDeclInfo DeclInfo; |
| 114 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 115 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 116 | CXIdxFunctionInfo Info = { &IdxEntityInfo, |
| 117 | D->isThisDeclarationADefinition() }; |
| 118 | |
| 119 | idxEntity = CB.indexFunction(ClientData, &Info); |
| 120 | } |
| 121 | |
| 122 | addEntityInMap(D, idxEntity); |
| 123 | |
| 124 | } else { |
| 125 | if (CB.indexFunctionRedeclaration) { |
| 126 | CXIdxIndexedDeclInfo DeclInfo; |
| 127 | CXIdxIndexedRedeclInfo RedeclInfo; |
| 128 | getIndexedRedeclInfo(D, RedeclInfo, DeclInfo); |
| 129 | CXIdxFunctionRedeclInfo Info = { &RedeclInfo, |
| 130 | D->isThisDeclarationADefinition() }; |
| 131 | |
| 132 | CB.indexFunctionRedeclaration(ClientData, &Info); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void IndexingContext::handleVar(const VarDecl *D) { |
| 138 | StrAdapter SA(this); |
| 139 | |
| 140 | if (D->isFirstDeclaration()) { |
| 141 | CXIdxEntity idxEntity = 0; |
| 142 | if (CB.indexVariable) { |
| 143 | CXIdxEntityInfo EntityInfo; |
| 144 | CXIdxIndexedDeclInfo DeclInfo; |
| 145 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 146 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 147 | CXIdxVariableInfo Info = { &IdxEntityInfo, |
| 148 | D->isThisDeclarationADefinition() }; |
| 149 | |
| 150 | idxEntity = CB.indexVariable(ClientData, &Info); |
| 151 | } |
| 152 | |
| 153 | addEntityInMap(D, idxEntity); |
| 154 | |
| 155 | } else { |
| 156 | if (CB.indexVariableRedeclaration) { |
| 157 | CXIdxIndexedDeclInfo DeclInfo; |
| 158 | CXIdxIndexedRedeclInfo RedeclInfo; |
| 159 | getIndexedRedeclInfo(D, RedeclInfo, DeclInfo); |
| 160 | CXIdxVariableRedeclInfo Info = { &RedeclInfo, |
| 161 | D->isThisDeclarationADefinition() }; |
| 162 | |
| 163 | CB.indexVariableRedeclaration(ClientData, &Info); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void IndexingContext::handleField(const FieldDecl *D) { |
| 169 | StrAdapter SA(this); |
| 170 | |
| 171 | CXIdxEntity idxEntity = 0; |
| 172 | if (CB.indexTypedef) { |
| 173 | CXIdxEntityInfo EntityInfo; |
| 174 | CXIdxIndexedDeclInfo DeclInfo; |
| 175 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 176 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 177 | CXIdxFieldInfo Info = { &IdxEntityInfo }; |
| 178 | |
| 179 | idxEntity = CB.indexField(ClientData, &Info); |
| 180 | } |
| 181 | |
| 182 | addEntityInMap(D, idxEntity); |
| 183 | } |
| 184 | |
| 185 | void IndexingContext::handleEnumerator(const EnumConstantDecl *D) { |
| 186 | StrAdapter SA(this); |
| 187 | |
| 188 | CXIdxEntity idxEntity = 0; |
| 189 | if (CB.indexTypedef) { |
| 190 | CXIdxEntityInfo EntityInfo; |
| 191 | CXIdxIndexedDeclInfo DeclInfo; |
| 192 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 193 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 194 | CXIdxEnumeratorInfo Info = { &IdxEntityInfo }; |
| 195 | |
| 196 | idxEntity = CB.indexEnumerator(ClientData, &Info); |
| 197 | } |
| 198 | |
| 199 | addEntityInMap(D, idxEntity); |
| 200 | } |
| 201 | |
| 202 | void IndexingContext::handleTagDecl(const TagDecl *D) { |
| 203 | StrAdapter SA(this); |
| 204 | |
| 205 | if (D->isFirstDeclaration()) { |
| 206 | CXIdxEntity idxEntity = 0; |
| 207 | if (CB.indexTagType) { |
| 208 | CXIdxEntityInfo EntityInfo; |
| 209 | CXIdxIndexedDeclInfo DeclInfo; |
| 210 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 211 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 212 | CXIdxTagTypeInfo Info = { &IdxEntityInfo, |
| 213 | D->isThisDeclarationADefinition(), |
| 214 | D->getIdentifier() == 0}; |
| 215 | |
| 216 | idxEntity = CB.indexTagType(ClientData, &Info); |
| 217 | } |
| 218 | |
| 219 | addEntityInMap(D, idxEntity); |
| 220 | |
| 221 | } else { |
| 222 | if (CB.indexTagTypeRedeclaration) { |
| 223 | CXIdxIndexedDeclInfo DeclInfo; |
| 224 | CXIdxIndexedRedeclInfo RedeclInfo; |
| 225 | getIndexedRedeclInfo(D, RedeclInfo, DeclInfo); |
| 226 | CXIdxTagTypeRedeclInfo Info = { &RedeclInfo, |
| 227 | D->isThisDeclarationADefinition() }; |
| 228 | |
| 229 | CB.indexTagTypeRedeclaration(ClientData, &Info); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void IndexingContext::handleTypedef(const TypedefDecl *D) { |
| 235 | StrAdapter SA(this); |
| 236 | |
| 237 | CXIdxEntity idxEntity = 0; |
| 238 | if (CB.indexTypedef) { |
| 239 | CXIdxEntityInfo EntityInfo; |
| 240 | CXIdxIndexedDeclInfo DeclInfo; |
| 241 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 242 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 243 | CXIdxTypedefInfo Info = { &IdxEntityInfo }; |
| 244 | |
| 245 | idxEntity = CB.indexTypedef(ClientData, &Info); |
| 246 | } |
| 247 | |
| 248 | addEntityInMap(D, idxEntity); |
| 249 | } |
| 250 | |
| 251 | void IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { |
| 252 | StrAdapter SA(this); |
| 253 | |
| 254 | CXIdxEntity idxEntity = 0; |
| 255 | if (CB.indexObjCClass) { |
| 256 | CXIdxEntityInfo EntityInfo; |
| 257 | CXIdxIndexedDeclInfo DeclInfo; |
| 258 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 259 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 260 | CXIdxObjCClassInfo Info = { &IdxEntityInfo, |
| 261 | D->isForwardDecl() }; |
| 262 | |
| 263 | idxEntity = CB.indexObjCClass(ClientData, &Info); |
| 264 | } |
| 265 | |
| 266 | addEntityInMap(D, idxEntity); |
| 267 | } |
| 268 | |
| 269 | void IndexingContext::defineObjCInterface(const ObjCInterfaceDecl *D) { |
| 270 | if (!CB.defineObjCClass) |
| 271 | return; |
| 272 | |
| 273 | CXIdxObjCBaseClassInfo BaseClass = { getIndexEntity(D->getSuperClass()), |
| 274 | getIndexLoc(D->getSuperClassLoc()) }; |
| 275 | if (D->getSuperClass()) { |
| 276 | BaseClass.objcClass = getIndexEntity(D->getSuperClass()); |
| 277 | BaseClass.loc = getIndexLoc(D->getSuperClassLoc()); |
| 278 | } |
| 279 | |
| 280 | SmallVector<CXIdxObjCProtocolRefInfo, 4> ProtInfos; |
| 281 | ObjCInterfaceDecl::protocol_loc_iterator LI = D->protocol_loc_begin(); |
| 282 | for (ObjCInterfaceDecl::protocol_iterator |
| 283 | I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I, ++LI) { |
| 284 | SourceLocation Loc = *LI; |
| 285 | ObjCProtocolDecl *PD = *I; |
| 286 | CXIdxObjCProtocolRefInfo ProtInfo = { getIndexEntity(PD), |
| 287 | getIndexLoc(Loc) }; |
| 288 | ProtInfos.push_back(ProtInfo); |
| 289 | } |
| 290 | |
| 291 | SmallVector<CXIdxObjCProtocolRefInfo *, 4> Prots; |
| 292 | for (unsigned i = 0, e = Prots.size(); i != e; ++i) |
| 293 | Prots.push_back(&ProtInfos[i]); |
| 294 | |
| 295 | CXIdxObjCClassDefineInfo Info = { getCursor(D), |
| 296 | getIndexEntity(D), |
| 297 | getIndexContainerForDC(D), |
| 298 | D->getSuperClass() ? &BaseClass : 0, |
| 299 | Prots.data(), |
Matt Beaumont-Gay | 3de2fc4 | 2011-10-17 22:19:09 +0000 | [diff] [blame] | 300 | static_cast<unsigned>(Prots.size()) }; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 301 | CB.defineObjCClass(ClientData, &Info); |
| 302 | } |
| 303 | |
| 304 | void IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { |
| 305 | StrAdapter SA(this); |
| 306 | |
| 307 | CXIdxEntity idxEntity = 0; |
| 308 | if (CB.indexObjCProtocol) { |
| 309 | CXIdxEntityInfo EntityInfo; |
| 310 | CXIdxIndexedDeclInfo DeclInfo; |
| 311 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 312 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 313 | CXIdxObjCProtocolInfo Info = { &IdxEntityInfo, |
| 314 | D->isForwardDecl() }; |
| 315 | |
| 316 | idxEntity = CB.indexObjCProtocol(ClientData, &Info); |
| 317 | } |
| 318 | |
| 319 | addEntityInMap(D, idxEntity); |
| 320 | } |
| 321 | |
| 322 | void IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { |
| 323 | StrAdapter SA(this); |
| 324 | |
| 325 | CXIdxEntity idxEntity = 0; |
| 326 | if (CB.indexObjCCategory) { |
| 327 | CXIdxEntityInfo EntityInfo; |
| 328 | CXIdxIndexedDeclInfo DeclInfo; |
| 329 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 330 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 331 | CXIdxObjCCategoryInfo Info = { &IdxEntityInfo, |
| 332 | getIndexEntity(D->getClassInterface()) }; |
| 333 | |
| 334 | idxEntity = CB.indexObjCCategory(ClientData, &Info); |
| 335 | } |
| 336 | |
| 337 | addEntityInMap(D, idxEntity); |
| 338 | } |
| 339 | |
| 340 | void IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { |
| 341 | StrAdapter SA(this); |
| 342 | |
| 343 | if (D->isCanonicalDecl()) { |
| 344 | CXIdxEntity idxEntity = 0; |
| 345 | if (CB.indexObjCMethod) { |
| 346 | CXIdxEntityInfo EntityInfo; |
| 347 | CXIdxIndexedDeclInfo DeclInfo; |
| 348 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 349 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 350 | CXIdxObjCMethodInfo Info = { &IdxEntityInfo, |
| 351 | D->isThisDeclarationADefinition() }; |
| 352 | |
| 353 | idxEntity = CB.indexObjCMethod(ClientData, &Info); |
| 354 | } |
| 355 | |
| 356 | addEntityInMap(D, idxEntity); |
| 357 | |
| 358 | } else { |
| 359 | if (CB.indexObjCMethodRedeclaration) { |
| 360 | CXIdxIndexedRedeclInfo RedeclInfo; |
| 361 | CXIdxIndexedDeclInfo DeclInfo; |
| 362 | getIndexedRedeclInfo(D, RedeclInfo, DeclInfo); |
| 363 | CXIdxObjCMethodRedeclInfo Info = { &RedeclInfo, |
| 364 | D->isThisDeclarationADefinition() }; |
| 365 | |
| 366 | CB.indexObjCMethodRedeclaration(ClientData, &Info); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { |
| 372 | StrAdapter SA(this); |
| 373 | |
| 374 | CXIdxEntity idxEntity = 0; |
| 375 | if (CB.indexObjCProperty) { |
| 376 | CXIdxEntityInfo EntityInfo; |
| 377 | CXIdxIndexedDeclInfo DeclInfo; |
| 378 | CXIdxIndexedEntityInfo IdxEntityInfo; |
| 379 | getIndexedEntityInfo(D, IdxEntityInfo, EntityInfo, DeclInfo, SA); |
| 380 | CXIdxObjCPropertyInfo Info = { &IdxEntityInfo }; |
| 381 | |
| 382 | idxEntity = CB.indexObjCProperty(ClientData, &Info); |
| 383 | } |
| 384 | |
| 385 | addEntityInMap(D, idxEntity); |
| 386 | } |
| 387 | |
| 388 | void IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, |
| 389 | const NamedDecl *Parent, |
| 390 | const DeclContext *DC, |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 391 | const Expr *E, |
| 392 | CXIdxEntityRefKind Kind) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 393 | if (Loc.isInvalid()) |
| 394 | return; |
| 395 | if (!CB.indexEntityReference) |
| 396 | return; |
| 397 | if (isNotFromSourceFile(D->getLocation())) |
| 398 | return; |
| 399 | |
Benjamin Kramer | 854625f | 2011-10-28 13:37:11 +0000 | [diff] [blame] | 400 | CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), |
| 401 | const_cast<Decl*>(cast<Decl>(DC)), CXTU) |
| 402 | : getRefCursor(D, Loc); |
| 403 | |
| 404 | CXIdxEntityRefInfo Info = { Cursor, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 405 | getIndexLoc(Loc), |
| 406 | getIndexEntity(D), |
| 407 | getIndexEntity(Parent), |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 408 | getIndexContainerForDC(DC), |
| 409 | Kind }; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 410 | CB.indexEntityReference(ClientData, &Info); |
| 411 | } |
| 412 | |
| 413 | void IndexingContext::invokeStartedStatementBody(const NamedDecl *D, |
| 414 | const DeclContext *DC) { |
| 415 | const Stmt *Body = cast<Decl>(DC)->getBody(); |
| 416 | assert(Body); |
| 417 | |
| 418 | CXIdxContainer idxCont = 0; |
| 419 | if (CB.startedStatementBody) { |
| 420 | CXIdxContainerInfo ContainerInfo; |
| 421 | getContainerInfo(D, ContainerInfo); |
| 422 | CXIdxStmtBodyInfo Info = { &ContainerInfo, |
| 423 | getIndexLoc(Body->getLocStart()) }; |
| 424 | |
| 425 | idxCont = CB.startedStatementBody(ClientData, &Info); |
| 426 | } |
| 427 | addContainerInMap(DC, idxCont); |
| 428 | } |
| 429 | |
| 430 | void IndexingContext::invokeStartedTagTypeDefinition(const TagDecl *D) { |
| 431 | CXIdxContainer idxCont = 0; |
| 432 | if (CB.startedTagTypeDefinition) { |
| 433 | CXIdxContainerInfo ContainerInfo; |
| 434 | getContainerInfo(D, ContainerInfo); |
| 435 | CXIdxTagTypeDefinitionInfo Info = { &ContainerInfo }; |
| 436 | |
| 437 | idxCont = CB.startedTagTypeDefinition(ClientData, &Info); |
| 438 | } |
| 439 | addContainerInMap(D, idxCont); |
| 440 | } |
| 441 | |
| 442 | void IndexingContext::invokeStartedObjCContainer(const ObjCContainerDecl *D) { |
| 443 | CXIdxContainer idxCont = 0; |
| 444 | if (CB.startedObjCContainer) { |
| 445 | CXIdxContainerInfo ContainerInfo; |
| 446 | getContainerInfo(D, ContainerInfo); |
| 447 | CXIdxObjCContainerInfo Info = { &ContainerInfo }; |
| 448 | |
| 449 | idxCont = CB.startedObjCContainer(ClientData, &Info); |
| 450 | } |
| 451 | addContainerInMap(D, idxCont); |
| 452 | } |
| 453 | |
| 454 | void IndexingContext::invokeEndedContainer(const DeclContext *DC) { |
| 455 | if (CB.endedContainer) { |
| 456 | CXIdxEndContainerInfo Info = { getIndexContainerForDC(DC), |
| 457 | getIndexLoc(cast<Decl>(DC)->getLocEnd()) }; |
| 458 | CB.endedContainer(ClientData, &Info); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { |
| 463 | if (Loc.isInvalid()) |
| 464 | return true; |
| 465 | SourceManager &SM = Ctx->getSourceManager(); |
| 466 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
| 467 | FileID FID = SM.getFileID(FileLoc); |
| 468 | return SM.getFileEntryForID(FID) == 0; |
| 469 | } |
| 470 | |
| 471 | void IndexingContext::addContainerInMap(const DeclContext *DC, |
| 472 | CXIdxContainer container) { |
| 473 | assert(getScopedContext(DC) == DC); |
| 474 | ContainerMapTy::iterator I = ContainerMap.find(DC); |
| 475 | if (I == ContainerMap.end()) { |
| 476 | if (container) |
| 477 | ContainerMap[DC] = container; |
| 478 | return; |
| 479 | } |
| 480 | // Allow changing the container of a previously seen DeclContext so we |
| 481 | // can handle invalid user code, like a function re-definition. |
| 482 | if (container) |
| 483 | I->second = container; |
| 484 | else |
| 485 | ContainerMap.erase(I); |
| 486 | } |
| 487 | |
| 488 | void IndexingContext::addEntityInMap(const NamedDecl *D, CXIdxEntity entity) { |
| 489 | assert(getEntityDecl(D) == D && |
| 490 | "Tried to add a non-entity (canonical) decl"); |
| 491 | assert(EntityMap.find(D) == EntityMap.end()); |
| 492 | if (entity || D->isFromASTFile()) |
| 493 | EntityMap[D] = entity; |
| 494 | } |
| 495 | |
| 496 | CXIdxEntity IndexingContext::getIndexEntity(const NamedDecl *D) { |
| 497 | if (!D) |
| 498 | return 0; |
| 499 | D = getEntityDecl(D); |
| 500 | EntityMapTy::const_iterator I = EntityMap.find(D); |
| 501 | if (I != EntityMap.end()) |
| 502 | return I->second; |
| 503 | |
| 504 | if (!D->isFromASTFile()) { |
| 505 | //assert(0 && "Entity not in map"); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | StrAdapter SA(this); |
| 510 | |
| 511 | CXIdxEntity idxEntity = 0; |
| 512 | if (CB.importedEntity) { |
| 513 | CXIdxEntityInfo EntityInfo; |
| 514 | getEntityInfo(D, EntityInfo, SA); |
| 515 | CXIdxImportedEntityInfo Info = { &EntityInfo, |
| 516 | getCursor(D), |
| 517 | getIndexLoc(D->getLocation()), |
| 518 | /*CXIdxASTFile*/0 }; |
| 519 | idxEntity = CB.importedEntity(ClientData, &Info); |
| 520 | } |
| 521 | addEntityInMap(D, idxEntity); |
| 522 | return idxEntity; |
| 523 | } |
| 524 | |
| 525 | const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { |
| 526 | assert(D); |
| 527 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 528 | |
| 529 | if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(D)) { |
| 530 | if (Cat->IsClassExtension()) |
| 531 | return getEntityDecl(Cat->getClassInterface()); |
| 532 | |
| 533 | } else if (const ObjCImplementationDecl * |
| 534 | ImplD = dyn_cast<ObjCImplementationDecl>(D)) { |
| 535 | return getEntityDecl(ImplD->getClassInterface()); |
| 536 | |
| 537 | } else if (const ObjCCategoryImplDecl * |
| 538 | CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { |
| 539 | return getEntityDecl(CatImplD->getCategoryDecl()); |
| 540 | } |
| 541 | |
| 542 | return D; |
| 543 | } |
| 544 | |
| 545 | const DeclContext * |
| 546 | IndexingContext::getScopedContext(const DeclContext *DC) const { |
| 547 | // Local contexts are ignored for indexing. |
| 548 | const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod(); |
| 549 | if (FuncCtx) |
| 550 | return FuncCtx; |
| 551 | |
| 552 | // We consider enums always scoped for indexing. |
| 553 | if (isa<TagDecl>(DC)) |
| 554 | return DC; |
| 555 | |
| 556 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 557 | if (NS->isAnonymousNamespace()) |
| 558 | return getScopedContext(NS->getParent()); |
| 559 | return NS; |
| 560 | } |
| 561 | |
| 562 | return DC->getRedeclContext(); |
| 563 | } |
| 564 | |
| 565 | CXIdxContainer |
| 566 | IndexingContext::getIndexContainerForDC(const DeclContext *DC) const { |
| 567 | DC = getScopedContext(DC); |
| 568 | ContainerMapTy::const_iterator I = ContainerMap.find(DC); |
| 569 | // assert(I != ContainerMap.end() && |
| 570 | // "Failed to include a scoped context in the container map"); |
| 571 | return I->second; |
| 572 | } |
| 573 | |
| 574 | CXIdxFile IndexingContext::getIndexFile(const FileEntry *File) { |
| 575 | if (!File) |
| 576 | return 0; |
| 577 | if (!CB.recordFile) |
| 578 | return 0; |
| 579 | |
| 580 | FileMapTy::iterator FI = FileMap.find(File); |
| 581 | if (FI != FileMap.end()) |
| 582 | return FI->second; |
| 583 | |
| 584 | CXIdxFile idxFile = CB.recordFile(ClientData, (CXFile)File, 0); |
| 585 | FileMap[File] = idxFile; |
| 586 | return idxFile; |
| 587 | } |
| 588 | |
| 589 | CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { |
| 590 | CXIdxLoc idxLoc = { {0, 0}, 0 }; |
| 591 | if (Loc.isInvalid()) |
| 592 | return idxLoc; |
| 593 | |
| 594 | idxLoc.ptr_data[0] = (void*)this; |
| 595 | idxLoc.int_data = Loc.getRawEncoding(); |
| 596 | return idxLoc; |
| 597 | } |
| 598 | |
| 599 | void IndexingContext::translateLoc(SourceLocation Loc, |
| 600 | CXIdxFile *indexFile, CXFile *file, |
| 601 | unsigned *line, unsigned *column, |
| 602 | unsigned *offset) { |
| 603 | if (Loc.isInvalid()) |
| 604 | return; |
| 605 | |
| 606 | SourceManager &SM = Ctx->getSourceManager(); |
| 607 | Loc = SM.getFileLoc(Loc); |
| 608 | |
| 609 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 610 | FileID FID = LocInfo.first; |
| 611 | unsigned FileOffset = LocInfo.second; |
| 612 | |
| 613 | if (FID.isInvalid()) |
| 614 | return; |
| 615 | |
| 616 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 617 | if (indexFile) |
| 618 | *indexFile = getIndexFile(FE); |
| 619 | if (file) |
| 620 | *file = (void *)FE; |
| 621 | if (line) |
| 622 | *line = SM.getLineNumber(FID, FileOffset); |
| 623 | if (column) |
| 624 | *column = SM.getColumnNumber(FID, FileOffset); |
| 625 | if (offset) |
| 626 | *offset = FileOffset; |
| 627 | } |
| 628 | |
| 629 | void IndexingContext::getIndexedEntityInfo(const NamedDecl *D, |
| 630 | CXIdxIndexedEntityInfo &IdxEntityInfo, |
| 631 | CXIdxEntityInfo &EntityInfo, |
| 632 | CXIdxIndexedDeclInfo &IdxDeclInfo, |
| 633 | StrAdapter &SA) { |
| 634 | getEntityInfo(D, EntityInfo, SA); |
| 635 | getIndexedDeclInfo(D, IdxDeclInfo); |
| 636 | IdxEntityInfo.entityInfo = &EntityInfo; |
| 637 | IdxEntityInfo.declInfo = &IdxDeclInfo; |
| 638 | } |
| 639 | |
| 640 | void IndexingContext::getIndexedDeclInfo(const NamedDecl *D, |
| 641 | CXIdxIndexedDeclInfo &IdxDeclInfo) { |
| 642 | IdxDeclInfo.cursor = getCursor(D); |
| 643 | IdxDeclInfo.loc = getIndexLoc(D->getLocation()); |
| 644 | IdxDeclInfo.container = getIndexContainer(D); |
| 645 | } |
| 646 | |
| 647 | void IndexingContext::getIndexedRedeclInfo(const NamedDecl *D, |
| 648 | CXIdxIndexedRedeclInfo &RedeclInfo, |
| 649 | CXIdxIndexedDeclInfo &IdxDeclInfo) { |
| 650 | getIndexedDeclInfo(D, IdxDeclInfo); |
| 651 | RedeclInfo.declInfo = &IdxDeclInfo; |
| 652 | RedeclInfo.entity = getIndexEntity(D); |
| 653 | } |
| 654 | |
| 655 | void IndexingContext::getContainerInfo(const NamedDecl *D, |
| 656 | CXIdxContainerInfo &ContainerInfo) { |
| 657 | ContainerInfo.cursor = getCursor(D); |
| 658 | ContainerInfo.loc = getIndexLoc(D->getLocation()); |
| 659 | ContainerInfo.entity = getIndexEntity(D); |
| 660 | } |
| 661 | |
| 662 | void IndexingContext::getEntityInfo(const NamedDecl *D, |
| 663 | CXIdxEntityInfo &EntityInfo, |
| 664 | StrAdapter &SA) { |
| 665 | if (IdentifierInfo *II = D->getIdentifier()) { |
| 666 | EntityInfo.name = SA.toCStr(II->getName()); |
| 667 | |
| 668 | } else if (isa<RecordDecl>(D) || isa<NamespaceDecl>(D)) { |
| 669 | EntityInfo.name = 0; |
| 670 | |
| 671 | } else { |
| 672 | unsigned Begin = SA.getCurSize(); |
| 673 | { |
| 674 | llvm::raw_svector_ostream OS(SA.getBuffer()); |
| 675 | D->printName(OS); |
| 676 | } |
| 677 | EntityInfo.name = SA.getCStr(Begin); |
| 678 | } |
| 679 | |
| 680 | unsigned Begin = SA.getCurSize(); |
| 681 | bool Ignore = getDeclCursorUSR(D, SA.getBuffer()); |
| 682 | if (Ignore) { |
| 683 | EntityInfo.USR = ""; |
| 684 | } else { |
| 685 | EntityInfo.USR = SA.getCStr(Begin); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { |
| 690 | if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) |
| 691 | return MakeCursorTypeRef(TD, Loc, CXTU); |
| 692 | if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) |
| 693 | return MakeCursorObjCClassRef(ID, Loc, CXTU); |
| 694 | if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) |
| 695 | return MakeCursorObjCProtocolRef(PD, Loc, CXTU); |
| 696 | |
| 697 | //assert(0 && "not yet"); |
| 698 | return clang_getNullCursor(); |
| 699 | } |