blob: 574d2fef1196b318451df92a92eca4affb4bd4f4 [file] [log] [blame]
Chris Lattner6d389332008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
Bill Wendlinge38859d2012-06-28 00:05:13 +000015#include "llvm/DebugInfo.h"
Chris Lattner6d389332008-11-10 02:56:27 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin89261392008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattner6d389332008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patel80ae3492009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman2637cc12010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.h"
Dan Gohman16a5d982010-08-20 22:02:26 +000025#include "llvm/ADT/STLExtras.h"
David Greene83d4781452009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis97324ce2009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.h"
Chris Lattner91922252009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner6d389332008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis97324ce2009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattner6d389332008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Devang Patel33a2cdf2010-08-02 22:51:46 +000036DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37}
38
39DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40}
41
Eric Christopher6647b832011-10-11 22:59:11 +000042DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43}
44
Devang Patel33a2cdf2010-08-02 22:51:46 +000045DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
46}
47
48DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
49}
50
51DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52}
53
Jim Grosbach6cd0deb2010-07-21 21:36:25 +000054StringRef
Devang Patelb2969422009-09-29 18:40:58 +000055DIDescriptor::getStringField(unsigned Elt) const {
Devang Patel80ae3492009-08-28 23:24:31 +000056 if (DbgNode == 0)
Devang Patel2d9caf92009-11-25 17:36:49 +000057 return StringRef();
Chris Lattner6d389332008-11-10 02:56:27 +000058
Chris Lattner9b493022009-12-31 01:22:29 +000059 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel2d9caf92009-11-25 17:36:49 +000061 return MDS->getString();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000062
Devang Patel2d9caf92009-11-25 17:36:49 +000063 return StringRef();
Chris Lattner6d389332008-11-10 02:56:27 +000064}
65
66uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000067 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000068 return 0;
Bill Wendling6ddc11a2009-05-14 18:26:15 +000069
Chris Lattner9b493022009-12-31 01:22:29 +000070 if (Elt < DbgNode->getNumOperands())
Eric Christophera4a0cf82012-03-16 00:21:54 +000071 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patel80ae3492009-08-28 23:24:31 +000072 return CI->getZExtValue();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000073
Chris Lattner6d389332008-11-10 02:56:27 +000074 return 0;
75}
76
Chris Lattner6d389332008-11-10 02:56:27 +000077DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000078 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000079 return DIDescriptor();
Bill Wendling6ddc11a2009-05-14 18:26:15 +000080
Chris Lattner743bdca2010-03-31 05:53:47 +000081 if (Elt < DbgNode->getNumOperands())
Jim Grosbach6cd0deb2010-07-21 21:36:25 +000082 return
83 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patel80ae3492009-08-28 23:24:31 +000084 return DIDescriptor();
Chris Lattner6d389332008-11-10 02:56:27 +000085}
86
87GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000088 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000089 return 0;
Bill Wendling6ddc11a2009-05-14 18:26:15 +000090
Chris Lattner9b493022009-12-31 01:22:29 +000091 if (Elt < DbgNode->getNumOperands())
92 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patel80ae3492009-08-28 23:24:31 +000093 return 0;
Chris Lattner6d389332008-11-10 02:56:27 +000094}
95
Devang Patelc7cf14f2010-08-09 21:39:24 +000096Constant *DIDescriptor::getConstantField(unsigned Elt) const {
97 if (DbgNode == 0)
98 return 0;
99
100 if (Elt < DbgNode->getNumOperands())
101 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
102 return 0;
103}
104
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000105Function *DIDescriptor::getFunctionField(unsigned Elt) const {
106 if (DbgNode == 0)
107 return 0;
108
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
111 return 0;
112}
113
Alexey Samsonov3b861ec2012-10-09 08:13:15 +0000114void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
115 if (DbgNode == 0)
116 return;
117
118 if (Elt < DbgNode->getNumOperands()) {
119 MDNode *Node = const_cast<MDNode*>(DbgNode);
120 Node->replaceOperandWith(Elt, F);
121 }
122}
123
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000124unsigned DIVariable::getNumAddrElements() const {
Bill Wendling16d944c2012-07-06 17:46:28 +0000125 if (getVersion() <= LLVMDebugVersion8)
Devang Patelbea08d12010-09-29 23:07:21 +0000126 return DbgNode->getNumOperands()-6;
Bill Wendling16d944c2012-07-06 17:46:28 +0000127 if (getVersion() == LLVMDebugVersion9)
Devang Patelcfa82a32011-07-19 19:41:54 +0000128 return DbgNode->getNumOperands()-7;
129 return DbgNode->getNumOperands()-8;
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000130}
131
Devang Patel8fb9fd62011-07-20 22:18:50 +0000132/// getInlinedAt - If this variable is inlined then return inline location.
Devang Patel3d6e3892011-08-09 01:03:14 +0000133MDNode *DIVariable::getInlinedAt() const {
Bill Wendling16d944c2012-07-06 17:46:28 +0000134 if (getVersion() <= LLVMDebugVersion9)
Devang Patel8fb9fd62011-07-20 22:18:50 +0000135 return NULL;
136 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
137}
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000138
Chris Lattner6d389332008-11-10 02:56:27 +0000139//===----------------------------------------------------------------------===//
Devang Patel9fda4bd2009-08-31 18:49:10 +0000140// Predicates
Chris Lattner6d389332008-11-10 02:56:27 +0000141//===----------------------------------------------------------------------===//
142
Devang Patel9fda4bd2009-08-31 18:49:10 +0000143/// isBasicType - Return true if the specified tag is legal for
144/// DIBasicType.
145bool DIDescriptor::isBasicType() const {
Devang Patel04d6d472011-09-14 23:13:28 +0000146 if (!DbgNode) return false;
147 switch (getTag()) {
148 case dwarf::DW_TAG_base_type:
149 case dwarf::DW_TAG_unspecified_type:
150 return true;
151 default:
152 return false;
153 }
Torok Edwin040688f2008-12-13 08:25:29 +0000154}
Chris Lattner6d389332008-11-10 02:56:27 +0000155
Devang Patel9fda4bd2009-08-31 18:49:10 +0000156/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
157bool DIDescriptor::isDerivedType() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000158 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000159 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000160 case dwarf::DW_TAG_typedef:
161 case dwarf::DW_TAG_pointer_type:
162 case dwarf::DW_TAG_reference_type:
Eric Christopherb5cf66c2012-05-19 01:36:37 +0000163 case dwarf::DW_TAG_rvalue_reference_type:
Chris Lattner6d389332008-11-10 02:56:27 +0000164 case dwarf::DW_TAG_const_type:
165 case dwarf::DW_TAG_volatile_type:
166 case dwarf::DW_TAG_restrict_type:
167 case dwarf::DW_TAG_member:
168 case dwarf::DW_TAG_inheritance:
Devang Pateldd719f72010-08-23 23:16:25 +0000169 case dwarf::DW_TAG_friend:
Chris Lattner6d389332008-11-10 02:56:27 +0000170 return true;
171 default:
Devang Patel80ae3492009-08-28 23:24:31 +0000172 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000173 return isCompositeType();
Chris Lattner6d389332008-11-10 02:56:27 +0000174 }
175}
176
Chris Lattner6d389332008-11-10 02:56:27 +0000177/// isCompositeType - Return true if the specified tag is legal for
178/// DICompositeType.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000179bool DIDescriptor::isCompositeType() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000180 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000181 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000182 case dwarf::DW_TAG_array_type:
183 case dwarf::DW_TAG_structure_type:
184 case dwarf::DW_TAG_union_type:
185 case dwarf::DW_TAG_enumeration_type:
186 case dwarf::DW_TAG_vector_type:
187 case dwarf::DW_TAG_subroutine_type:
Devang Patel68b08812009-03-25 03:52:06 +0000188 case dwarf::DW_TAG_class_type:
Chris Lattner6d389332008-11-10 02:56:27 +0000189 return true;
190 default:
191 return false;
192 }
193}
194
Chris Lattner6d389332008-11-10 02:56:27 +0000195/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000196bool DIDescriptor::isVariable() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000197 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000198 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000199 case dwarf::DW_TAG_auto_variable:
200 case dwarf::DW_TAG_arg_variable:
201 case dwarf::DW_TAG_return_variable:
202 return true;
203 default:
204 return false;
205 }
206}
207
Devang Patela49be762009-09-30 22:34:41 +0000208/// isType - Return true if the specified tag is legal for DIType.
209bool DIDescriptor::isType() const {
210 return isBasicType() || isCompositeType() || isDerivedType();
211}
212
Devang Patel9fda4bd2009-08-31 18:49:10 +0000213/// isSubprogram - Return true if the specified tag is legal for
214/// DISubprogram.
215bool DIDescriptor::isSubprogram() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000216 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel9fda4bd2009-08-31 18:49:10 +0000217}
218
219/// isGlobalVariable - Return true if the specified tag is legal for
220/// DIGlobalVariable.
221bool DIDescriptor::isGlobalVariable() const {
Devang Patelb2197462010-08-10 07:11:13 +0000222 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
223 getTag() == dwarf::DW_TAG_constant);
Devang Patel9fda4bd2009-08-31 18:49:10 +0000224}
225
Devang Patela49be762009-09-30 22:34:41 +0000226/// isGlobal - Return true if the specified tag is legal for DIGlobal.
227bool DIDescriptor::isGlobal() const {
228 return isGlobalVariable();
229}
230
Devang Pateldf0dd7d2011-02-03 00:13:47 +0000231/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Patel9a33ec22010-10-06 20:50:40 +0000232/// DW_TAG_unspecified_parameters.
233bool DIDescriptor::isUnspecifiedParameter() const {
234 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
235}
236
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000237/// isScope - Return true if the specified tag is one of the scope
Devang Pateld9b61152009-08-31 20:44:45 +0000238/// related tag.
239bool DIDescriptor::isScope() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000240 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000241 switch (getTag()) {
242 case dwarf::DW_TAG_compile_unit:
243 case dwarf::DW_TAG_lexical_block:
244 case dwarf::DW_TAG_subprogram:
245 case dwarf::DW_TAG_namespace:
246 return true;
247 default:
248 break;
Devang Pateld9b61152009-08-31 20:44:45 +0000249 }
250 return false;
251}
Devang Patel9fda4bd2009-08-31 18:49:10 +0000252
Devang Patel3a9e65e2011-02-02 21:38:25 +0000253/// isTemplateTypeParameter - Return true if the specified tag is
254/// DW_TAG_template_type_parameter.
255bool DIDescriptor::isTemplateTypeParameter() const {
256 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
257}
258
Devang Patelbe933b42011-02-02 22:35:53 +0000259/// isTemplateValueParameter - Return true if the specified tag is
260/// DW_TAG_template_value_parameter.
261bool DIDescriptor::isTemplateValueParameter() const {
262 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
263}
264
Devang Patele4cfcc32009-08-31 21:34:44 +0000265/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
266bool DIDescriptor::isCompileUnit() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000267 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patele4cfcc32009-08-31 21:34:44 +0000268}
269
Devang Patel2e520f62010-03-08 22:27:22 +0000270/// isFile - Return true if the specified tag is DW_TAG_file_type.
271bool DIDescriptor::isFile() const {
272 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
273}
274
Devang Patel1f4690c2009-12-15 19:16:48 +0000275/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
276bool DIDescriptor::isNameSpace() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000277 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel1f4690c2009-12-15 19:16:48 +0000278}
279
Eric Christopher6647b832011-10-11 22:59:11 +0000280/// isLexicalBlockFile - Return true if the specified descriptor is a
281/// lexical block with an extra file.
282bool DIDescriptor::isLexicalBlockFile() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
284 (DbgNode->getNumOperands() == 3);
285}
286
Devang Patel869529c2009-08-31 22:00:15 +0000287/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
288bool DIDescriptor::isLexicalBlock() const {
Eric Christopher6647b832011-10-11 22:59:11 +0000289 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
290 (DbgNode->getNumOperands() > 3);
Devang Patel869529c2009-08-31 22:00:15 +0000291}
292
Devang Patela49be762009-09-30 22:34:41 +0000293/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
294bool DIDescriptor::isSubrange() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000295 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patela49be762009-09-30 22:34:41 +0000296}
297
298/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
299bool DIDescriptor::isEnumerator() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000300 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patela49be762009-09-30 22:34:41 +0000301}
302
Devang Patelcc481592012-02-04 00:59:25 +0000303/// isObjCProperty - Return true if the specified tag is DW_TAG
304bool DIDescriptor::isObjCProperty() const {
Eric Christopherc13fd6d2012-03-29 21:35:05 +0000305 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
Devang Patelcc481592012-02-04 00:59:25 +0000306}
Devang Patel9fda4bd2009-08-31 18:49:10 +0000307//===----------------------------------------------------------------------===//
308// Simple Descriptor Constructors and other Methods
309//===----------------------------------------------------------------------===//
310
Devang Patel32cc43c2010-05-07 20:54:48 +0000311DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel9fda4bd2009-08-31 18:49:10 +0000312 if (!N) return;
313 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
314 DbgNode = 0;
315 }
316}
317
Devang Patel758e7d72009-01-05 18:33:01 +0000318unsigned DIArray::getNumElements() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000319 if (!DbgNode)
320 return 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000321 return DbgNode->getNumOperands();
Devang Patel758e7d72009-01-05 18:33:01 +0000322}
Chris Lattner6d389332008-11-10 02:56:27 +0000323
Devang Patelf03c9be2009-07-22 18:23:44 +0000324/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman093cb792010-07-21 18:54:18 +0000325/// this descriptor.
Dan Gohman16a5d982010-08-20 22:02:26 +0000326void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000327 if (!DbgNode)
Devang Patelf03c9be2009-07-22 18:23:44 +0000328 return;
329
Daniel Dunbar03564832009-09-22 02:03:18 +0000330 // Since we use a TrackingVH for the node, its easy for clients to manufacture
331 // legitimate situations where they want to replaceAllUsesWith() on something
332 // which, due to uniquing, has merged with the source. We shield clients from
333 // this detail by allowing a value to be replaced with replaceAllUsesWith()
334 // itself.
Devang Patel4423abd2010-05-07 18:19:32 +0000335 if (DbgNode != D) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000336 MDNode *Node = const_cast<MDNode*>(DbgNode);
337 const MDNode *DN = D;
338 const Value *V = cast_or_null<Value>(DN);
339 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman16a5d982010-08-20 22:02:26 +0000340 MDNode::deleteTemporary(Node);
Daniel Dunbar03564832009-09-22 02:03:18 +0000341 }
Devang Patelf03c9be2009-07-22 18:23:44 +0000342}
343
Devang Patel81c3c872010-12-08 20:18:20 +0000344/// replaceAllUsesWith - Replace all uses of debug info referenced by
345/// this descriptor.
346void DIType::replaceAllUsesWith(MDNode *D) {
347 if (!DbgNode)
348 return;
349
350 // Since we use a TrackingVH for the node, its easy for clients to manufacture
351 // legitimate situations where they want to replaceAllUsesWith() on something
352 // which, due to uniquing, has merged with the source. We shield clients from
353 // this detail by allowing a value to be replaced with replaceAllUsesWith()
354 // itself.
355 if (DbgNode != D) {
356 MDNode *Node = const_cast<MDNode*>(DbgNode);
357 const MDNode *DN = D;
358 const Value *V = cast_or_null<Value>(DN);
359 Node->replaceAllUsesWith(const_cast<Value*>(V));
360 MDNode::deleteTemporary(Node);
361 }
362}
363
Devang Pateldfd6ec32011-08-15 17:57:41 +0000364/// isUnsignedDIType - Return true if type encoding is unsigned.
365bool DIType::isUnsignedDIType() {
366 DIDerivedType DTy(DbgNode);
367 if (DTy.Verify())
368 return DTy.getTypeDerivedFrom().isUnsignedDIType();
369
370 DIBasicType BTy(DbgNode);
371 if (BTy.Verify()) {
372 unsigned Encoding = BTy.getEncoding();
373 if (Encoding == dwarf::DW_ATE_unsigned ||
374 Encoding == dwarf::DW_ATE_unsigned_char)
375 return true;
376 }
377 return false;
378}
379
Devang Patel44afc822009-01-19 23:21:49 +0000380/// Verify - Verify that a compile unit is well formed.
381bool DICompileUnit::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000382 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000383 return false;
Devang Patel2d9caf92009-11-25 17:36:49 +0000384 StringRef N = getFilename();
385 if (N.empty())
Bill Wendlingfa54bc22009-03-13 04:39:26 +0000386 return false;
Devang Patel44afc822009-01-19 23:21:49 +0000387 // It is possible that directory and produce string is empty.
Bill Wendlingfa54bc22009-03-13 04:39:26 +0000388 return true;
Devang Patel44afc822009-01-19 23:21:49 +0000389}
390
Eric Christopher70e1bd82012-03-29 08:42:56 +0000391/// Verify - Verify that an ObjC property is well formed.
392bool DIObjCProperty::Verify() const {
393 if (!DbgNode)
394 return false;
395 unsigned Tag = getTag();
Eric Christopherc13fd6d2012-03-29 21:35:05 +0000396 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
Eric Christopher70e1bd82012-03-29 08:42:56 +0000397 DIType Ty = getType();
398 if (!Ty.Verify()) return false;
399
400 // Don't worry about the rest of the strings for now.
401 return true;
402}
403
Devang Patel44afc822009-01-19 23:21:49 +0000404/// Verify - Verify that a type descriptor is well formed.
405bool DIType::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000406 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000407 return false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000408 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000409 return false;
Devang Patel415c5512010-11-02 20:41:13 +0000410 unsigned Tag = getTag();
411 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
412 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Eric Christopherb5cf66c2012-05-19 01:36:37 +0000413 Tag != dwarf::DW_TAG_reference_type &&
414 Tag != dwarf::DW_TAG_rvalue_reference_type &&
415 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
416 Tag != dwarf::DW_TAG_array_type &&
417 Tag != dwarf::DW_TAG_enumeration_type &&
418 Tag != dwarf::DW_TAG_subroutine_type &&
419 getFilename().empty())
Devang Patel44afc822009-01-19 23:21:49 +0000420 return false;
421 return true;
422}
423
Devang Patela8652672010-08-23 18:25:56 +0000424/// Verify - Verify that a basic type descriptor is well formed.
425bool DIBasicType::Verify() const {
426 return isBasicType();
427}
428
429/// Verify - Verify that a derived type descriptor is well formed.
430bool DIDerivedType::Verify() const {
431 return isDerivedType();
432}
433
Devang Patel44afc822009-01-19 23:21:49 +0000434/// Verify - Verify that a composite type descriptor is well formed.
435bool DICompositeType::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000436 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000437 return false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000438 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000439 return false;
440
Devang Patel44afc822009-01-19 23:21:49 +0000441 return true;
442}
443
444/// Verify - Verify that a subprogram descriptor is well formed.
445bool DISubprogram::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000446 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000447 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000448
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000449 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000450 return false;
451
452 DICompositeType Ty = getType();
Devang Patel3b548aa2010-03-08 20:52:55 +0000453 if (!Ty.Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000454 return false;
455 return true;
456}
457
458/// Verify - Verify that a global variable descriptor is well formed.
459bool DIGlobalVariable::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000460 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000461 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000462
Devang Patel2d9caf92009-11-25 17:36:49 +0000463 if (getDisplayName().empty())
Devang Patelcc11371b2009-11-06 17:58:12 +0000464 return false;
465
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000466 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000467 return false;
468
469 DIType Ty = getType();
470 if (!Ty.Verify())
471 return false;
472
Devang Patelc7cf14f2010-08-09 21:39:24 +0000473 if (!getGlobal() && !getConstant())
Devang Patel44afc822009-01-19 23:21:49 +0000474 return false;
475
476 return true;
477}
478
479/// Verify - Verify that a variable descriptor is well formed.
480bool DIVariable::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000481 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000482 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000483
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000484 if (getContext() && !getContext().Verify())
Devang Patel2c4d69d2010-05-07 21:42:24 +0000485 return false;
486
Devang Patel44afc822009-01-19 23:21:49 +0000487 DIType Ty = getType();
488 if (!Ty.Verify())
489 return false;
490
Devang Patel44afc822009-01-19 23:21:49 +0000491 return true;
492}
493
Devang Patel3b548aa2010-03-08 20:52:55 +0000494/// Verify - Verify that a location descriptor is well formed.
495bool DILocation::Verify() const {
496 if (!DbgNode)
497 return false;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000498
Devang Patel3b548aa2010-03-08 20:52:55 +0000499 return DbgNode->getNumOperands() == 4;
500}
501
Devang Patel54c59312010-05-07 23:04:32 +0000502/// Verify - Verify that a namespace descriptor is well formed.
503bool DINameSpace::Verify() const {
504 if (!DbgNode)
505 return false;
506 if (getName().empty())
507 return false;
Devang Patel54c59312010-05-07 23:04:32 +0000508 return true;
509}
510
Devang Patel528987a2009-02-17 21:23:59 +0000511/// getOriginalTypeSize - If this type is derived from a base type then
512/// return base type size.
513uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patelf05d5722009-11-04 23:48:00 +0000514 unsigned Tag = getTag();
Eric Christopher27886c62011-12-16 23:42:45 +0000515
Bill Wendling3b70d782012-06-26 23:22:18 +0000516 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
517 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
518 Tag != dwarf::DW_TAG_restrict_type)
519 return getSizeInBits();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000520
Bill Wendling3b70d782012-06-26 23:22:18 +0000521 DIType BaseType = getTypeDerivedFrom();
522
523 // If this type is not derived from any type then take conservative approach.
524 if (!BaseType.isValid())
525 return getSizeInBits();
526
527 // If this is a derived type, go ahead and get the base type, unless it's a
528 // reference then it's just the size of the field. Pointer types have no need
529 // of this since they're a different type of qualification on the type.
530 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
531 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
532 return getSizeInBits();
533
534 if (BaseType.isDerivedType())
535 return DIDerivedType(BaseType).getOriginalTypeSize();
536
537 return BaseType.getSizeInBits();
Devang Patel528987a2009-02-17 21:23:59 +0000538}
Devang Patel44afc822009-01-19 23:21:49 +0000539
Devang Patel44882172012-02-06 17:49:43 +0000540/// getObjCProperty - Return property node, if this ivar is associated with one.
541MDNode *DIDerivedType::getObjCProperty() const {
542 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
543 return NULL;
544 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
545}
546
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000547/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel03955532010-04-29 20:40:36 +0000548/// information for an inlined function arguments.
549bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
550 assert(CurFn && "Invalid function");
551 if (!getContext().isSubprogram())
552 return false;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000553 // This variable is not inlined function argument if its scope
Devang Patel03955532010-04-29 20:40:36 +0000554 // does not describe current function.
Bill Wendling3b70d782012-06-26 23:22:18 +0000555 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel03955532010-04-29 20:40:36 +0000556}
557
Devang Patel1be3b532009-04-15 00:06:07 +0000558/// describes - Return true if this subprogram provides debugging
559/// information for the function F.
560bool DISubprogram::describes(const Function *F) {
Chris Lattner047fd2f2009-12-29 09:22:47 +0000561 assert(F && "Invalid function");
Devang Pateld119da52010-06-16 06:42:02 +0000562 if (F == getFunction())
563 return true;
Devang Patel2d9caf92009-11-25 17:36:49 +0000564 StringRef Name = getLinkageName();
565 if (Name.empty())
Devang Patelb2969422009-09-29 18:40:58 +0000566 Name = getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000567 if (F->getName() == Name)
Devang Patel1be3b532009-04-15 00:06:07 +0000568 return true;
569 return false;
570}
571
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000572unsigned DISubprogram::isOptimized() const {
Devang Patelb4e3b902010-04-30 19:38:23 +0000573 assert (DbgNode && "Invalid subprogram descriptor!");
574 if (DbgNode->getNumOperands() == 16)
575 return getUnsignedField(15);
576 return 0;
577}
578
Devang Patel59e27c52011-08-19 23:28:12 +0000579MDNode *DISubprogram::getVariablesNodes() const {
580 if (!DbgNode || DbgNode->getNumOperands() <= 19)
581 return NULL;
582 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
583 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
584 return NULL;
585}
586
587DIArray DISubprogram::getVariables() const {
588 if (!DbgNode || DbgNode->getNumOperands() <= 19)
589 return DIArray();
590 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
591 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
592 return DIArray(A);
593 return DIArray();
594}
595
Devang Patel2d9caf92009-11-25 17:36:49 +0000596StringRef DIScope::getFilename() const {
Devang Patel8119fe872010-03-08 22:02:50 +0000597 if (!DbgNode)
598 return StringRef();
Eric Christopher6647b832011-10-11 22:59:11 +0000599 if (isLexicalBlockFile())
600 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000601 if (isLexicalBlock())
Devang Patela49be762009-09-30 22:34:41 +0000602 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000603 if (isSubprogram())
Devang Patela49be762009-09-30 22:34:41 +0000604 return DISubprogram(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000605 if (isCompileUnit())
Devang Patela49be762009-09-30 22:34:41 +0000606 return DICompileUnit(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000607 if (isNameSpace())
Devang Patel1f4690c2009-12-15 19:16:48 +0000608 return DINameSpace(DbgNode).getFilename();
Devang Patel8119fe872010-03-08 22:02:50 +0000609 if (isType())
610 return DIType(DbgNode).getFilename();
Devang Patel2e520f62010-03-08 22:27:22 +0000611 if (isFile())
612 return DIFile(DbgNode).getFilename();
Craig Toppera2886c22012-02-07 05:05:23 +0000613 llvm_unreachable("Invalid DIScope!");
Devang Patela49be762009-09-30 22:34:41 +0000614}
615
Devang Patel2d9caf92009-11-25 17:36:49 +0000616StringRef DIScope::getDirectory() const {
Devang Patel8119fe872010-03-08 22:02:50 +0000617 if (!DbgNode)
618 return StringRef();
Eric Christopher6647b832011-10-11 22:59:11 +0000619 if (isLexicalBlockFile())
620 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000621 if (isLexicalBlock())
Devang Patela49be762009-09-30 22:34:41 +0000622 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000623 if (isSubprogram())
Devang Patela49be762009-09-30 22:34:41 +0000624 return DISubprogram(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000625 if (isCompileUnit())
Devang Patela49be762009-09-30 22:34:41 +0000626 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000627 if (isNameSpace())
Devang Patel1f4690c2009-12-15 19:16:48 +0000628 return DINameSpace(DbgNode).getDirectory();
Devang Patel8119fe872010-03-08 22:02:50 +0000629 if (isType())
630 return DIType(DbgNode).getDirectory();
Devang Patel2e520f62010-03-08 22:27:22 +0000631 if (isFile())
632 return DIFile(DbgNode).getDirectory();
Craig Toppera2886c22012-02-07 05:05:23 +0000633 llvm_unreachable("Invalid DIScope!");
Devang Patela49be762009-09-30 22:34:41 +0000634}
635
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000636DIArray DICompileUnit::getEnumTypes() const {
637 if (!DbgNode || DbgNode->getNumOperands() < 14)
638 return DIArray();
639
640 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
641 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
642 return DIArray(A);
643 return DIArray();
644}
645
646DIArray DICompileUnit::getRetainedTypes() const {
647 if (!DbgNode || DbgNode->getNumOperands() < 14)
648 return DIArray();
649
650 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
651 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
652 return DIArray(A);
653 return DIArray();
654}
655
656DIArray DICompileUnit::getSubprograms() const {
657 if (!DbgNode || DbgNode->getNumOperands() < 14)
658 return DIArray();
659
660 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
661 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
662 return DIArray(A);
663 return DIArray();
664}
665
666
667DIArray DICompileUnit::getGlobalVariables() const {
668 if (!DbgNode || DbgNode->getNumOperands() < 14)
669 return DIArray();
670
671 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
672 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
673 return DIArray(A);
674 return DIArray();
675}
676
Devang Patel364bf042010-11-10 22:19:21 +0000677/// fixupObjcLikeName - Replace contains special characters used
678/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000679static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
680 bool isObjCLike = false;
Devang Patel364bf042010-11-10 22:19:21 +0000681 for (size_t i = 0, e = Str.size(); i < e; ++i) {
682 char C = Str[i];
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000683 if (C == '[')
684 isObjCLike = true;
685
686 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
687 C == '+' || C == '(' || C == ')'))
688 Out.push_back('.');
689 else
690 Out.push_back(C);
Devang Patel364bf042010-11-10 22:19:21 +0000691 }
692}
693
Devang Patel364bf042010-11-10 22:19:21 +0000694/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
695/// suitable to hold function specific information.
Devang Patel59e27c52011-08-19 23:28:12 +0000696NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000697 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel59e27c52011-08-19 23:28:12 +0000698 StringRef FName = "fn";
699 if (Fn.getFunction())
700 FName = Fn.getFunction()->getName();
701 else
702 FName = Fn.getName();
703 char One = '\1';
704 if (FName.startswith(StringRef(&One, 1)))
705 FName = FName.substr(1);
706 fixupObjcLikeName(FName, Name);
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000707 return M.getNamedMetadata(Name.str());
Devang Patel364bf042010-11-10 22:19:21 +0000708}
709
Duncan Sandsbf577d62011-03-02 20:30:37 +0000710/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
711/// to hold function specific information.
Devang Patel59e27c52011-08-19 23:28:12 +0000712NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000713 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel59e27c52011-08-19 23:28:12 +0000714 StringRef FName = "fn";
715 if (Fn.getFunction())
716 FName = Fn.getFunction()->getName();
717 else
718 FName = Fn.getName();
719 char One = '\1';
720 if (FName.startswith(StringRef(&One, 1)))
721 FName = FName.substr(1);
722 fixupObjcLikeName(FName, Name);
723
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000724 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel7a554812010-09-28 18:08:20 +0000725}
726
Devang Patelcfa82a32011-07-19 19:41:54 +0000727/// createInlinedVariable - Create a new inlined variable based on current
728/// variable.
729/// @param DV Current Variable.
730/// @param InlinedScope Location at current variable is inlined.
731DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
732 LLVMContext &VMContext) {
733 SmallVector<Value *, 16> Elts;
734 // Insert inlined scope as 7th element.
735 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
736 i == 7 ? Elts.push_back(InlinedScope) :
737 Elts.push_back(DV->getOperand(i));
738 return DIVariable(MDNode::get(VMContext, Elts));
739}
Devang Patel7a554812010-09-28 18:08:20 +0000740
Devang Patelbb23a4a2011-08-10 21:50:54 +0000741/// cleanseInlinedVariable - Remove inlined scope from the variable.
742DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
743 SmallVector<Value *, 16> Elts;
744 // Insert inlined scope as 7th element.
745 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
746 i == 7 ?
Bill Wendling16d944c2012-07-06 17:46:28 +0000747 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelbb23a4a2011-08-10 21:50:54 +0000748 Elts.push_back(DV->getOperand(i));
749 return DIVariable(MDNode::get(VMContext, Elts));
750}
751
Bill Wendlinge38859d2012-06-28 00:05:13 +0000752/// getDISubprogram - Find subprogram that is enclosing this scope.
753DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
754 DIDescriptor D(Scope);
755 if (D.isSubprogram())
756 return DISubprogram(Scope);
757
758 if (D.isLexicalBlockFile())
759 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
760
761 if (D.isLexicalBlock())
762 return getDISubprogram(DILexicalBlock(Scope).getContext());
763
764 return DISubprogram();
765}
766
767/// getDICompositeType - Find underlying composite type.
768DICompositeType llvm::getDICompositeType(DIType T) {
769 if (T.isCompositeType())
770 return DICompositeType(T);
771
772 if (T.isDerivedType())
773 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
774
775 return DICompositeType();
776}
777
778/// isSubprogramContext - Return true if Context is either a subprogram
779/// or another context nested inside a subprogram.
780bool llvm::isSubprogramContext(const MDNode *Context) {
781 if (!Context)
782 return false;
783 DIDescriptor D(Context);
784 if (D.isSubprogram())
785 return true;
786 if (D.isType())
787 return isSubprogramContext(DIType(Context).getContext());
788 return false;
789}
790
Devang Patel1da75552009-07-28 19:55:13 +0000791//===----------------------------------------------------------------------===//
Devang Patel3b4e8272009-07-30 18:25:15 +0000792// DebugInfoFinder implementations.
Devang Patel1da75552009-07-28 19:55:13 +0000793//===----------------------------------------------------------------------===//
794
Devang Patel3b4e8272009-07-30 18:25:15 +0000795/// processModule - Process entire module and collect debug info.
Eric Christopher58f41952012-11-19 22:42:15 +0000796void DebugInfoFinder::processModule(const Module &M) {
Devang Patel7973e782011-10-17 22:30:34 +0000797 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
798 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
799 DICompileUnit CU(CU_Nodes->getOperand(i));
800 addCompileUnit(CU);
801 if (CU.getVersion() > LLVMDebugVersion10) {
Devang Patela93cc252012-02-08 00:17:07 +0000802 DIArray GVs = CU.getGlobalVariables();
803 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
804 DIGlobalVariable DIG(GVs.getElement(i));
805 if (addGlobalVariable(DIG))
806 processType(DIG.getType());
807 }
808 DIArray SPs = CU.getSubprograms();
809 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
810 processSubprogram(DISubprogram(SPs.getElement(i)));
811 DIArray EnumTypes = CU.getEnumTypes();
812 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
813 processType(DIType(EnumTypes.getElement(i)));
814 DIArray RetainedTypes = CU.getRetainedTypes();
815 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
816 processType(DIType(RetainedTypes.getElement(i)));
817 return;
Devang Patel7973e782011-10-17 22:30:34 +0000818 }
819 }
820 }
Devang Patel5ea5d792011-09-06 17:40:08 +0000821
Eric Christopher58f41952012-11-19 22:42:15 +0000822 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
823 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
824 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
Devang Patel1da75552009-07-28 19:55:13 +0000825 ++BI) {
Eric Christopher58f41952012-11-19 22:42:15 +0000826 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patel9d7de2a2009-07-31 18:18:52 +0000827 processDeclare(DDI);
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000828
Chris Lattner44714c92010-04-02 20:44:29 +0000829 DebugLoc Loc = BI->getDebugLoc();
830 if (Loc.isUnknown())
831 continue;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000832
Chris Lattner44714c92010-04-02 20:44:29 +0000833 LLVMContext &Ctx = BI->getContext();
834 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000835
Chris Lattner44714c92010-04-02 20:44:29 +0000836 if (Scope.isCompileUnit())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000837 addCompileUnit(DICompileUnit(Scope));
Chris Lattner44714c92010-04-02 20:44:29 +0000838 else if (Scope.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000839 processSubprogram(DISubprogram(Scope));
Eric Christopher6647b832011-10-11 22:59:11 +0000840 else if (Scope.isLexicalBlockFile()) {
841 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
842 processLexicalBlock(DILexicalBlock(DBF.getScope()));
843 }
Chris Lattner44714c92010-04-02 20:44:29 +0000844 else if (Scope.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000845 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000846
Chris Lattner44714c92010-04-02 20:44:29 +0000847 if (MDNode *IA = Loc.getInlinedAt(Ctx))
848 processLocation(DILocation(IA));
Devang Patel1da75552009-07-28 19:55:13 +0000849 }
Devang Patel80ae3492009-08-28 23:24:31 +0000850
Devang Patelf7869a42010-06-28 05:53:08 +0000851 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
852 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
853 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
854 if (addGlobalVariable(DIG)) {
Devang Patel5ea5d792011-09-06 17:40:08 +0000855 if (DIG.getVersion() <= LLVMDebugVersion10)
856 addCompileUnit(DIG.getCompileUnit());
Devang Patelf7869a42010-06-28 05:53:08 +0000857 processType(DIG.getType());
858 }
Devang Patel1da75552009-07-28 19:55:13 +0000859 }
860 }
Devang Patelf7869a42010-06-28 05:53:08 +0000861
862 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
863 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
864 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Patel1da75552009-07-28 19:55:13 +0000865}
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000866
Devang Patel42c1d172009-11-10 22:05:35 +0000867/// processLocation - Process DILocation.
868void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000869 if (!Loc.Verify()) return;
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000870 DIDescriptor S(Loc.getScope());
Devang Patel42c1d172009-11-10 22:05:35 +0000871 if (S.isCompileUnit())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000872 addCompileUnit(DICompileUnit(S));
Devang Patel42c1d172009-11-10 22:05:35 +0000873 else if (S.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000874 processSubprogram(DISubprogram(S));
Devang Patel42c1d172009-11-10 22:05:35 +0000875 else if (S.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000876 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6647b832011-10-11 22:59:11 +0000877 else if (S.isLexicalBlockFile()) {
878 DILexicalBlockFile DBF = DILexicalBlockFile(S);
879 processLexicalBlock(DILexicalBlock(DBF.getScope()));
880 }
Devang Patel42c1d172009-11-10 22:05:35 +0000881 processLocation(Loc.getOrigLocation());
882}
883
Devang Patel3b4e8272009-07-30 18:25:15 +0000884/// processType - Process DIType.
885void DebugInfoFinder::processType(DIType DT) {
Devang Pateld0658ef2009-08-10 22:09:58 +0000886 if (!addType(DT))
Devang Patel1da75552009-07-28 19:55:13 +0000887 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000888 if (DT.getVersion() <= LLVMDebugVersion10)
889 addCompileUnit(DT.getCompileUnit());
Devang Patel9fda4bd2009-08-31 18:49:10 +0000890 if (DT.isCompositeType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000891 DICompositeType DCT(DT);
Devang Patel3b4e8272009-07-30 18:25:15 +0000892 processType(DCT.getTypeDerivedFrom());
Devang Patel1da75552009-07-28 19:55:13 +0000893 DIArray DA = DCT.getTypeArray();
Devang Patel3b548aa2010-03-08 20:52:55 +0000894 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
895 DIDescriptor D = DA.getElement(i);
896 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000897 processType(DIType(D));
Devang Patel3b548aa2010-03-08 20:52:55 +0000898 else if (D.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000899 processSubprogram(DISubprogram(D));
Devang Patel3b548aa2010-03-08 20:52:55 +0000900 }
Devang Patel9fda4bd2009-08-31 18:49:10 +0000901 } else if (DT.isDerivedType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000902 DIDerivedType DDT(DT);
Devang Patel3b548aa2010-03-08 20:52:55 +0000903 processType(DDT.getTypeDerivedFrom());
Devang Patel1da75552009-07-28 19:55:13 +0000904 }
905}
906
Devang Patel55571bd2009-10-07 22:04:08 +0000907/// processLexicalBlock
908void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patel55571bd2009-10-07 22:04:08 +0000909 DIScope Context = LB.getContext();
910 if (Context.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000911 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6647b832011-10-11 22:59:11 +0000912 else if (Context.isLexicalBlockFile()) {
913 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
914 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
915 }
Devang Patel55571bd2009-10-07 22:04:08 +0000916 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000917 return processSubprogram(DISubprogram(Context));
Devang Patel55571bd2009-10-07 22:04:08 +0000918}
919
Devang Patel3b4e8272009-07-30 18:25:15 +0000920/// processSubprogram - Process DISubprogram.
921void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patel1da75552009-07-28 19:55:13 +0000922 if (!addSubprogram(SP))
923 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000924 if (SP.getVersion() <= LLVMDebugVersion10)
925 addCompileUnit(SP.getCompileUnit());
Devang Patel3b4e8272009-07-30 18:25:15 +0000926 processType(SP.getType());
Devang Patel1da75552009-07-28 19:55:13 +0000927}
928
Devang Patel9d7de2a2009-07-31 18:18:52 +0000929/// processDeclare - Process DbgDeclareInst.
Eric Christopher58f41952012-11-19 22:42:15 +0000930void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000931 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
932 if (!N) return;
933
934 DIDescriptor DV(N);
935 if (!DV.isVariable())
Devang Patel9d7de2a2009-07-31 18:18:52 +0000936 return;
937
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000938 if (!NodesSeen.insert(DV))
Devang Patel9d7de2a2009-07-31 18:18:52 +0000939 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000940 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
941 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3b548aa2010-03-08 20:52:55 +0000942 processType(DIVariable(N).getType());
Devang Patel9d7de2a2009-07-31 18:18:52 +0000943}
944
Devang Pateld0658ef2009-08-10 22:09:58 +0000945/// addType - Add type into Tys.
946bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000947 if (!DT.isValid())
Devang Pateld0658ef2009-08-10 22:09:58 +0000948 return false;
949
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000950 if (!NodesSeen.insert(DT))
Devang Pateld0658ef2009-08-10 22:09:58 +0000951 return false;
952
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000953 TYs.push_back(DT);
Devang Pateld0658ef2009-08-10 22:09:58 +0000954 return true;
955}
956
Devang Patel1da75552009-07-28 19:55:13 +0000957/// addCompileUnit - Add compile unit into CUs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000958bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000959 if (!CU.Verify())
Devang Patel1da75552009-07-28 19:55:13 +0000960 return false;
961
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000962 if (!NodesSeen.insert(CU))
Devang Patel1da75552009-07-28 19:55:13 +0000963 return false;
964
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000965 CUs.push_back(CU);
Devang Patel1da75552009-07-28 19:55:13 +0000966 return true;
967}
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000968
Devang Patel1da75552009-07-28 19:55:13 +0000969/// addGlobalVariable - Add global variable into GVs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000970bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000971 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Patel1da75552009-07-28 19:55:13 +0000972 return false;
973
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000974 if (!NodesSeen.insert(DIG))
Devang Patel1da75552009-07-28 19:55:13 +0000975 return false;
976
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000977 GVs.push_back(DIG);
Devang Patel1da75552009-07-28 19:55:13 +0000978 return true;
979}
980
981// addSubprogram - Add subprgoram into SPs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000982bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000983 if (!DIDescriptor(SP).isSubprogram())
Devang Patel1da75552009-07-28 19:55:13 +0000984 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000985
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000986 if (!NodesSeen.insert(SP))
Devang Patel1da75552009-07-28 19:55:13 +0000987 return false;
988
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000989 SPs.push_back(SP);
Devang Patel1da75552009-07-28 19:55:13 +0000990 return true;
991}
992
Bill Wendling3b70d782012-06-26 23:22:18 +0000993//===----------------------------------------------------------------------===//
994// DIDescriptor: dump routines for all descriptors.
995//===----------------------------------------------------------------------===//
996
997/// dump - Print descriptor to dbgs() with a newline.
998void DIDescriptor::dump() const {
999 print(dbgs()); dbgs() << '\n';
1000}
1001
1002/// print - Print descriptor.
1003void DIDescriptor::print(raw_ostream &OS) const {
1004 if (!DbgNode) return;
1005
Bill Wendlinga2ccbf02012-06-28 02:17:58 +00001006 if (const char *Tag = dwarf::TagString(getTag()))
1007 OS << "[ " << Tag << " ]";
Bill Wendling3b70d782012-06-26 23:22:18 +00001008
1009 if (this->isSubrange()) {
1010 DISubrange(DbgNode).printInternal(OS);
Bill Wendling3b70d782012-06-26 23:22:18 +00001011 } else if (this->isCompileUnit()) {
1012 DICompileUnit(DbgNode).printInternal(OS);
1013 } else if (this->isFile()) {
1014 DIFile(DbgNode).printInternal(OS);
1015 } else if (this->isEnumerator()) {
1016 DIEnumerator(DbgNode).printInternal(OS);
1017 } else if (this->isBasicType()) {
1018 DIType(DbgNode).printInternal(OS);
1019 } else if (this->isDerivedType()) {
1020 DIDerivedType(DbgNode).printInternal(OS);
1021 } else if (this->isCompositeType()) {
1022 DICompositeType(DbgNode).printInternal(OS);
1023 } else if (this->isSubprogram()) {
1024 DISubprogram(DbgNode).printInternal(OS);
1025 } else if (this->isGlobalVariable()) {
1026 DIGlobalVariable(DbgNode).printInternal(OS);
1027 } else if (this->isVariable()) {
1028 DIVariable(DbgNode).printInternal(OS);
Bill Wendlingaa02e362012-07-06 19:12:31 +00001029 } else if (this->isObjCProperty()) {
1030 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling32705822012-07-06 23:06:16 +00001031 } else if (this->isScope()) {
1032 DIScope(DbgNode).printInternal(OS);
Bill Wendling3b70d782012-06-26 23:22:18 +00001033 }
1034}
1035
1036void DISubrange::printInternal(raw_ostream &OS) const {
1037 OS << " [" << getLo() << ", " << getHi() << ']';
1038}
1039
1040void DIScope::printInternal(raw_ostream &OS) const {
1041 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1042}
1043
1044void DICompileUnit::printInternal(raw_ostream &OS) const {
1045 DIScope::printInternal(OS);
1046 if (unsigned Lang = getLanguage())
1047 OS << " [" << dwarf::LanguageString(Lang) << ']';
1048}
1049
1050void DIEnumerator::printInternal(raw_ostream &OS) const {
1051 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1052}
1053
1054void DIType::printInternal(raw_ostream &OS) const {
1055 if (!DbgNode) return;
1056
1057 StringRef Res = getName();
1058 if (!Res.empty())
1059 OS << " [" << Res << "]";
1060
1061 // TODO: Print context?
1062
1063 OS << " [line " << getLineNumber()
1064 << ", size " << getSizeInBits()
1065 << ", align " << getAlignInBits()
1066 << ", offset " << getOffsetInBits();
1067 if (isBasicType())
Bill Wendling74ac0232012-06-28 02:12:20 +00001068 if (const char *Enc =
1069 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1070 OS << ", enc " << Enc;
Bill Wendling3b70d782012-06-26 23:22:18 +00001071 OS << "]";
1072
1073 if (isPrivate())
1074 OS << " [private]";
1075 else if (isProtected())
1076 OS << " [protected]";
1077
1078 if (isForwardDecl())
1079 OS << " [fwd]";
1080}
1081
1082void DIDerivedType::printInternal(raw_ostream &OS) const {
1083 DIType::printInternal(OS);
1084 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1085}
1086
1087void DICompositeType::printInternal(raw_ostream &OS) const {
1088 DIType::printInternal(OS);
1089 DIArray A = getTypeArray();
1090 OS << " [" << A.getNumElements() << " elements]";
1091}
1092
1093void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendling3b70d782012-06-26 23:22:18 +00001094 // TODO : Print context
Bill Wendling3b70d782012-06-26 23:22:18 +00001095 OS << " [line " << getLineNumber() << ']';
1096
1097 if (isLocalToUnit())
1098 OS << " [local]";
1099
1100 if (isDefinition())
1101 OS << " [def]";
1102
1103 if (getScopeLineNumber() != getLineNumber())
1104 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendling56543732012-07-06 23:43:12 +00001105
1106 StringRef Res = getName();
1107 if (!Res.empty())
1108 OS << " [" << Res << ']';
Bill Wendling3b70d782012-06-26 23:22:18 +00001109}
1110
1111void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1112 StringRef Res = getName();
1113 if (!Res.empty())
1114 OS << " [" << Res << ']';
1115
1116 OS << " [line " << getLineNumber() << ']';
1117
1118 // TODO : Print context
1119
1120 if (isLocalToUnit())
1121 OS << " [local]";
1122
1123 if (isDefinition())
1124 OS << " [def]";
1125}
1126
1127void DIVariable::printInternal(raw_ostream &OS) const {
1128 StringRef Res = getName();
1129 if (!Res.empty())
1130 OS << " [" << Res << ']';
1131
1132 OS << " [line " << getLineNumber() << ']';
1133}
1134
Bill Wendlingaa02e362012-07-06 19:12:31 +00001135void DIObjCProperty::printInternal(raw_ostream &OS) const {
1136 StringRef Name = getObjCPropertyName();
1137 if (!Name.empty())
1138 OS << " [" << Name << ']';
1139
1140 OS << " [line " << getLineNumber()
1141 << ", properties " << getUnsignedField(6) << ']';
1142}
1143
Bill Wendling3b70d782012-06-26 23:22:18 +00001144static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1145 const LLVMContext &Ctx) {
1146 if (!DL.isUnknown()) { // Print source line info.
1147 DIScope Scope(DL.getScope(Ctx));
1148 // Omit the directory, because it's likely to be long and uninteresting.
1149 if (Scope.Verify())
1150 CommentOS << Scope.getFilename();
1151 else
1152 CommentOS << "<unknown>";
1153 CommentOS << ':' << DL.getLine();
1154 if (DL.getCol() != 0)
1155 CommentOS << ':' << DL.getCol();
1156 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1157 if (!InlinedAtDL.isUnknown()) {
1158 CommentOS << " @[ ";
1159 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1160 CommentOS << " ]";
1161 }
1162 }
1163}
1164
1165void DIVariable::printExtendedName(raw_ostream &OS) const {
1166 const LLVMContext &Ctx = DbgNode->getContext();
1167 StringRef Res = getName();
1168 if (!Res.empty())
1169 OS << Res << "," << getLineNumber();
1170 if (MDNode *InlinedAt = getInlinedAt()) {
1171 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1172 if (!InlinedAtDL.isUnknown()) {
1173 OS << " @[";
1174 printDebugLoc(InlinedAtDL, OS, Ctx);
1175 OS << "]";
1176 }
1177 }
1178}