Skip to content

Commit 8d43ec3

Browse files
authored
Merge pull request #17087 from allevato/dwarf-command-line-flags
Make DWARF debug flag behavior match Clang.
2 parents 1edc3be + 50c8942 commit 8d43ec3

File tree

10 files changed

+56
-13
lines changed

10 files changed

+56
-13
lines changed

include/swift/Driver/ToolChain.h

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

200+
/// Specific toolchains should override this to provide additional conditions
201+
/// under which the compiler invocation should be written into debug info. For
202+
/// example, Darwin does this if the RC_DEBUG_OPTIONS environment variable is
203+
/// set to match the behavior of Clang.
204+
virtual bool shouldStoreInvocationInDebugInfo() const { return false; }
205+
200206
public:
201207
virtual ~ToolChain() = default;
202208

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ def verify_debug_info : Flag<["-"], "verify-debug-info">,
476476
Flags<[NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
477477
HelpText<"Verify the binary representation of debug output.">;
478478

479+
def debug_info_store_invocation : Flag<["-"], "debug-info-store-invocation">,
480+
Flags<[FrontendOption]>,
481+
HelpText<"Emit the compiler invocation in the debug info.">;
482+
479483
// Assert configuration identifiers.
480484
def AssertConfig : Separate<["-"], "assert-config">,
481485
Flags<[FrontendOption]>,

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::shouldStoreInvocationInDebugInfo() 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
@@ -364,6 +364,11 @@ ToolChain::constructInvocation(const CompileJobAction &job,
364364
Arguments.push_back("-index-system-modules");
365365
}
366366

367+
if (context.Args.hasArg(options::OPT_debug_info_store_invocation) ||
368+
shouldStoreInvocationInDebugInfo()) {
369+
Arguments.push_back("-debug-info-store-invocation");
370+
}
371+
367372
return II;
368373
}
369374

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 shouldStoreInvocationInDebugInfo() 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_debug_info_store_invocation)) {
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,11 @@ 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())
1079+
IRGenOpts.DWARFDebugFlags += " ";
1080+
IRGenOpts.DWARFDebugFlags += ("-private-discriminator " + PD.str()).str();
1081+
}
10791082
}
10801083

10811084
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: %swiftc_driver %s -emit-ir -g -target x86_64-apple-macosx10.10 -parse-stdlib -module-name scratch -o - | %FileCheck %s
5+
// RUN: RC_DEBUG_OPTIONS=1 %swiftc_driver %s -emit-ir -g -target x86_64-apple-macosx10.10 -parse-stdlib -module-name scratch -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 -debug-info-store-invocation -g -o - | %FileCheck %s
5+
// RUN: %target-swiftc_driver %s -emit-ir -debug-info-store-invocation -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 -debug-info-store-invocation -o - | %FileCheck --check-prefix CHECK-TEMP %s
1719
// CHECK-TEMP: !DICompileUnit({{.*}} flags: "{{.*}} -I <temporary-file>
1820

0 commit comments

Comments
 (0)