blob: eeaa197cf3bd953b5cfb2cd78fef2ab3f1c5cf74 [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)
Devang Patel2d6bb2b2009-04-23 18:09:16 +000037 : M(m), isMainCompileUnitCreated(false), 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();
Chris Lattnerf9890532009-04-19 06:50:29 +000055 unsigned FID = 0;
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
Devang Patel2d6bb2b2009-04-23 18:09:16 +000074 // See if thie compile unit is representing main source file. Each source
75 // file has corresponding compile unit. There is only one main source
76 // file at a time.
Devang Patel6fe2e152009-04-17 21:06:59 +000077 bool isMain = false;
78 const LangOptions &LO = M->getLangOptions();
79 const char *MainFileName = LO.getMainFileName();
Devang Patel2d6bb2b2009-04-23 18:09:16 +000080 if (isMainCompileUnitCreated == false) {
81 if (MainFileName) {
82 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
83 isMain = true;
84 } else {
85 if (Loc.isValid() && SM.isFromMainFile(Loc))
86 isMain = true;
87 }
88 if (isMain)
89 isMainCompileUnitCreated = true;
Devang Patel6fe2e152009-04-17 21:06:59 +000090 }
Daniel Dunbardedc94c2009-04-08 05:11:16 +000091
Chris Lattnera9aae3e2009-03-25 03:28:08 +000092 unsigned LangTag;
93 if (LO.CPlusPlus) {
94 if (LO.ObjC1)
95 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
96 else
97 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
98 } else if (LO.ObjC1) {
Devang Patel07461fa2009-03-24 20:35:51 +000099 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattnera9aae3e2009-03-25 03:28:08 +0000100 } else if (LO.C99) {
Devang Patel07461fa2009-03-24 20:35:51 +0000101 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattnera9aae3e2009-03-25 03:28:08 +0000102 } else {
103 LangTag = llvm::dwarf::DW_LANG_C89;
104 }
Devang Patel6fe2e152009-04-17 21:06:59 +0000105
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000106 // Create new compile unit.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000107 // FIXME: Do not know how to get clang version yet.
Devang Patel000ee2f2009-03-19 00:23:53 +0000108 // FIXME: Encode command line options.
109 // FIXME: Encode optimization level.
Devang Patel6fe2e152009-04-17 21:06:59 +0000110 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
111 AbsFileName.getDirname(),
Devang Patel07461fa2009-03-24 20:35:51 +0000112 "clang", isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000113}
114
Devang Patela4cc0c42009-02-25 01:36:11 +0000115/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +0000116/// one if necessary.
117llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +0000118 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000119 unsigned Encoding = 0;
120 switch (BT->getKind()) {
121 default:
122 case BuiltinType::Void:
123 return llvm::DIType();
124 case BuiltinType::UChar:
125 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
126 case BuiltinType::Char_S:
127 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
128 case BuiltinType::UShort:
129 case BuiltinType::UInt:
130 case BuiltinType::ULong:
131 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
132 case BuiltinType::Short:
133 case BuiltinType::Int:
134 case BuiltinType::Long:
135 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
136 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
137 case BuiltinType::Float:
138 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
139 }
140 // Bit size, align and offset of the type.
141 uint64_t Size = M->getContext().getTypeSize(BT);
142 uint64_t Align = M->getContext().getTypeAlign(BT);
143 uint64_t Offset = 0;
144
145 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
146 Offset, /*flags*/ 0, Encoding);
147}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000148
Chris Lattner016d55e2009-04-23 06:13:01 +0000149llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
150 llvm::DICompileUnit Unit) {
151 // Bit size, align and offset of the type.
152 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
153 if (Ty->isComplexIntegerType())
154 Encoding = llvm::dwarf::DW_ATE_lo_user;
155
156 uint64_t Size = M->getContext().getTypeSize(Ty);
157 uint64_t Align = M->getContext().getTypeAlign(Ty);
158 uint64_t Offset = 0;
159
160 return DebugFactory.CreateBasicType(Unit, "complex",
161 Unit, 0, Size, Align,
162 Offset, /*flags*/ 0, Encoding);
163}
164
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000165/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
166/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000167llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
168 // We will create one Derived type for one qualifier and recurse to handle any
169 // additional ones.
170 llvm::DIType FromTy;
171 unsigned Tag;
172 if (Ty.isConstQualified()) {
173 Tag = llvm::dwarf::DW_TAG_const_type;
174 Ty.removeConst();
175 FromTy = getOrCreateType(Ty, Unit);
176 } else if (Ty.isVolatileQualified()) {
177 Tag = llvm::dwarf::DW_TAG_volatile_type;
178 Ty.removeVolatile();
179 FromTy = getOrCreateType(Ty, Unit);
180 } else {
181 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
182 Tag = llvm::dwarf::DW_TAG_restrict_type;
183 Ty.removeRestrict();
184 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000185 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000186
Daniel Dunbar44252b42008-10-31 03:54:29 +0000187 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
188 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000189 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
190 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000191}
192
Chris Lattner562ce0a2008-11-10 06:08:34 +0000193llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
194 llvm::DICompileUnit Unit) {
195 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000196
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000197 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000198 uint64_t Size = M->getContext().getTypeSize(Ty);
199 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000200
Chris Lattner562ce0a2008-11-10 06:08:34 +0000201 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
202 "", llvm::DICompileUnit(),
203 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000204}
205
Chris Lattner562ce0a2008-11-10 06:08:34 +0000206llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
207 llvm::DICompileUnit Unit) {
208 // Typedefs are derived from some other type. If we have a typedef of a
209 // typedef, make sure to emit the whole chain.
210 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
211
212 // We don't set size information, but do specify where the typedef was
213 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000214 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000215 SourceLocation DefLoc = Ty->getDecl()->getLocation();
216 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000217
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000218 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000219 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
220 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000221
Chris Lattner562ce0a2008-11-10 06:08:34 +0000222 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
223 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000224}
225
Chris Lattner562ce0a2008-11-10 06:08:34 +0000226llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
227 llvm::DICompileUnit Unit) {
228 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000229
Chris Lattner562ce0a2008-11-10 06:08:34 +0000230 // Add the result type at least.
231 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
232
233 // Set up remainder of arguments if there is a prototype.
234 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000235 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000236 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
237 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
238 } else {
239 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000240 }
241
Chris Lattner562ce0a2008-11-10 06:08:34 +0000242 llvm::DIArray EltTypeArray =
243 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
244
245 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
246 Unit, "", llvm::DICompileUnit(),
247 0, 0, 0, 0, 0,
248 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000249}
250
Devang Patela4cc0c42009-02-25 01:36:11 +0000251/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000252llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
253 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000254 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000255
Chris Lattner562ce0a2008-11-10 06:08:34 +0000256 unsigned Tag;
257 if (Decl->isStruct())
258 Tag = llvm::dwarf::DW_TAG_structure_type;
259 else if (Decl->isUnion())
260 Tag = llvm::dwarf::DW_TAG_union_type;
261 else {
262 assert(Decl->isClass() && "Unknown RecordType!");
263 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000264 }
265
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000266 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000267
Chris Lattner562ce0a2008-11-10 06:08:34 +0000268 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000269 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000270
Chris Lattner562ce0a2008-11-10 06:08:34 +0000271 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel9b3f5f72009-04-17 21:35:15 +0000272 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
273 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000274
275 // Records and classes and unions can all be recursive. To handle them, we
276 // first generate a debug descriptor for the struct as a forward declaration.
277 // Then (if it is a definition) we go through and get debug info for all of
278 // its members. Finally, we create a descriptor for the complete type (which
279 // may refer to the forward decl if the struct is recursive) and replace all
280 // uses of the forward declaration with the final definition.
281 llvm::DIType FwdDecl =
282 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
283 llvm::DIType(), llvm::DIArray());
284
285 // If this is just a forward declaration, return it.
286 if (!Decl->getDefinition(M->getContext()))
287 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000288
Chris Lattner562ce0a2008-11-10 06:08:34 +0000289 // Otherwise, insert it into the TypeCache so that recursive uses will find
290 // it.
291 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
292
293 // Convert all the elements.
294 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
295
296 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
297
298 unsigned FieldNo = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000299 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
300 E = Decl->field_end(M->getContext());
Douglas Gregor640a04b2008-12-11 17:59:21 +0000301 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000302 FieldDecl *Field = *I;
303 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000304
305 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000306
307 // Get the location for the field.
308 SourceLocation FieldDefLoc = Field->getLocation();
309 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel9b3f5f72009-04-17 21:35:15 +0000310 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
311 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
312
Devang Patelb3a08ba2009-03-16 23:47:53 +0000313
314 QualType FType = Field->getType();
315 uint64_t FieldSize = 0;
316 unsigned FieldAlign = 0;
317 if (!FType->isIncompleteArrayType()) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000318
Devang Patelb3a08ba2009-03-16 23:47:53 +0000319 // Bit size, align and offset of the type.
320 FieldSize = M->getContext().getTypeSize(FType);
321 Expr *BitWidth = Field->getBitWidth();
322 if (BitWidth)
Eli Friedman5255e7a2009-04-26 19:19:15 +0000323 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelb3a08ba2009-03-16 23:47:53 +0000324
325 FieldAlign = M->getContext().getTypeAlign(FType);
326 }
327
Chris Lattner562ce0a2008-11-10 06:08:34 +0000328 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
329
330 // Create a DW_TAG_member node to remember the offset of this field in the
331 // struct. FIXME: This is an absolutely insane way to capture this
332 // information. When we gut debug info, this should be fixed.
333 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
334 FieldName, FieldDefUnit,
335 FieldLine, FieldSize, FieldAlign,
336 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000337 EltTys.push_back(FieldTy);
338 }
339
340 llvm::DIArray Elements =
341 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
342
343 // Bit size, align and offset of the type.
344 uint64_t Size = M->getContext().getTypeSize(Ty);
345 uint64_t Align = M->getContext().getTypeAlign(Ty);
346
347 llvm::DIType RealDecl =
348 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
349 Align, 0, 0, llvm::DIType(), Elements);
350
351 // Now that we have a real decl for the struct, replace anything using the
352 // old decl with the new one. This will recursively update the debug info.
353 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
354 FwdDecl.getGV()->eraseFromParent();
355
356 return RealDecl;
357}
358
Devang Patel3fc13e32009-02-26 21:10:26 +0000359/// CreateType - get objective-c interface type.
360llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
361 llvm::DICompileUnit Unit) {
362 ObjCInterfaceDecl *Decl = Ty->getDecl();
363
364 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
365 SourceManager &SM = M->getContext().getSourceManager();
366
367 // Get overall information about the record type for the debug info.
368 std::string Name = Decl->getNameAsString();
369
370 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel9b3f5f72009-04-17 21:35:15 +0000371 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
372 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
373
Devang Patel3fc13e32009-02-26 21:10:26 +0000374
375 // To handle recursive interface, we
376 // first generate a debug descriptor for the struct as a forward declaration.
377 // Then (if it is a definition) we go through and get debug info for all of
378 // its members. Finally, we create a descriptor for the complete type (which
379 // may refer to the forward decl if the struct is recursive) and replace all
380 // uses of the forward declaration with the final definition.
381 llvm::DIType FwdDecl =
382 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
383 llvm::DIType(), llvm::DIArray());
384
385 // If this is just a forward declaration, return it.
386 if (Decl->isForwardDecl())
387 return FwdDecl;
388
389 // Otherwise, insert it into the TypeCache so that recursive uses will find
390 // it.
391 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
392
393 // Convert all the elements.
394 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
395
Devang Patele6178df2009-03-10 21:30:26 +0000396 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
397 if (SClass) {
398 llvm::DIType SClassTy =
399 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
400 llvm::DIType InhTag =
401 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
402 Unit, "", Unit, 0, 0, 0,
403 0 /* offset */, 0, SClassTy);
404 EltTys.push_back(InhTag);
405 }
406
Devang Patel3fc13e32009-02-26 21:10:26 +0000407 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
408
409 unsigned FieldNo = 0;
410 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
411 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
412 ObjCIvarDecl *Field = *I;
413 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
414
415 std::string FieldName = Field->getNameAsString();
416
417 // Get the location for the field.
418 SourceLocation FieldDefLoc = Field->getLocation();
419 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel9b3f5f72009-04-17 21:35:15 +0000420 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
421 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
422
Devang Patel806af7d2009-03-20 18:24:39 +0000423
424 QualType FType = Field->getType();
425 uint64_t FieldSize = 0;
426 unsigned FieldAlign = 0;
Devang Patel000ee2f2009-03-19 00:23:53 +0000427
Devang Patel806af7d2009-03-20 18:24:39 +0000428 if (!FType->isIncompleteArrayType()) {
429
430 // Bit size, align and offset of the type.
431 FieldSize = M->getContext().getTypeSize(FType);
432 Expr *BitWidth = Field->getBitWidth();
433 if (BitWidth)
Eli Friedman5255e7a2009-04-26 19:19:15 +0000434 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
435
Devang Patel806af7d2009-03-20 18:24:39 +0000436 FieldAlign = M->getContext().getTypeAlign(FType);
437 }
438
439 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
440
Devang Patel000ee2f2009-03-19 00:23:53 +0000441 unsigned Flags = 0;
442 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
443 Flags = llvm::DIType::FlagProtected;
444 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
445 Flags = llvm::DIType::FlagPrivate;
446
Devang Patel3fc13e32009-02-26 21:10:26 +0000447 // Create a DW_TAG_member node to remember the offset of this field in the
448 // struct. FIXME: This is an absolutely insane way to capture this
449 // information. When we gut debug info, this should be fixed.
450 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
451 FieldName, FieldDefUnit,
452 FieldLine, FieldSize, FieldAlign,
Devang Patel000ee2f2009-03-19 00:23:53 +0000453 FieldOffset, Flags, FieldTy);
Devang Patel3fc13e32009-02-26 21:10:26 +0000454 EltTys.push_back(FieldTy);
455 }
456
457 llvm::DIArray Elements =
458 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
459
460 // Bit size, align and offset of the type.
461 uint64_t Size = M->getContext().getTypeSize(Ty);
462 uint64_t Align = M->getContext().getTypeAlign(Ty);
463
464 llvm::DIType RealDecl =
465 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
466 Align, 0, 0, llvm::DIType(), Elements);
467
468 // Now that we have a real decl for the struct, replace anything using the
469 // old decl with the new one. This will recursively update the debug info.
470 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
471 FwdDecl.getGV()->eraseFromParent();
472
473 return RealDecl;
474}
475
Chris Lattner562ce0a2008-11-10 06:08:34 +0000476llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
477 llvm::DICompileUnit Unit) {
478 EnumDecl *Decl = Ty->getDecl();
479
480 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
481
482 // Create DIEnumerator elements for each enumerator.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000483 for (EnumDecl::enumerator_iterator
484 Enum = Decl->enumerator_begin(M->getContext()),
485 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000486 Enum != EnumEnd; ++Enum) {
487 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
488 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000489 }
490
491 // Return a CompositeType for the enum itself.
492 llvm::DIArray EltArray =
493 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
494
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000495 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000496 SourceLocation DefLoc = Decl->getLocation();
497 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
498 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000499 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
500 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
501
Chris Lattner562ce0a2008-11-10 06:08:34 +0000502
503 // Size and align of the type.
504 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000505 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000506
507 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
508 Unit, EnumName, DefUnit, Line,
509 Size, Align, 0, 0,
510 llvm::DIType(), EltArray);
511}
512
513llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
514 llvm::DICompileUnit Unit) {
515 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
516 return CreateType(RT, Unit);
517 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
518 return CreateType(ET, Unit);
519
520 return llvm::DIType();
521}
522
523llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
524 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000525 uint64_t Size;
526 uint64_t Align;
527
528
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000529 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000530 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000531 Size = 0;
532 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000533 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
534 } else if (Ty->isIncompleteArrayType()) {
535 Size = 0;
536 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000537 } else {
538 // Size and align of the whole array, not the element type.
539 Size = M->getContext().getTypeSize(Ty);
540 Align = M->getContext().getTypeAlign(Ty);
541 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000542
543 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
544 // interior arrays, do we care? Why aren't nested arrays represented the
545 // obvious/recursive way?
546 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
547 QualType EltTy(Ty, 0);
548 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000549 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000550 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
551 Upper = CAT->getSize().getZExtValue() - 1;
552 // FIXME: Verify this is right for VLAs.
553 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
554 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000555 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000556
Chris Lattner562ce0a2008-11-10 06:08:34 +0000557 llvm::DIArray SubscriptArray =
558 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
559
560 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
561 Unit, "", llvm::DICompileUnit(),
562 0, Size, Align, 0, 0,
563 getOrCreateType(EltTy, Unit),
564 SubscriptArray);
565}
566
567
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000568/// getOrCreateType - Get the type from the cache or create a new
569/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000570llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
571 llvm::DICompileUnit Unit) {
572 if (Ty.isNull())
573 return llvm::DIType();
574
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000575 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000576 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
577 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000578
Chris Lattner562ce0a2008-11-10 06:08:34 +0000579 // Handle CVR qualifiers, which recursively handles what they refer to.
580 if (Ty.getCVRQualifiers())
581 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000582
583 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000584 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000585#define TYPE(Class, Base)
586#define ABSTRACT_TYPE(Class, Base)
587#define NON_CANONICAL_TYPE(Class, Base)
588#define DEPENDENT_TYPE(Class, Base) case Type::Class:
589#include "clang/AST/TypeNodes.def"
590 assert(false && "Dependent types cannot show up in debug information");
591
Sebastian Redlce6fff02009-03-16 23:22:08 +0000592 case Type::LValueReference:
593 case Type::RValueReference:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000594 case Type::Vector:
595 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000596 case Type::ExtQual:
Eli Friedman673e91b2009-02-27 23:15:07 +0000597 case Type::FixedWidthInt:
598 case Type::BlockPointer:
599 case Type::MemberPointer:
Douglas Gregordd13e842009-03-30 22:58:21 +0000600 case Type::TemplateSpecialization:
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000601 case Type::QualifiedName:
Eli Friedman673e91b2009-02-27 23:15:07 +0000602 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000603 return llvm::DIType();
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000604 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
605 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
606
607 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000608 case Type::ObjCInterface:
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000609 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
610 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattner016d55e2009-04-23 06:13:01 +0000611 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000612 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
613 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000614 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000615 case Type::Enum:
Chris Lattnereee8c7c2009-04-22 06:58:56 +0000616 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000617 case Type::FunctionProto:
618 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000619 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000620
621 case Type::ConstantArray:
622 case Type::VariableArray:
623 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000624 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000625 case Type::TypeOfExpr:
626 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000627 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000628 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000629 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
630 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000631 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000632
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000633 return Slot;
634}
635
636/// EmitFunctionStart - Constructs the debug code for entering a function -
637/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000638void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000639 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000640 CGBuilderTy &Builder) {
641 // FIXME: Why is this using CurLoc???
642 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000643 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel7f9e2092009-04-08 19:47:04 +0000644 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000645
646 llvm::DISubprogram SP =
647 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
648 getOrCreateType(ReturnType, Unit),
649 Fn->hasInternalLinkage(), true/*definition*/);
650
651 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
652
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000653 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000654 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000655}
656
657
Chris Lattner562ce0a2008-11-10 06:08:34 +0000658void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000659 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
660
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000661 // Don't bother if things are the same as last time.
662 SourceManager &SM = M->getContext().getSourceManager();
663 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000664 || (SM.getInstantiationLineNumber(CurLoc) ==
665 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000666 && SM.isFromSameFile(CurLoc, PrevLoc)))
667 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000668
669 // Update last state.
670 PrevLoc = CurLoc;
671
672 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000673 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel7f9e2092009-04-08 19:47:04 +0000674 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
675 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000676 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000677}
678
679/// EmitRegionStart- Constructs the debug code for entering a declarative
680/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000681void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
682 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000683 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000684 D = RegionStack.back();
685 D = DebugFactory.CreateBlock(D);
686 RegionStack.push_back(D);
687 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000688}
689
690/// EmitRegionEnd - Constructs the debug code for exiting a declarative
691/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000692void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000693 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
694
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000695 // Provide an region stop point.
696 EmitStopPoint(Fn, Builder);
697
Chris Lattner562ce0a2008-11-10 06:08:34 +0000698 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000699 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000700}
701
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000702/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000703void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
704 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000705 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
706
Devang Patelbcded192009-03-27 23:16:32 +0000707 // Do not emit variable debug information while generating optimized code.
708 // The llvm optimizer and code generator are not yet ready to support
709 // optimized code debugging.
710 const CompileOptions &CO = M->getCompileOpts();
711 if (CO.OptimizationLevel)
712 return;
713
Chris Lattner562ce0a2008-11-10 06:08:34 +0000714 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000715 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000716 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
717 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000718 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
719
720 // Create the descriptor for the variable.
721 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000722 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000723 Unit, Line,
724 getOrCreateType(Decl->getType(), Unit));
725 // Insert an llvm.dbg.declare into the current block.
726 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000727}
728
Chris Lattner562ce0a2008-11-10 06:08:34 +0000729void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
730 llvm::Value *Storage,
731 CGBuilderTy &Builder) {
732 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
733}
734
735/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
736/// variable declaration.
737void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
738 CGBuilderTy &Builder) {
739 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
740}
741
742
743
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000744/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000745void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
746 const VarDecl *Decl) {
Devang Patelbcded192009-03-27 23:16:32 +0000747
748 // Do not emit variable debug information while generating optimized code.
749 // The llvm optimizer and code generator are not yet ready to support
750 // optimized code debugging.
751 const CompileOptions &CO = M->getCompileOpts();
752 if (CO.OptimizationLevel)
753 return;
754
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000755 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000756 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000757 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000758 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
759 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000760
761 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000762
763 QualType T = Decl->getType();
764 if (T->isIncompleteArrayType()) {
765
766 // CodeGen turns int[] into int[1] so we'll do the same here.
767 llvm::APSInt ConstVal(32);
768
769 ConstVal = 1;
770 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
771
772 T = M->getContext().getConstantArrayType(ET, ConstVal,
773 ArrayType::Normal, 0);
774 }
775
Chris Lattner562ce0a2008-11-10 06:08:34 +0000776 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000777 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000778 Var->hasInternalLinkage(),
779 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000780}
781
Devang Patel3fc13e32009-02-26 21:10:26 +0000782/// EmitGlobalVariable - Emit information about an objective-c interface.
783void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
784 ObjCInterfaceDecl *Decl) {
785 // Create global variable debug descriptor.
786 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
787 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel9b3f5f72009-04-17 21:35:15 +0000788 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
789 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel3fc13e32009-02-26 21:10:26 +0000790
791 std::string Name = Decl->getNameAsString();
792
Chris Lattner46ee0f32009-04-01 06:23:52 +0000793 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel3fc13e32009-02-26 21:10:26 +0000794 if (T->isIncompleteArrayType()) {
795
796 // CodeGen turns int[] into int[1] so we'll do the same here.
797 llvm::APSInt ConstVal(32);
798
799 ConstVal = 1;
800 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
801
802 T = M->getContext().getConstantArrayType(ET, ConstVal,
803 ArrayType::Normal, 0);
804 }
805
806 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
807 getOrCreateType(T, Unit),
808 Var->hasInternalLinkage(),
809 true/*definition*/, Var);
810}
811