[PATCH] D42766: [DebugInfo] Support DWARFv5 source code embedding extension

2018-02-26 Thread Scott Linder via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL326102: [DebugInfo] Support DWARF v5 source code embedding 
extension (authored by scott.linder, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D42766?vs=135909=135918#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D42766

Files:
  cfe/trunk/docs/ClangCommandLineReference.rst
  cfe/trunk/include/clang/Driver/Options.td
  cfe/trunk/include/clang/Frontend/CodeGenOptions.def
  cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
  cfe/trunk/lib/CodeGen/CGDebugInfo.h
  cfe/trunk/lib/Driver/ToolChains/AMDGPU.h
  cfe/trunk/lib/Driver/ToolChains/Clang.cpp
  cfe/trunk/lib/Frontend/CompilerInvocation.cpp
  cfe/trunk/test/CodeGen/Inputs/debug-info-embed-source.c
  cfe/trunk/test/CodeGen/debug-info-embed-source.c
  cfe/trunk/test/Driver/amdgpu-toolchain.c
  cfe/trunk/test/Driver/debug-options.c

Index: cfe/trunk/test/CodeGen/Inputs/debug-info-embed-source.c
===
--- cfe/trunk/test/CodeGen/Inputs/debug-info-embed-source.c
+++ cfe/trunk/test/CodeGen/Inputs/debug-info-embed-source.c
@@ -0,0 +1 @@
+void foo() { }
Index: cfe/trunk/test/CodeGen/debug-info-embed-source.c
===
--- cfe/trunk/test/CodeGen/debug-info-embed-source.c
+++ cfe/trunk/test/CodeGen/debug-info-embed-source.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1-debug-info-kind=limited -emit-llvm %p/Inputs/debug-info-embed-source.c -o - | FileCheck %s --check-prefix=NOEMBED
+// RUN: %clang_cc1 -gembed-source -debug-info-kind=limited -emit-llvm %p/Inputs/debug-info-embed-source.c -o - | FileCheck %s --check-prefix=EMBED
+
+// NOEMBED-NOT: !DIFile({{.*}}source:
+// EMBED: !DIFile({{.*}}source: "void foo() { }\0A"
Index: cfe/trunk/test/Driver/debug-options.c
===
--- cfe/trunk/test/Driver/debug-options.c
+++ cfe/trunk/test/Driver/debug-options.c
@@ -245,3 +245,13 @@
 // RUN: %clang -###  %s 2>&1 | FileCheck -check-prefix=NOMACRO %s
 // MACRO: "-debug-info-macro"
 // NOMACRO-NOT: "-debug-info-macro"
+//
+// RUN: %clang -### -gdwarf-5 -gembed-source %s 2>&1 | FileCheck -check-prefix=GEMBED_5 %s
+// RUN: %clang -### -gdwarf-2 -gembed-source %s 2>&1 | FileCheck -check-prefix=GEMBED_2 %s
+// RUN: %clang -### -gdwarf-5 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_5 %s
+// RUN: %clang -### -gdwarf-2 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_2 %s
+//
+// GEMBED_5:  "-gembed-source"
+// GEMBED_2:  error: invalid argument '-gembed-source' only allowed with '-gdwarf-5'
+// NOGEMBED_5-NOT:  "-gembed-source"
+// NOGEMBED_2-NOT:  error: invalid argument '-gembed-source' only allowed with '-gdwarf-5'
Index: cfe/trunk/test/Driver/amdgpu-toolchain.c
===
--- cfe/trunk/test/Driver/amdgpu-toolchain.c
+++ cfe/trunk/test/Driver/amdgpu-toolchain.c
@@ -3,4 +3,4 @@
 // AS_LINK: ld.lld{{.*}} "-shared"
 
 // RUN: %clang -### -g -target amdgcn--amdhsa -mcpu=kaveri %s 2>&1 | FileCheck -check-prefix=DWARF_VER %s
-// DWARF_VER: "-dwarf-version=2"
+// DWARF_VER: "-dwarf-version=5"
Index: cfe/trunk/lib/CodeGen/CGDebugInfo.h
===
--- cfe/trunk/lib/CodeGen/CGDebugInfo.h
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.h
@@ -502,6 +502,9 @@
   Optional
   computeChecksum(FileID FID, SmallString<32> ) const;
 
+  /// Get the source of the given file ID.
+  Optional getSource(const SourceManager , FileID FID);
+
   /// Get the file debug info descriptor for the input location.
   llvm::DIFile *getOrCreateFile(SourceLocation Loc);
 
Index: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
===
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
@@ -385,6 +385,19 @@
   return llvm::DIFile::CSK_MD5;
 }
 
