blob: 88f7e33b7dc1b302e2c360295d86b09873f65e47 [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 Prantl87b7eb92014-10-01 18:55:02 +0000150bool DIExpression::isVariablePiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000151 unsigned N = getNumElements();
152 return N >=3 && getElement(N-3) == dwarf::DW_OP_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000153}
154
155uint64_t DIExpression::getPieceOffset() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000156 assert(isVariablePiece() && "not a piece");
157 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000158}
159
160uint64_t DIExpression::getPieceSize() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000161 assert(isVariablePiece() && "not a piece");
162 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 {
257 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
258 getTag() == dwarf::DW_TAG_constant);
259}
260
Bill Wendling523bea82013-11-08 08:13:15 +0000261bool DIDescriptor::isScope() const {
262 if (!DbgNode)
263 return false;
264 switch (getTag()) {
265 case dwarf::DW_TAG_compile_unit:
266 case dwarf::DW_TAG_lexical_block:
267 case dwarf::DW_TAG_subprogram:
268 case dwarf::DW_TAG_namespace:
269 case dwarf::DW_TAG_file_type:
270 return true;
271 default:
272 break;
273 }
274 return isType();
275}
276
Bill Wendling523bea82013-11-08 08:13:15 +0000277bool DIDescriptor::isTemplateTypeParameter() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
279}
280
Bill Wendling523bea82013-11-08 08:13:15 +0000281bool DIDescriptor::isTemplateValueParameter() const {
282 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
283 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
284 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
285}
286
Bill Wendling523bea82013-11-08 08:13:15 +0000287bool DIDescriptor::isCompileUnit() const {
288 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
289}
290
Bill Wendling523bea82013-11-08 08:13:15 +0000291bool DIDescriptor::isFile() const {
292 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
293}
294
Bill Wendling523bea82013-11-08 08:13:15 +0000295bool DIDescriptor::isNameSpace() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
297}
298
Bill Wendling523bea82013-11-08 08:13:15 +0000299bool DIDescriptor::isLexicalBlockFile() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000301 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000302}
303
Bill Wendling523bea82013-11-08 08:13:15 +0000304bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000305 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
306 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000307 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000308 DbgNode->getNumOperands() == 3 &&
309 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000310}
311
Bill Wendling523bea82013-11-08 08:13:15 +0000312bool DIDescriptor::isSubrange() const {
313 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
314}
315
Bill Wendling523bea82013-11-08 08:13:15 +0000316bool DIDescriptor::isEnumerator() const {
317 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
318}
319
Bill Wendling523bea82013-11-08 08:13:15 +0000320bool DIDescriptor::isObjCProperty() const {
321 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
322}
323
Bill Wendling523bea82013-11-08 08:13:15 +0000324bool DIDescriptor::isImportedEntity() const {
325 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
326 getTag() == dwarf::DW_TAG_imported_declaration);
327}
328
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000329bool DIDescriptor::isExpression() const {
330 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
331}
332
Bill Wendling523bea82013-11-08 08:13:15 +0000333//===----------------------------------------------------------------------===//
334// Simple Descriptor Constructors and other Methods
335//===----------------------------------------------------------------------===//
336
Frederic Riss36acf0f2014-09-15 07:50:36 +0000337void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000338
339 assert(DbgNode && "Trying to replace an unverified type!");
340
341 // Since we use a TrackingVH for the node, its easy for clients to manufacture
342 // legitimate situations where they want to replaceAllUsesWith() on something
343 // which, due to uniquing, has merged with the source. We shield clients from
344 // this detail by allowing a value to be replaced with replaceAllUsesWith()
345 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000346 const MDNode *DN = D;
347 if (DbgNode == DN) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000348 SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
David Blaikied3f094a2014-05-06 03:41:57 +0000349 for (size_t i = 0; i != Ops.size(); ++i)
350 Ops[i] = DbgNode->getOperand(i);
351 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000352 }
David Blaikied3f094a2014-05-06 03:41:57 +0000353
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000354 assert(DbgNode->isTemporary() && "Expected temporary node");
355 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000356 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
David Blaikied3f094a2014-05-06 03:41:57 +0000357 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000358 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000359}
360
Frederic Riss36acf0f2014-09-15 07:50:36 +0000361void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000362 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000363 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000364 assert(DbgNode->isTemporary() && "Expected temporary node");
365 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000366 Node->replaceAllUsesWith(D);
David Blaikied3f094a2014-05-06 03:41:57 +0000367 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000368}
369
Bill Wendling523bea82013-11-08 08:13:15 +0000370bool DICompileUnit::Verify() const {
371 if (!isCompileUnit())
372 return false;
373
374 // Don't bother verifying the compilation directory or producer string
375 // as those could be empty.
376 if (getFilename().empty())
377 return false;
378
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000379 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000380}
381
Bill Wendling523bea82013-11-08 08:13:15 +0000382bool DIObjCProperty::Verify() const {
383 if (!isObjCProperty())
384 return false;
385
386 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000387 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000388}
389
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000390/// \brief Check if a field at position Elt of a MDNode is a MDNode.
391///
Bill Wendling523bea82013-11-08 08:13:15 +0000392/// We currently allow an empty string and an integer.
393/// But we don't allow a non-empty string in a MDNode field.
394static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
395 // FIXME: This function should return true, if the field is null or the field
396 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000397 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000398 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
399 return false;
400 return true;
401}
402
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000403/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000404static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000405 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000406 return !Fld || isa<MDString>(Fld);
407}
408
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000409/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000410static bool isTypeRef(const Metadata *MD) {
411 if (!MD)
412 return true;
413 if (auto *S = dyn_cast<MDString>(MD))
414 return !S->getString().empty();
415 if (auto *N = dyn_cast<MDNode>(MD))
416 return DIType(N).isType();
417 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000418}
419
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000420/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000421static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000422 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000423}
424
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000425/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000426static bool isScopeRef(const Metadata *MD) {
427 if (!MD)
428 return true;
429 if (auto *S = dyn_cast<MDString>(MD))
430 return !S->getString().empty();
431 return isa<MDNode>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000432}
433
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000434/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000435static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000436 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000437}
438
Bill Wendling523bea82013-11-08 08:13:15 +0000439bool DIType::Verify() const {
440 if (!isType())
441 return false;
442 // Make sure Context @ field 2 is MDNode.
443 if (!fieldIsScopeRef(DbgNode, 2))
444 return false;
445
446 // FIXME: Sink this into the various subclass verifies.
447 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000448 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000449 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
450 Tag != dwarf::DW_TAG_ptr_to_member_type &&
451 Tag != dwarf::DW_TAG_reference_type &&
452 Tag != dwarf::DW_TAG_rvalue_reference_type &&
453 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
454 Tag != dwarf::DW_TAG_enumeration_type &&
455 Tag != dwarf::DW_TAG_subroutine_type &&
456 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
457 getFilename().empty())
458 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000459
Bill Wendling523bea82013-11-08 08:13:15 +0000460 // DIType is abstract, it should be a BasicType, a DerivedType or
461 // a CompositeType.
462 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000463 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000464 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000465 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000466 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000467 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000468 else
469 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000470}
471
Bill Wendling523bea82013-11-08 08:13:15 +0000472bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000473 return isBasicType() && DbgNode->getNumOperands() == 3 &&
474 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000475}
476
Bill Wendling523bea82013-11-08 08:13:15 +0000477bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000478 // Make sure DerivedFrom @ field 3 is TypeRef.
479 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000480 return false;
481 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000482 // Make sure ClassType @ field 4 is a TypeRef.
483 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000484 return false;
485
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000486 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
487 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
488 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000489}
490
Bill Wendling523bea82013-11-08 08:13:15 +0000491bool DICompositeType::Verify() const {
492 if (!isCompositeType())
493 return false;
494
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000495 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
496 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000497 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000498 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000499 return false;
500
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000501 // Make sure the type identifier at field 7 is MDString, it can be null.
502 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000503 return false;
504
Adrian Prantl99c7af22013-12-18 21:48:19 +0000505 // A subroutine type can't be both & and &&.
506 if (isLValueReference() && isRValueReference())
507 return false;
508
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000509 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000510}
511
Bill Wendling523bea82013-11-08 08:13:15 +0000512bool DISubprogram::Verify() const {
513 if (!isSubprogram())
514 return false;
515
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000516 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000517 if (!fieldIsScopeRef(DbgNode, 2))
518 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000519 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000520 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000521 // Containing type @ field 4.
522 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000523 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000524
525 // A subprogram can't be both & and &&.
526 if (isLValueReference() && isRValueReference())
527 return false;
528
David Blaikie3dfe4782014-10-14 18:22:52 +0000529 // If a DISubprogram has an llvm::Function*, then scope chains from all
530 // instructions within the function should lead to this DISubprogram.
531 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000532 for (auto &BB : *F) {
533 for (auto &I : BB) {
534 DebugLoc DL = I.getDebugLoc();
535 if (DL.isUnknown())
536 continue;
537
538 MDNode *Scope = nullptr;
539 MDNode *IA = nullptr;
540 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000541 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000542 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000543 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000544 if (!Scope)
545 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000546 assert(!IA);
547 while (!DIDescriptor(Scope).isSubprogram()) {
548 DILexicalBlockFile D(Scope);
549 Scope = D.isLexicalBlockFile()
550 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000551 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000552 if (!Scope)
553 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000554 }
555 if (!DISubprogram(Scope).describes(F))
556 return false;
557 }
558 }
559 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000560 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000561}
562
Bill Wendling523bea82013-11-08 08:13:15 +0000563bool DIGlobalVariable::Verify() const {
564 if (!isGlobalVariable())
565 return false;
566
567 if (getDisplayName().empty())
568 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000569 // Make sure context @ field 1 is an MDNode.
570 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000571 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000572 // Make sure that type @ field 3 is a DITypeRef.
573 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000574 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000575 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
576 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000577 return false;
578
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000579 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000580}
581
Bill Wendling523bea82013-11-08 08:13:15 +0000582bool DIVariable::Verify() const {
583 if (!isVariable())
584 return false;
585
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000586 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000587 if (!fieldIsMDNode(DbgNode, 1))
588 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000589 // Make sure that type @ field 3 is a DITypeRef.
590 if (!fieldIsTypeRef(DbgNode, 3))
591 return false;
592
593 // Check the number of header fields, which is common between complex and
594 // simple variables.
595 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000596 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000597
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000598 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000599 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000600 return true;
601
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000602 // Variable with an inline location.
603 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000604}
605
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000606bool DIExpression::Verify() const {
607 // Empty DIExpressions may be represented as a nullptr.
608 if (!DbgNode)
609 return true;
610
Adrian Prantl9260cca2015-01-22 00:00:52 +0000611 if (!(isExpression() && DbgNode->getNumOperands() == 1))
612 return false;
613
Adrian Prantl70f2a732015-01-23 23:40:47 +0000614 for (auto Op : *this)
615 switch (Op) {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000616 case DW_OP_piece:
Adrian Prantl9260cca2015-01-22 00:00:52 +0000617 // Must be the last element of the expression.
Adrian Prantl70f2a732015-01-23 23:40:47 +0000618 return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000619 case DW_OP_plus:
Adrian Prantl70f2a732015-01-23 23:40:47 +0000620 if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000621 return false;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000622 break;
Adrian Prantl9260cca2015-01-22 00:00:52 +0000623 case DW_OP_deref:
624 break;
625 default:
626 // Other operators are not yet supported by the backend.
627 return false;
628 }
629 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000630}
631
Bill Wendling523bea82013-11-08 08:13:15 +0000632bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000633 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000634}
635
Bill Wendling523bea82013-11-08 08:13:15 +0000636bool DINameSpace::Verify() const {
637 if (!isNameSpace())
638 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000639 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000640}
641
Bill Wendling523bea82013-11-08 08:13:15 +0000642MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
643
Bill Wendling523bea82013-11-08 08:13:15 +0000644bool DIFile::Verify() const {
645 return isFile() && DbgNode->getNumOperands() == 2;
646}
647
Bill Wendling523bea82013-11-08 08:13:15 +0000648bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000649 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
650 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000651}
652
Bill Wendling523bea82013-11-08 08:13:15 +0000653bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000654 return isSubrange() && DbgNode->getNumOperands() == 1 &&
655 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000656}
657
Bill Wendling523bea82013-11-08 08:13:15 +0000658bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000659 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
660 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000661}
662
Bill Wendling523bea82013-11-08 08:13:15 +0000663bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000664 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
665 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000666}
667
Bill Wendling523bea82013-11-08 08:13:15 +0000668bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000669 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
670 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000671}
672
Bill Wendling523bea82013-11-08 08:13:15 +0000673bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000674 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
675 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000676}
677
Bill Wendling523bea82013-11-08 08:13:15 +0000678bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000679 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
680 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000681}
682
Bill Wendling523bea82013-11-08 08:13:15 +0000683MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000684 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000685}
686
687MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000688 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000689}
690
691#ifndef NDEBUG
692static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
693 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
694 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000695 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000696 continue;
697 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
698 bool found = false;
699 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000700 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000701 assert(found && "Losing a member during member list replacement");
702 }
703}
704#endif
705
Manman Ren1a125c92014-07-28 19:33:20 +0000706void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000707 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000708 if (Elements) {
709#ifndef NDEBUG
710 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000711 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000712 VerifySubsetOf(El, Elements);
713#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000714 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000715 }
716 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000717 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000718 DbgNode = N;
719}
720
Bill Wendling523bea82013-11-08 08:13:15 +0000721DIScopeRef DIScope::getRef() const {
722 if (!isCompositeType())
723 return DIScopeRef(*this);
724 DICompositeType DTy(DbgNode);
725 if (!DTy.getIdentifier())
726 return DIScopeRef(*this);
727 return DIScopeRef(DTy.getIdentifier());
728}
729
Bill Wendling523bea82013-11-08 08:13:15 +0000730void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000731 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000732 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000733 DbgNode = N;
734}
735
Bill Wendling523bea82013-11-08 08:13:15 +0000736bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
737 assert(CurFn && "Invalid function");
738 if (!getContext().isSubprogram())
739 return false;
740 // This variable is not inlined function argument if its scope
741 // does not describe current function.
742 return !DISubprogram(getContext()).describes(CurFn);
743}
744
Bill Wendling523bea82013-11-08 08:13:15 +0000745bool DISubprogram::describes(const Function *F) {
746 assert(F && "Invalid function");
747 if (F == getFunction())
748 return true;
749 StringRef Name = getLinkageName();
750 if (Name.empty())
751 Name = getName();
752 if (F->getName() == Name)
753 return true;
754 return false;
755}
756
Bill Wendling523bea82013-11-08 08:13:15 +0000757MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000758 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000759}
760
761DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000762 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000763}
764
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000765Metadata *DITemplateValueParameter::getValue() const {
766 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000767}
768
Bill Wendling523bea82013-11-08 08:13:15 +0000769DIScopeRef DIScope::getContext() const {
770
771 if (isType())
772 return DIType(DbgNode).getContext();
773
774 if (isSubprogram())
775 return DIScopeRef(DISubprogram(DbgNode).getContext());
776
777 if (isLexicalBlock())
778 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
779
780 if (isLexicalBlockFile())
781 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
782
783 if (isNameSpace())
784 return DIScopeRef(DINameSpace(DbgNode).getContext());
785
786 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000787 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000788}
789
Bill Wendling523bea82013-11-08 08:13:15 +0000790StringRef DIScope::getName() const {
791 if (isType())
792 return DIType(DbgNode).getName();
793 if (isSubprogram())
794 return DISubprogram(DbgNode).getName();
795 if (isNameSpace())
796 return DINameSpace(DbgNode).getName();
797 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
798 isCompileUnit()) &&
799 "Unhandled type of scope.");
800 return StringRef();
801}
802
803StringRef DIScope::getFilename() const {
804 if (!DbgNode)
805 return StringRef();
806 return ::getStringField(getNodeField(DbgNode, 1), 0);
807}
808
809StringRef DIScope::getDirectory() const {
810 if (!DbgNode)
811 return StringRef();
812 return ::getStringField(getNodeField(DbgNode, 1), 1);
813}
814
815DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000816 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000817 return DIArray();
818
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000819 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000820}
821
822DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000823 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000824 return DIArray();
825
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000826 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000827}
828
829DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000830 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000831 return DIArray();
832
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000833 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000834}
835
836DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000837 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000838 return DIArray();
839
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000840 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000841}
842
843DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000844 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000845 return DIArray();
846
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000847 return DIArray(getNodeField(DbgNode, 6));
848}
849
850void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
851 assert(Verify() && "Expected compile unit");
852 if (Subprograms == getSubprograms())
853 return;
854
855 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
856}
857
858void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
859 assert(Verify() && "Expected compile unit");
860 if (GlobalVariables == getGlobalVariables())
861 return;
862
863 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000864}
865
Diego Novillof5041ce2014-03-03 20:06:11 +0000866DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000867 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000868 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000869 assert(NewScope && "Expected valid scope");
870
871 const auto *Old = cast<MDLocation>(DbgNode);
872 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
873 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000874}
875
Diego Novillof5041ce2014-03-03 20:06:11 +0000876unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
877 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
878 return ++Ctx.pImpl->DiscriminatorTable[Key];
879}
880
Bill Wendling523bea82013-11-08 08:13:15 +0000881DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
882 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000883 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
884 if (!InlinedScope)
885 return cleanseInlinedVariable(DV, VMContext);
886
887 // Insert inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000888 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000889 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
890 Elts.push_back(DV->getOperand(I));
891 Elts.push_back(InlinedScope);
892
893 DIVariable Inlined(MDNode::get(VMContext, Elts));
894 assert(Inlined.Verify() && "Expected to create a DIVariable");
895 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000896}
897
Bill Wendling523bea82013-11-08 08:13:15 +0000898DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000899 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
900 if (!DIVariable(DV).getInlinedAt())
901 return DIVariable(DV);
902
903 // Remove inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000904 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000905 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
906 Elts.push_back(DV->getOperand(I));
907
908 DIVariable Cleansed(MDNode::get(VMContext, Elts));
909 assert(Cleansed.Verify() && "Expected to create a DIVariable");
910 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000911}
912
Bill Wendling523bea82013-11-08 08:13:15 +0000913DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
914 DIDescriptor D(Scope);
915 if (D.isSubprogram())
916 return DISubprogram(Scope);
917
918 if (D.isLexicalBlockFile())
919 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
920
921 if (D.isLexicalBlock())
922 return getDISubprogram(DILexicalBlock(Scope).getContext());
923
924 return DISubprogram();
925}
926
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000927DISubprogram llvm::getDISubprogram(const Function *F) {
928 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000929 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000930 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
931 return !Inst.getDebugLoc().isUnknown();
932 });
933 if (Inst == BB.end())
934 continue;
935 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000936 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000937 DISubprogram Subprogram = getDISubprogram(Scope);
938 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000939 }
940
941 return DISubprogram();
942}
943
Bill Wendling523bea82013-11-08 08:13:15 +0000944DICompositeType llvm::getDICompositeType(DIType T) {
945 if (T.isCompositeType())
946 return DICompositeType(T);
947
948 if (T.isDerivedType()) {
949 // This function is currently used by dragonegg and dragonegg does
950 // not generate identifier for types, so using an empty map to resolve
951 // DerivedFrom should be fine.
952 DITypeIdentifierMap EmptyMap;
953 return getDICompositeType(
954 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
955 }
956
957 return DICompositeType();
958}
959
Bill Wendling523bea82013-11-08 08:13:15 +0000960DITypeIdentifierMap
961llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
962 DITypeIdentifierMap Map;
963 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000964 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000965 DIArray Retain = CU.getRetainedTypes();
966 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
967 if (!Retain.getElement(Ti).isCompositeType())
968 continue;
969 DICompositeType Ty(Retain.getElement(Ti));
970 if (MDString *TypeId = Ty.getIdentifier()) {
971 // Definition has priority over declaration.
972 // Try to insert (TypeId, Ty) to Map.
973 std::pair<DITypeIdentifierMap::iterator, bool> P =
974 Map.insert(std::make_pair(TypeId, Ty));
975 // If TypeId already exists in Map and this is a definition, replace
976 // whatever we had (declaration or definition) with the definition.
977 if (!P.second && !Ty.isForwardDecl())
978 P.first->second = Ty;
979 }
980 }
981 }
982 return Map;
983}
984
985//===----------------------------------------------------------------------===//
986// DebugInfoFinder implementations.
987//===----------------------------------------------------------------------===//
988
989void DebugInfoFinder::reset() {
990 CUs.clear();
991 SPs.clear();
992 GVs.clear();
993 TYs.clear();
994 Scopes.clear();
995 NodesSeen.clear();
996 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000997 TypeMapInitialized = false;
998}
999
Manman Renb46e5502013-11-17 19:35:03 +00001000void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001001 if (!TypeMapInitialized)
1002 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1003 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1004 TypeMapInitialized = true;
1005 }
Bill Wendling523bea82013-11-08 08:13:15 +00001006}
1007
Bill Wendling523bea82013-11-08 08:13:15 +00001008void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001009 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001010 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001011 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001012 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001013 addCompileUnit(CU);
1014 DIArray GVs = CU.getGlobalVariables();
1015 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1016 DIGlobalVariable DIG(GVs.getElement(i));
1017 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +00001018 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001019 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001020 }
1021 }
1022 DIArray SPs = CU.getSubprograms();
1023 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1024 processSubprogram(DISubprogram(SPs.getElement(i)));
1025 DIArray EnumTypes = CU.getEnumTypes();
1026 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1027 processType(DIType(EnumTypes.getElement(i)));
1028 DIArray RetainedTypes = CU.getRetainedTypes();
1029 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1030 processType(DIType(RetainedTypes.getElement(i)));
1031 DIArray Imports = CU.getImportedEntities();
1032 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1033 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001034 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001035 if (Entity.isType())
1036 processType(DIType(Entity));
1037 else if (Entity.isSubprogram())
1038 processSubprogram(DISubprogram(Entity));
1039 else if (Entity.isNameSpace())
1040 processScope(DINameSpace(Entity).getContext());
1041 }
1042 }
1043 }
1044}
1045
Manman Ren2085ccc2013-11-17 18:42:37 +00001046void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001047 if (!Loc)
1048 return;
Manman Renb46e5502013-11-17 19:35:03 +00001049 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001050 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001051 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001052}
1053
Bill Wendling523bea82013-11-08 08:13:15 +00001054void DebugInfoFinder::processType(DIType DT) {
1055 if (!addType(DT))
1056 return;
1057 processScope(DT.getContext().resolve(TypeIdentifierMap));
1058 if (DT.isCompositeType()) {
1059 DICompositeType DCT(DT);
1060 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001061 if (DT.isSubroutineType()) {
1062 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1063 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1064 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1065 return;
1066 }
Manman Renab8ffba2014-07-28 19:14:13 +00001067 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001068 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1069 DIDescriptor D = DA.getElement(i);
1070 if (D.isType())
1071 processType(DIType(D));
1072 else if (D.isSubprogram())
1073 processSubprogram(DISubprogram(D));
1074 }
1075 } else if (DT.isDerivedType()) {
1076 DIDerivedType DDT(DT);
1077 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1078 }
1079}
1080
1081void DebugInfoFinder::processScope(DIScope Scope) {
1082 if (Scope.isType()) {
1083 DIType Ty(Scope);
1084 processType(Ty);
1085 return;
1086 }
1087 if (Scope.isCompileUnit()) {
1088 addCompileUnit(DICompileUnit(Scope));
1089 return;
1090 }
1091 if (Scope.isSubprogram()) {
1092 processSubprogram(DISubprogram(Scope));
1093 return;
1094 }
1095 if (!addScope(Scope))
1096 return;
1097 if (Scope.isLexicalBlock()) {
1098 DILexicalBlock LB(Scope);
1099 processScope(LB.getContext());
1100 } else if (Scope.isLexicalBlockFile()) {
1101 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1102 processScope(LBF.getScope());
1103 } else if (Scope.isNameSpace()) {
1104 DINameSpace NS(Scope);
1105 processScope(NS.getContext());
1106 }
1107}
1108
Bill Wendling523bea82013-11-08 08:13:15 +00001109void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1110 if (!addSubprogram(SP))
1111 return;
1112 processScope(SP.getContext().resolve(TypeIdentifierMap));
1113 processType(SP.getType());
1114 DIArray TParams = SP.getTemplateParams();
1115 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1116 DIDescriptor Element = TParams.getElement(I);
1117 if (Element.isTemplateTypeParameter()) {
1118 DITemplateTypeParameter TType(Element);
1119 processScope(TType.getContext().resolve(TypeIdentifierMap));
1120 processType(TType.getType().resolve(TypeIdentifierMap));
1121 } else if (Element.isTemplateValueParameter()) {
1122 DITemplateValueParameter TVal(Element);
1123 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1124 processType(TVal.getType().resolve(TypeIdentifierMap));
1125 }
1126 }
1127}
1128
Manman Ren2085ccc2013-11-17 18:42:37 +00001129void DebugInfoFinder::processDeclare(const Module &M,
1130 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001131 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1132 if (!N)
1133 return;
Manman Renb46e5502013-11-17 19:35:03 +00001134 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001135
1136 DIDescriptor DV(N);
1137 if (!DV.isVariable())
1138 return;
1139
David Blaikie70573dc2014-11-19 07:49:26 +00001140 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001141 return;
1142 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001143 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001144}
1145
Manman Ren2085ccc2013-11-17 18:42:37 +00001146void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001147 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1148 if (!N)
1149 return;
Manman Renb46e5502013-11-17 19:35:03 +00001150 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001151
1152 DIDescriptor DV(N);
1153 if (!DV.isVariable())
1154 return;
1155
David Blaikie70573dc2014-11-19 07:49:26 +00001156 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001157 return;
1158 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001159 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001160}
1161
Bill Wendling523bea82013-11-08 08:13:15 +00001162bool DebugInfoFinder::addType(DIType DT) {
1163 if (!DT)
1164 return false;
1165
David Blaikie70573dc2014-11-19 07:49:26 +00001166 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001167 return false;
1168
1169 TYs.push_back(DT);
1170 return true;
1171}
1172
Bill Wendling523bea82013-11-08 08:13:15 +00001173bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1174 if (!CU)
1175 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001176 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001177 return false;
1178
1179 CUs.push_back(CU);
1180 return true;
1181}
1182
Bill Wendling523bea82013-11-08 08:13:15 +00001183bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1184 if (!DIG)
1185 return false;
1186
David Blaikie70573dc2014-11-19 07:49:26 +00001187 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001188 return false;
1189
1190 GVs.push_back(DIG);
1191 return true;
1192}
1193
Bill Wendling523bea82013-11-08 08:13:15 +00001194bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1195 if (!SP)
1196 return false;
1197
David Blaikie70573dc2014-11-19 07:49:26 +00001198 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001199 return false;
1200
1201 SPs.push_back(SP);
1202 return true;
1203}
1204
1205bool DebugInfoFinder::addScope(DIScope Scope) {
1206 if (!Scope)
1207 return false;
1208 // FIXME: Ocaml binding generates a scope with no content, we treat it
1209 // as null for now.
1210 if (Scope->getNumOperands() == 0)
1211 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001212 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001213 return false;
1214 Scopes.push_back(Scope);
1215 return true;
1216}
1217
1218//===----------------------------------------------------------------------===//
1219// DIDescriptor: dump routines for all descriptors.
1220//===----------------------------------------------------------------------===//
1221
Bill Wendling523bea82013-11-08 08:13:15 +00001222void DIDescriptor::dump() const {
1223 print(dbgs());
1224 dbgs() << '\n';
1225}
1226
Bill Wendling523bea82013-11-08 08:13:15 +00001227void DIDescriptor::print(raw_ostream &OS) const {
1228 if (!DbgNode)
1229 return;
1230
1231 if (const char *Tag = dwarf::TagString(getTag()))
1232 OS << "[ " << Tag << " ]";
1233
1234 if (this->isSubrange()) {
1235 DISubrange(DbgNode).printInternal(OS);
1236 } else if (this->isCompileUnit()) {
1237 DICompileUnit(DbgNode).printInternal(OS);
1238 } else if (this->isFile()) {
1239 DIFile(DbgNode).printInternal(OS);
1240 } else if (this->isEnumerator()) {
1241 DIEnumerator(DbgNode).printInternal(OS);
1242 } else if (this->isBasicType()) {
1243 DIType(DbgNode).printInternal(OS);
1244 } else if (this->isDerivedType()) {
1245 DIDerivedType(DbgNode).printInternal(OS);
1246 } else if (this->isCompositeType()) {
1247 DICompositeType(DbgNode).printInternal(OS);
1248 } else if (this->isSubprogram()) {
1249 DISubprogram(DbgNode).printInternal(OS);
1250 } else if (this->isGlobalVariable()) {
1251 DIGlobalVariable(DbgNode).printInternal(OS);
1252 } else if (this->isVariable()) {
1253 DIVariable(DbgNode).printInternal(OS);
1254 } else if (this->isObjCProperty()) {
1255 DIObjCProperty(DbgNode).printInternal(OS);
1256 } else if (this->isNameSpace()) {
1257 DINameSpace(DbgNode).printInternal(OS);
1258 } else if (this->isScope()) {
1259 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001260 } else if (this->isExpression()) {
1261 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001262 }
1263}
1264
1265void DISubrange::printInternal(raw_ostream &OS) const {
1266 int64_t Count = getCount();
1267 if (Count != -1)
1268 OS << " [" << getLo() << ", " << Count - 1 << ']';
1269 else
1270 OS << " [unbounded]";
1271}
1272
1273void DIScope::printInternal(raw_ostream &OS) const {
1274 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1275}
1276
1277void DICompileUnit::printInternal(raw_ostream &OS) const {
1278 DIScope::printInternal(OS);
1279 OS << " [";
1280 unsigned Lang = getLanguage();
1281 if (const char *LangStr = dwarf::LanguageString(Lang))
1282 OS << LangStr;
1283 else
1284 (OS << "lang 0x").write_hex(Lang);
1285 OS << ']';
1286}
1287
1288void DIEnumerator::printInternal(raw_ostream &OS) const {
1289 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1290}
1291
1292void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001293 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001294 return;
1295
1296 StringRef Res = getName();
1297 if (!Res.empty())
1298 OS << " [" << Res << "]";
1299
1300 // TODO: Print context?
1301
1302 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1303 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1304 if (isBasicType())
1305 if (const char *Enc =
1306 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1307 OS << ", enc " << Enc;
1308 OS << "]";
1309
1310 if (isPrivate())
1311 OS << " [private]";
1312 else if (isProtected())
1313 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001314 else if (isPublic())
1315 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001316
1317 if (isArtificial())
1318 OS << " [artificial]";
1319
1320 if (isForwardDecl())
1321 OS << " [decl]";
1322 else if (getTag() == dwarf::DW_TAG_structure_type ||
1323 getTag() == dwarf::DW_TAG_union_type ||
1324 getTag() == dwarf::DW_TAG_enumeration_type ||
1325 getTag() == dwarf::DW_TAG_class_type)
1326 OS << " [def]";
1327 if (isVector())
1328 OS << " [vector]";
1329 if (isStaticMember())
1330 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001331
1332 if (isLValueReference())
1333 OS << " [reference]";
1334
1335 if (isRValueReference())
1336 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001337}
1338
1339void DIDerivedType::printInternal(raw_ostream &OS) const {
1340 DIType::printInternal(OS);
1341 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1342}
1343
1344void DICompositeType::printInternal(raw_ostream &OS) const {
1345 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001346 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001347 OS << " [" << A.getNumElements() << " elements]";
1348}
1349
1350void DINameSpace::printInternal(raw_ostream &OS) const {
1351 StringRef Name = getName();
1352 if (!Name.empty())
1353 OS << " [" << Name << ']';
1354
1355 OS << " [line " << getLineNumber() << ']';
1356}
1357
1358void DISubprogram::printInternal(raw_ostream &OS) const {
1359 // TODO : Print context
1360 OS << " [line " << getLineNumber() << ']';
1361
1362 if (isLocalToUnit())
1363 OS << " [local]";
1364
1365 if (isDefinition())
1366 OS << " [def]";
1367
1368 if (getScopeLineNumber() != getLineNumber())
1369 OS << " [scope " << getScopeLineNumber() << "]";
1370
1371 if (isPrivate())
1372 OS << " [private]";
1373 else if (isProtected())
1374 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001375 else if (isPublic())
1376 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001377
Adrian Prantl99c7af22013-12-18 21:48:19 +00001378 if (isLValueReference())
1379 OS << " [reference]";
1380
1381 if (isRValueReference())
1382 OS << " [rvalue reference]";
1383
Bill Wendling523bea82013-11-08 08:13:15 +00001384 StringRef Res = getName();
1385 if (!Res.empty())
1386 OS << " [" << Res << ']';
1387}
1388
1389void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1390 StringRef Res = getName();
1391 if (!Res.empty())
1392 OS << " [" << Res << ']';
1393
1394 OS << " [line " << getLineNumber() << ']';
1395
1396 // TODO : Print context
1397
1398 if (isLocalToUnit())
1399 OS << " [local]";
1400
1401 if (isDefinition())
1402 OS << " [def]";
1403}
1404
1405void DIVariable::printInternal(raw_ostream &OS) const {
1406 StringRef Res = getName();
1407 if (!Res.empty())
1408 OS << " [" << Res << ']';
1409
1410 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001411}
Adrian Prantlb1416832014-08-01 22:11:58 +00001412
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001413void DIExpression::printInternal(raw_ostream &OS) const {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001414 for (auto Op : *this) {
1415 OS << " [" << OperationEncodingString(Op);
1416 switch (Op) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001417 case DW_OP_plus: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001418 OS << " " << Op.getArg(1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001419 break;
1420 }
1421 case DW_OP_piece: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001422 OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001423 break;
1424 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001425 case DW_OP_deref:
1426 // No arguments.
1427 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001428 default:
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001429 llvm_unreachable("unhandled operation");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001430 }
1431 OS << "]";
1432 }
Bill Wendling523bea82013-11-08 08:13:15 +00001433}
1434
1435void DIObjCProperty::printInternal(raw_ostream &OS) const {
1436 StringRef Name = getObjCPropertyName();
1437 if (!Name.empty())
1438 OS << " [" << Name << ']';
1439
1440 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1441 << ']';
1442}
1443
1444static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1445 const LLVMContext &Ctx) {
1446 if (!DL.isUnknown()) { // Print source line info.
1447 DIScope Scope(DL.getScope(Ctx));
1448 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1449 // Omit the directory, because it's likely to be long and uninteresting.
1450 CommentOS << Scope.getFilename();
1451 CommentOS << ':' << DL.getLine();
1452 if (DL.getCol() != 0)
1453 CommentOS << ':' << DL.getCol();
1454 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1455 if (!InlinedAtDL.isUnknown()) {
1456 CommentOS << " @[ ";
1457 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1458 CommentOS << " ]";
1459 }
1460 }
1461}
1462
1463void DIVariable::printExtendedName(raw_ostream &OS) const {
1464 const LLVMContext &Ctx = DbgNode->getContext();
1465 StringRef Res = getName();
1466 if (!Res.empty())
1467 OS << Res << "," << getLineNumber();
1468 if (MDNode *InlinedAt = getInlinedAt()) {
1469 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1470 if (!InlinedAtDL.isUnknown()) {
1471 OS << " @[";
1472 printDebugLoc(InlinedAtDL, OS, Ctx);
1473 OS << "]";
1474 }
1475 }
1476}
1477
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001478template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001479 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1480}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001481template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001482 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1483}
1484
Bill Wendling523bea82013-11-08 08:13:15 +00001485template <>
1486DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001487 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001488}
Bill Wendling523bea82013-11-08 08:13:15 +00001489template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001490 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001491}
Manman Rencb14bbc2013-11-22 22:06:31 +00001492
Manman Rencb14bbc2013-11-22 22:06:31 +00001493bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001494 bool Changed = false;
1495
1496 // Remove all of the calls to the debugger intrinsics, and remove them from
1497 // the module.
1498 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1499 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001500 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001501 CI->eraseFromParent();
1502 }
1503 Declare->eraseFromParent();
1504 Changed = true;
1505 }
1506
1507 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1508 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001509 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001510 CI->eraseFromParent();
1511 }
1512 DbgVal->eraseFromParent();
1513 Changed = true;
1514 }
1515
1516 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1517 NME = M.named_metadata_end(); NMI != NME;) {
1518 NamedMDNode *NMD = NMI;
1519 ++NMI;
1520 if (NMD->getName().startswith("llvm.dbg.")) {
1521 NMD->eraseFromParent();
1522 Changed = true;
1523 }
1524 }
1525
1526 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1527 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1528 ++FI)
1529 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1530 ++BI) {
1531 if (!BI->getDebugLoc().isUnknown()) {
1532 Changed = true;
1533 BI->setDebugLoc(DebugLoc());
1534 }
1535 }
1536
1537 return Changed;
1538}
Manman Ren8b4306c2013-12-02 21:29:56 +00001539
Manman Renbd4daf82013-12-03 00:12:14 +00001540unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001541 if (auto *Val = mdconst::extract_or_null<ConstantInt>(
1542 M.getModuleFlag("Debug Info Version")))
1543 return Val->getZExtValue();
1544 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001545}
David Blaikie6876b3b2014-07-01 20:05:26 +00001546
David Blaikiea8c35092014-07-02 18:30:05 +00001547llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1548llvm::makeSubprogramMap(const Module &M) {
1549 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001550
1551 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1552 if (!CU_Nodes)
1553 return R;
1554
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001555 for (MDNode *N : CU_Nodes->operands()) {
1556 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001557 DIArray SPs = CUNode.getSubprograms();
1558 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1559 DISubprogram SP(SPs.getElement(i));
1560 if (Function *F = SP.getFunction())
1561 R.insert(std::make_pair(F, SP));
1562 }
1563 }
1564 return R;
1565}