From bee23124959ec78467865605db51e1590e45acf2 Mon Sep 17 00:00:00 2001 From: Pofat Tseng Date: Sun, 26 Dec 2021 20:57:07 +0000 Subject: [PATCH] 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 --- include/swift/Option/Options.td | 4 ++++ lib/Driver/ToolChains.cpp | 3 +++ lib/Frontend/CompilerInvocation.cpp | 12 +++++++---- test/DebugInfo/file_compilation_dir.swift | 20 +++++++++++++++++++ .../file_compilation_dir_windows.swift | 20 +++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 test/DebugInfo/file_compilation_dir.swift create mode 100644 test/DebugInfo/file_compilation_dir_windows.swift diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td index 5df786fd2d044..70f8d8505f532 100644 --- a/include/swift/Option/Options.td +++ b/include/swift/Option/Options.td @@ -840,6 +840,10 @@ def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">, Flags<[FrontendOption]>, HelpText<"Remap source paths in coverage info">, MetaVarName<"">; +def file_compilation_dir : Separate<["-"], "file-compilation-dir">, + Flags<[FrontendOption]>, MetaVarName<"">, + 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'">; diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 2142f91d190f7..312918e648953 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -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. diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index c50461ae32614..729ae2f923866 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -1797,10 +1797,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)) { diff --git a/test/DebugInfo/file_compilation_dir.swift b/test/DebugInfo/file_compilation_dir.swift new file mode 100644 index 0000000000000..8d029751ad0b1 --- /dev/null +++ b/test/DebugInfo/file_compilation_dir.swift @@ -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: ".") diff --git a/test/DebugInfo/file_compilation_dir_windows.swift b/test/DebugInfo/file_compilation_dir_windows.swift new file mode 100644 index 0000000000000..a83fd2810eb2a --- /dev/null +++ b/test/DebugInfo/file_compilation_dir_windows.swift @@ -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: ".")