Skip to content

Commit 7fafaa0

Browse files
committed
[llvm-cov] Warn when -arch spec is missing/invalid for universal binary (reland)
llvm-cov reports a poor error message when the -arch specifier is missing or invalid, and a binary has multiple slices. Make the error message more specific. (This version of the patch avoids using llvm::none_of -- the way I used the utility caused compile errors on many bots, possibly because the wrong overload of `none_of` was selected.) rdar://40312677
1 parent 7d67c06 commit 7fafaa0

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ enum class coveragemap_error {
5555
unsupported_version,
5656
truncated,
5757
malformed,
58-
decompression_failed
58+
decompression_failed,
59+
invalid_or_missing_arch_specifier
5960
};
6061

6162
const std::error_category &coveragemap_category();

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ static std::string getCoverageMapErrString(coveragemap_error Err) {
816816
return "Malformed coverage data";
817817
case coveragemap_error::decompression_failed:
818818
return "Failed to decompress coverage data (zlib)";
819+
case coveragemap_error::invalid_or_missing_arch_specifier:
820+
return "`-arch` specifier is invalid or missing for universal binary";
819821
}
820822
llvm_unreachable("A value of coveragemap_error has no message.");
821823
}

llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,19 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch) {
950950
BytesInAddress, Endian);
951951
}
952952

953+
/// Determine whether \p Arch is invalid or empty, given \p Bin.
954+
static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {
955+
// If we have a universal binary and Arch doesn't identify any of its slices,
956+
// it's user error.
957+
if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
958+
for (auto &ObjForArch : Universal->objects())
959+
if (Arch == ObjForArch.getArchFlagName())
960+
return false;
961+
return true;
962+
}
963+
return false;
964+
}
965+
953966
Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
954967
BinaryCoverageReader::create(
955968
MemoryBufferRef ObjectBuffer, StringRef Arch,
@@ -970,6 +983,10 @@ BinaryCoverageReader::create(
970983
return BinOrErr.takeError();
971984
std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
972985

986+
if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
987+
return make_error<CoverageMapError>(
988+
coveragemap_error::invalid_or_missing_arch_specifier);
989+
973990
// MachO universal binaries which contain archives need to be treated as
974991
// archives, not as regular binaries.
975992
if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {

llvm/test/tools/llvm-cov/universal-binary.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ int main(int argc, const char *argv[]) {}
1010
// COMBINED: showTemplateInstantiations.cpp
1111
// COMBINED-NEXT: universal-binary.c
1212

13+
// 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
1314
// 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
14-
// WRONG-ARCH: Failed to load coverage
15+
// WRONG-ARCH: Failed to load coverage: `-arch` specifier is invalid or missing for universal binary
1516

1617
// 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
1718
// MADE-UP-ARCH: Unknown architecture: definitly_a_made_up_architecture

0 commit comments

Comments
 (0)