Author: Gulfem Savrun Yeniceri Date: 2023-04-27T00:24:18Z New Revision: d7fa92126cc398b993f301273816739b23ed8740
URL: https://github.com/llvm/llvm-project/commit/d7fa92126cc398b993f301273816739b23ed8740 DIFF: https://github.com/llvm/llvm-project/commit/d7fa92126cc398b993f301273816739b23ed8740.diff LOG: [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 Added: Modified: clang/include/clang/Basic/CodeGenOptions.h clang/include/clang/Driver/Options.td clang/lib/CodeGen/CoverageMappingGen.cpp clang/lib/Frontend/CompilerInvocation.cpp clang/test/Profile/coverage-prefix-map.c Removed: ################################################################################ diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 662271fc025d4..b0f22411e1ad2 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -207,7 +207,10 @@ class CodeGenOptions : public CodeGenOptionsBase { std::string RecordCommandLine; llvm::SmallVector<std::pair<std::string, std::string>, 0> DebugPrefixMap; - std::map<std::string, std::string> CoveragePrefixMap; + + /// Prefix replacement map for source-based code coverage to remap source + /// file paths in coverage mapping. + llvm::SmallVector<std::pair<std::string, std::string>, 0> CoveragePrefixMap; /// The ABI to use for passing floating point arguments. std::string FloatABI; diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index c8f505a92a7c8..592584ab625c9 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3223,7 +3223,8 @@ def fdebug_prefix_map_EQ def fcoverage_prefix_map_EQ : Joined<["-"], "fcoverage-prefix-map=">, Group<f_Group>, Flags<[CC1Option]>, - HelpText<"remap file source paths in coverage mapping">; + MetaVarName<"<old>=<new>">, + 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">; def ffile_prefix_map_EQ : Joined<["-"], "ffile-prefix-map=">, Group<f_Group>, HelpText<"remap file source paths in debug info, predefined preprocessor " diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp index 273d991c16c7b..bb4c6f5e0cdee 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -1648,8 +1648,13 @@ std::string CoverageMappingModuleGen::getCurrentDirname() { 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 : CGM.getCodeGenOpts().CoveragePrefixMap) { - if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) + + /// Traverse coverage prefix map in reverse order because prefix replacements + /// are applied in reverse order starting from the last one when multiple + /// prefix replacement options are provided. + for (const auto &[From, To] : + llvm::reverse(CGM.getCodeGenOpts().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 index a1481365daf98..cbd2663a56278 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1701,8 +1701,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, 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 index 6f2c0fba0b09f..de9e3773f66d2 100644 --- 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}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits