blob: 45ceb2b8e6813f23055718fa46241348343d663b [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"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000031#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000032using namespace clang;
33using namespace clang::CodeGen;
34
35CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000036 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000037}
38
Chris Lattner562ce0a2008-11-10 06:08:34 +000039CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000040 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000041}
42
Chris Lattner562ce0a2008-11-10 06:08:34 +000043void CGDebugInfo::setLocation(SourceLocation Loc) {
44 if (Loc.isValid())
Chris Lattner18c8dc02009-01-16 07:36:28 +000045 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000046}
47
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000048/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000049/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000050llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar67bb1fa2009-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 Dunbar4a66ef12008-10-24 08:38:36 +000055
Devang Patel61910072009-02-24 23:16:03 +000056 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnera9aae3e2009-03-25 03:28:08 +000057 bool isMain;
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000058 if (Loc.isValid()) {
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000059 Loc = SM.getInstantiationLoc(Loc);
60 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Chris Lattnera9aae3e2009-03-25 03:28:08 +000061 isMain = SM.getFileID(Loc) == SM.getMainFileID();
Devang Patel61910072009-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 Lattnera9aae3e2009-03-25 03:28:08 +000065 isMain = true;
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000066 }
67
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000068 // See if this compile unit has been used before.
Chris Lattner562ce0a2008-11-10 06:08:34 +000069 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
70 if (!Unit.isNull()) return Unit;
71
72 // Get source file information.
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000073 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbar2863cae2009-02-07 00:40:41 +000074 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000075
Chris Lattnera9aae3e2009-03-25 03:28:08 +000076 const LangOptions &LO = M->getLangOptions();
Daniel Dunbardedc94c2009-04-08 05:11:16 +000077
78 // If this is the main file, use the user provided main file name if
79 // specified.
80 if (isMain && LO.getMainFileName())
81 FileName = LO.getMainFileName();
82
Chris Lattnera9aae3e2009-03-25 03:28:08 +000083 unsigned LangTag;
84 if (LO.CPlusPlus) {
85 if (LO.ObjC1)
86 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
87 else
88 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
89 } else if (LO.ObjC1) {
Devang Patel07461fa2009-03-24 20:35:51 +000090 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000091 } else if (LO.C99) {
Devang Patel07461fa2009-03-24 20:35:51 +000092 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000093 } else {
94 LangTag = llvm::dwarf::DW_LANG_C89;
95 }
Devang Patel07461fa2009-03-24 20:35:51 +000096
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000097 // Create new compile unit.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000098 // FIXME: Do not know how to get clang version yet.
Devang Patel000ee2f2009-03-19 00:23:53 +000099 // FIXME: Encode command line options.
100 // FIXME: Encode optimization level.
Devang Patel07461fa2009-03-24 20:35:51 +0000101 return Unit = DebugFactory.CreateCompileUnit(LangTag, FileName, DirName,
102 "clang", isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000103}
104
Devang Patela4cc0c42009-02-25 01:36:11 +0000105/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +0000106/// one if necessary.
107llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +0000108 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000109 unsigned Encoding = 0;
110 switch (BT->getKind()) {
111 default:
112 case BuiltinType::Void:
113 return llvm::DIType();
114 case BuiltinType::UChar:
115 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
116 case BuiltinType::Char_S:
117 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
118 case BuiltinType::UShort:
119 case BuiltinType::UInt:
120 case BuiltinType::ULong:
121 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
122 case BuiltinType::Short:
123 case BuiltinType::Int:
124 case BuiltinType::Long:
125 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
126 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
127 case BuiltinType::Float:
128 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
129 }
130 // Bit size, align and offset of the type.
131 uint64_t Size = M->getContext().getTypeSize(BT);
132 uint64_t Align = M->getContext().getTypeAlign(BT);
133 uint64_t Offset = 0;
134
135 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
136 Offset, /*flags*/ 0, Encoding);
137}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000138
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000139/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
140/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000141llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
142 // We will create one Derived type for one qualifier and recurse to handle any
143 // additional ones.
144 llvm::DIType FromTy;
145 unsigned Tag;
146 if (Ty.isConstQualified()) {
147 Tag = llvm::dwarf::DW_TAG_const_type;
148 Ty.removeConst();
149 FromTy = getOrCreateType(Ty, Unit);
150 } else if (Ty.isVolatileQualified()) {
151 Tag = llvm::dwarf::DW_TAG_volatile_type;
152 Ty.removeVolatile();
153 FromTy = getOrCreateType(Ty, Unit);
154 } else {
155 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
156 Tag = llvm::dwarf::DW_TAG_restrict_type;
157 Ty.removeRestrict();
158 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000159 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000160
Daniel Dunbar44252b42008-10-31 03:54:29 +0000161 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
162 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000163 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
164 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000165}
166
Chris Lattner562ce0a2008-11-10 06:08:34 +0000167llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
168 llvm::DICompileUnit Unit) {
169 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000170
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000171 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000172 uint64_t Size = M->getContext().getTypeSize(Ty);
173 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000174
Chris Lattner562ce0a2008-11-10 06:08:34 +0000175 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
176 "", llvm::DICompileUnit(),
177 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000178}
179
Chris Lattner562ce0a2008-11-10 06:08:34 +0000180llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
181 llvm::DICompileUnit Unit) {
182 // Typedefs are derived from some other type. If we have a typedef of a
183 // typedef, make sure to emit the whole chain.
184 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
185
186 // We don't set size information, but do specify where the typedef was
187 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000188 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000189 SourceLocation DefLoc = Ty->getDecl()->getLocation();
190 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000191
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000192 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000193 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000194
Chris Lattner562ce0a2008-11-10 06:08:34 +0000195 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
196 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000197}
198
Chris Lattner562ce0a2008-11-10 06:08:34 +0000199llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
200 llvm::DICompileUnit Unit) {
201 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000202
Chris Lattner562ce0a2008-11-10 06:08:34 +0000203 // Add the result type at least.
204 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
205
206 // Set up remainder of arguments if there is a prototype.
207 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000208 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000209 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
210 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
211 } else {
212 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000213 }
214
Chris Lattner562ce0a2008-11-10 06:08:34 +0000215 llvm::DIArray EltTypeArray =
216 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
217
218 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
219 Unit, "", llvm::DICompileUnit(),
220 0, 0, 0, 0, 0,
221 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000222}
223
Devang Patela4cc0c42009-02-25 01:36:11 +0000224/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000225llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
226 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000227 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000228
Chris Lattner562ce0a2008-11-10 06:08:34 +0000229 unsigned Tag;
230 if (Decl->isStruct())
231 Tag = llvm::dwarf::DW_TAG_structure_type;
232 else if (Decl->isUnion())
233 Tag = llvm::dwarf::DW_TAG_union_type;
234 else {
235 assert(Decl->isClass() && "Unknown RecordType!");
236 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000237 }
238
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000239 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000240
Chris Lattner562ce0a2008-11-10 06:08:34 +0000241 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000242 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000243
Chris Lattner562ce0a2008-11-10 06:08:34 +0000244 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattner18c8dc02009-01-16 07:36:28 +0000245 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000246
247
248 // Records and classes and unions can all be recursive. To handle them, we
249 // first generate a debug descriptor for the struct as a forward declaration.
250 // Then (if it is a definition) we go through and get debug info for all of
251 // its members. Finally, we create a descriptor for the complete type (which
252 // may refer to the forward decl if the struct is recursive) and replace all
253 // uses of the forward declaration with the final definition.
254 llvm::DIType FwdDecl =
255 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
256 llvm::DIType(), llvm::DIArray());
257
258 // If this is just a forward declaration, return it.
259 if (!Decl->getDefinition(M->getContext()))
260 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000261
Chris Lattner562ce0a2008-11-10 06:08:34 +0000262 // Otherwise, insert it into the TypeCache so that recursive uses will find
263 // it.
264 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
265
266 // Convert all the elements.
267 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
268
269 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
270
271 unsigned FieldNo = 0;
Douglas Gregor640a04b2008-12-11 17:59:21 +0000272 for (RecordDecl::field_iterator I = Decl->field_begin(),
273 E = Decl->field_end();
274 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000275 FieldDecl *Field = *I;
276 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000277
278 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000279
280 // Get the location for the field.
281 SourceLocation FieldDefLoc = Field->getLocation();
282 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000283 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelb3a08ba2009-03-16 23:47:53 +0000284
285 QualType FType = Field->getType();
286 uint64_t FieldSize = 0;
287 unsigned FieldAlign = 0;
288 if (!FType->isIncompleteArrayType()) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000289
Devang Patelb3a08ba2009-03-16 23:47:53 +0000290 // Bit size, align and offset of the type.
291 FieldSize = M->getContext().getTypeSize(FType);
292 Expr *BitWidth = Field->getBitWidth();
293 if (BitWidth)
294 FieldSize =
295 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
296
297 FieldAlign = M->getContext().getTypeAlign(FType);
298 }
299
Chris Lattner562ce0a2008-11-10 06:08:34 +0000300 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
301
302 // Create a DW_TAG_member node to remember the offset of this field in the
303 // struct. FIXME: This is an absolutely insane way to capture this
304 // information. When we gut debug info, this should be fixed.
305 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
306 FieldName, FieldDefUnit,
307 FieldLine, FieldSize, FieldAlign,
308 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000309 EltTys.push_back(FieldTy);
310 }
311
312 llvm::DIArray Elements =
313 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
314
315 // Bit size, align and offset of the type.
316 uint64_t Size = M->getContext().getTypeSize(Ty);
317 uint64_t Align = M->getContext().getTypeAlign(Ty);
318
319 llvm::DIType RealDecl =
320 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
321 Align, 0, 0, llvm::DIType(), Elements);
322
323 // Now that we have a real decl for the struct, replace anything using the
324 // old decl with the new one. This will recursively update the debug info.
325 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
326 FwdDecl.getGV()->eraseFromParent();
327
328 return RealDecl;
329}
330
Devang Patel3fc13e32009-02-26 21:10:26 +0000331/// CreateType - get objective-c interface type.
332llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
333 llvm::DICompileUnit Unit) {
334 ObjCInterfaceDecl *Decl = Ty->getDecl();
335
336 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
337 SourceManager &SM = M->getContext().getSourceManager();
338
339 // Get overall information about the record type for the debug info.
340 std::string Name = Decl->getNameAsString();
341
342 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
343 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
344
345
346 // To handle recursive interface, we
347 // first generate a debug descriptor for the struct as a forward declaration.
348 // Then (if it is a definition) we go through and get debug info for all of
349 // its members. Finally, we create a descriptor for the complete type (which
350 // may refer to the forward decl if the struct is recursive) and replace all
351 // uses of the forward declaration with the final definition.
352 llvm::DIType FwdDecl =
353 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
354 llvm::DIType(), llvm::DIArray());
355
356 // If this is just a forward declaration, return it.
357 if (Decl->isForwardDecl())
358 return FwdDecl;
359
360 // Otherwise, insert it into the TypeCache so that recursive uses will find
361 // it.
362 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
363
364 // Convert all the elements.
365 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
366
Devang Patele6178df2009-03-10 21:30:26 +0000367 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
368 if (SClass) {
369 llvm::DIType SClassTy =
370 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
371 llvm::DIType InhTag =
372 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
373 Unit, "", Unit, 0, 0, 0,
374 0 /* offset */, 0, SClassTy);
375 EltTys.push_back(InhTag);
376 }
377
Devang Patel3fc13e32009-02-26 21:10:26 +0000378 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
379
380 unsigned FieldNo = 0;
381 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
382 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
383 ObjCIvarDecl *Field = *I;
384 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
385
386 std::string FieldName = Field->getNameAsString();
387
388 // Get the location for the field.
389 SourceLocation FieldDefLoc = Field->getLocation();
390 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
391 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patel806af7d2009-03-20 18:24:39 +0000392
393 QualType FType = Field->getType();
394 uint64_t FieldSize = 0;
395 unsigned FieldAlign = 0;
Devang Patel000ee2f2009-03-19 00:23:53 +0000396
Devang Patel806af7d2009-03-20 18:24:39 +0000397 if (!FType->isIncompleteArrayType()) {
398
399 // Bit size, align and offset of the type.
400 FieldSize = M->getContext().getTypeSize(FType);
401 Expr *BitWidth = Field->getBitWidth();
402 if (BitWidth)
403 FieldSize =
404 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
405
406 FieldAlign = M->getContext().getTypeAlign(FType);
407 }
408
409 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
410
Devang Patel000ee2f2009-03-19 00:23:53 +0000411 unsigned Flags = 0;
412 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
413 Flags = llvm::DIType::FlagProtected;
414 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
415 Flags = llvm::DIType::FlagPrivate;
416
Devang Patel3fc13e32009-02-26 21:10:26 +0000417 // Create a DW_TAG_member node to remember the offset of this field in the
418 // struct. FIXME: This is an absolutely insane way to capture this
419 // information. When we gut debug info, this should be fixed.
420 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
421 FieldName, FieldDefUnit,
422 FieldLine, FieldSize, FieldAlign,
Devang Patel000ee2f2009-03-19 00:23:53 +0000423 FieldOffset, Flags, FieldTy);
Devang Patel3fc13e32009-02-26 21:10:26 +0000424 EltTys.push_back(FieldTy);
425 }
426
427 llvm::DIArray Elements =
428 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
429
430 // Bit size, align and offset of the type.
431 uint64_t Size = M->getContext().getTypeSize(Ty);
432 uint64_t Align = M->getContext().getTypeAlign(Ty);
433
434 llvm::DIType RealDecl =
435 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
436 Align, 0, 0, llvm::DIType(), Elements);
437
438 // Now that we have a real decl for the struct, replace anything using the
439 // old decl with the new one. This will recursively update the debug info.
440 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
441 FwdDecl.getGV()->eraseFromParent();
442
443 return RealDecl;
444}
445
Chris Lattner562ce0a2008-11-10 06:08:34 +0000446llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
447 llvm::DICompileUnit Unit) {
448 EnumDecl *Decl = Ty->getDecl();
449
450 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
451
452 // Create DIEnumerator elements for each enumerator.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000453 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
454 EnumEnd = Decl->enumerator_end();
455 Enum != EnumEnd; ++Enum) {
456 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
457 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000458 }
459
460 // Return a CompositeType for the enum itself.
461 llvm::DIArray EltArray =
462 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
463
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000464 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000465 SourceLocation DefLoc = Decl->getLocation();
466 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
467 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000468 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000469
470 // Size and align of the type.
471 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000472 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000473
474 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
475 Unit, EnumName, DefUnit, Line,
476 Size, Align, 0, 0,
477 llvm::DIType(), EltArray);
478}
479
480llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
481 llvm::DICompileUnit Unit) {
482 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
483 return CreateType(RT, Unit);
484 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
485 return CreateType(ET, Unit);
486
487 return llvm::DIType();
488}
489
490llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
491 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000492 uint64_t Size;
493 uint64_t Align;
494
495
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000496 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000497 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000498 Size = 0;
499 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000500 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
501 } else if (Ty->isIncompleteArrayType()) {
502 Size = 0;
503 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000504 } else {
505 // Size and align of the whole array, not the element type.
506 Size = M->getContext().getTypeSize(Ty);
507 Align = M->getContext().getTypeAlign(Ty);
508 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000509
510 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
511 // interior arrays, do we care? Why aren't nested arrays represented the
512 // obvious/recursive way?
513 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
514 QualType EltTy(Ty, 0);
515 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000516 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000517 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
518 Upper = CAT->getSize().getZExtValue() - 1;
519 // FIXME: Verify this is right for VLAs.
520 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
521 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000522 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000523
Chris Lattner562ce0a2008-11-10 06:08:34 +0000524 llvm::DIArray SubscriptArray =
525 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
526
527 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
528 Unit, "", llvm::DICompileUnit(),
529 0, Size, Align, 0, 0,
530 getOrCreateType(EltTy, Unit),
531 SubscriptArray);
532}
533
534
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000535/// getOrCreateType - Get the type from the cache or create a new
536/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000537llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
538 llvm::DICompileUnit Unit) {
539 if (Ty.isNull())
540 return llvm::DIType();
541
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000542 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000543 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
544 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000545
Chris Lattner562ce0a2008-11-10 06:08:34 +0000546 // Handle CVR qualifiers, which recursively handles what they refer to.
547 if (Ty.getCVRQualifiers())
548 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000549
550 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000551 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000552#define TYPE(Class, Base)
553#define ABSTRACT_TYPE(Class, Base)
554#define NON_CANONICAL_TYPE(Class, Base)
555#define DEPENDENT_TYPE(Class, Base) case Type::Class:
556#include "clang/AST/TypeNodes.def"
557 assert(false && "Dependent types cannot show up in debug information");
558
Chris Lattner562ce0a2008-11-10 06:08:34 +0000559 case Type::Complex:
Sebastian Redlce6fff02009-03-16 23:22:08 +0000560 case Type::LValueReference:
561 case Type::RValueReference:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000562 case Type::Vector:
563 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000564 case Type::ExtQual:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000565 case Type::ObjCQualifiedInterface:
566 case Type::ObjCQualifiedId:
Eli Friedman673e91b2009-02-27 23:15:07 +0000567 case Type::FixedWidthInt:
568 case Type::BlockPointer:
569 case Type::MemberPointer:
Douglas Gregordd13e842009-03-30 22:58:21 +0000570 case Type::TemplateSpecialization:
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000571 case Type::QualifiedName:
Eli Friedman673e91b2009-02-27 23:15:07 +0000572 case Type::ObjCQualifiedClass:
573 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000574 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000575
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000576 case Type::ObjCInterface:
577 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000578 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
579 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000580 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
581 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000582 case Type::Enum:
583 Slot = CreateType(cast<TagType>(Ty), Unit);
584 break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000585 case Type::FunctionProto:
586 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000587 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000588
589 case Type::ConstantArray:
590 case Type::VariableArray:
591 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000592 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000593 case Type::TypeOfExpr:
594 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000595 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000596 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000597 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
598 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000599 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000600
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000601 return Slot;
602}
603
604/// EmitFunctionStart - Constructs the debug code for entering a function -
605/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000606void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000607 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000608 CGBuilderTy &Builder) {
609 // FIXME: Why is this using CurLoc???
610 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000611 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000612 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000613
614 llvm::DISubprogram SP =
615 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
616 getOrCreateType(ReturnType, Unit),
617 Fn->hasInternalLinkage(), true/*definition*/);
618
619 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
620
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000621 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000622 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000623}
624
625
Chris Lattner562ce0a2008-11-10 06:08:34 +0000626void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000627 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
628
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000629 // Don't bother if things are the same as last time.
630 SourceManager &SM = M->getContext().getSourceManager();
631 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000632 || (SM.getInstantiationLineNumber(CurLoc) ==
633 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000634 && SM.isFromSameFile(CurLoc, PrevLoc)))
635 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000636
637 // Update last state.
638 PrevLoc = CurLoc;
639
640 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000641 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000642 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
643 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000644 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000645}
646
647/// EmitRegionStart- Constructs the debug code for entering a declarative
648/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000649void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
650 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000651 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000652 D = RegionStack.back();
653 D = DebugFactory.CreateBlock(D);
654 RegionStack.push_back(D);
655 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000656}
657
658/// EmitRegionEnd - Constructs the debug code for exiting a declarative
659/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000660void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000661 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
662
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000663 // Provide an region stop point.
664 EmitStopPoint(Fn, Builder);
665
Chris Lattner562ce0a2008-11-10 06:08:34 +0000666 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000667 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000668}
669
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000670/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000671void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
672 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000673 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
674
Devang Patelbcded192009-03-27 23:16:32 +0000675 // Do not emit variable debug information while generating optimized code.
676 // The llvm optimizer and code generator are not yet ready to support
677 // optimized code debugging.
678 const CompileOptions &CO = M->getCompileOpts();
679 if (CO.OptimizationLevel)
680 return;
681
Chris Lattner562ce0a2008-11-10 06:08:34 +0000682 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000683 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000684 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000685 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
686
687 // Create the descriptor for the variable.
688 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000689 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000690 Unit, Line,
691 getOrCreateType(Decl->getType(), Unit));
692 // Insert an llvm.dbg.declare into the current block.
693 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000694}
695
Chris Lattner562ce0a2008-11-10 06:08:34 +0000696void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
697 llvm::Value *Storage,
698 CGBuilderTy &Builder) {
699 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
700}
701
702/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
703/// variable declaration.
704void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
705 CGBuilderTy &Builder) {
706 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
707}
708
709
710
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000711/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000712void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
713 const VarDecl *Decl) {
Devang Patelbcded192009-03-27 23:16:32 +0000714
715 // Do not emit variable debug information while generating optimized code.
716 // The llvm optimizer and code generator are not yet ready to support
717 // optimized code debugging.
718 const CompileOptions &CO = M->getCompileOpts();
719 if (CO.OptimizationLevel)
720 return;
721
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000722 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000723 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000724 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000725 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000726
727 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000728
729 QualType T = Decl->getType();
730 if (T->isIncompleteArrayType()) {
731
732 // CodeGen turns int[] into int[1] so we'll do the same here.
733 llvm::APSInt ConstVal(32);
734
735 ConstVal = 1;
736 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
737
738 T = M->getContext().getConstantArrayType(ET, ConstVal,
739 ArrayType::Normal, 0);
740 }
741
Chris Lattner562ce0a2008-11-10 06:08:34 +0000742 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000743 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000744 Var->hasInternalLinkage(),
745 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000746}
747
Devang Patel3fc13e32009-02-26 21:10:26 +0000748/// EmitGlobalVariable - Emit information about an objective-c interface.
749void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
750 ObjCInterfaceDecl *Decl) {
751 // Create global variable debug descriptor.
752 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
753 SourceManager &SM = M->getContext().getSourceManager();
754 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
755
756 std::string Name = Decl->getNameAsString();
757
Chris Lattner46ee0f32009-04-01 06:23:52 +0000758 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel3fc13e32009-02-26 21:10:26 +0000759 if (T->isIncompleteArrayType()) {
760
761 // CodeGen turns int[] into int[1] so we'll do the same here.
762 llvm::APSInt ConstVal(32);
763
764 ConstVal = 1;
765 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
766
767 T = M->getContext().getConstantArrayType(ET, ConstVal,
768 ArrayType::Normal, 0);
769 }
770
771 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
772 getOrCreateType(T, Unit),
773 Var->hasInternalLinkage(),
774 true/*definition*/, Var);
775}
776