Skip to content

Commit 22ac700

Browse files
committed
Make DWARF debug flag behavior match Clang.
Only write the compilation flags to debug info for Mach-O targets, and only if the RC_DEBUG_OPTIONS environment variable is set.
1 parent db75a67 commit 22ac700

File tree

10 files changed

+52
-13
lines changed

10 files changed

+52
-13
lines changed

include/swift/Driver/ToolChain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ class ToolChain {
197197
const llvm::opt::ArgList &args,
198198
StringRef extraEntry = "") const;
199199

200+
/// Specific toolchains should override this to indicate whether the
201+
/// compilation flags should be written into DWARF debug info.
202+
virtual bool usesDWARFDebugFlags() const { return false; }
203+
200204
public:
201205
virtual ~ToolChain() = default;
202206

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,7 @@ def disable_verify_exclusivity : Flag<["-"], "disable-verify-exclusivity">,
486486
def enable_key_path_resilience : Flag<["-"], "enable-key-path-resilience">,
487487
HelpText<"Enable key path resilience.">;
488488

489+
def use_dwarf_debug_flags : Flag<["-"], "use-dwarf-debug-flags">,
490+
HelpText<"Write the compilation flags into the DWARF debug info.">;
491+
489492
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

lib/Driver/DarwinToolChains.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,11 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
451451

452452
return II;
453453
}
454+
455+
bool toolchains::Darwin::usesDWARFDebugFlags() const {
456+
// This matches the behavior in Clang (see
457+
// clang/lib/driver/ToolChains/Darwin.cpp).
458+
if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
459+
return S[0] != '\0';
460+
return false;
461+
}

lib/Driver/ToolChains.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ ToolChain::constructInvocation(const CompileJobAction &job,
360360
Arguments.push_back("-index-system-modules");
361361
}
362362

363+
// Request whether the compilation flags should be written into DWARF info.
364+
if (usesDWARFDebugFlags()) {
365+
Arguments.push_back("-use-dwarf-debug-flags");
366+
}
367+
363368
return II;
364369
}
365370

lib/Driver/ToolChains.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
3030

3131
std::string findProgramRelativeToSwiftImpl(StringRef name) const override;
3232

33+
bool usesDWARFDebugFlags() const override;
34+
3335
public:
3436
Darwin(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
3537
~Darwin() = default;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,12 +769,14 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
769769
"unknown -g<kind> option");
770770

771771
if (Opts.DebugInfoKind > IRGenDebugInfoKind::LineTables) {
772-
ArgStringList RenderedArgs;
773-
for (auto A : Args)
774-
A->render(Args, RenderedArgs);
775-
CompilerInvocation::buildDWARFDebugFlags(Opts.DWARFDebugFlags,
776-
RenderedArgs, SDKPath,
777-
ResourceDir);
772+
if (Args.hasArg(options::OPT_use_dwarf_debug_flags)) {
773+
ArgStringList RenderedArgs;
774+
for (auto A : Args)
775+
A->render(Args, RenderedArgs);
776+
CompilerInvocation::buildDWARFDebugFlags(Opts.DWARFDebugFlags,
777+
RenderedArgs, SDKPath,
778+
ResourceDir);
779+
}
778780
// TODO: Should we support -fdebug-compilation-dir?
779781
llvm::SmallString<256> cwd;
780782
llvm::sys::fs::current_path(cwd);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,10 @@ static void setPrivateDiscriminatorIfNeeded(IRGenOptions &IRGenOpts,
10741074
!MSF.is<SourceFile *>())
10751075
return;
10761076
Identifier PD = MSF.get<SourceFile *>()->getPrivateDiscriminator();
1077-
if (!PD.empty())
1078-
IRGenOpts.DWARFDebugFlags += (" -private-discriminator " + PD.str()).str();
1077+
if (!PD.empty()) {
1078+
if (!IRGenOpts.DWARFDebugFlags.empty()) IRGenOpts.DWARFDebugFlags += " ";
1079+
IRGenOpts.DWARFDebugFlags += ("-private-discriminator " + PD.str()).str();
1080+
}
10791081
}
10801082

