blob: 9003fff707128c3e2cfa06767c52a45d25b8bd06 [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 {
151 return getNumElements() && getElement(0) == dwarf::DW_OP_piece;
152}
153
154uint64_t DIExpression::getPieceOffset() const {
155 assert(isVariablePiece());
156 return getElement(1);
157}
158
159uint64_t DIExpression::getPieceSize() const {
160 assert(isVariablePiece());
161 return getElement(2);
162}
Adrian Prantlb1416832014-08-01 22:11:58 +0000163
Bill Wendling523bea82013-11-08 08:13:15 +0000164//===----------------------------------------------------------------------===//
165// Predicates
166//===----------------------------------------------------------------------===//
167
Manman Renf8a19672014-07-28 22:24:06 +0000168bool DIDescriptor::isSubroutineType() const {
Yaron Kerene8270222014-12-19 22:15:09 +0000169 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
Manman Renf8a19672014-07-28 22:24:06 +0000170}
171
Bill Wendling523bea82013-11-08 08:13:15 +0000172bool DIDescriptor::isBasicType() const {
173 if (!DbgNode)
174 return false;
175 switch (getTag()) {
176 case dwarf::DW_TAG_base_type:
177 case dwarf::DW_TAG_unspecified_type:
178 return true;
179 default:
180 return false;
181 }
182}
183
Bill Wendling523bea82013-11-08 08:13:15 +0000184bool DIDescriptor::isDerivedType() const {
185 if (!DbgNode)
186 return false;
187 switch (getTag()) {
188 case dwarf::DW_TAG_typedef:
189 case dwarf::DW_TAG_pointer_type:
190 case dwarf::DW_TAG_ptr_to_member_type:
191 case dwarf::DW_TAG_reference_type:
192 case dwarf::DW_TAG_rvalue_reference_type:
193 case dwarf::DW_TAG_const_type:
194 case dwarf::DW_TAG_volatile_type:
195 case dwarf::DW_TAG_restrict_type:
196 case dwarf::DW_TAG_member:
197 case dwarf::DW_TAG_inheritance:
198 case dwarf::DW_TAG_friend:
199 return true;
200 default:
201 // CompositeTypes are currently modelled as DerivedTypes.
202 return isCompositeType();
203 }
204}
205
Bill Wendling523bea82013-11-08 08:13:15 +0000206bool DIDescriptor::isCompositeType() const {
207 if (!DbgNode)
208 return false;
209 switch (getTag()) {
210 case dwarf::DW_TAG_array_type:
211 case dwarf::DW_TAG_structure_type:
212 case dwarf::DW_TAG_union_type:
213 case dwarf::DW_TAG_enumeration_type:
214 case dwarf::DW_TAG_subroutine_type:
215 case dwarf::DW_TAG_class_type:
216 return true;
217 default:
218 return false;
219 }
220}
221
Bill Wendling523bea82013-11-08 08:13:15 +0000222bool DIDescriptor::isVariable() const {
223 if (!DbgNode)
224 return false;
225 switch (getTag()) {
226 case dwarf::DW_TAG_auto_variable:
227 case dwarf::DW_TAG_arg_variable:
228 return true;
229 default:
230 return false;
231 }
232}
233
Bill Wendling523bea82013-11-08 08:13:15 +0000234bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000235 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000236}
237
Bill Wendling523bea82013-11-08 08:13:15 +0000238bool DIDescriptor::isSubprogram() const {
239 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
240}
241
Bill Wendling523bea82013-11-08 08:13:15 +0000242bool DIDescriptor::isGlobalVariable() const {
243 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
244 getTag() == dwarf::DW_TAG_constant);
245}
246
Bill Wendling523bea82013-11-08 08:13:15 +0000247bool DIDescriptor::isScope() const {
248 if (!DbgNode)
249 return false;
250 switch (getTag()) {
251 case dwarf::DW_TAG_compile_unit:
252 case dwarf::DW_TAG_lexical_block:
253 case dwarf::DW_TAG_subprogram:
254 case dwarf::DW_TAG_namespace:
255 case dwarf::DW_TAG_file_type:
256 return true;
257 default:
258 break;
259 }
260 return isType();
261}
262
Bill Wendling523bea82013-11-08 08:13:15 +0000263bool DIDescriptor::isTemplateTypeParameter() const {
264 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
265}
266
Bill Wendling523bea82013-11-08 08:13:15 +0000267bool DIDescriptor::isTemplateValueParameter() const {
268 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
269 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
270 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
271}
272
Bill Wendling523bea82013-11-08 08:13:15 +0000273bool DIDescriptor::isCompileUnit() const {
274 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
275}
276
Bill Wendling523bea82013-11-08 08:13:15 +0000277bool DIDescriptor::isFile() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
279}
280
Bill Wendling523bea82013-11-08 08:13:15 +0000281bool DIDescriptor::isNameSpace() const {
282 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
283}
284
Bill Wendling523bea82013-11-08 08:13:15 +0000285bool DIDescriptor::isLexicalBlockFile() const {
286 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000287 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000288}
289
Bill Wendling523bea82013-11-08 08:13:15 +0000290bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000291 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
292 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000293 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000294 DbgNode->getNumOperands() == 3 &&
295 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000296}
297
Bill Wendling523bea82013-11-08 08:13:15 +0000298bool DIDescriptor::isSubrange() const {
299 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
300}
301
Bill Wendling523bea82013-11-08 08:13:15 +0000302bool DIDescriptor::isEnumerator() const {
303 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
304}
305
Bill Wendling523bea82013-11-08 08:13:15 +0000306bool DIDescriptor::isObjCProperty() const {
307 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
308}
309
Bill Wendling523bea82013-11-08 08:13:15 +0000310bool DIDescriptor::isImportedEntity() const {
311 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
312 getTag() == dwarf::DW_TAG_imported_declaration);
313}
314
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000315bool DIDescriptor::isExpression() const {
316 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
317}
318
Bill Wendling523bea82013-11-08 08:13:15 +0000319//===----------------------------------------------------------------------===//
320// Simple Descriptor Constructors and other Methods
321//===----------------------------------------------------------------------===//
322
Frederic Riss36acf0f2014-09-15 07:50:36 +0000323void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000324
325 assert(DbgNode && "Trying to replace an unverified type!");
326
327 // Since we use a TrackingVH for the node, its easy for clients to manufacture
328 // legitimate situations where they want to replaceAllUsesWith() on something
329 // which, due to uniquing, has merged with the source. We shield clients from
330 // this detail by allowing a value to be replaced with replaceAllUsesWith()
331 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000332 const MDNode *DN = D;
333 if (DbgNode == DN) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000334 SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
David Blaikied3f094a2014-05-06 03:41:57 +0000335 for (size_t i = 0; i != Ops.size(); ++i)
336 Ops[i] = DbgNode->getOperand(i);
337 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000338 }
David Blaikied3f094a2014-05-06 03:41:57 +0000339
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000340 assert(DbgNode->isTemporary() && "Expected temporary node");
341 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000342 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
David Blaikied3f094a2014-05-06 03:41:57 +0000343 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000344 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000345}
346
Frederic Riss36acf0f2014-09-15 07:50:36 +0000347void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000348 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000349 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000350 assert(DbgNode->isTemporary() && "Expected temporary node");
351 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000352 Node->replaceAllUsesWith(D);
David Blaikied3f094a2014-05-06 03:41:57 +0000353 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000354}
355
Bill Wendling523bea82013-11-08 08:13:15 +0000356bool DICompileUnit::Verify() const {
357 if (!isCompileUnit())
358 return false;
359
360 // Don't bother verifying the compilation directory or producer string
361 // as those could be empty.
362 if (getFilename().empty())
363 return false;
364
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000365 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000366}
367
Bill Wendling523bea82013-11-08 08:13:15 +0000368bool DIObjCProperty::Verify() const {
369 if (!isObjCProperty())
370 return false;
371
372 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000373 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000374}
375
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000376/// \brief Check if a field at position Elt of a MDNode is a MDNode.
377///
Bill Wendling523bea82013-11-08 08:13:15 +0000378/// We currently allow an empty string and an integer.
379/// But we don't allow a non-empty string in a MDNode field.
380static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
381 // FIXME: This function should return true, if the field is null or the field
382 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000383 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000384 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
385 return false;
386 return true;
387}
388
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000389/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000390static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000391 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000392 return !Fld || isa<MDString>(Fld);
393}
394
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000395/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000396static bool isTypeRef(const Metadata *MD) {
397 if (!MD)
398 return true;
399 if (auto *S = dyn_cast<MDString>(MD))
400 return !S->getString().empty();
401 if (auto *N = dyn_cast<MDNode>(MD))
402 return DIType(N).isType();
403 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000404}
405
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000406/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000407static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000408 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000409}
410
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000411/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000412static bool isScopeRef(const Metadata *MD) {
413 if (!MD)
414 return true;
415 if (auto *S = dyn_cast<MDString>(MD))
416 return !S->getString().empty();
417 return isa<MDNode>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000418}
419
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000420/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000421static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000422 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000423}
424
Bill Wendling523bea82013-11-08 08:13:15 +0000425bool DIType::Verify() const {
426 if (!isType())
427 return false;
428 // Make sure Context @ field 2 is MDNode.
429 if (!fieldIsScopeRef(DbgNode, 2))
430 return false;
431
432 // FIXME: Sink this into the various subclass verifies.
433 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000434 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000435 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
436 Tag != dwarf::DW_TAG_ptr_to_member_type &&
437 Tag != dwarf::DW_TAG_reference_type &&
438 Tag != dwarf::DW_TAG_rvalue_reference_type &&
439 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
440 Tag != dwarf::DW_TAG_enumeration_type &&
441 Tag != dwarf::DW_TAG_subroutine_type &&
442 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
443 getFilename().empty())
444 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000445
Bill Wendling523bea82013-11-08 08:13:15 +0000446 // DIType is abstract, it should be a BasicType, a DerivedType or
447 // a CompositeType.
448 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000449 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000450 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000451 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000452 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000453 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000454 else
455 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000456}
457
Bill Wendling523bea82013-11-08 08:13:15 +0000458bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000459 return isBasicType() && DbgNode->getNumOperands() == 3 &&
460 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000461}
462
Bill Wendling523bea82013-11-08 08:13:15 +0000463bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000464 // Make sure DerivedFrom @ field 3 is TypeRef.
465 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000466 return false;
467 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000468 // Make sure ClassType @ field 4 is a TypeRef.
469 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000470 return false;
471
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000472 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
473 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
474 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000475}
476
Bill Wendling523bea82013-11-08 08:13:15 +0000477bool DICompositeType::Verify() const {
478 if (!isCompositeType())
479 return false;
480
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000481 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
482 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000483 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000484 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000485 return false;
486
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000487 // Make sure the type identifier at field 7 is MDString, it can be null.
488 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000489 return false;
490
Adrian Prantl99c7af22013-12-18 21:48:19 +0000491 // A subroutine type can't be both & and &&.
492 if (isLValueReference() && isRValueReference())
493 return false;
494
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000495 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000496}
497
Bill Wendling523bea82013-11-08 08:13:15 +0000498bool DISubprogram::Verify() const {
499 if (!isSubprogram())
500 return false;
501
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000502 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000503 if (!fieldIsScopeRef(DbgNode, 2))
504 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000505 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000506 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000507 // Containing type @ field 4.
508 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000509 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000510
511 // A subprogram can't be both & and &&.
512 if (isLValueReference() && isRValueReference())
513 return false;
514
David Blaikie3dfe4782014-10-14 18:22:52 +0000515 // If a DISubprogram has an llvm::Function*, then scope chains from all
516 // instructions within the function should lead to this DISubprogram.
517 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000518 for (auto &BB : *F) {
519 for (auto &I : BB) {
520 DebugLoc DL = I.getDebugLoc();
521 if (DL.isUnknown())
522 continue;
523
524 MDNode *Scope = nullptr;
525 MDNode *IA = nullptr;
526 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000527 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000528 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000529 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000530 if (!Scope)
531 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000532 assert(!IA);
533 while (!DIDescriptor(Scope).isSubprogram()) {
534 DILexicalBlockFile D(Scope);
535 Scope = D.isLexicalBlockFile()
536 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000537 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantlde200df2015-01-20 22:37:25 +0000538 assert(Scope && "lexical block file has no scope");
David Blaikie3dfe4782014-10-14 18:22:52 +0000539 }
540 if (!DISubprogram(Scope).describes(F))
541 return false;
542 }
543 }
544 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000545 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000546}
547
Bill Wendling523bea82013-11-08 08:13:15 +0000548bool DIGlobalVariable::Verify() const {
549 if (!isGlobalVariable())
550 return false;
551
552 if (getDisplayName().empty())
553 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000554 // Make sure context @ field 1 is an MDNode.
555 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000556 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000557 // Make sure that type @ field 3 is a DITypeRef.
558 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000559 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000560 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
561 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000562 return false;
563
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000564 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000565}
566
Bill Wendling523bea82013-11-08 08:13:15 +0000567bool DIVariable::Verify() const {
568 if (!isVariable())
569 return false;
570
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000571 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000572 if (!fieldIsMDNode(DbgNode, 1))
573 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000574 // Make sure that type @ field 3 is a DITypeRef.
575 if (!fieldIsTypeRef(DbgNode, 3))
576 return false;
577
578 // Check the number of header fields, which is common between complex and
579 // simple variables.
580 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000581 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000582
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000583 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000584 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000585 return true;
586
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000587 // Variable with an inline location.
588 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000589}
590
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000591bool DIExpression::Verify() const {
592 // Empty DIExpressions may be represented as a nullptr.
593 if (!DbgNode)
594 return true;
595
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000596 return isExpression() && DbgNode->getNumOperands() == 1;
Bill Wendling523bea82013-11-08 08:13:15 +0000597}
598
Bill Wendling523bea82013-11-08 08:13:15 +0000599bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000600 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000601}
602
Bill Wendling523bea82013-11-08 08:13:15 +0000603bool DINameSpace::Verify() const {
604 if (!isNameSpace())
605 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000606 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000607}
608
Bill Wendling523bea82013-11-08 08:13:15 +0000609MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
610
Bill Wendling523bea82013-11-08 08:13:15 +0000611bool DIFile::Verify() const {
612 return isFile() && DbgNode->getNumOperands() == 2;
613}
614
Bill Wendling523bea82013-11-08 08:13:15 +0000615bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000616 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
617 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000618}
619
Bill Wendling523bea82013-11-08 08:13:15 +0000620bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000621 return isSubrange() && DbgNode->getNumOperands() == 1 &&
622 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000623}
624
Bill Wendling523bea82013-11-08 08:13:15 +0000625bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000626 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
627 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000628}
629
Bill Wendling523bea82013-11-08 08:13:15 +0000630bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000631 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
632 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000633}
634
Bill Wendling523bea82013-11-08 08:13:15 +0000635bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000636 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
637 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000638}
639
Bill Wendling523bea82013-11-08 08:13:15 +0000640bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000641 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
642 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000643}
644
Bill Wendling523bea82013-11-08 08:13:15 +0000645bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000646 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
647 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000648}
649
Bill Wendling523bea82013-11-08 08:13:15 +0000650MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000651 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000652}
653
654MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000655 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000656}
657
658#ifndef NDEBUG
659static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
660 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
661 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000662 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000663 continue;
664 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
665 bool found = false;
666 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000667 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000668 assert(found && "Losing a member during member list replacement");
669 }
670}
671#endif
672
Manman Ren1a125c92014-07-28 19:33:20 +0000673void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000674 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000675 if (Elements) {
676#ifndef NDEBUG
677 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000678 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000679 VerifySubsetOf(El, Elements);
680#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000681 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000682 }
683 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000684 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000685 DbgNode = N;
686}
687
Bill Wendling523bea82013-11-08 08:13:15 +0000688DIScopeRef DIScope::getRef() const {
689 if (!isCompositeType())
690 return DIScopeRef(*this);
691 DICompositeType DTy(DbgNode);
692 if (!DTy.getIdentifier())
693 return DIScopeRef(*this);
694 return DIScopeRef(DTy.getIdentifier());
695}
696
Bill Wendling523bea82013-11-08 08:13:15 +0000697void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000698 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000699 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000700 DbgNode = N;
701}
702
Bill Wendling523bea82013-11-08 08:13:15 +0000703bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
704 assert(CurFn && "Invalid function");
705 if (!getContext().isSubprogram())
706 return false;
707 // This variable is not inlined function argument if its scope
708 // does not describe current function.
709 return !DISubprogram(getContext()).describes(CurFn);
710}
711
Bill Wendling523bea82013-11-08 08:13:15 +0000712bool DISubprogram::describes(const Function *F) {
713 assert(F && "Invalid function");
714 if (F == getFunction())
715 return true;
716 StringRef Name = getLinkageName();
717 if (Name.empty())
718 Name = getName();
719 if (F->getName() == Name)
720 return true;
721 return false;
722}
723
Bill Wendling523bea82013-11-08 08:13:15 +0000724MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000725 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000726}
727
728DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000729 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000730}
731
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000732Metadata *DITemplateValueParameter::getValue() const {
733 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000734}
735
Bill Wendling523bea82013-11-08 08:13:15 +0000736DIScopeRef DIScope::getContext() const {
737
738 if (isType())
739 return DIType(DbgNode).getContext();
740
741 if (isSubprogram())
742 return DIScopeRef(DISubprogram(DbgNode).getContext());
743
744 if (isLexicalBlock())
745 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
746
747 if (isLexicalBlockFile())
748 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
749
750 if (isNameSpace())
751 return DIScopeRef(DINameSpace(DbgNode).getContext());
752
753 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000754 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000755}
756
Bill Wendling523bea82013-11-08 08:13:15 +0000757StringRef DIScope::getName() const {
758 if (isType())
759 return DIType(DbgNode).getName();
760 if (isSubprogram())
761 return DISubprogram(DbgNode).getName();
762 if (isNameSpace())
763 return DINameSpace(DbgNode).getName();
764 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
765 isCompileUnit()) &&
766 "Unhandled type of scope.");
767 return StringRef();
768}
769
770StringRef DIScope::getFilename() const {
771 if (!DbgNode)
772 return StringRef();
773 return ::getStringField(getNodeField(DbgNode, 1), 0);
774}
775
776StringRef DIScope::getDirectory() const {
777 if (!DbgNode)
778 return StringRef();
779 return ::getStringField(getNodeField(DbgNode, 1), 1);
780}
781
782DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000783 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000784 return DIArray();
785
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000786 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000787}
788
789DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000790 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000791 return DIArray();
792
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000793 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000794}
795
796DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000797 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000798 return DIArray();
799
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000800 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000801}
802
803DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000804 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000805 return DIArray();
806
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000807 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000808}
809
810DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000811 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000812 return DIArray();
813
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000814 return DIArray(getNodeField(DbgNode, 6));
815}
816
817void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
818 assert(Verify() && "Expected compile unit");
819 if (Subprograms == getSubprograms())
820 return;
821
822 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
823}
824
825void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
826 assert(Verify() && "Expected compile unit");
827 if (GlobalVariables == getGlobalVariables())
828 return;
829
830 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000831}
832
Diego Novillof5041ce2014-03-03 20:06:11 +0000833DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000834 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000835 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000836 assert(NewScope && "Expected valid scope");
837
838 const auto *Old = cast<MDLocation>(DbgNode);
839 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
840 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000841}
842
Diego Novillof5041ce2014-03-03 20:06:11 +0000843unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
844 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
845 return ++Ctx.pImpl->DiscriminatorTable[Key];
846}
847
Bill Wendling523bea82013-11-08 08:13:15 +0000848DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
849 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000850 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
851 if (!InlinedScope)
852 return cleanseInlinedVariable(DV, VMContext);
853
854 // Insert inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000855 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000856 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
857 Elts.push_back(DV->getOperand(I));
858 Elts.push_back(InlinedScope);
859
860 DIVariable Inlined(MDNode::get(VMContext, Elts));
861 assert(Inlined.Verify() && "Expected to create a DIVariable");
862 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000863}
864
Bill Wendling523bea82013-11-08 08:13:15 +0000865DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000866 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
867 if (!DIVariable(DV).getInlinedAt())
868 return DIVariable(DV);
869
870 // Remove inlined scope.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000871 SmallVector<Metadata *, 8> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000872 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
873 Elts.push_back(DV->getOperand(I));
874
875 DIVariable Cleansed(MDNode::get(VMContext, Elts));
876 assert(Cleansed.Verify() && "Expected to create a DIVariable");
877 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000878}
879
Bill Wendling523bea82013-11-08 08:13:15 +0000880DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
881 DIDescriptor D(Scope);
882 if (D.isSubprogram())
883 return DISubprogram(Scope);
884
885 if (D.isLexicalBlockFile())
886 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
887
888 if (D.isLexicalBlock())
889 return getDISubprogram(DILexicalBlock(Scope).getContext());
890
891 return DISubprogram();
892}
893
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000894DISubprogram llvm::getDISubprogram(const Function *F) {
895 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000896 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000897 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
898 return !Inst.getDebugLoc().isUnknown();
899 });
900 if (Inst == BB.end())
901 continue;
902 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000903 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000904 DISubprogram Subprogram = getDISubprogram(Scope);
905 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000906 }
907
908 return DISubprogram();
909}
910
Bill Wendling523bea82013-11-08 08:13:15 +0000911DICompositeType llvm::getDICompositeType(DIType T) {
912 if (T.isCompositeType())
913 return DICompositeType(T);
914
915 if (T.isDerivedType()) {
916 // This function is currently used by dragonegg and dragonegg does
917 // not generate identifier for types, so using an empty map to resolve
918 // DerivedFrom should be fine.
919 DITypeIdentifierMap EmptyMap;
920 return getDICompositeType(
921 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
922 }
923
924 return DICompositeType();
925}
926
Bill Wendling523bea82013-11-08 08:13:15 +0000927DITypeIdentifierMap
928llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
929 DITypeIdentifierMap Map;
930 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000931 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000932 DIArray Retain = CU.getRetainedTypes();
933 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
934 if (!Retain.getElement(Ti).isCompositeType())
935 continue;
936 DICompositeType Ty(Retain.getElement(Ti));
937 if (MDString *TypeId = Ty.getIdentifier()) {
938 // Definition has priority over declaration.
939 // Try to insert (TypeId, Ty) to Map.
940 std::pair<DITypeIdentifierMap::iterator, bool> P =
941 Map.insert(std::make_pair(TypeId, Ty));
942 // If TypeId already exists in Map and this is a definition, replace
943 // whatever we had (declaration or definition) with the definition.
944 if (!P.second && !Ty.isForwardDecl())
945 P.first->second = Ty;
946 }
947 }
948 }
949 return Map;
950}
951
952//===----------------------------------------------------------------------===//
953// DebugInfoFinder implementations.
954//===----------------------------------------------------------------------===//
955
956void DebugInfoFinder::reset() {
957 CUs.clear();
958 SPs.clear();
959 GVs.clear();
960 TYs.clear();
961 Scopes.clear();
962 NodesSeen.clear();
963 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000964 TypeMapInitialized = false;
965}
966
Manman Renb46e5502013-11-17 19:35:03 +0000967void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000968 if (!TypeMapInitialized)
969 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
970 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
971 TypeMapInitialized = true;
972 }
Bill Wendling523bea82013-11-08 08:13:15 +0000973}
974
Bill Wendling523bea82013-11-08 08:13:15 +0000975void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000976 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000977 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000978 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000979 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000980 addCompileUnit(CU);
981 DIArray GVs = CU.getGlobalVariables();
982 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
983 DIGlobalVariable DIG(GVs.getElement(i));
984 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000985 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000986 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000987 }
988 }
989 DIArray SPs = CU.getSubprograms();
990 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
991 processSubprogram(DISubprogram(SPs.getElement(i)));
992 DIArray EnumTypes = CU.getEnumTypes();
993 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
994 processType(DIType(EnumTypes.getElement(i)));
995 DIArray RetainedTypes = CU.getRetainedTypes();
996 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
997 processType(DIType(RetainedTypes.getElement(i)));
998 DIArray Imports = CU.getImportedEntities();
999 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1000 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001001 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001002 if (Entity.isType())
1003 processType(DIType(Entity));
1004 else if (Entity.isSubprogram())
1005 processSubprogram(DISubprogram(Entity));
1006 else if (Entity.isNameSpace())
1007 processScope(DINameSpace(Entity).getContext());
1008 }
1009 }
1010 }
1011}
1012
Manman Ren2085ccc2013-11-17 18:42:37 +00001013void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001014 if (!Loc)
1015 return;
Manman Renb46e5502013-11-17 19:35:03 +00001016 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001017 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001018 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001019}
1020
Bill Wendling523bea82013-11-08 08:13:15 +00001021void DebugInfoFinder::processType(DIType DT) {
1022 if (!addType(DT))
1023 return;
1024 processScope(DT.getContext().resolve(TypeIdentifierMap));
1025 if (DT.isCompositeType()) {
1026 DICompositeType DCT(DT);
1027 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001028 if (DT.isSubroutineType()) {
1029 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1030 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1031 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1032 return;
1033 }
Manman Renab8ffba2014-07-28 19:14:13 +00001034 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001035 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1036 DIDescriptor D = DA.getElement(i);
1037 if (D.isType())
1038 processType(DIType(D));
1039 else if (D.isSubprogram())
1040 processSubprogram(DISubprogram(D));
1041 }
1042 } else if (DT.isDerivedType()) {
1043 DIDerivedType DDT(DT);
1044 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1045 }
1046}
1047
1048void DebugInfoFinder::processScope(DIScope Scope) {
1049 if (Scope.isType()) {
1050 DIType Ty(Scope);
1051 processType(Ty);
1052 return;
1053 }
1054 if (Scope.isCompileUnit()) {
1055 addCompileUnit(DICompileUnit(Scope));
1056 return;
1057 }
1058 if (Scope.isSubprogram()) {
1059 processSubprogram(DISubprogram(Scope));
1060 return;
1061 }
1062 if (!addScope(Scope))
1063 return;
1064 if (Scope.isLexicalBlock()) {
1065 DILexicalBlock LB(Scope);
1066 processScope(LB.getContext());
1067 } else if (Scope.isLexicalBlockFile()) {
1068 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1069 processScope(LBF.getScope());
1070 } else if (Scope.isNameSpace()) {
1071 DINameSpace NS(Scope);
1072 processScope(NS.getContext());
1073 }
1074}
1075
Bill Wendling523bea82013-11-08 08:13:15 +00001076void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1077 if (!addSubprogram(SP))
1078 return;
1079 processScope(SP.getContext().resolve(TypeIdentifierMap));
1080 processType(SP.getType());
1081 DIArray TParams = SP.getTemplateParams();
1082 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1083 DIDescriptor Element = TParams.getElement(I);
1084 if (Element.isTemplateTypeParameter()) {
1085 DITemplateTypeParameter TType(Element);
1086 processScope(TType.getContext().resolve(TypeIdentifierMap));
1087 processType(TType.getType().resolve(TypeIdentifierMap));
1088 } else if (Element.isTemplateValueParameter()) {
1089 DITemplateValueParameter TVal(Element);
1090 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1091 processType(TVal.getType().resolve(TypeIdentifierMap));
1092 }
1093 }
1094}
1095
Manman Ren2085ccc2013-11-17 18:42:37 +00001096void DebugInfoFinder::processDeclare(const Module &M,
1097 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001098 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1099 if (!N)
1100 return;
Manman Renb46e5502013-11-17 19:35:03 +00001101 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001102
1103 DIDescriptor DV(N);
1104 if (!DV.isVariable())
1105 return;
1106
David Blaikie70573dc2014-11-19 07:49:26 +00001107 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001108 return;
1109 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001110 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001111}
1112
Manman Ren2085ccc2013-11-17 18:42:37 +00001113void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001114 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1115 if (!N)
1116 return;
Manman Renb46e5502013-11-17 19:35:03 +00001117 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001118
1119 DIDescriptor DV(N);
1120 if (!DV.isVariable())
1121 return;
1122
David Blaikie70573dc2014-11-19 07:49:26 +00001123 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001124 return;
1125 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001126 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001127}
1128
Bill Wendling523bea82013-11-08 08:13:15 +00001129bool DebugInfoFinder::addType(DIType DT) {
1130 if (!DT)
1131 return false;
1132
David Blaikie70573dc2014-11-19 07:49:26 +00001133 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001134 return false;
1135
1136 TYs.push_back(DT);
1137 return true;
1138}
1139
Bill Wendling523bea82013-11-08 08:13:15 +00001140bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1141 if (!CU)
1142 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001143 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001144 return false;
1145
1146 CUs.push_back(CU);
1147 return true;
1148}
1149
Bill Wendling523bea82013-11-08 08:13:15 +00001150bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1151 if (!DIG)
1152 return false;
1153
David Blaikie70573dc2014-11-19 07:49:26 +00001154 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001155 return false;
1156
1157 GVs.push_back(DIG);
1158 return true;
1159}
1160
Bill Wendling523bea82013-11-08 08:13:15 +00001161bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1162 if (!SP)
1163 return false;
1164
David Blaikie70573dc2014-11-19 07:49:26 +00001165 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001166 return false;
1167
1168 SPs.push_back(SP);
1169 return true;
1170}
1171
1172bool DebugInfoFinder::addScope(DIScope Scope) {
1173 if (!Scope)
1174 return false;
1175 // FIXME: Ocaml binding generates a scope with no content, we treat it
1176 // as null for now.
1177 if (Scope->getNumOperands() == 0)
1178 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001179 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001180 return false;
1181 Scopes.push_back(Scope);
1182 return true;
1183}
1184
1185//===----------------------------------------------------------------------===//
1186// DIDescriptor: dump routines for all descriptors.
1187//===----------------------------------------------------------------------===//
1188
Bill Wendling523bea82013-11-08 08:13:15 +00001189void DIDescriptor::dump() const {
1190 print(dbgs());
1191 dbgs() << '\n';
1192}
1193
Bill Wendling523bea82013-11-08 08:13:15 +00001194void DIDescriptor::print(raw_ostream &OS) const {
1195 if (!DbgNode)
1196 return;
1197
1198 if (const char *Tag = dwarf::TagString(getTag()))
1199 OS << "[ " << Tag << " ]";
1200
1201 if (this->isSubrange()) {
1202 DISubrange(DbgNode).printInternal(OS);
1203 } else if (this->isCompileUnit()) {
1204 DICompileUnit(DbgNode).printInternal(OS);
1205 } else if (this->isFile()) {
1206 DIFile(DbgNode).printInternal(OS);
1207 } else if (this->isEnumerator()) {
1208 DIEnumerator(DbgNode).printInternal(OS);
1209 } else if (this->isBasicType()) {
1210 DIType(DbgNode).printInternal(OS);
1211 } else if (this->isDerivedType()) {
1212 DIDerivedType(DbgNode).printInternal(OS);
1213 } else if (this->isCompositeType()) {
1214 DICompositeType(DbgNode).printInternal(OS);
1215 } else if (this->isSubprogram()) {
1216 DISubprogram(DbgNode).printInternal(OS);
1217 } else if (this->isGlobalVariable()) {
1218 DIGlobalVariable(DbgNode).printInternal(OS);
1219 } else if (this->isVariable()) {
1220 DIVariable(DbgNode).printInternal(OS);
1221 } else if (this->isObjCProperty()) {
1222 DIObjCProperty(DbgNode).printInternal(OS);
1223 } else if (this->isNameSpace()) {
1224 DINameSpace(DbgNode).printInternal(OS);
1225 } else if (this->isScope()) {
1226 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001227 } else if (this->isExpression()) {
1228 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001229 }
1230}
1231
1232void DISubrange::printInternal(raw_ostream &OS) const {
1233 int64_t Count = getCount();
1234 if (Count != -1)
1235 OS << " [" << getLo() << ", " << Count - 1 << ']';
1236 else
1237 OS << " [unbounded]";
1238}
1239
1240void DIScope::printInternal(raw_ostream &OS) const {
1241 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1242}
1243
1244void DICompileUnit::printInternal(raw_ostream &OS) const {
1245 DIScope::printInternal(OS);
1246 OS << " [";
1247 unsigned Lang = getLanguage();
1248 if (const char *LangStr = dwarf::LanguageString(Lang))
1249 OS << LangStr;
1250 else
1251 (OS << "lang 0x").write_hex(Lang);
1252 OS << ']';
1253}
1254
1255void DIEnumerator::printInternal(raw_ostream &OS) const {
1256 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1257}
1258
1259void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001260 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001261 return;
1262
1263 StringRef Res = getName();
1264 if (!Res.empty())
1265 OS << " [" << Res << "]";
1266
1267 // TODO: Print context?
1268
1269 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1270 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1271 if (isBasicType())
1272 if (const char *Enc =
1273 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1274 OS << ", enc " << Enc;
1275 OS << "]";
1276
1277 if (isPrivate())
1278 OS << " [private]";
1279 else if (isProtected())
1280 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001281 else if (isPublic())
1282 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001283
1284 if (isArtificial())
1285 OS << " [artificial]";
1286
1287 if (isForwardDecl())
1288 OS << " [decl]";
1289 else if (getTag() == dwarf::DW_TAG_structure_type ||
1290 getTag() == dwarf::DW_TAG_union_type ||
1291 getTag() == dwarf::DW_TAG_enumeration_type ||
1292 getTag() == dwarf::DW_TAG_class_type)
1293 OS << " [def]";
1294 if (isVector())
1295 OS << " [vector]";
1296 if (isStaticMember())
1297 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001298
1299 if (isLValueReference())
1300 OS << " [reference]";
1301
1302 if (isRValueReference())
1303 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001304}
1305
1306void DIDerivedType::printInternal(raw_ostream &OS) const {
1307 DIType::printInternal(OS);
1308 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1309}
1310
1311void DICompositeType::printInternal(raw_ostream &OS) const {
1312 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001313 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001314 OS << " [" << A.getNumElements() << " elements]";
1315}
1316
1317void DINameSpace::printInternal(raw_ostream &OS) const {
1318 StringRef Name = getName();
1319 if (!Name.empty())
1320 OS << " [" << Name << ']';
1321
1322 OS << " [line " << getLineNumber() << ']';
1323}
1324
1325void DISubprogram::printInternal(raw_ostream &OS) const {
1326 // TODO : Print context
1327 OS << " [line " << getLineNumber() << ']';
1328
1329 if (isLocalToUnit())
1330 OS << " [local]";
1331
1332 if (isDefinition())
1333 OS << " [def]";
1334
1335 if (getScopeLineNumber() != getLineNumber())
1336 OS << " [scope " << getScopeLineNumber() << "]";
1337
1338 if (isPrivate())
1339 OS << " [private]";
1340 else if (isProtected())
1341 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001342 else if (isPublic())
1343 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001344
Adrian Prantl99c7af22013-12-18 21:48:19 +00001345 if (isLValueReference())
1346 OS << " [reference]";
1347
1348 if (isRValueReference())
1349 OS << " [rvalue reference]";
1350
Bill Wendling523bea82013-11-08 08:13:15 +00001351 StringRef Res = getName();
1352 if (!Res.empty())
1353 OS << " [" << Res << ']';
1354}
1355
1356void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1357 StringRef Res = getName();
1358 if (!Res.empty())
1359 OS << " [" << Res << ']';
1360
1361 OS << " [line " << getLineNumber() << ']';
1362
1363 // TODO : Print context
1364
1365 if (isLocalToUnit())
1366 OS << " [local]";
1367
1368 if (isDefinition())
1369 OS << " [def]";
1370}
1371
1372void DIVariable::printInternal(raw_ostream &OS) const {
1373 StringRef Res = getName();
1374 if (!Res.empty())
1375 OS << " [" << Res << ']';
1376
1377 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001378}
Adrian Prantlb1416832014-08-01 22:11:58 +00001379
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001380void DIExpression::printInternal(raw_ostream &OS) const {
1381 for (unsigned I = 0; I < getNumElements(); ++I) {
1382 uint64_t OpCode = getElement(I);
1383 OS << " [" << OperationEncodingString(OpCode);
1384 switch (OpCode) {
1385 case DW_OP_plus: {
1386 OS << " " << getElement(++I);
1387 break;
1388 }
1389 case DW_OP_piece: {
1390 unsigned Offset = getElement(++I);
1391 unsigned Size = getElement(++I);
Adrian Prantl38666f12014-10-02 16:42:15 +00001392 OS << " offset=" << Offset << ", size=" << Size;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001393 break;
1394 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001395 case DW_OP_deref:
1396 // No arguments.
1397 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001398 default:
Adrian Prantl75a0dac2014-10-02 16:42:13 +00001399 // Else bail out early. This may be a line table entry.
1400 OS << "Unknown]";
1401 return;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001402 }
1403 OS << "]";
1404 }
Bill Wendling523bea82013-11-08 08:13:15 +00001405}
1406
1407void DIObjCProperty::printInternal(raw_ostream &OS) const {
1408 StringRef Name = getObjCPropertyName();
1409 if (!Name.empty())
1410 OS << " [" << Name << ']';
1411
1412 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1413 << ']';
1414}
1415
1416static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1417 const LLVMContext &Ctx) {
1418 if (!DL.isUnknown()) { // Print source line info.
1419 DIScope Scope(DL.getScope(Ctx));
1420 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1421 // Omit the directory, because it's likely to be long and uninteresting.
1422 CommentOS << Scope.getFilename();
1423 CommentOS << ':' << DL.getLine();
1424 if (DL.getCol() != 0)
1425 CommentOS << ':' << DL.getCol();
1426 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1427 if (!InlinedAtDL.isUnknown()) {
1428 CommentOS << " @[ ";
1429 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1430 CommentOS << " ]";
1431 }
1432 }
1433}
1434
1435void DIVariable::printExtendedName(raw_ostream &OS) const {
1436 const LLVMContext &Ctx = DbgNode->getContext();
1437 StringRef Res = getName();
1438 if (!Res.empty())
1439 OS << Res << "," << getLineNumber();
1440 if (MDNode *InlinedAt = getInlinedAt()) {
1441 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1442 if (!InlinedAtDL.isUnknown()) {
1443 OS << " @[";
1444 printDebugLoc(InlinedAtDL, OS, Ctx);
1445 OS << "]";
1446 }
1447 }
1448}
1449
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001450template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001451 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1452}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001453template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001454 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1455}
1456
Bill Wendling523bea82013-11-08 08:13:15 +00001457template <>
1458DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001459 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001460}
Bill Wendling523bea82013-11-08 08:13:15 +00001461template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001462 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001463}
Manman Rencb14bbc2013-11-22 22:06:31 +00001464
Manman Rencb14bbc2013-11-22 22:06:31 +00001465bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001466 bool Changed = false;
1467
1468 // Remove all of the calls to the debugger intrinsics, and remove them from
1469 // the module.
1470 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1471 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001472 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001473 CI->eraseFromParent();
1474 }
1475 Declare->eraseFromParent();
1476 Changed = true;
1477 }
1478
1479 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1480 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001481 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001482 CI->eraseFromParent();
1483 }
1484 DbgVal->eraseFromParent();
1485 Changed = true;
1486 }
1487
1488 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1489 NME = M.named_metadata_end(); NMI != NME;) {
1490 NamedMDNode *NMD = NMI;
1491 ++NMI;
1492 if (NMD->getName().startswith("llvm.dbg.")) {
1493 NMD->eraseFromParent();
1494 Changed = true;
1495 }
1496 }
1497
1498 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1499 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1500 ++FI)
1501 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1502 ++BI) {
1503 if (!BI->getDebugLoc().isUnknown()) {
1504 Changed = true;
1505 BI->setDebugLoc(DebugLoc());
1506 }
1507 }
1508
1509 return Changed;
1510}
Manman Ren8b4306c2013-12-02 21:29:56 +00001511
Manman Renbd4daf82013-12-03 00:12:14 +00001512unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001513 if (auto *Val = mdconst::extract_or_null<ConstantInt>(
1514 M.getModuleFlag("Debug Info Version")))
1515 return Val->getZExtValue();
1516 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001517}
David Blaikie6876b3b2014-07-01 20:05:26 +00001518
David Blaikiea8c35092014-07-02 18:30:05 +00001519llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1520llvm::makeSubprogramMap(const Module &M) {
1521 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001522
1523 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1524 if (!CU_Nodes)
1525 return R;
1526
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001527 for (MDNode *N : CU_Nodes->operands()) {
1528 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001529 DIArray SPs = CUNode.getSubprograms();
1530 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1531 DISubprogram SP(SPs.getElement(i));
1532 if (Function *F = SP.getFunction())
1533 R.insert(std::make_pair(F, SP));
1534 }
1535 }
1536 return R;
1537}