<div dir="ltr">Heads-up: I see segmentation faults in Clang following this revision. I will try to come up with a standalone reproducer and update <a href="https://llvm.org/bugs/show_bug.cgi?id=26942">https://llvm.org/bugs/show_bug.cgi?id=26942</a>, possibly reverting this change.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 24, 2016 at 6:30 AM, Amjad Aboud via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: aaboud<br>
Date: Thu Mar 24 08:30:41 2016<br>
New Revision: 264281<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=264281&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=264281&view=rev</a><br>
Log:<br>
Recommitted r263425 "Supporting all entities declared in lexical scope in LLVM debug info."<br>
After fixing PR26942 (the fix is included in this commit).<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D18350" rel="noreferrer" target="_blank">http://reviews.llvm.org/D18350</a><br>
<br>
Added:<br>
    cfe/trunk/test/CodeGenCXX/debug-info-lb.cpp<br>
      - copied unchanged from r263435, cfe/trunk/test/CodeGenCXX/debug-info-lb.cpp<br>
Modified:<br>
    cfe/trunk/lib/CodeGen/CGDebugInfo.cpp<br>
    cfe/trunk/lib/CodeGen/CGDebugInfo.h<br>
    cfe/trunk/lib/CodeGen/CGDecl.cpp<br>
    cfe/trunk/test/CodeGenCXX/debug-info-anon-union-vars.cpp<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=264281&r1=264280&r2=264281&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=264281&r1=264280&r2=264281&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Thu Mar 24 08:30:41 2016<br>
@@ -831,15 +831,18 @@ llvm::DIType *CGDebugInfo::CreateType(co<br>
<br>
 llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,<br>
                                       llvm::DIFile *Unit) {<br>
+  TypedefNameDecl *TD = Ty->getDecl();<br>
   // We don't set size information, but do specify where the typedef was<br>
   // declared.<br>
-  SourceLocation Loc = Ty->getDecl()->getLocation();<br>
+  SourceLocation Loc = TD->getLocation();<br>
+<br>
+  llvm::DIScope *TDContext = getDeclarationLexicalScope(*TD, QualType(Ty, 0));<br>
<br>
   // Typedefs are derived from some other type.<br>
   return DBuilder.createTypedef(<br>
       getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),<br>
       Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),<br>
-      getDeclContextDescriptor(Ty->getDecl()));<br>
+      TDContext);<br>
 }<br>
<br>
 llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,<br>
@@ -1472,6 +1475,23 @@ llvm::DIType *CGDebugInfo::getOrCreateSt<br>
   return T;<br>
 }<br>
<br>
+void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) {<br>
+  assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() &&<br>
+         "D is already mapped to lexical block scope");<br>
+  if (!LexicalBlockStack.empty())<br>
+    LexicalBlockMap[&D] = LexicalBlockStack.back();<br>
+}<br>
+<br>
+llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl &D,<br>
+                                                       QualType Ty) {<br>
+  auto I = LexicalBlockMap.find(&D);<br>
+  if (I != LexicalBlockMap.end()) {<br>
+    RetainedTypes.push_back(Ty.getAsOpaquePtr());<br>
+    return I->second;<br>
+  }<br>
+  return getDeclContextDescriptor(cast<Decl>(&D));<br>
+}<br>
+<br>
 void CGDebugInfo::completeType(const EnumDecl *ED) {<br>
   if (DebugKind <= codegenoptions::DebugLineTablesOnly)<br>
     return;<br>
@@ -2057,7 +2077,7 @@ llvm::DIType *CGDebugInfo::CreateEnumTyp<br>
     // entered into the ReplaceMap: finalize() will replace the first<br>
     // FwdDecl with the second and then replace the second with<br>
     // complete type.<br>
-    llvm::DIScope *EDContext = getDeclContextDescriptor(ED);<br>
+    llvm::DIScope *EDContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0));<br>
     llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());<br>
     llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(<br>
         llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));<br>
@@ -2101,7 +2121,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDef<br>
<br>
   llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());<br>
   unsigned Line = getLineNumber(ED->getLocation());<br>
-  llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);<br>
+  llvm::DIScope *EnumContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0));<br>
   llvm::DIType *ClassTy =<br>
       ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;<br>
   return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,<br>
@@ -2362,7 +2382,7 @@ llvm::DICompositeType *CGDebugInfo::Crea<br>
   unsigned Line = getLineNumber(RD->getLocation());<br>
   StringRef RDName = getClassName(RD);<br>
<br>
-  llvm::DIScope *RDContext = getDeclContextDescriptor(RD);<br>
+  llvm::DIScope *RDContext = getDeclarationLexicalScope(*RD, QualType(Ty, 0));<br>
<br>
   // If we ended up creating the type during the context chain construction,<br>
   // just return that.<br>
@@ -2509,8 +2529,15 @@ void CGDebugInfo::collectVarDeclProps(co<br>
   if (DC->isRecord())<br>
     DC = CGM.getContext().getTranslationUnitDecl();<br>
<br>
- llvm::DIScope *Mod = getParentModuleOrNull(VD);<br>
- VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);<br>
+  if (VD->isStaticLocal()) {<br>
+    // Get context for static locals (that are technically globals) the same way<br>
+    // we do for "local" locals -- by using current lexical block.<br>
+    assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");<br>
+    VDContext = LexicalBlockStack.back();<br>
+  } else {<br>
+    llvm::DIScope *Mod = getParentModuleOrNull(VD);<br>
+    VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);<br>
+  }<br>
 }<br>
