Skip to content

[llvm-cov] - Output better error message when the error kind is coveragemap_error::malforme. #65264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ inline std::error_code make_error_code(coveragemap_error E) {

class CoverageMapError : public ErrorInfo<CoverageMapError> {
public:
CoverageMapError(coveragemap_error Err) : Err(Err) {
CoverageMapError(coveragemap_error Err, const Twine &ErrStr = Twine())
: Err(Err), Msg(ErrStr.str()) {
assert(Err != coveragemap_error::success && "Not an error");
}

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

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

static char ID;

private:
coveragemap_error Err;
std::string Msg;
};

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

Expand Down
46 changes: 33 additions & 13 deletions llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ Error CoverageMapping::loadFunctionRecord(
IndexedInstrProfReader &ProfileReader) {
StringRef OrigFuncName = Record.FunctionName;
if (OrigFuncName.empty())
return make_error<CoverageMapError>(coveragemap_error::malformed);
return make_error<CoverageMapError>(coveragemap_error::malformed,
"record function name is empty");

if (Record.Filenames.empty())
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
Expand Down Expand Up @@ -342,7 +343,7 @@ static Error handleMaybeNoDataFoundError(Error E) {
std::move(E), [](const CoverageMapError &CME) {
if (CME.get() == coveragemap_error::no_data_found)
return static_cast<Error>(Error::success());
return make_error<CoverageMapError>(CME.get());
return make_error<CoverageMapError>(CME.get(), CME.getMessage());
});
}

Expand Down Expand Up @@ -925,26 +926,45 @@ LineCoverageIterator &LineCoverageIterator::operator++() {
return *this;
}

static std::string getCoverageMapErrString(coveragemap_error Err) {
static std::string getCoverageMapErrString(coveragemap_error Err,
const std::string &ErrMsg = "") {
std::string Msg;
raw_string_ostream OS(Msg);

switch (Err) {
case coveragemap_error::success:
return "Success";
OS << "success";
break;
case coveragemap_error::eof:
return "End of File";
OS << "end of File";
break;
case coveragemap_error::no_data_found:
return "No coverage data found";
OS << "no coverage data found";
break;
case coveragemap_error::unsupported_version:
return "Unsupported coverage format version";
OS << "unsupported coverage format version";
break;
case coveragemap_error::truncated:
return "Truncated coverage data";
OS << "truncated coverage data";
break;
case coveragemap_error::malformed:
return "Malformed coverage data";
OS << "malformed coverage data";
break;
case coveragemap_error::decompression_failed:
return "Failed to decompress coverage data (zlib)";
OS << "failed to decompress coverage data (zlib)";
break;
case coveragemap_error::invalid_or_missing_arch_specifier:
return "`-arch` specifier is invalid or missing for universal binary";
OS << "`-arch` specifier is invalid or missing for universal binary";
break;
default:
llvm_unreachable("invalid coverage mapping error.");
}
llvm_unreachable("A value of coveragemap_error has no message.");

// If optional error message is not empty, append it to the message.
if (!ErrMsg.empty())
OS << ": " << ErrMsg;

return Msg;
}

namespace {
Expand All @@ -962,7 +982,7 @@ class CoverageMappingErrorCategoryType : public std::error_category {
} // end anonymous namespace

std::string CoverageMapError::message() const {
return getCoverageMapErrString(Err);
return getCoverageMapErrString(Err, Msg);
}

const std::error_category &llvm::coverage::coveragemap_category() {
Expand Down
Loading