+Optional CGDebugInfo::getSource(const SourceManager , FileID FID) {
+  if (!CGM.getCodeGenOpts().EmbedSource)
+return None;
+
+  bool SourceInvalid = false;
+  StringRef Source = SM.getBufferData(FID, );
+
+  if (SourceInvalid)
+return None;
+
+  return Source;
+}
+
 llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
   if (!Loc.isValid())
 // If Location is not valid then use main input file.
@@ -416,16 +429,19 @@
 
   llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
 remapDIPath(getCurrentDirname()),
-CSInfo);
+CSInfo,
+getSource(SM, SM.getFileID(Loc)));
 
   DIFileCache[fname].reset(F);
   return F;
 }
 
 llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
-  return 

[PATCH] D42766: [DebugInfo] Support DWARFv5 source code embedding extension

2018-02-26 Thread Jonas Devlieghere via Phabricator via cfe-commits
JDevlieghere accepted this revision.
JDevlieghere added a comment.
This revision is now accepted and ready to land.

LGTM


https://reviews.llvm.org/D42766



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42766: [DebugInfo] Support DWARFv5 source code embedding extension

2018-02-26 Thread Scott Linder via Phabricator via cfe-commits
scott.linder updated this revision to Diff 135909.
scott.linder added a comment.

New diff addresses feedback. I committed the refactoring as NFC and rebased, 
then clarified the extension comment.


https://reviews.llvm.org/D42766

Files:
  docs/ClangCommandLineReference.rst
  include/clang/Driver/Options.td
  include/clang/Frontend/CodeGenOptions.def
  lib/CodeGen/CGDebugInfo.cpp
  lib/CodeGen/CGDebugInfo.h
  lib/Driver/ToolChains/AMDGPU.h
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/Inputs/debug-info-embed-source.c
  test/CodeGen/debug-info-embed-source.c
  test/Driver/amdgpu-toolchain.c
  test/Driver/debug-options.c

Index: test/Driver/debug-options.c
===
--- test/Driver/debug-options.c
+++ test/Driver/debug-options.c
@@ -245,3 +245,13 @@
 // RUN: %clang -###  %s 2>&1 | FileCheck -check-prefix=NOMACRO %s
 // MACRO: "-debug-info-macro"
 // NOMACRO-NOT: "-debug-info-macro"
+//
+// RUN: %clang -### -gdwarf-5 -gembed-source %s 2>&1 | FileCheck -check-prefix=GEMBED_5 %s
+// RUN: %clang -### -gdwarf-2 -gembed-source %s 2>&1 | FileCheck -check-prefix=GEMBED_2 %s
+// RUN: %clang -### -gdwarf-5 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_5 %s
+// RUN: %clang -### -gdwarf-2 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_2 %s
+//
+// GEMBED_5:  "-gembed-source"
+// GEMBED_2:  error: invalid argument '-gembed-source' only allowed with '-gdwarf-5'
+// NOGEMBED_5-NOT:  "-gembed-source"
+// NOGEMBED_2-NOT:  error: invalid argument '-gembed-source' only allowed with '-gdwarf-5'
Index: test/Driver/amdgpu-toolchain.c
===
--- test/Driver/amdgpu-toolchain.c
+++ test/Driver/amdgpu-toolchain.c
@@ -3,4 +3,4 @@
 // AS_LINK: ld.lld{{.*}} "-shared"
 
 // RUN: %clang -### -g -target amdgcn--amdhsa -mcpu=kaveri %s 2>&1 | FileCheck -check-prefix=DWARF_VER %s
-// DWARF_VER: "-dwarf-version=2"
+// DWARF_VER: "-dwarf-version=5"
Index: test/CodeGen/debug-info-embed-source.c
===
--- /dev/null
+++ test/CodeGen/debug-info-embed-source.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1-debug-info-kind=limited -emit-llvm %p/Inputs/debug-info-embed-source.c -o - | FileCheck %s --check-prefix=NOEMBED
+// RUN: %clang_cc1 -gembed-source -debug-info-kind=limited -emit-llvm %p/Inputs/debug-info-embed-source.c -o - | FileCheck %s --check-prefix=EMBED
+
+// NOEMBED-NOT: !DIFile({{.*}}source:
+// EMBED: !DIFile({{.*}}source: "void foo() { }\0A"
Index: test/CodeGen/Inputs/debug-info-embed-source.c
===
--- /dev/null
+++ test/CodeGen/Inputs/debug-info-embed-source.c
@@ -0,0 +1 @@
+void foo() { }
Index: lib/Frontend/CompilerInvocation.cpp
===
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -545,6 +545,7 @@
   Opts.DebugTypeExtRefs = Args.hasArg(OPT_dwarf_ext_refs);
   Opts.DebugExplicitImport = Args.hasArg(OPT_dwarf_explicit_import);
   Opts.DebugFwdTemplateParams = Args.hasArg(OPT_debug_forward_template_params);
