Skip to content

Commit 1b77448

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 403b2bf commit 1b77448

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
@@ -299,6 +299,9 @@ ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
299299
ERROR(error_invalid_debug_prefix_map, none,
300300
"invalid argument '%0' to -debug-prefix-map; it must be of the form "
301301
"'original=remapped'", (StringRef))
302+
ERROR(error_invalid_coverage_prefix_map, none,
303+
"invalid argument '%0' to -coverage-prefix-map; it must be of the form "
304+
"'original=remapped'", (StringRef))
302305

303306

304307
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
@@ -724,6 +724,9 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
724724
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
725725
Flags<[FrontendOption]>,
726726
HelpText<"Remap source paths in debug info">;
727+
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
728+
Flags<[FrontendOption]>,
729+
HelpText<"Remap source paths in coverage info">;
727730

728731
def debug_info_format : Joined<["-"], "debug-info-format=">,
729732
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
@@ -262,6 +262,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
262262

263263
// Pass on file paths that should be remapped in debug info.
264264
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map);
265+
inputArgs.AddAllArgs(arguments, options::OPT_coverage_prefix_map);
265266

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

lib/Frontend/CompilerInvocation.cpp

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

1310+
for (auto A : Args.getAllArgValues(options::OPT_coverage_prefix_map)) {
1311+
auto SplitMap = StringRef(A).split('=');
1312+
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
1313+
}
1314+
13101315
for (const Arg *A : Args.filtered(OPT_Xcc)) {
13111316
StringRef Opt = A->getValue();
13121317
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: cp %s %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 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 $PWD=. -emit-ir 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)