CGDebugInfo.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. //===--- 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. #include "CGDebugInfo.h"
  14. #include "CodeGenModule.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Decl.h"
  17. #include "clang/AST/Expr.h"
  18. #include "clang/AST/RecordLayout.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Basic/FileManager.h"
  21. #include "llvm/Constants.h"
  22. #include "llvm/DerivedTypes.h"
  23. #include "llvm/Instructions.h"
  24. #include "llvm/Intrinsics.h"
  25. #include "llvm/Module.h"
  26. #include "llvm/ADT/StringExtras.h"
  27. #include "llvm/ADT/SmallVector.h"
  28. #include "llvm/Support/Dwarf.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. using namespace clang;
  31. using namespace clang::CodeGen;
  32. CGDebugInfo::CGDebugInfo(CodeGenModule *m)
  33. : M(m), DebugFactory(M->getModule()) {
  34. }
  35. CGDebugInfo::~CGDebugInfo() {
  36. assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
  37. }
  38. void CGDebugInfo::setLocation(SourceLocation Loc) {
  39. if (Loc.isValid())
  40. CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
  41. }
  42. /// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
  43. /// one if necessary. This returns null for invalid source locations.
  44. llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
  45. if (Loc.isInvalid())
  46. return llvm::DICompileUnit();
  47. SourceManager &SM = M->getContext().getSourceManager();
  48. const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc));
  49. if (FE == 0) return llvm::DICompileUnit();
  50. // See if this compile unit has been used before.
  51. llvm::DICompileUnit &Unit = CompileUnitCache[FE];
  52. if (!Unit.isNull()) return Unit;
  53. // Get source file information.
  54. const char *FileName = FE->getName();
  55. const char *DirName = FE->getDir()->getName();
  56. // Create new compile unit.
  57. // FIXME: Handle other language IDs as well.
  58. // FIXME: Do not know how to get clang version yet.
  59. return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
  60. FileName, DirName, "clang");
  61. }
  62. /// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
  63. /// one if necessary.
  64. llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
  65. llvm::DICompileUnit Unit){
  66. unsigned Encoding = 0;
  67. switch (BT->getKind()) {
  68. default:
  69. case BuiltinType::Void:
  70. return llvm::DIType();
  71. case BuiltinType::UChar:
  72. case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
  73. case BuiltinType::Char_S:
  74. case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
  75. case BuiltinType::UShort:
  76. case BuiltinType::UInt:
  77. case BuiltinType::ULong:
  78. case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
  79. case BuiltinType::Short:
  80. case BuiltinType::Int:
  81. case BuiltinType::Long:
  82. case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
  83. case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
  84. case BuiltinType::Float:
  85. case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
  86. }
  87. // Bit size, align and offset of the type.
  88. uint64_t Size = M->getContext().getTypeSize(BT);
  89. uint64_t Align = M->getContext().getTypeAlign(BT);
  90. uint64_t Offset = 0;
  91. return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
  92. Offset, /*flags*/ 0, Encoding);
  93. }
  94. /// getOrCreateCVRType - Get the CVR qualified type from the cache or create
  95. /// a new one if necessary.
  96. llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
  97. // We will create one Derived type for one qualifier and recurse to handle any
  98. // additional ones.
  99. llvm::DIType FromTy;
  100. unsigned Tag;
  101. if (Ty.isConstQualified()) {
  102. Tag = llvm::dwarf::DW_TAG_const_type;
  103. Ty.removeConst();
  104. FromTy = getOrCreateType(Ty, Unit);
  105. } else if (Ty.isVolatileQualified()) {
  106. Tag = llvm::dwarf::DW_TAG_volatile_type;
  107. Ty.removeVolatile();
  108. FromTy = getOrCreateType(Ty, Unit);
  109. } else {
  110. assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
  111. Tag = llvm::dwarf::DW_TAG_restrict_type;
  112. Ty.removeRestrict();
  113. FromTy = getOrCreateType(Ty, Unit);
  114. }
  115. // No need to fill in the Name, Line, Size, Alignment, Offset in case of
  116. // CVR derived types.
  117. return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
  118. 0, 0, 0, 0, 0, FromTy);
  119. }
  120. llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
  121. llvm::DICompileUnit Unit) {
  122. llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
  123. // Bit size, align and offset of the type.
  124. uint64_t Size = M->getContext().getTypeSize(Ty);
  125. uint64_t Align = M->getContext().getTypeAlign(Ty);
  126. return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
  127. "", llvm::DICompileUnit(),
  128. 0, Size, Align, 0, 0, EltTy);
  129. }
  130. llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
  131. llvm::DICompileUnit Unit) {
  132. // Typedefs are derived from some other type. If we have a typedef of a
  133. // typedef, make sure to emit the whole chain.
  134. llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
  135. // We don't set size information, but do specify where the typedef was
  136. // declared.
  137. std::string TyName = Ty->getDecl()->getNameAsString();
  138. SourceLocation DefLoc = Ty->getDecl()->getLocation();
  139. llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
  140. SourceManager &SM = M->getContext().getSourceManager();
  141. uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
  142. return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
  143. TyName, DefUnit, Line, 0, 0, 0, 0, Src);
  144. }
  145. llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
  146. llvm::DICompileUnit Unit) {
  147. llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
  148. // Add the result type at least.
  149. EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
  150. // Set up remainder of arguments if there is a prototype.
  151. // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
  152. if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
  153. for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
  154. EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
  155. } else {
  156. // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
  157. }
  158. llvm::DIArray EltTypeArray =
  159. DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
  160. return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
  161. Unit, "", llvm::DICompileUnit(),
  162. 0, 0, 0, 0, 0,
  163. llvm::DIType(), EltTypeArray);
  164. }
  165. /// getOrCreateRecordType - get structure or union type.
  166. llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
  167. llvm::DICompileUnit Unit) {
  168. RecordDecl *Decl = Ty->getDecl();
  169. unsigned Tag;
  170. if (Decl->isStruct())
  171. Tag = llvm::dwarf::DW_TAG_structure_type;
  172. else if (Decl->isUnion())
  173. Tag = llvm::dwarf::DW_TAG_union_type;
  174. else {
  175. assert(Decl->isClass() && "Unknown RecordType!");
  176. Tag = llvm::dwarf::DW_TAG_class_type;
  177. }
  178. SourceManager &SM = M->getContext().getSourceManager();
  179. // Get overall information about the record type for the debug info.
  180. std::string Name = Decl->getNameAsString();
  181. llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
  182. unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
  183. // Records and classes and unions can all be recursive. To handle them, we
  184. // first generate a debug descriptor for the struct as a forward declaration.
  185. // Then (if it is a definition) we go through and get debug info for all of
  186. // its members. Finally, we create a descriptor for the complete type (which
  187. // may refer to the forward decl if the struct is recursive) and replace all
  188. // uses of the forward declaration with the final definition.
  189. llvm::DIType FwdDecl =
  190. DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
  191. llvm::DIType(), llvm::DIArray());
  192. // If this is just a forward declaration, return it.
  193. if (!Decl->getDefinition(M->getContext()))
  194. return FwdDecl;
  195. // Otherwise, insert it into the TypeCache so that recursive uses will find
  196. // it.
  197. TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
  198. // Convert all the elements.
  199. llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
  200. const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
  201. unsigned FieldNo = 0;
  202. for (RecordDecl::field_iterator I = Decl->field_begin(),
  203. E = Decl->field_end();
  204. I != E; ++I, ++FieldNo) {
  205. FieldDecl *Field = *I;
  206. llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
  207. std::string FieldName = Field->getNameAsString();
  208. // Get the location for the field.
  209. SourceLocation FieldDefLoc = Field->getLocation();
  210. llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
  211. unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
  212. // Bit size, align and offset of the type.
  213. uint64_t FieldSize = M->getContext().getTypeSize(Ty);
  214. unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
  215. uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
  216. // Create a DW_TAG_member node to remember the offset of this field in the
  217. // struct. FIXME: This is an absolutely insane way to capture this
  218. // information. When we gut debug info, this should be fixed.
  219. FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
  220. FieldName, FieldDefUnit,
  221. FieldLine, FieldSize, FieldAlign,
  222. FieldOffset, 0, FieldTy);
  223. EltTys.push_back(FieldTy);
  224. }
  225. llvm::DIArray Elements =
  226. DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
  227. // Bit size, align and offset of the type.
  228. uint64_t Size = M->getContext().getTypeSize(Ty);
  229. uint64_t Align = M->getContext().getTypeAlign(Ty);
  230. llvm::DIType RealDecl =
  231. DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
  232. Align, 0, 0, llvm::DIType(), Elements);
  233. // Now that we have a real decl for the struct, replace anything using the
  234. // old decl with the new one. This will recursively update the debug info.
  235. FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
  236. FwdDecl.getGV()->eraseFromParent();
  237. return RealDecl;
  238. }
  239. llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
  240. llvm::DICompileUnit Unit) {
  241. EnumDecl *Decl = Ty->getDecl();
  242. llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
  243. // Create DIEnumerator elements for each enumerator.
  244. for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
  245. EnumEnd = Decl->enumerator_end();
  246. Enum != EnumEnd; ++Enum) {
  247. Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
  248. Enum->getInitVal().getZExtValue()));
  249. }
  250. // Return a CompositeType for the enum itself.
  251. llvm::DIArray EltArray =
  252. DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
  253. std::string EnumName = Decl->getNameAsString();
  254. SourceLocation DefLoc = Decl->getLocation();
  255. llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
  256. SourceManager &SM = M->getContext().getSourceManager();
  257. unsigned Line = SM.getInstantiationLineNumber(DefLoc);
  258. // Size and align of the type.
  259. uint64_t Size = M->getContext().getTypeSize(Ty);
  260. unsigned Align = M->getContext().getTypeAlign(Ty);
  261. return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
  262. Unit, EnumName, DefUnit, Line,
  263. Size, Align, 0, 0,
  264. llvm::DIType(), EltArray);
  265. }
  266. llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
  267. llvm::DICompileUnit Unit) {
  268. if (const RecordType *RT = dyn_cast<RecordType>(Ty))
  269. return CreateType(RT, Unit);
  270. else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
  271. return CreateType(ET, Unit);
  272. return llvm::DIType();
  273. }
  274. llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
  275. llvm::DICompileUnit Unit) {
  276. uint64_t Size;
  277. uint64_t Align;
  278. if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
  279. Size = 0;
  280. Align =
  281. M->getContext().getTypeSize(M->getContext().getBaseElementType(VAT));
  282. } else {
  283. // Size and align of the whole array, not the element type.
  284. Size = M->getContext().getTypeSize(Ty);
  285. Align = M->getContext().getTypeAlign(Ty);
  286. }
  287. // Add the dimensions of the array. FIXME: This loses CV qualifiers from
  288. // interior arrays, do we care? Why aren't nested arrays represented the
  289. // obvious/recursive way?
  290. llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
  291. QualType EltTy(Ty, 0);
  292. while ((Ty = dyn_cast<ArrayType>(EltTy))) {
  293. uint64_t Upper = 0;
  294. if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
  295. Upper = CAT->getSize().getZExtValue() - 1;
  296. // FIXME: Verify this is right for VLAs.
  297. Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
  298. EltTy = Ty->getElementType();
  299. }
  300. llvm::DIArray SubscriptArray =
  301. DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
  302. return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
  303. Unit, "", llvm::DICompileUnit(),
  304. 0, Size, Align, 0, 0,
  305. getOrCreateType(EltTy, Unit),
  306. SubscriptArray);
  307. }
  308. /// getOrCreateType - Get the type from the cache or create a new
  309. /// one if necessary.
  310. llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
  311. llvm::DICompileUnit Unit) {
  312. if (Ty.isNull())
  313. return llvm::DIType();
  314. // Check to see if the compile unit already has created this type.
  315. llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
  316. if (!Slot.isNull()) return Slot;
  317. // Handle CVR qualifiers, which recursively handles what they refer to.
  318. if (Ty.getCVRQualifiers())
  319. return Slot = CreateCVRType(Ty, Unit);
  320. // Work out details of type.
  321. switch (Ty->getTypeClass()) {
  322. case Type::Complex:
  323. case Type::Reference:
  324. case Type::Vector:
  325. case Type::ExtVector:
  326. case Type::ASQual:
  327. case Type::ObjCInterface:
  328. case Type::ObjCQualifiedInterface:
  329. case Type::ObjCQualifiedId:
  330. default:
  331. return llvm::DIType();
  332. case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
  333. case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
  334. case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
  335. case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
  336. case Type::FunctionProto:
  337. case Type::FunctionNoProto:
  338. return Slot = CreateType(cast<FunctionType>(Ty), Unit);
  339. case Type::ConstantArray:
  340. case Type::VariableArray:
  341. case Type::IncompleteArray:
  342. return Slot = CreateType(cast<ArrayType>(Ty), Unit);
  343. case Type::TypeOfExp:
  344. return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr()
  345. ->getType(), Unit);
  346. case Type::TypeOfTyp:
  347. return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
  348. Unit);
  349. }
  350. return Slot;
  351. }
  352. /// EmitFunctionStart - Constructs the debug code for entering a function -
  353. /// "llvm.dbg.func.start.".
  354. void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
  355. llvm::Function *Fn,
  356. CGBuilderTy &Builder) {
  357. // FIXME: Why is this using CurLoc???
  358. llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
  359. SourceManager &SM = M->getContext().getSourceManager();
  360. unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
  361. llvm::DISubprogram SP =
  362. DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
  363. getOrCreateType(ReturnType, Unit),
  364. Fn->hasInternalLinkage(), true/*definition*/);
  365. DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
  366. // Push function on region stack.
  367. RegionStack.push_back(SP);
  368. }
  369. void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
  370. if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
  371. // Don't bother if things are the same as last time.
  372. SourceManager &SM = M->getContext().getSourceManager();
  373. if (CurLoc == PrevLoc
  374. || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
  375. && SM.isFromSameFile(CurLoc, PrevLoc)))
  376. return;
  377. // Update last state.
  378. PrevLoc = CurLoc;
  379. // Get the appropriate compile unit.
  380. llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
  381. DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
  382. SM.getInstantiationColumnNumber(CurLoc),
  383. Builder.GetInsertBlock());
  384. }
  385. /// EmitRegionStart- Constructs the debug code for entering a declarative
  386. /// region - "llvm.dbg.region.start.".
  387. void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
  388. llvm::DIDescriptor D;
  389. if (!RegionStack.empty())
  390. D = RegionStack.back();
  391. D = DebugFactory.CreateBlock(D);
  392. RegionStack.push_back(D);
  393. DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
  394. }
  395. /// EmitRegionEnd - Constructs the debug code for exiting a declarative
  396. /// region - "llvm.dbg.region.end."
  397. void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
  398. assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
  399. // Provide an region stop point.
  400. EmitStopPoint(Fn, Builder);
  401. DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
  402. RegionStack.pop_back();
  403. }
  404. /// EmitDeclare - Emit local variable declaration debug info.
  405. void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
  406. llvm::Value *Storage, CGBuilderTy &Builder) {
  407. assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
  408. // Get location information.
  409. SourceManager &SM = M->getContext().getSourceManager();
  410. unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
  411. llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
  412. // Create the descriptor for the variable.
  413. llvm::DIVariable D =
  414. DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
  415. Unit, Line,
  416. getOrCreateType(Decl->getType(), Unit));
  417. // Insert an llvm.dbg.declare into the current block.
  418. DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
  419. }
  420. void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
  421. llvm::Value *Storage,
  422. CGBuilderTy &Builder) {
  423. EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
  424. }
  425. /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
  426. /// variable declaration.
  427. void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
  428. CGBuilderTy &Builder) {
  429. EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
  430. }
  431. /// EmitGlobalVariable - Emit information about a global variable.
  432. void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
  433. const VarDecl *Decl) {
  434. // Create global variable debug descriptor.
  435. llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
  436. SourceManager &SM = M->getContext().getSourceManager();
  437. unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
  438. std::string Name = Decl->getNameAsString();
  439. QualType T = Decl->getType();
  440. if (T->isIncompleteArrayType()) {
  441. // CodeGen turns int[] into int[1] so we'll do the same here.
  442. llvm::APSInt ConstVal(32);
  443. ConstVal = 1;
  444. QualType ET = M->getContext().getAsArrayType(T)->getElementType();
  445. T = M->getContext().getConstantArrayType(ET, ConstVal,
  446. ArrayType::Normal, 0);
  447. }
  448. DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
  449. getOrCreateType(T, Unit),
  450. Var->hasInternalLinkage(),
  451. true/*definition*/, Var);
  452. }