blob: c0819369b1f5e1b7009d435f461a0fe31f9a412c [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-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 Gupta1c6a38b2008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patel07739032009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-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 Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000031#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032using namespace clang;
33using namespace clang::CodeGen;
34
35CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner9c85ba32008-11-10 06:08:34 +000036 : M(m), DebugFactory(M->getModule()) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037}
38
Chris Lattner9c85ba32008-11-10 06:08:34 +000039CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000040 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043void CGDebugInfo::setLocation(SourceLocation Loc) {
44 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000045 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000046}
47
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000049/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000050llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar831570c2009-01-22 00:09:25 +000051 // FIXME: Until we do a complete job of emitting debug information,
52 // we need to support making dummy compile units so that we generate
53 // "well formed" debug info.
54 const FileEntry *FE = 0;
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000055
Devang Patel77820222009-02-24 23:16:03 +000056 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner515455a2009-03-25 03:28:08 +000057 bool isMain;
Daniel Dunbar831570c2009-01-22 00:09:25 +000058 if (Loc.isValid()) {
Daniel Dunbar831570c2009-01-22 00:09:25 +000059 Loc = SM.getInstantiationLoc(Loc);
60 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Chris Lattner515455a2009-03-25 03:28:08 +000061 isMain = SM.getFileID(Loc) == SM.getMainFileID();
Devang Patel77820222009-02-24 23:16:03 +000062 } else {
63 // If Loc is not valid then use main file id.
64 FE = SM.getFileEntryForID(SM.getMainFileID());
Chris Lattner515455a2009-03-25 03:28:08 +000065 isMain = true;
Daniel Dunbar831570c2009-01-22 00:09:25 +000066 }
67
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000068 // See if this compile unit has been used before.
Chris Lattner9c85ba32008-11-10 06:08:34 +000069 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
70 if (!Unit.isNull()) return Unit;
71
72 // Get source file information.
Daniel Dunbar831570c2009-01-22 00:09:25 +000073 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbarf8e58d02009-02-07 00:40:41 +000074 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar2104bf92008-10-24 00:46:51 +000075
Chris Lattner515455a2009-03-25 03:28:08 +000076 const LangOptions &LO = M->getLangOptions();
77 unsigned LangTag;
78 if (LO.CPlusPlus) {
79 if (LO.ObjC1)
80 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
81 else
82 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
83 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +000084 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +000085 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +000086 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +000087 } else {
88 LangTag = llvm::dwarf::DW_LANG_C89;
89 }
Devang Patel8d9aefc2009-03-24 20:35:51 +000090
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000091 // Create new compile unit.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000092 // FIXME: Do not know how to get clang version yet.
Devang Patelc20482b2009-03-19 00:23:53 +000093 // FIXME: Encode command line options.
94 // FIXME: Encode optimization level.
Devang Patel8d9aefc2009-03-24 20:35:51 +000095 return Unit = DebugFactory.CreateCompileUnit(LangTag, FileName, DirName,
96 "clang", isMain);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000097}
98
Devang Patel65e99f22009-02-25 01:36:11 +000099/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000100/// one if necessary.
101llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000102 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000103 unsigned Encoding = 0;
104 switch (BT->getKind()) {
105 default:
106 case BuiltinType::Void:
107 return llvm::DIType();
108 case BuiltinType::UChar:
109 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
110 case BuiltinType::Char_S:
111 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
112 case BuiltinType::UShort:
113 case BuiltinType::UInt:
114 case BuiltinType::ULong:
115 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
116 case BuiltinType::Short:
117 case BuiltinType::Int:
118 case BuiltinType::Long:
119 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
120 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
121 case BuiltinType::Float:
122 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
123 }
124 // Bit size, align and offset of the type.
125 uint64_t Size = M->getContext().getTypeSize(BT);
126 uint64_t Align = M->getContext().getTypeAlign(BT);
127 uint64_t Offset = 0;
128
129 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
130 Offset, /*flags*/ 0, Encoding);
131}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000132
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000133/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
134/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000135llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
136 // We will create one Derived type for one qualifier and recurse to handle any
137 // additional ones.
138 llvm::DIType FromTy;
139 unsigned Tag;
140 if (Ty.isConstQualified()) {
141 Tag = llvm::dwarf::DW_TAG_const_type;
142 Ty.removeConst();
143 FromTy = getOrCreateType(Ty, Unit);
144 } else if (Ty.isVolatileQualified()) {
145 Tag = llvm::dwarf::DW_TAG_volatile_type;
146 Ty.removeVolatile();
147 FromTy = getOrCreateType(Ty, Unit);
148 } else {
149 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
150 Tag = llvm::dwarf::DW_TAG_restrict_type;
151 Ty.removeRestrict();
152 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000153 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000154
Daniel Dunbar3845f862008-10-31 03:54:29 +0000155 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
156 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000157 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
158 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000159}
160
Chris Lattner9c85ba32008-11-10 06:08:34 +0000161llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
162 llvm::DICompileUnit Unit) {
163 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000164
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000165 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000166 uint64_t Size = M->getContext().getTypeSize(Ty);
167 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000168
Chris Lattner9c85ba32008-11-10 06:08:34 +0000169 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
170 "", llvm::DICompileUnit(),
171 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000172}
173
Chris Lattner9c85ba32008-11-10 06:08:34 +0000174llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
175 llvm::DICompileUnit Unit) {
176 // Typedefs are derived from some other type. If we have a typedef of a
177 // typedef, make sure to emit the whole chain.
178 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
179
180 // We don't set size information, but do specify where the typedef was
181 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000182 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000183 SourceLocation DefLoc = Ty->getDecl()->getLocation();
184 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000185
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000186 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000187 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000188
Chris Lattner9c85ba32008-11-10 06:08:34 +0000189 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
190 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000191}
192
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
194 llvm::DICompileUnit Unit) {
195 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000196
Chris Lattner9c85ba32008-11-10 06:08:34 +0000197 // Add the result type at least.
198 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
199
200 // Set up remainder of arguments if there is a prototype.
201 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000202 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000203 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
204 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
205 } else {
206 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000207 }
208
Chris Lattner9c85ba32008-11-10 06:08:34 +0000209 llvm::DIArray EltTypeArray =
210 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
211
212 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
213 Unit, "", llvm::DICompileUnit(),
214 0, 0, 0, 0, 0,
215 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000216}
217
Devang Patel65e99f22009-02-25 01:36:11 +0000218/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
220 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000221 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000222
Chris Lattner9c85ba32008-11-10 06:08:34 +0000223 unsigned Tag;
224 if (Decl->isStruct())
225 Tag = llvm::dwarf::DW_TAG_structure_type;
226 else if (Decl->isUnion())
227 Tag = llvm::dwarf::DW_TAG_union_type;
228 else {
229 assert(Decl->isClass() && "Unknown RecordType!");
230 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000231 }
232
Sanjiv Gupta507de852008-06-09 10:47:41 +0000233 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000234
Chris Lattner9c85ba32008-11-10 06:08:34 +0000235 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000236 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000237
Chris Lattner9c85ba32008-11-10 06:08:34 +0000238 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000239 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000240
241
242 // Records and classes and unions can all be recursive. To handle them, we
243 // first generate a debug descriptor for the struct as a forward declaration.
244 // Then (if it is a definition) we go through and get debug info for all of
245 // its members. Finally, we create a descriptor for the complete type (which
246 // may refer to the forward decl if the struct is recursive) and replace all
247 // uses of the forward declaration with the final definition.
248 llvm::DIType FwdDecl =
249 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
250 llvm::DIType(), llvm::DIArray());
251
252 // If this is just a forward declaration, return it.
253 if (!Decl->getDefinition(M->getContext()))
254 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000255
Chris Lattner9c85ba32008-11-10 06:08:34 +0000256 // Otherwise, insert it into the TypeCache so that recursive uses will find
257 // it.
258 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
259
260 // Convert all the elements.
261 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
262
263 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
264
265 unsigned FieldNo = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000266 for (RecordDecl::field_iterator I = Decl->field_begin(),
267 E = Decl->field_end();
268 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000269 FieldDecl *Field = *I;
270 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000271
272 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000273
274 // Get the location for the field.
275 SourceLocation FieldDefLoc = Field->getLocation();
276 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000277 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelec9b5d52009-03-16 23:47:53 +0000278
279 QualType FType = Field->getType();
280 uint64_t FieldSize = 0;
281 unsigned FieldAlign = 0;
282 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000283
Devang Patelec9b5d52009-03-16 23:47:53 +0000284 // Bit size, align and offset of the type.
285 FieldSize = M->getContext().getTypeSize(FType);
286 Expr *BitWidth = Field->getBitWidth();
287 if (BitWidth)
288 FieldSize =
289 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
290
291 FieldAlign = M->getContext().getTypeAlign(FType);
292 }
293
Chris Lattner9c85ba32008-11-10 06:08:34 +0000294 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
295
296 // Create a DW_TAG_member node to remember the offset of this field in the
297 // struct. FIXME: This is an absolutely insane way to capture this
298 // information. When we gut debug info, this should be fixed.
299 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
300 FieldName, FieldDefUnit,
301 FieldLine, FieldSize, FieldAlign,
302 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000303 EltTys.push_back(FieldTy);
304 }
305
306 llvm::DIArray Elements =
307 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
308
309 // Bit size, align and offset of the type.
310 uint64_t Size = M->getContext().getTypeSize(Ty);
311 uint64_t Align = M->getContext().getTypeAlign(Ty);
312
313 llvm::DIType RealDecl =
314 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
315 Align, 0, 0, llvm::DIType(), Elements);
316
317 // Now that we have a real decl for the struct, replace anything using the
318 // old decl with the new one. This will recursively update the debug info.
319 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
320 FwdDecl.getGV()->eraseFromParent();
321
322 return RealDecl;
323}
324
Devang Patel9ca36b62009-02-26 21:10:26 +0000325/// CreateType - get objective-c interface type.
326llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
327 llvm::DICompileUnit Unit) {
328 ObjCInterfaceDecl *Decl = Ty->getDecl();
329
330 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
331 SourceManager &SM = M->getContext().getSourceManager();
332
333 // Get overall information about the record type for the debug info.
334 std::string Name = Decl->getNameAsString();
335
336 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
337 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
338
339
340 // To handle recursive interface, we
341 // first generate a debug descriptor for the struct as a forward declaration.
342 // Then (if it is a definition) we go through and get debug info for all of
343 // its members. Finally, we create a descriptor for the complete type (which
344 // may refer to the forward decl if the struct is recursive) and replace all
345 // uses of the forward declaration with the final definition.
346 llvm::DIType FwdDecl =
347 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
348 llvm::DIType(), llvm::DIArray());
349
350 // If this is just a forward declaration, return it.
351 if (Decl->isForwardDecl())
352 return FwdDecl;
353
354 // Otherwise, insert it into the TypeCache so that recursive uses will find
355 // it.
356 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
357
358 // Convert all the elements.
359 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
360
Devang Patelfbe899f2009-03-10 21:30:26 +0000361 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
362 if (SClass) {
363 llvm::DIType SClassTy =
364 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
365 llvm::DIType InhTag =
366 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
367 Unit, "", Unit, 0, 0, 0,
368 0 /* offset */, 0, SClassTy);
369 EltTys.push_back(InhTag);
370 }
371
Devang Patel9ca36b62009-02-26 21:10:26 +0000372 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
373
374 unsigned FieldNo = 0;
375 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
376 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
377 ObjCIvarDecl *Field = *I;
378 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
379
380 std::string FieldName = Field->getNameAsString();
381
382 // Get the location for the field.
383 SourceLocation FieldDefLoc = Field->getLocation();
384 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
385 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patel99c20eb2009-03-20 18:24:39 +0000386
387 QualType FType = Field->getType();
388 uint64_t FieldSize = 0;
389 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000390
Devang Patel99c20eb2009-03-20 18:24:39 +0000391 if (!FType->isIncompleteArrayType()) {
392
393 // Bit size, align and offset of the type.
394 FieldSize = M->getContext().getTypeSize(FType);
395 Expr *BitWidth = Field->getBitWidth();
396 if (BitWidth)
397 FieldSize =
398 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
399
400 FieldAlign = M->getContext().getTypeAlign(FType);
401 }
402
403 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
404
Devang Patelc20482b2009-03-19 00:23:53 +0000405 unsigned Flags = 0;
406 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
407 Flags = llvm::DIType::FlagProtected;
408 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
409 Flags = llvm::DIType::FlagPrivate;
410
Devang Patel9ca36b62009-02-26 21:10:26 +0000411 // Create a DW_TAG_member node to remember the offset of this field in the
412 // struct. FIXME: This is an absolutely insane way to capture this
413 // information. When we gut debug info, this should be fixed.
414 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
415 FieldName, FieldDefUnit,
416 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000417 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000418 EltTys.push_back(FieldTy);
419 }
420
421 llvm::DIArray Elements =
422 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
423
424 // Bit size, align and offset of the type.
425 uint64_t Size = M->getContext().getTypeSize(Ty);
426 uint64_t Align = M->getContext().getTypeAlign(Ty);
427
428 llvm::DIType RealDecl =
429 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
430 Align, 0, 0, llvm::DIType(), Elements);
431
432 // Now that we have a real decl for the struct, replace anything using the
433 // old decl with the new one. This will recursively update the debug info.
434 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
435 FwdDecl.getGV()->eraseFromParent();
436
437 return RealDecl;
438}
439
Chris Lattner9c85ba32008-11-10 06:08:34 +0000440llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
441 llvm::DICompileUnit Unit) {
442 EnumDecl *Decl = Ty->getDecl();
443
444 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
445
446 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000447 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
448 EnumEnd = Decl->enumerator_end();
449 Enum != EnumEnd; ++Enum) {
450 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
451 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452 }
453
454 // Return a CompositeType for the enum itself.
455 llvm::DIArray EltArray =
456 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
457
Chris Lattner8ec03f52008-11-24 03:54:41 +0000458 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000459 SourceLocation DefLoc = Decl->getLocation();
460 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
461 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000462 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000463
464 // Size and align of the type.
465 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000466 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467
468 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
469 Unit, EnumName, DefUnit, Line,
470 Size, Align, 0, 0,
471 llvm::DIType(), EltArray);
472}
473
474llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
475 llvm::DICompileUnit Unit) {
476 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
477 return CreateType(RT, Unit);
478 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
479 return CreateType(ET, Unit);
480
481 return llvm::DIType();
482}
483
484llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
485 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000486 uint64_t Size;
487 uint64_t Align;
488
489
Nuno Lopes010d5142009-01-28 00:35:17 +0000490 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000491 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000492 Size = 0;
493 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000494 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
495 } else if (Ty->isIncompleteArrayType()) {
496 Size = 0;
497 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000498 } else {
499 // Size and align of the whole array, not the element type.
500 Size = M->getContext().getTypeSize(Ty);
501 Align = M->getContext().getTypeAlign(Ty);
502 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000503
504 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
505 // interior arrays, do we care? Why aren't nested arrays represented the
506 // obvious/recursive way?
507 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
508 QualType EltTy(Ty, 0);
509 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000510 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
512 Upper = CAT->getSize().getZExtValue() - 1;
513 // FIXME: Verify this is right for VLAs.
514 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
515 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000516 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000517
Chris Lattner9c85ba32008-11-10 06:08:34 +0000518 llvm::DIArray SubscriptArray =
519 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
520
521 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
522 Unit, "", llvm::DICompileUnit(),
523 0, Size, Align, 0, 0,
524 getOrCreateType(EltTy, Unit),
525 SubscriptArray);
526}
527
528
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000529/// getOrCreateType - Get the type from the cache or create a new
530/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
532 llvm::DICompileUnit Unit) {
533 if (Ty.isNull())
534 return llvm::DIType();
535
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000536 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000537 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
538 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000539
Chris Lattner9c85ba32008-11-10 06:08:34 +0000540 // Handle CVR qualifiers, which recursively handles what they refer to.
541 if (Ty.getCVRQualifiers())
542 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000543
544 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000545 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000546#define TYPE(Class, Base)
547#define ABSTRACT_TYPE(Class, Base)
548#define NON_CANONICAL_TYPE(Class, Base)
549#define DEPENDENT_TYPE(Class, Base) case Type::Class:
550#include "clang/AST/TypeNodes.def"
551 assert(false && "Dependent types cannot show up in debug information");
552
Chris Lattner9c85ba32008-11-10 06:08:34 +0000553 case Type::Complex:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000554 case Type::LValueReference:
555 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556 case Type::Vector:
557 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000558 case Type::ExtQual:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000559 case Type::ObjCQualifiedInterface:
560 case Type::ObjCQualifiedId:
Eli Friedman00524e32009-02-27 23:15:07 +0000561 case Type::FixedWidthInt:
562 case Type::BlockPointer:
563 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000564 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000565 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000566 case Type::ObjCQualifiedClass:
567 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000568 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000569
Devang Patele7987062009-03-02 17:58:28 +0000570 case Type::ObjCInterface:
571 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
573 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor72564e72009-02-26 23:50:07 +0000574 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
575 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000576 case Type::Enum:
577 Slot = CreateType(cast<TagType>(Ty), Unit);
578 break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000579 case Type::FunctionProto:
580 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000581 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000582
583 case Type::ConstantArray:
584 case Type::VariableArray:
585 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000586 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000587 case Type::TypeOfExpr:
588 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000589 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000590 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000591 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
592 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000593 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000594
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000595 return Slot;
596}
597
598/// EmitFunctionStart - Constructs the debug code for entering a function -
599/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000600void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000601 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000602 CGBuilderTy &Builder) {
603 // FIXME: Why is this using CurLoc???
604 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000605 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000606 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000607
608 llvm::DISubprogram SP =
609 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
610 getOrCreateType(ReturnType, Unit),
611 Fn->hasInternalLinkage(), true/*definition*/);
612
613 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
614
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000615 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000616 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000617}
618
619
Chris Lattner9c85ba32008-11-10 06:08:34 +0000620void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000621 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
622
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000623 // Don't bother if things are the same as last time.
624 SourceManager &SM = M->getContext().getSourceManager();
625 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000626 || (SM.getInstantiationLineNumber(CurLoc) ==
627 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000628 && SM.isFromSameFile(CurLoc, PrevLoc)))
629 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000630
631 // Update last state.
632 PrevLoc = CurLoc;
633
634 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000635 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000636 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
637 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000638 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000639}
640
641/// EmitRegionStart- Constructs the debug code for entering a declarative
642/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000643void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
644 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000645 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000646 D = RegionStack.back();
647 D = DebugFactory.CreateBlock(D);
648 RegionStack.push_back(D);
649 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000650}
651
652/// EmitRegionEnd - Constructs the debug code for exiting a declarative
653/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000654void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000655 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
656
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000657 // Provide an region stop point.
658 EmitStopPoint(Fn, Builder);
659
Chris Lattner9c85ba32008-11-10 06:08:34 +0000660 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000661 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000662}
663
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000664/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000665void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
666 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000667 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
668
Devang Patel07739032009-03-27 23:16:32 +0000669 // Do not emit variable debug information while generating optimized code.
670 // The llvm optimizer and code generator are not yet ready to support
671 // optimized code debugging.
672 const CompileOptions &CO = M->getCompileOpts();
673 if (CO.OptimizationLevel)
674 return;
675
Chris Lattner9c85ba32008-11-10 06:08:34 +0000676 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000677 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000678 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000679 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
680
681 // Create the descriptor for the variable.
682 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000683 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000684 Unit, Line,
685 getOrCreateType(Decl->getType(), Unit));
686 // Insert an llvm.dbg.declare into the current block.
687 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000688}
689
Chris Lattner9c85ba32008-11-10 06:08:34 +0000690void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
691 llvm::Value *Storage,
692 CGBuilderTy &Builder) {
693 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
694}
695
696/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
697/// variable declaration.
698void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
699 CGBuilderTy &Builder) {
700 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
701}
702
703
704
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000705/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000706void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
707 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000708
709 // Do not emit variable debug information while generating optimized code.
710 // The llvm optimizer and code generator are not yet ready to support
711 // optimized code debugging.
712 const CompileOptions &CO = M->getCompileOpts();
713 if (CO.OptimizationLevel)
714 return;
715
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000716 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000717 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000718 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000719 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000720
721 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000722
723 QualType T = Decl->getType();
724 if (T->isIncompleteArrayType()) {
725
726 // CodeGen turns int[] into int[1] so we'll do the same here.
727 llvm::APSInt ConstVal(32);
728
729 ConstVal = 1;
730 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
731
732 T = M->getContext().getConstantArrayType(ET, ConstVal,
733 ArrayType::Normal, 0);
734 }
735
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000737 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 Var->hasInternalLinkage(),
739 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000740}
741
Devang Patel9ca36b62009-02-26 21:10:26 +0000742/// EmitGlobalVariable - Emit information about an objective-c interface.
743void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
744 ObjCInterfaceDecl *Decl) {
745 // Create global variable debug descriptor.
746 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
747 SourceManager &SM = M->getContext().getSourceManager();
748 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
749
750 std::string Name = Decl->getNameAsString();
751
Chris Lattner03d9f342009-04-01 06:23:52 +0000752 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000753 if (T->isIncompleteArrayType()) {
754
755 // CodeGen turns int[] into int[1] so we'll do the same here.
756 llvm::APSInt ConstVal(32);
757
758 ConstVal = 1;
759 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
760
761 T = M->getContext().getConstantArrayType(ET, ConstVal,
762 ArrayType::Normal, 0);
763 }
764
765 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
766 getOrCreateType(T, Unit),
767 Var->hasInternalLinkage(),
768 true/*definition*/, Var);
769}
770