Today the front-ends (llvm-gcc and clang) generate multiple llvm.dbg.compile_units to identify source file for various debug entities. Each llvm.dbg.compile_unit matches one file on the disk. However, the backend only supports one DW_TAG_compile_unit per .o file. The backend selects first compile_unit from the vector to construct DW_TAG_compile_unit entry, which is not correct in all cases.

First step to resolve this is, record file name and directory directly in debug info for various debug entities. 



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61164 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/DebugInfo.cpp b/lib/Analysis/DebugInfo.cpp
index a49a3b3..f1f7d8e 100644
--- a/lib/Analysis/DebugInfo.cpp
+++ b/lib/Analysis/DebugInfo.cpp
@@ -195,7 +195,7 @@
 Constant *DIFactory::GetTagConstant(unsigned TAG) {
   assert((TAG & DIDescriptor::VersionMask) == 0 &&
          "Tag too large for debug encoding!");
-  return ConstantInt::get(Type::Int32Ty, TAG | DIDescriptor::Version6);
+  return ConstantInt::get(Type::Int32Ty, TAG | DIDescriptor::Version7);
 }
 
 Constant *DIFactory::GetStringConstant(const std::string &String) {
@@ -389,7 +389,9 @@
                                        uint64_t SizeInBits,
                                        uint64_t AlignInBits,
                                        uint64_t OffsetInBits, unsigned Flags,
-                                       unsigned Encoding) {
+                                       unsigned Encoding,
+                                       const std::string *FileName,
+                                       const std::string *Directory) {
   Constant *Elts[] = {
     GetTagConstant(dwarf::DW_TAG_base_type),
     getCastToEmpty(Context),
@@ -400,7 +402,9 @@
     ConstantInt::get(Type::Int64Ty, AlignInBits),
     ConstantInt::get(Type::Int64Ty, OffsetInBits),
     ConstantInt::get(Type::Int32Ty, Flags),
-    ConstantInt::get(Type::Int32Ty, Encoding)
+    ConstantInt::get(Type::Int32Ty, Encoding),
+    GetStringConstant(FileName ? FileName->c_str() : ""),
+    GetStringConstant(Directory ? Directory->c_str() : "")
   };
   
   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
@@ -424,7 +428,9 @@
                                            uint64_t AlignInBits,
                                            uint64_t OffsetInBits,
                                            unsigned Flags,
-                                           DIType DerivedFrom) {
+                                           DIType DerivedFrom,
+                                           const std::string *FileName,
+                                           const std::string *Directory) {
   Constant *Elts[] = {
     GetTagConstant(Tag),
     getCastToEmpty(Context),
@@ -435,7 +441,9 @@
     ConstantInt::get(Type::Int64Ty, AlignInBits),
     ConstantInt::get(Type::Int64Ty, OffsetInBits),
     ConstantInt::get(Type::Int32Ty, Flags),
-    getCastToEmpty(DerivedFrom)
+    getCastToEmpty(DerivedFrom),
+    GetStringConstant(FileName ? FileName->c_str() : ""),
+    GetStringConstant(Directory ? Directory->c_str() : "")
   };
   
   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
@@ -459,7 +467,10 @@
                                                uint64_t OffsetInBits,
                                                unsigned Flags,
                                                DIType DerivedFrom,
-                                               DIArray Elements) {
+                                               DIArray Elements,
+                                               const std::string *FileName,
+                                               const std::string *Directory) {
+
   Constant *Elts[] = {
     GetTagConstant(Tag),
     getCastToEmpty(Context),
@@ -471,7 +482,9 @@
     ConstantInt::get(Type::Int64Ty, OffsetInBits),
     ConstantInt::get(Type::Int32Ty, Flags),
     getCastToEmpty(DerivedFrom),
-    getCastToEmpty(Elements)
+    getCastToEmpty(Elements),
+    GetStringConstant(FileName ? FileName->c_str() : ""),
+    GetStringConstant(Directory ? Directory->c_str() : "")
   };
   
   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
@@ -495,7 +508,10 @@
                                          DICompileUnit CompileUnit,
                                          unsigned LineNo, DIType Type,
                                          bool isLocalToUnit,
-                                         bool isDefinition) {
+                                         bool isDefinition,
+                                         const std::string *FileName,
+                                         const std::string *Directory) {
+
   Constant *Elts[] = {
     GetTagConstant(dwarf::DW_TAG_subprogram),
     getCastToEmpty(GetOrCreateSubprogramAnchor()),
@@ -507,7 +523,9 @@
     ConstantInt::get(Type::Int32Ty, LineNo),
     getCastToEmpty(Type),
     ConstantInt::get(Type::Int1Ty, isLocalToUnit),
-    ConstantInt::get(Type::Int1Ty, isDefinition)
+    ConstantInt::get(Type::Int1Ty, isDefinition),
+    GetStringConstant(FileName ? FileName->c_str() : ""),
+    GetStringConstant(Directory ? Directory->c_str() : "")
   };
   
   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
@@ -527,8 +545,9 @@
                                 const std::string &LinkageName,
                                 DICompileUnit CompileUnit,
                                 unsigned LineNo, DIType Type,bool isLocalToUnit,
-                                bool isDefinition, llvm::GlobalVariable *Val) {
-  
+                                bool isDefinition, llvm::GlobalVariable *Val,
+                                const std::string *FileName,
+                                const std::string *Directory) {
   Constant *Elts[] = {
     GetTagConstant(dwarf::DW_TAG_variable),
     getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
@@ -541,7 +560,9 @@
     getCastToEmpty(Type),
     ConstantInt::get(Type::Int1Ty, isLocalToUnit),
     ConstantInt::get(Type::Int1Ty, isDefinition),
-    ConstantExpr::getBitCast(Val, EmptyStructPtr)
+    ConstantExpr::getBitCast(Val, EmptyStructPtr),
+    GetStringConstant(FileName ? FileName->c_str() : ""),
+    GetStringConstant(Directory ? Directory->c_str() : "")
   };
   
   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
@@ -559,7 +580,10 @@
 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
                                      const std::string &Name,
                                      DICompileUnit CompileUnit, unsigned LineNo,
-                                     DIType Type) {
+                                     DIType Type,
+                                     const std::string *FileName,
+                                     const std::string *Directory) {
+
   
   Constant *Elts[] = {
     GetTagConstant(Tag),
@@ -568,6 +592,8 @@
     getCastToEmpty(CompileUnit),
     ConstantInt::get(Type::Int32Ty, LineNo),
     getCastToEmpty(Type),
+    GetStringConstant(FileName ? FileName->c_str() : ""),
+    GetStringConstant(Directory ? Directory->c_str() : "")
   };
   
   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));