Skip to content

Commit cd8fe1d

Browse files
authored
[llvm-cov] - Output better error message when the error kind is coveragemap_error::malforme. (#65264)
The current llvm-cov error message for kind `coveragemap_error::malforme`, just gives the issue kind without any reason for what caused the issue. This patch is aimed at improving the llvm-cov error message to help identify what caused the issue. Reviewed By: MaskRay Close: #65264
1 parent 6258912 commit cd8fe1d

File tree

11 files changed

+147
-74
lines changed

11 files changed

+147
-74
lines changed

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ inline std::error_code make_error_code(coveragemap_error E) {
7575

7676
class CoverageMapError : public ErrorInfo<CoverageMapError> {
7777
public:
78-
CoverageMapError(coveragemap_error Err) : Err(Err) {
78+
CoverageMapError(coveragemap_error Err, const Twine &ErrStr = Twine())
79+
: Err(Err), Msg(ErrStr.str()) {
7980
assert(Err != coveragemap_error::success && "Not an error");
8081
}
8182

@@ -88,11 +89,13 @@ class CoverageMapError : public ErrorInfo<CoverageMapError> {
8889
}
8990

9091
coveragemap_error get() const { return Err; }
92+
const std::string &getMessage() const { return Msg; }
9193

9294
static char ID;
9395

9496
private:
9597
coveragemap_error Err;
98+
std::string Msg;
9699
};
97100

98101
/// A Counter is an abstract value that describes how to compute the
@@ -864,7 +867,8 @@ struct CovMapFunctionRecordV1 {
864867
uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
865868
FuncName = ProfileNames.getFuncName(NameRef, NameS);
866869
if (NameS && FuncName.empty())
867-
return make_error<CoverageMapError>(coveragemap_error::malformed);
870+
return make_error<CoverageMapError>(coveragemap_error::malformed,
871+
"function name is empty");
868872
return Error::success();
869873
}
870874

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

+33-13
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ Error CoverageMapping::loadFunctionRecord(
237237
IndexedInstrProfReader &ProfileReader) {
238238
StringRef OrigFuncName = Record.FunctionName;
239239
if (OrigFuncName.empty())
240-
return make_error<CoverageMapError>(coveragemap_error::malformed);
240+
return make_error<CoverageMapError>(coveragemap_error::malformed,
241+
"record function name is empty");
241242

242243
if (Record.Filenames.empty())
243244
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
@@ -342,7 +343,7 @@ static Error handleMaybeNoDataFoundError(Error E) {
342343
std::move(E), [](const CoverageMapError &CME) {
343344
if (CME.get() == coveragemap_error::no_data_found)
344345
return static_cast<Error>(Error::success());
345-
return make_error<CoverageMapError>(CME.get());
346+
return make_error<CoverageMapError>(CME.get(), CME.getMessage());
346347
});
347348
}
348349

@@ -925,26 +926,45 @@ LineCoverageIterator &LineCoverageIterator::operator++() {
925926
return *this;
926927
}
927928

928-
static std::string getCoverageMapErrString(coveragemap_error Err) {
929+
static std::string getCoverageMapErrString(coveragemap_error Err,
930+
const std::string &ErrMsg = "") {
931+
std::string Msg;
932+
raw_string_ostream OS(Msg);
933+
929934
switch (Err) {
930935
case coveragemap_error::success:
931-
return "Success";
936+
OS << "success";
937+
break;
932938
case coveragemap_error::eof:
933-
return "End of File";
939+
OS << "end of File";
940+
break;
934941
case coveragemap_error::no_data_found:
935-
return "No coverage data found";
942+
OS << "no coverage data found";
943+
break;
936944
case coveragemap_error::unsupported_version:
937-
return "Unsupported coverage format version";
945+
OS << "unsupported coverage format version";
946+
break;
938947
case coveragemap_error::truncated:
939-
return "Truncated coverage data";
948+
OS << "truncated coverage data";
949+
break;
940950
case coveragemap_error::malformed:
941-
return "Malformed coverage data";
951+
OS << "malformed coverage data";
952+
break;
942953
case coveragemap_error::decompression_failed:
943-
return "Failed to decompress coverage data (zlib)";
954+
OS << "failed to decompress coverage data (zlib)";
955+
break;
944956
case coveragemap_error::invalid_or_missing_arch_specifier:
945-
return "`-arch` specifier is invalid or missing for universal binary";
957+
OS << "`-arch` specifier is invalid or missing for universal binary";
958+
break;
959+
default:
960+
llvm_unreachable("invalid coverage mapping error.");
946961
}
947-
llvm_unreachable("A value of coveragemap_error has no message.");
962+
963+
// If optional error message is not empty, append it to the message.
964+
if (!ErrMsg.empty())
965+
OS << ": " << ErrMsg;
966+
967+
return Msg;
948968
}
949969

950970
namespace {
@@ -962,7 +982,7 @@ class CoverageMappingErrorCategoryType : public std::error_category {
962982
} // end anonymous namespace
963983

964984
std::string CoverageMapError::message() const {
965-
return getCoverageMapErrString(Err);
985+
return getCoverageMapErrString(Err, Msg);
966986
}
967987

968988
const std::error_category &llvm::coverage::coveragemap_category() {

0 commit comments

Comments
 (0)