Skip to content

Commit 7df405e

Browse files
SvennyMaskRay
authored andcommitted
Apply -fmacro-prefix-map to __builtin_FILE()
This matches the behavior of GCC. Patch does not change remapping logic itself, so adding one simple smoke test should be enough. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D107393
1 parent f135a91 commit 7df405e

File tree

9 files changed

+41
-28
lines changed

9 files changed

+41
-28
lines changed

clang/include/clang/Basic/LangOptions.h

+6
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ class LangOptions : public LangOptionsBase {
367367
/// A list of all -fno-builtin-* function names (e.g., memset).
368368
std::vector<std::string> NoBuiltinFuncs;
369369

370+
/// A prefix map for __FILE__, __BASE_FILE__ and __builtin_FILE().
371+
std::map<std::string, std::string, std::greater<std::string>> MacroPrefixMap;
372+
370373
/// Triples of the OpenMP targets that the host code codegen should
371374
/// take into account in order to generate accurate offloading descriptors.
372375
std::vector<llvm::Triple> OMPTargetTriples;
@@ -473,6 +476,9 @@ class LangOptions : public LangOptionsBase {
473476
}
474477

475478
bool isSYCL() const { return SYCLIsDevice || SYCLIsHost; }
479+
480+
/// Remap path prefix according to -fmacro-prefix-path option.
481+
void remapPathPrefix(SmallString<256> &Path) const;
476482
};
477483

478484
/// Floating point control options

clang/include/clang/Driver/Options.td

+3-3
Original file line numberDiff line numberDiff line change
@@ -2840,10 +2840,10 @@ def fcoverage_prefix_map_EQ
28402840
HelpText<"remap file source paths in coverage mapping">;
28412841
def ffile_prefix_map_EQ
28422842
: Joined<["-"], "ffile-prefix-map=">, Group<f_Group>,
2843-
HelpText<"remap file source paths in debug info and predefined preprocessor macros">;
2843+
HelpText<"remap file source paths in debug info, predefined preprocessor macros and __builtin_FILE()">;
28442844
def fmacro_prefix_map_EQ
2845-
: Joined<["-"], "fmacro-prefix-map=">, Group<Preprocessor_Group>, Flags<[CC1Option]>,
2846-
HelpText<"remap file source paths in predefined preprocessor macros">;
2845+
: Joined<["-"], "fmacro-prefix-map=">, Group<f_Group>, Flags<[CC1Option]>,
2846+
HelpText<"remap file source paths in predefined preprocessor macros and __builtin_FILE()">;
28472847
defm force_dwarf_frame : BoolFOption<"force-dwarf-frame",
28482848
CodeGenOpts<"ForceDwarfFrameSection">, DefaultFalse,
28492849
PosFlag<SetTrue, [CC1Option], "Always emit a debug frame section">, NegFlag<SetFalse>>;

clang/include/clang/Lex/PreprocessorOptions.h

-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,6 @@ class PreprocessorOptions {
202202
/// build it again.
203203
std::shared_ptr<FailedModulesSet> FailedModules;
204204

205-
/// A prefix map for __FILE__ and __BASE_FILE__.
206-
std::map<std::string, std::string, std::greater<std::string>> MacroPrefixMap;
207-
208205
/// Contains the currently active skipped range mappings for skipping excluded
209206
/// conditional directives.
210207
///

clang/lib/AST/Expr.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -2233,8 +2233,11 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
22332233
};
22342234

