blob: b5e1d177df62de9495f29c0fc3e74f7d552f3de0 [file] [log] [blame]
Chris Lattnera45664f2008-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 Wendling0bcbd1d2012-06-28 00:05:13 +000015#include "llvm/DebugInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
Devang Patele4b27562009-08-28 23:24:31 +000017#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman17aa92c2010-07-21 23:38:33 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/Module.h"
David Greene0eb5b662009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Devang Patel5b164b52010-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 Christopher6618a242011-10-11 22:59:11 +000042DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43}
44
Devang Patel5b164b52010-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 Grosbache62b6902010-07-21 21:36:25 +000054StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000055DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000056 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000057 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000058
Chris Lattner5d0cacd2009-12-31 01:22:29 +000059 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000061 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000062
Devang Patel65dbc902009-11-25 17:36:49 +000063 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000064}
65
66uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000067 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000068 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000069
Chris Lattner5d0cacd2009-12-31 01:22:29 +000070 if (Elt < DbgNode->getNumOperands())
Eric Christopher5f214ae2012-11-20 00:15:36 +000071 if (ConstantInt *CI
72 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000073 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074
Chris Lattnera45664f2008-11-10 02:56:27 +000075 return 0;
76}
77
Bill Wendling9127be82012-12-03 19:44:25 +000078int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
79 if (DbgNode == 0)
80 return 0;
81
82 if (Elt < DbgNode->getNumOperands())
83 if (ConstantInt *CI
84 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
85 return CI->getSExtValue();
86
87 return 0;
88}
89
Chris Lattnera45664f2008-11-10 02:56:27 +000090DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000091 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000092 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000093
Chris Lattner7a2f3e02010-03-31 05:53:47 +000094 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +000095 return
96 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000097 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000098}
99
100GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000101 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000102 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000103
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000104 if (Elt < DbgNode->getNumOperands())
105 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000106 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000107}
108
Devang Patel27398962010-08-09 21:39:24 +0000109Constant *DIDescriptor::getConstantField(unsigned Elt) const {
110 if (DbgNode == 0)
111 return 0;
112
113 if (Elt < DbgNode->getNumOperands())
114 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
115 return 0;
116}
117
Stuart Hastings215aa152010-06-11 20:08:44 +0000118Function *DIDescriptor::getFunctionField(unsigned Elt) const {
119 if (DbgNode == 0)
120 return 0;
121
122 if (Elt < DbgNode->getNumOperands())
123 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
124 return 0;
125}
126
Alexey Samsonove97a3a42012-10-09 08:13:15 +0000127void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
128 if (DbgNode == 0)
129 return;
130
131 if (Elt < DbgNode->getNumOperands()) {
132 MDNode *Node = const_cast<MDNode*>(DbgNode);
133 Node->replaceOperandWith(Elt, F);
134 }
135}
136
Chris Lattnerf0908a32009-12-31 03:02:08 +0000137unsigned DIVariable::getNumAddrElements() const {
Bill Wendling0735e812012-07-06 17:46:28 +0000138 if (getVersion() <= LLVMDebugVersion8)
Devang Patel3cf763d2010-09-29 23:07:21 +0000139 return DbgNode->getNumOperands()-6;
Bill Wendling0735e812012-07-06 17:46:28 +0000140 if (getVersion() == LLVMDebugVersion9)
Devang Patel23336b42011-07-19 19:41:54 +0000141 return DbgNode->getNumOperands()-7;
142 return DbgNode->getNumOperands()-8;
Chris Lattnerf0908a32009-12-31 03:02:08 +0000143}
144
Devang Patel40c7e412011-07-20 22:18:50 +0000145/// getInlinedAt - If this variable is inlined then return inline location.
Devang Patel48d726f2011-08-09 01:03:14 +0000146MDNode *DIVariable::getInlinedAt() const {
Bill Wendling0735e812012-07-06 17:46:28 +0000147 if (getVersion() <= LLVMDebugVersion9)
Devang Patel40c7e412011-07-20 22:18:50 +0000148 return NULL;
149 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
150}
Chris Lattnerf0908a32009-12-31 03:02:08 +0000151
Chris Lattnera45664f2008-11-10 02:56:27 +0000152//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000153// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000154//===----------------------------------------------------------------------===//
155
Devang Patel6ceea332009-08-31 18:49:10 +0000156/// isBasicType - Return true if the specified tag is legal for
157/// DIBasicType.
158bool DIDescriptor::isBasicType() const {
Devang Patel734a67c2011-09-14 23:13:28 +0000159 if (!DbgNode) return false;
160 switch (getTag()) {
161 case dwarf::DW_TAG_base_type:
162 case dwarf::DW_TAG_unspecified_type:
163 return true;
164 default:
165 return false;
166 }
Torok Edwinb07fbd92008-12-13 08:25:29 +0000167}
Chris Lattnera45664f2008-11-10 02:56:27 +0000168
Devang Patel6ceea332009-08-31 18:49:10 +0000169/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
170bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000171 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000172 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000173 case dwarf::DW_TAG_typedef:
174 case dwarf::DW_TAG_pointer_type:
David Blaikie62fdfb52013-01-07 05:51:15 +0000175 case dwarf::DW_TAG_ptr_to_member_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000176 case dwarf::DW_TAG_reference_type:
Eric Christopher791e6292012-05-19 01:36:37 +0000177 case dwarf::DW_TAG_rvalue_reference_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000178 case dwarf::DW_TAG_const_type:
179 case dwarf::DW_TAG_volatile_type:
180 case dwarf::DW_TAG_restrict_type:
181 case dwarf::DW_TAG_member:
182 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000183 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000184 return true;
185 default:
Devang Patele4b27562009-08-28 23:24:31 +0000186 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000187 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000188 }
189}
190
Chris Lattnera45664f2008-11-10 02:56:27 +0000191/// isCompositeType - Return true if the specified tag is legal for
192/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000193bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000194 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000195 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000196 case dwarf::DW_TAG_array_type:
197 case dwarf::DW_TAG_structure_type:
198 case dwarf::DW_TAG_union_type:
199 case dwarf::DW_TAG_enumeration_type:
200 case dwarf::DW_TAG_vector_type:
201 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000202 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000203 return true;
204 default:
205 return false;
206 }
207}
208
Chris Lattnera45664f2008-11-10 02:56:27 +0000209/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000210bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000211 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000212 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000213 case dwarf::DW_TAG_auto_variable:
214 case dwarf::DW_TAG_arg_variable:
215 case dwarf::DW_TAG_return_variable:
216 return true;
217 default:
218 return false;
219 }
220}
221
Devang Patelecbeb1a2009-09-30 22:34:41 +0000222/// isType - Return true if the specified tag is legal for DIType.
223bool DIDescriptor::isType() const {
224 return isBasicType() || isCompositeType() || isDerivedType();
225}
226
Devang Patel6ceea332009-08-31 18:49:10 +0000227/// isSubprogram - Return true if the specified tag is legal for
228/// DISubprogram.
229bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000230 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000231}
232
233/// isGlobalVariable - Return true if the specified tag is legal for
234/// DIGlobalVariable.
235bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000236 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
237 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000238}
239
Devang Patelecbeb1a2009-09-30 22:34:41 +0000240/// isGlobal - Return true if the specified tag is legal for DIGlobal.
241bool DIDescriptor::isGlobal() const {
242 return isGlobalVariable();
243}
244
Devang Patel716a67f2011-02-03 00:13:47 +0000245/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000246/// DW_TAG_unspecified_parameters.
247bool DIDescriptor::isUnspecifiedParameter() const {
248 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
249}
250
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000251/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000252/// related tag.
253bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000254 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000255 switch (getTag()) {
256 case dwarf::DW_TAG_compile_unit:
257 case dwarf::DW_TAG_lexical_block:
258 case dwarf::DW_TAG_subprogram:
259 case dwarf::DW_TAG_namespace:
260 return true;
261 default:
262 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000263 }
264 return false;
265}
Devang Patel6ceea332009-08-31 18:49:10 +0000266
Devang Patel7e2cb112011-02-02 21:38:25 +0000267/// isTemplateTypeParameter - Return true if the specified tag is
268/// DW_TAG_template_type_parameter.
269bool DIDescriptor::isTemplateTypeParameter() const {
270 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
271}
272
Devang Patele7d93872011-02-02 22:35:53 +0000273/// isTemplateValueParameter - Return true if the specified tag is
274/// DW_TAG_template_value_parameter.
275bool DIDescriptor::isTemplateValueParameter() const {
276 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
277}
278
Devang Patelc9f322d2009-08-31 21:34:44 +0000279/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
280bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000281 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000282}
283
Devang Patel7aa81892010-03-08 22:27:22 +0000284/// isFile - Return true if the specified tag is DW_TAG_file_type.
285bool DIDescriptor::isFile() const {
286 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
287}
288
Devang Patel6404e4e2009-12-15 19:16:48 +0000289/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
290bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000291 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000292}
293
Eric Christopher6618a242011-10-11 22:59:11 +0000294/// isLexicalBlockFile - Return true if the specified descriptor is a
295/// lexical block with an extra file.
296bool DIDescriptor::isLexicalBlockFile() const {
297 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
298 (DbgNode->getNumOperands() == 3);
299}
300
Devang Patel5e005d82009-08-31 22:00:15 +0000301/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
302bool DIDescriptor::isLexicalBlock() const {
Eric Christopher6618a242011-10-11 22:59:11 +0000303 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
304 (DbgNode->getNumOperands() > 3);
Devang Patel5e005d82009-08-31 22:00:15 +0000305}
306
Devang Patelecbeb1a2009-09-30 22:34:41 +0000307/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
308bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000309 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000310}
311
312/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
313bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000314 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000315}
316
Devang Patel1ea02d42012-02-04 00:59:25 +0000317/// isObjCProperty - Return true if the specified tag is DW_TAG
318bool DIDescriptor::isObjCProperty() const {
Eric Christopher6c31ee22012-03-29 21:35:05 +0000319 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
Devang Patel1ea02d42012-02-04 00:59:25 +0000320}
Devang Patel6ceea332009-08-31 18:49:10 +0000321//===----------------------------------------------------------------------===//
322// Simple Descriptor Constructors and other Methods
323//===----------------------------------------------------------------------===//
324
Devang Patele9f8f5e2010-05-07 20:54:48 +0000325DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000326 if (!N) return;
327 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
328 DbgNode = 0;
329 }
330}
331
Devang Patel68afdc32009-01-05 18:33:01 +0000332unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000333 if (!DbgNode)
334 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000335 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000336}
Chris Lattnera45664f2008-11-10 02:56:27 +0000337
Devang Patelc4999d72009-07-22 18:23:44 +0000338/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000339/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000340void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000341 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000342 return;
343
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000344 // Since we use a TrackingVH for the node, its easy for clients to manufacture
345 // legitimate situations where they want to replaceAllUsesWith() on something
346 // which, due to uniquing, has merged with the source. We shield clients from
347 // this detail by allowing a value to be replaced with replaceAllUsesWith()
348 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000349 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000350 MDNode *Node = const_cast<MDNode*>(DbgNode);
351 const MDNode *DN = D;
352 const Value *V = cast_or_null<Value>(DN);
353 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000354 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000355 }
Devang Patelc4999d72009-07-22 18:23:44 +0000356}
357
Devang Patel0a2551d2010-12-08 20:18:20 +0000358/// replaceAllUsesWith - Replace all uses of debug info referenced by
359/// this descriptor.
360void DIType::replaceAllUsesWith(MDNode *D) {
361 if (!DbgNode)
362 return;
363
364 // Since we use a TrackingVH for the node, its easy for clients to manufacture
365 // legitimate situations where they want to replaceAllUsesWith() on something
366 // which, due to uniquing, has merged with the source. We shield clients from
367 // this detail by allowing a value to be replaced with replaceAllUsesWith()
368 // itself.
369 if (DbgNode != D) {
370 MDNode *Node = const_cast<MDNode*>(DbgNode);
371 const MDNode *DN = D;
372 const Value *V = cast_or_null<Value>(DN);
373 Node->replaceAllUsesWith(const_cast<Value*>(V));
374 MDNode::deleteTemporary(Node);
375 }
376}
377
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000378/// isUnsignedDIType - Return true if type encoding is unsigned.
379bool DIType::isUnsignedDIType() {
380 DIDerivedType DTy(DbgNode);
381 if (DTy.Verify())
382 return DTy.getTypeDerivedFrom().isUnsignedDIType();
383
384 DIBasicType BTy(DbgNode);
385 if (BTy.Verify()) {
386 unsigned Encoding = BTy.getEncoding();
387 if (Encoding == dwarf::DW_ATE_unsigned ||
388 Encoding == dwarf::DW_ATE_unsigned_char)
389 return true;
390 }
391 return false;
392}
393
Devang Patelb79b5352009-01-19 23:21:49 +0000394/// Verify - Verify that a compile unit is well formed.
395bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000396 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000397 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000398 StringRef N = getFilename();
399 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000400 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000401 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000402 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000403}
404
Eric Christopherb8ca9882012-03-29 08:42:56 +0000405/// Verify - Verify that an ObjC property is well formed.
406bool DIObjCProperty::Verify() const {
407 if (!DbgNode)
408 return false;
409 unsigned Tag = getTag();
Eric Christopher6c31ee22012-03-29 21:35:05 +0000410 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
Eric Christopherb8ca9882012-03-29 08:42:56 +0000411 DIType Ty = getType();
412 if (!Ty.Verify()) return false;
413
414 // Don't worry about the rest of the strings for now.
415 return true;
416}
417
Devang Patelb79b5352009-01-19 23:21:49 +0000418/// Verify - Verify that a type descriptor is well formed.
419bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000420 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000421 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000422 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000423 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000424 unsigned Tag = getTag();
425 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
426 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
David Blaikie62fdfb52013-01-07 05:51:15 +0000427 Tag != dwarf::DW_TAG_ptr_to_member_type &&
Eric Christopher791e6292012-05-19 01:36:37 +0000428 Tag != dwarf::DW_TAG_reference_type &&
429 Tag != dwarf::DW_TAG_rvalue_reference_type &&
430 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
431 Tag != dwarf::DW_TAG_array_type &&
432 Tag != dwarf::DW_TAG_enumeration_type &&
433 Tag != dwarf::DW_TAG_subroutine_type &&
434 getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000435 return false;
436 return true;
437}
438
Devang Patel0c4720c2010-08-23 18:25:56 +0000439/// Verify - Verify that a basic type descriptor is well formed.
440bool DIBasicType::Verify() const {
441 return isBasicType();
442}
443
444/// Verify - Verify that a derived type descriptor is well formed.
445bool DIDerivedType::Verify() const {
446 return isDerivedType();
447}
448
Devang Patelb79b5352009-01-19 23:21:49 +0000449/// Verify - Verify that a composite type descriptor is well formed.
450bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000451 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000452 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000453 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000454 return false;
455
Devang Patelb79b5352009-01-19 23:21:49 +0000456 return true;
457}
458
459/// Verify - Verify that a subprogram descriptor is well formed.
460bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000461 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000462 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000463
Devang Patel94c7ddb2011-08-16 22:09:43 +0000464 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000465 return false;
466
467 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000468 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000469 return false;
470 return true;
471}
472
473/// Verify - Verify that a global variable descriptor is well formed.
474bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000475 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000476 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000477
Devang Patel65dbc902009-11-25 17:36:49 +0000478 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000479 return false;
480
Devang Patel94c7ddb2011-08-16 22:09:43 +0000481 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000482 return false;
483
484 DIType Ty = getType();
485 if (!Ty.Verify())
486 return false;
487
Devang Patel27398962010-08-09 21:39:24 +0000488 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000489 return false;
490
491 return true;
492}
493
494/// Verify - Verify that a variable descriptor is well formed.
495bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000496 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000497 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000498
Devang Patel94c7ddb2011-08-16 22:09:43 +0000499 if (getContext() && !getContext().Verify())
Devang Patel62077af2010-05-07 21:42:24 +0000500 return false;
501
Devang Patelb79b5352009-01-19 23:21:49 +0000502 DIType Ty = getType();
503 if (!Ty.Verify())
504 return false;
505
Devang Patelb79b5352009-01-19 23:21:49 +0000506 return true;
507}
508
Devang Patel3c91b052010-03-08 20:52:55 +0000509/// Verify - Verify that a location descriptor is well formed.
510bool DILocation::Verify() const {
511 if (!DbgNode)
512 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000513
Devang Patel3c91b052010-03-08 20:52:55 +0000514 return DbgNode->getNumOperands() == 4;
515}
516
Devang Patel47e22652010-05-07 23:04:32 +0000517/// Verify - Verify that a namespace descriptor is well formed.
518bool DINameSpace::Verify() const {
519 if (!DbgNode)
520 return false;
521 if (getName().empty())
522 return false;
Devang Patel47e22652010-05-07 23:04:32 +0000523 return true;
524}
525
Devang Patel36375ee2009-02-17 21:23:59 +0000526/// getOriginalTypeSize - If this type is derived from a base type then
527/// return base type size.
528uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000529 unsigned Tag = getTag();
Eric Christopher2e1b0c02011-12-16 23:42:45 +0000530
Bill Wendlinge8778242012-06-26 23:22:18 +0000531 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
532 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
533 Tag != dwarf::DW_TAG_restrict_type)
534 return getSizeInBits();
Jim Grosbache62b6902010-07-21 21:36:25 +0000535
Bill Wendlinge8778242012-06-26 23:22:18 +0000536 DIType BaseType = getTypeDerivedFrom();
537
538 // If this type is not derived from any type then take conservative approach.
539 if (!BaseType.isValid())
540 return getSizeInBits();
541
542 // If this is a derived type, go ahead and get the base type, unless it's a
543 // reference then it's just the size of the field. Pointer types have no need
544 // of this since they're a different type of qualification on the type.
545 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
546 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
547 return getSizeInBits();
548
549 if (BaseType.isDerivedType())
550 return DIDerivedType(BaseType).getOriginalTypeSize();
551
552 return BaseType.getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000553}
Devang Patelb79b5352009-01-19 23:21:49 +0000554
Devang Patel6588abf2012-02-06 17:49:43 +0000555/// getObjCProperty - Return property node, if this ivar is associated with one.
556MDNode *DIDerivedType::getObjCProperty() const {
557 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
558 return NULL;
559 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
560}
561
Jim Grosbache62b6902010-07-21 21:36:25 +0000562/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000563/// information for an inlined function arguments.
564bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
565 assert(CurFn && "Invalid function");
566 if (!getContext().isSubprogram())
567 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000568 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000569 // does not describe current function.
Bill Wendlinge8778242012-06-26 23:22:18 +0000570 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel719f6a92010-04-29 20:40:36 +0000571}
572
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000573/// describes - Return true if this subprogram provides debugging
574/// information for the function F.
575bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000576 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000577 if (F == getFunction())
578 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000579 StringRef Name = getLinkageName();
580 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000581 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000582 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000583 return true;
584 return false;
585}
586
Jim Grosbache62b6902010-07-21 21:36:25 +0000587unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000588 assert (DbgNode && "Invalid subprogram descriptor!");
589 if (DbgNode->getNumOperands() == 16)
590 return getUnsignedField(15);
591 return 0;
592}
593
Devang Patel93d39be2011-08-19 23:28:12 +0000594MDNode *DISubprogram::getVariablesNodes() const {
595 if (!DbgNode || DbgNode->getNumOperands() <= 19)
596 return NULL;
597 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
598 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
599 return NULL;
600}
601
602DIArray DISubprogram::getVariables() const {
603 if (!DbgNode || DbgNode->getNumOperands() <= 19)
604 return DIArray();
605 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
606 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
607 return DIArray(A);
608 return DIArray();
609}
610
Devang Patel65dbc902009-11-25 17:36:49 +0000611StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000612 if (!DbgNode)
613 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000614 if (isLexicalBlockFile())
615 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbache62b6902010-07-21 21:36:25 +0000616 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000617 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000618 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000619 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000620 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000621 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000622 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000623 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000624 if (isType())
625 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000626 if (isFile())
627 return DIFile(DbgNode).getFilename();
Craig Topper85814382012-02-07 05:05:23 +0000628 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000629}
630
Devang Patel65dbc902009-11-25 17:36:49 +0000631StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000632 if (!DbgNode)
633 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000634 if (isLexicalBlockFile())
635 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbache62b6902010-07-21 21:36:25 +0000636 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000637 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000638 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000639 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000640 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000641 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000642 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000643 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000644 if (isType())
645 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000646 if (isFile())
647 return DIFile(DbgNode).getDirectory();
Craig Topper85814382012-02-07 05:05:23 +0000648 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000649}
650
Devang Patel94c7ddb2011-08-16 22:09:43 +0000651DIArray DICompileUnit::getEnumTypes() const {
652 if (!DbgNode || DbgNode->getNumOperands() < 14)
653 return DIArray();
654
655 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
656 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
657 return DIArray(A);
658 return DIArray();
659}
660
661DIArray DICompileUnit::getRetainedTypes() const {
662 if (!DbgNode || DbgNode->getNumOperands() < 14)
663 return DIArray();
664
665 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
666 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
667 return DIArray(A);
668 return DIArray();
669}
670
671DIArray DICompileUnit::getSubprograms() const {
672 if (!DbgNode || DbgNode->getNumOperands() < 14)
673 return DIArray();
674
675 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
NAKAMURA Takumi2587a8a2012-12-01 02:23:45 +0000676 if (N->getNumOperands() > 0)
677 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
678 return DIArray(A);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000679 return DIArray();
680}
681
682
683DIArray DICompileUnit::getGlobalVariables() const {
684 if (!DbgNode || DbgNode->getNumOperands() < 14)
685 return DIArray();
686
687 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
688 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
689 return DIArray(A);
690 return DIArray();
691}
692
Devang Patel62367042010-11-10 22:19:21 +0000693/// fixupObjcLikeName - Replace contains special characters used
694/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000695static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
696 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000697 for (size_t i = 0, e = Str.size(); i < e; ++i) {
698 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000699 if (C == '[')
700 isObjCLike = true;
701
702 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
703 C == '+' || C == '(' || C == ')'))
704 Out.push_back('.');
705 else
706 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000707 }
708}
709
Eric Christopher5f214ae2012-11-20 00:15:36 +0000710/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
Devang Patel62367042010-11-10 22:19:21 +0000711/// suitable to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000712NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000713 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-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);
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000723 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000724}
725
Duncan Sands291bb702011-03-02 20:30:37 +0000726/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
727/// to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000728NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000729 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000730 StringRef FName = "fn";
731 if (Fn.getFunction())
732 FName = Fn.getFunction()->getName();
733 else
734 FName = Fn.getName();
735 char One = '\1';
736 if (FName.startswith(StringRef(&One, 1)))
737 FName = FName.substr(1);
738 fixupObjcLikeName(FName, Name);
Eric Christopher5f214ae2012-11-20 00:15:36 +0000739
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000740 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000741}
742
Devang Patel23336b42011-07-19 19:41:54 +0000743/// createInlinedVariable - Create a new inlined variable based on current
744/// variable.
745/// @param DV Current Variable.
746/// @param InlinedScope Location at current variable is inlined.
747DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
748 LLVMContext &VMContext) {
749 SmallVector<Value *, 16> Elts;
750 // Insert inlined scope as 7th element.
751 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
752 i == 7 ? Elts.push_back(InlinedScope) :
753 Elts.push_back(DV->getOperand(i));
754 return DIVariable(MDNode::get(VMContext, Elts));
755}
Devang Patel1a7ca032010-09-28 18:08:20 +0000756
Devang Patelb549bcf2011-08-10 21:50:54 +0000757/// cleanseInlinedVariable - Remove inlined scope from the variable.
758DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
759 SmallVector<Value *, 16> Elts;
760 // Insert inlined scope as 7th element.
761 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000762 i == 7 ?
Bill Wendling0735e812012-07-06 17:46:28 +0000763 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelb549bcf2011-08-10 21:50:54 +0000764 Elts.push_back(DV->getOperand(i));
765 return DIVariable(MDNode::get(VMContext, Elts));
766}
767
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000768/// getDISubprogram - Find subprogram that is enclosing this scope.
769DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
770 DIDescriptor D(Scope);
771 if (D.isSubprogram())
772 return DISubprogram(Scope);
773
774 if (D.isLexicalBlockFile())
775 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
Eric Christopher5f214ae2012-11-20 00:15:36 +0000776
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000777 if (D.isLexicalBlock())
778 return getDISubprogram(DILexicalBlock(Scope).getContext());
779
780 return DISubprogram();
781}
782
783/// getDICompositeType - Find underlying composite type.
784DICompositeType llvm::getDICompositeType(DIType T) {
785 if (T.isCompositeType())
786 return DICompositeType(T);
787
788 if (T.isDerivedType())
789 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
790
791 return DICompositeType();
792}
793
794/// isSubprogramContext - Return true if Context is either a subprogram
795/// or another context nested inside a subprogram.
796bool llvm::isSubprogramContext(const MDNode *Context) {
797 if (!Context)
798 return false;
799 DIDescriptor D(Context);
800 if (D.isSubprogram())
801 return true;
802 if (D.isType())
803 return isSubprogramContext(DIType(Context).getContext());
804 return false;
805}
806
Devang Pateld2f79a12009-07-28 19:55:13 +0000807//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000808// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000809//===----------------------------------------------------------------------===//
810
Devang Patel98c65172009-07-30 18:25:15 +0000811/// processModule - Process entire module and collect debug info.
Eric Christopherc4639d62012-11-19 22:42:15 +0000812void DebugInfoFinder::processModule(const Module &M) {
Devang Patelcd2db162011-10-17 22:30:34 +0000813 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
814 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
815 DICompileUnit CU(CU_Nodes->getOperand(i));
816 addCompileUnit(CU);
817 if (CU.getVersion() > LLVMDebugVersion10) {
Devang Patel9f997212012-02-08 00:17:07 +0000818 DIArray GVs = CU.getGlobalVariables();
819 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
820 DIGlobalVariable DIG(GVs.getElement(i));
821 if (addGlobalVariable(DIG))
822 processType(DIG.getType());
823 }
824 DIArray SPs = CU.getSubprograms();
825 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
826 processSubprogram(DISubprogram(SPs.getElement(i)));
827 DIArray EnumTypes = CU.getEnumTypes();
828 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
829 processType(DIType(EnumTypes.getElement(i)));
830 DIArray RetainedTypes = CU.getRetainedTypes();
831 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
832 processType(DIType(RetainedTypes.getElement(i)));
833 return;
Devang Patelcd2db162011-10-17 22:30:34 +0000834 }
835 }
836 }
Devang Patelfa515ec2011-09-06 17:40:08 +0000837
Eric Christopherc4639d62012-11-19 22:42:15 +0000838 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000839 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
840 FI != FE; ++FI)
841 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
842 BI != BE; ++BI) {
Eric Christopherc4639d62012-11-19 22:42:15 +0000843 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000844 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000845
Chris Lattner28a9bf62010-04-02 20:44:29 +0000846 DebugLoc Loc = BI->getDebugLoc();
847 if (Loc.isUnknown())
848 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000849
Chris Lattner28a9bf62010-04-02 20:44:29 +0000850 LLVMContext &Ctx = BI->getContext();
851 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000852
Chris Lattner28a9bf62010-04-02 20:44:29 +0000853 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000854 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000855 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000856 processSubprogram(DISubprogram(Scope));
Eric Christopher6618a242011-10-11 22:59:11 +0000857 else if (Scope.isLexicalBlockFile()) {
858 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
859 processLexicalBlock(DILexicalBlock(DBF.getScope()));
860 }
Chris Lattner28a9bf62010-04-02 20:44:29 +0000861 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000862 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000863
Chris Lattner28a9bf62010-04-02 20:44:29 +0000864 if (MDNode *IA = Loc.getInlinedAt(Ctx))
865 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000866 }
Devang Patele4b27562009-08-28 23:24:31 +0000867
Devang Patelfd5fdc32010-06-28 05:53:08 +0000868 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
869 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
870 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
871 if (addGlobalVariable(DIG)) {
Devang Patelfa515ec2011-09-06 17:40:08 +0000872 if (DIG.getVersion() <= LLVMDebugVersion10)
873 addCompileUnit(DIG.getCompileUnit());
Devang Patelfd5fdc32010-06-28 05:53:08 +0000874 processType(DIG.getType());
875 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000876 }
877 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000878
879 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
880 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
881 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000882}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000883
Devang Patel6daf99b2009-11-10 22:05:35 +0000884/// processLocation - Process DILocation.
885void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000886 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000887 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000888 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000889 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000890 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000891 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000892 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000893 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6618a242011-10-11 22:59:11 +0000894 else if (S.isLexicalBlockFile()) {
895 DILexicalBlockFile DBF = DILexicalBlockFile(S);
896 processLexicalBlock(DILexicalBlock(DBF.getScope()));
897 }
Devang Patel6daf99b2009-11-10 22:05:35 +0000898 processLocation(Loc.getOrigLocation());
899}
900
Devang Patel98c65172009-07-30 18:25:15 +0000901/// processType - Process DIType.
902void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000903 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000904 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000905 if (DT.getVersion() <= LLVMDebugVersion10)
906 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000907 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000908 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000909 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000910 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000911 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
912 DIDescriptor D = DA.getElement(i);
913 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000914 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000915 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000916 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000917 }
Devang Patel6ceea332009-08-31 18:49:10 +0000918 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000919 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000920 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000921 }
922}
923
Devang Patelbeab41b2009-10-07 22:04:08 +0000924/// processLexicalBlock
925void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000926 DIScope Context = LB.getContext();
927 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000928 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6618a242011-10-11 22:59:11 +0000929 else if (Context.isLexicalBlockFile()) {
930 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
931 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
932 }
Devang Patelbeab41b2009-10-07 22:04:08 +0000933 else
Devang Patel2db49d72010-05-07 18:11:54 +0000934 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000935}
936
Devang Patel98c65172009-07-30 18:25:15 +0000937/// processSubprogram - Process DISubprogram.
938void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000939 if (!addSubprogram(SP))
940 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000941 if (SP.getVersion() <= LLVMDebugVersion10)
942 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000943 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000944}
945
Devang Patelb4d31302009-07-31 18:18:52 +0000946/// processDeclare - Process DbgDeclareInst.
Eric Christopherc4639d62012-11-19 22:42:15 +0000947void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +0000948 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
949 if (!N) return;
950
951 DIDescriptor DV(N);
952 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +0000953 return;
954
Devang Patel2db49d72010-05-07 18:11:54 +0000955 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +0000956 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000957 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
958 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3c91b052010-03-08 20:52:55 +0000959 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +0000960}
961
Devang Patel72bcdb62009-08-10 22:09:58 +0000962/// addType - Add type into Tys.
963bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +0000964 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +0000965 return false;
966
Devang Patel2db49d72010-05-07 18:11:54 +0000967 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +0000968 return false;
969
Devang Patel2db49d72010-05-07 18:11:54 +0000970 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +0000971 return true;
972}
973
Devang Pateld2f79a12009-07-28 19:55:13 +0000974/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000975bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +0000976 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +0000977 return false;
978
Devang Patel2db49d72010-05-07 18:11:54 +0000979 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +0000980 return false;
981
Devang Patel2db49d72010-05-07 18:11:54 +0000982 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +0000983 return true;
984}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000985
Devang Pateld2f79a12009-07-28 19:55:13 +0000986/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000987bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +0000988 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +0000989 return false;
990
Devang Patel2db49d72010-05-07 18:11:54 +0000991 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +0000992 return false;
993
Devang Patel2db49d72010-05-07 18:11:54 +0000994 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +0000995 return true;
996}
997
998// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000999bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001000 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001001 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001002
Devang Patel2db49d72010-05-07 18:11:54 +00001003 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001004 return false;
1005
Devang Patel2db49d72010-05-07 18:11:54 +00001006 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001007 return true;
1008}
1009
Bill Wendlinge8778242012-06-26 23:22:18 +00001010//===----------------------------------------------------------------------===//
1011// DIDescriptor: dump routines for all descriptors.
1012//===----------------------------------------------------------------------===//
1013
1014/// dump - Print descriptor to dbgs() with a newline.
1015void DIDescriptor::dump() const {
1016 print(dbgs()); dbgs() << '\n';
1017}
1018
1019/// print - Print descriptor.
1020void DIDescriptor::print(raw_ostream &OS) const {
1021 if (!DbgNode) return;
1022
Bill Wendling97a75922012-06-28 02:17:58 +00001023 if (const char *Tag = dwarf::TagString(getTag()))
1024 OS << "[ " << Tag << " ]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001025
1026 if (this->isSubrange()) {
1027 DISubrange(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001028 } else if (this->isCompileUnit()) {
1029 DICompileUnit(DbgNode).printInternal(OS);
1030 } else if (this->isFile()) {
1031 DIFile(DbgNode).printInternal(OS);
1032 } else if (this->isEnumerator()) {
1033 DIEnumerator(DbgNode).printInternal(OS);
1034 } else if (this->isBasicType()) {
1035 DIType(DbgNode).printInternal(OS);
1036 } else if (this->isDerivedType()) {
1037 DIDerivedType(DbgNode).printInternal(OS);
1038 } else if (this->isCompositeType()) {
1039 DICompositeType(DbgNode).printInternal(OS);
1040 } else if (this->isSubprogram()) {
1041 DISubprogram(DbgNode).printInternal(OS);
1042 } else if (this->isGlobalVariable()) {
1043 DIGlobalVariable(DbgNode).printInternal(OS);
1044 } else if (this->isVariable()) {
1045 DIVariable(DbgNode).printInternal(OS);
Bill Wendling2da1a162012-07-06 19:12:31 +00001046 } else if (this->isObjCProperty()) {
1047 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling625252f2012-07-06 23:06:16 +00001048 } else if (this->isScope()) {
1049 DIScope(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001050 }
1051}
1052
1053void DISubrange::printInternal(raw_ostream &OS) const {
Bill Wendling9493dae2012-12-04 21:34:03 +00001054 int64_t Count = getCount();
1055 if (Count != -1)
1056 OS << " [" << getLo() << ", " << Count - 1 << ']';
1057 else
Bill Wendling46defa52012-12-05 21:43:30 +00001058 OS << " [unbounded]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001059}
1060
1061void DIScope::printInternal(raw_ostream &OS) const {
1062 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1063}
1064
1065void DICompileUnit::printInternal(raw_ostream &OS) const {
1066 DIScope::printInternal(OS);
1067 if (unsigned Lang = getLanguage())
1068 OS << " [" << dwarf::LanguageString(Lang) << ']';
1069}
1070
1071void DIEnumerator::printInternal(raw_ostream &OS) const {
1072 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1073}
1074
1075void DIType::printInternal(raw_ostream &OS) const {
1076 if (!DbgNode) return;
1077
1078 StringRef Res = getName();
1079 if (!Res.empty())
1080 OS << " [" << Res << "]";
1081
1082 // TODO: Print context?
1083
1084 OS << " [line " << getLineNumber()
1085 << ", size " << getSizeInBits()
1086 << ", align " << getAlignInBits()
1087 << ", offset " << getOffsetInBits();
1088 if (isBasicType())
Eric Christopher5f214ae2012-11-20 00:15:36 +00001089 if (const char *Enc =
Bill Wendlingfc1c70a2012-06-28 02:12:20 +00001090 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1091 OS << ", enc " << Enc;
Bill Wendlinge8778242012-06-26 23:22:18 +00001092 OS << "]";
1093
1094 if (isPrivate())
1095 OS << " [private]";
1096 else if (isProtected())
1097 OS << " [protected]";
1098
1099 if (isForwardDecl())
1100 OS << " [fwd]";
1101}
1102
1103void DIDerivedType::printInternal(raw_ostream &OS) const {
1104 DIType::printInternal(OS);
1105 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1106}
1107
1108void DICompositeType::printInternal(raw_ostream &OS) const {
1109 DIType::printInternal(OS);
1110 DIArray A = getTypeArray();
1111 OS << " [" << A.getNumElements() << " elements]";
1112}
1113
1114void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendlinge8778242012-06-26 23:22:18 +00001115 // TODO : Print context
Bill Wendlinge8778242012-06-26 23:22:18 +00001116 OS << " [line " << getLineNumber() << ']';
1117
1118 if (isLocalToUnit())
1119 OS << " [local]";
1120
1121 if (isDefinition())
1122 OS << " [def]";
1123
1124 if (getScopeLineNumber() != getLineNumber())
1125 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendlingee17cee2012-07-06 23:43:12 +00001126
David Blaikiee302b6c2013-01-05 21:39:33 +00001127 if (isPrivate())
1128 OS << " [private]";
1129 else if (isProtected())
1130 OS << " [protected]";
1131
Bill Wendlingee17cee2012-07-06 23:43:12 +00001132 StringRef Res = getName();
1133 if (!Res.empty())
1134 OS << " [" << Res << ']';
Bill Wendlinge8778242012-06-26 23:22:18 +00001135}
1136
1137void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1138 StringRef Res = getName();
1139 if (!Res.empty())
1140 OS << " [" << Res << ']';
1141
1142 OS << " [line " << getLineNumber() << ']';
1143
1144 // TODO : Print context
1145
1146 if (isLocalToUnit())
1147 OS << " [local]";
1148
1149 if (isDefinition())
1150 OS << " [def]";
1151}
1152
1153void DIVariable::printInternal(raw_ostream &OS) const {
1154 StringRef Res = getName();
1155 if (!Res.empty())
1156 OS << " [" << Res << ']';
1157
1158 OS << " [line " << getLineNumber() << ']';
1159}
1160
Bill Wendling2da1a162012-07-06 19:12:31 +00001161void DIObjCProperty::printInternal(raw_ostream &OS) const {
1162 StringRef Name = getObjCPropertyName();
1163 if (!Name.empty())
1164 OS << " [" << Name << ']';
1165
1166 OS << " [line " << getLineNumber()
1167 << ", properties " << getUnsignedField(6) << ']';
1168}
1169
Bill Wendlinge8778242012-06-26 23:22:18 +00001170static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1171 const LLVMContext &Ctx) {
1172 if (!DL.isUnknown()) { // Print source line info.
1173 DIScope Scope(DL.getScope(Ctx));
1174 // Omit the directory, because it's likely to be long and uninteresting.
1175 if (Scope.Verify())
1176 CommentOS << Scope.getFilename();
1177 else
1178 CommentOS << "<unknown>";
1179 CommentOS << ':' << DL.getLine();
1180 if (DL.getCol() != 0)
1181 CommentOS << ':' << DL.getCol();
1182 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1183 if (!InlinedAtDL.isUnknown()) {
1184 CommentOS << " @[ ";
1185 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1186 CommentOS << " ]";
1187 }
1188 }
1189}
1190
1191void DIVariable::printExtendedName(raw_ostream &OS) const {
1192 const LLVMContext &Ctx = DbgNode->getContext();
1193 StringRef Res = getName();
1194 if (!Res.empty())
1195 OS << Res << "," << getLineNumber();
1196 if (MDNode *InlinedAt = getInlinedAt()) {
1197 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1198 if (!InlinedAtDL.isUnknown()) {
1199 OS << " @[";
1200 printDebugLoc(InlinedAtDL, OS, Ctx);
1201 OS << "]";
1202 }
1203 }
1204}