<br>
 llvm::DISubprogram *<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.h?rev=264281&r1=264280&r2=264281&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.h?rev=264281&r1=264280&r2=264281&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGDebugInfo.h (original)<br>
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.h Thu Mar 24 08:30:41 2016<br>
@@ -116,6 +116,11 @@ class CGDebugInfo {<br>
<br>
   /// Keep track of our current nested lexical block.<br>
   std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;<br>
+<br>
+  /// Map of AST declaration to its lexical block scope.<br>
+  llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIScope>><br>
+      LexicalBlockMap;<br>
+<br>
   llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;<br>
   /// Keep track of LexicalBlockStack counter at the beginning of a<br>
   /// function. This is used to pop unbalanced regions at the end of a<br>
@@ -378,6 +383,12 @@ public:<br>
   /// Emit an Objective-C interface type standalone debug info.<br>
   llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);<br>
<br>
+  /// Map AST declaration to its lexical block scope if available.<br>
+  void recordDeclarationLexicalScope(const Decl &D);<br>
+<br>
+  /// Get lexical scope of AST declaration.<br>
+  llvm::DIScope *getDeclarationLexicalScope(const Decl &D, QualType Ty);<br>
+<br>
   /// Emit standalone debug info for a type.<br>
   llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);<br>
<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=264281&r1=264280&r2=264281&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=264281&r1=264280&r2=264281&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Thu Mar 24 08:30:41 2016<br>
@@ -87,11 +87,7 @@ void CodeGenFunction::EmitDecl(const Dec<br>
   case Decl::UsingShadow:<br>
   case Decl::ObjCTypeParam:<br>
     llvm_unreachable("Declaration should not be in declstmts!");<br>
-  case Decl::Function:  // void X();<br>
-  case Decl::Record:    // struct/union/class X;<br>
-  case Decl::Enum:      // enum X;<br>
-  case Decl::EnumConstant: // enum ? { X = ? }<br>
-  case Decl::CXXRecord: // struct/union/class X; [C++]<br>
+  case Decl::Function:     // void X();<br>
   case Decl::StaticAssert: // static_assert(X, ""); [C++0x]<br>
   case Decl::Label:        // __label__ x;<br>
   case Decl::Import:<br>
@@ -101,13 +97,21 @@ void CodeGenFunction::EmitDecl(const Dec<br>
     // None of these decls require codegen support.<br>
     return;<br>
<br>
+  case Decl::Record:       // struct/union/class X;<br>
+  case Decl::Enum:         // enum X;<br>
+  case Decl::EnumConstant: // enum ? { X = ? }<br>
+  case Decl::CXXRecord:    // struct/union/class X; [C++]<br>
+    if (CGDebugInfo *DI = getDebugInfo())<br>
+      DI->recordDeclarationLexicalScope(D);<br>
+    return;<br>
+<br>
   case Decl::NamespaceAlias:<br>
     if (CGDebugInfo *DI = getDebugInfo())<br>
-        DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));<br>
+      DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));<br>
     return;<br>
   case Decl::Using:          // using X; [C++]<br>
     if (CGDebugInfo *DI = getDebugInfo())<br>
-        DI->EmitUsingDecl(cast<UsingDecl>(D));<br>
+      DI->EmitUsingDecl(cast<UsingDecl>(D));<br>
     return;<br>
   case Decl::UsingDirective: // using namespace X; [C++]<br>
     if (CGDebugInfo *DI = getDebugInfo())<br>
@@ -128,6 +132,9 @@ void CodeGenFunction::EmitDecl(const Dec<br>
     const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);<br>
     QualType Ty = TD.getUnderlyingType();<br>
<br>
+    if (CGDebugInfo *DI = getDebugInfo())<br>
+      DI->recordDeclarationLexicalScope(D);<br>
+<br>
     if (Ty->isVariablyModifiedType())<br>
       EmitVariablyModifiedType(Ty);<br>
   }<br>
<br>
Modified: cfe/trunk/test/CodeGenCXX/debug-info-anon-union-vars.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-anon-union-vars.cpp?rev=264281&r1=264280&r2=264281&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-anon-union-vars.cpp?rev=264281&r1=264280&r2=264281&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGenCXX/debug-info-anon-union-vars.cpp (original)<br>
+++ cfe/trunk/test/CodeGenCXX/debug-info-anon-union-vars.cpp Thu Mar 24 08:30:41 2016<br>
@@ -44,7 +44,12 @@ void instantiate(int x) {<br>
   buildBytes(x);<br>
 }<br>
<br>
+// CHECK: ![[UNION:[0-9]+]] = !DICompositeType(tag: DW_TAG_union_type,<br>
+// CHECK-NOT: name:<br>
+// CHECK: elements<br>
 // CHECK: [[FILE:.*]] = !DIFile(filename: "{{.*}}debug-info-anon-union-vars.cpp",<br>
+// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "i", scope: ![[UNION]],<br>
+// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "c", scope: ![[UNION]],<br>
 // CHECK: !DIGlobalVariable(name: "c",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true<br>
 // CHECK: !DIGlobalVariable(name: "d",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true<br>
 // CHECK: !DIGlobalVariable(name: "a",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true<br>
@@ -55,9 +60,4 @@ void instantiate(int x) {<br>
 // CHECK: !DILocalVariable(name: "c", {{.*}}, flags: DIFlagArtificial<br>
 // CHECK: !DILocalVariable(<br>
 // CHECK-NOT: name:<br>
-// CHECK: type: ![[UNION:[0-9]+]]<br>
-// CHECK: ![[UNION]] = !DICompositeType(tag: DW_TAG_union_type,<br>
-// CHECK-NOT: name:<br>
-// CHECK: elements<br>
-// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "i", scope: ![[UNION]],<br>
-// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "c", scope: ![[UNION]],<br>
+// CHECK: type: ![[UNION]]<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</div>