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