Skip to content

Make DWARF debug flag behavior match Clang. #17087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ class ToolChain {
const llvm::opt::ArgList &args,
StringRef extraEntry = "") const;

/// Specific toolchains should override this to provide additional conditions
/// under which the compiler invocation should be written into debug info. For
/// example, Darwin does this if the RC_DEBUG_OPTIONS environment variable is
/// set to match the behavior of Clang.
virtual bool shouldStoreInvocationInDebugInfo() const { return false; }

public:
virtual ~ToolChain() = default;

Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ def verify_debug_info : Flag<["-"], "verify-debug-info">,
Flags<[NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Verify the binary representation of debug output.">;

def debug_info_store_invocation : Flag<["-"], "debug-info-store-invocation">,
Flags<[FrontendOption]>,
HelpText<"Emit the compiler invocation in the debug info.">;

// Assert configuration identifiers.
def AssertConfig : Separate<["-"], "assert-config">,
Flags<[FrontendOption]>,
Expand Down
8 changes: 8 additions & 0 deletions lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,11 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,

return II;
}

bool toolchains::Darwin::shouldStoreInvocationInDebugInfo() const {
// This matches the behavior in Clang (see
// clang/lib/driver/ToolChains/Darwin.cpp).
if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
return S[0] != '\0';
return false;
}
5 changes: 5 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ ToolChain::constructInvocation(const CompileJobAction &job,
Arguments.push_back("-index-system-modules");
}

if (context.Args.hasArg(options::OPT_debug_info_store_invocation) ||
shouldStoreInvocationInDebugInfo()) {
Arguments.push_back("-debug-info-store-invocation");
}

return II;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {

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

bool shouldStoreInvocationInDebugInfo() const override;

public:
Darwin(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
~Darwin() = default;
Expand Down
14 changes: 8 additions & 6 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,14 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
"unknown -g<kind> option");

if (Opts.DebugInfoKind > IRGenDebugInfoKind::LineTables) {
ArgStringList RenderedArgs;
for (auto A : Args)
A->render(Args, RenderedArgs);
CompilerInvocation::buildDWARFDebugFlags(Opts.DWARFDebugFlags,
RenderedArgs, SDKPath,
ResourceDir);
if (Args.hasArg(options::OPT_debug_info_store_invocation)) {
ArgStringList RenderedArgs;
for (auto A : Args)
A->render(Args, RenderedArgs);
CompilerInvocation::buildDWARFDebugFlags(Opts.DWARFDebugFlags,
RenderedArgs, SDKPath,
ResourceDir);
}
// TODO: Should we support -fdebug-compilation-dir?
llvm::SmallString<256> cwd;
llvm::sys::fs::current_path(cwd);
Expand Down
7 changes: 5 additions & 2 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,11 @@ static void setPrivateDiscriminatorIfNeeded(IRGenOptions &IRGenOpts,
!MSF.is<SourceFile *>())
return;
Identifier PD = MSF.get<SourceFile *>()->getPrivateDiscriminator();
if (!PD.empty())
IRGenOpts.DWARFDebugFlags += (" -private-discriminator " + PD.str()).str();
if (!PD.empty()) {
if (!IRGenOpts.DWARFDebugFlags.empty())
IRGenOpts.DWARFDebugFlags += " ";
IRGenOpts.DWARFDebugFlags += ("-private-discriminator " + PD.str()).str();
}
}

static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs,
Expand Down
2 changes: 1 addition & 1 deletion test/DebugInfo/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func foo(_ a: Int64, _ b: Int64) -> Int64 {

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

// Function type for foo.
Expand Down
11 changes: 11 additions & 0 deletions test/DebugInfo/compiler-flags-macosx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Check that the sdk and resource dirs end up in the debug info if we build for
// a Darwin target and set the RC_DEBUG_OPTIONS environment variable. This
// matches the behavior found in Clang.
// RUN: %swiftc_driver %s -emit-ir -g -target x86_64-apple-macosx10.10 -parse-stdlib -module-name scratch -o - | %FileCheck %s
// 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
// CHECK: !DICompileUnit({{.*}} producer: "{{(Apple )?Swift version [^"]+}}"
// CHECK-NOT: flags: "
// CHECK-VAR-SET: !DICompileUnit({{.*}}producer: "{{(Apple )?Swift version [^"]+}}"
// CHECK-VAR-SET-SAME: flags: "
// CHECK-VAR-SET-NOT: "
// CHECK-VAR-SET-SAME: -resource-dir
10 changes: 6 additions & 4 deletions test/DebugInfo/compiler-flags.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Check that the sdk and resource dirs end up in the debug info.
// RUN: %target-swiftc_driver %s -emit-ir -g -o - | %FileCheck %s
// RUN: %target-swiftc_driver %s -emit-ir -sdk "/Weird Location/SDK" -g -o - | %FileCheck --check-prefix CHECK-EXPLICIT %s
// Check that the sdk and resource dirs end up in the debug info if we pass the
// frontend flag. This tests the general functionality; we test the macosx
// specific toolchain logic in compiler-flags-macosx.swift.
// RUN: %target-swiftc_driver %s -emit-ir -debug-info-store-invocation -g -o - | %FileCheck %s
// RUN: %target-swiftc_driver %s -emit-ir -debug-info-store-invocation -sdk "/Weird Location/SDK" -g -o - | %FileCheck --check-prefix CHECK-EXPLICIT %s
// CHECK: !DICompileUnit({{.*}}producer: "{{(Apple )?Swift version [^"]+}}"
// CHECK-SAME: flags: "
// CHECK-NOT: "
Expand All @@ -13,6 +15,6 @@
// CHECK-EXPLICIT-SAME: -resource-dir

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