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