r241154 - [DebugInfo] Fix debug info generation for function static variables, typedefs, and records

Michael Kuperstein michael.m.kuperstein at intel.com
Wed Jul 1 05:34:39 PDT 2015


Author: mkuper
Date: Wed Jul  1 07:34:39 2015
New Revision: 241154

URL: http://llvm.org/viewvc/llvm-project?rev=241154&view=rev
Log:
[DebugInfo] Fix debug info generation for function static variables, typedefs, and records

Function static variables, typedefs and records (class, struct or union) declared inside
a lexical scope were associated with the function as their parent scope, rather than the
lexical scope they are defined or declared in.

This fixes PR19238

Patch by: amjad.aboud at intel.com
Differential Revision: http://reviews.llvm.org/D9760

Added:
    cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp   (with props)
    cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp   (with props)
    cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp   (with props)
    cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp   (with props)
Modified:
    cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
    cfe/trunk/lib/CodeGen/CGDebugInfo.h
    cfe/trunk/lib/CodeGen/CGDecl.cpp

Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=241154&r1=241153&r2=241154&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Wed Jul  1 07:34:39 2015
@@ -786,15 +786,18 @@ llvm::DIType *CGDebugInfo::CreateType(co
 
 llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
                                       llvm::DIFile *Unit) {
+  TypedefNameDecl *TD = Ty->getDecl();
   // We don't set size information, but do specify where the typedef was
   // declared.
-  SourceLocation Loc = Ty->getDecl()->getLocation();
+  SourceLocation Loc = TD->getLocation();
+
+  llvm::DIScope *TDContext = getDeclarationLexicalScope(*TD, QualType(Ty, 0));
 
   // Typedefs are derived from some other type.
   return DBuilder.createTypedef(
       getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
       Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
-      getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
+      TDContext);
 }
 
 llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
@@ -1442,6 +1445,23 @@ llvm::DIType *CGDebugInfo::getOrCreateIn
   return T;
 }
 
+void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) {
+  assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() &&
+         "D is already mapped to lexical block scope");
+  if (!LexicalBlockStack.empty())
+    LexicalBlockMap[&D] = LexicalBlockStack.back();
+}
+
+llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl &D,
+                                                       QualType Ty) {
+  auto I = LexicalBlockMap.find(&D);
+  if (I != LexicalBlockMap.end()) {
+    RetainedTypes.push_back(Ty.getAsOpaquePtr());
+    return I->second;
+  } else
+    return getContextDescriptor(cast<Decl>(D.getDeclContext()));
+}
+
 void CGDebugInfo::completeType(const EnumDecl *ED) {
   if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
     return;
@@ -2269,8 +2289,7 @@ llvm::DICompositeType *CGDebugInfo::Crea
   unsigned Line = getLineNumber(RD->getLocation());
   StringRef RDName = getClassName(RD);
 
-  llvm::DIScope *RDContext =
-      getContextDescriptor(cast<Decl>(RD->getDeclContext()));
+  llvm::DIScope *RDContext = getDeclarationLexicalScope(*RD, QualType(Ty, 0));
 
   // If we ended up creating the type during the context chain construction,
   // just return that.
@@ -2416,7 +2435,14 @@ void CGDebugInfo::collectVarDeclProps(co
   // outside the class by putting it in the global scope.
   if (DC->isRecord())
     DC = CGM.getContext().getTranslationUnitDecl();
-  VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
+
+  if (VD->isStaticLocal()) {
+    // Get context for static locals (that are technically globals) the same way
+    // we do for "local" locals -- by using current lexical block.
+    assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
+    VDContext = LexicalBlockStack.back();
+  } else
+    VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
 }
 
 llvm::DISubprogram *

Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.h?rev=241154&r1=241153&r2=241154&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.h (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.h Wed Jul  1 07:34:39 2015
@@ -100,6 +100,11 @@ class CGDebugInfo {
 
   // LexicalBlockStack - Keep track of our current nested lexical block.
   std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
+
+  /// \brief Map of AST declaration to its lexical block scope.
+  llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIScope>>
+      LexicalBlockMap;
+
   llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
   // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
   // beginning of a function. This is used to pop unbalanced regions at
@@ -305,6 +310,12 @@ public:
   /// debug info.
   llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
 
+  /// \brief Map AST declaration to its lexical block scope if available.
+  void recordDeclarationLexicalScope(const Decl &D);
+
+  /// \brief Get lexical scope of AST declaration.
+  llvm::DIScope *getDeclarationLexicalScope(const Decl &D, QualType Ty);
+
   void completeType(const EnumDecl *ED);
   void completeType(const RecordDecl *RD);
   void completeRequiredType(const RecordDecl *RD);

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=241154&r1=241153&r2=241154&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Wed Jul  1 07:34:39 2015
@@ -81,10 +81,8 @@ void CodeGenFunction::EmitDecl(const Dec
   case Decl::UsingShadow:
     llvm_unreachable("Declaration should not be in declstmts!");
   case Decl::Function:  // void X();
-  case Decl::Record:    // struct/union/class X;
   case Decl::Enum:      // enum X;
   case Decl::EnumConstant: // enum ? { X = ? }
-  case Decl::CXXRecord: // struct/union/class X; [C++]
   case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
   case Decl::Label:        // __label__ x;
   case Decl::Import:
@@ -93,6 +91,12 @@ void CodeGenFunction::EmitDecl(const Dec
     // None of these decls require codegen support.
     return;
 
+  case Decl::CXXRecord: // struct/union/class X; [C++]
+  case Decl::Record:    // struct/union/class X;
+    if (CGDebugInfo *DI = getDebugInfo())
+      DI->recordDeclarationLexicalScope(D);
+    return;
+
   case Decl::NamespaceAlias:
     if (CGDebugInfo *DI = getDebugInfo())
         DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
@@ -117,6 +121,9 @@ void CodeGenFunction::EmitDecl(const Dec
     const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
     QualType Ty = TD.getUnderlyingType();
 
+    if (CGDebugInfo *DI = getDebugInfo())
+      DI->recordDeclarationLexicalScope(D);
+
     if (Ty->isVariablyModifiedType())
       EmitVariablyModifiedType(Ty);
   }

Added: cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp?rev=241154&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp Wed Jul  1 07:34:39 2015
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int foo(int x) {
+  if(x)
+  {
+    class X {
+    public:
+      char z;
+      X(int y) : z(y) {}
+    };
+    {
+      X a(x);
+      return a.z;
+    }
+  }
+  return 0;
+}
+
+// CHECK: !{{[0-9]+}} = !DICompositeType(tag: DW_TAG_class_type, name: "X", scope: [[LBScope:![0-9]+]],
+// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
+

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-class.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp?rev=241154&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp Wed Jul  1 07:34:39 2015
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int foo(int x) {
+  if(x)
+  {
+    static int bar = 0;
+    {
+      int a = bar++;
+      return a;
+    }
+  }
+  return 0;
+}
+
+// CHECK: !{{[0-9]+}} = !DIGlobalVariable(name: "bar", scope: [[LBScope:![0-9]+]],
+// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
+

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-static.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp?rev=241154&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp Wed Jul  1 07:34:39 2015
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int main() {
+  static int X = 10;
+  {
+    static bool X = false;
+    return (int) X;
+  }
+  return X;
+}
+
+// CHECK: [[FuncScope:![0-9]+]] = !DISubprogram(name: "main",
+// CHECK: !{{[0-9]+}} = !DIGlobalVariable(name: "X", scope: [[FuncScope:![0-9]+]],
+// CHECK: !{{[0-9]+}} = !DIGlobalVariable(name: "X", scope: [[LBScope:![0-9]+]],
+// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
+

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-static2.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp?rev=241154&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp Wed Jul  1 07:34:39 2015
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int foo(int x) {
+  if(x) 
+  {
+    typedef char X;
+    {
+      X a = x;
+      return a;
+    }
+  }
+  return 0;
+}
+
+// CHECK: !{{[0-9]+}} = !DIDerivedType(tag: DW_TAG_typedef, name: "X", scope: [[LBScope:![0-9]+]],
+// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
+

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: cfe/trunk/test/CodeGenCXX/debug-info-lb-typedef.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list