blob: c8f8f7d67b845678eac7969d423acde39d1e96ef [file] [log] [blame]
Chris Lattner6d389332008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
Bill Wendlinge38859d2012-06-28 00:05:13 +000015#include "llvm/DebugInfo.h"
Chris Lattner6d389332008-11-10 02:56:27 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin89261392008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattner6d389332008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patel80ae3492009-08-28 23:24:31 +000023#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman2637cc12010-07-21 23:38:33 +000024#include "llvm/ADT/SmallString.h"
Dan Gohman16a5d982010-08-20 22:02:26 +000025#include "llvm/ADT/STLExtras.h"
David Greene83d4781452009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis97324ce2009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.h"
Chris Lattner91922252009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner6d389332008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis97324ce2009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattner6d389332008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Devang Patel33a2cdf2010-08-02 22:51:46 +000036DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37}
38
39DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40}
41
Eric Christopher6647b832011-10-11 22:59:11 +000042DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43}
44
Devang Patel33a2cdf2010-08-02 22:51:46 +000045DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
46}
47
48DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
49}
50
51DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52}
53
Jim Grosbach6cd0deb2010-07-21 21:36:25 +000054StringRef
Devang Patelb2969422009-09-29 18:40:58 +000055DIDescriptor::getStringField(unsigned Elt) const {
Devang Patel80ae3492009-08-28 23:24:31 +000056 if (DbgNode == 0)
Devang Patel2d9caf92009-11-25 17:36:49 +000057 return StringRef();
Chris Lattner6d389332008-11-10 02:56:27 +000058
Chris Lattner9b493022009-12-31 01:22:29 +000059 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel2d9caf92009-11-25 17:36:49 +000061 return MDS->getString();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000062
Devang Patel2d9caf92009-11-25 17:36:49 +000063 return StringRef();
Chris Lattner6d389332008-11-10 02:56:27 +000064}
65
66uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000067 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000068 return 0;
Bill Wendling6ddc11a2009-05-14 18:26:15 +000069
Chris Lattner9b493022009-12-31 01:22:29 +000070 if (Elt < DbgNode->getNumOperands())
Eric Christophera4a0cf82012-03-16 00:21:54 +000071 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patel80ae3492009-08-28 23:24:31 +000072 return CI->getZExtValue();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000073
Chris Lattner6d389332008-11-10 02:56:27 +000074 return 0;
75}
76
Chris Lattner6d389332008-11-10 02:56:27 +000077DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000078 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000079 return DIDescriptor();
Bill Wendling6ddc11a2009-05-14 18:26:15 +000080
Chris Lattner743bdca2010-03-31 05:53:47 +000081 if (Elt < DbgNode->getNumOperands())
Jim Grosbach6cd0deb2010-07-21 21:36:25 +000082 return
83 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patel80ae3492009-08-28 23:24:31 +000084 return DIDescriptor();
Chris Lattner6d389332008-11-10 02:56:27 +000085}
86
87GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +000088 if (DbgNode == 0)
Chris Lattner6d389332008-11-10 02:56:27 +000089 return 0;
Bill Wendling6ddc11a2009-05-14 18:26:15 +000090
Chris Lattner9b493022009-12-31 01:22:29 +000091 if (Elt < DbgNode->getNumOperands())
92 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patel80ae3492009-08-28 23:24:31 +000093 return 0;
Chris Lattner6d389332008-11-10 02:56:27 +000094}
95
Devang Patelc7cf14f2010-08-09 21:39:24 +000096Constant *DIDescriptor::getConstantField(unsigned Elt) const {
97 if (DbgNode == 0)
98 return 0;
99
100 if (Elt < DbgNode->getNumOperands())
101 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
102 return 0;
103}
104
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000105Function *DIDescriptor::getFunctionField(unsigned Elt) const {
106 if (DbgNode == 0)
107 return 0;
108
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
111 return 0;
112}
113
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000114unsigned DIVariable::getNumAddrElements() const {
Bill Wendling16d944c2012-07-06 17:46:28 +0000115 if (getVersion() <= LLVMDebugVersion8)
Devang Patelbea08d12010-09-29 23:07:21 +0000116 return DbgNode->getNumOperands()-6;
Bill Wendling16d944c2012-07-06 17:46:28 +0000117 if (getVersion() == LLVMDebugVersion9)
Devang Patelcfa82a32011-07-19 19:41:54 +0000118 return DbgNode->getNumOperands()-7;
119 return DbgNode->getNumOperands()-8;
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000120}
121
Devang Patel8fb9fd62011-07-20 22:18:50 +0000122/// getInlinedAt - If this variable is inlined then return inline location.
Devang Patel3d6e3892011-08-09 01:03:14 +0000123MDNode *DIVariable::getInlinedAt() const {
Bill Wendling16d944c2012-07-06 17:46:28 +0000124 if (getVersion() <= LLVMDebugVersion9)
Devang Patel8fb9fd62011-07-20 22:18:50 +0000125 return NULL;
126 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
127}
Chris Lattner6a0ca6a2009-12-31 03:02:08 +0000128
Chris Lattner6d389332008-11-10 02:56:27 +0000129//===----------------------------------------------------------------------===//
Devang Patel9fda4bd2009-08-31 18:49:10 +0000130// Predicates
Chris Lattner6d389332008-11-10 02:56:27 +0000131//===----------------------------------------------------------------------===//
132
Devang Patel9fda4bd2009-08-31 18:49:10 +0000133/// isBasicType - Return true if the specified tag is legal for
134/// DIBasicType.
135bool DIDescriptor::isBasicType() const {
Devang Patel04d6d472011-09-14 23:13:28 +0000136 if (!DbgNode) return false;
137 switch (getTag()) {
138 case dwarf::DW_TAG_base_type:
139 case dwarf::DW_TAG_unspecified_type:
140 return true;
141 default:
142 return false;
143 }
Torok Edwin040688f2008-12-13 08:25:29 +0000144}
Chris Lattner6d389332008-11-10 02:56:27 +0000145
Devang Patel9fda4bd2009-08-31 18:49:10 +0000146/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
147bool DIDescriptor::isDerivedType() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000148 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000149 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000150 case dwarf::DW_TAG_typedef:
151 case dwarf::DW_TAG_pointer_type:
152 case dwarf::DW_TAG_reference_type:
Eric Christopherb5cf66c2012-05-19 01:36:37 +0000153 case dwarf::DW_TAG_rvalue_reference_type:
Chris Lattner6d389332008-11-10 02:56:27 +0000154 case dwarf::DW_TAG_const_type:
155 case dwarf::DW_TAG_volatile_type:
156 case dwarf::DW_TAG_restrict_type:
157 case dwarf::DW_TAG_member:
158 case dwarf::DW_TAG_inheritance:
Devang Pateldd719f72010-08-23 23:16:25 +0000159 case dwarf::DW_TAG_friend:
Chris Lattner6d389332008-11-10 02:56:27 +0000160 return true;
161 default:
Devang Patel80ae3492009-08-28 23:24:31 +0000162 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000163 return isCompositeType();
Chris Lattner6d389332008-11-10 02:56:27 +0000164 }
165}
166
Chris Lattner6d389332008-11-10 02:56:27 +0000167/// isCompositeType - Return true if the specified tag is legal for
168/// DICompositeType.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000169bool DIDescriptor::isCompositeType() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000170 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000171 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000172 case dwarf::DW_TAG_array_type:
173 case dwarf::DW_TAG_structure_type:
174 case dwarf::DW_TAG_union_type:
175 case dwarf::DW_TAG_enumeration_type:
176 case dwarf::DW_TAG_vector_type:
177 case dwarf::DW_TAG_subroutine_type:
Devang Patel68b08812009-03-25 03:52:06 +0000178 case dwarf::DW_TAG_class_type:
Chris Lattner6d389332008-11-10 02:56:27 +0000179 return true;
180 default:
181 return false;
182 }
183}
184
Chris Lattner6d389332008-11-10 02:56:27 +0000185/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel9fda4bd2009-08-31 18:49:10 +0000186bool DIDescriptor::isVariable() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000187 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000188 switch (getTag()) {
Chris Lattner6d389332008-11-10 02:56:27 +0000189 case dwarf::DW_TAG_auto_variable:
190 case dwarf::DW_TAG_arg_variable:
191 case dwarf::DW_TAG_return_variable:
192 return true;
193 default:
194 return false;
195 }
196}
197
Devang Patela49be762009-09-30 22:34:41 +0000198/// isType - Return true if the specified tag is legal for DIType.
199bool DIDescriptor::isType() const {
200 return isBasicType() || isCompositeType() || isDerivedType();
201}
202
Devang Patel9fda4bd2009-08-31 18:49:10 +0000203/// isSubprogram - Return true if the specified tag is legal for
204/// DISubprogram.
205bool DIDescriptor::isSubprogram() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000206 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel9fda4bd2009-08-31 18:49:10 +0000207}
208
209/// isGlobalVariable - Return true if the specified tag is legal for
210/// DIGlobalVariable.
211bool DIDescriptor::isGlobalVariable() const {
Devang Patelb2197462010-08-10 07:11:13 +0000212 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
213 getTag() == dwarf::DW_TAG_constant);
Devang Patel9fda4bd2009-08-31 18:49:10 +0000214}
215
Devang Patela49be762009-09-30 22:34:41 +0000216/// isGlobal - Return true if the specified tag is legal for DIGlobal.
217bool DIDescriptor::isGlobal() const {
218 return isGlobalVariable();
219}
220
Devang Pateldf0dd7d2011-02-03 00:13:47 +0000221/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Patel9a33ec22010-10-06 20:50:40 +0000222/// DW_TAG_unspecified_parameters.
223bool DIDescriptor::isUnspecifiedParameter() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
225}
226
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000227/// isScope - Return true if the specified tag is one of the scope
Devang Pateld9b61152009-08-31 20:44:45 +0000228/// related tag.
229bool DIDescriptor::isScope() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000230 if (!DbgNode) return false;
Chris Lattner047fd2f2009-12-29 09:22:47 +0000231 switch (getTag()) {
232 case dwarf::DW_TAG_compile_unit:
233 case dwarf::DW_TAG_lexical_block:
234 case dwarf::DW_TAG_subprogram:
235 case dwarf::DW_TAG_namespace:
236 return true;
237 default:
238 break;
Devang Pateld9b61152009-08-31 20:44:45 +0000239 }
240 return false;
241}
Devang Patel9fda4bd2009-08-31 18:49:10 +0000242
Devang Patel3a9e65e2011-02-02 21:38:25 +0000243/// isTemplateTypeParameter - Return true if the specified tag is
244/// DW_TAG_template_type_parameter.
245bool DIDescriptor::isTemplateTypeParameter() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
247}
248
Devang Patelbe933b42011-02-02 22:35:53 +0000249/// isTemplateValueParameter - Return true if the specified tag is
250/// DW_TAG_template_value_parameter.
251bool DIDescriptor::isTemplateValueParameter() const {
252 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
253}
254
Devang Patele4cfcc32009-08-31 21:34:44 +0000255/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
256bool DIDescriptor::isCompileUnit() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000257 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patele4cfcc32009-08-31 21:34:44 +0000258}
259
Devang Patel2e520f62010-03-08 22:27:22 +0000260/// isFile - Return true if the specified tag is DW_TAG_file_type.
261bool DIDescriptor::isFile() const {
262 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
263}
264
Devang Patel1f4690c2009-12-15 19:16:48 +0000265/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
266bool DIDescriptor::isNameSpace() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000267 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel1f4690c2009-12-15 19:16:48 +0000268}
269
Eric Christopher6647b832011-10-11 22:59:11 +0000270/// isLexicalBlockFile - Return true if the specified descriptor is a
271/// lexical block with an extra file.
272bool DIDescriptor::isLexicalBlockFile() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
274 (DbgNode->getNumOperands() == 3);
275}
276
Devang Patel869529c2009-08-31 22:00:15 +0000277/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
278bool DIDescriptor::isLexicalBlock() const {
Eric Christopher6647b832011-10-11 22:59:11 +0000279 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
280 (DbgNode->getNumOperands() > 3);
Devang Patel869529c2009-08-31 22:00:15 +0000281}
282
Devang Patela49be762009-09-30 22:34:41 +0000283/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
284bool DIDescriptor::isSubrange() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000285 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patela49be762009-09-30 22:34:41 +0000286}
287
288/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
289bool DIDescriptor::isEnumerator() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000290 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patela49be762009-09-30 22:34:41 +0000291}
292
Devang Patelcc481592012-02-04 00:59:25 +0000293/// isObjCProperty - Return true if the specified tag is DW_TAG
294bool DIDescriptor::isObjCProperty() const {
Eric Christopherc13fd6d2012-03-29 21:35:05 +0000295 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
Devang Patelcc481592012-02-04 00:59:25 +0000296}
Devang Patel9fda4bd2009-08-31 18:49:10 +0000297//===----------------------------------------------------------------------===//
298// Simple Descriptor Constructors and other Methods
299//===----------------------------------------------------------------------===//
300
Devang Patel32cc43c2010-05-07 20:54:48 +0000301DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel9fda4bd2009-08-31 18:49:10 +0000302 if (!N) return;
303 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
304 DbgNode = 0;
305 }
306}
307
Devang Patel758e7d72009-01-05 18:33:01 +0000308unsigned DIArray::getNumElements() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000309 if (!DbgNode)
310 return 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000311 return DbgNode->getNumOperands();
Devang Patel758e7d72009-01-05 18:33:01 +0000312}
Chris Lattner6d389332008-11-10 02:56:27 +0000313
Devang Patelf03c9be2009-07-22 18:23:44 +0000314/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman093cb792010-07-21 18:54:18 +0000315/// this descriptor.
Dan Gohman16a5d982010-08-20 22:02:26 +0000316void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000317 if (!DbgNode)
Devang Patelf03c9be2009-07-22 18:23:44 +0000318 return;
319
Daniel Dunbar03564832009-09-22 02:03:18 +0000320 // Since we use a TrackingVH for the node, its easy for clients to manufacture
321 // legitimate situations where they want to replaceAllUsesWith() on something
322 // which, due to uniquing, has merged with the source. We shield clients from
323 // this detail by allowing a value to be replaced with replaceAllUsesWith()
324 // itself.
Devang Patel4423abd2010-05-07 18:19:32 +0000325 if (DbgNode != D) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000326 MDNode *Node = const_cast<MDNode*>(DbgNode);
327 const MDNode *DN = D;
328 const Value *V = cast_or_null<Value>(DN);
329 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman16a5d982010-08-20 22:02:26 +0000330 MDNode::deleteTemporary(Node);
Daniel Dunbar03564832009-09-22 02:03:18 +0000331 }
Devang Patelf03c9be2009-07-22 18:23:44 +0000332}
333
Devang Patel81c3c872010-12-08 20:18:20 +0000334/// replaceAllUsesWith - Replace all uses of debug info referenced by
335/// this descriptor.
336void DIType::replaceAllUsesWith(MDNode *D) {
337 if (!DbgNode)
338 return;
339
340 // Since we use a TrackingVH for the node, its easy for clients to manufacture
341 // legitimate situations where they want to replaceAllUsesWith() on something
342 // which, due to uniquing, has merged with the source. We shield clients from
343 // this detail by allowing a value to be replaced with replaceAllUsesWith()
344 // itself.
345 if (DbgNode != D) {
346 MDNode *Node = const_cast<MDNode*>(DbgNode);
347 const MDNode *DN = D;
348 const Value *V = cast_or_null<Value>(DN);
349 Node->replaceAllUsesWith(const_cast<Value*>(V));
350 MDNode::deleteTemporary(Node);
351 }
352}
353
Devang Pateldfd6ec32011-08-15 17:57:41 +0000354/// isUnsignedDIType - Return true if type encoding is unsigned.
355bool DIType::isUnsignedDIType() {
356 DIDerivedType DTy(DbgNode);
357 if (DTy.Verify())
358 return DTy.getTypeDerivedFrom().isUnsignedDIType();
359
360 DIBasicType BTy(DbgNode);
361 if (BTy.Verify()) {
362 unsigned Encoding = BTy.getEncoding();
363 if (Encoding == dwarf::DW_ATE_unsigned ||
364 Encoding == dwarf::DW_ATE_unsigned_char)
365 return true;
366 }
367 return false;
368}
369
Devang Patel44afc822009-01-19 23:21:49 +0000370/// Verify - Verify that a compile unit is well formed.
371bool DICompileUnit::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000372 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000373 return false;
Devang Patel2d9caf92009-11-25 17:36:49 +0000374 StringRef N = getFilename();
375 if (N.empty())
Bill Wendlingfa54bc22009-03-13 04:39:26 +0000376 return false;
Devang Patel44afc822009-01-19 23:21:49 +0000377 // It is possible that directory and produce string is empty.
Bill Wendlingfa54bc22009-03-13 04:39:26 +0000378 return true;
Devang Patel44afc822009-01-19 23:21:49 +0000379}
380
Eric Christopher70e1bd82012-03-29 08:42:56 +0000381/// Verify - Verify that an ObjC property is well formed.
382bool DIObjCProperty::Verify() const {
383 if (!DbgNode)
384 return false;
385 unsigned Tag = getTag();
Eric Christopherc13fd6d2012-03-29 21:35:05 +0000386 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
Eric Christopher70e1bd82012-03-29 08:42:56 +0000387 DIType Ty = getType();
388 if (!Ty.Verify()) return false;
389
390 // Don't worry about the rest of the strings for now.
391 return true;
392}
393
Devang Patel44afc822009-01-19 23:21:49 +0000394/// Verify - Verify that a type descriptor is well formed.
395bool DIType::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000396 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000397 return false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000398 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000399 return false;
Devang Patel415c5512010-11-02 20:41:13 +0000400 unsigned Tag = getTag();
401 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
402 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
Eric Christopherb5cf66c2012-05-19 01:36:37 +0000403 Tag != dwarf::DW_TAG_reference_type &&
404 Tag != dwarf::DW_TAG_rvalue_reference_type &&
405 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
406 Tag != dwarf::DW_TAG_array_type &&
407 Tag != dwarf::DW_TAG_enumeration_type &&
408 Tag != dwarf::DW_TAG_subroutine_type &&
409 getFilename().empty())
Devang Patel44afc822009-01-19 23:21:49 +0000410 return false;
411 return true;
412}
413
Devang Patela8652672010-08-23 18:25:56 +0000414/// Verify - Verify that a basic type descriptor is well formed.
415bool DIBasicType::Verify() const {
416 return isBasicType();
417}
418
419/// Verify - Verify that a derived type descriptor is well formed.
420bool DIDerivedType::Verify() const {
421 return isDerivedType();
422}
423
Devang Patel44afc822009-01-19 23:21:49 +0000424/// Verify - Verify that a composite type descriptor is well formed.
425bool DICompositeType::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000426 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000427 return false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000428 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000429 return false;
430
Devang Patel44afc822009-01-19 23:21:49 +0000431 return true;
432}
433
434/// Verify - Verify that a subprogram descriptor is well formed.
435bool DISubprogram::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000436 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000437 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000438
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000439 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000440 return false;
441
442 DICompositeType Ty = getType();
Devang Patel3b548aa2010-03-08 20:52:55 +0000443 if (!Ty.Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000444 return false;
445 return true;
446}
447
448/// Verify - Verify that a global variable descriptor is well formed.
449bool DIGlobalVariable::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000450 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000451 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000452
Devang Patel2d9caf92009-11-25 17:36:49 +0000453 if (getDisplayName().empty())
Devang Patelcc11371b2009-11-06 17:58:12 +0000454 return false;
455
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000456 if (getContext() && !getContext().Verify())
Devang Patel44afc822009-01-19 23:21:49 +0000457 return false;
458
459 DIType Ty = getType();
460 if (!Ty.Verify())
461 return false;
462
Devang Patelc7cf14f2010-08-09 21:39:24 +0000463 if (!getGlobal() && !getConstant())
Devang Patel44afc822009-01-19 23:21:49 +0000464 return false;
465
466 return true;
467}
468
469/// Verify - Verify that a variable descriptor is well formed.
470bool DIVariable::Verify() const {
Devang Patel3b548aa2010-03-08 20:52:55 +0000471 if (!DbgNode)
Devang Patel44afc822009-01-19 23:21:49 +0000472 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000473
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000474 if (getContext() && !getContext().Verify())
Devang Patel2c4d69d2010-05-07 21:42:24 +0000475 return false;
476
Devang Patel44afc822009-01-19 23:21:49 +0000477 DIType Ty = getType();
478 if (!Ty.Verify())
479 return false;
480
Devang Patel44afc822009-01-19 23:21:49 +0000481 return true;
482}
483
Devang Patel3b548aa2010-03-08 20:52:55 +0000484/// Verify - Verify that a location descriptor is well formed.
485bool DILocation::Verify() const {
486 if (!DbgNode)
487 return false;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000488
Devang Patel3b548aa2010-03-08 20:52:55 +0000489 return DbgNode->getNumOperands() == 4;
490}
491
Devang Patel54c59312010-05-07 23:04:32 +0000492/// Verify - Verify that a namespace descriptor is well formed.
493bool DINameSpace::Verify() const {
494 if (!DbgNode)
495 return false;
496 if (getName().empty())
497 return false;
Devang Patel54c59312010-05-07 23:04:32 +0000498 return true;
499}
500
Devang Patel528987a2009-02-17 21:23:59 +0000501/// getOriginalTypeSize - If this type is derived from a base type then
502/// return base type size.
503uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patelf05d5722009-11-04 23:48:00 +0000504 unsigned Tag = getTag();
Eric Christopher27886c62011-12-16 23:42:45 +0000505
Bill Wendling3b70d782012-06-26 23:22:18 +0000506 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
507 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
508 Tag != dwarf::DW_TAG_restrict_type)
509 return getSizeInBits();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000510
Bill Wendling3b70d782012-06-26 23:22:18 +0000511 DIType BaseType = getTypeDerivedFrom();
512
513 // If this type is not derived from any type then take conservative approach.
514 if (!BaseType.isValid())
515 return getSizeInBits();
516
517 // If this is a derived type, go ahead and get the base type, unless it's a
518 // reference then it's just the size of the field. Pointer types have no need
519 // of this since they're a different type of qualification on the type.
520 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
521 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
522 return getSizeInBits();
523
524 if (BaseType.isDerivedType())
525 return DIDerivedType(BaseType).getOriginalTypeSize();
526
527 return BaseType.getSizeInBits();
Devang Patel528987a2009-02-17 21:23:59 +0000528}
Devang Patel44afc822009-01-19 23:21:49 +0000529
Devang Patel44882172012-02-06 17:49:43 +0000530/// getObjCProperty - Return property node, if this ivar is associated with one.
531MDNode *DIDerivedType::getObjCProperty() const {
532 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
533 return NULL;
534 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
535}
536
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000537/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel03955532010-04-29 20:40:36 +0000538/// information for an inlined function arguments.
539bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
540 assert(CurFn && "Invalid function");
541 if (!getContext().isSubprogram())
542 return false;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000543 // This variable is not inlined function argument if its scope
Devang Patel03955532010-04-29 20:40:36 +0000544 // does not describe current function.
Bill Wendling3b70d782012-06-26 23:22:18 +0000545 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel03955532010-04-29 20:40:36 +0000546}
547
Devang Patel1be3b532009-04-15 00:06:07 +0000548/// describes - Return true if this subprogram provides debugging
549/// information for the function F.
550bool DISubprogram::describes(const Function *F) {
Chris Lattner047fd2f2009-12-29 09:22:47 +0000551 assert(F && "Invalid function");
Devang Pateld119da52010-06-16 06:42:02 +0000552 if (F == getFunction())
553 return true;
Devang Patel2d9caf92009-11-25 17:36:49 +0000554 StringRef Name = getLinkageName();
555 if (Name.empty())
Devang Patelb2969422009-09-29 18:40:58 +0000556 Name = getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000557 if (F->getName() == Name)
Devang Patel1be3b532009-04-15 00:06:07 +0000558 return true;
559 return false;
560}
561
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000562unsigned DISubprogram::isOptimized() const {
Devang Patelb4e3b902010-04-30 19:38:23 +0000563 assert (DbgNode && "Invalid subprogram descriptor!");
564 if (DbgNode->getNumOperands() == 16)
565 return getUnsignedField(15);
566 return 0;
567}
568
Devang Patel59e27c52011-08-19 23:28:12 +0000569MDNode *DISubprogram::getVariablesNodes() const {
570 if (!DbgNode || DbgNode->getNumOperands() <= 19)
571 return NULL;
572 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
573 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
574 return NULL;
575}
576
577DIArray DISubprogram::getVariables() const {
578 if (!DbgNode || DbgNode->getNumOperands() <= 19)
579 return DIArray();
580 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
581 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
582 return DIArray(A);
583 return DIArray();
584}
585
Devang Patel2d9caf92009-11-25 17:36:49 +0000586StringRef DIScope::getFilename() const {
Devang Patel8119fe872010-03-08 22:02:50 +0000587 if (!DbgNode)
588 return StringRef();
Eric Christopher6647b832011-10-11 22:59:11 +0000589 if (isLexicalBlockFile())
590 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000591 if (isLexicalBlock())
Devang Patela49be762009-09-30 22:34:41 +0000592 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000593 if (isSubprogram())
Devang Patela49be762009-09-30 22:34:41 +0000594 return DISubprogram(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000595 if (isCompileUnit())
Devang Patela49be762009-09-30 22:34:41 +0000596 return DICompileUnit(DbgNode).getFilename();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000597 if (isNameSpace())
Devang Patel1f4690c2009-12-15 19:16:48 +0000598 return DINameSpace(DbgNode).getFilename();
Devang Patel8119fe872010-03-08 22:02:50 +0000599 if (isType())
600 return DIType(DbgNode).getFilename();
Devang Patel2e520f62010-03-08 22:27:22 +0000601 if (isFile())
602 return DIFile(DbgNode).getFilename();
Craig Toppera2886c22012-02-07 05:05:23 +0000603 llvm_unreachable("Invalid DIScope!");
Devang Patela49be762009-09-30 22:34:41 +0000604}
605
Devang Patel2d9caf92009-11-25 17:36:49 +0000606StringRef DIScope::getDirectory() const {
Devang Patel8119fe872010-03-08 22:02:50 +0000607 if (!DbgNode)
608 return StringRef();
Eric Christopher6647b832011-10-11 22:59:11 +0000609 if (isLexicalBlockFile())
610 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000611 if (isLexicalBlock())
Devang Patela49be762009-09-30 22:34:41 +0000612 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000613 if (isSubprogram())
Devang Patela49be762009-09-30 22:34:41 +0000614 return DISubprogram(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000615 if (isCompileUnit())
Devang Patela49be762009-09-30 22:34:41 +0000616 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner047fd2f2009-12-29 09:22:47 +0000617 if (isNameSpace())
Devang Patel1f4690c2009-12-15 19:16:48 +0000618 return DINameSpace(DbgNode).getDirectory();
Devang Patel8119fe872010-03-08 22:02:50 +0000619 if (isType())
620 return DIType(DbgNode).getDirectory();
Devang Patel2e520f62010-03-08 22:27:22 +0000621 if (isFile())
622 return DIFile(DbgNode).getDirectory();
Craig Toppera2886c22012-02-07 05:05:23 +0000623 llvm_unreachable("Invalid DIScope!");
Devang Patela49be762009-09-30 22:34:41 +0000624}
625
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000626DIArray DICompileUnit::getEnumTypes() const {
627 if (!DbgNode || DbgNode->getNumOperands() < 14)
628 return DIArray();
629
630 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
631 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
632 return DIArray(A);
633 return DIArray();
634}
635
636DIArray DICompileUnit::getRetainedTypes() const {
637 if (!DbgNode || DbgNode->getNumOperands() < 14)
638 return DIArray();
639
640 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
641 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
642 return DIArray(A);
643 return DIArray();
644}
645
646DIArray DICompileUnit::getSubprograms() const {
647 if (!DbgNode || DbgNode->getNumOperands() < 14)
648 return DIArray();
649
650 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
651 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
652 return DIArray(A);
653 return DIArray();
654}
655
656
657DIArray DICompileUnit::getGlobalVariables() const {
658 if (!DbgNode || DbgNode->getNumOperands() < 14)
659 return DIArray();
660
661 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
662 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
663 return DIArray(A);
664 return DIArray();
665}
666
Devang Patel364bf042010-11-10 22:19:21 +0000667/// fixupObjcLikeName - Replace contains special characters used
668/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000669static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
670 bool isObjCLike = false;
Devang Patel364bf042010-11-10 22:19:21 +0000671 for (size_t i = 0, e = Str.size(); i < e; ++i) {
672 char C = Str[i];
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000673 if (C == '[')
674 isObjCLike = true;
675
676 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
677 C == '+' || C == '(' || C == ')'))
678 Out.push_back('.');
679 else
680 Out.push_back(C);
Devang Patel364bf042010-11-10 22:19:21 +0000681 }
682}
683
Devang Patel364bf042010-11-10 22:19:21 +0000684/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
685/// suitable to hold function specific information.
Devang Patel59e27c52011-08-19 23:28:12 +0000686NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000687 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel59e27c52011-08-19 23:28:12 +0000688 StringRef FName = "fn";
689 if (Fn.getFunction())
690 FName = Fn.getFunction()->getName();
691 else
692 FName = Fn.getName();
693 char One = '\1';
694 if (FName.startswith(StringRef(&One, 1)))
695 FName = FName.substr(1);
696 fixupObjcLikeName(FName, Name);
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000697 return M.getNamedMetadata(Name.str());
Devang Patel364bf042010-11-10 22:19:21 +0000698}
699
Duncan Sandsbf577d62011-03-02 20:30:37 +0000700/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
701/// to hold function specific information.
Devang Patel59e27c52011-08-19 23:28:12 +0000702NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000703 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel59e27c52011-08-19 23:28:12 +0000704 StringRef FName = "fn";
705 if (Fn.getFunction())
706 FName = Fn.getFunction()->getName();
707 else
708 FName = Fn.getName();
709 char One = '\1';
710 if (FName.startswith(StringRef(&One, 1)))
711 FName = FName.substr(1);
712 fixupObjcLikeName(FName, Name);
713
Benjamin Kramer9319e9c2011-06-18 14:42:42 +0000714 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel7a554812010-09-28 18:08:20 +0000715}
716
Devang Patelcfa82a32011-07-19 19:41:54 +0000717/// createInlinedVariable - Create a new inlined variable based on current
718/// variable.
719/// @param DV Current Variable.
720/// @param InlinedScope Location at current variable is inlined.
721DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
722 LLVMContext &VMContext) {
723 SmallVector<Value *, 16> Elts;
724 // Insert inlined scope as 7th element.
725 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
726 i == 7 ? Elts.push_back(InlinedScope) :
727 Elts.push_back(DV->getOperand(i));
728 return DIVariable(MDNode::get(VMContext, Elts));
729}
Devang Patel7a554812010-09-28 18:08:20 +0000730
Devang Patelbb23a4a2011-08-10 21:50:54 +0000731/// cleanseInlinedVariable - Remove inlined scope from the variable.
732DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
733 SmallVector<Value *, 16> Elts;
734 // Insert inlined scope as 7th element.
735 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
736 i == 7 ?
Bill Wendling16d944c2012-07-06 17:46:28 +0000737 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelbb23a4a2011-08-10 21:50:54 +0000738 Elts.push_back(DV->getOperand(i));
739 return DIVariable(MDNode::get(VMContext, Elts));
740}
741
Bill Wendlinge38859d2012-06-28 00:05:13 +0000742/// getDISubprogram - Find subprogram that is enclosing this scope.
743DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
744 DIDescriptor D(Scope);
745 if (D.isSubprogram())
746 return DISubprogram(Scope);
747
748 if (D.isLexicalBlockFile())
749 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
750
751 if (D.isLexicalBlock())
752 return getDISubprogram(DILexicalBlock(Scope).getContext());
753
754 return DISubprogram();
755}
756
757/// getDICompositeType - Find underlying composite type.
758DICompositeType llvm::getDICompositeType(DIType T) {
759 if (T.isCompositeType())
760 return DICompositeType(T);
761
762 if (T.isDerivedType())
763 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
764
765 return DICompositeType();
766}
767
768/// isSubprogramContext - Return true if Context is either a subprogram
769/// or another context nested inside a subprogram.
770bool llvm::isSubprogramContext(const MDNode *Context) {
771 if (!Context)
772 return false;
773 DIDescriptor D(Context);
774 if (D.isSubprogram())
775 return true;
776 if (D.isType())
777 return isSubprogramContext(DIType(Context).getContext());
778 return false;
779}
780
Devang Patel1da75552009-07-28 19:55:13 +0000781//===----------------------------------------------------------------------===//
Devang Patel3b4e8272009-07-30 18:25:15 +0000782// DebugInfoFinder implementations.
Devang Patel1da75552009-07-28 19:55:13 +0000783//===----------------------------------------------------------------------===//
784
Devang Patel3b4e8272009-07-30 18:25:15 +0000785/// processModule - Process entire module and collect debug info.
786void DebugInfoFinder::processModule(Module &M) {
Devang Patel7973e782011-10-17 22:30:34 +0000787 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
788 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
789 DICompileUnit CU(CU_Nodes->getOperand(i));
790 addCompileUnit(CU);
791 if (CU.getVersion() > LLVMDebugVersion10) {
Devang Patela93cc252012-02-08 00:17:07 +0000792 DIArray GVs = CU.getGlobalVariables();
793 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
794 DIGlobalVariable DIG(GVs.getElement(i));
795 if (addGlobalVariable(DIG))
796 processType(DIG.getType());
797 }
798 DIArray SPs = CU.getSubprograms();
799 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
800 processSubprogram(DISubprogram(SPs.getElement(i)));
801 DIArray EnumTypes = CU.getEnumTypes();
802 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
803 processType(DIType(EnumTypes.getElement(i)));
804 DIArray RetainedTypes = CU.getRetainedTypes();
805 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
806 processType(DIType(RetainedTypes.getElement(i)));
807 return;
Devang Patel7973e782011-10-17 22:30:34 +0000808 }
809 }
810 }
Devang Patel5ea5d792011-09-06 17:40:08 +0000811
Devang Patel1da75552009-07-28 19:55:13 +0000812 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
813 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
814 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
815 ++BI) {
Devang Patel801b8ea2010-05-04 01:05:02 +0000816 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patel9d7de2a2009-07-31 18:18:52 +0000817 processDeclare(DDI);
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000818
Chris Lattner44714c92010-04-02 20:44:29 +0000819 DebugLoc Loc = BI->getDebugLoc();
820 if (Loc.isUnknown())
821 continue;
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000822
Chris Lattner44714c92010-04-02 20:44:29 +0000823 LLVMContext &Ctx = BI->getContext();
824 DIDescriptor Scope(Loc.getScope(Ctx));
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000825
Chris Lattner44714c92010-04-02 20:44:29 +0000826 if (Scope.isCompileUnit())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000827 addCompileUnit(DICompileUnit(Scope));
Chris Lattner44714c92010-04-02 20:44:29 +0000828 else if (Scope.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000829 processSubprogram(DISubprogram(Scope));
Eric Christopher6647b832011-10-11 22:59:11 +0000830 else if (Scope.isLexicalBlockFile()) {
831 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
832 processLexicalBlock(DILexicalBlock(DBF.getScope()));
833 }
Chris Lattner44714c92010-04-02 20:44:29 +0000834 else if (Scope.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000835 processLexicalBlock(DILexicalBlock(Scope));
Jim Grosbach6cd0deb2010-07-21 21:36:25 +0000836
Chris Lattner44714c92010-04-02 20:44:29 +0000837 if (MDNode *IA = Loc.getInlinedAt(Ctx))
838 processLocation(DILocation(IA));
Devang Patel1da75552009-07-28 19:55:13 +0000839 }
Devang Patel80ae3492009-08-28 23:24:31 +0000840
Devang Patelf7869a42010-06-28 05:53:08 +0000841 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
842 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
843 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
844 if (addGlobalVariable(DIG)) {
Devang Patel5ea5d792011-09-06 17:40:08 +0000845 if (DIG.getVersion() <= LLVMDebugVersion10)
846 addCompileUnit(DIG.getCompileUnit());
Devang Patelf7869a42010-06-28 05:53:08 +0000847 processType(DIG.getType());
848 }
Devang Patel1da75552009-07-28 19:55:13 +0000849 }
850 }
Devang Patelf7869a42010-06-28 05:53:08 +0000851
852 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
853 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
854 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Patel1da75552009-07-28 19:55:13 +0000855}
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000856
Devang Patel42c1d172009-11-10 22:05:35 +0000857/// processLocation - Process DILocation.
858void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000859 if (!Loc.Verify()) return;
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000860 DIDescriptor S(Loc.getScope());
Devang Patel42c1d172009-11-10 22:05:35 +0000861 if (S.isCompileUnit())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000862 addCompileUnit(DICompileUnit(S));
Devang Patel42c1d172009-11-10 22:05:35 +0000863 else if (S.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000864 processSubprogram(DISubprogram(S));
Devang Patel42c1d172009-11-10 22:05:35 +0000865 else if (S.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000866 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6647b832011-10-11 22:59:11 +0000867 else if (S.isLexicalBlockFile()) {
868 DILexicalBlockFile DBF = DILexicalBlockFile(S);
869 processLexicalBlock(DILexicalBlock(DBF.getScope()));
870 }
Devang Patel42c1d172009-11-10 22:05:35 +0000871 processLocation(Loc.getOrigLocation());
872}
873
Devang Patel3b4e8272009-07-30 18:25:15 +0000874/// processType - Process DIType.
875void DebugInfoFinder::processType(DIType DT) {
Devang Pateld0658ef2009-08-10 22:09:58 +0000876 if (!addType(DT))
Devang Patel1da75552009-07-28 19:55:13 +0000877 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000878 if (DT.getVersion() <= LLVMDebugVersion10)
879 addCompileUnit(DT.getCompileUnit());
Devang Patel9fda4bd2009-08-31 18:49:10 +0000880 if (DT.isCompositeType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000881 DICompositeType DCT(DT);
Devang Patel3b4e8272009-07-30 18:25:15 +0000882 processType(DCT.getTypeDerivedFrom());
Devang Patel1da75552009-07-28 19:55:13 +0000883 DIArray DA = DCT.getTypeArray();
Devang Patel3b548aa2010-03-08 20:52:55 +0000884 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
885 DIDescriptor D = DA.getElement(i);
886 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000887 processType(DIType(D));
Devang Patel3b548aa2010-03-08 20:52:55 +0000888 else if (D.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000889 processSubprogram(DISubprogram(D));
Devang Patel3b548aa2010-03-08 20:52:55 +0000890 }
Devang Patel9fda4bd2009-08-31 18:49:10 +0000891 } else if (DT.isDerivedType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000892 DIDerivedType DDT(DT);
Devang Patel3b548aa2010-03-08 20:52:55 +0000893 processType(DDT.getTypeDerivedFrom());
Devang Patel1da75552009-07-28 19:55:13 +0000894 }
895}
896
Devang Patel55571bd2009-10-07 22:04:08 +0000897/// processLexicalBlock
898void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patel55571bd2009-10-07 22:04:08 +0000899 DIScope Context = LB.getContext();
900 if (Context.isLexicalBlock())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000901 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6647b832011-10-11 22:59:11 +0000902 else if (Context.isLexicalBlockFile()) {
903 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
904 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
905 }
Devang Patel55571bd2009-10-07 22:04:08 +0000906 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000907 return processSubprogram(DISubprogram(Context));
Devang Patel55571bd2009-10-07 22:04:08 +0000908}
909
Devang Patel3b4e8272009-07-30 18:25:15 +0000910/// processSubprogram - Process DISubprogram.
911void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patel1da75552009-07-28 19:55:13 +0000912 if (!addSubprogram(SP))
913 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000914 if (SP.getVersion() <= LLVMDebugVersion10)
915 addCompileUnit(SP.getCompileUnit());
Devang Patel3b4e8272009-07-30 18:25:15 +0000916 processType(SP.getType());
Devang Patel1da75552009-07-28 19:55:13 +0000917}
918
Devang Patel9d7de2a2009-07-31 18:18:52 +0000919/// processDeclare - Process DbgDeclareInst.
920void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000921 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
922 if (!N) return;
923
924 DIDescriptor DV(N);
925 if (!DV.isVariable())
Devang Patel9d7de2a2009-07-31 18:18:52 +0000926 return;
927
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000928 if (!NodesSeen.insert(DV))
Devang Patel9d7de2a2009-07-31 18:18:52 +0000929 return;
Devang Patel5ea5d792011-09-06 17:40:08 +0000930 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
931 addCompileUnit(DIVariable(N).getCompileUnit());
Devang Patel3b548aa2010-03-08 20:52:55 +0000932 processType(DIVariable(N).getType());
Devang Patel9d7de2a2009-07-31 18:18:52 +0000933}
934
Devang Pateld0658ef2009-08-10 22:09:58 +0000935/// addType - Add type into Tys.
936bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000937 if (!DT.isValid())
Devang Pateld0658ef2009-08-10 22:09:58 +0000938 return false;
939
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000940 if (!NodesSeen.insert(DT))
Devang Pateld0658ef2009-08-10 22:09:58 +0000941 return false;
942
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000943 TYs.push_back(DT);
Devang Pateld0658ef2009-08-10 22:09:58 +0000944 return true;
945}
946
Devang Patel1da75552009-07-28 19:55:13 +0000947/// addCompileUnit - Add compile unit into CUs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000948bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000949 if (!CU.Verify())
Devang Patel1da75552009-07-28 19:55:13 +0000950 return false;
951
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000952 if (!NodesSeen.insert(CU))
Devang Patel1da75552009-07-28 19:55:13 +0000953 return false;
954
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000955 CUs.push_back(CU);
Devang Patel1da75552009-07-28 19:55:13 +0000956 return true;
957}
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000958
Devang Patel1da75552009-07-28 19:55:13 +0000959/// addGlobalVariable - Add global variable into GVs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000960bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000961 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Patel1da75552009-07-28 19:55:13 +0000962 return false;
963
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000964 if (!NodesSeen.insert(DIG))
Devang Patel1da75552009-07-28 19:55:13 +0000965 return false;
966
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000967 GVs.push_back(DIG);
Devang Patel1da75552009-07-28 19:55:13 +0000968 return true;
969}
970
971// addSubprogram - Add subprgoram into SPs.
Devang Patel3b4e8272009-07-30 18:25:15 +0000972bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000973 if (!DIDescriptor(SP).isSubprogram())
Devang Patel1da75552009-07-28 19:55:13 +0000974 return false;
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000975
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000976 if (!NodesSeen.insert(SP))
Devang Patel1da75552009-07-28 19:55:13 +0000977 return false;
978
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000979 SPs.push_back(SP);
Devang Patel1da75552009-07-28 19:55:13 +0000980 return true;
981}
982
Bill Wendling3b70d782012-06-26 23:22:18 +0000983//===----------------------------------------------------------------------===//
984// DIDescriptor: dump routines for all descriptors.
985//===----------------------------------------------------------------------===//
986
987/// dump - Print descriptor to dbgs() with a newline.
988void DIDescriptor::dump() const {
989 print(dbgs()); dbgs() << '\n';
990}
991
992/// print - Print descriptor.
993void DIDescriptor::print(raw_ostream &OS) const {
994 if (!DbgNode) return;
995
Bill Wendlinga2ccbf02012-06-28 02:17:58 +0000996 if (const char *Tag = dwarf::TagString(getTag()))
997 OS << "[ " << Tag << " ]";
Bill Wendling3b70d782012-06-26 23:22:18 +0000998
999 if (this->isSubrange()) {
1000 DISubrange(DbgNode).printInternal(OS);
Bill Wendling3b70d782012-06-26 23:22:18 +00001001 } else if (this->isCompileUnit()) {
1002 DICompileUnit(DbgNode).printInternal(OS);
1003 } else if (this->isFile()) {
1004 DIFile(DbgNode).printInternal(OS);
1005 } else if (this->isEnumerator()) {
1006 DIEnumerator(DbgNode).printInternal(OS);
1007 } else if (this->isBasicType()) {
1008 DIType(DbgNode).printInternal(OS);
1009 } else if (this->isDerivedType()) {
1010 DIDerivedType(DbgNode).printInternal(OS);
1011 } else if (this->isCompositeType()) {
1012 DICompositeType(DbgNode).printInternal(OS);
1013 } else if (this->isSubprogram()) {
1014 DISubprogram(DbgNode).printInternal(OS);
1015 } else if (this->isGlobalVariable()) {
1016 DIGlobalVariable(DbgNode).printInternal(OS);
1017 } else if (this->isVariable()) {
1018 DIVariable(DbgNode).printInternal(OS);
Bill Wendlingaa02e362012-07-06 19:12:31 +00001019 } else if (this->isObjCProperty()) {
1020 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling32705822012-07-06 23:06:16 +00001021 } else if (this->isScope()) {
1022 DIScope(DbgNode).printInternal(OS);
Bill Wendling3b70d782012-06-26 23:22:18 +00001023 }
1024}
1025
1026void DISubrange::printInternal(raw_ostream &OS) const {
1027 OS << " [" << getLo() << ", " << getHi() << ']';
1028}
1029
1030void DIScope::printInternal(raw_ostream &OS) const {
1031 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1032}
1033
1034void DICompileUnit::printInternal(raw_ostream &OS) const {
1035 DIScope::printInternal(OS);
1036 if (unsigned Lang = getLanguage())
1037 OS << " [" << dwarf::LanguageString(Lang) << ']';
1038}
1039
1040void DIEnumerator::printInternal(raw_ostream &OS) const {
1041 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1042}
1043
1044void DIType::printInternal(raw_ostream &OS) const {
1045 if (!DbgNode) return;
1046
1047 StringRef Res = getName();
1048 if (!Res.empty())
1049 OS << " [" << Res << "]";
1050
1051 // TODO: Print context?
1052
1053 OS << " [line " << getLineNumber()
1054 << ", size " << getSizeInBits()
1055 << ", align " << getAlignInBits()
1056 << ", offset " << getOffsetInBits();
1057 if (isBasicType())
Bill Wendling74ac0232012-06-28 02:12:20 +00001058 if (const char *Enc =
1059 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1060 OS << ", enc " << Enc;
Bill Wendling3b70d782012-06-26 23:22:18 +00001061 OS << "]";
1062
1063 if (isPrivate())
1064 OS << " [private]";
1065 else if (isProtected())
1066 OS << " [protected]";
1067
1068 if (isForwardDecl())
1069 OS << " [fwd]";
1070}
1071
1072void DIDerivedType::printInternal(raw_ostream &OS) const {
1073 DIType::printInternal(OS);
1074 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1075}
1076
1077void DICompositeType::printInternal(raw_ostream &OS) const {
1078 DIType::printInternal(OS);
1079 DIArray A = getTypeArray();
1080 OS << " [" << A.getNumElements() << " elements]";
1081}
1082
1083void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendling3b70d782012-06-26 23:22:18 +00001084 // TODO : Print context
Bill Wendling3b70d782012-06-26 23:22:18 +00001085 OS << " [line " << getLineNumber() << ']';
1086
1087 if (isLocalToUnit())
1088 OS << " [local]";
1089
1090 if (isDefinition())
1091 OS << " [def]";
1092
1093 if (getScopeLineNumber() != getLineNumber())
1094 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendling56543732012-07-06 23:43:12 +00001095
1096 StringRef Res = getName();
1097 if (!Res.empty())
1098 OS << " [" << Res << ']';
Bill Wendling3b70d782012-06-26 23:22:18 +00001099}
1100
1101void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1102 StringRef Res = getName();
1103 if (!Res.empty())
1104 OS << " [" << Res << ']';
1105
1106 OS << " [line " << getLineNumber() << ']';
1107
1108 // TODO : Print context
1109
1110 if (isLocalToUnit())
1111 OS << " [local]";
1112
1113 if (isDefinition())
1114 OS << " [def]";
1115}
1116
1117void DIVariable::printInternal(raw_ostream &OS) const {
1118 StringRef Res = getName();
1119 if (!Res.empty())
1120 OS << " [" << Res << ']';
1121
1122 OS << " [line " << getLineNumber() << ']';
1123}
1124
Bill Wendlingaa02e362012-07-06 19:12:31 +00001125void DIObjCProperty::printInternal(raw_ostream &OS) const {
1126 StringRef Name = getObjCPropertyName();
1127 if (!Name.empty())
1128 OS << " [" << Name << ']';
1129
1130 OS << " [line " << getLineNumber()
1131 << ", properties " << getUnsignedField(6) << ']';
1132}
1133
Bill Wendling3b70d782012-06-26 23:22:18 +00001134static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1135 const LLVMContext &Ctx) {
1136 if (!DL.isUnknown()) { // Print source line info.
1137 DIScope Scope(DL.getScope(Ctx));
1138 // Omit the directory, because it's likely to be long and uninteresting.
1139 if (Scope.Verify())
1140 CommentOS << Scope.getFilename();
1141 else
1142 CommentOS << "<unknown>";
1143 CommentOS << ':' << DL.getLine();
1144 if (DL.getCol() != 0)
1145 CommentOS << ':' << DL.getCol();
1146 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1147 if (!InlinedAtDL.isUnknown()) {
1148 CommentOS << " @[ ";
1149 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1150 CommentOS << " ]";
1151 }
1152 }
1153}
1154
1155void DIVariable::printExtendedName(raw_ostream &OS) const {
1156 const LLVMContext &Ctx = DbgNode->getContext();
1157 StringRef Res = getName();
1158 if (!Res.empty())
1159 OS << Res << "," << getLineNumber();
1160 if (MDNode *InlinedAt = getInlinedAt()) {
1161 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1162 if (!InlinedAtDL.isUnknown()) {
1163 OS << " @[";
1164 printDebugLoc(InlinedAtDL, OS, Ctx);
1165 OS << "]";
1166 }
1167 }
1168}