Prepare LLVM to fix PR14625, exposing a hook in MCContext to manage the
authorChandler Carruth <chandlerc@gmail.com>
Mon, 17 Dec 2012 21:32:42 +0000 (21:32 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Mon, 17 Dec 2012 21:32:42 +0000 (21:32 +0000)
compilation directory.

This defaults to the current working directory, just as it always has,
but now an assembler can choose to override it with a custom directory.
I've taught llvm-mc about this option and added a test case.

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

include/llvm/MC/MCContext.h
lib/MC/MCContext.cpp
lib/MC/MCDwarf.cpp
test/MC/ELF/comp-dir.s [new file with mode: 0644]
tools/llvm-mc/llvm-mc.cpp

index ce22325e856f4040d6ecb1518b73b1e60cebf1b7..0da75cbda4d6580d1d932336cc0d91786d38c692 100644 (file)
@@ -94,6 +94,9 @@ namespace llvm {
     /// .secure_log_reset appearing between them.
     bool SecureLogUsed;
 
+    /// The compilation directory to use for DW_AT_comp_dir.
+    std::string CompilationDir;
+
     /// The dwarf file and directory tables from the dwarf .file directive.
     std::vector<MCDwarfFile *> MCDwarfFiles;
     std::vector<StringRef> MCDwarfDirs;
@@ -248,6 +251,16 @@ namespace llvm {
     /// @name Dwarf Management
     /// @{
 
+    /// \brief Get the compilation directory for DW_AT_comp_dir
+    /// This can be overridden by clients which want to control the reported
+    /// compilation directory and have it be something other than the current
+    /// working directory.
+    const std::string &getCompilationDir() const { return CompilationDir; }
+
+    /// \brief Set the compilation directory for DW_AT_comp_dir
+    /// Override the default (CWD) compilation directory.
+    void setCompilationDir(StringRef S) { CompilationDir = S.str(); }
+
     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
     unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
                           unsigned FileNumber);
index 19ff49cd8f789fcfc7657db5d8f08896eed8d2c6..29727ac8a6a4a8bfa238e12fd8fa73364dc920d3 100644 (file)
@@ -35,7 +35,8 @@ MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
                      bool DoAutoReset ) :
   SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
   Allocator(), Symbols(Allocator), UsedNames(Allocator),
-  NextUniqueID(0), 
+  NextUniqueID(0),
+  CompilationDir(llvm::sys::Path::GetCurrentDirectory().str()),
   CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0), 
   DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0),
   AllowTemporaryLabels(true), AutoReset(DoAutoReset) {
index 597ee1d691c59678cc217d31e84ab6336b68064a..d53d2fc0b7223e0f146f97275e5fb3db11384677 100644 (file)
@@ -627,8 +627,7 @@ static void EmitGenDwarfInfo(MCStreamer *MCOS,
   MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
 
   // AT_comp_dir, the working directory the assembly was done in.
-  llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
-  MCOS->EmitBytes(StringRef(CWD.c_str()), 0);
+  MCOS->EmitBytes(context.getCompilationDir(), 0);
   MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
 
   // AT_APPLE_flags, the command line arguments of the assembler tool.
diff --git a/test/MC/ELF/comp-dir.s b/test/MC/ELF/comp-dir.s
new file mode 100644 (file)
index 0000000..2c7de4c
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: llvm-mc -g -fdebug-compilation-dir=/test/comp/dir %s -filetype=obj -o %t.o
+// RUN: llvm-dwarfdump %t.o | FileCheck %s
+
+// CHECK: DW_AT_comp_dir [DW_FORM_string] ("/test/comp/dir")
+
+f:
+  nop
index 15cacfabeb573b77d242e174e20d101d93b9738c..c12a93d1716e65836a185cbb5e8e351ef7449c81 100644 (file)
@@ -157,6 +157,10 @@ static cl::opt<bool>
 GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
                                   "source files"));
 
+static cl::opt<std::string>
+DebugCompilationDir("fdebug-compilation-dir",
+                    cl::desc("Specifies the debug info's compilation dir"));
+
 enum ActionType {
   AC_AsLex,
   AC_Assemble,
@@ -389,8 +393,10 @@ int main(int argc, char **argv) {
     Ctx.setAllowTemporaryLabels(false);
 
   Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
-  if (!DwarfDebugFlags.empty()) 
+  if (!DwarfDebugFlags.empty())
     Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
+  if (!DebugCompilationDir.empty())
+    Ctx.setCompilationDir(DebugCompilationDir);
 
   // Package up features to be passed to target/subtarget
   std::string FeaturesStr;