ayzhao created this revision. Herald added a project: All. ayzhao requested review of this revision. Herald added subscribers: cfe-commits, MaskRay. Herald added a project: clang.
When Clang generates the path prefix (i.e. the path of the directory where the file is) when generating __FILE__, __builtin_FILE(), and std::source_location, Clang uses the platform-specific path separator character of the build environment where Clang _itself_ is built. This leads to inconsistencies in Chrome builds where Clang running on non-Windows environments uses the forward slash (/) path separator while Clang running on Windows builds uses the backslash (\) path separator. To fix this, we add a flag -ffile-reproducible (and its inverse, -fno-file-reproducible) to have Clang use the target's platform-specific file separator character. Additionally, the existing flags -fmacro-prefix-map and -ffile-prefix-map now both imply -ffile-reproducible. This can be overriden by setting -fno-file-reproducible. [0]: https://crbug.com/1310767 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D125138 Files: clang/include/clang/Basic/LangOptions.h clang/include/clang/Driver/Options.td clang/include/clang/Lex/Preprocessor.h clang/lib/AST/Expr.cpp clang/lib/Basic/LangOptions.cpp clang/lib/Driver/ToolChains/Clang.cpp clang/lib/Frontend/CompilerInvocation.cpp clang/lib/Lex/PPMacroExpansion.cpp clang/test/CodeGenCXX/builtin-source-location.cpp clang/test/Preprocessor/file_test.c clang/test/Preprocessor/file_test_windows.c
Index: clang/test/Preprocessor/file_test_windows.c =================================================================== --- clang/test/Preprocessor/file_test_windows.c +++ clang/test/Preprocessor/file_test_windows.c @@ -1,9 +1,25 @@ // REQUIRES: system-windows -// RUN: %clang -E -ffile-prefix-map=%p=A:\UNLIKELY_PATH\empty -c -o - %s | FileCheck %s -// RUN: %clang -E -fmacro-prefix-map=%p=A:\UNLIKELY_PATH\empty -c -o - %s | FileCheck %s -// RUN: %clang -E -fmacro-prefix-map=%p=A:\UNLIKELY_PATH=empty -c -o - %s | FileCheck %s -check-prefix CHECK-EVIL -// RUN: %clang -E -fmacro-prefix-map=%p/iNPUTS\=A:\UNLIKELY_PATH_INC\ -fmacro-prefix-map=%p/=A:\UNLIKELY_PATH_BASE\ -c -o - %s | FileCheck %s -check-prefix CHECK-CASE -// RUN: %clang -E -fmacro-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE +// RUN: %clang -E -fno-file-reproducible -ffile-prefix-map=%p=A:\UNLIKELY_PATH\empty -c -o - %s | FileCheck %s +// RUN: %clang -E -fno-file-reproducible -fmacro-prefix-map=%p=A:\UNLIKELY_PATH\empty -c -o - %s | FileCheck %s +// RUN: %clang -E -fno-file-reproducible -fmacro-prefix-map=%p=A:\UNLIKELY_PATH=empty -c -o - %s | FileCheck %s -check-prefix CHECK-EVIL +// RUN: %clang -E -fno-file-reproducible -fmacro-prefix-map=%p/iNPUTS\=A:\UNLIKELY_PATH_INC\ -fmacro-prefix-map=%p/=A:\UNLIKELY_PATH_BASE\ -c -o - %s | FileCheck %s -check-prefix CHECK-CASE +// RUN: %clang -E -fno-file-reproducible -fmacro-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE + +// RUN: %clang -E -ffile-prefix-map=%p=A:\UNLIKELY_PATH\empty -c -o - %s | FileCheck %s --check-prefix CHECK-REPRODUCIBLE +// RUN: %clang -E -fmacro-prefix-map=%p=A:\UNLIKELY_PATH\empty -c -o - %s | FileCheck %s --check-prefix CHECK-REPRODUCIBLE +// RUN: %clang -E -fmacro-prefix-map=%p=A:\UNLIKELY_PATH=empty -c -o - %s | FileCheck %s -check-prefix CHECK-EVIL-REPRODUCIBLE +// RUN: %clang -E -fmacro-prefix-map=%p/iNPUTS\=A:\UNLIKELY_PATH_INC\ -fmacro-prefix-map=%p/=A:\UNLIKELY_PATH_BASE\ -c -o - %s | FileCheck %s -check-prefix CHECK-CASE-REPRODUCIBLE +// RUN: %clang -E -fmacro-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE-REPRODUCIBLE + +// RUN: %clang -E -target x86_64-pc-linux-gnu -fmacro-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE +// RUN: %clang -E -target x86_64-pc-linux-gnu -ffile-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE + +// Clang defaults to forward slashes for the non-prefix portion of the path even if the build environment is Windows. +// RUN: %clang -E -fno-file-reproducible -target x86_64-pc-linux-gnu -fmacro-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE +// RUN: %clang -E -fno-file-reproducible -target x86_64-pc-linux-gnu -ffile-prefix-map=%p\= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE + +// RUN: %clang -E -ffile-reproducible -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-WINDOWS-FULL +// RUN: %clang -E -ffile-reproducible -target x86_64-pc-linux-gnu -c -o - %s | FileCheck %s --check-prefix CHECK-LINUX-FULL filename: __FILE__ #include "Inputs/include-file-test/file_test.h" @@ -27,3 +43,34 @@ // CHECK-REMOVE: filename: "Inputs/include-file-test/file_test.h" // CHECK-REMOVE: basefile: "file_test_windows.c" // CHECK-REMOVE-NOT: filename: + +// CHECK-REPRODUCIBLE: filename: "A:\\UNLIKELY_PATH\\empty\\file_test_windows.c" +// CHECK-REPRODUCIBLE: filename: "A:\\UNLIKELY_PATH\\empty\\Inputs\\include-file-test\\file_test.h" +// CHECK-REPRODUCIBLE: basefile: "A:\\UNLIKELY_PATH\\empty\\file_test_windows.c" +// CHECK-REPRODUCIBLE-NOT: filename: + +// CHECK-EVIL-REPRODUCIBLE: filename: "A:\\UNLIKELY_PATH=empty\\file_test_windows.c" +// CHECK-EVIL-REPRODUCIBLE: filename: "A:\\UNLIKELY_PATH=empty\\Inputs\\include-file-test\\file_test.h" +// CHECK-EVIL-REPRODUCIBLE: basefile: "A:\\UNLIKELY_PATH=empty\\file_test_windows.c" +// CHECK-EVIL-REPRODUCIBLE-NOT: filename: + +// CHECK-CASE-REPRODUCIBLE: filename: "A:\\UNLIKELY_PATH_BASE\\file_test_windows.c" +// CHECK-CASE-REPRODUCIBLE: filename: "A:\\UNLIKELY_PATH_INC\\include-file-test\\file_test.h" +// CHECK-CASE-REPRODUCIBLE: basefile: "A:\\UNLIKELY_PATH_BASE\\file_test_windows.c" +// CHECK-CASE-REPRODUCIBLE-NOT: filename: + +// CHECK-REMOVE-REPRODUCIBLE: filename: "file_test_windows.c" +// CHECK-REMOVE-REPRODUCIBLE: filename: "Inputs\\include-file-test\\file_test.h" +// CHECK-REMOVE-REPRODUCIBLE: basefile: "file_test_windows.c" +// CHECK-REMOVE-REPRODUCIBLE-NOT: filename: + +// CHECK-WINDOWS-FULL: filename: "{{[^/]*}}file_test_windows.c" +// CHECK-WINDOWS-FULL: filename: "{{[^/]*}}Inputs\\include-file-test\\file_test.h" +// CHECK-WINDOWS-FULL: basefile: "{{[^/]*}}file_test_windows.c" +// CHECK-WINDOWS-FULL-NOT: filename: + +// Clang does not modify the prefix for POSIX style, so it may have backslashes. +// CHECK-LINUX-FULL: filename: "{{.*}}file_test_windows.c" +// CHECK-LINUX-FULL: filename: "{{.*}}Inputs/include-file-test/file_test.h" +// CHECK-LINUX-FULL: basefile: "{{.*}}file_test_windows.c" +// CHECK-LINUX-FULL-NOT: filename: \ No newline at end of file Index: clang/test/Preprocessor/file_test.c =================================================================== --- clang/test/Preprocessor/file_test.c +++ clang/test/Preprocessor/file_test.c @@ -3,6 +3,11 @@ // RUN: %clang -E -fmacro-prefix-map=%p=/UNLIKELY_PATH/empty -c -o - %s | FileCheck %s // RUN: %clang -E -fmacro-prefix-map=%p=/UNLIKELY_PATH=empty -c -o - %s | FileCheck %s -check-prefix CHECK-EVIL // RUN: %clang -E -fmacro-prefix-map=%p/= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE +// RUN: %clang -E -fno-file-reproducible -fmacro-prefix-map=%p/= -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE +// RUN: %clang -E -fno-file-reproducible -ffile-prefix-map=%p/= -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE +// RUN: %clang -E -fmacro-prefix-map=%p/= -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-WINDOWS +// RUN: %clang -E -ffile-prefix-map=%p/= -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-WINDOWS +// RUN: %clang -E -ffile-reproducible -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-WINDOWS-REPRODUCIBLE filename: __FILE__ #include "Inputs/include-file-test/file_test.h" @@ -21,3 +26,13 @@ // CHECK-REMOVE: filename: "Inputs/include-file-test/file_test.h" // CHECK-REMOVE: basefile: "file_test.c" // CHECK-REMOVE-NOT: filename: + +// CHECK-WINDOWS: filename: "file_test.c" +// CHECK-WINDOWS: filename: "Inputs\\include-file-test\\file_test.h" +// CHECK-WINDOWS: basefile: "file_test.c" +// CHECK-WINDOWS-NOT: filename: + +// CHECK-WINDOWS-REPRODUCIBLE: filename: "{{[^/]*}}file_test.c" +// CHECK-WINDOWS-REPRODUCIBLE: filename: "{{[^/]*}}Inputs\\include-file-test\\file_test.h" +// CHECK-WINDOWS-REPRODUCIBLE: basefile: "{{[^/]*}}file_test.c" +// CHECK-WINDOWS-REPRODUCIBLE-NOT: filename: \ No newline at end of file Index: clang/test/CodeGenCXX/builtin-source-location.cpp =================================================================== --- clang/test/CodeGenCXX/builtin-source-location.cpp +++ clang/test/CodeGenCXX/builtin-source-location.cpp @@ -1,7 +1,7 @@ // RUN: %clang_cc1 -no-opaque-pointers -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 -no-opaque-pointers -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-PREFIX-MAP +// RUN: %clang_cc1 -no-opaque-pointers -fno-file-reproducible -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() { Index: clang/lib/Lex/PPMacroExpansion.cpp =================================================================== --- clang/lib/Lex/PPMacroExpansion.cpp +++ clang/lib/Lex/PPMacroExpansion.cpp @@ -1511,7 +1511,7 @@ } else { FN += PLoc.getFilename(); } - getLangOpts().remapPathPrefix(FN); + processPathForFileMacro(FN, getLangOpts(), getTargetInfo()); Lexer::Stringify(FN); OS << '"' << FN << '"'; } @@ -1886,3 +1886,17 @@ WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); MI->setIsUsed(true); } + +void Preprocessor::processPathForFileMacro(SmallVectorImpl<char> &Path, + const LangOptions &LangOpts, + const TargetInfo &TI) { + LangOpts.remapPathPrefix(Path); + if (LangOpts.UseTargetPathSeparator) { + if (TI.getTriple().isOSWindows()) { + llvm::sys::path::make_preferred( + Path, llvm::sys::path::Style::windows_backslash); + } else { + llvm::sys::path::make_preferred(Path, llvm::sys::path::Style::posix); + } + } +} Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3541,6 +3541,11 @@ else GenerateArg(Args, OPT_fno_experimental_relative_cxx_abi_vtables, SA); + if (Opts.UseTargetPathSeparator) + GenerateArg(Args, OPT_ffile_reproducible, SA); + else + GenerateArg(Args, OPT_fno_file_reproducible, SA); + for (const auto &MP : Opts.MacroPrefixMap) GenerateArg(Args, OPT_fmacro_prefix_map_EQ, MP.first + "=" + MP.second, SA); @@ -4093,6 +4098,12 @@ {std::string(Split.first), std::string(Split.second)}); } + Opts.UseTargetPathSeparator = + !Args.getLastArg(options::OPT_fno_file_reproducible) && + (Args.getLastArg(options::OPT_ffile_compilation_dir_EQ) || + Args.getLastArg(options::OPT_fmacro_prefix_map_EQ) || + Args.getLastArg(options::OPT_ffile_reproducible)); + // Error if -mvscale-min is unbounded. if (Arg *A = Args.getLastArg(options::OPT_mvscale_min_EQ)) { unsigned VScaleMin; Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -1485,6 +1485,9 @@ addMacroPrefixMapArg(D, Args, CmdArgs); addCoveragePrefixMapArg(D, Args, CmdArgs); + + Args.AddLastArg(CmdArgs, options::OPT_ffile_reproducible); + Args.AddLastArg(CmdArgs, options::OPT_fno_file_reproducible); } // FIXME: Move to target hook. Index: clang/lib/Basic/LangOptions.cpp =================================================================== --- clang/lib/Basic/LangOptions.cpp +++ clang/lib/Basic/LangOptions.cpp @@ -62,7 +62,7 @@ llvm_unreachable("Unknown OpenCL version"); } -void LangOptions::remapPathPrefix(SmallString<256> &Path) const { +void LangOptions::remapPathPrefix(SmallVectorImpl<char> &Path) const { for (const auto &Entry : MacroPrefixMap) if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) break; Index: clang/lib/AST/Expr.cpp =================================================================== --- clang/lib/AST/Expr.cpp +++ clang/lib/AST/Expr.cpp @@ -31,6 +31,7 @@ #include "clang/Basic/TargetInfo.h" #include "clang/Lex/Lexer.h" #include "clang/Lex/LiteralSupport.h" +#include "clang/Lex/Preprocessor.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" @@ -2189,7 +2190,8 @@ switch (getIdentKind()) { case SourceLocExpr::File: { SmallString<256> Path(PLoc.getFilename()); - Ctx.getLangOpts().remapPathPrefix(Path); + clang::Preprocessor::processPathForFileMacro(Path, Ctx.getLangOpts(), + Ctx.getTargetInfo()); return MakeStringLiteral(Path); } case SourceLocExpr::Function: { @@ -2222,7 +2224,8 @@ StringRef Name = F->getName(); if (Name == "_M_file_name") { SmallString<256> Path(PLoc.getFilename()); - Ctx.getLangOpts().remapPathPrefix(Path); + clang::Preprocessor::processPathForFileMacro(Path, Ctx.getLangOpts(), + Ctx.getTargetInfo()); Value.getStructField(F->getFieldIndex()) = MakeStringLiteral(Path); } else if (Name == "_M_function_name") { // Note: this emits the PrettyFunction name -- different than what Index: clang/include/clang/Lex/Preprocessor.h =================================================================== --- clang/include/clang/Lex/Preprocessor.h +++ clang/include/clang/Lex/Preprocessor.h @@ -2580,6 +2580,10 @@ emitRestrictExpansionWarning(Identifier); } + static void processPathForFileMacro(SmallVectorImpl<char> &Path, + const LangOptions &LangOpts, + const TargetInfo &TI); + private: void emitMacroDeprecationWarning(const Token &Identifier) const; void emitRestrictExpansionWarning(const Token &Identifier) const; Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1507,6 +1507,14 @@ def : Flag<["-"], "fno-extended-identifiers">, Group<f_Group>, Flags<[Unsupported]>; def fhosted : Flag<["-"], "fhosted">, Group<f_Group>; def fdenormal_fp_math_EQ : Joined<["-"], "fdenormal-fp-math=">, Group<f_Group>, Flags<[CC1Option]>; +def ffile_reproducible : Flag<["-"], "ffile-reproducible">, Group<f_Group>, + Flags<[CoreOption, CC1Option]>, + HelpText<"Use the target's platform-specific path separator character when " + "expanding the __FILE__ macro">; +def fno_file_reproducible : Flag<["-"], "fno-file-reproducible">, + Group<f_Group>, Flags<[CoreOption, CC1Option]>, + HelpText<"Use the build environment's platform-specific path separator " + "character when expanding the __FILE__ macro">; def ffp_eval_method_EQ : Joined<["-"], "ffp-eval-method=">, Group<f_Group>, Flags<[CC1Option]>, HelpText<"Specifies the evaluation method to use for floating-point arithmetic.">, Values<"source,double,extended">, NormalizedValuesScope<"LangOptions">, @@ -3009,10 +3017,10 @@ HelpText<"remap file source paths in coverage mapping">; def ffile_prefix_map_EQ : Joined<["-"], "ffile-prefix-map=">, Group<f_Group>, - HelpText<"remap file source paths in debug info, predefined preprocessor macros and __builtin_FILE()">; + HelpText<"remap file source paths in debug info, predefined preprocessor macros and __builtin_FILE(). Implies -ffile-reproducible.">; def fmacro_prefix_map_EQ : Joined<["-"], "fmacro-prefix-map=">, Group<f_Group>, Flags<[CC1Option]>, - HelpText<"remap file source paths in predefined preprocessor macros and __builtin_FILE()">; + HelpText<"remap file source paths in predefined preprocessor macros and __builtin_FILE(). Implies -ffile-reproducible.">; defm force_dwarf_frame : BoolFOption<"force-dwarf-frame", CodeGenOpts<"ForceDwarfFrameSection">, DefaultFalse, PosFlag<SetTrue, [CC1Option], "Always emit a debug frame section">, NegFlag<SetFalse>>; Index: clang/include/clang/Basic/LangOptions.h =================================================================== --- clang/include/clang/Basic/LangOptions.h +++ clang/include/clang/Basic/LangOptions.h @@ -452,6 +452,14 @@ /// The seed used by the randomize structure layout feature. std::string RandstructSeed; + /// Indicates whether the __FILE__ macro should use the target's + /// platform-specific file separator or whether it should use the build + /// environment's platform-specific file separator. + /// + /// The plaform-specific path separator is the backslash(\) for Windows and + /// forward slash (/) elsewhere. + bool UseTargetPathSeparator = false; + LangOptions(); /// Set language defaults for the given input language and @@ -577,7 +585,7 @@ bool isSYCL() const { return SYCLIsDevice || SYCLIsHost; } /// Remap path prefix according to -fmacro-prefix-path option. - void remapPathPrefix(SmallString<256> &Path) const; + void remapPathPrefix(SmallVectorImpl<char> &Path) const; }; /// Floating point control options
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits