r189649 - Fixing a bug where debug info for a local variable gets emitted at file scope.

Yunzhong Gao Yunzhong_Gao at playstation.sony.com
Thu Aug 29 22:37:03 PDT 2013


Author: ygao
Date: Fri Aug 30 00:37:02 2013
New Revision: 189649

URL: http://llvm.org/viewvc/llvm-project?rev=189649&view=rev
Log:
Fixing a bug where debug info for a local variable gets emitted at file scope.

The patch was discussed in Phabricator. See:
http://llvm-reviews.chandlerc.com/D1281


Added:
    cfe/trunk/test/CodeGen/debug-info-line5.cpp
    cfe/trunk/test/CodeGen/debug-info-line5.h
Modified:
    cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
    cfe/trunk/lib/CodeGen/CGDebugInfo.h
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/test/CodeGen/debug-info-scope.c
    cfe/trunk/test/CodeGenCXX/debug-info-namespace.cpp

Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Fri Aug 30 00:37:02 2013
@@ -3142,14 +3142,31 @@ CGDebugInfo::getOrCreateStaticDataMember
 }
 
 /// EmitGlobalVariable - Emit information about a global variable.
-void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
+/// \param VarOrInit either the global variable itself or the initializer
+/// \param D the global declaration
+void CGDebugInfo::EmitGlobalVariable(llvm::Value *VarOrInit,
                                      const VarDecl *D) {
   assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
   // Create global variable debug descriptor.
   llvm::DIFile Unit = getOrCreateFile(D->getLocation());
   unsigned LineNo = getLineNumber(D->getLocation());
+  StringRef DeclName = D->getName();
+  StringRef LinkageName;
+  bool IsLocalToUnit = true;
 
-  setLocation(D->getLocation());
+  // For deferred global variables, the current source location is usually
+  // where they are being referenced. Do not change the current source location
+  // to the place where they are declared, lest we get a bogus line table.
+  // FIXME: maybe we do not need to set the source location here at all.
+  if (llvm::GlobalVariable *Var = dyn_cast<llvm::GlobalVariable>(VarOrInit)) {
+    setLocation(D->getLocation());
+    IsLocalToUnit = Var->hasInternalLinkage();
+    if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
+        && !isa<ObjCMethodDecl>(D->getDeclContext()))
+      LinkageName = Var->getName();
+    if (LinkageName == DeclName)
+      LinkageName = StringRef();
+  }
 
   QualType T = D->getType();
   if (T->isIncompleteArrayType()) {
@@ -3161,18 +3178,11 @@ void CGDebugInfo::EmitGlobalVariable(llv
     T = CGM.getContext().getConstantArrayType(ET, ConstVal,
                                               ArrayType::Normal, 0);
   }
-  StringRef DeclName = D->getName();
-  StringRef LinkageName;
-  if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
-      && !isa<ObjCMethodDecl>(D->getDeclContext()))
-    LinkageName = Var->getName();
-  if (LinkageName == DeclName)
-    LinkageName = StringRef();
   llvm::DIDescriptor DContext =
     getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
   llvm::DIGlobalVariable GV = DBuilder.createStaticVariable(
       DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
-      Var->hasInternalLinkage(), Var,
+      IsLocalToUnit, VarOrInit,
       getOrCreateStaticDataMemberDeclarationOrNull(D));
   DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
 }
@@ -3203,26 +3213,16 @@ void CGDebugInfo::EmitGlobalVariable(llv
                                 Var->hasInternalLinkage(), Var);
 }
 
-/// EmitGlobalVariable - Emit global variable's debug info.
-void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
-                                     llvm::Constant *Init) {
+/// EmitEnumConstant - Emit debug info for an enumerator constant
+void CGDebugInfo::EmitEnumConstant(const EnumConstantDecl *ECD)
+{
   assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
-  // Create the descriptor for the variable.
-  llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
-  StringRef Name = VD->getName();
-  llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
-  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
-    const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
-    assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
-    Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
-  }
-  // Do not use DIGlobalVariable for enums.
-  if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
-    return;
-  llvm::DIGlobalVariable GV = DBuilder.createStaticVariable(
-      Unit, Name, Name, Unit, getLineNumber(VD->getLocation()), Ty, true, Init,
-      getOrCreateStaticDataMemberDeclarationOrNull(cast<VarDecl>(VD)));
-  DeclCache.insert(std::make_pair(VD->getCanonicalDecl(), llvm::WeakVH(GV)));
+  llvm::DIFile Unit = getOrCreateFile(ECD->getLocation());
+  llvm::DIType Ty = getOrCreateType(ECD->getType(), Unit);
+
+  const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
+  assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
+  Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
 }
 
 llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {

Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.h?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.h (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.h Fri Aug 30 00:37:02 2013
@@ -264,13 +264,13 @@ public:
                                             CGBuilderTy &Builder);
 
   /// EmitGlobalVariable - Emit information about a global variable.
-  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
+  void EmitGlobalVariable(llvm::Value *VarOrInit, const VarDecl *Decl);
 
   /// EmitGlobalVariable - Emit information about an objective-c interface.
   void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
 
-  /// EmitGlobalVariable - Emit global variable's debug info.
-  void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
+  /// EmitEnumConstant - Emit information about an enumerator constant
+  void EmitEnumConstant(const EnumConstantDecl *ECD);
 
   /// \brief - Emit C++ using directive.
   void EmitUsingDirective(const UsingDirectiveDecl &UD);

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Fri Aug 30 00:37:02 2013
@@ -967,15 +967,9 @@ CodeGenFunction::tryEmitAsConstant(DeclR
   // Emit as a constant.
   llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this);
 
-  // Make sure we emit a debug reference to the global variable.
-  // This should probably fire even for
-  if (isa<VarDecl>(value)) {
-    if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
-      EmitDeclRefExprDbgValue(refExpr, C);
-  } else {
-    assert(isa<EnumConstantDecl>(value));
-    EmitDeclRefExprDbgValue(refExpr, C);
-  }
+  // Make sure we emit a debug reference to the global variable or
+  // enumerator constant.
+  EmitValueDeclDbgValue(value, C);
 
   // If we emitted a reference constant, we need to dereference that.
   if (resultIsReference)

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Fri Aug 30 00:37:02 2013
@@ -1376,12 +1376,27 @@ llvm::Value* CodeGenFunction::EmitVAList
   return EmitLValue(E).getAddress();
 }
 
