Skip to content

Commit f025968

Browse files
committed
Ignore object files that lack coverage information.
Before this change, if multiple binary files were presented, all of them must have been instrumented or the load would fail with coverage_map_error::no_data_found. Patch by Dean Sturtevant. Differential Revision: https://reviews.llvm.org/D66763 llvm-svn: 370257
1 parent 0b62951 commit f025968

File tree

9 files changed

+40
-2
lines changed

9 files changed

+40
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ class CoverageMapping {
527527

528528
/// Load the coverage mapping from the given object files and profile. If
529529
/// \p Arches is non-empty, it must specify an architecture for each object.
530+
/// Ignores non-instrumented object files unless all are not instrumented.
530531
static Expected<std::unique_ptr<CoverageMapping>>
531532
load(ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
532533
ArrayRef<StringRef> Arches = None);

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
270270
return std::move(Coverage);
271271
}
272272

273+
// If E is a no_data_found error, returns success. Otherwise returns E.
274+
static Error handleMaybeNoDataFoundError(Error E) {
275+
return handleErrors(
276+
std::move(E), [](const CoverageMapError &CME) {
277+
if (CME.get() == coveragemap_error::no_data_found)
278+
return static_cast<Error>(Error::success());
279+
return make_error<CoverageMapError>(CME.get());
280+
});
281+
}
282+
273283
Expected<std::unique_ptr<CoverageMapping>>
274284
CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
275285
StringRef ProfileFilename, ArrayRef<StringRef> Arches) {
@@ -289,12 +299,21 @@ CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
289299
CovMappingBufOrErr.get()->getMemBufferRef();
290300
auto CoverageReadersOrErr =
291301
BinaryCoverageReader::create(CovMappingBufRef, Arch, Buffers);
292-
if (Error E = CoverageReadersOrErr.takeError())
293-
return std::move(E);
302+
if (Error E = CoverageReadersOrErr.takeError()) {
303+
E = handleMaybeNoDataFoundError(std::move(E));
304+
if (E)
305+
return std::move(E);
306+
// E == success (originally a no_data_found error).
307+
continue;
308+
}
294309
for (auto &Reader : CoverageReadersOrErr.get())
295310
Readers.push_back(std::move(Reader));
296311
Buffers.push_back(std::move(CovMappingBufOrErr.get()));
297312
}
313+
// If no readers were created, either no objects were provided or none of them
314+
// had coverage data. Return an error in the latter case.
315+
if (Readers.empty() && !ObjectFilenames.empty())
316+
return make_error<CoverageMapError>(coveragemap_error::no_data_found);
298317
return load(Readers, *ProfileReader);
299318
}
300319

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void f1() {}
2+
3+
int main(int argc, char** argv) {
4+
f1();
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int main(int argc, char** argv) {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RUN: llvm-cov export --format=lcov --instr-profile=%S/Inputs/multiple_objects_not_all_instrumented/instrumented.profdata \
2+
RUN: -object %S/Inputs/multiple_objects_not_all_instrumented/not_instrumented \
3+
RUN: -object %S/Inputs/multiple_objects_not_all_instrumented/instrumented | FileCheck -check-prefix=FN %s
4+
5+
FN:1,_Z2f1v
6+
7+
Instructions for regenerating the test:
8+
9+
clang -std=c++11 not_instrumented.cc -o not_instrumented
10+
clang -std=c++11 -mllvm -enable-name-compression=false -fprofile-instr-generate -fcoverage-mapping instrumented.cc -o instrumented
11+
LLVM_PROFILE_FILE="instrumented.raw" ./instrumented
12+
llvm-profdata merge instrumented.raw -o instrumented.profdata

0 commit comments

Comments
 (0)