Skip to content

Commit 96231e2

Browse files
authored
Support -file-compilation-dir (swiftlang#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
1 parent 89b0a4c commit 96231e2

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,10 @@ def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
844844
Flags<[FrontendOption]>,
845845
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
846846

847+
def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
848+
Flags<[FrontendOption]>, MetaVarName<"<path>">,
849+
HelpText<"The compilation directory to embed in the debug info. Coverage mapping is not supported yet.">;
850+
847851
def debug_info_format : Joined<["-"], "debug-info-format=">,
848852
Flags<[FrontendOption]>,
849853
HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">;

lib/Driver/ToolChains.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
334334
auto OptArg = inputArgs.getLastArgNoClaim(options::OPT_O_Group);
335335
if (!OptArg || OptArg->getOption().matches(options::OPT_Onone))
336336
arguments.push_back("-enable-anonymous-context-mangled-names");
337+
338+
// TODO: Should we support -fcoverage-compilation-dir?
339+
inputArgs.AddAllArgs(arguments, options::OPT_file_compilation_dir);
337340
}
338341

339342
// Pass through any subsystem flags.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,10 +1822,14 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
18221822
RenderedArgs, SDKPath,
18231823
ResourceDir);
18241824
}
1825-
// TODO: Should we support -fdebug-compilation-dir?
1826-
llvm::SmallString<256> cwd;
1827-
llvm::sys::fs::current_path(cwd);
1828-
Opts.DebugCompilationDir = std::string(cwd.str());
1825+
1826+
if (const Arg *A = Args.getLastArg(OPT_file_compilation_dir))
1827+
Opts.DebugCompilationDir = A->getValue();
1828+
else {
1829+
llvm::SmallString<256> cwd;
1830+
llvm::sys::fs::current_path(cwd);
1831+
Opts.DebugCompilationDir = std::string(cwd.str());
1832+
}
18291833
}
18301834

18311835
if (const Arg *A = Args.getLastArg(options::OPT_debug_info_format)) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// UNSUPPORTED: OS=windows-msvc
2+
// RUN: %target-swiftc_driver -g \
3+
// RUN: -c -file-compilation-dir /path/to \
4+
// RUN: %s -o - -emit-ir | %FileCheck --check-prefix=CHECK-ABS %s
5+
// RUN: %empty-directory(%t)
6+
// RUN: mkdir -p %t
7+
// RUN: cd %t
8+
// RUN: cp %s .
9+
// RUN: %target-swiftc_driver -g \
10+
// RUN: -c -file-compilation-dir /path/to \
11+
// RUN: file_compilation_dir.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL %s
12+
// RUN: %target-swiftc_driver -g \
13+
// RUN: -c -file-compilation-dir . \
14+
// RUN: file_compilation_dir.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL-CWD %s
15+
16+
func foo() {}
17+
18+
// CHECK-ABS: !DIFile(filename: "{{.*}}/file_compilation_dir.swift", directory: "/path/to")
19+
// CHECK-REL: !DIFile(filename: "file_compilation_dir.swift", directory: "/path/to")
20+
// CHECK-REL-CWD: !DIFile(filename: "file_compilation_dir.swift", directory: ".")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// REQUIRES: OS=windows-msvc
2+
// RUN: %target-swiftc_driver -g \
3+
// RUN: -c -file-compilation-dir Z:\path\to \
4+
// RUN: %s -o - -emit-ir | %FileCheck --check-prefix=CHECK-ABS %s
5+
// RUN: %empty-directory(%t)
6+
// RUN: mkdir -p %t
7+
// RUN: cd %t
8+
// RUN: xcopy %s .
9+
// RUN: %target-swiftc_driver -g \
10+
// RUN: -c -file-compilation-dir Z:\path\to \
11+
// RUN: file_compilation_dir_windows.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL %s
12+
// RUN: %target-swiftc_driver -g \
13+
// RUN: -c -file-compilation-dir . \
14+
// RUN: file_compilation_dir_windows.swift -o - -emit-ir | %FileCheck --check-prefix=CHECK-REL-CWD %s
15+
16+
func foo() {}
17+
18+
// CHECK-ABS: !DIFile(filename: "{{[a-zA-Z]:\\\\.*\\\\}}file_compilation_dir_windows.swift", directory: "Z:\\path\\to")
19+
// CHECK-REL: !DIFile(filename: "file_compilation_dir_windows.swift", directory: "Z:\\path\\to")
20+
// CHECK-REL-CWD: !DIFile(filename: "file_compilation_dir_windows.swift", directory: ".")

0 commit comments

Comments
 (0)