-void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
-                                              llvm::Constant *Init) {
+void CodeGenFunction::EmitValueDeclDbgValue(const ValueDecl *Val,
+                                            llvm::Constant *Init) {
   assert (Init && "Invalid DeclRefExpr initializer!");
-  if (CGDebugInfo *Dbg = getDebugInfo())
-    if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
-      Dbg->EmitGlobalVariable(E->getDecl(), Init);
+  CGDebugInfo *Dbg = getDebugInfo();
+  if (!Dbg ||
+      CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
+    return;
+
+  // Make sure we emit a debug reference to the global variable.
+  if (const VarDecl *VD = dyn_cast<VarDecl>(Val)) {
+    // Do not duplicate DIE entry for local variables; they are not deferred
+    // like global variables are.
+    if (VD->isFileVarDecl() && !getLangOpts().EmitAllDecls &&
+        !getContext().DeclMustBeEmitted(Val))
+      Dbg->EmitGlobalVariable(Init, VD);
+
+  // Make sure we emit a debug reference to an enumerator constant.
+  } else {
+    assert(isa<EnumConstantDecl>(Val));
+    Dbg->EmitEnumConstant(dyn_cast<EnumConstantDecl>(Val));
+  }
 }
 
 CodeGenFunction::PeepholeProtection

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Aug 30 00:37:02 2013
@@ -2047,7 +2047,8 @@ public:
   LValue EmitStmtExprLValue(const StmtExpr *E);
   LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
   LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
-  void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
+  void   EmitValueDeclDbgValue(const ValueDecl *Val, llvm::Constant *Init);
+
 
   //===--------------------------------------------------------------------===//
   //                         Scalar Expression Emission

Added: cfe/trunk/test/CodeGen/debug-info-line5.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-line5.cpp?rev=189649&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/debug-info-line5.cpp (added)
+++ cfe/trunk/test/CodeGen/debug-info-line5.cpp Fri Aug 30 00:37:02 2013
@@ -0,0 +1,18 @@
+// RUN: %clang %s -g -gcolumn-info -S -emit-llvm -o - | FileCheck %s
+// Line table entries should reference this cpp file, not the header
+
+#include "debug-info-line5.h"
+
+int result;
+int foo(unsigned);
+
+int main()
+{
+  while ( 1 )
+  {
+    result = foo(Marker);
+  }
+  return result;
+}
+
+// CHECK: !{{[0-9]*}} = metadata !{i32 {{[0-9]*}}, i32 {{[0-9]*}}, null, metadata !"Marker", {{.*}} ; [ DW_TAG_variable ] [Marker]

Added: cfe/trunk/test/CodeGen/debug-info-line5.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-line5.h?rev=189649&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/debug-info-line5.h (added)
+++ cfe/trunk/test/CodeGen/debug-info-line5.h Fri Aug 30 00:37:02 2013
@@ -0,0 +1,2 @@
+// Declaration of "Marker"
+const unsigned Marker = 0xFF999999;

Modified: cfe/trunk/test/CodeGen/debug-info-scope.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-scope.c?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/debug-info-scope.c (original)
+++ cfe/trunk/test/CodeGen/debug-info-scope.c Fri Aug 30 00:37:02 2013
@@ -1,7 +1,16 @@
 // RUN: %clang_cc1 -g -emit-llvm < %s | FileCheck %s
+
+// Make sure that the debug info of the local variable d does not shadow
+// the global variable d
+// CHECK: [ DW_TAG_variable ] [d] [line 6] [def]
+const int d = 100;
+
 // Two variables with same name in separate scope.
 // Radar 8330217.
 int main() {
+// CHECK-NOT: [ DW_TAG_variable ] [d] [line 13]
+// CHECK: [ DW_TAG_auto_variable ] [d] [line 13]
+	const int d = 4;
 	int j = 0;
 	int k = 0;
 // CHECK: DW_TAG_auto_variable ] [i]
@@ -12,5 +21,5 @@ int main() {
 // CHECK-NEXT: DW_TAG_lexical_block
 	for (int i = 0; i < 10; i++)
 		k++;
-	return 0;
+	return d; // make a reference to d so that its debug info gets included
 }

Modified: cfe/trunk/test/CodeGenCXX/debug-info-namespace.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-namespace.cpp?rev=189649&r1=189648&r2=189649&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/debug-info-namespace.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/debug-info-namespace.cpp Fri Aug 30 00:37:02 2013
@@ -39,6 +39,16 @@ int func(bool b) {
 // This should work even if 'i' and 'func' were declarations & not definitions,
 // but it doesn't yet.
 
+namespace C
+{
+ const int j = 32;
+}
+
+int func2()
+{
+ return C::j;
+} 
+
 // CHECK: [[CU:![0-9]*]] = {{.*}}[[MODULES:![0-9]*]], metadata !""} ; [ DW_TAG_compile_unit ]
 // CHECK: [[FILE:![0-9]*]] {{.*}}debug-info-namespace.cpp"
 // CHECK: [[FOO:![0-9]*]] {{.*}} ; [ DW_TAG_structure_type ] [foo] [line 5, size 0, align 0, offset 0] [decl] [from ]
@@ -50,6 +60,8 @@ int func(bool b) {
 // CHECK: [[FUNC:![0-9]*]] {{.*}} ; [ DW_TAG_subprogram ] {{.*}} [def] [func]
 // CHECK: [[FILE2]]} ; [ DW_TAG_file_type ] [{{.*}}foo.cpp]
 // CHECK: [[I:![0-9]*]] = {{.*}}, metadata [[NS]], metadata !"i", {{.*}} ; [ DW_TAG_variable ] [i]
+// CHECK: [[VAR:![0-9]*]] = {{.*}}, metadata [[SPACE:![0-9]*]], metadata !"j", {{.*}} ; [ DW_TAG_variable ] [j]
+// CHECK: [[SPACE]] = {{.*}}, metadata !"C", {{.*}} ; [ DW_TAG_namespace ] [C]
 // CHECK: [[MODULES]] = metadata !{metadata [[M1:![0-9]*]], metadata [[M2:![0-9]*]], metadata [[M3:![0-9]*]], metadata [[M4:![0-9]*]], metadata [[M5:![0-9]*]], metadata [[M6:![0-9]*]], metadata [[M7:![0-9]*]], metadata [[M8:![0-9]*]], metadata [[M9:![0-9]*]], metadata [[M10:![0-9]*]], metadata [[M11:![0-9]*]], metadata [[M12:![0-9]*]]}
 // CHECK: [[M1]] = metadata !{i32 {{[0-9]*}}, metadata [[CTXT]], metadata [[NS]], i32 11} ; [ DW_TAG_imported_module ]
 // CHECK: [[M2]] = metadata !{i32 {{[0-9]*}}, metadata [[CU]], metadata [[CTXT]], i32 {{[0-9]*}}} ; [ DW_TAG_imported_module ]





More information about the cfe-commits mailing list