Refactor.
authorDevang Patel <dpatel@apple.com>
Thu, 10 Dec 2009 18:05:33 +0000 (18:05 +0000)
committerDevang Patel <dpatel@apple.com>
Thu, 10 Dec 2009 18:05:33 +0000 (18:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91051 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/DwarfDebug.cpp
lib/CodeGen/AsmPrinter/DwarfDebug.h

index e451d841f64a365a79fc6351f64fd878af2ddba9..21c7dcb227c586cd0998f12f2781d44f4bdc76d5 100644 (file)
@@ -738,6 +738,40 @@ void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
   addBlock(Die, Attribute, 0, Block);
 }
 
+/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
+/// given DIType.
+DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
+  DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
+  if (TyDIE)
+    return TyDIE;
+
+  // Create new type.
+  TyDIE = new DIE(dwarf::DW_TAG_base_type);
+  ModuleCU->insertDIE(Ty.getNode(), TyDIE);
+  if (Ty.isBasicType())
+    constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
+  else if (Ty.isCompositeType())
+    constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
+  else {
+    assert(Ty.isDerivedType() && "Unknown kind of DIType");
+    constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
+  }
+
+  DIDescriptor Context = Ty.getContext();
+  if (Context.isNull())
+    // Add this type into the module cu.
+    ModuleCU->addDie(TyDIE);
+  else if (Context.isType()) {
+    DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
+    ContextDIE->addChild(TyDIE);
+  } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
+    ContextDIE->addChild(TyDIE);
+  else 
+    ModuleCU->addDie(TyDIE);
+
+  return TyDIE;
+}
+
 /// addType - Add a new type attribute to the specified entity.
 void DwarfDebug::addType(DIE *Entity, DIType Ty) {
   if (Ty.isNull())
@@ -757,27 +791,8 @@ void DwarfDebug::addType(DIE *Entity, DIType Ty) {
   ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
 
   // Construct type.
-  DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
-  ModuleCU->insertDIE(Ty.getNode(), Buffer);
-  if (Ty.isBasicType())
-    constructTypeDIE(*Buffer, DIBasicType(Ty.getNode()));
-  else if (Ty.isCompositeType())
-    constructTypeDIE(*Buffer, DICompositeType(Ty.getNode()));
-  else {
-    assert(Ty.isDerivedType() && "Unknown kind of DIType");
-    constructTypeDIE(*Buffer, DIDerivedType(Ty.getNode()));
-  }
-
-  // Add debug information entry to entity and appropriate context.
-  DIE *Die = NULL;
-  DIDescriptor Context = Ty.getContext();
-  if (!Context.isNull())
-    Die = ModuleCU->getDIE(Context.getNode());
+  DIE *Buffer = getOrCreateTypeDIE(Ty);
 
-  if (Die)
-    Die->addChild(Buffer);
-  else
-    ModuleCU->addDie(Buffer);
   Entry->setEntry(Buffer);
   Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
 }
index 3bebc13dc654e9a32d3b99c38ac05dc213ae31dd..580262039de3c78b4b8fc46513a488e42fc5cf5a 100644 (file)
@@ -316,6 +316,10 @@ class DwarfDebug : public Dwarf {
   /// addType - Add a new type attribute to the specified entity.
   void addType(DIE *Entity, DIType Ty);
 
+  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
+  /// given DIType.
+  DIE *getOrCreateTypeDIE(DIType Ty);
+
   void addPubTypes(DISubprogram SP);
 
   /// constructTypeDIE - Construct basic type die from DIBasicType.