diff options
Diffstat (limited to 'clang/tools/libclang/Indexing.cpp')
| -rw-r--r-- | clang/tools/libclang/Indexing.cpp | 146 |
1 files changed, 46 insertions, 100 deletions
diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp index e35640029e6..8a0498497e0 100644 --- a/clang/tools/libclang/Indexing.cpp +++ b/clang/tools/libclang/Indexing.cpp @@ -462,48 +462,24 @@ struct IndexSessionData { : CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {} }; -struct IndexSourceFileInfo { - CXIndexAction idxAction; - CXClientData client_data; - IndexerCallbacks *index_callbacks; - unsigned index_callbacks_size; - unsigned index_options; - const char *source_filename; - const char *const *command_line_args; - int num_command_line_args; - ArrayRef<CXUnsavedFile> unsaved_files; - CXTranslationUnit *out_TU; - unsigned TU_options; - CXErrorCode &result; -}; - } // anonymous namespace -static void clang_indexSourceFile_Impl(void *UserData) { - const IndexSourceFileInfo *ITUI = - static_cast<IndexSourceFileInfo *>(UserData); - CXIndexAction cxIdxAction = ITUI->idxAction; - CXClientData client_data = ITUI->client_data; - IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; - unsigned index_callbacks_size = ITUI->index_callbacks_size; - unsigned index_options = ITUI->index_options; - const char *source_filename = ITUI->source_filename; - const char * const *command_line_args = ITUI->command_line_args; - int num_command_line_args = ITUI->num_command_line_args; - CXTranslationUnit *out_TU = ITUI->out_TU; - unsigned TU_options = ITUI->TU_options; - +static CXErrorCode clang_indexSourceFile_Impl( + CXIndexAction cxIdxAction, CXClientData client_data, + IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size, + unsigned index_options, const char *source_filename, + const char *const *command_line_args, int num_command_line_args, + ArrayRef<CXUnsavedFile> unsaved_files, CXTranslationUnit *out_TU, + unsigned TU_options) { if (out_TU) *out_TU = nullptr; bool requestedToGetTU = (out_TU != nullptr); if (!cxIdxAction) { - ITUI->result = CXError_InvalidArguments; - return; + return CXError_InvalidArguments; } if (!client_index_callbacks || index_callbacks_size == 0) { - ITUI->result = CXError_InvalidArguments; - return; + return CXError_InvalidArguments; } IndexerCallbacks CB; @@ -557,7 +533,7 @@ static void clang_indexSourceFile_Impl(void *UserData) { CInvok(createInvocationFromCommandLine(*Args, Diags)); if (!CInvok) - return; + return CXError_Failure; // Recover resources if we crash before exiting this function. llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, @@ -565,7 +541,7 @@ static void clang_indexSourceFile_Impl(void *UserData) { CInvokCleanup(CInvok.get()); if (CInvok->getFrontendOpts().Inputs.empty()) - return; + return CXError_Failure; typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner; std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner); @@ -574,7 +550,7 @@ static void clang_indexSourceFile_Impl(void *UserData) { llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup( BufOwner.get()); - for (auto &UF : ITUI->unsaved_files) { + for (auto &UF : unsaved_files) { std::unique_ptr<llvm::MemoryBuffer> MB = llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); CInvok->getPreprocessorOpts().addRemappedFile(UF.Filename, MB.get()); @@ -592,10 +568,8 @@ static void clang_indexSourceFile_Impl(void *UserData) { ASTUnit *Unit = ASTUnit::create(CInvok.get(), Diags, CaptureDiagnostics, /*UserFilesAreVolatile=*/true); - if (!Unit) { - ITUI->result = CXError_InvalidArguments; - return; - } + if (!Unit) + return CXError_InvalidArguments; std::unique_ptr<CXTUOwner> CXTU( new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit))); @@ -653,38 +627,22 @@ static void clang_indexSourceFile_Impl(void *UserData) { if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics()) printDiagsToStderr(Unit); - if (isASTReadError(Unit)) { - ITUI->result = CXError_ASTReadError; - return; - } + if (isASTReadError(Unit)) + return CXError_ASTReadError; if (!Success) - return; + return CXError_Failure; if (out_TU) *out_TU = CXTU->takeTU(); - ITUI->result = CXError_Success; + return CXError_Success; } //===----------------------------------------------------------------------===// // clang_indexTranslationUnit Implementation //===----------------------------------------------------------------------===// -namespace { - -struct IndexTranslationUnitInfo { - CXIndexAction idxAction; - CXClientData client_data; - IndexerCallbacks *index_callbacks; - unsigned index_callbacks_size; - unsigned index_options; - CXTranslationUnit TU; - int result; -}; - -} // anonymous namespace - static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) { Preprocessor &PP = Unit.getPreprocessor(); if (!PP.getPreprocessingRecord()) @@ -728,27 +686,17 @@ static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) { IdxCtx.handleDiagnosticSet(DiagSet); } -static void clang_indexTranslationUnit_Impl(void *UserData) { - IndexTranslationUnitInfo *ITUI = - static_cast<IndexTranslationUnitInfo*>(UserData); - CXTranslationUnit TU = ITUI->TU; - CXClientData client_data = ITUI->client_data; - IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; - unsigned index_callbacks_size = ITUI->index_callbacks_size; - unsigned index_options = ITUI->index_options; - - // Set up the initial return value. - ITUI->result = CXError_Failure; - +static CXErrorCode clang_indexTranslationUnit_Impl( + CXIndexAction idxAction, CXClientData client_data, + IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size, + unsigned index_options, CXTranslationUnit TU) { // Check arguments. if (isNotUsableTU(TU)) { LOG_BAD_TU(TU); - ITUI->result = CXError_InvalidArguments; - return; + return CXError_InvalidArguments; } if (!client_index_callbacks || index_callbacks_size == 0) { - ITUI->result = CXError_InvalidArguments; - return; + return CXError_InvalidArguments; } CIndexer *CXXIdx = TU->CIdx; @@ -777,7 +725,7 @@ static void clang_indexTranslationUnit_Impl(void *UserData) { ASTUnit *Unit = cxtu::getASTUnit(TU); if (!Unit) - return; + return CXError_Failure; ASTUnit::ConcurrencyCheck Check(*Unit); @@ -797,7 +745,7 @@ static void clang_indexTranslationUnit_Impl(void *UserData) { indexTranslationUnit(*Unit, *IndexCtx); indexDiagnostics(TU, *IndexCtx); - ITUI->result = CXError_Success; + return CXError_Success; } //===----------------------------------------------------------------------===// @@ -969,28 +917,23 @@ int clang_indexSourceFile(CXIndexAction idxAction, return CXError_InvalidArguments; CXErrorCode result = CXError_Failure; - IndexSourceFileInfo ITUI = { - idxAction, - client_data, - index_callbacks, - index_callbacks_size, - index_options, - source_filename, - command_line_args, - num_command_line_args, - llvm::makeArrayRef(unsaved_files, num_unsaved_files), - out_TU, - TU_options, - result}; + auto IndexSourceFileImpl = [=, &result]() { + result = clang_indexSourceFile_Impl( + idxAction, client_data, index_callbacks, index_callbacks_size, + index_options, source_filename, command_line_args, + num_command_line_args, + llvm::makeArrayRef(unsaved_files, num_unsaved_files), out_TU, + TU_options); + }; if (getenv("LIBCLANG_NOTHREADS")) { - clang_indexSourceFile_Impl(&ITUI); + IndexSourceFileImpl(); return result; } llvm::CrashRecoveryContext CRC; - if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) { + if (!RunSafely(CRC, IndexSourceFileImpl)) { fprintf(stderr, "libclang: crash detected during indexing source file: {\n"); fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); fprintf(stderr, " 'command_line_args' : ["); @@ -1030,24 +973,27 @@ int clang_indexTranslationUnit(CXIndexAction idxAction, *Log << TU; } - IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks, - index_callbacks_size, index_options, TU, - 0 }; + CXErrorCode result; + auto IndexTranslationUnitImpl = [=, &result]() { + result = clang_indexTranslationUnit_Impl( + idxAction, client_data, index_callbacks, index_callbacks_size, + index_options, TU); + }; if (getenv("LIBCLANG_NOTHREADS")) { - clang_indexTranslationUnit_Impl(&ITUI); - return ITUI.result; + IndexTranslationUnitImpl(); + return result; } llvm::CrashRecoveryContext CRC; - if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) { + if (!RunSafely(CRC, IndexTranslationUnitImpl)) { fprintf(stderr, "libclang: crash detected during indexing TU\n"); return 1; } - return ITUI.result; + return result; } void clang_indexLoc_getFileLocation(CXIdxLoc location, |

