Indexing.cpp revision e1d4330adaaa7faf093e725c9c993207eb2d778a
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 "CXCursor.h" 12#include "CXSourceLocation.h" 13#include "CXTranslationUnit.h" 14#include "CXString.h" 15#include "CIndexDiagnostic.h" 16#include "CIndexer.h" 17 18#include "clang/Frontend/ASTUnit.h" 19#include "clang/Frontend/CompilerInvocation.h" 20#include "clang/Frontend/CompilerInstance.h" 21#include "clang/Frontend/FrontendAction.h" 22#include "clang/Frontend/Utils.h" 23#include "clang/Sema/SemaConsumer.h" 24#include "clang/AST/ASTConsumer.h" 25#include "clang/AST/DeclVisitor.h" 26#include "clang/Lex/Preprocessor.h" 27#include "clang/Lex/PPCallbacks.h" 28#include "llvm/Support/MemoryBuffer.h" 29#include "llvm/Support/CrashRecoveryContext.h" 30 31using namespace clang; 32using namespace cxstring; 33using namespace cxtu; 34using namespace cxindex; 35 36static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx); 37 38namespace { 39 40//===----------------------------------------------------------------------===// 41// IndexPPCallbacks 42//===----------------------------------------------------------------------===// 43 44class IndexPPCallbacks : public PPCallbacks { 45 Preprocessor &PP; 46 IndexingContext &IndexCtx; 47 bool IsMainFileEntered; 48 49public: 50 IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx) 51 : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { } 52 53 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 54 SrcMgr::CharacteristicKind FileType, FileID PrevFID) { 55 if (IsMainFileEntered) 56 return; 57 58 SourceManager &SM = PP.getSourceManager(); 59 SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID()); 60 61 if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) { 62 IsMainFileEntered = true; 63 IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID())); 64 } 65 } 66 67 virtual void InclusionDirective(SourceLocation HashLoc, 68 const Token &IncludeTok, 69 StringRef FileName, 70 bool IsAngled, 71 const FileEntry *File, 72 SourceLocation EndLoc, 73 StringRef SearchPath, 74 StringRef RelativePath) { 75 bool isImport = (IncludeTok.is(tok::identifier) && 76 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); 77 IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled); 78 } 79 80 /// MacroDefined - This hook is called whenever a macro definition is seen. 81 virtual void MacroDefined(const Token &Id, const MacroInfo *MI) { 82 } 83 84 /// MacroUndefined - This hook is called whenever a macro #undef is seen. 85 /// MI is released immediately following this callback. 86 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) { 87 } 88 89 /// MacroExpands - This is called by when a macro invocation is found. 90 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI, 91 SourceRange Range) { 92 } 93 94 /// SourceRangeSkipped - This hook is called when a source range is skipped. 95 /// \param Range The SourceRange that was skipped. The range begins at the 96 /// #if/#else directive and ends after the #endif/#else directive. 97 virtual void SourceRangeSkipped(SourceRange Range) { 98 } 99}; 100 101//===----------------------------------------------------------------------===// 102// IndexingConsumer 103//===----------------------------------------------------------------------===// 104 105class IndexingConsumer : public ASTConsumer { 106 IndexingContext &IndexCtx; 107 108public: 109 explicit IndexingConsumer(IndexingContext &indexCtx) 110 : IndexCtx(indexCtx) { } 111 112 // ASTConsumer Implementation 113 114 virtual void Initialize(ASTContext &Context) { 115 IndexCtx.setASTContext(Context); 116 IndexCtx.startedTranslationUnit(); 117 } 118 119 virtual void HandleTranslationUnit(ASTContext &Ctx) { 120 } 121 122 virtual bool HandleTopLevelDecl(DeclGroupRef DG) { 123 IndexCtx.indexDeclGroupRef(DG); 124 return !IndexCtx.shouldAbort(); 125 } 126 127 /// \brief Handle the specified top-level declaration that occurred inside 128 /// and ObjC container. 129 virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) { 130 // They will be handled after the interface is seen first. 131 IndexCtx.addTUDeclInObjCContainer(D); 132 } 133 134 /// \brief This is called by the AST reader when deserializing things. 135 /// The default implementation forwards to HandleTopLevelDecl but we don't 136 /// care about them when indexing, so have an empty definition. 137 virtual void HandleInterestingDecl(DeclGroupRef D) {} 138 139 virtual void HandleTagDeclDefinition(TagDecl *D) { 140 if (!IndexCtx.shouldIndexImplicitTemplateInsts()) 141 return; 142 143 if (IndexCtx.isTemplateImplicitInstantiation(D)) 144 IndexCtx.indexDecl(D); 145 } 146 147 virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) { 148 if (!IndexCtx.shouldIndexImplicitTemplateInsts()) 149 return; 150 151 IndexCtx.indexDecl(D); 152 } 153}; 154 155//===----------------------------------------------------------------------===// 156// CaptureDiagnosticConsumer 157//===----------------------------------------------------------------------===// 158 159class CaptureDiagnosticConsumer : public DiagnosticConsumer { 160 SmallVector<StoredDiagnostic, 4> Errors; 161public: 162 163 virtual void HandleDiagnostic(DiagnosticsEngine::Level level, 164 const Diagnostic &Info) { 165 if (level >= DiagnosticsEngine::Error) 166 Errors.push_back(StoredDiagnostic(level, Info)); 167 } 168 169 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { 170 return new IgnoringDiagConsumer(); 171 } 172}; 173 174//===----------------------------------------------------------------------===// 175// IndexingFrontendAction 176//===----------------------------------------------------------------------===// 177 178class IndexingFrontendAction : public ASTFrontendAction { 179 IndexingContext IndexCtx; 180 CXTranslationUnit CXTU; 181 182public: 183 IndexingFrontendAction(CXClientData clientData, 184 IndexerCallbacks &indexCallbacks, 185 unsigned indexOptions, 186 CXTranslationUnit cxTU) 187 : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU), 188 CXTU(cxTU) { } 189 190 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 191 StringRef InFile) { 192 IndexCtx.setASTContext(CI.getASTContext()); 193 Preprocessor &PP = CI.getPreprocessor(); 194 PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx)); 195 IndexCtx.setPreprocessor(PP); 196 return new IndexingConsumer(IndexCtx); 197 } 198 199 virtual void EndSourceFileAction() { 200 indexDiagnostics(CXTU, IndexCtx); 201 } 202 203 virtual TranslationUnitKind getTranslationUnitKind() { 204 if (IndexCtx.shouldIndexImplicitTemplateInsts()) 205 return TU_Complete; 206 else 207 return TU_Prefix; 208 } 209 virtual bool hasCodeCompletionSupport() const { return false; } 210}; 211 212//===----------------------------------------------------------------------===// 213// clang_indexSourceFileUnit Implementation 214//===----------------------------------------------------------------------===// 215 216struct IndexSourceFileInfo { 217 CXIndexAction idxAction; 218 CXClientData client_data; 219 IndexerCallbacks *index_callbacks; 220 unsigned index_callbacks_size; 221 unsigned index_options; 222 const char *source_filename; 223 const char *const *command_line_args; 224 int num_command_line_args; 225 struct CXUnsavedFile *unsaved_files; 226 unsigned num_unsaved_files; 227 CXTranslationUnit *out_TU; 228 unsigned TU_options; 229 int result; 230}; 231 232struct MemBufferOwner { 233 SmallVector<const llvm::MemoryBuffer *, 8> Buffers; 234 235 ~MemBufferOwner() { 236 for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator 237 I = Buffers.begin(), E = Buffers.end(); I != E; ++I) 238 delete *I; 239 } 240}; 241 242} // anonymous namespace 243 244static void clang_indexSourceFile_Impl(void *UserData) { 245 IndexSourceFileInfo *ITUI = 246 static_cast<IndexSourceFileInfo*>(UserData); 247 CXIndex CIdx = (CXIndex)ITUI->idxAction; 248 CXClientData client_data = ITUI->client_data; 249 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; 250 unsigned index_callbacks_size = ITUI->index_callbacks_size; 251 unsigned index_options = ITUI->index_options; 252 const char *source_filename = ITUI->source_filename; 253 const char * const *command_line_args = ITUI->command_line_args; 254 int num_command_line_args = ITUI->num_command_line_args; 255 struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files; 256 unsigned num_unsaved_files = ITUI->num_unsaved_files; 257 CXTranslationUnit *out_TU = ITUI->out_TU; 258 unsigned TU_options = ITUI->TU_options; 259 ITUI->result = 1; // init as error. 260 261 if (out_TU) 262 *out_TU = 0; 263 bool requestedToGetTU = (out_TU != 0); 264 265 if (!CIdx) 266 return; 267 if (!client_index_callbacks || index_callbacks_size == 0) 268 return; 269 270 IndexerCallbacks CB; 271 memset(&CB, 0, sizeof(CB)); 272 unsigned ClientCBSize = index_callbacks_size < sizeof(CB) 273 ? index_callbacks_size : sizeof(CB); 274 memcpy(&CB, client_index_callbacks, ClientCBSize); 275 276 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 277 278 CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer(); 279 280 // Configure the diagnostics. 281 DiagnosticOptions DiagOpts; 282 IntrusiveRefCntPtr<DiagnosticsEngine> 283 Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, 284 command_line_args, 285 CaptureDiag, 286 /*ShouldOwnClient=*/true, 287 /*ShouldCloneClient=*/false)); 288 289 // Recover resources if we crash before exiting this function. 290 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 291 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 292 DiagCleanup(Diags.getPtr()); 293 294 OwningPtr<std::vector<const char *> > 295 Args(new std::vector<const char*>()); 296 297 // Recover resources if we crash before exiting this method. 298 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > 299 ArgsCleanup(Args.get()); 300 301 Args->insert(Args->end(), command_line_args, 302 command_line_args + num_command_line_args); 303 304 // The 'source_filename' argument is optional. If the caller does not 305 // specify it then it is assumed that the source file is specified 306 // in the actual argument list. 307 // Put the source file after command_line_args otherwise if '-x' flag is 308 // present it will be unused. 309 if (source_filename) 310 Args->push_back(source_filename); 311 312 IntrusiveRefCntPtr<CompilerInvocation> 313 CInvok(createInvocationFromCommandLine(*Args, Diags)); 314 315 if (!CInvok) 316 return; 317 318 // Recover resources if we crash before exiting this function. 319 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, 320 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> > 321 CInvokCleanup(CInvok.getPtr()); 322 323 if (CInvok->getFrontendOpts().Inputs.empty()) 324 return; 325 326 OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner()); 327 328 // Recover resources if we crash before exiting this method. 329 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> 330 BufOwnerCleanup(BufOwner.get()); 331 332 for (unsigned I = 0; I != num_unsaved_files; ++I) { 333 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 334 const llvm::MemoryBuffer *Buffer 335 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 336 CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer); 337 BufOwner->Buffers.push_back(Buffer); 338 } 339 340 // Since libclang is primarily used by batch tools dealing with 341 // (often very broken) source code, where spell-checking can have a 342 // significant negative impact on performance (particularly when 343 // precompiled headers are involved), we disable it. 344 CInvok->getLangOpts()->SpellChecking = false; 345 346 if (!requestedToGetTU) 347 CInvok->getPreprocessorOpts().DetailedRecord = false; 348 349 ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags, 350 /*CaptureDiagnostics=*/true); 351 OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(Unit))); 352 353 // Recover resources if we crash before exiting this method. 354 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner> 355 CXTUCleanup(CXTU.get()); 356 357 OwningPtr<IndexingFrontendAction> IndexAction; 358 IndexAction.reset(new IndexingFrontendAction(client_data, CB, 359 index_options, CXTU->getTU())); 360 361 // Recover resources if we crash before exiting this method. 362 llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction> 363 IndexActionCleanup(IndexAction.get()); 364 365 bool Persistent = requestedToGetTU; 366 StringRef ResourceFilesPath = CXXIdx->getClangResourcesPath(); 367 bool OnlyLocalDecls = false; 368 bool PrecompilePreamble = false; 369 bool CacheCodeCompletionResults = false; 370 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); 371 PPOpts.DetailedRecord = false; 372 373 if (requestedToGetTU) { 374 OnlyLocalDecls = CXXIdx->getOnlyLocalDecls(); 375 PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble; 376 // FIXME: Add a flag for modules. 377 CacheCodeCompletionResults 378 = TU_options & CXTranslationUnit_CacheCompletionResults; 379 if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) { 380 PPOpts.DetailedRecord = true; 381 } 382 } 383 384 Unit = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags, 385 IndexAction.get(), 386 Unit, 387 Persistent, 388 ResourceFilesPath, 389 OnlyLocalDecls, 390 /*CaptureDiagnostics=*/true, 391 PrecompilePreamble, 392 CacheCodeCompletionResults); 393 if (!Unit) 394 return; 395 396 if (out_TU) 397 *out_TU = CXTU->takeTU(); 398 399 ITUI->result = 0; // success. 400} 401 402//===----------------------------------------------------------------------===// 403// clang_indexTranslationUnit Implementation 404//===----------------------------------------------------------------------===// 405 406namespace { 407 408struct IndexTranslationUnitInfo { 409 CXIndexAction idxAction; 410 CXClientData client_data; 411 IndexerCallbacks *index_callbacks; 412 unsigned index_callbacks_size; 413 unsigned index_options; 414 CXTranslationUnit TU; 415 int result; 416}; 417 418} // anonymous namespace 419 420static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) { 421 Preprocessor &PP = Unit.getPreprocessor(); 422 if (!PP.getPreprocessingRecord()) 423 return; 424 425 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 426 427 // FIXME: Only deserialize inclusion directives. 428 // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module 429 // that it depends on. 430 431 bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls(); 432 PreprocessingRecord::iterator I, E; 433 if (OnlyLocal) { 434 I = PPRec.local_begin(); 435 E = PPRec.local_end(); 436 } else { 437 I = PPRec.begin(); 438 E = PPRec.end(); 439 } 440 441 for (; I != E; ++I) { 442 PreprocessedEntity *PPE = *I; 443 444 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { 445 IdxCtx.ppIncludedFile(ID->getSourceRange().getBegin(), ID->getFileName(), 446 ID->getFile(), ID->getKind() == InclusionDirective::Import, 447 !ID->wasInQuotes()); 448 } 449 } 450} 451 452static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) { 453 // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module 454 // that it depends on. 455 456 bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls(); 457 458 if (OnlyLocal) { 459 for (ASTUnit::top_level_iterator TL = Unit.top_level_begin(), 460 TLEnd = Unit.top_level_end(); 461 TL != TLEnd; ++TL) { 462 IdxCtx.indexTopLevelDecl(*TL); 463 if (IdxCtx.shouldAbort()) 464 return; 465 } 466 467 } else { 468 TranslationUnitDecl *TUDecl = Unit.getASTContext().getTranslationUnitDecl(); 469 for (TranslationUnitDecl::decl_iterator 470 I = TUDecl->decls_begin(), E = TUDecl->decls_end(); I != E; ++I) { 471 IdxCtx.indexTopLevelDecl(*I); 472 if (IdxCtx.shouldAbort()) 473 return; 474 } 475 } 476} 477 478static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) { 479 if (!IdxCtx.hasDiagnosticCallback()) 480 return; 481 482 CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU); 483 IdxCtx.handleDiagnosticSet(DiagSet); 484} 485 486static void clang_indexTranslationUnit_Impl(void *UserData) { 487 IndexTranslationUnitInfo *ITUI = 488 static_cast<IndexTranslationUnitInfo*>(UserData); 489 CXTranslationUnit TU = ITUI->TU; 490 CXClientData client_data = ITUI->client_data; 491 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; 492 unsigned index_callbacks_size = ITUI->index_callbacks_size; 493 unsigned index_options = ITUI->index_options; 494 ITUI->result = 1; // init as error. 495 496 if (!TU) 497 return; 498 if (!client_index_callbacks || index_callbacks_size == 0) 499 return; 500 501 IndexerCallbacks CB; 502 memset(&CB, 0, sizeof(CB)); 503 unsigned ClientCBSize = index_callbacks_size < sizeof(CB) 504 ? index_callbacks_size : sizeof(CB); 505 memcpy(&CB, client_index_callbacks, ClientCBSize); 506 507 OwningPtr<IndexingContext> IndexCtx; 508 IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU)); 509 510 // Recover resources if we crash before exiting this method. 511 llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext> 512 IndexCtxCleanup(IndexCtx.get()); 513 514 OwningPtr<IndexingConsumer> IndexConsumer; 515 IndexConsumer.reset(new IndexingConsumer(*IndexCtx)); 516 517 // Recover resources if we crash before exiting this method. 518 llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer> 519 IndexConsumerCleanup(IndexConsumer.get()); 520 521 ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); 522 if (!Unit) 523 return; 524 525 FileManager &FileMgr = Unit->getFileManager(); 526 527 if (Unit->getOriginalSourceFileName().empty()) 528 IndexCtx->enteredMainFile(0); 529 else 530 IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName())); 531 532 IndexConsumer->Initialize(Unit->getASTContext()); 533 534 indexPreprocessingRecord(*Unit, *IndexCtx); 535 indexTranslationUnit(*Unit, *IndexCtx); 536 indexDiagnostics(TU, *IndexCtx); 537 538 ITUI->result = 0; 539} 540 541//===----------------------------------------------------------------------===// 542// libclang public APIs. 543//===----------------------------------------------------------------------===// 544 545extern "C" { 546 547int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) { 548 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory; 549} 550 551const CXIdxObjCContainerDeclInfo * 552clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) { 553 if (!DInfo) 554 return 0; 555 556 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); 557 if (const ObjCContainerDeclInfo * 558 ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI)) 559 return &ContInfo->ObjCContDeclInfo; 560 561 return 0; 562} 563 564const CXIdxObjCInterfaceDeclInfo * 565clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) { 566 if (!DInfo) 567 return 0; 568 569 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); 570 if (const ObjCInterfaceDeclInfo * 571 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) 572 return &InterInfo->ObjCInterDeclInfo; 573 574 return 0; 575} 576 577const CXIdxObjCCategoryDeclInfo * 578clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){ 579 if (!DInfo) 580 return 0; 581 582 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); 583 if (const ObjCCategoryDeclInfo * 584 CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) 585 return &CatInfo->ObjCCatDeclInfo; 586 587 return 0; 588} 589 590const CXIdxObjCProtocolRefListInfo * 591clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) { 592 if (!DInfo) 593 return 0; 594 595 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); 596 597 if (const ObjCInterfaceDeclInfo * 598 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) 599 return InterInfo->ObjCInterDeclInfo.protocols; 600 601 if (const ObjCProtocolDeclInfo * 602 ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI)) 603 return &ProtInfo->ObjCProtoRefListInfo; 604 605 if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) 606 return CatInfo->ObjCCatDeclInfo.protocols; 607 608 return 0; 609} 610 611const CXIdxIBOutletCollectionAttrInfo * 612clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) { 613 if (!AInfo) 614 return 0; 615 616 const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo); 617 if (const IBOutletCollectionInfo * 618 IBInfo = dyn_cast<IBOutletCollectionInfo>(DI)) 619 return &IBInfo->IBCollInfo; 620 621 return 0; 622} 623 624const CXIdxCXXClassDeclInfo * 625clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) { 626 if (!DInfo) 627 return 0; 628 629 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); 630 if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI)) 631 return &ClassInfo->CXXClassInfo; 632 633 return 0; 634} 635 636CXIdxClientContainer 637clang_index_getClientContainer(const CXIdxContainerInfo *info) { 638 if (!info) 639 return 0; 640 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); 641 return Container->IndexCtx->getClientContainerForDC(Container->DC); 642} 643 644void clang_index_setClientContainer(const CXIdxContainerInfo *info, 645 CXIdxClientContainer client) { 646 if (!info) 647 return; 648 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); 649 Container->IndexCtx->addContainerInMap(Container->DC, client); 650} 651 652CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) { 653 if (!info) 654 return 0; 655 const EntityInfo *Entity = static_cast<const EntityInfo *>(info); 656 return Entity->IndexCtx->getClientEntity(Entity->Dcl); 657} 658 659void clang_index_setClientEntity(const CXIdxEntityInfo *info, 660 CXIdxClientEntity client) { 661 if (!info) 662 return; 663 const EntityInfo *Entity = static_cast<const EntityInfo *>(info); 664 Entity->IndexCtx->setClientEntity(Entity->Dcl, client); 665} 666 667CXIndexAction clang_IndexAction_create(CXIndex CIdx) { 668 // For now, CXIndexAction is featureless. 669 return CIdx; 670} 671 672void clang_IndexAction_dispose(CXIndexAction idxAction) { 673 // For now, CXIndexAction is featureless. 674} 675 676int clang_indexSourceFile(CXIndexAction idxAction, 677 CXClientData client_data, 678 IndexerCallbacks *index_callbacks, 679 unsigned index_callbacks_size, 680 unsigned index_options, 681 const char *source_filename, 682 const char * const *command_line_args, 683 int num_command_line_args, 684 struct CXUnsavedFile *unsaved_files, 685 unsigned num_unsaved_files, 686 CXTranslationUnit *out_TU, 687 unsigned TU_options) { 688 689 IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks, 690 index_callbacks_size, index_options, 691 source_filename, command_line_args, 692 num_command_line_args, unsaved_files, 693 num_unsaved_files, out_TU, TU_options, 0 }; 694 695 if (getenv("LIBCLANG_NOTHREADS")) { 696 clang_indexSourceFile_Impl(&ITUI); 697 return ITUI.result; 698 } 699 700 llvm::CrashRecoveryContext CRC; 701 702 if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) { 703 fprintf(stderr, "libclang: crash detected during indexing source file: {\n"); 704 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); 705 fprintf(stderr, " 'command_line_args' : ["); 706 for (int i = 0; i != num_command_line_args; ++i) { 707 if (i) 708 fprintf(stderr, ", "); 709 fprintf(stderr, "'%s'", command_line_args[i]); 710 } 711 fprintf(stderr, "],\n"); 712 fprintf(stderr, " 'unsaved_files' : ["); 713 for (unsigned i = 0; i != num_unsaved_files; ++i) { 714 if (i) 715 fprintf(stderr, ", "); 716 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, 717 unsaved_files[i].Length); 718 } 719 fprintf(stderr, "],\n"); 720 fprintf(stderr, " 'options' : %d,\n", TU_options); 721 fprintf(stderr, "}\n"); 722 723 return 1; 724 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { 725 if (out_TU) 726 PrintLibclangResourceUsage(*out_TU); 727 } 728 729 return ITUI.result; 730} 731 732int clang_indexTranslationUnit(CXIndexAction idxAction, 733 CXClientData client_data, 734 IndexerCallbacks *index_callbacks, 735 unsigned index_callbacks_size, 736 unsigned index_options, 737 CXTranslationUnit TU) { 738 739 IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks, 740 index_callbacks_size, index_options, TU, 741 0 }; 742 743 if (getenv("LIBCLANG_NOTHREADS")) { 744 clang_indexTranslationUnit_Impl(&ITUI); 745 return ITUI.result; 746 } 747 748 llvm::CrashRecoveryContext CRC; 749 750 if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) { 751 fprintf(stderr, "libclang: crash detected during indexing TU\n"); 752 753 return 1; 754 } 755 756 return ITUI.result; 757} 758 759void clang_indexLoc_getFileLocation(CXIdxLoc location, 760 CXIdxClientFile *indexFile, 761 CXFile *file, 762 unsigned *line, 763 unsigned *column, 764 unsigned *offset) { 765 if (indexFile) *indexFile = 0; 766 if (file) *file = 0; 767 if (line) *line = 0; 768 if (column) *column = 0; 769 if (offset) *offset = 0; 770 771 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 772 if (!location.ptr_data[0] || Loc.isInvalid()) 773 return; 774 775 IndexingContext &IndexCtx = 776 *static_cast<IndexingContext*>(location.ptr_data[0]); 777 IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset); 778} 779 780CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { 781 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 782 if (!location.ptr_data[0] || Loc.isInvalid()) 783 return clang_getNullLocation(); 784 785 IndexingContext &IndexCtx = 786 *static_cast<IndexingContext*>(location.ptr_data[0]); 787 return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc); 788} 789 790} // end: extern "C" 791 792