@@ -108,59 +108,8 @@ unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) {
108108 File->getSource (), CUID);
109109}
110110
111- DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE (
112- const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
113- // Check for pre-existence.
114- if (DIE *Die = getDIE (GV))
115- return Die;
116-
117- assert (GV);
118-
119- auto *GVContext = GV->getScope ();
120- auto *GTy = DD->resolve (GV->getType ());
121-
122- // Construct the context before querying for the existence of the DIE in
123- // case such construction creates the DIE.
124- DIE *ContextDIE = getOrCreateContextDIE (GVContext);
125-
126- // Add to map.
127- DIE *VariableDIE = &createAndAddDIE (GV->getTag (), *ContextDIE, GV);
128- DIScope *DeclContext;
129- if (auto *SDMDecl = GV->getStaticDataMemberDeclaration ()) {
130- DeclContext = resolve (SDMDecl->getScope ());
131- assert (SDMDecl->isStaticMember () && " Expected static member decl" );
132- assert (GV->isDefinition ());
133- // We need the declaration DIE that is in the static member's class.
134- DIE *VariableSpecDIE = getOrCreateStaticMemberDIE (SDMDecl);
135- addDIEEntry (*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
136- // If the global variable's type is different from the one in the class
137- // member type, assume that it's more specific and also emit it.
138- if (GTy != DD->resolve (SDMDecl->getBaseType ()))
139- addType (*VariableDIE, GTy);
140- } else {
141- DeclContext = GV->getScope ();
142- // Add name and type.
143- addString (*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName ());
144- addType (*VariableDIE, GTy);
145-
146- // Add scoping info.
147- if (!GV->isLocalToUnit ())
148- addFlag (*VariableDIE, dwarf::DW_AT_external);
149-
150- // Add line number info.
151- addSourceLine (*VariableDIE, GV);
152- }
153-
154- if (!GV->isDefinition ())
155- addFlag (*VariableDIE, dwarf::DW_AT_declaration);
156- else
157- addGlobalName (GV->getName (), *VariableDIE, DeclContext);
158-
159- if (uint32_t AlignInBytes = GV->getAlignInBytes ())
160- addUInt (*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
161- AlignInBytes);
162-
163- // Add location.
111+ void DwarfCompileUnit::addLocationAttribute (
112+ DIE *ToDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
164113 bool addToAccelTable = false ;
165114 DIELoc *Loc = nullptr ;
166115 std::unique_ptr<DIEDwarfExpression> DwarfExpr;
@@ -173,7 +122,7 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
173122 // DW_AT_const_value(X).
174123 if (GlobalExprs.size () == 1 && Expr && Expr->isConstant ()) {
175124 addToAccelTable = true ;
176- addConstantValue (*VariableDIE , /* Unsigned=*/ true , Expr->getElement (1 ));
125+ addConstantValue (*ToDIE , /* Unsigned=*/ true , Expr->getElement (1 ));
177126 break ;
178127 }
179128
@@ -239,20 +188,101 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
239188 DwarfExpr->addExpression (Expr);
240189 }
241190 if (Loc)
242- addBlock (*VariableDIE , dwarf::DW_AT_location, DwarfExpr->finalize ());
191+ addBlock (*ToDIE , dwarf::DW_AT_location, DwarfExpr->finalize ());
243192
244193 if (DD->useAllLinkageNames ())
245- addLinkageName (*VariableDIE , GV->getLinkageName ());
194+ addLinkageName (*ToDIE , GV->getLinkageName ());
246195
247196 if (addToAccelTable) {
248- DD->addAccelName (GV->getName (), *VariableDIE );
197+ DD->addAccelName (GV->getName (), *ToDIE );
249198
250199 // If the linkage name is different than the name, go ahead and output
251200 // that as well into the name table.
252201 if (GV->getLinkageName () != " " && GV->getName () != GV->getLinkageName () &&
253202 DD->useAllLinkageNames ())
254- DD->addAccelName (GV->getLinkageName (), *VariableDIE);
203+ DD->addAccelName (GV->getLinkageName (), *ToDIE);
204+ }
205+ }
206+
207+ DIE *DwarfCompileUnit::getOrCreateCommonBlock (
208+ const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) {
209+ // Construct the context before querying for the existence of the DIE in case
210+ // such construction creates the DIE.
211+ DIE *ContextDIE = getOrCreateContextDIE (CB->getScope ());
212+
213+ if (DIE *NDie = getDIE (CB))
214+ return NDie;
215+ DIE &NDie = createAndAddDIE (dwarf::DW_TAG_common_block, *ContextDIE, CB);
216+ StringRef Name = CB->getName ().empty () ? " _BLNK_" : CB->getName ();
217+ addString (NDie, dwarf::DW_AT_name, Name);
218+ addGlobalName (Name, NDie, CB->getScope ());
219+ if (CB->getFile ())
220+ addSourceLine (NDie, CB->getLineNo (), CB->getFile ());
221+ if (DIGlobalVariable *V = CB->getDecl ())
222+ getCU ().addLocationAttribute (&NDie, V, GlobalExprs);
223+ if (uint32_t AlignInBits = CB->getAlignInBits ()) {
224+ uint32_t AlignInBytes = AlignInBits >> 3 ;
225+ addUInt (NDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, AlignInBytes);
255226 }
227+ return &NDie;
228+ }
229+
230+ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE (
231+ const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
232+ // Check for pre-existence.
233+ if (DIE *Die = getDIE (GV))
234+ return Die;
235+
236+ assert (GV);
237+
238+ auto *GVContext = GV->getScope ();
239+ auto *GTy = DD->resolve (GV->getType ());
240+
241+ // Construct the context before querying for the existence of the DIE in
242+ // case such construction creates the DIE.
243+ auto *CB = dyn_cast<DICommonBlock>(GVContext);
244+ DIE *ContextDIE = CB ? getOrCreateCommonBlock (CB, GlobalExprs)
245+ : getOrCreateContextDIE (GVContext);
246+
247+ // Add to map.
248+ DIE *VariableDIE = &createAndAddDIE (GV->getTag (), *ContextDIE, GV);
249+ DIScope *DeclContext;
250+ if (auto *SDMDecl = GV->getStaticDataMemberDeclaration ()) {
251+ DeclContext = resolve (SDMDecl->getScope ());
252+ assert (SDMDecl->isStaticMember () && " Expected static member decl" );
253+ assert (GV->isDefinition ());
254+ // We need the declaration DIE that is in the static member's class.
255+ DIE *VariableSpecDIE = getOrCreateStaticMemberDIE (SDMDecl);
256+ addDIEEntry (*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
257+ // If the global variable's type is different from the one in the class
258+ // member type, assume that it's more specific and also emit it.
259+ if (GTy != DD->resolve (SDMDecl->getBaseType ()))
260+ addType (*VariableDIE, GTy);
261+ } else {
262+ DeclContext = GV->getScope ();
263+ // Add name and type.
264+ addString (*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName ());
265+ addType (*VariableDIE, GTy);
266+
267+ // Add scoping info.
268+ if (!GV->isLocalToUnit ())
269+ addFlag (*VariableDIE, dwarf::DW_AT_external);
270+
271+ // Add line number info.
272+ addSourceLine (*VariableDIE, GV);
273+ }
274+
275+ if (!GV->isDefinition ())
276+ addFlag (*VariableDIE, dwarf::DW_AT_declaration);
277+ else
278+ addGlobalName (GV->getName (), *VariableDIE, DeclContext);
279+
280+ if (uint32_t AlignInBytes = GV->getAlignInBytes ())
281+ addUInt (*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
282+ AlignInBytes);
283+
284+ // Add location.
285+ addLocationAttribute (VariableDIE, GV, GlobalExprs);
256286
257287 return VariableDIE;
258288}
0 commit comments