Skip to content

Commit d7fa921

Browse files
committed
[clang] Apply -fcoverage-prefix-map reverse order
This patch changes handling multiple -fcoverage-prefix-map options to match GCC's behavior. GCC applies prefix remappings that are provided in reverse order (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109591). Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D148757
1 parent 5854b39 commit d7fa921

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ class CodeGenOptions : public CodeGenOptionsBase {
207207
std::string RecordCommandLine;
208208

209209
llvm::SmallVector<std::pair<std::string, std::string>, 0> DebugPrefixMap;
210-
std::map<std::string, std::string> CoveragePrefixMap;
210+
211+
/// Prefix replacement map for source-based code coverage to remap source
212+
/// file paths in coverage mapping.
213+
llvm::SmallVector<std::pair<std::string, std::string>, 0> CoveragePrefixMap;
211214

212215
/// The ABI to use for passing floating point arguments.
213216
std::string FloatABI;

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3223,7 +3223,8 @@ def fdebug_prefix_map_EQ
32233223
def fcoverage_prefix_map_EQ
32243224
: Joined<["-"], "fcoverage-prefix-map=">, Group<f_Group>,
32253225
Flags<[CC1Option]>,
3226-
HelpText<"remap file source paths in coverage mapping">;
3226+
MetaVarName<"<old>=<new>">,
3227+
HelpText<"remap file source paths <old> to <new> in coverage mapping. If there are multiple options, prefix replacement is applied in reverse order starting from the last one">;
32273228
def ffile_prefix_map_EQ
32283229
: Joined<["-"], "ffile-prefix-map=">, Group<f_Group>,
32293230
HelpText<"remap file source paths in debug info, predefined preprocessor "

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,8 +1648,13 @@ std::string CoverageMappingModuleGen::getCurrentDirname() {
16481648
std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) {
16491649
llvm::SmallString<256> Path(Filename);
16501650
llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1651-
for (const auto &Entry : CGM.getCodeGenOpts().CoveragePrefixMap) {
1652-
if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
1651+
1652+
/// Traverse coverage prefix map in reverse order because prefix replacements
1653+
/// are applied in reverse order starting from the last one when multiple
1654+
/// prefix replacement options are provided.
1655+
for (const auto &[From, To] :
1656+
llvm::reverse(CGM.getCodeGenOpts().CoveragePrefixMap)) {
1657+
if (llvm::sys::path::replace_path_prefix(Path, From, To))
16531658
break;
16541659
}
16551660
return Path.str().str();

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
17011701

17021702
for (const auto &Arg : Args.getAllArgValues(OPT_fcoverage_prefix_map_EQ)) {
17031703
auto Split = StringRef(Arg).split('=');
1704-
Opts.CoveragePrefixMap.insert(
1705-
{std::string(Split.first), std::string(Split.second)});
1704+
Opts.CoveragePrefixMap.emplace_back(Split.first, Split.second);
17061705
}
17071706

17081707
const llvm::Triple::ArchType DebugEntryValueArchs[] = {

clang/test/Profile/coverage-prefix-map.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@
1919

2020
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -mllvm -enable-name-compression=false -main-file-name coverage-prefix-map.c %t/root/nested/coverage-prefix-map.c -fcoverage-compilation-dir=/custom -fcoverage-prefix-map=/custom=/nonsense -o - | FileCheck --check-prefix=COVERAGE-COMPILATION-DIR %s
2121
// COVERAGE-COMPILATION-DIR: @__llvm_coverage_mapping = {{.*"\\02.*}}nonsense
22+
23+
// Test that last -fcoverage-prefix-map option (-fcoverage-prefix-map==newpath) is applied.
24+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -mllvm -enable-name-compression=false -main-file-name coverage-prefix-map.c %t/root/nested/coverage-prefix-map.c -fcoverage-prefix-map=%/t/root=. -fcoverage-prefix-map==newpath -o - | FileCheck --check-prefix=COVERAGE-PREFIX-MAP-ORDER %s
25+
// COVERAGE-PREFIX-MAP-ORDER: @__llvm_coverage_mapping = {{.*"\\02.*newpath.*root.*nested.*coverage-prefix-map\.c}}
26+
27+
// Test that last -fcoverage-prefix-map option (-fcoverage-prefix-map=%/t/root=.) is applied.
28+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -mllvm -enable-name-compression=false -main-file-name coverage-prefix-map.c %t/root/nested/coverage-prefix-map.c -fcoverage-prefix-map==newpath -fcoverage-prefix-map=%/t/root=. -o - | FileCheck --check-prefix=COVERAGE-PREFIX-MAP-REORDER %s
29+
// COVERAGE-PREFIX-MAP-REORDER: @__llvm_coverage_mapping =
30+
// COVERAGE-PREFIX-MAP-REORDER-NOT: newpath
31+
// COVERAGE-PREFIX-MAP-REORDER-SAME: nested{{.*coverage-prefix-map\.c}}

0 commit comments

Comments
 (0)