Skip to content

Add path remapping with -coverage-prefix-map to coverage data #32175

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
Jun 16, 2020
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
ERROR(error_invalid_debug_prefix_map, none,
"invalid argument '%0' to -debug-prefix-map; it must be of the form "
"'original=remapped'", (StringRef))
ERROR(error_invalid_coverage_prefix_map, none,
"invalid argument '%0' to -coverage-prefix-map; it must be of the form "
"'original=remapped'", (StringRef))


ERROR(error_unable_to_write_swift_ranges_file, none,
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class IRGenOptions {
/// Path prefixes that should be rewritten in debug info.
PathRemapper DebugPrefixMap;

/// Path prefixes that should be rewritten in coverage info.
PathRemapper CoveragePrefixMap;

/// What level of debug info to generate.
IRGenDebugInfoLevel DebugInfoLevel : 2;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,9 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
Flags<[FrontendOption]>,
HelpText<"Remap source paths in debug info">;
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
Flags<[FrontendOption]>,
HelpText<"Remap source paths in coverage info">;

def debug_info_format : Joined<["-"], "debug-info-format=">,
Flags<[FrontendOption]>,
Expand Down
6 changes: 6 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ static void validateDebugInfoArgs(DiagnosticEngine &diags,
for (auto A : args.getAllArgValues(options::OPT_debug_prefix_map))
if (A.find('=') == StringRef::npos)
diags.diagnose(SourceLoc(), diag::error_invalid_debug_prefix_map, A);

// Check for any -coverage-prefix-map options that aren't of the form
// 'original=remapped' (either side can be empty, however).
for (auto A : args.getAllArgValues(options::OPT_coverage_prefix_map))
if (A.find('=') == StringRef::npos)
diags.diagnose(SourceLoc(), diag::error_invalid_coverage_prefix_map, A);
}

static void validateVerifyIncrementalDependencyArgs(DiagnosticEngine &diags,
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,

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

// Pass through the values passed to -Xfrontend.
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
Opts.DebugPrefixMap.addMapping(SplitMap.first, SplitMap.second);
}

for (auto A : Args.getAllArgValues(options::OPT_coverage_prefix_map)) {
auto SplitMap = StringRef(A).split('=');
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
}

for (const Arg *A : Args.filtered(OPT_Xcc)) {
StringRef Opt = A->getValue();
if (Opt.startswith("-D") || Opt.startswith("-U"))
Expand Down
4 changes: 3 additions & 1 deletion lib/IRGen/GenCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "IRGenModule.h"
#include "SwiftTargetInfo.h"

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

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

Expand Down
9 changes: 9 additions & 0 deletions test/Driver/coverage-prefix-map.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: not %target-swiftc_driver -coverage-prefix-map old %s 2>&1 | %FileCheck %s -check-prefix CHECK-INVALID
// RUN: %target-swiftc_driver -### -coverage-prefix-map old=new %s 2>&1 | %FileCheck %s -check-prefix CHECK-SIMPLE
// RUN: %target-swiftc_driver -### -coverage-prefix-map old=n=ew %s 2>&1 | %FileCheck %s -check-prefix CHECK-COMPLEX
// RUN: %target-swiftc_driver -### -coverage-prefix-map old= %s 2>&1 | %FileCheck %s -check-prefix CHECK-EMPTY

// CHECK-INVALID: error: invalid argument 'old' to -coverage-prefix-map
// CHECK-SIMPLE: coverage-prefix-map old=new
// CHECK-COMPLEX: coverage-prefix-map old=n=ew
// CHECK-EMPTY: coverage-prefix-map old=
16 changes: 16 additions & 0 deletions test/Profiler/coverage_relative_path.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// %s expands to an absolute path, so to test relative paths we need to create a
// clean directory, put the source there, and cd into it.
// RUN: rm -rf %t
// RUN: mkdir -p %t/foo/bar/baz
// RUN: cp %s %t/foo/bar/baz/coverage_relative_path.swift
// RUN: cd %t/foo/bar

// 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
//
// ABSOLUTE: @__llvm_coverage_mapping = {{.*"\\01.*foo.*bar.*baz.*coverage_relative_path\.swift}}

// 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
//
// RELATIVE: @__llvm_coverage_mapping = {{.*"\\01[^/]*}}.{{/|\\}}baz{{.*coverage_relative_path\.swift}}

func coverage() {}