diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -207,7 +207,9 @@ std::string RecordCommandLine; std::map DebugPrefixMap; - std::map CoveragePrefixMap; + + /// Prefix replacement map for coverage. + llvm::SmallVector, 0> CoveragePrefixMap; /// The ABI to use for passing floating point arguments. std::string FloatABI; diff --git a/clang/lib/CodeGen/CoverageMappingGen.h b/clang/lib/CodeGen/CoverageMappingGen.h --- a/clang/lib/CodeGen/CoverageMappingGen.h +++ b/clang/lib/CodeGen/CoverageMappingGen.h @@ -107,7 +107,10 @@ llvm::SmallDenseMap FileEntries; std::vector FunctionNames; std::vector FunctionRecords; - std::map CoveragePrefixMap; + + /// Prefix replacement map for coverage. + llvm::SmallVector, 0> + CoveragePrefixMap; std::string getCurrentDirname(); std::string normalizeFilename(StringRef Filename); diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -1635,7 +1635,8 @@ CoverageMappingModuleGen::CoverageMappingModuleGen( CodeGenModule &CGM, CoverageSourceInfo &SourceInfo) : CGM(CGM), SourceInfo(SourceInfo) { - CoveragePrefixMap = CGM.getCodeGenOpts().CoveragePrefixMap; + for (const auto &[From, To] : CGM.getCodeGenOpts().CoveragePrefixMap) + CoveragePrefixMap.emplace_back(From, To); } std::string CoverageMappingModuleGen::getCurrentDirname() { @@ -1650,10 +1651,14 @@ std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) { llvm::SmallString<256> Path(Filename); llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); - for (const auto &Entry : CoveragePrefixMap) { - if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) + + // Traverse CoveragePrefixMap in reverse order to apply the last provided path + // prefix replacement when multiple -fcoverage-prefix-map options are + // provided. + for (const auto &[From, To] : llvm::reverse(CoveragePrefixMap)) + if (llvm::sys::path::replace_path_prefix(Path, From, To)) break; - } + return Path.str().str(); } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1702,8 +1702,7 @@ for (const auto &Arg : Args.getAllArgValues(OPT_fcoverage_prefix_map_EQ)) { auto Split = StringRef(Arg).split('='); - Opts.CoveragePrefixMap.insert( - {std::string(Split.first), std::string(Split.second)}); + Opts.CoveragePrefixMap.emplace_back(Split.first, Split.second); } const llvm::Triple::ArchType DebugEntryValueArchs[] = { diff --git a/clang/test/Profile/coverage-prefix-map.c b/clang/test/Profile/coverage-prefix-map.c --- a/clang/test/Profile/coverage-prefix-map.c +++ b/clang/test/Profile/coverage-prefix-map.c @@ -19,3 +19,13 @@ // 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 // COVERAGE-COMPILATION-DIR: @__llvm_coverage_mapping = {{.*"\\02.*}}nonsense + +// Test that last -fcoverage-prefix-map option (-fcoverage-prefix-map==newpath) is applied. +// 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 +// COVERAGE-PREFIX-MAP-ORDER: @__llvm_coverage_mapping = {{.*"\\02.*newpath.*root.*nested.*coverage-prefix-map\.c}} + +// Test that last -fcoverage-prefix-map option (-fcoverage-prefix-map=%/t/root=.) is applied. +// 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 +// COVERAGE-PREFIX-MAP-REORDER: @__llvm_coverage_mapping = +// COVERAGE-PREFIX-MAP-REORDER-NOT: newpath +// COVERAGE-PREFIX-MAP-REORDER-SAME: nested{{.*coverage-prefix-map\.c}}