[llvm-cov] Improve error message by printing the object file name that produces error

If error occurs on constructing coverage info for one of the object files, it prints the name of the object file, so that users know which one is the cause of error.

Differential Revision: https://reviews.llvm.org/D130196
This commit is contained in:
Zequan Wu 2022-07-21 10:18:16 -07:00
parent d870a57563
commit 4979b16db1
5 changed files with 14 additions and 9 deletions

View File

@ -349,7 +349,7 @@ CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
StringRef CompilationDir) { StringRef CompilationDir) {
auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
if (Error E = ProfileReaderOrErr.takeError()) if (Error E = ProfileReaderOrErr.takeError())
return std::move(E); return createFileError(ProfileFilename, std::move(E));
auto ProfileReader = std::move(ProfileReaderOrErr.get()); auto ProfileReader = std::move(ProfileReaderOrErr.get());
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping()); auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
bool DataFound = false; bool DataFound = false;
@ -358,7 +358,7 @@ CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN( auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false); File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
if (std::error_code EC = CovMappingBufOrErr.getError()) if (std::error_code EC = CovMappingBufOrErr.getError())
return errorCodeToError(EC); return createFileError(File.value(), errorCodeToError(EC));
StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()]; StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()];
MemoryBufferRef CovMappingBufRef = MemoryBufferRef CovMappingBufRef =
CovMappingBufOrErr.get()->getMemBufferRef(); CovMappingBufOrErr.get()->getMemBufferRef();
@ -368,7 +368,7 @@ CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
if (Error E = CoverageReadersOrErr.takeError()) { if (Error E = CoverageReadersOrErr.takeError()) {
E = handleMaybeNoDataFoundError(std::move(E)); E = handleMaybeNoDataFoundError(std::move(E));
if (E) if (E)
return std::move(E); return createFileError(File.value(), std::move(E));
// E == success (originally a no_data_found error). // E == success (originally a no_data_found error).
continue; continue;
} }
@ -378,12 +378,14 @@ CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
Readers.push_back(std::move(Reader)); Readers.push_back(std::move(Reader));
DataFound |= !Readers.empty(); DataFound |= !Readers.empty();
if (Error E = loadFromReaders(Readers, *ProfileReader, *Coverage)) if (Error E = loadFromReaders(Readers, *ProfileReader, *Coverage))
return std::move(E); return createFileError(File.value(), std::move(E));
} }
// If no readers were created, either no objects were provided or none of them // If no readers were created, either no objects were provided or none of them
// had coverage data. Return an error in the latter case. // had coverage data. Return an error in the latter case.
if (!DataFound && !ObjectFilenames.empty()) if (!DataFound && !ObjectFilenames.empty())
return make_error<CoverageMapError>(coveragemap_error::no_data_found); return createFileError(
join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "),
make_error<CoverageMapError>(coveragemap_error::no_data_found));
return std::move(Coverage); return std::move(Coverage);
} }

View File

@ -0,0 +1,4 @@
RUN: llvm-profdata merge %S/Inputs/binary-formats.proftext -o %t.profdata
RUN: not llvm-cov show -instr-profile=%t.profdata --object=%t.nonexistent.binary.1 --object=%t.nonexistent.binary.2 2>&1 | FileCheck %s
CHECK: Failed to load coverage: '{{.*}}nonexistent.binary.1':

View File

@ -14,7 +14,7 @@ int main(int argc, const char *argv[]) {}
// RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -path-equivalence=/tmp,%S %s 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s // RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -path-equivalence=/tmp,%S %s 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s
// RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -path-equivalence=/tmp,%S %s -arch i386 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s // RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -path-equivalence=/tmp,%S %s -arch i386 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s
// WRONG-ARCH: Failed to load coverage: `-arch` specifier is invalid or missing for universal binary // WRONG-ARCH: Failed to load coverage: '{{.*}}universal-binary': `-arch` specifier is invalid or missing for universal binary
// RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -path-equivalence=/tmp,%S %s -arch definitly_a_made_up_architecture 2>&1 | FileCheck --check-prefix=MADE-UP-ARCH %s // RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -path-equivalence=/tmp,%S %s -arch definitly_a_made_up_architecture 2>&1 | FileCheck --check-prefix=MADE-UP-ARCH %s
// MADE-UP-ARCH: Unknown architecture: definitly_a_made_up_architecture // MADE-UP-ARCH: Unknown architecture: definitly_a_made_up_architecture

View File

@ -13,4 +13,4 @@
// FAKE-FUNC-STDERR: Could not read coverage for '{{.*}}'. // FAKE-FUNC-STDERR: Could not read coverage for '{{.*}}'.
// RUN: not llvm-cov report %S/Inputs/malformedRegions.covmapping -instr-profile %S/Inputs/elf_binary_comdat.profdata 2>&1 | FileCheck %s -check-prefix=MALFORMED-REGION // RUN: not llvm-cov report %S/Inputs/malformedRegions.covmapping -instr-profile %S/Inputs/elf_binary_comdat.profdata 2>&1 | FileCheck %s -check-prefix=MALFORMED-REGION
// MALFORMED-REGION: malformedRegions.covmapping: Failed to load coverage: Malformed coverage data // MALFORMED-REGION: Failed to load coverage: '{{.*}}malformedRegions.covmapping': Malformed coverage data

View File

@ -436,8 +436,7 @@ std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches, CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches,
ViewOpts.CompilationDirectory); ViewOpts.CompilationDirectory);
if (Error E = CoverageOrErr.takeError()) { if (Error E = CoverageOrErr.takeError()) {
error("Failed to load coverage: " + toString(std::move(E)), error("Failed to load coverage: " + toString(std::move(E)));
join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
return nullptr; return nullptr;
} }
auto Coverage = std::move(CoverageOrErr.get()); auto Coverage = std::move(CoverageOrErr.get());