Support -file-compilation-dir (#40735)

This PR adds a new flag -file-compilation-dir, which does the same thing as -ffile-compilation-dir in Clang.

swiftc -g -ffile-compilation-dir=. path/to/foo.swift gives us identical debug info paths regardless of what location we compiled the file from. It's useful to debug correctly using object files built on different machines in different locations.
There's also a long-existed TODO comment.

Resolves SR-5694
This commit is contained in:
Pofat
2022-01-28 04:56:10 +00:00
committed by GitHub
parent 89b0a4ce89
commit 96231e2d5f
5 changed files with 55 additions and 4 deletions

View File

@@ -844,6 +844,10 @@ def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
Flags<[FrontendOption]>,
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
Flags<[FrontendOption]>, MetaVarName<"<path>">,
HelpText<"The compilation directory to embed in the debug info. Coverage mapping is not supported yet.">;
def debug_info_format : Joined<["-"], "debug-info-format=">,
Flags<[FrontendOption]>,
HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">;

View File

@@ -334,6 +334,9 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
auto OptArg = inputArgs.getLastArgNoClaim(options::OPT_O_Group);
if (!OptArg || OptArg->getOption().matches(options::OPT_Onone))
arguments.push_back("-enable-anonymous-context-mangled-names");
// TODO: Should we support -fcoverage-compilation-dir?
inputArgs.AddAllArgs(arguments, options::OPT_file_compilation_dir);
}
// Pass through any subsystem flags.

View File

@@ -1822,10 +1822,14 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
RenderedArgs, SDKPath,
ResourceDir);
}
// TODO: Should we support -fdebug-compilation-dir?
llvm::SmallString<256> cwd;
llvm::sys::fs::current_path(cwd);
Opts.DebugCompilationDir = std::string(cwd.str());
if (const Arg *A = Args.getLastArg(OPT_file_compilation_dir))
Opts.DebugCompilationDir = A->getValue();
else {
llvm::SmallString<256> cwd;
llvm::sys::fs::current_path(cwd);
Opts.DebugCompilationDir = std::string(cwd.str());
}
}
if (const Arg *A = Args.getLastArg(options::OPT_debug_info_format)) {

View File

@@ -0,0 +1,20 @@
// UNSUPPORTED: OS=windows-msvc
// RUN: %target-swiftc_driver -g \
// RUN: -c -file-compilation-dir /path/to \
// RUN: %s -o - -emit-ir | %FileCheck --check-prefix=CHECK-ABS %s
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t
// RUN: cd %t
// RUN: cp %s .
// RUN: %target-swiftc_driver -g \
// RUN: -c -file-compilation-dir /path/to \
// RUN: file_compilation_dir.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL %s
// RUN: %target-swiftc_driver -g \
// RUN: -c -file-compilation-dir . \
// RUN: file_compilation_dir.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL-CWD %s
func foo() {}
// CHECK-ABS: !DIFile(filename: "{{.*}}/file_compilation_dir.swift", directory: "/path/to")
// CHECK-REL: !DIFile(filename: "file_compilation_dir.swift", directory: "/path/to")
// CHECK-REL-CWD: !DIFile(filename: "file_compilation_dir.swift", directory: ".")

View File

@@ -0,0 +1,20 @@
// REQUIRES: OS=windows-msvc
// RUN: %target-swiftc_driver -g \
// RUN: -c -file-compilation-dir Z:\path\to \
// RUN: %s -o - -emit-ir | %FileCheck --check-prefix=CHECK-ABS %s
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t
// RUN: cd %t
// RUN: xcopy %s .
// RUN: %target-swiftc_driver -g \
// RUN: -c -file-compilation-dir Z:\path\to \
// RUN: file_compilation_dir_windows.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL %s
// RUN: %target-swiftc_driver -g \
// RUN: -c -file-compilation-dir . \
// RUN: file_compilation_dir_windows.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL-CWD %s
func foo() {}
// CHECK-ABS: !DIFile(filename: "{{[a-zA-Z]:\\\\.*\\\\}}file_compilation_dir_windows.swift", directory: "Z:\\path\\to")
// CHECK-REL: !DIFile(filename: "file_compilation_dir_windows.swift", directory: "Z:\\path\\to")
// CHECK-REL-CWD: !DIFile(filename: "file_compilation_dir_windows.swift", directory: ".")