[PATCH] D107393: Apply -fmacro-prefix-map to __builtin_FILE()
Svenny created this revision. Svenny added reviewers: dankm, MaskRay, rnk, Lekensteyn, echristo. Svenny added a project: clang. Herald added a subscriber: dexonsmith. Svenny requested review of this revision. Herald added a subscriber: cfe-commits. This will match the behavior of GCC. Patch does not change remapping logic itself, so adding one simple smoke test should be enough. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D107393 Files: clang/include/clang/Basic/LangOptions.h clang/include/clang/Lex/PreprocessorOptions.h clang/lib/AST/Expr.cpp clang/lib/Basic/LangOptions.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/Frontend/CompilerInvocation.cpp clang/lib/Lex/PPMacroExpansion.cpp clang/test/CodeGen/macro-prefix-map.c Index: clang/test/CodeGen/macro-prefix-map.c === --- /dev/null +++ clang/test/CodeGen/macro-prefix-map.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s + +void test() { + // CHECK: /UNLIKELY/PATH{{/|}}macro-prefix-map.c + const char *file = __builtin_FILE(); +} Index: clang/lib/Lex/PPMacroExpansion.cpp === --- clang/lib/Lex/PPMacroExpansion.cpp +++ clang/lib/Lex/PPMacroExpansion.cpp @@ -1460,15 +1460,6 @@ return TI.getTriple().getEnvironment() == Env.getEnvironment(); } -static void remapMacroPath( -SmallString<256> &Path, -const std::map> -&MacroPrefixMap) { - for (const auto &Entry : MacroPrefixMap) -if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) - break; -} - /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded /// as a builtin macro, handle it and return the next token as 'Tok'. void Preprocessor::ExpandBuiltinMacro(Token &Tok) { @@ -1550,7 +1541,7 @@ } else { FN += PLoc.getFilename(); } - remapMacroPath(FN, PPOpts->MacroPrefixMap); + getLangOpts().remapPathPrefix(FN); Lexer::Stringify(FN); OS << '"' << FN << '"'; } Index: clang/lib/Frontend/CompilerInvocation.cpp === --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3528,6 +3528,9 @@ GenerateArg(Args, OPT_fexperimental_relative_cxx_abi_vtables, SA); else GenerateArg(Args, OPT_fno_experimental_relative_cxx_abi_vtables, SA); + + for (const auto &MP : Opts.MacroPrefixMap) +GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); } bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, @@ -4038,6 +4041,12 @@ options::OPT_fno_experimental_relative_cxx_abi_vtables, TargetCXXABI::usesRelativeVTables(T)); + for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) { +auto Split = StringRef(A).split('='); +Opts.MacroPrefixMap.insert( +{std::string(Split.first), std::string(Split.second)}); + } + return Diags.getNumErrors() == NumErrorsBefore; } @@ -4110,9 +4119,6 @@ for (const auto &D : Opts.DeserializedPCHDeclsToErrorOn) GenerateArg(Args, OPT_error_on_deserialized_pch_decl, D, SA); - for (const auto &MP : Opts.MacroPrefixMap) -GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); - if (Opts.PrecompiledPreambleBytes != std::make_pair(0u, false)) GenerateArg(Args, OPT_preamble_bytes_EQ, Twine(Opts.PrecompiledPreambleBytes.first) + "," + @@ -4181,12 +4187,6 @@ for (const auto *A : Args.filtered(OPT_error_on_deserialized_pch_decl)) Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue()); - for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) { -auto Split = StringRef(A).split('='); -Opts.MacroPrefixMap.insert( -{std::string(Split.first), std::string(Split.second)}); - } - if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) { StringRef Value(A->getValue()); size_t Comma = Value.find(','); Index: clang/lib/CodeGen/CodeGenModule.cpp === --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -186,7 +186,7 @@ !getModule().getSourceFileName().empty()) { std::string Path = getModule().getSourceFileName(); // Check if a path substitution is needed from the MacroPrefixMap. -for (const auto &Entry : PPO.MacroPrefixMap) +for (const auto &Entry : LangOpts.MacroPrefixMap) if (Path.rfind(Entry.first, 0) != std::string::npos) { Path = Entry.second + Path.substr(Entry.first.size()); break; Index: clang/lib/Basic/LangOptions.cpp === --- clang/lib/Basic/LangOptions.cpp +++ clang/lib/Basic/LangOptions.cpp @@ -11,6 +11,8 @@
[PATCH] D107393: Apply -fmacro-prefix-map to __builtin_FILE()
Svenny updated this revision to Diff 364195. Svenny added a comment. Moved new test to CodeGenCXX/builtin-source-location.cpp. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D107393/new/ https://reviews.llvm.org/D107393 Files: clang/include/clang/Basic/LangOptions.h clang/include/clang/Lex/PreprocessorOptions.h clang/lib/AST/Expr.cpp clang/lib/Basic/LangOptions.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/Frontend/CompilerInvocation.cpp clang/lib/Lex/PPMacroExpansion.cpp clang/test/CodeGenCXX/builtin-source-location.cpp Index: clang/test/CodeGenCXX/builtin-source-location.cpp === --- clang/test/CodeGenCXX/builtin-source-location.cpp +++ clang/test/CodeGenCXX/builtin-source-location.cpp @@ -1,5 +1,13 @@ // RUN: %clang_cc1 -std=c++2a -fblocks %s -triple x86_64-unknown-unknown -emit-llvm -o %t.ll +// This needs to be performed before #line directives which alter filename +// RUN: %clang_cc1 -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-PREFIX-MAP +// +// CHECK-PREFIX-MAP: /UNLIKELY/PATH{{/|}}builtin-source-location.cpp +void testRemap() { + const char *file = __builtin_FILE(); +} + #line 8 "builtin-source-location.cpp" struct source_location { Index: clang/lib/Lex/PPMacroExpansion.cpp === --- clang/lib/Lex/PPMacroExpansion.cpp +++ clang/lib/Lex/PPMacroExpansion.cpp @@ -1460,15 +1460,6 @@ return TI.getTriple().getEnvironment() == Env.getEnvironment(); } -static void remapMacroPath( -SmallString<256> &Path, -const std::map> -&MacroPrefixMap) { - for (const auto &Entry : MacroPrefixMap) -if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) - break; -} - /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded /// as a builtin macro, handle it and return the next token as 'Tok'. void Preprocessor::ExpandBuiltinMacro(Token &Tok) { @@ -1550,7 +1541,7 @@ } else { FN += PLoc.getFilename(); } - remapMacroPath(FN, PPOpts->MacroPrefixMap); + getLangOpts().remapPathPrefix(FN); Lexer::Stringify(FN); OS << '"' << FN << '"'; } Index: clang/lib/Frontend/CompilerInvocation.cpp === --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3528,6 +3528,9 @@ GenerateArg(Args, OPT_fexperimental_relative_cxx_abi_vtables, SA); else GenerateArg(Args, OPT_fno_experimental_relative_cxx_abi_vtables, SA); + + for (const auto &MP : Opts.MacroPrefixMap) +GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); } bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, @@ -4038,6 +4041,12 @@ options::OPT_fno_experimental_relative_cxx_abi_vtables, TargetCXXABI::usesRelativeVTables(T)); + for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) { +auto Split = StringRef(A).split('='); +Opts.MacroPrefixMap.insert( +{std::string(Split.first), std::string(Split.second)}); + } + return Diags.getNumErrors() == NumErrorsBefore; } @@ -4110,9 +4119,6 @@ for (const auto &D : Opts.DeserializedPCHDeclsToErrorOn) GenerateArg(Args, OPT_error_on_deserialized_pch_decl, D, SA); - for (const auto &MP : Opts.MacroPrefixMap) -GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); - if (Opts.PrecompiledPreambleBytes != std::make_pair(0u, false)) GenerateArg(Args, OPT_preamble_bytes_EQ, Twine(Opts.PrecompiledPreambleBytes.first) + "," + @@ -4181,12 +4187,6 @@ for (const auto *A : Args.filtered(OPT_error_on_deserialized_pch_decl)) Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue()); - for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) { -auto Split = StringRef(A).split('='); -Opts.MacroPrefixMap.insert( -{std::string(Split.first), std::string(Split.second)}); - } - if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) { StringRef Value(A->getValue()); size_t Comma = Value.find(','); Index: clang/lib/CodeGen/CodeGenModule.cpp === --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -186,7 +186,7 @@ !getModule().getSourceFileName().empty()) { std::string Path = getModule().getSourceFileName(); // Check if a path substitution is needed from the MacroPrefixMap. -for (const auto &Entry : PPO.MacroPrefixMap) +for (const auto &Entry : LangOpts.MacroPrefixMap) if (Path.rfind(Entry.first, 0) != std::string::npos) { Path = Entry.second + Path.substr(Entry.first.size()); break; Index: clang/lib/Basic/LangOptions.cpp ===
[PATCH] D107393: Apply -fmacro-prefix-map to __builtin_FILE()
Svenny marked an inline comment as done. Svenny added a comment. Could you please land it? I don't have commit access. Also, any chance this patch can get into 13.x release branch? Not sure if it counts as a feature or a bugfix :) CHANGES SINCE LAST ACTION https://reviews.llvm.org/D107393/new/ https://reviews.llvm.org/D107393 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D107393: Apply -fmacro-prefix-map to __builtin_FILE()
Svenny added a comment. Great! My commit author string is `Pavel Asyutchenko `. I have an even uglier hack to work around this: to offset the value of `__builtin_FILE()` by compile-time calculated location of some known substring like `src` or `include`. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D107393/new/ https://reviews.llvm.org/D107393 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D107393: Apply -fmacro-prefix-map to __builtin_FILE()
Svenny updated this revision to Diff 364217. Svenny added a comment. Herald added a subscriber: dang. Updated ffile-prefix-map and fmacro-prefix-map in clang/include/clang/Driver/Options.td. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D107393/new/ https://reviews.llvm.org/D107393 Files: clang/include/clang/Basic/LangOptions.h clang/include/clang/Driver/Options.td clang/include/clang/Lex/PreprocessorOptions.h clang/lib/AST/Expr.cpp clang/lib/Basic/LangOptions.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/Frontend/CompilerInvocation.cpp clang/lib/Lex/PPMacroExpansion.cpp clang/test/CodeGenCXX/builtin-source-location.cpp Index: clang/test/CodeGenCXX/builtin-source-location.cpp === --- clang/test/CodeGenCXX/builtin-source-location.cpp +++ clang/test/CodeGenCXX/builtin-source-location.cpp @@ -1,5 +1,13 @@ // RUN: %clang_cc1 -std=c++2a -fblocks %s -triple x86_64-unknown-unknown -emit-llvm -o %t.ll +// This needs to be performed before #line directives which alter filename +// RUN: %clang_cc1 -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-PREFIX-MAP +// +// CHECK-PREFIX-MAP: /UNLIKELY/PATH{{/|}}builtin-source-location.cpp +void testRemap() { + const char *file = __builtin_FILE(); +} + #line 8 "builtin-source-location.cpp" struct source_location { Index: clang/lib/Lex/PPMacroExpansion.cpp === --- clang/lib/Lex/PPMacroExpansion.cpp +++ clang/lib/Lex/PPMacroExpansion.cpp @@ -1460,15 +1460,6 @@ return TI.getTriple().getEnvironment() == Env.getEnvironment(); } -static void remapMacroPath( -SmallString<256> &Path, -const std::map> -&MacroPrefixMap) { - for (const auto &Entry : MacroPrefixMap) -if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) - break; -} - /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded /// as a builtin macro, handle it and return the next token as 'Tok'. void Preprocessor::ExpandBuiltinMacro(Token &Tok) { @@ -1550,7 +1541,7 @@ } else { FN += PLoc.getFilename(); } - remapMacroPath(FN, PPOpts->MacroPrefixMap); + getLangOpts().remapPathPrefix(FN); Lexer::Stringify(FN); OS << '"' << FN << '"'; } Index: clang/lib/Frontend/CompilerInvocation.cpp === --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3528,6 +3528,9 @@ GenerateArg(Args, OPT_fexperimental_relative_cxx_abi_vtables, SA); else GenerateArg(Args, OPT_fno_experimental_relative_cxx_abi_vtables, SA); + + for (const auto &MP : Opts.MacroPrefixMap) +GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); } bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, @@ -4038,6 +4041,12 @@ options::OPT_fno_experimental_relative_cxx_abi_vtables, TargetCXXABI::usesRelativeVTables(T)); + for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) { +auto Split = StringRef(A).split('='); +Opts.MacroPrefixMap.insert( +{std::string(Split.first), std::string(Split.second)}); + } + return Diags.getNumErrors() == NumErrorsBefore; } @@ -4110,9 +4119,6 @@ for (const auto &D : Opts.DeserializedPCHDeclsToErrorOn) GenerateArg(Args, OPT_error_on_deserialized_pch_decl, D, SA); - for (const auto &MP : Opts.MacroPrefixMap) -GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); - if (Opts.PrecompiledPreambleBytes != std::make_pair(0u, false)) GenerateArg(Args, OPT_preamble_bytes_EQ, Twine(Opts.PrecompiledPreambleBytes.first) + "," + @@ -4181,12 +4187,6 @@ for (const auto *A : Args.filtered(OPT_error_on_deserialized_pch_decl)) Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue()); - for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) { -auto Split = StringRef(A).split('='); -Opts.MacroPrefixMap.insert( -{std::string(Split.first), std::string(Split.second)}); - } - if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) { StringRef Value(A->getValue()); size_t Comma = Value.find(','); Index: clang/lib/CodeGen/CodeGenModule.cpp === --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -186,7 +186,7 @@ !getModule().getSourceFileName().empty()) { std::string Path = getModule().getSourceFileName(); // Check if a path substitution is needed from the MacroPrefixMap. -for (const auto &Entry : PPO.MacroPrefixMap) +for (const auto &Entry : LangOpts.MacroPrefixMap) if (Path.rfind(Entry.first, 0) != std::string::npos) { Path = Entry.sec