blob: 5dd9dd1c9d5d5d8ce93cb7fb3049b3437d3fdfc4 [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:
175 case dwarf::DW_TAG_reference_type:
Eric Christopher791e6292012-05-19 01:36:37 +0000176 case dwarf::DW_TAG_rvalue_reference_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000177 case dwarf::DW_TAG_const_type:
178 case dwarf::DW_TAG_volatile_type:
179 case dwarf::DW_TAG_restrict_type:
180 case dwarf::DW_TAG_member:
181 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000182 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000183 return true;
184 default:
Devang Patele4b27562009-08-28 23:24:31 +0000185 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000186 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000187 }
188}
189
Chris Lattnera45664f2008-11-10 02:56:27 +0000190/// isCompositeType - Return true if the specified tag is legal for
191/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000192bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000193 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000194 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000195 case dwarf::DW_TAG_array_type:
196 case dwarf::DW_TAG_structure_type:
197 case dwarf::DW_TAG_union_type:
198 case dwarf::DW_TAG_enumeration_type:
199 case dwarf::DW_TAG_vector_type:
200 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000201 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000202 return true;
203 default:
204 return false;
205 }
206}
207
Chris Lattnera45664f2008-11-10 02:56:27 +0000208/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000209bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000210 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000211 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000212 case dwarf::DW_TAG_auto_variable:
213 case dwarf::DW_TAG_arg_variable:
214 case dwarf::DW_TAG_return_variable:
215 return true;
216 default:
217 return false;
218 }
219}
220
Devang Patelecbeb1a2009-09-30 22:34:41 +0000221/// isType - Return true if the specified tag is legal for DIType.
222bool DIDescriptor::isType() const {
223 return isBasicType() || isCompositeType() || isDerivedType();
224}
225
Devang Patel6ceea332009-08-31 18:49:10 +0000226/// isSubprogram - Return true if the specified tag is legal for
227/// DISubprogram.
228bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000229 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000230}
231
232/// isGlobalVariable - Return true if the specified tag is legal for
233/// DIGlobalVariable.
234bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000235 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
236 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000237}
238
Devang Patelecbeb1a2009-09-30 22:34:41 +0000239/// isGlobal - Return true if the specified tag is legal for DIGlobal.
240bool DIDescriptor::isGlobal() const {
241 return isGlobalVariable();
242}
243
Devang Patel716a67f2011-02-03 00:13:47 +0000244/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000245/// DW_TAG_unspecified_parameters.
246bool DIDescriptor::isUnspecifiedParameter() const {
247 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
248}
249
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000250/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000251/// related tag.
252bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000253 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000254 switch (getTag()) {
255 case dwarf::DW_TAG_compile_unit:
256 case dwarf::DW_TAG_lexical_block:
257 case dwarf::DW_TAG_subprogram:
258 case dwarf::DW_TAG_namespace:
259 return true;
260 default:
261 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000262 }
263 return false;
264}
Devang Patel6ceea332009-08-31 18:49:10 +0000265
Devang Patel7e2cb112011-02-02 21:38:25 +0000266/// isTemplateTypeParameter - Return true if the specified tag is
267/// DW_TAG_template_type_parameter.
268bool DIDescriptor::isTemplateTypeParameter() const {
269 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
270}
271
Devang Patele7d93872011-02-02 22:35:53 +0000272/// isTemplateValueParameter - Return true if the specified tag is
273/// DW_TAG_template_value_parameter.
274bool DIDescriptor::isTemplateValueParameter() const {
275 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
276}
277
Devang Patelc9f322d2009-08-31 21:34:44 +0000278/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
279bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000280 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000281}
282
Devang Patel7aa81892010-03-08 22:27:22 +0000283/// isFile - Return true if the specified tag is DW_TAG_file_type.
284bool DIDescriptor::isFile() const {
285 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
286}
287
Devang Patel6404e4e2009-12-15 19:16:48 +0000288/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
289bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000290 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000291}
292
Eric Christopher6618a242011-10-11 22:59:11 +0000293/// isLexicalBlockFile - Return true if the specified descriptor is a
294/// lexical block with an extra file.
295bool DIDescriptor::isLexicalBlockFile() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
297 (DbgNode->getNumOperands() == 3);
298}
299
Devang Patel5e005d82009-08-31 22:00:15 +0000300/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
301bool DIDescriptor::isLexicalBlock() const {
Eric Christopher6618a242011-10-11 22:59:11 +0000302 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
303 (DbgNode->getNumOperands() > 3);
Devang Patel5e005d82009-08-31 22:00:15 +0000304}
305
Devang Patelecbeb1a2009-09-30 22:34:41 +0000306/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
307bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000308 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000309}
310
311/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
312bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000313 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000314}
315
Devang Patel1ea02d42012-02-04 00:59:25 +0000316/// isObjCProperty - Return true if the specified tag is DW_TAG
317bool DIDescriptor::isObjCProperty() const {
Eric Christopher6c31ee22012-03-29 21:35:05 +0000318 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
Devang Patel1ea02d42012-02-04 00:59:25 +0000319}
Devang Patel6ceea332009-08-31 18:49:10 +0000320//===----------------------------------------------------------------------===//
321// Simple Descriptor Constructors and other Methods
322//===----------------------------------------------------------------------===//
323
Devang Patele9f8f5e2010-05-07 20:54:48 +0000324DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000325 if (!N) return;
326 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
327 DbgNode = 0;
328 }
329}
330
Devang Patel68afdc32009-01-05 18:33:01 +0000331unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000332 if (!DbgNode)
333 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000334 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000335}
Chris Lattnera45664f2008-11-10 02:56:27 +0000336
Devang Patelc4999d72009-07-22 18:23:44 +0000337/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000338/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000339void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000340 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000341 return;
342
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000343 // Since we use a TrackingVH for the node, its easy for clients to manufacture
344 // legitimate situations where they want to replaceAllUsesWith() on something
345 // which, due to uniquing, has merged with the source. We shield clients from
346 // this detail by allowing a value to be replaced with replaceAllUsesWith()
347 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000348 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000349 MDNode *Node = const_cast<MDNode*>(DbgNode);
350 const MDNode *DN = D;
351 const Value *V = cast_or_null<Value>(DN);
352 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000353 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000354 }
Devang Patelc4999d72009-07-22 18:23:44 +0000355}
356
Devang Patel0a2551d2010-12-08 20:18:20 +0000357/// replaceAllUsesWith - Replace all uses of debug info referenced by
358/// this descriptor.
359void DIType::replaceAllUsesWith(MDNode *D) {
360 if (!DbgNode)
361 return;
362
363 // Since we use a TrackingVH for the node, its easy for clients to manufacture
364 // legitimate situations where they want to replaceAllUsesWith() on something
365 // which, due to uniquing, has merged with the source. We shield clients from
366 // this detail by allowing a value to be replaced with replaceAllUsesWith()
367 // itself.
368 if (DbgNode != D) {
369 MDNode *Node = const_cast<MDNode*>(DbgNode);
370 const MDNode *DN = D;
371 const Value *V = cast_or_null<Value>(DN);
372 Node->replaceAllUsesWith(const_cast<Value*>(V));
373 MDNode::deleteTemporary(Node);
374 }
375}
376
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000377/// isUnsignedDIType - Return true if type encoding is unsigned.
378bool DIType::isUnsignedDIType() {
379 DIDerivedType DTy(DbgNode);
380 if (DTy.Verify())
381 return DTy.getTypeDerivedFrom().isUnsignedDIType();
382
383 DIBasicType BTy(DbgNode);
384 if (BTy.Verify()) {
385 unsigned Encoding = BTy.getEncoding();
386 if (Encoding == dwarf::DW_ATE_unsigned ||
387 Encoding == dwarf::DW_ATE_unsigned_char)
388 return true;
389 }
390 return false;
391}
392
Devang Patelb79b5352009-01-19 23:21:49 +0000393/// Verify - Verify that a compile unit is well formed.
394bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000395 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000396 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000397 StringRef N = getFilename();
398 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000399 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000400 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000401 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000402}
403
Eric Christopherb8ca9882012-03-29 08:42:56 +0000404/// Verify - Verify that an ObjC property is well formed.
405bool DIObjCProperty::Verify() const {
406 if (!DbgNode)
407 return false;
408 unsigned Tag = getTag();
Eric Christopher6c31ee22012-03-29 21:35:05 +0000409 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
Eric Christopherb8ca9882012-03-29 08:42:56 +0000410 DIType Ty = getType();
411 if (!Ty.Verify()) return false;
412
413 // Don't worry about the rest of the strings for now.
414 return true;
415}
416
Devang Patelb79b5352009-01-19 23:21:49 +0000417/// Verify - Verify that a type descriptor is well formed.
418bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000419 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000420 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000421 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000422 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000423 unsigned Tag = getTag();
424 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
425 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Eric Christopher791e6292012-05-19 01:36:37 +0000426 Tag != dwarf::DW_TAG_reference_type &&
427 Tag != dwarf::DW_TAG_rvalue_reference_type &&
428 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
429 Tag != dwarf::DW_TAG_array_type &&
430 Tag != dwarf::DW_TAG_enumeration_type &&
431 Tag != dwarf::DW_TAG_subroutine_type &&
432 getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000433 return false;
434 return true;
435}
436
Devang Patel0c4720c2010-08-23 18:25:56 +0000437/// Verify - Verify that a basic type descriptor is well formed.
438bool DIBasicType::Verify() const {
439 return isBasicType();
440}
441
442/// Verify - Verify that a derived type descriptor is well formed.
443bool DIDerivedType::Verify() const {
444 return isDerivedType();
445}
446
Devang Patelb79b5352009-01-19 23:21:49 +0000447/// Verify - Verify that a composite type descriptor is well formed.
448bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000449 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000450 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000451 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000452 return false;
453
Devang Patelb79b5352009-01-19 23:21:49 +0000454 return true;
455}
456
457/// Verify - Verify that a subprogram descriptor is well formed.
458bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000459 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000460 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000461
Devang Patel94c7ddb2011-08-16 22:09:43 +0000462 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000463 return false;
464
465 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000466 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000467 return false;
468 return true;
469}
470
471/// Verify - Verify that a global variable descriptor is well formed.
472bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000473 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000474 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000475
Devang Patel65dbc902009-11-25 17:36:49 +0000476 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000477 return false;
478
Devang Patel94c7ddb2011-08-16 22:09:43 +0000479 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000480 return false;
481
482 DIType Ty = getType();
483 if (!Ty.Verify())
484 return false;
485
Devang Patel27398962010-08-09 21:39:24 +0000486 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000487 return false;
488
489 return true;
490}
491
492/// Verify - Verify that a variable descriptor is well formed.
493bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000494 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000495 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000496
Devang Patel94c7ddb2011-08-16 22:09:43 +0000497 if (getContext() && !getContext().Verify())
Devang Patel62077af2010-05-07 21:42:24 +0000498 return false;
499
Devang Patelb79b5352009-01-19 23:21:49 +0000500 DIType Ty = getType();
501 if (!Ty.Verify())
502 return false;
503
Devang Patelb79b5352009-01-19 23:21:49 +0000504 return true;
505}
506
Devang Patel3c91b052010-03-08 20:52:55 +0000507/// Verify - Verify that a location descriptor is well formed.
508bool DILocation::Verify() const {
509 if (!DbgNode)
510 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000511
Devang Patel3c91b052010-03-08 20:52:55 +0000512 return DbgNode->getNumOperands() == 4;
513}
514
Devang Patel47e22652010-05-07 23:04:32 +0000515/// Verify - Verify that a namespace descriptor is well formed.
516bool DINameSpace::Verify() const {
517 if (!DbgNode)
518 return false;
519 if (getName().empty())
520 return false;
Devang Patel47e22652010-05-07 23:04:32 +0000521 return true;
522}
523
Devang Patel36375ee2009-02-17 21:23:59 +0000524/// getOriginalTypeSize - If this type is derived from a base type then
525/// return base type size.
526uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000527 unsigned Tag = getTag();
Eric Christopher2e1b0c02011-12-16 23:42:45 +0000528
Bill Wendlinge8778242012-06-26 23:22:18 +0000529 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
530 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
531 Tag != dwarf::DW_TAG_restrict_type)
532 return getSizeInBits();
Jim Grosbache62b6902010-07-21 21:36:25 +0000533
Bill Wendlinge8778242012-06-26 23:22:18 +0000534 DIType BaseType = getTypeDerivedFrom();
535
536 // If this type is not derived from any type then take conservative approach.
537 if (!BaseType.isValid())
538 return getSizeInBits();
539
540 // If this is a derived type, go ahead and get the base type, unless it's a
541 // reference then it's just the size of the field. Pointer types have no need
542 // of this since they're a different type of qualification on the type.
543 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
544 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
545 return getSizeInBits();
546
547 if (BaseType.isDerivedType())
548 return DIDerivedType(BaseType).getOriginalTypeSize();
549
550 return BaseType.getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000551}
Devang Patelb79b5352009-01-19 23:21:49 +0000552
Devang Patel6588abf2012-02-06 17:49:43 +0000553/// getObjCProperty - Return property node, if this ivar is associated with one.
554MDNode *DIDerivedType::getObjCProperty() const {
555 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
556 return NULL;
557 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
558}
559
Jim Grosbache62b6902010-07-21 21:36:25 +0000560/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000561/// information for an inlined function arguments.
562bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
563 assert(CurFn && "Invalid function");
564 if (!getContext().isSubprogram())
565 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000566 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000567 // does not describe current function.
Bill Wendlinge8778242012-06-26 23:22:18 +0000568 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel719f6a92010-04-29 20:40:36 +0000569}
570
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000571/// describes - Return true if this subprogram provides debugging
572/// information for the function F.
573bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000574 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000575 if (F == getFunction())
576 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000577 StringRef Name = getLinkageName();
578 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000579 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000580 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000581 return true;
582 return false;
583}
584
Jim Grosbache62b6902010-07-21 21:36:25 +0000585unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000586 assert (DbgNode && "Invalid subprogram descriptor!");
587 if (DbgNode->getNumOperands() == 16)
588 return getUnsignedField(15);
589 return 0;
590}
591
Devang Patel93d39be2011-08-19 23:28:12 +0000592MDNode *DISubprogram::getVariablesNodes() const {
593 if (!DbgNode || DbgNode->getNumOperands() <= 19)
594 return NULL;
595 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
596 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
597 return NULL;
598}
599
600DIArray DISubprogram::getVariables() const {
601 if (!DbgNode || DbgNode->getNumOperands() <= 19)
602 return DIArray();
603 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
604 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
605 return DIArray(A);
606 return DIArray();
607}
608
Devang Patel65dbc902009-11-25 17:36:49 +0000609StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000610 if (!DbgNode)
611 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000612 if (isLexicalBlockFile())
613 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbache62b6902010-07-21 21:36:25 +0000614 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000615 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000616 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000617 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000618 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000619 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000620 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000621 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000622 if (isType())
623 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000624 if (isFile())
625 return DIFile(DbgNode).getFilename();
Craig Topper85814382012-02-07 05:05:23 +0000626 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000627}
628
Devang Patel65dbc902009-11-25 17:36:49 +0000629StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000630 if (!DbgNode)
631 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000632 if (isLexicalBlockFile())
633 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbache62b6902010-07-21 21:36:25 +0000634 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000635 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000636 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000637 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000638 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000639 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000640 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000641 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000642 if (isType())
643 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000644 if (isFile())
645 return DIFile(DbgNode).getDirectory();
Craig Topper85814382012-02-07 05:05:23 +0000646 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000647}
648
Devang Patel94c7ddb2011-08-16 22:09:43 +0000649DIArray DICompileUnit::getEnumTypes() const {
650 if (!DbgNode || DbgNode->getNumOperands() < 14)
651 return DIArray();
652
653 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
654 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
655 return DIArray(A);
656 return DIArray();
657}
658
659DIArray DICompileUnit::getRetainedTypes() const {
660 if (!DbgNode || DbgNode->getNumOperands() < 14)
661 return DIArray();
662
663 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
664 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
665 return DIArray(A);
666 return DIArray();
667}
668
669DIArray DICompileUnit::getSubprograms() const {
670 if (!DbgNode || DbgNode->getNumOperands() < 14)
671 return DIArray();
672
673 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
NAKAMURA Takumi2587a8a2012-12-01 02:23:45 +0000674 if (N->getNumOperands() > 0)
675 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
676 return DIArray(A);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000677 return DIArray();
678}
679
680
681DIArray DICompileUnit::getGlobalVariables() const {
682 if (!DbgNode || DbgNode->getNumOperands() < 14)
683 return DIArray();
684
685 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
686 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
687 return DIArray(A);
688 return DIArray();
689}
690
Devang Patel62367042010-11-10 22:19:21 +0000691/// fixupObjcLikeName - Replace contains special characters used
692/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000693static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
694 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000695 for (size_t i = 0, e = Str.size(); i < e; ++i) {
696 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000697 if (C == '[')
698 isObjCLike = true;
699
700 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
701 C == '+' || C == '(' || C == ')'))
702 Out.push_back('.');
703 else
704 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000705 }
706}
707
Eric Christopher5f214ae2012-11-20 00:15:36 +0000708/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
Devang Patel62367042010-11-10 22:19:21 +0000709/// suitable to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000710NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000711 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000712 StringRef FName = "fn";
713 if (Fn.getFunction())
714 FName = Fn.getFunction()->getName();
715 else
716 FName = Fn.getName();
717 char One = '\1';
718 if (FName.startswith(StringRef(&One, 1)))
719 FName = FName.substr(1);
720 fixupObjcLikeName(FName, Name);
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000721 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000722}
723
Duncan Sands291bb702011-03-02 20:30:37 +0000724/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
725/// to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000726NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000727 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000728 StringRef FName = "fn";
729 if (Fn.getFunction())
730 FName = Fn.getFunction()->getName();
731 else
732 FName = Fn.getName();
733 char One = '\1';
734 if (FName.startswith(StringRef(&One, 1)))
735 FName = FName.substr(1);
736 fixupObjcLikeName(FName, Name);
Eric Christopher5f214ae2012-11-20 00:15:36 +0000737
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000738 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000739}
740
Devang Patel23336b42011-07-19 19:41:54 +0000741/// createInlinedVariable - Create a new inlined variable based on current
742/// variable.
743/// @param DV Current Variable.
744/// @param InlinedScope Location at current variable is inlined.
745DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
746 LLVMContext &VMContext) {
747 SmallVector<Value *, 16> Elts;
748 // Insert inlined scope as 7th element.
749 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
750 i == 7 ? Elts.push_back(InlinedScope) :
751 Elts.push_back(DV->getOperand(i));
752 return DIVariable(MDNode::get(VMContext, Elts));
753}
Devang Patel1a7ca032010-09-28 18:08:20 +0000754
Devang Patelb549bcf2011-08-10 21:50:54 +0000755/// cleanseInlinedVariable - Remove inlined scope from the variable.
756DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
757 SmallVector<Value *, 16> Elts;
758 // Insert inlined scope as 7th element.
759 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000760 i == 7 ?
Bill Wendling0735e812012-07-06 17:46:28 +0000761 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelb549bcf2011-08-10 21:50:54 +0000762 Elts.push_back(DV->getOperand(i));
763 return DIVariable(MDNode::get(VMContext, Elts));
764}
765
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000766/// getDISubprogram - Find subprogram that is enclosing this scope.
767DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
768 DIDescriptor D(Scope);
769 if (D.isSubprogram())
770 return DISubprogram(Scope);
771
772 if (D.isLexicalBlockFile())
773 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
Eric Christopher5f214ae2012-11-20 00:15:36 +0000774
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000775 if (D.isLexicalBlock())
776 return getDISubprogram(DILexicalBlock(Scope).getContext());
777
778 return DISubprogram();
779}
780
781/// getDICompositeType - Find underlying composite type.
782DICompositeType llvm::getDICompositeType(DIType T) {
783 if (T.isCompositeType())
784 return DICompositeType(T);
785
786 if (T.isDerivedType())
787 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
788
789 return DICompositeType();
790}
791
792/// isSubprogramContext - Return true if Context is either a subprogram
793/// or another context nested inside a subprogram.
794bool llvm::isSubprogramContext(const MDNode *Context) {
795 if (!Context)
796 return false;
797 DIDescriptor D(Context);
798 if (D.isSubprogram())
799 return true;
800 if (D.isType())
801 return isSubprogramContext(DIType(Context).getContext());
802 return false;
803}
804
Devang Pateld2f79a12009-07-28 19:55:13 +0000805//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000806// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000807//===----------------------------------------------------------------------===//
808
Devang Patel98c65172009-07-30 18:25:15 +0000809/// processModule - Process entire module and collect debug info.
Eric Christopherc4639d62012-11-19 22:42:15 +0000810void DebugInfoFinder::processModule(const Module &M) {
Devang Patelcd2db162011-10-17 22:30:34 +0000811 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
812 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
813 DICompileUnit CU(CU_Nodes->getOperand(i));
814 addCompileUnit(CU);
815 if (CU.getVersion() > LLVMDebugVersion10) {
Devang Patel9f997212012-02-08 00:17:07 +0000816 DIArray GVs = CU.getGlobalVariables();
817 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
818 DIGlobalVariable DIG(GVs.getElement(i));
819 if (addGlobalVariable(DIG))
820 processType(DIG.getType());
821 }
822 DIArray SPs = CU.getSubprograms();
823 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
824 processSubprogram(DISubprogram(SPs.getElement(i)));
825 DIArray EnumTypes = CU.getEnumTypes();
826 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
827 processType(DIType(EnumTypes.getElement(i)));
828 DIArray RetainedTypes = CU.getRetainedTypes();
829 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
830 processType(DIType(RetainedTypes.getElement(i)));
831 return;
Devang Patelcd2db162011-10-17 22:30:34 +0000832 }
833 }
834 }
Devang Patelfa515ec2011-09-06 17:40:08 +0000835
Eric Christopherc4639d62012-11-19 22:42:15 +0000836 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000837 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
838 FI != FE; ++FI)
839 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
840 BI != BE; ++BI) {
Eric Christopherc4639d62012-11-19 22:42:15 +0000841 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000842 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000843
Chris Lattner28a9bf62010-04-02 20:44:29 +0000844 DebugLoc Loc = BI->getDebugLoc();
845 if (Loc.isUnknown())
846 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000847
Chris Lattner28a9bf62010-04-02 20:44:29 +0000848 LLVMContext &Ctx = BI->getContext();
849 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000850
Chris Lattner28a9bf62010-04-02 20:44:29 +0000851 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000852 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000853 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000854 processSubprogram(DISubprogram(Scope));
Eric Christopher6618a242011-10-11 22:59:11 +0000855 else if (Scope.isLexicalBlockFile()) {
856 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
857 processLexicalBlock(DILexicalBlock(DBF.getScope()));
858 }
Chris Lattner28a9bf62010-04-02 20:44:29 +0000859 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000860 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000861
Chris Lattner28a9bf62010-04-02 20:44:29 +0000862 if (MDNode *IA = Loc.getInlinedAt(Ctx))
863 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000864 }
Devang Patele4b27562009-08-28 23:24:31 +0000865
Devang Patelfd5fdc32010-06-28 05:53:08 +0000866 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
867 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
868 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
869 if (addGlobalVariable(DIG)) {
Devang Patelfa515ec2011-09-06 17:40:08 +0000870 if (DIG.getVersion() <= LLVMDebugVersion10)
871 addCompileUnit(DIG.getCompileUnit());
Devang Patelfd5fdc32010-06-28 05:53:08 +0000872 processType(DIG.getType());
873 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000874 }
875 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000876
877 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
878 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
879 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000880}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000881
Devang Patel6daf99b2009-11-10 22:05:35 +0000882/// processLocation - Process DILocation.
883void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000884 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000885 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000886 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000887 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000888 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000889 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000890 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000891 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6618a242011-10-11 22:59:11 +0000892 else if (S.isLexicalBlockFile()) {
893 DILexicalBlockFile DBF = DILexicalBlockFile(S);
894 processLexicalBlock(DILexicalBlock(DBF.getScope()));
895 }
Devang Patel6daf99b2009-11-10 22:05:35 +0000896 processLocation(Loc.getOrigLocation());
897}
898
Devang Patel98c65172009-07-30 18:25:15 +0000899/// processType - Process DIType.
900void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000901 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000902 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000903 if (DT.getVersion() <= LLVMDebugVersion10)
904 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000905 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000906 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000907 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000908 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000909 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
910 DIDescriptor D = DA.getElement(i);
911 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000912 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000913 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000914 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000915 }
Devang Patel6ceea332009-08-31 18:49:10 +0000916 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000917 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000918 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000919 }
920}
921
Devang Patelbeab41b2009-10-07 22:04:08 +0000922/// processLexicalBlock
923void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000924 DIScope Context = LB.getContext();
925 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000926 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6618a242011-10-11 22:59:11 +0000927 else if (Context.isLexicalBlockFile()) {
928 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
929 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
930 }
Devang Patelbeab41b2009-10-07 22:04:08 +0000931 else
Devang Patel2db49d72010-05-07 18:11:54 +0000932 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000933}
934
Devang Patel98c65172009-07-30 18:25:15 +0000935/// processSubprogram - Process DISubprogram.
936void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000937 if (!addSubprogram(SP))
938 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000939 if (SP.getVersion() <= LLVMDebugVersion10)
940 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000941 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000942}
943
Devang Patelb4d31302009-07-31 18:18:52 +0000944/// processDeclare - Process DbgDeclareInst.
Eric Christopherc4639d62012-11-19 22:42:15 +0000945void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +0000946 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
947 if (!N) return;
948
949 DIDescriptor DV(N);
950 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +0000951 return;
952
Devang Patel2db49d72010-05-07 18:11:54 +0000953 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +0000954 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000955 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
956 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3c91b052010-03-08 20:52:55 +0000957 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +0000958}
959
Devang Patel72bcdb62009-08-10 22:09:58 +0000960/// addType - Add type into Tys.
961bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +0000962 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +0000963 return false;
964
Devang Patel2db49d72010-05-07 18:11:54 +0000965 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +0000966 return false;
967
Devang Patel2db49d72010-05-07 18:11:54 +0000968 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +0000969 return true;
970}
971
Devang Pateld2f79a12009-07-28 19:55:13 +0000972/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000973bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +0000974 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +0000975 return false;
976
Devang Patel2db49d72010-05-07 18:11:54 +0000977 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +0000978 return false;
979
Devang Patel2db49d72010-05-07 18:11:54 +0000980 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +0000981 return true;
982}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000983
Devang Pateld2f79a12009-07-28 19:55:13 +0000984/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000985bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +0000986 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +0000987 return false;
988
Devang Patel2db49d72010-05-07 18:11:54 +0000989 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +0000990 return false;
991
Devang Patel2db49d72010-05-07 18:11:54 +0000992 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +0000993 return true;
994}
995
996// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000997bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +0000998 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +0000999 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001000
Devang Patel2db49d72010-05-07 18:11:54 +00001001 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001002 return false;
1003
Devang Patel2db49d72010-05-07 18:11:54 +00001004 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001005 return true;
1006}
1007
Bill Wendlinge8778242012-06-26 23:22:18 +00001008//===----------------------------------------------------------------------===//
1009// DIDescriptor: dump routines for all descriptors.
1010//===----------------------------------------------------------------------===//
1011
1012/// dump - Print descriptor to dbgs() with a newline.
1013void DIDescriptor::dump() const {
1014 print(dbgs()); dbgs() << '\n';
1015}
1016
1017/// print - Print descriptor.
1018void DIDescriptor::print(raw_ostream &OS) const {
1019 if (!DbgNode) return;
1020
Bill Wendling97a75922012-06-28 02:17:58 +00001021 if (const char *Tag = dwarf::TagString(getTag()))
1022 OS << "[ " << Tag << " ]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001023
1024 if (this->isSubrange()) {
1025 DISubrange(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001026 } else if (this->isCompileUnit()) {
1027 DICompileUnit(DbgNode).printInternal(OS);
1028 } else if (this->isFile()) {
1029 DIFile(DbgNode).printInternal(OS);
1030 } else if (this->isEnumerator()) {
1031 DIEnumerator(DbgNode).printInternal(OS);
1032 } else if (this->isBasicType()) {
1033 DIType(DbgNode).printInternal(OS);
1034 } else if (this->isDerivedType()) {
1035 DIDerivedType(DbgNode).printInternal(OS);
1036 } else if (this->isCompositeType()) {
1037 DICompositeType(DbgNode).printInternal(OS);
1038 } else if (this->isSubprogram()) {
1039 DISubprogram(DbgNode).printInternal(OS);
1040 } else if (this->isGlobalVariable()) {
1041 DIGlobalVariable(DbgNode).printInternal(OS);
1042 } else if (this->isVariable()) {
1043 DIVariable(DbgNode).printInternal(OS);
Bill Wendling2da1a162012-07-06 19:12:31 +00001044 } else if (this->isObjCProperty()) {
1045 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling625252f2012-07-06 23:06:16 +00001046 } else if (this->isScope()) {
1047 DIScope(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001048 }
1049}
1050
1051void DISubrange::printInternal(raw_ostream &OS) const {
Bill Wendling9493dae2012-12-04 21:34:03 +00001052 int64_t Count = getCount();
1053 if (Count != -1)
1054 OS << " [" << getLo() << ", " << Count - 1 << ']';
1055 else
Bill Wendling46defa52012-12-05 21:43:30 +00001056 OS << " [unbounded]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001057}
1058
1059void DIScope::printInternal(raw_ostream &OS) const {
1060 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1061}
1062
1063void DICompileUnit::printInternal(raw_ostream &OS) const {
1064 DIScope::printInternal(OS);
1065 if (unsigned Lang = getLanguage())
1066 OS << " [" << dwarf::LanguageString(Lang) << ']';
1067}
1068
1069void DIEnumerator::printInternal(raw_ostream &OS) const {
1070 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1071}
1072
1073void DIType::printInternal(raw_ostream &OS) const {
1074 if (!DbgNode) return;
1075
1076 StringRef Res = getName();
1077 if (!Res.empty())
1078 OS << " [" << Res << "]";
1079
1080 // TODO: Print context?
1081
1082 OS << " [line " << getLineNumber()
1083 << ", size " << getSizeInBits()
1084 << ", align " << getAlignInBits()
1085 << ", offset " << getOffsetInBits();
1086 if (isBasicType())
Eric Christopher5f214ae2012-11-20 00:15:36 +00001087 if (const char *Enc =
Bill Wendlingfc1c70a2012-06-28 02:12:20 +00001088 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1089 OS << ", enc " << Enc;
Bill Wendlinge8778242012-06-26 23:22:18 +00001090 OS << "]";
1091
1092 if (isPrivate())
1093 OS << " [private]";
1094 else if (isProtected())
1095 OS << " [protected]";
1096
1097 if (isForwardDecl())
1098 OS << " [fwd]";
1099}
1100
1101void DIDerivedType::printInternal(raw_ostream &OS) const {
1102 DIType::printInternal(OS);
1103 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1104}
1105
1106void DICompositeType::printInternal(raw_ostream &OS) const {
1107 DIType::printInternal(OS);
1108 DIArray A = getTypeArray();
1109 OS << " [" << A.getNumElements() << " elements]";
1110}
1111
1112void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendlinge8778242012-06-26 23:22:18 +00001113 // TODO : Print context
Bill Wendlinge8778242012-06-26 23:22:18 +00001114 OS << " [line " << getLineNumber() << ']';
1115
1116 if (isLocalToUnit())
1117 OS << " [local]";
1118
1119 if (isDefinition())
1120 OS << " [def]";
1121
1122 if (getScopeLineNumber() != getLineNumber())
1123 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendlingee17cee2012-07-06 23:43:12 +00001124
David Blaikiee302b6c2013-01-05 21:39:33 +00001125 if (isPrivate())
1126 OS << " [private]";
1127 else if (isProtected())
1128 OS << " [protected]";
1129
Bill Wendlingee17cee2012-07-06 23:43:12 +00001130 StringRef Res = getName();
1131 if (!Res.empty())
1132 OS << " [" << Res << ']';
Bill Wendlinge8778242012-06-26 23:22:18 +00001133}
1134
1135void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1136 StringRef Res = getName();
1137 if (!Res.empty())
1138 OS << " [" << Res << ']';
1139
1140 OS << " [line " << getLineNumber() << ']';
1141
1142 // TODO : Print context
1143
1144 if (isLocalToUnit())
1145 OS << " [local]";
1146
1147 if (isDefinition())
1148 OS << " [def]";
1149}
1150
1151void DIVariable::printInternal(raw_ostream &OS) const {
1152 StringRef Res = getName();
1153 if (!Res.empty())
1154 OS << " [" << Res << ']';
1155
1156 OS << " [line " << getLineNumber() << ']';
1157}
1158
Bill Wendling2da1a162012-07-06 19:12:31 +00001159void DIObjCProperty::printInternal(raw_ostream &OS) const {
1160 StringRef Name = getObjCPropertyName();
1161 if (!Name.empty())
1162 OS << " [" << Name << ']';
1163
1164 OS << " [line " << getLineNumber()
1165 << ", properties " << getUnsignedField(6) << ']';
1166}
1167
Bill Wendlinge8778242012-06-26 23:22:18 +00001168static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1169 const LLVMContext &Ctx) {
1170 if (!DL.isUnknown()) { // Print source line info.
1171 DIScope Scope(DL.getScope(Ctx));
1172 // Omit the directory, because it's likely to be long and uninteresting.
1173 if (Scope.Verify())
1174 CommentOS << Scope.getFilename();
1175 else
1176 CommentOS << "<unknown>";
1177 CommentOS << ':' << DL.getLine();
1178 if (DL.getCol() != 0)
1179 CommentOS << ':' << DL.getCol();
1180 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1181 if (!InlinedAtDL.isUnknown()) {
1182 CommentOS << " @[ ";
1183 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1184 CommentOS << " ]";
1185 }
1186 }
1187}
1188
1189void DIVariable::printExtendedName(raw_ostream &OS) const {
1190 const LLVMContext &Ctx = DbgNode->getContext();
1191 StringRef Res = getName();
1192 if (!Res.empty())
1193 OS << Res << "," << getLineNumber();
1194 if (MDNode *InlinedAt = getInlinedAt()) {
1195 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1196 if (!InlinedAtDL.isUnknown()) {
1197 OS << " @[";
1198 printDebugLoc(InlinedAtDL, OS, Ctx);
1199 OS << "]";
1200 }
1201 }
1202}