Skip to content

Commit c7d7b1e

Browse files
committed
Add path remapping with -coverage-prefix-map to coverage data
Previously the path to covered files in the __LLVM_COV / __llvm_covmap section were absolute. This made remote builds with coverage information difficult because all machines would have to have the same build root. This change uses the values for `-coverage-prefix-map` to remap files in the coverage info to relative paths. These paths work correctly with llvm-cov when it is run from the same source directory as the compilation, or from a different directory using the `-path-equivalence` argument. This is analogous to this change in clang https://reviews.llvm.org/D81122
1 parent 30491e6 commit c7d7b1e

File tree

9 files changed

+49
-1
lines changed

9 files changed

+49
-1
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
305305
ERROR(error_invalid_debug_prefix_map, none,
306306
"invalid argument '%0' to -debug-prefix-map; it must be of the form "
307307
"'original=remapped'", (StringRef))
308+
ERROR(error_invalid_coverage_prefix_map, none,
309+
"invalid argument '%0' to -coverage-prefix-map; it must be of the form "
310+
"'original=remapped'", (StringRef))
308311

309312

310313
ERROR(error_unable_to_write_swift_ranges_file, none,

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class IRGenOptions {
169169
/// Path prefixes that should be rewritten in debug info.
170170
PathRemapper DebugPrefixMap;
171171

172+
/// Path prefixes that should be rewritten in coverage info.
173+
PathRemapper CoveragePrefixMap;
174+
172175
/// What level of debug info to generate.
173176
IRGenDebugInfoLevel DebugInfoLevel : 2;
174177

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,9 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
698698
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
699699
Flags<[FrontendOption]>,
700700
HelpText<"Remap source paths in debug info">;
701+
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
702+
Flags<[FrontendOption]>,
703+
HelpText<"Remap source paths in coverage info">;
701704

702705
def debug_info_format : Joined<["-"], "debug-info-format=">,
703706
Flags<[FrontendOption]>,

lib/Driver/Driver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ static void validateDebugInfoArgs(DiagnosticEngine &diags,
179179
for (auto A : args.getAllArgValues(options::OPT_debug_prefix_map))
180180
if (A.find('=') == StringRef::npos)
181181
diags.diagnose(SourceLoc(), diag::error_invalid_debug_prefix_map, A);
182+
183+
// Check for any -coverage-prefix-map options that aren't of the form
184+
// 'original=remapped' (either side can be empty, however).
185+
for (auto A : args.getAllArgValues(options::OPT_coverage_prefix_map))
186+
if (A.find('=') == StringRef::npos)
187+
diags.diagnose(SourceLoc(), diag::error_invalid_coverage_prefix_map, A);
182188
}
183189

184190
static void validateVerifyIncrementalDependencyArgs(DiagnosticEngine &diags,

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
260260

261261
// Pass on file paths that should be remapped in debug info.
262262
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map);
263+
inputArgs.AddAllArgs(arguments, options::OPT_coverage_prefix_map);
263264

264265
// Pass through the values passed to -Xfrontend.
265266
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
12491249
Opts.DebugPrefixMap.addMapping(SplitMap.first, SplitMap.second);
12501250
}
12511251

1252+
for (auto A : Args.getAllArgValues(options::OPT_coverage_prefix_map)) {
1253+
auto SplitMap = StringRef(A).split('=');
1254+
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
1255+
}
1256+
12521257
for (const Arg *A : Args.filtered(OPT_Xcc)) {
12531258
StringRef Opt = A->getValue();
12541259
if (Opt.startswith("-D") || Opt.startswith("-U"))

lib/IRGen/GenCoverage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "IRGenModule.h"
1919
#include "SwiftTargetInfo.h"
2020

21+
#include "swift/AST/IRGenOptions.h"
2122
#include "swift/SIL/SILModule.h"
2223
#include "llvm/IR/Constants.h"
2324
#include "llvm/IR/Module.h"
@@ -60,6 +61,7 @@ void IRGenModule::emitCoverageMapping() {
6061
if (std::find(Files.begin(), Files.end(), M->getFile()) == Files.end())
6162
Files.push_back(M->getFile());
6263

64+
auto remapper = getOptions().CoveragePrefixMap;
6365
// Awkwardly munge absolute filenames into a vector of StringRefs.
6466
// TODO: This is heinous - the same thing is happening in clang, but the API
6567
// really needs to be cleaned up for both.
@@ -68,7 +70,7 @@ void IRGenModule::emitCoverageMapping() {
6870
for (StringRef Name : Files) {
6971
llvm::SmallString<256> Path(Name);
7072
llvm::sys::fs::make_absolute(Path);
71-
FilenameStrs.push_back(std::string(Path.begin(), Path.end()));
73+
FilenameStrs.push_back(remapper.remapPath(Path));
7274
FilenameRefs.push_back(FilenameStrs.back());
7375
}
7476

test/Driver/coverage-prefix-map.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not %target-swiftc_driver -coverage-prefix-map old %s 2>&1 | %FileCheck %s -check-prefix CHECK-INVALID
2+
// RUN: %target-swiftc_driver -### -coverage-prefix-map old=new %s 2>&1 | %FileCheck %s -check-prefix CHECK-SIMPLE
3+
// RUN: %target-swiftc_driver -### -coverage-prefix-map old=n=ew %s 2>&1 | %FileCheck %s -check-prefix CHECK-COMPLEX
4+
// RUN: %target-swiftc_driver -### -coverage-prefix-map old= %s 2>&1 | %FileCheck %s -check-prefix CHECK-EMPTY
5+
6+
// CHECK-INVALID: error: invalid argument 'old' to -coverage-prefix-map
7+
// CHECK-SIMPLE: coverage-prefix-map old=new
8+
// CHECK-COMPLEX: coverage-prefix-map old=n=ew
9+
// CHECK-EMPTY: coverage-prefix-map old=
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// %s expands to an absolute path, so to test relative paths we need to create a
2+
// clean directory, put the source there, and cd into it.
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t/foo/bar/baz
5+
// RUN: echo "func coverage() {}" > %t/foo/bar/baz/coverage_relative_path.swift
6+
// RUN: cd %t/foo/bar
7+
8+
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -Xllvm -enable-name-compression=false -emit-ir %/t/foo/bar/baz/coverage_relative_path.swift | %FileCheck -check-prefix=ABSOLUTE %s
9+
//
10+
// ABSOLUTE: @__llvm_coverage_mapping = {{.*"\\01.*foo.*bar.*baz.*coverage_relative_path\.swift}}
11+
12+
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -Xllvm -enable-name-compression=false -coverage-prefix-map %/t/foo/bar=. -emit-ir %/t/foo/bar/baz/coverage_relative_path.swift | %FileCheck -check-prefix=RELATIVE %s
13+
//
14+
// RELATIVE: @__llvm_coverage_mapping = {{.*"\\01[^/]*}}.{{/|\\}}baz{{.*coverage_relative_path\.swift}}
15+
16+
func coverage() {}

0 commit comments

Comments
 (0)