blob: d2883080977a260e418fc988d9f7a7e5785da042 [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:
Chris Lattnera45664f2008-11-10 02:56:27 +0000215 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 &&
David Blaikie62fdfb52013-01-07 05:51:15 +0000426 Tag != dwarf::DW_TAG_ptr_to_member_type &&
Eric Christopher791e6292012-05-19 01:36:37 +0000427 Tag != dwarf::DW_TAG_reference_type &&
428 Tag != dwarf::DW_TAG_rvalue_reference_type &&
429 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
430 Tag != dwarf::DW_TAG_array_type &&
431 Tag != dwarf::DW_TAG_enumeration_type &&
432 Tag != dwarf::DW_TAG_subroutine_type &&
433 getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000434 return false;
435 return true;
436}
437
Devang Patel0c4720c2010-08-23 18:25:56 +0000438/// Verify - Verify that a basic type descriptor is well formed.
439bool DIBasicType::Verify() const {
440 return isBasicType();
441}
442
443/// Verify - Verify that a derived type descriptor is well formed.
444bool DIDerivedType::Verify() const {
445 return isDerivedType();
446}
447
Devang Patelb79b5352009-01-19 23:21:49 +0000448/// Verify - Verify that a composite type descriptor is well formed.
449bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000450 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000451 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000452 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000453 return false;
454
Devang Patelb79b5352009-01-19 23:21:49 +0000455 return true;
456}
457
458/// Verify - Verify that a subprogram descriptor is well formed.
459bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000460 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000461 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000462
Devang Patel94c7ddb2011-08-16 22:09:43 +0000463 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000464 return false;
465
466 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000467 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000468 return false;
469 return true;
470}
471
472/// Verify - Verify that a global variable descriptor is well formed.
473bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000474 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000475 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000476
Devang Patel65dbc902009-11-25 17:36:49 +0000477 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000478 return false;
479
Devang Patel94c7ddb2011-08-16 22:09:43 +0000480 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000481 return false;
482
483 DIType Ty = getType();
484 if (!Ty.Verify())
485 return false;
486
Devang Patel27398962010-08-09 21:39:24 +0000487 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000488 return false;
489
490 return true;
491}
492
493/// Verify - Verify that a variable descriptor is well formed.
494bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000495 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000496 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000497
Devang Patel94c7ddb2011-08-16 22:09:43 +0000498 if (getContext() && !getContext().Verify())
Devang Patel62077af2010-05-07 21:42:24 +0000499 return false;
500
Devang Patelb79b5352009-01-19 23:21:49 +0000501 DIType Ty = getType();
502 if (!Ty.Verify())
503 return false;
504
Devang Patelb79b5352009-01-19 23:21:49 +0000505 return true;
506}
507
Devang Patel3c91b052010-03-08 20:52:55 +0000508/// Verify - Verify that a location descriptor is well formed.
509bool DILocation::Verify() const {
510 if (!DbgNode)
511 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000512
Devang Patel3c91b052010-03-08 20:52:55 +0000513 return DbgNode->getNumOperands() == 4;
514}
515
Devang Patel47e22652010-05-07 23:04:32 +0000516/// Verify - Verify that a namespace descriptor is well formed.
517bool DINameSpace::Verify() const {
518 if (!DbgNode)
519 return false;
520 if (getName().empty())
521 return false;
Devang Patel47e22652010-05-07 23:04:32 +0000522 return true;
523}
524
Devang Patel36375ee2009-02-17 21:23:59 +0000525/// getOriginalTypeSize - If this type is derived from a base type then
526/// return base type size.
527uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000528 unsigned Tag = getTag();
Eric Christopher2e1b0c02011-12-16 23:42:45 +0000529
Bill Wendlinge8778242012-06-26 23:22:18 +0000530 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
531 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
532 Tag != dwarf::DW_TAG_restrict_type)
533 return getSizeInBits();
Jim Grosbache62b6902010-07-21 21:36:25 +0000534
Bill Wendlinge8778242012-06-26 23:22:18 +0000535 DIType BaseType = getTypeDerivedFrom();
536
537 // If this type is not derived from any type then take conservative approach.
538 if (!BaseType.isValid())
539 return getSizeInBits();
540
541 // If this is a derived type, go ahead and get the base type, unless it's a
542 // reference then it's just the size of the field. Pointer types have no need
543 // of this since they're a different type of qualification on the type.
544 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
545 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
546 return getSizeInBits();
547
548 if (BaseType.isDerivedType())
549 return DIDerivedType(BaseType).getOriginalTypeSize();
550
551 return BaseType.getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000552}
Devang Patelb79b5352009-01-19 23:21:49 +0000553
Devang Patel6588abf2012-02-06 17:49:43 +0000554/// getObjCProperty - Return property node, if this ivar is associated with one.
555MDNode *DIDerivedType::getObjCProperty() const {
556 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
557 return NULL;
558 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
559}
560
Jim Grosbache62b6902010-07-21 21:36:25 +0000561/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000562/// information for an inlined function arguments.
563bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
564 assert(CurFn && "Invalid function");
565 if (!getContext().isSubprogram())
566 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000567 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000568 // does not describe current function.
Bill Wendlinge8778242012-06-26 23:22:18 +0000569 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel719f6a92010-04-29 20:40:36 +0000570}
571
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000572/// describes - Return true if this subprogram provides debugging
573/// information for the function F.
574bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000575 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000576 if (F == getFunction())
577 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000578 StringRef Name = getLinkageName();
579 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000580 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000581 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000582 return true;
583 return false;
584}
585
Jim Grosbache62b6902010-07-21 21:36:25 +0000586unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000587 assert (DbgNode && "Invalid subprogram descriptor!");
588 if (DbgNode->getNumOperands() == 16)
589 return getUnsignedField(15);
590 return 0;
591}
592
Devang Patel93d39be2011-08-19 23:28:12 +0000593MDNode *DISubprogram::getVariablesNodes() const {
594 if (!DbgNode || DbgNode->getNumOperands() <= 19)
595 return NULL;
596 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
597 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
598 return NULL;
599}
600
601DIArray DISubprogram::getVariables() const {
602 if (!DbgNode || DbgNode->getNumOperands() <= 19)
603 return DIArray();
604 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
605 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
606 return DIArray(A);
607 return DIArray();
608}
609
Devang Patel65dbc902009-11-25 17:36:49 +0000610StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000611 if (!DbgNode)
612 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000613 if (isLexicalBlockFile())
614 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbache62b6902010-07-21 21:36:25 +0000615 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000616 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000617 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000618 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000619 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000620 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000621 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000622 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000623 if (isType())
624 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000625 if (isFile())
626 return DIFile(DbgNode).getFilename();
Craig Topper85814382012-02-07 05:05:23 +0000627 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000628}
629
Devang Patel65dbc902009-11-25 17:36:49 +0000630StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000631 if (!DbgNode)
632 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000633 if (isLexicalBlockFile())
634 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbache62b6902010-07-21 21:36:25 +0000635 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000636 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000637 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000638 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000639 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000640 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000641 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000642 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000643 if (isType())
644 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000645 if (isFile())
646 return DIFile(DbgNode).getDirectory();
Craig Topper85814382012-02-07 05:05:23 +0000647 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000648}
649
Devang Patel94c7ddb2011-08-16 22:09:43 +0000650DIArray DICompileUnit::getEnumTypes() const {
651 if (!DbgNode || DbgNode->getNumOperands() < 14)
652 return DIArray();
653
654 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
655 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
656 return DIArray(A);
657 return DIArray();
658}
659
660DIArray DICompileUnit::getRetainedTypes() const {
661 if (!DbgNode || DbgNode->getNumOperands() < 14)
662 return DIArray();
663
664 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
665 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
666 return DIArray(A);
667 return DIArray();
668}
669
670DIArray DICompileUnit::getSubprograms() const {
671 if (!DbgNode || DbgNode->getNumOperands() < 14)
672 return DIArray();
673
674 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
NAKAMURA Takumi2587a8a2012-12-01 02:23:45 +0000675 if (N->getNumOperands() > 0)
676 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
677 return DIArray(A);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000678 return DIArray();
679}
680
681
682DIArray DICompileUnit::getGlobalVariables() const {
683 if (!DbgNode || DbgNode->getNumOperands() < 14)
684 return DIArray();
685
686 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
687 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
688 return DIArray(A);
689 return DIArray();
690}
691
Devang Patel62367042010-11-10 22:19:21 +0000692/// fixupObjcLikeName - Replace contains special characters used
693/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000694static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
695 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000696 for (size_t i = 0, e = Str.size(); i < e; ++i) {
697 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000698 if (C == '[')
699 isObjCLike = true;
700
701 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
702 C == '+' || C == '(' || C == ')'))
703 Out.push_back('.');
704 else
705 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000706 }
707}
708
Eric Christopher5f214ae2012-11-20 00:15:36 +0000709/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
Devang Patel62367042010-11-10 22:19:21 +0000710/// suitable to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000711NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000712 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000713 StringRef FName = "fn";
714 if (Fn.getFunction())
715 FName = Fn.getFunction()->getName();
716 else
717 FName = Fn.getName();
718 char One = '\1';
719 if (FName.startswith(StringRef(&One, 1)))
720 FName = FName.substr(1);
721 fixupObjcLikeName(FName, Name);
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000722 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000723}
724
Duncan Sands291bb702011-03-02 20:30:37 +0000725/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
726/// to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000727NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000728 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000729 StringRef FName = "fn";
730 if (Fn.getFunction())
731 FName = Fn.getFunction()->getName();
732 else
733 FName = Fn.getName();
734 char One = '\1';
735 if (FName.startswith(StringRef(&One, 1)))
736 FName = FName.substr(1);
737 fixupObjcLikeName(FName, Name);
Eric Christopher5f214ae2012-11-20 00:15:36 +0000738
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000739 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000740}
741
Devang Patel23336b42011-07-19 19:41:54 +0000742/// createInlinedVariable - Create a new inlined variable based on current
743/// variable.
744/// @param DV Current Variable.
745/// @param InlinedScope Location at current variable is inlined.
746DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
747 LLVMContext &VMContext) {
748 SmallVector<Value *, 16> Elts;
749 // Insert inlined scope as 7th element.
750 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
751 i == 7 ? Elts.push_back(InlinedScope) :
752 Elts.push_back(DV->getOperand(i));
753 return DIVariable(MDNode::get(VMContext, Elts));
754}
Devang Patel1a7ca032010-09-28 18:08:20 +0000755
Devang Patelb549bcf2011-08-10 21:50:54 +0000756/// cleanseInlinedVariable - Remove inlined scope from the variable.
757DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
758 SmallVector<Value *, 16> Elts;
759 // Insert inlined scope as 7th element.
760 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000761 i == 7 ?
Bill Wendling0735e812012-07-06 17:46:28 +0000762 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelb549bcf2011-08-10 21:50:54 +0000763 Elts.push_back(DV->getOperand(i));
764 return DIVariable(MDNode::get(VMContext, Elts));
765}
766
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000767/// getDISubprogram - Find subprogram that is enclosing this scope.
768DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
769 DIDescriptor D(Scope);
770 if (D.isSubprogram())
771 return DISubprogram(Scope);
772
773 if (D.isLexicalBlockFile())
774 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
Eric Christopher5f214ae2012-11-20 00:15:36 +0000775
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000776 if (D.isLexicalBlock())
777 return getDISubprogram(DILexicalBlock(Scope).getContext());
778
779 return DISubprogram();
780}
781
782/// getDICompositeType - Find underlying composite type.
783DICompositeType llvm::getDICompositeType(DIType T) {
784 if (T.isCompositeType())
785 return DICompositeType(T);
786
787 if (T.isDerivedType())
788 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
789
790 return DICompositeType();
791}
792
793/// isSubprogramContext - Return true if Context is either a subprogram
794/// or another context nested inside a subprogram.
795bool llvm::isSubprogramContext(const MDNode *Context) {
796 if (!Context)
797 return false;
798 DIDescriptor D(Context);
799 if (D.isSubprogram())
800 return true;
801 if (D.isType())
802 return isSubprogramContext(DIType(Context).getContext());
803 return false;
804}
805
Devang Pateld2f79a12009-07-28 19:55:13 +0000806//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000807// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000808//===----------------------------------------------------------------------===//
809
Devang Patel98c65172009-07-30 18:25:15 +0000810/// processModule - Process entire module and collect debug info.
Eric Christopherc4639d62012-11-19 22:42:15 +0000811void DebugInfoFinder::processModule(const Module &M) {
Devang Patelcd2db162011-10-17 22:30:34 +0000812 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
813 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
814 DICompileUnit CU(CU_Nodes->getOperand(i));
815 addCompileUnit(CU);
816 if (CU.getVersion() > LLVMDebugVersion10) {
Devang Patel9f997212012-02-08 00:17:07 +0000817 DIArray GVs = CU.getGlobalVariables();
818 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
819 DIGlobalVariable DIG(GVs.getElement(i));
820 if (addGlobalVariable(DIG))
821 processType(DIG.getType());
822 }
823 DIArray SPs = CU.getSubprograms();
824 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
825 processSubprogram(DISubprogram(SPs.getElement(i)));
826 DIArray EnumTypes = CU.getEnumTypes();
827 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
828 processType(DIType(EnumTypes.getElement(i)));
829 DIArray RetainedTypes = CU.getRetainedTypes();
830 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
831 processType(DIType(RetainedTypes.getElement(i)));
832 return;
Devang Patelcd2db162011-10-17 22:30:34 +0000833 }
834 }
835 }
Devang Patelfa515ec2011-09-06 17:40:08 +0000836
Eric Christopherc4639d62012-11-19 22:42:15 +0000837 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000838 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
839 FI != FE; ++FI)
840 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
841 BI != BE; ++BI) {
Eric Christopherc4639d62012-11-19 22:42:15 +0000842 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +0000843 processDeclare(DDI);
Jim Grosbache62b6902010-07-21 21:36:25 +0000844
Chris Lattner28a9bf62010-04-02 20:44:29 +0000845 DebugLoc Loc = BI->getDebugLoc();
846 if (Loc.isUnknown())
847 continue;
Jim Grosbache62b6902010-07-21 21:36:25 +0000848
Chris Lattner28a9bf62010-04-02 20:44:29 +0000849 LLVMContext &Ctx = BI->getContext();
850 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbache62b6902010-07-21 21:36:25 +0000851
Chris Lattner28a9bf62010-04-02 20:44:29 +0000852 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000853 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +0000854 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000855 processSubprogram(DISubprogram(Scope));
Eric Christopher6618a242011-10-11 22:59:11 +0000856 else if (Scope.isLexicalBlockFile()) {
857 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
858 processLexicalBlock(DILexicalBlock(DBF.getScope()));
859 }
Chris Lattner28a9bf62010-04-02 20:44:29 +0000860 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000861 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbache62b6902010-07-21 21:36:25 +0000862
Chris Lattner28a9bf62010-04-02 20:44:29 +0000863 if (MDNode *IA = Loc.getInlinedAt(Ctx))
864 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +0000865 }
Devang Patele4b27562009-08-28 23:24:31 +0000866
Devang Patelfd5fdc32010-06-28 05:53:08 +0000867 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
868 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
869 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
870 if (addGlobalVariable(DIG)) {
Devang Patelfa515ec2011-09-06 17:40:08 +0000871 if (DIG.getVersion() <= LLVMDebugVersion10)
872 addCompileUnit(DIG.getCompileUnit());
Devang Patelfd5fdc32010-06-28 05:53:08 +0000873 processType(DIG.getType());
874 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000875 }
876 }
Devang Patelfd5fdc32010-06-28 05:53:08 +0000877
878 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
879 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
880 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000881}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000882
Devang Patel6daf99b2009-11-10 22:05:35 +0000883/// processLocation - Process DILocation.
884void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000885 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000886 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000887 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000888 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000889 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000890 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000891 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000892 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6618a242011-10-11 22:59:11 +0000893 else if (S.isLexicalBlockFile()) {
894 DILexicalBlockFile DBF = DILexicalBlockFile(S);
895 processLexicalBlock(DILexicalBlock(DBF.getScope()));
896 }
Devang Patel6daf99b2009-11-10 22:05:35 +0000897 processLocation(Loc.getOrigLocation());
898}
899
Devang Patel98c65172009-07-30 18:25:15 +0000900/// processType - Process DIType.
901void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000902 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000903 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000904 if (DT.getVersion() <= LLVMDebugVersion10)
905 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000906 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000907 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000908 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000909 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000910 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
911 DIDescriptor D = DA.getElement(i);
912 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000913 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000914 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000915 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000916 }
Devang Patel6ceea332009-08-31 18:49:10 +0000917 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000918 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000919 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000920 }
921}
922
Devang Patelbeab41b2009-10-07 22:04:08 +0000923/// processLexicalBlock
924void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000925 DIScope Context = LB.getContext();
926 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000927 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6618a242011-10-11 22:59:11 +0000928 else if (Context.isLexicalBlockFile()) {
929 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
930 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
931 }
Devang Patelbeab41b2009-10-07 22:04:08 +0000932 else
Devang Patel2db49d72010-05-07 18:11:54 +0000933 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000934}
935
Devang Patel98c65172009-07-30 18:25:15 +0000936/// processSubprogram - Process DISubprogram.
937void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000938 if (!addSubprogram(SP))
939 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000940 if (SP.getVersion() <= LLVMDebugVersion10)
941 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000942 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000943}
944
Devang Patelb4d31302009-07-31 18:18:52 +0000945/// processDeclare - Process DbgDeclareInst.
Eric Christopherc4639d62012-11-19 22:42:15 +0000946void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +0000947 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
948 if (!N) return;
949
950 DIDescriptor DV(N);
951 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +0000952 return;
953
Devang Patel2db49d72010-05-07 18:11:54 +0000954 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +0000955 return;
Devang Patelfa515ec2011-09-06 17:40:08 +0000956 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
957 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3c91b052010-03-08 20:52:55 +0000958 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +0000959}
960
Devang Patel72bcdb62009-08-10 22:09:58 +0000961/// addType - Add type into Tys.
962bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +0000963 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +0000964 return false;
965
Devang Patel2db49d72010-05-07 18:11:54 +0000966 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +0000967 return false;
968
Devang Patel2db49d72010-05-07 18:11:54 +0000969 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +0000970 return true;
971}
972
Devang Pateld2f79a12009-07-28 19:55:13 +0000973/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000974bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +0000975 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +0000976 return false;
977
Devang Patel2db49d72010-05-07 18:11:54 +0000978 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +0000979 return false;
980
Devang Patel2db49d72010-05-07 18:11:54 +0000981 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +0000982 return true;
983}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000984
Devang Pateld2f79a12009-07-28 19:55:13 +0000985/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000986bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +0000987 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +0000988 return false;
989
Devang Patel2db49d72010-05-07 18:11:54 +0000990 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +0000991 return false;
992
Devang Patel2db49d72010-05-07 18:11:54 +0000993 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +0000994 return true;
995}
996
997// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000998bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +0000999 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001000 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001001
Devang Patel2db49d72010-05-07 18:11:54 +00001002 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001003 return false;
1004
Devang Patel2db49d72010-05-07 18:11:54 +00001005 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001006 return true;
1007}
1008
Bill Wendlinge8778242012-06-26 23:22:18 +00001009//===----------------------------------------------------------------------===//
1010// DIDescriptor: dump routines for all descriptors.
1011//===----------------------------------------------------------------------===//
1012
1013/// dump - Print descriptor to dbgs() with a newline.
1014void DIDescriptor::dump() const {
1015 print(dbgs()); dbgs() << '\n';
1016}
1017
1018/// print - Print descriptor.
1019void DIDescriptor::print(raw_ostream &OS) const {
1020 if (!DbgNode) return;
1021
Bill Wendling97a75922012-06-28 02:17:58 +00001022 if (const char *Tag = dwarf::TagString(getTag()))
1023 OS << "[ " << Tag << " ]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001024
1025 if (this->isSubrange()) {
1026 DISubrange(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001027 } else if (this->isCompileUnit()) {
1028 DICompileUnit(DbgNode).printInternal(OS);
1029 } else if (this->isFile()) {
1030 DIFile(DbgNode).printInternal(OS);
1031 } else if (this->isEnumerator()) {
1032 DIEnumerator(DbgNode).printInternal(OS);
1033 } else if (this->isBasicType()) {
1034 DIType(DbgNode).printInternal(OS);
1035 } else if (this->isDerivedType()) {
1036 DIDerivedType(DbgNode).printInternal(OS);
1037 } else if (this->isCompositeType()) {
1038 DICompositeType(DbgNode).printInternal(OS);
1039 } else if (this->isSubprogram()) {
1040 DISubprogram(DbgNode).printInternal(OS);
1041 } else if (this->isGlobalVariable()) {
1042 DIGlobalVariable(DbgNode).printInternal(OS);
1043 } else if (this->isVariable()) {
1044 DIVariable(DbgNode).printInternal(OS);
Bill Wendling2da1a162012-07-06 19:12:31 +00001045 } else if (this->isObjCProperty()) {
1046 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling625252f2012-07-06 23:06:16 +00001047 } else if (this->isScope()) {
1048 DIScope(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001049 }
1050}
1051
1052void DISubrange::printInternal(raw_ostream &OS) const {
Bill Wendling9493dae2012-12-04 21:34:03 +00001053 int64_t Count = getCount();
1054 if (Count != -1)
1055 OS << " [" << getLo() << ", " << Count - 1 << ']';
1056 else
Bill Wendling46defa52012-12-05 21:43:30 +00001057 OS << " [unbounded]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001058}
1059
1060void DIScope::printInternal(raw_ostream &OS) const {
1061 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1062}
1063
1064void DICompileUnit::printInternal(raw_ostream &OS) const {
1065 DIScope::printInternal(OS);
1066 if (unsigned Lang = getLanguage())
1067 OS << " [" << dwarf::LanguageString(Lang) << ']';
1068}
1069
1070void DIEnumerator::printInternal(raw_ostream &OS) const {
1071 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1072}
1073
1074void DIType::printInternal(raw_ostream &OS) const {
1075 if (!DbgNode) return;
1076
1077 StringRef Res = getName();
1078 if (!Res.empty())
1079 OS << " [" << Res << "]";
1080
1081 // TODO: Print context?
1082
1083 OS << " [line " << getLineNumber()
1084 << ", size " << getSizeInBits()
1085 << ", align " << getAlignInBits()
1086 << ", offset " << getOffsetInBits();
1087 if (isBasicType())
Eric Christopher5f214ae2012-11-20 00:15:36 +00001088 if (const char *Enc =
Bill Wendlingfc1c70a2012-06-28 02:12:20 +00001089 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1090 OS << ", enc " << Enc;
Bill Wendlinge8778242012-06-26 23:22:18 +00001091 OS << "]";
1092
1093 if (isPrivate())
1094 OS << " [private]";
1095 else if (isProtected())
1096 OS << " [protected]";
1097
David Blaikie0fecdfb2013-01-08 00:31:02 +00001098 if (isArtificial())
1099 OS << " [artificial]";
1100
Bill Wendlinge8778242012-06-26 23:22:18 +00001101 if (isForwardDecl())
1102 OS << " [fwd]";
1103}
1104
1105void DIDerivedType::printInternal(raw_ostream &OS) const {
1106 DIType::printInternal(OS);
1107 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1108}
1109
1110void DICompositeType::printInternal(raw_ostream &OS) const {
1111 DIType::printInternal(OS);
1112 DIArray A = getTypeArray();
1113 OS << " [" << A.getNumElements() << " elements]";
1114}
1115
1116void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendlinge8778242012-06-26 23:22:18 +00001117 // TODO : Print context
Bill Wendlinge8778242012-06-26 23:22:18 +00001118 OS << " [line " << getLineNumber() << ']';
1119
1120 if (isLocalToUnit())
1121 OS << " [local]";
1122
1123 if (isDefinition())
1124 OS << " [def]";
1125
1126 if (getScopeLineNumber() != getLineNumber())
1127 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendlingee17cee2012-07-06 23:43:12 +00001128
David Blaikiee302b6c2013-01-05 21:39:33 +00001129 if (isPrivate())
1130 OS << " [private]";
1131 else if (isProtected())
1132 OS << " [protected]";
1133
Bill Wendlingee17cee2012-07-06 23:43:12 +00001134 StringRef Res = getName();
1135 if (!Res.empty())
1136 OS << " [" << Res << ']';
Bill Wendlinge8778242012-06-26 23:22:18 +00001137}
1138
1139void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1140 StringRef Res = getName();
1141 if (!Res.empty())
1142 OS << " [" << Res << ']';
1143
1144 OS << " [line " << getLineNumber() << ']';
1145
1146 // TODO : Print context
1147
1148 if (isLocalToUnit())
1149 OS << " [local]";
1150
1151 if (isDefinition())
1152 OS << " [def]";
1153}
1154
1155void DIVariable::printInternal(raw_ostream &OS) const {
1156 StringRef Res = getName();
1157 if (!Res.empty())
1158 OS << " [" << Res << ']';
1159
1160 OS << " [line " << getLineNumber() << ']';
1161}
1162
Bill Wendling2da1a162012-07-06 19:12:31 +00001163void DIObjCProperty::printInternal(raw_ostream &OS) const {
1164 StringRef Name = getObjCPropertyName();
1165 if (!Name.empty())
1166 OS << " [" << Name << ']';
1167
1168 OS << " [line " << getLineNumber()
1169 << ", properties " << getUnsignedField(6) << ']';
1170}
1171
Bill Wendlinge8778242012-06-26 23:22:18 +00001172static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1173 const LLVMContext &Ctx) {
1174 if (!DL.isUnknown()) { // Print source line info.
1175 DIScope Scope(DL.getScope(Ctx));
1176 // Omit the directory, because it's likely to be long and uninteresting.
1177 if (Scope.Verify())
1178 CommentOS << Scope.getFilename();
1179 else
1180 CommentOS << "<unknown>";
1181 CommentOS << ':' << DL.getLine();
1182 if (DL.getCol() != 0)
1183 CommentOS << ':' << DL.getCol();
1184 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1185 if (!InlinedAtDL.isUnknown()) {
1186 CommentOS << " @[ ";
1187 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1188 CommentOS << " ]";
1189 }
1190 }
1191}
1192
1193void DIVariable::printExtendedName(raw_ostream &OS) const {
1194 const LLVMContext &Ctx = DbgNode->getContext();
1195 StringRef Res = getName();
1196 if (!Res.empty())
1197 OS << Res << "," << getLineNumber();
1198 if (MDNode *InlinedAt = getInlinedAt()) {
1199 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1200 if (!InlinedAtDL.isUnknown()) {
1201 OS << " @[";
1202 printDebugLoc(InlinedAtDL, OS, Ctx);
1203 OS << "]";
1204 }
1205 }
1206}