blob: 92d7e4590a9606a2e4c47bd973a77c0d3530b889 [file] [log] [blame]
Bill Wendling523bea82013-11-08 08:13:15 +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
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "LLVMContextImpl.h"
Bill Wendling523bea82013-11-08 08:13:15 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000022#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000028#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33using namespace llvm::dwarf;
34
35//===----------------------------------------------------------------------===//
36// DIDescriptor
37//===----------------------------------------------------------------------===//
38
39bool DIDescriptor::Verify() const {
40 return DbgNode &&
41 (DIDerivedType(DbgNode).Verify() ||
42 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
43 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
44 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
45 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
46 DILexicalBlock(DbgNode).Verify() ||
47 DILexicalBlockFile(DbgNode).Verify() ||
48 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
49 DIObjCProperty(DbgNode).Verify() ||
50 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
Adrian Prantl87b7eb92014-10-01 18:55:02 +000052 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000053}
54
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000055static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000056 if (!DbgNode || Elt >= DbgNode->getNumOperands())
57 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000058 return DbgNode->getOperand(Elt);
59}
60
61static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
63}
64
65static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
67 return MDS->getString();
68 return StringRef();
69}
70
71StringRef DIDescriptor::getStringField(unsigned Elt) const {
72 return ::getStringField(DbgNode, Elt);
73}
74
75uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000076 if (auto *C = getConstantField(Elt))
77 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Bill Wendling523bea82013-11-08 08:13:15 +000078 return CI->getZExtValue();
79
80 return 0;
81}
82
83int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000084 if (auto *C = getConstantField(Elt))
85 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
86 return CI->getZExtValue();
Bill Wendling523bea82013-11-08 08:13:15 +000087
88 return 0;
89}
90
91DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
92 MDNode *Field = getNodeField(DbgNode, Elt);
93 return DIDescriptor(Field);
94}
95
96GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000097 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +000098}
99
100Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000101 if (!DbgNode)
102 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000103
104 if (Elt < DbgNode->getNumOperands())
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000105 if (auto *C =
106 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
107 return C->getValue();
Craig Topperc6207612014-04-09 06:08:46 +0000108 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000109}
110
111Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000112 return dyn_cast_or_null<Function>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000113}
114
115void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000116 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000117 return;
118
119 if (Elt < DbgNode->getNumOperands()) {
120 MDNode *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000121 Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000122 }
123}
124
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000125static unsigned DIVariableInlinedAtIndex = 4;
126MDNode *DIVariable::getInlinedAt() const {
127 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
128}
Bill Wendling523bea82013-11-08 08:13:15 +0000129
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000130/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000131unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
132 DIType Ty = getType().resolve(Map);
133 // Follow derived types until we reach a type that
134 // reports back a size.
135 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
136 DIDerivedType DT(&*Ty);
137 Ty = DT.getTypeDerivedFrom().resolve(Map);
138 }
139 assert(Ty.getSizeInBits() && "type with size 0");
140 return Ty.getSizeInBits();
141}
142
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000143uint64_t DIExpression::getElement(unsigned Idx) const {
144 unsigned I = Idx + 1;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000145 assert(I < getNumHeaderFields() &&
146 "non-existing complex address element requested");
147 return getHeaderFieldAs<int64_t>(I);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000148}
Adrian Prantlb1416832014-08-01 22:11:58 +0000149
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000150bool DIExpression::isBitPiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000151 unsigned N = getNumElements();
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000152 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000153}
154
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000155uint64_t DIExpression::getBitPieceOffset() const {
156 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000157 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000158}
159
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000160uint64_t DIExpression::getBitPieceSize() const {
161 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000162 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000163}
Adrian Prantlb1416832014-08-01 22:11:58 +0000164
Adrian Prantl2585a982015-01-22 16:55:20 +0000165DIExpression::iterator DIExpression::begin() const {
166 return DIExpression::iterator(*this);
Adrian Prantl9260cca2015-01-22 00:00:52 +0000167}
168
Adrian Prantl2585a982015-01-22 16:55:20 +0000169DIExpression::iterator DIExpression::end() const {
170 return DIExpression::iterator();
Adrian Prantl9260cca2015-01-22 00:00:52 +0000171}
172
Benjamin Kramer4d50b6c2015-01-24 19:55:23 +0000173DIExpression::Operand DIExpression::Operand::getNext() const {
Adrian Prantl70f2a732015-01-23 23:40:47 +0000174 iterator it(I);
175 return *(++it);
176}
177
Bill Wendling523bea82013-11-08 08:13:15 +0000178//===----------------------------------------------------------------------===//
179// Predicates
180//===----------------------------------------------------------------------===//
181
Manman Renf8a19672014-07-28 22:24:06 +0000182bool DIDescriptor::isSubroutineType() const {
Yaron Kerene8270222014-12-19 22:15:09 +0000183 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
Manman Renf8a19672014-07-28 22:24:06 +0000184}
185
Bill Wendling523bea82013-11-08 08:13:15 +0000186bool DIDescriptor::isBasicType() const {
187 if (!DbgNode)
188 return false;
189 switch (getTag()) {
190 case dwarf::DW_TAG_base_type:
191 case dwarf::DW_TAG_unspecified_type:
192 return true;
193 default:
194 return false;
195 }
196}
197
Bill Wendling523bea82013-11-08 08:13:15 +0000198bool DIDescriptor::isDerivedType() const {
199 if (!DbgNode)
200 return false;
201 switch (getTag()) {
202 case dwarf::DW_TAG_typedef:
203 case dwarf::DW_TAG_pointer_type:
204 case dwarf::DW_TAG_ptr_to_member_type:
205 case dwarf::DW_TAG_reference_type:
206 case dwarf::DW_TAG_rvalue_reference_type:
207 case dwarf::DW_TAG_const_type:
208 case dwarf::DW_TAG_volatile_type:
209 case dwarf::DW_TAG_restrict_type:
210 case dwarf::DW_TAG_member:
211 case dwarf::DW_TAG_inheritance:
212 case dwarf::DW_TAG_friend:
213 return true;
214 default:
215 // CompositeTypes are currently modelled as DerivedTypes.
216 return isCompositeType();
217 }
218}
219
Bill Wendling523bea82013-11-08 08:13:15 +0000220bool DIDescriptor::isCompositeType() const {
221 if (!DbgNode)
222 return false;
223 switch (getTag()) {
224 case dwarf::DW_TAG_array_type:
225 case dwarf::DW_TAG_structure_type:
226 case dwarf::DW_TAG_union_type:
227 case dwarf::DW_TAG_enumeration_type:
228 case dwarf::DW_TAG_subroutine_type:
229 case dwarf::DW_TAG_class_type:
230 return true;
231 default:
232 return false;
233 }
234}
235
Bill Wendling523bea82013-11-08 08:13:15 +0000236bool DIDescriptor::isVariable() const {
237 if (!DbgNode)
238 return false;
239 switch (getTag()) {
240 case dwarf::DW_TAG_auto_variable:
241 case dwarf::DW_TAG_arg_variable:
242 return true;
243 default:
244 return false;
245 }
246}
247
Bill Wendling523bea82013-11-08 08:13:15 +0000248bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000249 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000250}
251
Bill Wendling523bea82013-11-08 08:13:15 +0000252bool DIDescriptor::isSubprogram() const {
253 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
254}
255
Bill Wendling523bea82013-11-08 08:13:15 +0000256bool DIDescriptor::isGlobalVariable() const {
Duncan P. N. Exon Smithb407bb22015-02-09 22:48:04 +0000257 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Bill Wendling523bea82013-11-08 08:13:15 +0000258}
259
Bill Wendling523bea82013-11-08 08:13:15 +0000260bool DIDescriptor::isScope() const {
261 if (!DbgNode)
262 return false;
263 switch (getTag()) {
264 case dwarf::DW_TAG_compile_unit:
265 case dwarf::DW_TAG_lexical_block:
266 case dwarf::DW_TAG_subprogram:
267 case dwarf::DW_TAG_namespace:
268 case dwarf::DW_TAG_file_type:
269 return true;
270 default:
271 break;
272 }
273 return isType();
274}
275
Bill Wendling523bea82013-11-08 08:13:15 +0000276bool DIDescriptor::isTemplateTypeParameter() const {
277 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
278}
279
Bill Wendling523bea82013-11-08 08:13:15 +0000280bool DIDescriptor::isTemplateValueParameter() const {
281 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
282 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
283 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
284}
285
Bill Wendling523bea82013-11-08 08:13:15 +0000286bool DIDescriptor::isCompileUnit() const {
287 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
288}
289
Bill Wendling523bea82013-11-08 08:13:15 +0000290bool DIDescriptor::isFile() const {
291 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
292}
293
Bill Wendling523bea82013-11-08 08:13:15 +0000294bool DIDescriptor::isNameSpace() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
296}
297
Bill Wendling523bea82013-11-08 08:13:15 +0000298bool DIDescriptor::isLexicalBlockFile() const {
299 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000300 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000301}
302
Bill Wendling523bea82013-11-08 08:13:15 +0000303bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000304 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
305 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000306 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000307 DbgNode->getNumOperands() == 3 &&
308 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000309}
310
Bill Wendling523bea82013-11-08 08:13:15 +0000311bool DIDescriptor::isSubrange() const {
312 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
313}
314
Bill Wendling523bea82013-11-08 08:13:15 +0000315bool DIDescriptor::isEnumerator() const {
316 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
317}
318
Bill Wendling523bea82013-11-08 08:13:15 +0000319bool DIDescriptor::isObjCProperty() const {
320 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
321}
322
Bill Wendling523bea82013-11-08 08:13:15 +0000323bool DIDescriptor::isImportedEntity() const {
324 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
325 getTag() == dwarf::DW_TAG_imported_declaration);
326}
327
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000328bool DIDescriptor::isExpression() const {
329 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
330}
331
Bill Wendling523bea82013-11-08 08:13:15 +0000332//===----------------------------------------------------------------------===//
333// Simple Descriptor Constructors and other Methods
334//===----------------------------------------------------------------------===//
335
Frederic Riss36acf0f2014-09-15 07:50:36 +0000336void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000337
338 assert(DbgNode && "Trying to replace an unverified type!");
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.
David Blaikied3f094a2014-05-06 03:41:57 +0000345 const MDNode *DN = D;
346 if (DbgNode == DN) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000347 SmallVector<Metadata *, 10> Ops(DbgNode->op_begin(), DbgNode->op_end());
David Blaikied3f094a2014-05-06 03:41:57 +0000348 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000349 }
David Blaikied3f094a2014-05-06 03:41:57 +0000350
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000351 assert(DbgNode->isTemporary() && "Expected temporary node");
352 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000353 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
David Blaikied3f094a2014-05-06 03:41:57 +0000354 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000355 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000356}
357
Frederic Riss36acf0f2014-09-15 07:50:36 +0000358void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000359 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000360 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000361 assert(DbgNode->isTemporary() && "Expected temporary node");
362 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000363 Node->replaceAllUsesWith(D);
David Blaikied3f094a2014-05-06 03:41:57 +0000364 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000365}
366
Bill Wendling523bea82013-11-08 08:13:15 +0000367bool DICompileUnit::Verify() const {
368 if (!isCompileUnit())
369 return false;
370
371 // Don't bother verifying the compilation directory or producer string
372 // as those could be empty.
373 if (getFilename().empty())
374 return false;
375
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000376 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000377}
378
Bill Wendling523bea82013-11-08 08:13:15 +0000379bool DIObjCProperty::Verify() const {
380 if (!isObjCProperty())
381 return false;
382
383 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000384 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000385}
386
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000387/// \brief Check if a field at position Elt of a MDNode is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000388static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000389 Metadata *Fld = getField(DbgNode, Elt);
Duncan P. N. Exon Smitha55dcaf2015-02-17 22:34:15 +0000390 return !Fld || isa<MDNode>(Fld);
Bill Wendling523bea82013-11-08 08:13:15 +0000391}
392
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000393/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000394static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000395 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000396 return !Fld || isa<MDString>(Fld);
397}
398
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000399/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000400static bool isTypeRef(const Metadata *MD) {
401 if (!MD)
402 return true;
403 if (auto *S = dyn_cast<MDString>(MD))
404 return !S->getString().empty();
405 if (auto *N = dyn_cast<MDNode>(MD))
406 return DIType(N).isType();
407 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000408}
409
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000410/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000411static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000412 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000413}
414
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000415/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000416static bool isScopeRef(const Metadata *MD) {
417 if (!MD)
418 return true;
419 if (auto *S = dyn_cast<MDString>(MD))
420 return !S->getString().empty();
Duncan P. N. Exon Smith8551d252015-02-18 19:46:02 +0000421 if (auto *N = dyn_cast<MDNode>(MD))
422 return DIScope(N).isScope();
423 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000424}
425
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000426/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000427static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000428 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000429}
430
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000431#ifndef NDEBUG
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000432/// \brief Check if a value can be a DescriptorRef.
433static bool isDescriptorRef(const Metadata *MD) {
434 if (!MD)
435 return true;
436 if (auto *S = dyn_cast<MDString>(MD))
437 return !S->getString().empty();
438 return isa<MDNode>(MD);
439}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000440#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000441
Bill Wendling523bea82013-11-08 08:13:15 +0000442bool DIType::Verify() const {
443 if (!isType())
444 return false;
445 // Make sure Context @ field 2 is MDNode.
446 if (!fieldIsScopeRef(DbgNode, 2))
447 return false;
448
449 // FIXME: Sink this into the various subclass verifies.
450 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000451 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000452 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
453 Tag != dwarf::DW_TAG_ptr_to_member_type &&
454 Tag != dwarf::DW_TAG_reference_type &&
455 Tag != dwarf::DW_TAG_rvalue_reference_type &&
456 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
457 Tag != dwarf::DW_TAG_enumeration_type &&
458 Tag != dwarf::DW_TAG_subroutine_type &&
459 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
460 getFilename().empty())
461 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000462
Bill Wendling523bea82013-11-08 08:13:15 +0000463 // DIType is abstract, it should be a BasicType, a DerivedType or
464 // a CompositeType.
465 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000466 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000467 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000468 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000469 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000470 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000471 else
472 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000473}
474
Bill Wendling523bea82013-11-08 08:13:15 +0000475bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000476 return isBasicType() && DbgNode->getNumOperands() == 3 &&
477 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000478}
479
Bill Wendling523bea82013-11-08 08:13:15 +0000480bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000481 // Make sure DerivedFrom @ field 3 is TypeRef.
482 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000483 return false;
484 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000485 // Make sure ClassType @ field 4 is a TypeRef.
486 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000487 return false;
488
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000489 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
490 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
491 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000492}
493
Bill Wendling523bea82013-11-08 08:13:15 +0000494bool DICompositeType::Verify() const {
495 if (!isCompositeType())
496 return false;
497
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000498 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
499 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000500 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000501 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000502 return false;
503
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000504 // Make sure the type identifier at field 7 is MDString, it can be null.
505 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000506 return false;
507
Adrian Prantl99c7af22013-12-18 21:48:19 +0000508 // A subroutine type can't be both & and &&.
509 if (isLValueReference() && isRValueReference())
510 return false;
511
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000512 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000513}
514
Bill Wendling523bea82013-11-08 08:13:15 +0000515bool DISubprogram::Verify() const {
516 if (!isSubprogram())
517 return false;
518
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000519 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000520 if (!fieldIsScopeRef(DbgNode, 2))
521 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000522 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000523 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000524 // Containing type @ field 4.
525 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000526 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000527
528 // A subprogram can't be both & and &&.
529 if (isLValueReference() && isRValueReference())
530 return false;
531
David Blaikie3dfe4782014-10-14 18:22:52 +0000532 // If a DISubprogram has an llvm::Function*, then scope chains from all
533 // instructions within the function should lead to this DISubprogram.
534 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000535 for (auto &BB : *F) {
536 for (auto &I : BB) {
537 DebugLoc DL = I.getDebugLoc();
538 if (DL.isUnknown())
539 continue;
540
541 MDNode *Scope = nullptr;
542 MDNode *IA = nullptr;
543 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000544 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000545 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000546 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000547 if (!Scope)
548 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000549 assert(!IA);
550 while (!DIDescriptor(Scope).isSubprogram()) {
551 DILexicalBlockFile D(Scope);
552 Scope = D.isLexicalBlockFile()
553 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000554 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000555 if (!Scope)
556 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000557 }
558 if (!DISubprogram(Scope).describes(F))
559 return false;
560 }
561 }
562 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000563 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000564}
565
Bill Wendling523bea82013-11-08 08:13:15 +0000566bool DIGlobalVariable::Verify() const {
567 if (!isGlobalVariable())
568 return false;
569
570 if (getDisplayName().empty())
571 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000572 // Make sure context @ field 1 is an MDNode.
573 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000574 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000575 // Make sure that type @ field 3 is a DITypeRef.
576 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000577 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000578 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
579 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000580 return false;
581
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000582 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000583}
584
Bill Wendling523bea82013-11-08 08:13:15 +0000585bool DIVariable::Verify() const {
586 if (!isVariable())
587 return false;
588
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000589 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000590 if (!fieldIsMDNode(DbgNode, 1))
591 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000592 // Make sure that type @ field 3 is a DITypeRef.
593 if (!fieldIsTypeRef(DbgNode, 3))
594 return false;
595
596 // Check the number of header fields, which is common between complex and
597 // simple variables.
598 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000599 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000600
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000601 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000602 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000603 return true;
604
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000605 // Variable with an inline location.
606 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000607}
608
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000609bool DIExpression::Verify() const {
610 // Empty DIExpressions may be represented as a nullptr.
611 if (!DbgNode)
612 return true;
613
Adrian Prantl9260cca2015-01-22 00:00:52 +0000614 if (!(isExpression() && DbgNode->getNumOperands() == 1))
615 return false;
616
Adrian Prantl70f2a732015-01-23 23:40:47 +0000617 for (auto Op : *this)
618 switch (Op) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000619 case DW_OP_bit_piece:
Adrian Prantl9260cca2015-01-22 00:00:52 +0000620 // Must be the last element of the expression.
Adrian Prantl70f2a732015-01-23 23:40:47 +0000621 return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000622 case DW_OP_plus:
Adrian Prantl70f2a732015-01-23 23:40:47 +0000623 if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000624 return false;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000625 break;
Adrian Prantl9260cca2015-01-22 00:00:52 +0000626 case DW_OP_deref:
627 break;
628 default:
629 // Other operators are not yet supported by the backend.
630 return false;
631 }
632 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000633}
634
Bill Wendling523bea82013-11-08 08:13:15 +0000635bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000636 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000637}
638
Bill Wendling523bea82013-11-08 08:13:15 +0000639bool DINameSpace::Verify() const {
640 if (!isNameSpace())
641 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000642 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000643}
644
Bill Wendling523bea82013-11-08 08:13:15 +0000645MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
646
Bill Wendling523bea82013-11-08 08:13:15 +0000647bool DIFile::Verify() const {
648 return isFile() && DbgNode->getNumOperands() == 2;
649}
650
Bill Wendling523bea82013-11-08 08:13:15 +0000651bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000652 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
653 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000654}
655
Bill Wendling523bea82013-11-08 08:13:15 +0000656bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000657 return isSubrange() && DbgNode->getNumOperands() == 1 &&
658 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000659}
660
Bill Wendling523bea82013-11-08 08:13:15 +0000661bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000662 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
663 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000664}
665
Bill Wendling523bea82013-11-08 08:13:15 +0000666bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000667 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
668 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000669}
670
Bill Wendling523bea82013-11-08 08:13:15 +0000671bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000672 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
673 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000674}
675
Bill Wendling523bea82013-11-08 08:13:15 +0000676bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000677 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
678 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000679}
680
Bill Wendling523bea82013-11-08 08:13:15 +0000681bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000682 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
683 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000684}
685
Bill Wendling523bea82013-11-08 08:13:15 +0000686MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000687 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000688}
689
690MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000691 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000692}
693
694#ifndef NDEBUG
695static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
696 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
697 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000698 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000699 continue;
700 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
701 bool found = false;
702 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000703 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000704 assert(found && "Losing a member during member list replacement");
705 }
706}
707#endif
708
Manman Ren1a125c92014-07-28 19:33:20 +0000709void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000710 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000711 if (Elements) {
712#ifndef NDEBUG
713 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000714 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000715 VerifySubsetOf(El, Elements);
716#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000717 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000718 }
719 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000720 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000721 DbgNode = N;
722}
723
Bill Wendling523bea82013-11-08 08:13:15 +0000724DIScopeRef DIScope::getRef() const {
725 if (!isCompositeType())
726 return DIScopeRef(*this);
727 DICompositeType DTy(DbgNode);
728 if (!DTy.getIdentifier())
729 return DIScopeRef(*this);
730 return DIScopeRef(DTy.getIdentifier());
731}
732
Bill Wendling523bea82013-11-08 08:13:15 +0000733void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000734 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000735 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000736 DbgNode = N;
737}
738
Bill Wendling523bea82013-11-08 08:13:15 +0000739bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
740 assert(CurFn && "Invalid function");
741 if (!getContext().isSubprogram())
742 return false;
743 // This variable is not inlined function argument if its scope
744 // does not describe current function.
745 return !DISubprogram(getContext()).describes(CurFn);
746}
747
Bill Wendling523bea82013-11-08 08:13:15 +0000748bool DISubprogram::describes(const Function *F) {
749 assert(F && "Invalid function");
750 if (F == getFunction())
751 return true;
752 StringRef Name = getLinkageName();
753 if (Name.empty())
754 Name = getName();
755 if (F->getName() == Name)
756 return true;
757 return false;
758}
759
Bill Wendling523bea82013-11-08 08:13:15 +0000760MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000761 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000762}
763
764DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000765 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000766}
767
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000768Metadata *DITemplateValueParameter::getValue() const {
769 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000770}
771
Bill Wendling523bea82013-11-08 08:13:15 +0000772DIScopeRef DIScope::getContext() const {
773
774 if (isType())
775 return DIType(DbgNode).getContext();
776
777 if (isSubprogram())
778 return DIScopeRef(DISubprogram(DbgNode).getContext());
779
780 if (isLexicalBlock())
781 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
782
783 if (isLexicalBlockFile())
784 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
785
786 if (isNameSpace())
787 return DIScopeRef(DINameSpace(DbgNode).getContext());
788
789 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000790 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000791}
792
Bill Wendling523bea82013-11-08 08:13:15 +0000793StringRef DIScope::getName() const {
794 if (isType())
795 return DIType(DbgNode).getName();
796 if (isSubprogram())
797 return DISubprogram(DbgNode).getName();
798 if (isNameSpace())
799 return DINameSpace(DbgNode).getName();
800 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
801 isCompileUnit()) &&
802 "Unhandled type of scope.");
803 return StringRef();
804}
805
806StringRef DIScope::getFilename() const {
807 if (!DbgNode)
808 return StringRef();
809 return ::getStringField(getNodeField(DbgNode, 1), 0);
810}
811
812StringRef DIScope::getDirectory() const {
813 if (!DbgNode)
814 return StringRef();
815 return ::getStringField(getNodeField(DbgNode, 1), 1);
816}
817
818DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000819 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000820 return DIArray();
821
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000822 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000823}
824
825DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000826 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000827 return DIArray();
828
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000829 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000830}
831
832DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000833 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000834 return DIArray();
835
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000836 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000837}
838
839DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000840 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000841 return DIArray();
842
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000843 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000844}
845
846DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000847 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000848 return DIArray();
849
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000850 return DIArray(getNodeField(DbgNode, 6));
851}
852
853void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
854 assert(Verify() && "Expected compile unit");
855 if (Subprograms == getSubprograms())
856 return;
857
858 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
859}
860
861void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
862 assert(Verify() && "Expected compile unit");
863 if (GlobalVariables == getGlobalVariables())
864 return;
865
866 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000867}
868
Diego Novillof5041ce2014-03-03 20:06:11 +0000869DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000870 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000871 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000872 assert(NewScope && "Expected valid scope");
873
874 const auto *Old = cast<MDLocation>(DbgNode);
875 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
876 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000877}
878
Diego Novillof5041ce2014-03-03 20:06:11 +0000879unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
880 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
881 return ++Ctx.pImpl->DiscriminatorTable[Key];
882}
883
Bill Wendling523bea82013-11-08 08:13:15 +0000884DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
885 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000886 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
887 if (!InlinedScope)
888 return cleanseInlinedVariable(DV, VMContext);
889
890 // Insert inlined scope.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000891 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
892 DV->op_begin() + DIVariableInlinedAtIndex);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000893 Elts.push_back(InlinedScope);
894
895 DIVariable Inlined(MDNode::get(VMContext, Elts));
896 assert(Inlined.Verify() && "Expected to create a DIVariable");
897 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000898}
899
Bill Wendling523bea82013-11-08 08:13:15 +0000900DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000901 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
902 if (!DIVariable(DV).getInlinedAt())
903 return DIVariable(DV);
904
905 // Remove inlined scope.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000906 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
907 DV->op_begin() + DIVariableInlinedAtIndex);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000908
909 DIVariable Cleansed(MDNode::get(VMContext, Elts));
910 assert(Cleansed.Verify() && "Expected to create a DIVariable");
911 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000912}
913
Bill Wendling523bea82013-11-08 08:13:15 +0000914DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
915 DIDescriptor D(Scope);
916 if (D.isSubprogram())
917 return DISubprogram(Scope);
918
919 if (D.isLexicalBlockFile())
920 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
921
922 if (D.isLexicalBlock())
923 return getDISubprogram(DILexicalBlock(Scope).getContext());
924
925 return DISubprogram();
926}
927
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000928DISubprogram llvm::getDISubprogram(const Function *F) {
929 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000930 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000931 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
932 return !Inst.getDebugLoc().isUnknown();
933 });
934 if (Inst == BB.end())
935 continue;
936 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000937 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000938 DISubprogram Subprogram = getDISubprogram(Scope);
939 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000940 }
941
942 return DISubprogram();
943}
944
Bill Wendling523bea82013-11-08 08:13:15 +0000945DICompositeType llvm::getDICompositeType(DIType T) {
946 if (T.isCompositeType())
947 return DICompositeType(T);
948
949 if (T.isDerivedType()) {
950 // This function is currently used by dragonegg and dragonegg does
951 // not generate identifier for types, so using an empty map to resolve
952 // DerivedFrom should be fine.
953 DITypeIdentifierMap EmptyMap;
954 return getDICompositeType(
955 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
956 }
957
958 return DICompositeType();
959}
960
Bill Wendling523bea82013-11-08 08:13:15 +0000961DITypeIdentifierMap
962llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
963 DITypeIdentifierMap Map;
964 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000965 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000966 DIArray Retain = CU.getRetainedTypes();
967 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
968 if (!Retain.getElement(Ti).isCompositeType())
969 continue;
970 DICompositeType Ty(Retain.getElement(Ti));
971 if (MDString *TypeId = Ty.getIdentifier()) {
972 // Definition has priority over declaration.
973 // Try to insert (TypeId, Ty) to Map.
974 std::pair<DITypeIdentifierMap::iterator, bool> P =
975 Map.insert(std::make_pair(TypeId, Ty));
976 // If TypeId already exists in Map and this is a definition, replace
977 // whatever we had (declaration or definition) with the definition.
978 if (!P.second && !Ty.isForwardDecl())
979 P.first->second = Ty;
980 }
981 }
982 }
983 return Map;
984}
985
986//===----------------------------------------------------------------------===//
987// DebugInfoFinder implementations.
988//===----------------------------------------------------------------------===//
989
990void DebugInfoFinder::reset() {
991 CUs.clear();
992 SPs.clear();
993 GVs.clear();
994 TYs.clear();
995 Scopes.clear();
996 NodesSeen.clear();
997 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000998 TypeMapInitialized = false;
999}
1000
Manman Renb46e5502013-11-17 19:35:03 +00001001void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001002 if (!TypeMapInitialized)
1003 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1004 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1005 TypeMapInitialized = true;
1006 }
Bill Wendling523bea82013-11-08 08:13:15 +00001007}
1008
Bill Wendling523bea82013-11-08 08:13:15 +00001009void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001010 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001011 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001012 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001013 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001014 addCompileUnit(CU);
1015 DIArray GVs = CU.getGlobalVariables();
1016 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1017 DIGlobalVariable DIG(GVs.getElement(i));
1018 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +00001019 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001020 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001021 }
1022 }
1023 DIArray SPs = CU.getSubprograms();
1024 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1025 processSubprogram(DISubprogram(SPs.getElement(i)));
1026 DIArray EnumTypes = CU.getEnumTypes();
1027 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1028 processType(DIType(EnumTypes.getElement(i)));
1029 DIArray RetainedTypes = CU.getRetainedTypes();
1030 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1031 processType(DIType(RetainedTypes.getElement(i)));
1032 DIArray Imports = CU.getImportedEntities();
1033 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1034 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001035 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001036 if (Entity.isType())
1037 processType(DIType(Entity));
1038 else if (Entity.isSubprogram())
1039 processSubprogram(DISubprogram(Entity));
1040 else if (Entity.isNameSpace())
1041 processScope(DINameSpace(Entity).getContext());
1042 }
1043 }
1044 }
1045}
1046
Manman Ren2085ccc2013-11-17 18:42:37 +00001047void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001048 if (!Loc)
1049 return;
Manman Renb46e5502013-11-17 19:35:03 +00001050 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001051 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001052 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001053}
1054
Bill Wendling523bea82013-11-08 08:13:15 +00001055void DebugInfoFinder::processType(DIType DT) {
1056 if (!addType(DT))
1057 return;
1058 processScope(DT.getContext().resolve(TypeIdentifierMap));
1059 if (DT.isCompositeType()) {
1060 DICompositeType DCT(DT);
1061 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001062 if (DT.isSubroutineType()) {
1063 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1064 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1065 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1066 return;
1067 }
Manman Renab8ffba2014-07-28 19:14:13 +00001068 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001069 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1070 DIDescriptor D = DA.getElement(i);
1071 if (D.isType())
1072 processType(DIType(D));
1073 else if (D.isSubprogram())
1074 processSubprogram(DISubprogram(D));
1075 }
1076 } else if (DT.isDerivedType()) {
1077 DIDerivedType DDT(DT);
1078 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1079 }
1080}
1081
1082void DebugInfoFinder::processScope(DIScope Scope) {
1083 if (Scope.isType()) {
1084 DIType Ty(Scope);
1085 processType(Ty);
1086 return;
1087 }
1088 if (Scope.isCompileUnit()) {
1089 addCompileUnit(DICompileUnit(Scope));
1090 return;
1091 }
1092 if (Scope.isSubprogram()) {
1093 processSubprogram(DISubprogram(Scope));
1094 return;
1095 }
1096 if (!addScope(Scope))
1097 return;
1098 if (Scope.isLexicalBlock()) {
1099 DILexicalBlock LB(Scope);
1100 processScope(LB.getContext());
1101 } else if (Scope.isLexicalBlockFile()) {
1102 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1103 processScope(LBF.getScope());
1104 } else if (Scope.isNameSpace()) {
1105 DINameSpace NS(Scope);
1106 processScope(NS.getContext());
1107 }
1108}
1109
Bill Wendling523bea82013-11-08 08:13:15 +00001110void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1111 if (!addSubprogram(SP))
1112 return;
1113 processScope(SP.getContext().resolve(TypeIdentifierMap));
1114 processType(SP.getType());
1115 DIArray TParams = SP.getTemplateParams();
1116 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1117 DIDescriptor Element = TParams.getElement(I);
1118 if (Element.isTemplateTypeParameter()) {
1119 DITemplateTypeParameter TType(Element);
1120 processScope(TType.getContext().resolve(TypeIdentifierMap));
1121 processType(TType.getType().resolve(TypeIdentifierMap));
1122 } else if (Element.isTemplateValueParameter()) {
1123 DITemplateValueParameter TVal(Element);
1124 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1125 processType(TVal.getType().resolve(TypeIdentifierMap));
1126 }
1127 }
1128}
1129
Manman Ren2085ccc2013-11-17 18:42:37 +00001130void DebugInfoFinder::processDeclare(const Module &M,
1131 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001132 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1133 if (!N)
1134 return;
Manman Renb46e5502013-11-17 19:35:03 +00001135 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001136
1137 DIDescriptor DV(N);
1138 if (!DV.isVariable())
1139 return;
1140
David Blaikie70573dc2014-11-19 07:49:26 +00001141 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001142 return;
1143 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001144 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001145}
1146
Manman Ren2085ccc2013-11-17 18:42:37 +00001147void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001148 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1149 if (!N)
1150 return;
Manman Renb46e5502013-11-17 19:35:03 +00001151 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001152
1153 DIDescriptor DV(N);
1154 if (!DV.isVariable())
1155 return;
1156
David Blaikie70573dc2014-11-19 07:49:26 +00001157 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001158 return;
1159 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001160 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001161}
1162
Bill Wendling523bea82013-11-08 08:13:15 +00001163bool DebugInfoFinder::addType(DIType DT) {
1164 if (!DT)
1165 return false;
1166
David Blaikie70573dc2014-11-19 07:49:26 +00001167 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001168 return false;
1169
1170 TYs.push_back(DT);
1171 return true;
1172}
1173
Bill Wendling523bea82013-11-08 08:13:15 +00001174bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1175 if (!CU)
1176 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001177 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001178 return false;
1179
1180 CUs.push_back(CU);
1181 return true;
1182}
1183
Bill Wendling523bea82013-11-08 08:13:15 +00001184bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1185 if (!DIG)
1186 return false;
1187
David Blaikie70573dc2014-11-19 07:49:26 +00001188 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001189 return false;
1190
1191 GVs.push_back(DIG);
1192 return true;
1193}
1194
Bill Wendling523bea82013-11-08 08:13:15 +00001195bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1196 if (!SP)
1197 return false;
1198
David Blaikie70573dc2014-11-19 07:49:26 +00001199 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001200 return false;
1201
1202 SPs.push_back(SP);
1203 return true;
1204}
1205
1206bool DebugInfoFinder::addScope(DIScope Scope) {
1207 if (!Scope)
1208 return false;
1209 // FIXME: Ocaml binding generates a scope with no content, we treat it
1210 // as null for now.
1211 if (Scope->getNumOperands() == 0)
1212 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001213 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001214 return false;
1215 Scopes.push_back(Scope);
1216 return true;
1217}
1218
1219//===----------------------------------------------------------------------===//
1220// DIDescriptor: dump routines for all descriptors.
1221//===----------------------------------------------------------------------===//
1222
Bill Wendling523bea82013-11-08 08:13:15 +00001223void DIDescriptor::dump() const {
1224 print(dbgs());
1225 dbgs() << '\n';
1226}
1227
Bill Wendling523bea82013-11-08 08:13:15 +00001228void DIDescriptor::print(raw_ostream &OS) const {
1229 if (!DbgNode)
1230 return;
1231
1232 if (const char *Tag = dwarf::TagString(getTag()))
1233 OS << "[ " << Tag << " ]";
1234
1235 if (this->isSubrange()) {
1236 DISubrange(DbgNode).printInternal(OS);
1237 } else if (this->isCompileUnit()) {
1238 DICompileUnit(DbgNode).printInternal(OS);
1239 } else if (this->isFile()) {
1240 DIFile(DbgNode).printInternal(OS);
1241 } else if (this->isEnumerator()) {
1242 DIEnumerator(DbgNode).printInternal(OS);
1243 } else if (this->isBasicType()) {
1244 DIType(DbgNode).printInternal(OS);
1245 } else if (this->isDerivedType()) {
1246 DIDerivedType(DbgNode).printInternal(OS);
1247 } else if (this->isCompositeType()) {
1248 DICompositeType(DbgNode).printInternal(OS);
1249 } else if (this->isSubprogram()) {
1250 DISubprogram(DbgNode).printInternal(OS);
1251 } else if (this->isGlobalVariable()) {
1252 DIGlobalVariable(DbgNode).printInternal(OS);
1253 } else if (this->isVariable()) {
1254 DIVariable(DbgNode).printInternal(OS);
1255 } else if (this->isObjCProperty()) {
1256 DIObjCProperty(DbgNode).printInternal(OS);
1257 } else if (this->isNameSpace()) {
1258 DINameSpace(DbgNode).printInternal(OS);
1259 } else if (this->isScope()) {
1260 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001261 } else if (this->isExpression()) {
1262 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001263 }
1264}
1265
1266void DISubrange::printInternal(raw_ostream &OS) const {
1267 int64_t Count = getCount();
1268 if (Count != -1)
1269 OS << " [" << getLo() << ", " << Count - 1 << ']';
1270 else
1271 OS << " [unbounded]";
1272}
1273
1274void DIScope::printInternal(raw_ostream &OS) const {
1275 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1276}
1277
1278void DICompileUnit::printInternal(raw_ostream &OS) const {
1279 DIScope::printInternal(OS);
1280 OS << " [";
1281 unsigned Lang = getLanguage();
1282 if (const char *LangStr = dwarf::LanguageString(Lang))
1283 OS << LangStr;
1284 else
1285 (OS << "lang 0x").write_hex(Lang);
1286 OS << ']';
1287}
1288
1289void DIEnumerator::printInternal(raw_ostream &OS) const {
1290 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1291}
1292
1293void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001294 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001295 return;
1296
1297 StringRef Res = getName();
1298 if (!Res.empty())
1299 OS << " [" << Res << "]";
1300
1301 // TODO: Print context?
1302
1303 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1304 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1305 if (isBasicType())
1306 if (const char *Enc =
1307 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1308 OS << ", enc " << Enc;
1309 OS << "]";
1310
1311 if (isPrivate())
1312 OS << " [private]";
1313 else if (isProtected())
1314 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001315 else if (isPublic())
1316 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001317
1318 if (isArtificial())
1319 OS << " [artificial]";
1320
1321 if (isForwardDecl())
1322 OS << " [decl]";
1323 else if (getTag() == dwarf::DW_TAG_structure_type ||
1324 getTag() == dwarf::DW_TAG_union_type ||
1325 getTag() == dwarf::DW_TAG_enumeration_type ||
1326 getTag() == dwarf::DW_TAG_class_type)
1327 OS << " [def]";
1328 if (isVector())
1329 OS << " [vector]";
1330 if (isStaticMember())
1331 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001332
1333 if (isLValueReference())
1334 OS << " [reference]";
1335
1336 if (isRValueReference())
1337 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001338}
1339
1340void DIDerivedType::printInternal(raw_ostream &OS) const {
1341 DIType::printInternal(OS);
1342 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1343}
1344
1345void DICompositeType::printInternal(raw_ostream &OS) const {
1346 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001347 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001348 OS << " [" << A.getNumElements() << " elements]";
1349}
1350
1351void DINameSpace::printInternal(raw_ostream &OS) const {
1352 StringRef Name = getName();
1353 if (!Name.empty())
1354 OS << " [" << Name << ']';
1355
1356 OS << " [line " << getLineNumber() << ']';
1357}
1358
1359void DISubprogram::printInternal(raw_ostream &OS) const {
1360 // TODO : Print context
1361 OS << " [line " << getLineNumber() << ']';
1362
1363 if (isLocalToUnit())
1364 OS << " [local]";
1365
1366 if (isDefinition())
1367 OS << " [def]";
1368
1369 if (getScopeLineNumber() != getLineNumber())
1370 OS << " [scope " << getScopeLineNumber() << "]";
1371
1372 if (isPrivate())
1373 OS << " [private]";
1374 else if (isProtected())
1375 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001376 else if (isPublic())
1377 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001378
Adrian Prantl99c7af22013-12-18 21:48:19 +00001379 if (isLValueReference())
1380 OS << " [reference]";
1381
1382 if (isRValueReference())
1383 OS << " [rvalue reference]";
1384
Bill Wendling523bea82013-11-08 08:13:15 +00001385 StringRef Res = getName();
1386 if (!Res.empty())
1387 OS << " [" << Res << ']';
1388}
1389
1390void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1391 StringRef Res = getName();
1392 if (!Res.empty())
1393 OS << " [" << Res << ']';
1394
1395 OS << " [line " << getLineNumber() << ']';
1396
1397 // TODO : Print context
1398
1399 if (isLocalToUnit())
1400 OS << " [local]";
1401
1402 if (isDefinition())
1403 OS << " [def]";
1404}
1405
1406void DIVariable::printInternal(raw_ostream &OS) const {
1407 StringRef Res = getName();
1408 if (!Res.empty())
1409 OS << " [" << Res << ']';
1410
1411 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001412}
Adrian Prantlb1416832014-08-01 22:11:58 +00001413
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001414void DIExpression::printInternal(raw_ostream &OS) const {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001415 for (auto Op : *this) {
1416 OS << " [" << OperationEncodingString(Op);
1417 switch (Op) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001418 case DW_OP_plus: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001419 OS << " " << Op.getArg(1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001420 break;
1421 }
Adrian Prantl27bd01f2015-02-09 23:57:15 +00001422 case DW_OP_bit_piece: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001423 OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001424 break;
1425 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001426 case DW_OP_deref:
1427 // No arguments.
1428 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001429 default:
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001430 llvm_unreachable("unhandled operation");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001431 }
1432 OS << "]";
1433 }
Bill Wendling523bea82013-11-08 08:13:15 +00001434}
1435
1436void DIObjCProperty::printInternal(raw_ostream &OS) const {
1437 StringRef Name = getObjCPropertyName();
1438 if (!Name.empty())
1439 OS << " [" << Name << ']';
1440
1441 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1442 << ']';
1443}
1444
1445static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1446 const LLVMContext &Ctx) {
1447 if (!DL.isUnknown()) { // Print source line info.
1448 DIScope Scope(DL.getScope(Ctx));
1449 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1450 // Omit the directory, because it's likely to be long and uninteresting.
1451 CommentOS << Scope.getFilename();
1452 CommentOS << ':' << DL.getLine();
1453 if (DL.getCol() != 0)
1454 CommentOS << ':' << DL.getCol();
1455 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1456 if (!InlinedAtDL.isUnknown()) {
1457 CommentOS << " @[ ";
1458 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1459 CommentOS << " ]";
1460 }
1461 }
1462}
1463
1464void DIVariable::printExtendedName(raw_ostream &OS) const {
1465 const LLVMContext &Ctx = DbgNode->getContext();
1466 StringRef Res = getName();
1467 if (!Res.empty())
1468 OS << Res << "," << getLineNumber();
1469 if (MDNode *InlinedAt = getInlinedAt()) {
1470 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1471 if (!InlinedAtDL.isUnknown()) {
1472 OS << " @[";
1473 printDebugLoc(InlinedAtDL, OS, Ctx);
1474 OS << "]";
1475 }
1476 }
1477}
1478
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +00001479template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
1480 assert(isDescriptorRef(V) &&
1481 "DIDescriptorRef should be a MDString or MDNode");
1482}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001483template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001484 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1485}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001486template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001487 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1488}
1489
Bill Wendling523bea82013-11-08 08:13:15 +00001490template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +00001491DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
1492 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1493}
1494template <>
Bill Wendling523bea82013-11-08 08:13:15 +00001495DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001496 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001497}
Bill Wendling523bea82013-11-08 08:13:15 +00001498template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001499 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001500}
Manman Rencb14bbc2013-11-22 22:06:31 +00001501
Manman Rencb14bbc2013-11-22 22:06:31 +00001502bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001503 bool Changed = false;
1504
1505 // Remove all of the calls to the debugger intrinsics, and remove them from
1506 // the module.
1507 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1508 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001509 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001510 CI->eraseFromParent();
1511 }
1512 Declare->eraseFromParent();
1513 Changed = true;
1514 }
1515
1516 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1517 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001518 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001519 CI->eraseFromParent();
1520 }
1521 DbgVal->eraseFromParent();
1522 Changed = true;
1523 }
1524
1525 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1526 NME = M.named_metadata_end(); NMI != NME;) {
1527 NamedMDNode *NMD = NMI;
1528 ++NMI;
1529 if (NMD->getName().startswith("llvm.dbg.")) {
1530 NMD->eraseFromParent();
1531 Changed = true;
1532 }
1533 }
1534
1535 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1536 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1537 ++FI)
1538 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1539 ++BI) {
1540 if (!BI->getDebugLoc().isUnknown()) {
1541 Changed = true;
1542 BI->setDebugLoc(DebugLoc());
1543 }
1544 }
1545
1546 return Changed;
1547}
Manman Ren8b4306c2013-12-02 21:29:56 +00001548
Manman Renbd4daf82013-12-03 00:12:14 +00001549unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +00001550 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001551 M.getModuleFlag("Debug Info Version")))
1552 return Val->getZExtValue();
1553 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001554}
David Blaikie6876b3b2014-07-01 20:05:26 +00001555
David Blaikiea8c35092014-07-02 18:30:05 +00001556llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1557llvm::makeSubprogramMap(const Module &M) {
1558 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001559
1560 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1561 if (!CU_Nodes)
1562 return R;
1563
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001564 for (MDNode *N : CU_Nodes->operands()) {
1565 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001566 DIArray SPs = CUNode.getSubprograms();
1567 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1568 DISubprogram SP(SPs.getElement(i));
1569 if (Function *F = SP.getFunction())
1570 R.insert(std::make_pair(F, SP));
1571 }
1572 }
1573 return R;
1574}