From 4979b16db1b743d3505d1bf58bf0bf7225f2acc0 Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Thu, 21 Jul 2022 10:18:16 -0700 Subject: [PATCH] [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 --- llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 12 +++++++----- llvm/test/tools/llvm-cov/missing-binaries.test | 4 ++++ llvm/test/tools/llvm-cov/universal-binary.c | 2 +- llvm/test/tools/llvm-cov/warnings.h | 2 +- llvm/tools/llvm-cov/CodeCoverage.cpp | 3 +-- 5 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 llvm/test/tools/llvm-cov/missing-binaries.test diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp index f4f13bafb233..8c1eadbe8271 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -349,7 +349,7 @@ CoverageMapping::load(ArrayRef ObjectFilenames, StringRef CompilationDir) { auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); if (Error E = ProfileReaderOrErr.takeError()) - return std::move(E); + return createFileError(ProfileFilename, std::move(E)); auto ProfileReader = std::move(ProfileReaderOrErr.get()); auto Coverage = std::unique_ptr(new CoverageMapping()); bool DataFound = false; @@ -358,7 +358,7 @@ CoverageMapping::load(ArrayRef ObjectFilenames, auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN( File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false); if (std::error_code EC = CovMappingBufOrErr.getError()) - return errorCodeToError(EC); + return createFileError(File.value(), errorCodeToError(EC)); StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()]; MemoryBufferRef CovMappingBufRef = CovMappingBufOrErr.get()->getMemBufferRef(); @@ -368,7 +368,7 @@ CoverageMapping::load(ArrayRef ObjectFilenames, if (Error E = CoverageReadersOrErr.takeError()) { E = handleMaybeNoDataFoundError(std::move(E)); if (E) - return std::move(E); + return createFileError(File.value(), std::move(E)); // E == success (originally a no_data_found error). continue; } @@ -378,12 +378,14 @@ CoverageMapping::load(ArrayRef ObjectFilenames, Readers.push_back(std::move(Reader)); DataFound |= !Readers.empty(); 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 // had coverage data. Return an error in the latter case. if (!DataFound && !ObjectFilenames.empty()) - return make_error(coveragemap_error::no_data_found); + return createFileError( + join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "), + make_error(coveragemap_error::no_data_found)); return std::move(Coverage); } diff --git a/llvm/test/tools/llvm-cov/missing-binaries.test b/llvm/test/tools/llvm-cov/missing-binaries.test new file mode 100644 index 000000000000..e0ba107ac9f4 --- /dev/null +++ b/llvm/test/tools/llvm-cov/missing-binaries.test @@ -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': diff --git a/llvm/test/tools/llvm-cov/universal-binary.c b/llvm/test/tools/llvm-cov/universal-binary.c index 5c970ac54480..985bad345cd1 100644 --- a/llvm/test/tools/llvm-cov/universal-binary.c +++ b/llvm/test/tools/llvm-cov/universal-binary.c @@ -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 -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 // MADE-UP-ARCH: Unknown architecture: definitly_a_made_up_architecture diff --git a/llvm/test/tools/llvm-cov/warnings.h b/llvm/test/tools/llvm-cov/warnings.h index 52bd6926f281..b7b124565f59 100644 --- a/llvm/test/tools/llvm-cov/warnings.h +++ b/llvm/test/tools/llvm-cov/warnings.h @@ -13,4 +13,4 @@ // 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 -// MALFORMED-REGION: malformedRegions.covmapping: Failed to load coverage: Malformed coverage data +// MALFORMED-REGION: Failed to load coverage: '{{.*}}malformedRegions.covmapping': Malformed coverage data diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp index 27e181c129b8..13b6c3002216 100644 --- a/llvm/tools/llvm-cov/CodeCoverage.cpp +++ b/llvm/tools/llvm-cov/CodeCoverage.cpp @@ -436,8 +436,7 @@ std::unique_ptr CodeCoverageTool::load() { CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches, ViewOpts.CompilationDirectory); if (Error E = CoverageOrErr.takeError()) { - error("Failed to load coverage: " + toString(std::move(E)), - join(ObjectFilenames.begin(), ObjectFilenames.end(), ", ")); + error("Failed to load coverage: " + toString(std::move(E))); return nullptr; } auto Coverage = std::move(CoverageOrErr.get());