[PATCH] D44265: [DWARF] Fix mixing -g with DWARF .file directives

Paul Robinson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 8 10:55:26 PST 2018


probinson created this revision.
probinson added a reviewer: dblaikie.
probinson added a project: debug-info.
Herald added subscribers: JDevlieghere, hiraditya.

This combination was emitting the assembler source filename as file 1,
instead of the filename specified by the '.file 1' directive.

Fixes PR36636.


https://reviews.llvm.org/D44265

Files:
  llvm/lib/MC/MCParser/AsmParser.cpp
  llvm/test/MC/AsmParser/directive_file-2.s


Index: llvm/test/MC/AsmParser/directive_file-2.s
===================================================================
--- llvm/test/MC/AsmParser/directive_file-2.s
+++ llvm/test/MC/AsmParser/directive_file-2.s
@@ -1,4 +1,4 @@
-// RUN: llvm-mc -triple i386-unknown-unknown %s | FileCheck %s
+// RUN: llvm-mc -g -triple i386-unknown-unknown %s | FileCheck %s
 // Test for Bug 11740
 // This testcase has two directive files,
 // when compiled with -g, this testcase will not report error,
Index: llvm/lib/MC/MCParser/AsmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmParser.cpp
+++ llvm/lib/MC/MCParser/AsmParser.cpp
@@ -311,6 +311,9 @@
   }
   static void DiagHandler(const SMDiagnostic &Diag, void *Context);
 
+  /// Should we emit DWARF describing this assembler source?
+  bool enabledGenDwarfForAssembly();
+
   /// \brief Enter the specified file. This returns true on failure.
   bool enterIncludeFile(const std::string &Filename);
 
@@ -824,6 +827,15 @@
   return *tok;
 }
 
+bool AsmParser::enabledGenDwarfForAssembly() {
+  if (!getContext().getGenDwarfForAssembly())
+    return false;
+  if (getContext().getGenDwarfFileNumber() == 0)
+    getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
+        0, StringRef(), getContext().getMainFileName()));
+  return true;
+}
+
 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
   // Create the initial section, if requested.
   if (!NoInitialTextSection)
@@ -837,7 +849,7 @@
   SmallVector<AsmRewrite, 4> AsmStrRewrites;
 
   // If we are generating dwarf for assembly source files save the initial text
-  // section and generate a .file directive.
+  // section.
   if (getContext().getGenDwarfForAssembly()) {
     MCSection *Sec = getStreamer().getCurrentSectionOnly();
     if (!Sec->getBeginSymbol()) {
@@ -848,8 +860,6 @@
     bool InsertResult = getContext().addGenDwarfSection(Sec);
     assert(InsertResult && ".text section should not have debug info yet");
     (void)InsertResult;
-    getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
-        0, StringRef(), getContext().getMainFileName()));
   }
 
   // While we have input, parse each statement.
@@ -1784,7 +1794,7 @@
 
     // If we are generating dwarf for assembly source files then gather the
     // info to make a dwarf label entry for this label if needed.
-    if (getContext().getGenDwarfForAssembly())
+    if (enabledGenDwarfForAssembly())
       MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
                                  IDLoc);
 
@@ -2153,7 +2163,7 @@
 
   // If we are generating dwarf for the current section then generate a .loc
   // directive for the instruction.
-  if (!ParseHadError && getContext().getGenDwarfForAssembly() &&
+  if (!ParseHadError && enabledGenDwarfForAssembly() &&
       getContext().getGenDwarfSectionSyms().count(
           getStreamer().getCurrentSectionOnly())) {
     unsigned Line;
@@ -3336,15 +3346,12 @@
     }
     // If there is -g option as well as debug info from directive file,
     // we turn off -g option, directly use the existing debug info instead.
-    if (getContext().getGenDwarfForAssembly())
-      getContext().setGenDwarfForAssembly(false);
-    else {
-      Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
-          FileNumber, Directory, Filename, CKMem, Source);
-      if (!FileNumOrErr)
-        return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
-      FileNumber = FileNumOrErr.get();
-    }
+    getContext().setGenDwarfForAssembly(false);
+    Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
+        FileNumber, Directory, Filename, CKMem, Source);
+    if (!FileNumOrErr)
+      return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
+    FileNumber = FileNumOrErr.get();
   }
 
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44265.137616.patch
Type: text/x-patch
Size: 3925 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180308/11447767/attachment.bin>


More information about the llvm-commits mailing list