+  Opts.EmbedSource = Args.hasArg(OPT_gembed_source);
 
   for (const auto  : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ))
 Opts.DebugPrefixMap.insert(StringRef(Arg).split('='));
Index: lib/Driver/ToolChains/Clang.cpp
===
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -3024,6 +3024,18 @@
   if (DebugInfoKind == codegenoptions::LimitedDebugInfo && NeedFullDebug)
 DebugInfoKind = codegenoptions::FullDebugInfo;
 
+  if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, false)) {
+// Source embedding is a vendor extension to DWARF v5. By now we have
+// checked if a DWARF version was stated explicitly, and have otherwise
+// fallen back to the target default, so if this is still not at least 5 we
+// emit an error.
+if (DWARFVersion < 5)
+  D.Diag(diag::err_drv_argument_only_allowed_with)
+  << Args.getLastArg(options::OPT_gembed_source)->getAsString(Args)
+  << "-gdwarf-5";
+CmdArgs.push_back("-gembed-source");
+  }
+
   RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
   DebuggerTuning);
 
Index: lib/Driver/ToolChains/AMDGPU.h
===
--- lib/Driver/ToolChains/AMDGPU.h
+++ lib/Driver/ToolChains/AMDGPU.h
@@ -56,7 +56,7 @@
 public:
   AMDGPUToolChain(const Driver , const llvm::Triple ,
   const llvm::opt::ArgList );
-  unsigned GetDefaultDwarfVersion() const override { return 2; }
+  unsigned GetDefaultDwarfVersion() const override { return 5; }
   bool IsIntegratedAssemblerDefault() const override { return true; }
   

[PATCH] D42766: [DebugInfo] Support DWARFv5 source code embedding extension

2018-02-23 Thread Paul Robinson via Phabricator via cfe-commits
probinson added a comment.

Looks generally straightforward.  I'd give other people a chance to chime in 
but I have only minor comments.




Comment at: lib/CodeGen/CGDebugInfo.cpp:411
 // If the location is not valid then use main input file.
-return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
-   remapDIPath(TheCU->getDirectory()),
-   TheCU->getFile()->getChecksumKind(),
-   TheCU->getFile()->getChecksum());
+return getOrCreateMainFile();
 

Looks like this simplification (replace two calls to createFile() with 
getOrCreateMainFile()) could be its own NFC commit and simplify this patch 
slightly.



