Add support for passing -main-file-name all the way through to
authorEric Christopher <echristo@gmail.com>
Tue, 18 Dec 2012 00:31:01 +0000 (00:31 +0000)
committerEric Christopher <echristo@gmail.com>
Tue, 18 Dec 2012 00:31:01 +0000 (00:31 +0000)
the assembler.

Part of PR14624

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170390 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCContext.h
include/llvm/Support/SourceMgr.h
lib/MC/MCContext.cpp
lib/MC/MCParser/AsmParser.cpp
test/DebugInfo/X86/lit.local.cfg
test/DebugInfo/X86/main-file-name.s [new file with mode: 0644]
tools/llvm-mc/llvm-mc.cpp

index 0da75cbda4d6580d1d932336cc0d91786d38c692..e92d3b9e71099f9d642d4837eaccdb3e7ee4f10d 100644 (file)
@@ -97,6 +97,9 @@ namespace llvm {
     /// The compilation directory to use for DW_AT_comp_dir.
     std::string CompilationDir;
 
+    /// The main file name if passed in explicitly.
+    std::string MainFileName;
+
     /// The dwarf file and directory tables from the dwarf .file directive.
     std::vector<MCDwarfFile *> MCDwarfFiles;
     std::vector<StringRef> MCDwarfDirs;
@@ -261,6 +264,14 @@ namespace llvm {
     /// Override the default (CWD) compilation directory.
     void setCompilationDir(StringRef S) { CompilationDir = S.str(); }
 
+    /// \brief Get the main file name for use in error messages and debug
+    /// info. This can be set to ensure we've got the correct file name
+    /// after preprocessing or for -save-temps.
+    const std::string &getMainFileName() const { return MainFileName; }
+
+    /// \brief Set the main file name and override the default.
+    void setMainFileName(StringRef S) { MainFileName = S.str(); }
+
     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
     unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
                           unsigned FileNumber);
index bc832e0c9e52e367b482711e99d9a98e5fb63d6e..aaee344755460568cfcb97bb7a9421853ea3c5f5 100644 (file)
@@ -95,6 +95,10 @@ public:
     return Buffers[i].Buffer;
   }
 
+  unsigned getNumBuffers() const {
+    return Buffers.size();
+  }
+
   SMLoc getParentIncludeLoc(unsigned i) const {
     assert(i < Buffers.size() && "Invalid Buffer ID!");
     return Buffers[i].IncludeLoc;
index 29727ac8a6a4a8bfa238e12fd8fa73364dc920d3..d206dd9f5041d1f63df1d1ff4de163ae10d1cc26 100644 (file)
@@ -21,6 +21,7 @@
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/Support/ELF.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/SourceMgr.h"
 using namespace llvm;
@@ -48,6 +49,11 @@ MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
   SecureLogFile = getenv("AS_SECURE_LOG_FILE");
   SecureLog = 0;
   SecureLogUsed = false;
+
+  if (SrcMgr && SrcMgr->getNumBuffers() > 0)
+    MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
+  else
+    MainFileName = "";
 }
 
 MCContext::~MCContext() {
index e1a1f89f423ae4170e106bb8b81569ef76076a50..85d31872a7394ac8a9b9b86cd7d2ff782cd2d4ca 100644 (file)
@@ -613,7 +613,8 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
     getStreamer().EmitLabel(SectionStartSym);
     getContext().setGenDwarfSectionStartSym(SectionStartSym);
     getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
-      StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
+                                         StringRef(),
+                                         getContext().getMainFileName());
   }
 
   // While we have input, parse each statement.
index 0d694da8df5a347241221f697122d9919784a1fb..60d66eae4953ccea985f060642e9023218905d1d 100644 (file)
@@ -1,4 +1,4 @@
-config.suffixes = ['.ll']
+config.suffixes = ['.ll', '.s']
 
 targets = set(config.root.targets_to_build.split())
 if not 'X86' in targets:
diff --git a/test/DebugInfo/X86/main-file-name.s b/test/DebugInfo/X86/main-file-name.s
new file mode 100644 (file)
index 0000000..6817c9e
--- /dev/null
@@ -0,0 +1,17 @@
+// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -main-file-name foo.S -g -o %t %s
+// RUN: llvm-dwarfdump %t | FileCheck %s
+
+// CHECK: DW_TAG_compile_unit [1]
+// CHECK-NOT: DW_TAG_
+// CHECK: DW_AT_name [DW_FORM_string]       ("foo.S")
+        
+
+# 1 "foo.S"
+# 1 "<built-in>" 1
+# 1 "foo.S" 2
+
+foo:
+  nop
+  nop
+  nop
+        
index c12a93d1716e65836a185cbb5e8e351ef7449c81..930f528668ff5570a90ca07808feed259f43ffc6 100644 (file)
@@ -161,6 +161,10 @@ static cl::opt<std::string>
 DebugCompilationDir("fdebug-compilation-dir",
                     cl::desc("Specifies the debug info's compilation dir"));
 
+static cl::opt<std::string>
+MainFileName("main-file-name",
+             cl::desc("Specifies the name we should consider the input file"));
+
 enum ActionType {
   AC_AsLex,
   AC_Assemble,
@@ -397,6 +401,8 @@ int main(int argc, char **argv) {
     Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
   if (!DebugCompilationDir.empty())
     Ctx.setCompilationDir(DebugCompilationDir);
+  if (!MainFileName.empty())
+    Ctx.setMainFileName(MainFileName);
 
   // Package up features to be passed to target/subtarget
   std::string FeaturesStr;