blob: e7a4a45573ca32ffa4cd8c644ffa0c02a12a8490 [file] [log] [blame]
Sanjiv Gupta40e56a12008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CodeGenModule.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel3fc13e32009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattnere3afa462008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patelbcded192009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Module.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Devang Patel6fe2e152009-04-17 21:06:59 +000031#include "llvm/System/Path.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000032#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000033using namespace clang;
34using namespace clang::CodeGen;
35
36CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000037 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000038}
39
Chris Lattner562ce0a2008-11-10 06:08:34 +000040CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000041 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000042}
43
Chris Lattner562ce0a2008-11-10 06:08:34 +000044void CGDebugInfo::setLocation(SourceLocation Loc) {
45 if (Loc.isValid())
Chris Lattner18c8dc02009-01-16 07:36:28 +000046 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000047}
48
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000049/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000050/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000051llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel6fe2e152009-04-17 21:06:59 +000052 // Get source file information.
53 const char *FileName = "<unknown>";
Devang Patel61910072009-02-24 23:16:03 +000054 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel6fe2e152009-04-17 21:06:59 +000055 unsigned FID;
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Devang Patel6fe2e152009-04-17 21:06:59 +000057 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
58 FileName = PLoc.getFilename();
59 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000060 }
61
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000062 // See if this compile unit has been used before.
Devang Patel6fe2e152009-04-17 21:06:59 +000063 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner562ce0a2008-11-10 06:08:34 +000064 if (!Unit.isNull()) return Unit;
Daniel Dunbardedc94c2009-04-08 05:11:16 +000065
Devang Patel6fe2e152009-04-17 21:06:59 +000066 // Get absolute path name.
67 llvm::sys::Path AbsFileName(FileName);
68 if (!AbsFileName.isAbsolute()) {
69 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
70 tmp.appendComponent(FileName);
71 AbsFileName = tmp;
72 }
73
74 // See if thie compile unit is represnting main source file.
75 bool isMain = false;
76 const LangOptions &LO = M->getLangOptions();
77 const char *MainFileName = LO.getMainFileName();
78 if (MainFileName) {
79 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
80 isMain = true;
81 } else {
82 if (Loc.isValid() && SM.isFromMainFile(Loc))
83 isMain = true;
84 }
Daniel Dunbardedc94c2009-04-08 05:11:16 +000085
Chris Lattnera9aae3e2009-03-25 03:28:08 +000086 unsigned LangTag;
87 if (LO.CPlusPlus) {
88 if (LO.ObjC1)
89 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
90 else
91 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
92 } else if (LO.ObjC1) {
Devang Patel07461fa2009-03-24 20:35:51 +000093 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000094 } else if (LO.C99) {
Devang Patel07461fa2009-03-24 20:35:51 +000095 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000096 } else {
97 LangTag = llvm::dwarf::DW_LANG_C89;
98 }
Devang Patel6fe2e152009-04-17 21:06:59 +000099
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000100 // Create new compile unit.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000101 // FIXME: Do not know how to get clang version yet.
Devang Patel000ee2f2009-03-19 00:23:53 +0000102 // FIXME: Encode command line options.
103 // FIXME: Encode optimization level.
Devang Patel6fe2e152009-04-17 21:06:59 +0000104 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
105 AbsFileName.getDirname(),
Devang Patel07461fa2009-03-24 20:35:51 +0000106 "clang", isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000107}
108
Devang Patela4cc0c42009-02-25 01:36:11 +0000109/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +0000110/// one if necessary.
111llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +0000112 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000113 unsigned Encoding = 0;
114 switch (BT->getKind()) {
115 default:
116 case BuiltinType::Void:
117 return llvm::DIType();
118 case BuiltinType::UChar:
119 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
120 case BuiltinType::Char_S:
121 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
122 case BuiltinType::UShort:
123 case BuiltinType::UInt:
124 case BuiltinType::ULong:
125 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
126 case BuiltinType::Short:
127 case BuiltinType::Int:
128 case BuiltinType::Long:
129 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
130 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
131 case BuiltinType::Float:
132 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
133 }
134 // Bit size, align and offset of the type.
135 uint64_t Size = M->getContext().getTypeSize(BT);
136 uint64_t Align = M->getContext().getTypeAlign(BT);
137 uint64_t Offset = 0;
138
139 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
140 Offset, /*flags*/ 0, Encoding);
141}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000142
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000143/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
144/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000145llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
146 // We will create one Derived type for one qualifier and recurse to handle any
147 // additional ones.
148 llvm::DIType FromTy;
149 unsigned Tag;
150 if (Ty.isConstQualified()) {
151 Tag = llvm::dwarf::DW_TAG_const_type;
152 Ty.removeConst();
153 FromTy = getOrCreateType(Ty, Unit);
154 } else if (Ty.isVolatileQualified()) {
155 Tag = llvm::dwarf::DW_TAG_volatile_type;
156 Ty.removeVolatile();
157 FromTy = getOrCreateType(Ty, Unit);
158 } else {
159 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
160 Tag = llvm::dwarf::DW_TAG_restrict_type;
161 Ty.removeRestrict();
162 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000163 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000164
Daniel Dunbar44252b42008-10-31 03:54:29 +0000165 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
166 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000167 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
168 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000169}
170
Chris Lattner562ce0a2008-11-10 06:08:34 +0000171llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
172 llvm::DICompileUnit Unit) {
173 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000174
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000175 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000176 uint64_t Size = M->getContext().getTypeSize(Ty);
177 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000178
Chris Lattner562ce0a2008-11-10 06:08:34 +0000179 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
180 "", llvm::DICompileUnit(),
181 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000182}
183
Chris Lattner562ce0a2008-11-10 06:08:34 +0000184llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
185 llvm::DICompileUnit Unit) {
186 // Typedefs are derived from some other type. If we have a typedef of a
187 // typedef, make sure to emit the whole chain.
188 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
189
190 // We don't set size information, but do specify where the typedef was
191 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000192 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000193 SourceLocation DefLoc = Ty->getDecl()->getLocation();
194 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000195
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000196 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000197 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
198 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000199
Chris Lattner562ce0a2008-11-10 06:08:34 +0000200 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
201 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000202}
203
Chris Lattner562ce0a2008-11-10 06:08:34 +0000204llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
205 llvm::DICompileUnit Unit) {
206 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000207
Chris Lattner562ce0a2008-11-10 06:08:34 +0000208 // Add the result type at least.
209 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
210
211 // Set up remainder of arguments if there is a prototype.
212 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000213 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000214 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
215 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
216 } else {
217 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000218 }
219
Chris Lattner562ce0a2008-11-10 06:08:34 +0000220 llvm::DIArray EltTypeArray =
221 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
222
223 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
224 Unit, "", llvm::DICompileUnit(),
225 0, 0, 0, 0, 0,
226 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000227}
228
Devang Patela4cc0c42009-02-25 01:36:11 +0000229/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000230llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
231 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000232 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000233
Chris Lattner562ce0a2008-11-10 06:08:34 +0000234 unsigned Tag;
235 if (Decl->isStruct())
236 Tag = llvm::dwarf::DW_TAG_structure_type;
237 else if (Decl->isUnion())
238 Tag = llvm::dwarf::DW_TAG_union_type;
239 else {
240 assert(Decl->isClass() && "Unknown RecordType!");
241 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000242 }
243
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000244 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000245
Chris Lattner562ce0a2008-11-10 06:08:34 +0000246 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000247 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000248
Chris Lattner562ce0a2008-11-10 06:08:34 +0000249 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel9b3f5f72009-04-17 21:35:15 +0000250 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
251 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000252
253 // Records and classes and unions can all be recursive. To handle them, we
254 // first generate a debug descriptor for the struct as a forward declaration.
255 // Then (if it is a definition) we go through and get debug info for all of
256 // its members. Finally, we create a descriptor for the complete type (which
257 // may refer to the forward decl if the struct is recursive) and replace all
258 // uses of the forward declaration with the final definition.
259 llvm::DIType FwdDecl =
260 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
261 llvm::DIType(), llvm::DIArray());
262
263 // If this is just a forward declaration, return it.
264 if (!Decl->getDefinition(M->getContext()))
265 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000266
Chris Lattner562ce0a2008-11-10 06:08:34 +0000267 // Otherwise, insert it into the TypeCache so that recursive uses will find
268 // it.
269 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
270
271 // Convert all the elements.
272 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
273
274 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
275
276 unsigned FieldNo = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000277 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
278 E = Decl->field_end(M->getContext());
Douglas Gregor640a04b2008-12-11 17:59:21 +0000279 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000280 FieldDecl *Field = *I;
281 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000282
283 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000284
285 // Get the location for the field.
286 SourceLocation FieldDefLoc = Field->getLocation();
287 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel9b3f5f72009-04-17 21:35:15 +0000288 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
289 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
290
Devang Patelb3a08ba2009-03-16 23:47:53 +0000291
292 QualType FType = Field->getType();
293 uint64_t FieldSize = 0;
294 unsigned FieldAlign = 0;
295 if (!FType->isIncompleteArrayType()) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000296
Devang Patelb3a08ba2009-03-16 23:47:53 +0000297 // Bit size, align and offset of the type.
298 FieldSize = M->getContext().getTypeSize(FType);
299 Expr *BitWidth = Field->getBitWidth();
300 if (BitWidth)
301 FieldSize =
302 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
303
304 FieldAlign = M->getContext().getTypeAlign(FType);
305 }
306
Chris Lattner562ce0a2008-11-10 06:08:34 +0000307 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
308
309 // Create a DW_TAG_member node to remember the offset of this field in the
310 // struct. FIXME: This is an absolutely insane way to capture this
311 // information. When we gut debug info, this should be fixed.
312 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
313 FieldName, FieldDefUnit,
314 FieldLine, FieldSize, FieldAlign,
315 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000316 EltTys.push_back(FieldTy);
317 }
318
319 llvm::DIArray Elements =
320 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
321
322 // Bit size, align and offset of the type.
323 uint64_t Size = M->getContext().getTypeSize(Ty);
324 uint64_t Align = M->getContext().getTypeAlign(Ty);
325
326 llvm::DIType RealDecl =
327 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
328 Align, 0, 0, llvm::DIType(), Elements);
329
330 // Now that we have a real decl for the struct, replace anything using the
331 // old decl with the new one. This will recursively update the debug info.
332 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
333 FwdDecl.getGV()->eraseFromParent();
334
335 return RealDecl;
336}
337
Devang Patel3fc13e32009-02-26 21:10:26 +0000338/// CreateType - get objective-c interface type.
339llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
340 llvm::DICompileUnit Unit) {
341 ObjCInterfaceDecl *Decl = Ty->getDecl();
342
343 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
344 SourceManager &SM = M->getContext().getSourceManager();
345
346 // Get overall information about the record type for the debug info.
347 std::string Name = Decl->getNameAsString();
348
349 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel9b3f5f72009-04-17 21:35:15 +0000350 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
351 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
352
Devang Patel3fc13e32009-02-26 21:10:26 +0000353
354 // To handle recursive interface, we
355 // first generate a debug descriptor for the struct as a forward declaration.
356 // Then (if it is a definition) we go through and get debug info for all of
357 // its members. Finally, we create a descriptor for the complete type (which
358 // may refer to the forward decl if the struct is recursive) and replace all
359 // uses of the forward declaration with the final definition.
360 llvm::DIType FwdDecl =
361 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
362 llvm::DIType(), llvm::DIArray());
363
364 // If this is just a forward declaration, return it.
365 if (Decl->isForwardDecl())
366 return FwdDecl;
367
368 // Otherwise, insert it into the TypeCache so that recursive uses will find
369 // it.
370 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
371
372 // Convert all the elements.
373 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
374
Devang Patele6178df2009-03-10 21:30:26 +0000375 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
376 if (SClass) {
377 llvm::DIType SClassTy =
378 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
379 llvm::DIType InhTag =
380 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
381 Unit, "", Unit, 0, 0, 0,
382 0 /* offset */, 0, SClassTy);
383 EltTys.push_back(InhTag);
384 }
385
Devang Patel3fc13e32009-02-26 21:10:26 +0000386 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
387
388 unsigned FieldNo = 0;
389 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
390 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
391 ObjCIvarDecl *Field = *I;
392 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
393
394 std::string FieldName = Field->getNameAsString();
395
396 // Get the location for the field.
397 SourceLocation FieldDefLoc = Field->getLocation();
398 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel9b3f5f72009-04-17 21:35:15 +0000399 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
400 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
401
Devang Patel806af7d2009-03-20 18:24:39 +0000402
403 QualType FType = Field->getType();
404 uint64_t FieldSize = 0;
405 unsigned FieldAlign = 0;
Devang Patel000ee2f2009-03-19 00:23:53 +0000406
Devang Patel806af7d2009-03-20 18:24:39 +0000407 if (!FType->isIncompleteArrayType()) {
408
409 // Bit size, align and offset of the type.
410 FieldSize = M->getContext().getTypeSize(FType);
411 Expr *BitWidth = Field->getBitWidth();
412 if (BitWidth)
413 FieldSize =
414 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
415
416 FieldAlign = M->getContext().getTypeAlign(FType);
417 }
418
419 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
420
Devang Patel000ee2f2009-03-19 00:23:53 +0000421 unsigned Flags = 0;
422 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
423 Flags = llvm::DIType::FlagProtected;
424 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
425 Flags = llvm::DIType::FlagPrivate;
426
Devang Patel3fc13e32009-02-26 21:10:26 +0000427 // Create a DW_TAG_member node to remember the offset of this field in the
428 // struct. FIXME: This is an absolutely insane way to capture this
429 // information. When we gut debug info, this should be fixed.
430 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
431 FieldName, FieldDefUnit,
432 FieldLine, FieldSize, FieldAlign,
Devang Patel000ee2f2009-03-19 00:23:53 +0000433 FieldOffset, Flags, FieldTy);
Devang Patel3fc13e32009-02-26 21:10:26 +0000434 EltTys.push_back(FieldTy);
435 }
436
437 llvm::DIArray Elements =
438 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
439
440 // Bit size, align and offset of the type.
441 uint64_t Size = M->getContext().getTypeSize(Ty);
442 uint64_t Align = M->getContext().getTypeAlign(Ty);
443
444 llvm::DIType RealDecl =
445 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
446 Align, 0, 0, llvm::DIType(), Elements);
447
448 // Now that we have a real decl for the struct, replace anything using the
449 // old decl with the new one. This will recursively update the debug info.
450 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
451 FwdDecl.getGV()->eraseFromParent();
452
453 return RealDecl;
454}
455
Chris Lattner562ce0a2008-11-10 06:08:34 +0000456llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
457 llvm::DICompileUnit Unit) {
458 EnumDecl *Decl = Ty->getDecl();
459
460 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
461
462 // Create DIEnumerator elements for each enumerator.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000463 for (EnumDecl::enumerator_iterator
464 Enum = Decl->enumerator_begin(M->getContext()),
465 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000466 Enum != EnumEnd; ++Enum) {
467 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
468 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000469 }
470
471 // Return a CompositeType for the enum itself.
472 llvm::DIArray EltArray =
473 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
474
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000475 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000476 SourceLocation DefLoc = Decl->getLocation();
477 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
478 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000479 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
480 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
481
Chris Lattner562ce0a2008-11-10 06:08:34 +0000482
483 // Size and align of the type.
484 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000485 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000486
487 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
488 Unit, EnumName, DefUnit, Line,
489 Size, Align, 0, 0,
490 llvm::DIType(), EltArray);
491}
492
493llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
494 llvm::DICompileUnit Unit) {
495 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
496 return CreateType(RT, Unit);
497 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
498 return CreateType(ET, Unit);
499
500 return llvm::DIType();
501}
502
503llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
504 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000505 uint64_t Size;
506 uint64_t Align;
507
508
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000509 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000510 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000511 Size = 0;
512 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000513 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
514 } else if (Ty->isIncompleteArrayType()) {
515 Size = 0;
516 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000517 } else {
518 // Size and align of the whole array, not the element type.
519 Size = M->getContext().getTypeSize(Ty);
520 Align = M->getContext().getTypeAlign(Ty);
521 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000522
523 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
524 // interior arrays, do we care? Why aren't nested arrays represented the
525 // obvious/recursive way?
526 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
527 QualType EltTy(Ty, 0);
528 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000529 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000530 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
531 Upper = CAT->getSize().getZExtValue() - 1;
532 // FIXME: Verify this is right for VLAs.
533 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
534 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000535 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000536
Chris Lattner562ce0a2008-11-10 06:08:34 +0000537 llvm::DIArray SubscriptArray =
538 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
539
540 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
541 Unit, "", llvm::DICompileUnit(),
542 0, Size, Align, 0, 0,
543 getOrCreateType(EltTy, Unit),
544 SubscriptArray);
545}
546
547
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000548/// getOrCreateType - Get the type from the cache or create a new
549/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000550llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
551 llvm::DICompileUnit Unit) {
552 if (Ty.isNull())
553 return llvm::DIType();
554
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000555 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000556 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
557 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000558
Chris Lattner562ce0a2008-11-10 06:08:34 +0000559 // Handle CVR qualifiers, which recursively handles what they refer to.
560 if (Ty.getCVRQualifiers())
561 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000562
563 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000564 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000565#define TYPE(Class, Base)
566#define ABSTRACT_TYPE(Class, Base)
567#define NON_CANONICAL_TYPE(Class, Base)
568#define DEPENDENT_TYPE(Class, Base) case Type::Class:
569#include "clang/AST/TypeNodes.def"
570 assert(false && "Dependent types cannot show up in debug information");
571
Chris Lattner562ce0a2008-11-10 06:08:34 +0000572 case Type::Complex:
Sebastian Redlce6fff02009-03-16 23:22:08 +0000573 case Type::LValueReference:
574 case Type::RValueReference:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000575 case Type::Vector:
576 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000577 case Type::ExtQual:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000578 case Type::ObjCQualifiedInterface:
579 case Type::ObjCQualifiedId:
Eli Friedman673e91b2009-02-27 23:15:07 +0000580 case Type::FixedWidthInt:
581 case Type::BlockPointer:
582 case Type::MemberPointer:
Douglas Gregordd13e842009-03-30 22:58:21 +0000583 case Type::TemplateSpecialization:
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000584 case Type::QualifiedName:
Eli Friedman673e91b2009-02-27 23:15:07 +0000585 case Type::ObjCQualifiedClass:
586 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000587 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000588
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000589 case Type::ObjCInterface:
590 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000591 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
592 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000593 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
594 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000595 case Type::Enum:
596 Slot = CreateType(cast<TagType>(Ty), Unit);
597 break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000598 case Type::FunctionProto:
599 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000600 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000601
602 case Type::ConstantArray:
603 case Type::VariableArray:
604 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000605 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000606 case Type::TypeOfExpr:
607 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000608 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000609 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000610 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
611 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000612 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000613
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000614 return Slot;
615}
616
617/// EmitFunctionStart - Constructs the debug code for entering a function -
618/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000619void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000620 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000621 CGBuilderTy &Builder) {
622 // FIXME: Why is this using CurLoc???
623 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000624 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel7f9e2092009-04-08 19:47:04 +0000625 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000626
627 llvm::DISubprogram SP =
628 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
629 getOrCreateType(ReturnType, Unit),
630 Fn->hasInternalLinkage(), true/*definition*/);
631
632 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
633
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000634 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000635 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000636}
637
638
Chris Lattner562ce0a2008-11-10 06:08:34 +0000639void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000640 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
641
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000642 // Don't bother if things are the same as last time.
643 SourceManager &SM = M->getContext().getSourceManager();
644 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000645 || (SM.getInstantiationLineNumber(CurLoc) ==
646 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000647 && SM.isFromSameFile(CurLoc, PrevLoc)))
648 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000649
650 // Update last state.
651 PrevLoc = CurLoc;
652
653 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000654 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel7f9e2092009-04-08 19:47:04 +0000655 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
656 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000657 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000658}
659
660/// EmitRegionStart- Constructs the debug code for entering a declarative
661/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000662void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
663 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000664 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000665 D = RegionStack.back();
666 D = DebugFactory.CreateBlock(D);
667 RegionStack.push_back(D);
668 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000669}
670
671/// EmitRegionEnd - Constructs the debug code for exiting a declarative
672/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000673void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000674 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
675
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000676 // Provide an region stop point.
677 EmitStopPoint(Fn, Builder);
678
Chris Lattner562ce0a2008-11-10 06:08:34 +0000679 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000680 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000681}
682
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000683/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000684void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
685 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000686 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
687
Devang Patelbcded192009-03-27 23:16:32 +0000688 // Do not emit variable debug information while generating optimized code.
689 // The llvm optimizer and code generator are not yet ready to support
690 // optimized code debugging.
691 const CompileOptions &CO = M->getCompileOpts();
692 if (CO.OptimizationLevel)
693 return;
694
Chris Lattner562ce0a2008-11-10 06:08:34 +0000695 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000696 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000697 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
698 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000699 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
700
701 // Create the descriptor for the variable.
702 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000703 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000704 Unit, Line,
705 getOrCreateType(Decl->getType(), Unit));
706 // Insert an llvm.dbg.declare into the current block.
707 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000708}
709
Chris Lattner562ce0a2008-11-10 06:08:34 +0000710void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
711 llvm::Value *Storage,
712 CGBuilderTy &Builder) {
713 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
714}
715
716/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
717/// variable declaration.
718void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
719 CGBuilderTy &Builder) {
720 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
721}
722
723
724
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000725/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000726void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
727 const VarDecl *Decl) {
Devang Patelbcded192009-03-27 23:16:32 +0000728
729 // Do not emit variable debug information while generating optimized code.
730 // The llvm optimizer and code generator are not yet ready to support
731 // optimized code debugging.
732 const CompileOptions &CO = M->getCompileOpts();
733 if (CO.OptimizationLevel)
734 return;
735
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000736 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000737 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000738 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000739 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
740 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000741
742 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000743
744 QualType T = Decl->getType();
745 if (T->isIncompleteArrayType()) {
746
747 // CodeGen turns int[] into int[1] so we'll do the same here.
748 llvm::APSInt ConstVal(32);
749
750 ConstVal = 1;
751 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
752
753 T = M->getContext().getConstantArrayType(ET, ConstVal,
754 ArrayType::Normal, 0);
755 }
756
Chris Lattner562ce0a2008-11-10 06:08:34 +0000757 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000758 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000759 Var->hasInternalLinkage(),
760 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000761}
762
Devang Patel3fc13e32009-02-26 21:10:26 +0000763/// EmitGlobalVariable - Emit information about an objective-c interface.
764void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
765 ObjCInterfaceDecl *Decl) {
766 // Create global variable debug descriptor.
767 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
768 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000769 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
770 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel3fc13e32009-02-26 21:10:26 +0000771
772 std::string Name = Decl->getNameAsString();
773
Chris Lattner46ee0f32009-04-01 06:23:52 +0000774 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel3fc13e32009-02-26 21:10:26 +0000775 if (T->isIncompleteArrayType()) {
776
777 // CodeGen turns int[] into int[1] so we'll do the same here.
778 llvm::APSInt ConstVal(32);
779
780 ConstVal = 1;
781 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
782
783 T = M->getContext().getConstantArrayType(ET, ConstVal,
784 ArrayType::Normal, 0);
785 }
786
787 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
788 getOrCreateType(T, Unit),
789 Var->hasInternalLinkage(),
790 true/*definition*/, Var);
791}
792