Index: clang/include/clang/CrossTU/CrossTranslationUnit.h =================================================================== --- clang/include/clang/CrossTU/CrossTranslationUnit.h +++ clang/include/clang/CrossTU/CrossTranslationUnit.h @@ -38,6 +38,7 @@ namespace cross_tu { enum class index_error_code { + no_error = 0, unspecified = 1, missing_index_file, invalid_index_format, @@ -253,6 +254,7 @@ /// In case of on-demand parsing, the invocations for parsing the source /// files is stored. llvm::Optional InvocationList; + index_error_code InvocationListParsingError = index_error_code::no_error; }; /// Maintain number of AST loads and check for reaching the load limit. Index: clang/lib/CrossTU/CrossTranslationUnit.cpp =================================================================== --- clang/lib/CrossTU/CrossTranslationUnit.cpp +++ clang/lib/CrossTU/CrossTranslationUnit.cpp @@ -667,12 +667,16 @@ /// Lazily initialize the invocation list member used for on-demand parsing. if (InvocationList) return llvm::Error::success(); + else if (index_error_code::no_error != InvocationListParsingError) + return llvm::make_error(InvocationListParsingError); llvm::ErrorOr> FileContent = llvm::MemoryBuffer::getFile(InvocationListFilePath); - if (!FileContent) - return llvm::make_error( - index_error_code::invocation_list_file_not_found); + if (!FileContent) { + InvocationListParsingError = + index_error_code::invocation_list_file_not_found; + return llvm::make_error(InvocationListParsingError); + } std::unique_ptr ContentBuffer = std::move(*FileContent); assert(ContentBuffer && "If no error was produced after loading, the pointer " "should not be nullptr."); @@ -680,8 +684,13 @@ llvm::Expected ExpectedInvocationList = parseInvocationList(ContentBuffer->getBuffer(), PathStyle); - if (!ExpectedInvocationList) - return ExpectedInvocationList.takeError(); + // Handle the error to store the code for next call to this function. + if (!ExpectedInvocationList) { + llvm::handleAllErrors( + ExpectedInvocationList.takeError(), + [&](IndexError &E) { InvocationListParsingError = E.getCode(); }); + return llvm::make_error(InvocationListParsingError); + } InvocationList = *ExpectedInvocationList;