Indexing.cpp revision dd93c596cd95e1b96031ff47efe0a5095ff3d7f1
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 "CIndexer.h" 16 17#include "clang/Frontend/ASTUnit.h" 18#include "clang/Frontend/CompilerInvocation.h" 19#include "clang/Frontend/CompilerInstance.h" 20#include "clang/Frontend/Utils.h" 21#include "clang/Sema/SemaConsumer.h" 22#include "clang/AST/ASTConsumer.h" 23#include "clang/AST/DeclVisitor.h" 24#include "clang/Lex/Preprocessor.h" 25#include "clang/Lex/PPCallbacks.h" 26#include "llvm/Support/MemoryBuffer.h" 27#include "llvm/Support/CrashRecoveryContext.h" 28 29using namespace clang; 30using namespace cxstring; 31using namespace cxtu; 32using namespace cxindex; 33 34namespace { 35 36//===----------------------------------------------------------------------===// 37// IndexPPCallbacks 38//===----------------------------------------------------------------------===// 39 40class IndexPPCallbacks : public PPCallbacks { 41 Preprocessor &PP; 42 IndexingContext &IndexCtx; 43 bool IsMainFileEntered; 44 45public: 46 IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx) 47 : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { } 48 49 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 50 SrcMgr::CharacteristicKind FileType, FileID PrevFID) { 51 if (IsMainFileEntered) 52 return; 53 54 SourceManager &SM = PP.getSourceManager(); 55 SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID()); 56 57 if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) { 58 IsMainFileEntered = true; 59 IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID())); 60 } 61 } 62 63 virtual void InclusionDirective(SourceLocation HashLoc, 64 const Token &IncludeTok, 65 StringRef FileName, 66 bool IsAngled, 67 const FileEntry *File, 68 SourceLocation EndLoc, 69 StringRef SearchPath, 70 StringRef RelativePath) { 71 bool isImport = (IncludeTok.is(tok::identifier) && 72 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); 73 IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled); 74 } 75 76 /// MacroDefined - This hook is called whenever a macro definition is seen. 77 virtual void MacroDefined(const Token &Id, const MacroInfo *MI) { 78 if (MI->isBuiltinMacro()) 79 return; 80 if (IndexCtx.isNotFromSourceFile(MI->getDefinitionLoc())) 81 return; 82 83 SourceLocation Loc = MI->getDefinitionLoc(); 84 SourceLocation DefBegin = MI->tokens_empty() ? Loc 85 : MI->getReplacementToken(0).getLocation(); 86 IndexCtx.ppMacroDefined(Loc, 87 Id.getIdentifierInfo()->getName(), 88 DefBegin, 89 MI->getDefinitionLength(PP.getSourceManager()), 90 MI); 91 } 92 93 /// MacroUndefined - This hook is called whenever a macro #undef is seen. 94 /// MI is released immediately following this callback. 95 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) { 96 if (MI->isBuiltinMacro()) 97 return; 98 if (IndexCtx.isNotFromSourceFile(MI->getDefinitionLoc())) 99 return; 100 101 SourceLocation Loc = MacroNameTok.getLocation(); 102 IndexCtx.ppMacroUndefined(Loc, 103 MacroNameTok.getIdentifierInfo()->getName(), 104 MI); 105 } 106 107 /// MacroExpands - This is called by when a macro invocation is found. 108 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI, 109 SourceRange Range) { 110 if (MI->isBuiltinMacro()) 111 return; 112 if (IndexCtx.isNotFromSourceFile(MI->getDefinitionLoc())) 113 return; 114 115 SourceLocation Loc = MacroNameTok.getLocation(); 116 IndexCtx.ppMacroExpanded(Loc, 117 MacroNameTok.getIdentifierInfo()->getName(), 118 MI); 119 } 120 121 /// SourceRangeSkipped - This hook is called when a source range is skipped. 122 /// \param Range The SourceRange that was skipped. The range begins at the 123 /// #if/#else directive and ends after the #endif/#else directive. 124 virtual void SourceRangeSkipped(SourceRange Range) { 125 } 126}; 127 128//===----------------------------------------------------------------------===// 129// IndexingConsumer 130//===----------------------------------------------------------------------===// 131 132class IndexingConsumer : public ASTConsumer { 133 IndexingContext &IndexCtx; 134 135public: 136 explicit IndexingConsumer(IndexingContext &indexCtx) 137 : IndexCtx(indexCtx) { } 138 139 // ASTConsumer Implementation 140 141 virtual void Initialize(ASTContext &Context) { 142 IndexCtx.setASTContext(Context); 143 IndexCtx.invokeStartedTranslationUnit(); 144 } 145 146 virtual void HandleTranslationUnit(ASTContext &Ctx) { 147 IndexCtx.invokeFinishedTranslationUnit(); 148 } 149 150 virtual void HandleTopLevelDecl(DeclGroupRef DG) { 151 IndexCtx.indexDeclGroupRef(DG); 152 } 153 154 /// \brief Handle the specified top-level declaration that occurred inside 155 /// and ObjC container. 156 virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) { 157 // They will be handled after the interface is seen first. 158 IndexCtx.addTUDeclInObjCContainer(D); 159 } 160 161 /// \brief This is called by the AST reader when deserializing things. 162 /// The default implementation forwards to HandleTopLevelDecl but we don't 163 /// care about them when indexing, so have an empty definition. 164 virtual void HandleInterestingDecl(DeclGroupRef D) {} 165}; 166 167//===----------------------------------------------------------------------===// 168// IndexingDiagnosticConsumer 169//===----------------------------------------------------------------------===// 170 171class IndexingDiagnosticConsumer : public DiagnosticConsumer { 172 IndexingContext &IndexCtx; 173 174public: 175 explicit IndexingDiagnosticConsumer(IndexingContext &indexCtx) 176 : IndexCtx(indexCtx) {} 177 178 virtual void HandleDiagnostic(DiagnosticsEngine::Level Level, 179 const Diagnostic &Info) { 180 // Default implementation (Warnings/errors count). 181 DiagnosticConsumer::HandleDiagnostic(Level, Info); 182 183 IndexCtx.handleDiagnostic(StoredDiagnostic(Level, Info)); 184 } 185 186 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { 187 return new IgnoringDiagConsumer(); 188 } 189}; 190 191class CaptureDiagnosticConsumer : public DiagnosticConsumer { 192 SmallVector<StoredDiagnostic, 4> Errors; 193public: 194 195 virtual void HandleDiagnostic(DiagnosticsEngine::Level level, 196 const Diagnostic &Info) { 197 if (level >= DiagnosticsEngine::Error) 198 Errors.push_back(StoredDiagnostic(level, Info)); 199 } 200 201 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { 202 return new IgnoringDiagConsumer(); 203 } 204}; 205 206//===----------------------------------------------------------------------===// 207// IndexingFrontendAction 208//===----------------------------------------------------------------------===// 209 210class IndexingFrontendAction : public ASTFrontendAction { 211 IndexingContext IndexCtx; 212 213public: 214 IndexingFrontendAction(CXClientData clientData, 215 IndexerCallbacks &indexCallbacks, 216 unsigned indexOptions, 217 CXTranslationUnit cxTU) 218 : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU) { } 219 220 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 221 StringRef InFile) { 222 CI.getDiagnostics().setClient(new IndexingDiagnosticConsumer(IndexCtx), 223 /*own=*/true); 224 IndexCtx.setASTContext(CI.getASTContext()); 225 Preprocessor &PP = CI.getPreprocessor(); 226 PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx)); 227 return new IndexingConsumer(IndexCtx); 228 } 229 230 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; } 231 virtual bool hasCodeCompletionSupport() const { return false; } 232}; 233 234//===----------------------------------------------------------------------===// 235// clang_indexTranslationUnit Implementation 236//===----------------------------------------------------------------------===// 237 238struct IndexTranslationUnitInfo { 239 CXIndex CIdx; 240 CXClientData client_data; 241 IndexerCallbacks *index_callbacks; 242 unsigned index_callbacks_size; 243 unsigned index_options; 244 const char *source_filename; 245 const char *const *command_line_args; 246 int num_command_line_args; 247 struct CXUnsavedFile *unsaved_files; 248 unsigned num_unsaved_files; 249 CXTranslationUnit *out_TU; 250 unsigned TU_options; 251 int result; 252}; 253 254struct MemBufferOwner { 255 SmallVector<const llvm::MemoryBuffer *, 8> Buffers; 256 257 ~MemBufferOwner() { 258 for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator 259 I = Buffers.begin(), E = Buffers.end(); I != E; ++I) 260 delete *I; 261 } 262}; 263 264} // anonymous namespace 265 266static void clang_indexTranslationUnit_Impl(void *UserData) { 267 IndexTranslationUnitInfo *ITUI = 268 static_cast<IndexTranslationUnitInfo*>(UserData); 269 CXIndex CIdx = ITUI->CIdx; 270 CXClientData client_data = ITUI->client_data; 271 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; 272 unsigned index_callbacks_size = ITUI->index_callbacks_size; 273 unsigned index_options = ITUI->index_options; 274 const char *source_filename = ITUI->source_filename; 275 const char * const *command_line_args = ITUI->command_line_args; 276 int num_command_line_args = ITUI->num_command_line_args; 277 struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files; 278 unsigned num_unsaved_files = ITUI->num_unsaved_files; 279 CXTranslationUnit *out_TU = ITUI->out_TU; 280 unsigned TU_options = ITUI->TU_options; 281 ITUI->result = 1; // init as error. 282 283 if (out_TU) 284 *out_TU = 0; 285 bool requestedToGetTU = (out_TU != 0); 286 287 if (!CIdx) 288 return; 289 if (!client_index_callbacks || index_callbacks_size == 0) 290 return; 291 292 IndexerCallbacks CB; 293 memset(&CB, 0, sizeof(CB)); 294 unsigned ClientCBSize = index_callbacks_size < sizeof(CB) 295 ? index_callbacks_size : sizeof(CB); 296 memcpy(&CB, client_index_callbacks, ClientCBSize); 297 298 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 299 300 (void)CXXIdx; 301 (void)TU_options; 302 303 CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer(); 304 305 // Configure the diagnostics. 306 DiagnosticOptions DiagOpts; 307 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> 308 Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, 309 command_line_args, 310 CaptureDiag, 311 /*ShouldOwnClient=*/true)); 312 313 // Recover resources if we crash before exiting this function. 314 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 315 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 316 DiagCleanup(Diags.getPtr()); 317 318 llvm::OwningPtr<std::vector<const char *> > 319 Args(new std::vector<const char*>()); 320 321 // Recover resources if we crash before exiting this method. 322 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > 323 ArgsCleanup(Args.get()); 324 325 Args->insert(Args->end(), command_line_args, 326 command_line_args + num_command_line_args); 327 328 // The 'source_filename' argument is optional. If the caller does not 329 // specify it then it is assumed that the source file is specified 330 // in the actual argument list. 331 // Put the source file after command_line_args otherwise if '-x' flag is 332 // present it will be unused. 333 if (source_filename) 334 Args->push_back(source_filename); 335 336 llvm::IntrusiveRefCntPtr<CompilerInvocation> 337 CInvok(createInvocationFromCommandLine(*Args, Diags)); 338 339 if (!CInvok) 340 return; 341 342 // Recover resources if we crash before exiting this function. 343 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, 344 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> > 345 CInvokCleanup(CInvok.getPtr()); 346 347 if (CInvok->getFrontendOpts().Inputs.empty()) 348 return; 349 350 llvm::OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner()); 351 352 // Recover resources if we crash before exiting this method. 353 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> 354 BufOwnerCleanup(BufOwner.get()); 355 356 for (unsigned I = 0; I != num_unsaved_files; ++I) { 357 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 358 const llvm::MemoryBuffer *Buffer 359 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 360 CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer); 361 BufOwner->Buffers.push_back(Buffer); 362 } 363 364 // Since libclang is primarily used by batch tools dealing with 365 // (often very broken) source code, where spell-checking can have a 366 // significant negative impact on performance (particularly when 367 // precompiled headers are involved), we disable it. 368 CInvok->getLangOpts().SpellChecking = false; 369 370 if (!requestedToGetTU) 371 CInvok->getPreprocessorOpts().DetailedRecord = false; 372 373 ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags); 374 llvm::OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(Unit))); 375 376 // Recover resources if we crash before exiting this method. 377 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner> 378 CXTUCleanup(CXTU.get()); 379 380 llvm::OwningPtr<IndexingFrontendAction> IndexAction; 381 IndexAction.reset(new IndexingFrontendAction(client_data, CB, 382 index_options, CXTU->getTU())); 383 384 // Recover resources if we crash before exiting this method. 385 llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction> 386 IndexActionCleanup(IndexAction.get()); 387 388 Unit = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags, 389 IndexAction.get(), 390 Unit); 391 if (!Unit) 392 return; 393 394 // FIXME: Set state of the ASTUnit according to the TU_options. 395 if (out_TU) 396 *out_TU = CXTU->takeTU(); 397 398 ITUI->result = 0; // success. 399} 400 401//===----------------------------------------------------------------------===// 402// libclang public APIs. 403//===----------------------------------------------------------------------===// 404 405extern "C" { 406 407int clang_index_isEntityTagKind(CXIdxEntityKind K) { 408 return CXIdxEntity_Enum <= K && K <= CXIdxEntity_CXXClass; 409} 410 411CXIdxTagDeclInfo *clang_index_getTagDeclInfo(CXIdxDeclInfo *DInfo) { 412 if (clang_index_isEntityTagKind(DInfo->entityInfo->kind)) 413 return &static_cast<TagDeclInfo*>(DInfo)->CXTagDeclInfo; 414 415 return 0; 416} 417 418int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) { 419 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory; 420} 421 422CXIdxObjCContainerDeclInfo * 423clang_index_getObjCContainerDeclInfo(CXIdxDeclInfo *DInfo) { 424 if (clang_index_isEntityObjCContainerKind(DInfo->entityInfo->kind)) 425 return &static_cast<ObjCContainerDeclInfo*>(DInfo)->CXObjCContDeclInfo; 426 427 return 0; 428} 429 430int clang_index_isEntityObjCCategoryKind(CXIdxEntityKind K) { 431 return K == CXIdxEntity_ObjCCategory; 432} 433 434CXIdxObjCCategoryDeclInfo * 435clang_index_getObjCCategoryDeclInfo(CXIdxDeclInfo *DInfo){ 436 if (clang_index_isEntityObjCCategoryKind(DInfo->entityInfo->kind)) 437 return &static_cast<ObjCCategoryDeclInfo*>(DInfo)->CXObjCCatDeclInfo; 438 439 return 0; 440} 441 442int clang_indexTranslationUnit(CXIndex CIdx, 443 CXClientData client_data, 444 IndexerCallbacks *index_callbacks, 445 unsigned index_callbacks_size, 446 unsigned index_options, 447 const char *source_filename, 448 const char * const *command_line_args, 449 int num_command_line_args, 450 struct CXUnsavedFile *unsaved_files, 451 unsigned num_unsaved_files, 452 CXTranslationUnit *out_TU, 453 unsigned TU_options) { 454 IndexTranslationUnitInfo ITUI = { CIdx, client_data, index_callbacks, 455 index_callbacks_size, index_options, 456 source_filename, command_line_args, 457 num_command_line_args, unsaved_files, 458 num_unsaved_files, out_TU, TU_options, 0 }; 459 460 if (getenv("LIBCLANG_NOTHREADS")) { 461 clang_indexTranslationUnit_Impl(&ITUI); 462 return ITUI.result; 463 } 464 465 llvm::CrashRecoveryContext CRC; 466 467 if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) { 468 fprintf(stderr, "libclang: crash detected during parsing: {\n"); 469 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); 470 fprintf(stderr, " 'command_line_args' : ["); 471 for (int i = 0; i != num_command_line_args; ++i) { 472 if (i) 473 fprintf(stderr, ", "); 474 fprintf(stderr, "'%s'", command_line_args[i]); 475 } 476 fprintf(stderr, "],\n"); 477 fprintf(stderr, " 'unsaved_files' : ["); 478 for (unsigned i = 0; i != num_unsaved_files; ++i) { 479 if (i) 480 fprintf(stderr, ", "); 481 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, 482 unsaved_files[i].Length); 483 } 484 fprintf(stderr, "],\n"); 485 fprintf(stderr, " 'options' : %d,\n", TU_options); 486 fprintf(stderr, "}\n"); 487 488 return 1; 489 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { 490 if (out_TU) 491 PrintLibclangResourceUsage(*out_TU); 492 } 493 494 return ITUI.result; 495} 496 497void clang_indexLoc_getFileLocation(CXIdxLoc location, 498 CXIdxClientFile *indexFile, 499 CXFile *file, 500 unsigned *line, 501 unsigned *column, 502 unsigned *offset) { 503 if (indexFile) *indexFile = 0; 504 if (file) *file = 0; 505 if (line) *line = 0; 506 if (column) *column = 0; 507 if (offset) *offset = 0; 508 509 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 510 if (!location.ptr_data[0] || Loc.isInvalid()) 511 return; 512 513 IndexingContext &IndexCtx = 514 *static_cast<IndexingContext*>(location.ptr_data[0]); 515 IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset); 516} 517 518CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { 519 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 520 if (!location.ptr_data[0] || Loc.isInvalid()) 521 return clang_getNullLocation(); 522 523 IndexingContext &IndexCtx = 524 *static_cast<IndexingContext*>(location.ptr_data[0]); 525 return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc); 526} 527 528} // end: extern "C" 529 530