Skip to content

Commit bee2312

Browse files
committed
1. Add new flag: -file-compilation-dir
2. Implement the logic: Configure DebugCompilationDir with the path specified by the flag, otherwise with current working directory. 3. Add test case for mac, linux and windows
1 parent 5a2df1c commit bee2312

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
@@ -840,6 +840,10 @@ def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
840840
Flags<[FrontendOption]>,
841841
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
842842

843+
def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
844+
Flags<[FrontendOption]>, MetaVarName<"<path>">,
845+
HelpText<"The compilation directory to embed in the debug info. Coverage mapping is not supported yet.">;
846+
843847
def debug_info_format : Joined<["-"], "debug-info-format=">,
844848
Flags<[FrontendOption]>,
845849
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
@@ -1797,10 +1797,14 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
17971797
RenderedArgs, SDKPath,
17981798
ResourceDir);
17991799
}
1800-
// TODO: Should we support -fdebug-compilation-dir?
1801-
llvm::SmallString<256> cwd;
1802-
llvm::sys::fs::current_path(cwd);
1803-
Opts.DebugCompilationDir = std::string(cwd.str());
1800+
1801+
if (const Arg *A = Args.getLastArg(OPT_file_compilation_dir))
1802+
Opts.DebugCompilationDir = A->getValue();
1803+
else {
1804+
llvm::SmallString<256> cwd;
1805+
llvm::sys::fs::current_path(cwd);
1806+
Opts.DebugCompilationDir = std::string(cwd.str());
1807+
}
18041808
}
18051809

18061810
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)