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