22352235
switch (getIdentKind()) {
2236-
case SourceLocExpr::File:
2237-
return MakeStringLiteral(PLoc.getFilename());
2236+
case SourceLocExpr::File: {
2237+
SmallString<256> Path(PLoc.getFilename());
2238+
Ctx.getLangOpts().remapPathPrefix(Path);
2239+
return MakeStringLiteral(Path);
2240+
}
22382241
case SourceLocExpr::Function: {
22392242
const Decl *CurDecl = dyn_cast_or_null<Decl>(Context);
22402243
return MakeStringLiteral(

clang/lib/Basic/LangOptions.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "clang/Basic/LangOptions.h"
14+
#include "llvm/ADT/SmallString.h"
15+
#include "llvm/Support/Path.h"
1416

1517
using namespace clang;
1618

@@ -48,6 +50,12 @@ VersionTuple LangOptions::getOpenCLVersionTuple() const {
4850
return VersionTuple(Ver / 100, (Ver % 100) / 10);
4951
}
5052

53+
void LangOptions::remapPathPrefix(SmallString<256> &Path) const {
54+
for (const auto &Entry : MacroPrefixMap)
55+
if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
56+
break;
57+
}
58+
5159
FPOptions FPOptions::defaultWithoutTrailingStorage(const LangOptions &LO) {
5260
FPOptions result(LO);
5361
return result;

clang/lib/CodeGen/CodeGenModule.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
186186
!getModule().getSourceFileName().empty()) {
187187
std::string Path = getModule().getSourceFileName();
188188
// Check if a path substitution is needed from the MacroPrefixMap.
189-
for (const auto &Entry : PPO.MacroPrefixMap)
189+
for (const auto &Entry : LangOpts.MacroPrefixMap)
190190
if (Path.rfind(Entry.first, 0) != std::string::npos) {
191191
Path = Entry.second + Path.substr(Entry.first.size());
192192
break;

clang/lib/Frontend/CompilerInvocation.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -3526,6 +3526,9 @@ void CompilerInvocation::GenerateLangArgs(const LangOptions &Opts,
35263526
GenerateArg(Args, OPT_fexperimental_relative_cxx_abi_vtables, SA);
35273527
else
35283528
GenerateArg(Args, OPT_fno_experimental_relative_cxx_abi_vtables, SA);
3529+
3530+
for (const auto &MP : Opts.MacroPrefixMap)
3531+
GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA);
35293532
}
35303533

35313534
bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
@@ -4036,6 +4039,12 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
40364039
options::OPT_fno_experimental_relative_cxx_abi_vtables,
40374040
TargetCXXABI::usesRelativeVTables(T));
40384041

4042+
for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) {
4043+
auto Split = StringRef(A).split('=');
4044+
Opts.MacroPrefixMap.insert(
4045+
{std::string(Split.first), std::string(Split.second)});
4046+
}
4047+
40394048
return Diags.getNumErrors() == NumErrorsBefore;
40404049
}
40414050

@@ -4108,9 +4117,6 @@ static void GeneratePreprocessorArgs(PreprocessorOptions &Opts,
41084117
for (const auto &D : Opts.DeserializedPCHDeclsToErrorOn)
41094118
GenerateArg(Args, OPT_error_on_deserialized_pch_decl, D, SA);
41104119

4111-
for (const auto &MP : Opts.MacroPrefixMap)
4112-
GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA);
4113-
41144120
if (Opts.PrecompiledPreambleBytes != std::make_pair(0u, false))
41154121
GenerateArg(Args, OPT_preamble_bytes_EQ,
41164122
Twine(Opts.PrecompiledPreambleBytes.first) + "," +
@@ -4179,12 +4185,6 @@ static bool ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
41794185
for (const auto *A : Args.filtered(OPT_error_on_deserialized_pch_decl))
41804186
Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue());
41814187

4182-
for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) {
4183-
auto Split = StringRef(A).split('=');
4184-
Opts.MacroPrefixMap.insert(
4185-
{std::string(Split.first), std::string(Split.second)});
4186-
}
4187-
41884188
if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) {
41894189
StringRef Value(A->getValue());
41904190
size_t Comma = Value.find(',');

clang/lib/Lex/PPMacroExpansion.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -1460,15 +1460,6 @@ static bool isTargetEnvironment(const TargetInfo &TI,
14601460
return TI.getTriple().getEnvironment() == Env.getEnvironment();
14611461
}
14621462

1463-
static void remapMacroPath(
1464-
SmallString<256> &Path,
1465-
const std::map<std::string, std::string, std::greater<std::string>>
1466-
&MacroPrefixMap) {
1467-
for (const auto &Entry : MacroPrefixMap)
1468-
if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
1469-
break;
1470-
}
1471-
14721463
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
14731464
/// as a builtin macro, handle it and return the next token as 'Tok'.
14741465
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1550,7 +1541,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
15501541
} else {
15511542
FN += PLoc.getFilename();
15521543
}
1553-
remapMacroPath(FN, PPOpts->MacroPrefixMap);
1544+
getLangOpts().remapPathPrefix(FN);
15541545
Lexer::Stringify(FN);
15551546
OS << '"' << FN << '"';
15561547
}

clang/test/CodeGenCXX/builtin-source-location.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// RUN: %clang_cc1 -std=c++2a -fblocks %s -triple x86_64-unknown-unknown -emit-llvm -o %t.ll
22

3+
// This needs to be performed before #line directives which alter filename
4+
// RUN: %clang_cc1 -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-PREFIX-MAP
5+
//
6+
// CHECK-PREFIX-MAP: /UNLIKELY/PATH{{/|\\\\}}builtin-source-location.cpp
7+
void testRemap() {
8+
const char *file = __builtin_FILE();
9+
}
10+
311
#line 8 "builtin-source-location.cpp"
412

513
struct source_location {

0 commit comments

Comments
 (0)