10811083
static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs,

test/DebugInfo/basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func foo(_ a: Int64, _ b: Int64) -> Int64 {
6464

6565
// CHECK-DAG: ![[FILE_CWD:[0-9]+]] = !DIFile(filename: "{{.*}}DebugInfo/basic.swift", directory: "{{.*}}")
6666
// CHECK-DAG: ![[MAINFILE:[0-9]+]] = !DIFile(filename: "basic.swift", directory: "{{.*}}DebugInfo")
67-
// CHECK-DAG: !DICompileUnit(language: DW_LANG_Swift, file: ![[FILE_CWD]],{{.*}} producer: "{{.*}}Swift version{{.*}},{{.*}} flags: "{{[^"]*}}-emit-ir
67+
// CHECK-DAG: !DICompileUnit(language: DW_LANG_Swift, file: ![[FILE_CWD]],{{.*}} producer: "{{.*}}Swift version{{.*}},{{.*}}
6868
// CHECK-DAG: !DISubprogram(name: "main", {{.*}}file: ![[MAINFILE]],
6969

7070
// Function type for foo.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Check that the sdk and resource dirs end up in the debug info if we build for
2+
// a Darwin target and set the RC_DEBUG_OPTIONS environment variable. This
3+
// matches the behavior found in Clang.
4+
// RUN: %target-swiftc_driver %s -emit-ir -g -target x86_64-apple-macosx10.10 -o - | %FileCheck %s
5+
// RUN: RC_DEBUG_OPTIONS=1 %target-swiftc_driver %s -emit-ir -g -target x86_64-apple-macosx10.10 -o - | %FileCheck --check-prefix CHECK-VAR-SET %s
6+
// CHECK: !DICompileUnit({{.*}} producer: "{{(Apple )?Swift version [^"]+}}"
7+
// CHECK-NOT: flags: "
8+
// CHECK-VAR-SET: !DICompileUnit({{.*}}producer: "{{(Apple )?Swift version [^"]+}}"
9+
// CHECK-VAR-SET-SAME: flags: "
10+
// CHECK-VAR-SET-NOT: "
11+
// CHECK-VAR-SET-SAME: -resource-dir

test/DebugInfo/compiler-flags.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Check that the sdk and resource dirs end up in the debug info.
2-
// RUN: %target-swiftc_driver %s -emit-ir -g -o - | %FileCheck %s
3-
// RUN: %target-swiftc_driver %s -emit-ir -sdk "/Weird Location/SDK" -g -o - | %FileCheck --check-prefix CHECK-EXPLICIT %s
1+
// Check that the sdk and resource dirs end up in the debug info if we pass the
2+
// frontend flag. This tests the general functionality; we test the macosx
3+
// specific toolchain logic in compiler-flags-macosx.swift.
4+
// RUN: %target-swiftc_driver %s -emit-ir -Xfrontend -use-dwarf-debug-flags -g -o - | %FileCheck %s
5+
// RUN: %target-swiftc_driver %s -emit-ir -Xfrontend -use-dwarf-debug-flags -sdk "/Weird Location/SDK" -g -o - | %FileCheck --check-prefix CHECK-EXPLICIT %s
46
// CHECK: !DICompileUnit({{.*}}producer: "{{(Apple )?Swift version [^"]+}}"
57
// CHECK-SAME: flags: "
68
// CHECK-NOT: "
@@ -13,6 +15,6 @@
1315
// CHECK-EXPLICIT-SAME: -resource-dir
1416

1517
// Check that we don't write temporary file names in the debug info
16-
// RUN: TMPDIR=abc/def %target-swift-frontend %s -I abc/def/xyz -g -emit-ir -o - | %FileCheck --check-prefix CHECK-TEMP %s
18+
// RUN: TMPDIR=abc/def %target-swift-frontend %s -I abc/def/xyz -g -emit-ir -use-dwarf-debug-flags -o - | %FileCheck --check-prefix CHECK-TEMP %s
1719
// CHECK-TEMP: !DICompileUnit({{.*}} flags: "{{.*}} -I <temporary-file>
1820

0 commit comments

Comments
 (0)