Comment at: lib/Driver/ToolChains/Clang.cpp:3021
+  if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, 
false)) {
+// Source embedding is a DWARFv5 extension. By now we have checked if a
+// DWARF version was stated explicitly, and have otherwise fallen back to

Maybe "is an extension to DWARF v5" to clarify it's not actually part of DWARF 
v5.


Repository:
  rC Clang

https://reviews.llvm.org/D42766



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42766: [DebugInfo] Support DWARFv5 source code embedding extension

2018-02-23 Thread Scott Linder via Phabricator via cfe-commits
scott.linder added a comment.

With https://reviews.llvm.org/D42765 committed I think this is ready for review.


Repository:
  rC Clang

https://reviews.llvm.org/D42766



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42766: [DebugInfo] Support DWARFv5 source code embedding extension

2018-01-31 Thread Scott Linder via Phabricator via cfe-commits
scott.linder created this revision.
scott.linder added reviewers: echristo, arsenm, aprantl.
Herald added subscribers: cfe-commits, JDevlieghere, nhaehnle, wdng.

In DWARFv5 the Line Number Program Header is extensible, allowing values
with new content types. In this extension source is embedded directly
in the line tables of the DWARFv5 line debug section.

Add new flag (-g[no-]embed-source) to Driver and CC1 which indicates
that source should be passed through to LLVM during CodeGen.

Depends on LLVM review https://reviews.llvm.org/D42765


Repository:
  rC Clang

https://reviews.llvm.org/D42766

Files:
  docs/ClangCommandLineReference.rst
  include/clang/Driver/Options.td
  include/clang/Frontend/CodeGenOptions.def
  lib/CodeGen/CGDebugInfo.cpp
  lib/CodeGen/CGDebugInfo.h
  lib/Driver/ToolChains/AMDGPU.h
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/Inputs/debug-info-embed-source.c
  test/CodeGen/debug-info-embed-source.c
  test/Driver/amdgpu-toolchain.c
  test/Driver/debug-options.c

Index: test/Driver/debug-options.c
===
--- test/Driver/debug-options.c
+++ test/Driver/debug-options.c
@@ -245,3 +245,13 @@
 // RUN: %clang -###  %s 2>&1 | FileCheck -check-prefix=NOMACRO %s
 // MACRO: "-debug-info-macro"
 // NOMACRO-NOT: "-debug-info-macro"
+//
+// RUN: %clang -### -gdwarf-5 -gembed-source %s 2>&1 | FileCheck -check-prefix=GEMBED_5 %s
+// RUN: %clang -### -gdwarf-2 -gembed-source %s 2>&1 | FileCheck -check-prefix=GEMBED_2 %s
+// RUN: %clang -### -gdwarf-5 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_5 %s
+// RUN: %clang -### -gdwarf-2 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_2 %s
+//
+// GEMBED_5:  "-gembed-source"
+// GEMBED_2:  error: invalid argument '-gembed-source' only allowed with '-gdwarf-5'
+// NOGEMBED_5-NOT:  "-gembed-source"
+// NOGEMBED_2-NOT:  error: invalid argument '-gembed-source' only allowed with '-gdwarf-5'
Index: test/Driver/amdgpu-toolchain.c
===
--- test/Driver/amdgpu-toolchain.c
+++ test/Driver/amdgpu-toolchain.c
@@ -3,4 +3,4 @@
 // AS_LINK: ld.lld{{.*}} "-shared"
 
 // RUN: %clang -### -g -target amdgcn--amdhsa -mcpu=kaveri %s 2>&1 | FileCheck -check-prefix=DWARF_VER %s
-// DWARF_VER: "-dwarf-version=2"
+// DWARF_VER: "-dwarf-version=5"
Index: test/CodeGen/debug-info-embed-source.c
===
--- /dev/null
+++ test/CodeGen/debug-info-embed-source.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1-debug-info-kind=limited -emit-llvm %p/Inputs/debug-info-embed-source.c -o - | FileCheck %s --check-prefix=NOEMBED
+// RUN: %clang_cc1 -gembed-source -debug-info-kind=limited -emit-llvm %p/Inputs/debug-info-embed-source.c -o - | FileCheck %s --check-prefix=EMBED
+
+// NOEMBED-NOT: !DIFile({{.*}}source:
+// EMBED: !DIFile({{.*}}source: "void foo() { }\0A"
Index: test/CodeGen/Inputs/debug-info-embed-source.c
===
--- /dev/null
+++ test/CodeGen/Inputs/debug-info-embed-source.c
@@ -0,0 +1 @@
+void foo() { }
Index: lib/Frontend/CompilerInvocation.cpp
===
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -545,6 +545,7 @@
   Opts.DebugTypeExtRefs = Args.hasArg(OPT_dwarf_ext_refs);
   Opts.DebugExplicitImport = Args.hasArg(OPT_dwarf_explicit_import);
   Opts.DebugFwdTemplateParams = Args.hasArg(OPT_debug_forward_template_params);
+  Opts.EmbedSource = Args.hasArg(OPT_gembed_source);
 
   for (const auto  : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ))
 Opts.DebugPrefixMap.insert(StringRef(Arg).split('='));
Index: lib/Driver/ToolChains/Clang.cpp
===
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -3017,6 +3017,17 @@
   if (DebugInfoKind == codegenoptions::LimitedDebugInfo && NeedFullDebug)
 DebugInfoKind = codegenoptions::FullDebugInfo;
 
+  if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, false)) {
+// Source embedding is a DWARFv5 extension. By now we have checked if a
+// DWARF version was stated explicitly, and have otherwise fallen back to
+// the target default, so if this is still not at least 5 we emit an error.
+if (DWARFVersion < 5)
+  D.Diag(diag::err_drv_argument_only_allowed_with)
+  << Args.getLastArg(options::OPT_gembed_source)->getAsString(Args)
+  << "-gdwarf-5";
+CmdArgs.push_back("-gembed-source");
+  }
+
   RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
   DebuggerTuning);
 
Index: lib/Driver/ToolChains/AMDGPU.h
===
--- lib/Driver/ToolChains/AMDGPU.h
+++