blob: ad75fbae80db6ecf87f45cd7af80708e56f3ce76 [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"
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000020#include "llvm/ADT/StringSwitch.h"
Bill Wendling523bea82013-11-08 08:13:15 +000021#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000023#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000024#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000029#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34using namespace llvm::dwarf;
35
36//===----------------------------------------------------------------------===//
37// DIDescriptor
38//===----------------------------------------------------------------------===//
39
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000040unsigned DIDescriptor::getFlag(StringRef Flag) {
41 return StringSwitch<unsigned>(Flag)
42#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
43#include "llvm/IR/DebugInfoFlags.def"
44 .Default(0);
45}
46
47const char *DIDescriptor::getFlagString(unsigned Flag) {
48 switch (Flag) {
49 default:
50 return "";
51#define HANDLE_DI_FLAG(ID, NAME) \
52 case Flag##NAME: \
53 return "DIFlag" #NAME;
54#include "llvm/IR/DebugInfoFlags.def"
55 }
56}
57
Bill Wendling523bea82013-11-08 08:13:15 +000058bool DIDescriptor::Verify() const {
59 return DbgNode &&
60 (DIDerivedType(DbgNode).Verify() ||
61 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
62 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
63 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
64 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
65 DILexicalBlock(DbgNode).Verify() ||
66 DILexicalBlockFile(DbgNode).Verify() ||
67 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
68 DIObjCProperty(DbgNode).Verify() ||
69 DITemplateTypeParameter(DbgNode).Verify() ||
70 DITemplateValueParameter(DbgNode).Verify() ||
Adrian Prantl87b7eb92014-10-01 18:55:02 +000071 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000072}
73
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000074static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000075 if (!DbgNode || Elt >= DbgNode->getNumOperands())
76 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000077 return DbgNode->getOperand(Elt);
78}
79
80static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
81 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
82}
83
84static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
85 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
86 return MDS->getString();
87 return StringRef();
88}
89
90StringRef DIDescriptor::getStringField(unsigned Elt) const {
91 return ::getStringField(DbgNode, Elt);
92}
93
94uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000095 if (auto *C = getConstantField(Elt))
96 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Bill Wendling523bea82013-11-08 08:13:15 +000097 return CI->getZExtValue();
98
99 return 0;
100}
101
102int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000103 if (auto *C = getConstantField(Elt))
104 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
105 return CI->getZExtValue();
Bill Wendling523bea82013-11-08 08:13:15 +0000106
107 return 0;
108}
109
110DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
111 MDNode *Field = getNodeField(DbgNode, Elt);
112 return DIDescriptor(Field);
113}
114
115GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000116 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000117}
118
119Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000120 if (!DbgNode)
121 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000122
123 if (Elt < DbgNode->getNumOperands())
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000124 if (auto *C =
125 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
126 return C->getValue();
Craig Topperc6207612014-04-09 06:08:46 +0000127 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000128}
129
130Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000131 return dyn_cast_or_null<Function>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000132}
133
134void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000135 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000136 return;
137
138 if (Elt < DbgNode->getNumOperands()) {
139 MDNode *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000140 Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000141 }
142}
143
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000144static unsigned DIVariableInlinedAtIndex = 4;
145MDNode *DIVariable::getInlinedAt() const {
146 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
147}
Bill Wendling523bea82013-11-08 08:13:15 +0000148
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000149/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000150unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
151 DIType Ty = getType().resolve(Map);
152 // Follow derived types until we reach a type that
153 // reports back a size.
154 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
155 DIDerivedType DT(&*Ty);
156 Ty = DT.getTypeDerivedFrom().resolve(Map);
157 }
158 assert(Ty.getSizeInBits() && "type with size 0");
159 return Ty.getSizeInBits();
160}
161
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000162uint64_t DIExpression::getElement(unsigned Idx) const {
163 unsigned I = Idx + 1;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000164 assert(I < getNumHeaderFields() &&
165 "non-existing complex address element requested");
166 return getHeaderFieldAs<int64_t>(I);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000167}
Adrian Prantlb1416832014-08-01 22:11:58 +0000168
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000169bool DIExpression::isBitPiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000170 unsigned N = getNumElements();
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000171 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000172}
173
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000174uint64_t DIExpression::getBitPieceOffset() const {
175 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000176 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000177}
178
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000179uint64_t DIExpression::getBitPieceSize() const {
180 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000181 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000182}
Adrian Prantlb1416832014-08-01 22:11:58 +0000183
Adrian Prantl2585a982015-01-22 16:55:20 +0000184DIExpression::iterator DIExpression::begin() const {
185 return DIExpression::iterator(*this);
Adrian Prantl9260cca2015-01-22 00:00:52 +0000186}
187
Adrian Prantl2585a982015-01-22 16:55:20 +0000188DIExpression::iterator DIExpression::end() const {
189 return DIExpression::iterator();
Adrian Prantl9260cca2015-01-22 00:00:52 +0000190}
191
Benjamin Kramer4d50b6c2015-01-24 19:55:23 +0000192DIExpression::Operand DIExpression::Operand::getNext() const {
Adrian Prantl70f2a732015-01-23 23:40:47 +0000193 iterator it(I);
194 return *(++it);
195}
196
Bill Wendling523bea82013-11-08 08:13:15 +0000197//===----------------------------------------------------------------------===//
198// Predicates
199//===----------------------------------------------------------------------===//
200
Manman Renf8a19672014-07-28 22:24:06 +0000201bool DIDescriptor::isSubroutineType() const {
Yaron Kerene8270222014-12-19 22:15:09 +0000202 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
Manman Renf8a19672014-07-28 22:24:06 +0000203}
204
Bill Wendling523bea82013-11-08 08:13:15 +0000205bool DIDescriptor::isBasicType() const {
206 if (!DbgNode)
207 return false;
208 switch (getTag()) {
209 case dwarf::DW_TAG_base_type:
210 case dwarf::DW_TAG_unspecified_type:
211 return true;
212 default:
213 return false;
214 }
215}
216
Bill Wendling523bea82013-11-08 08:13:15 +0000217bool DIDescriptor::isDerivedType() const {
218 if (!DbgNode)
219 return false;
220 switch (getTag()) {
221 case dwarf::DW_TAG_typedef:
222 case dwarf::DW_TAG_pointer_type:
223 case dwarf::DW_TAG_ptr_to_member_type:
224 case dwarf::DW_TAG_reference_type:
225 case dwarf::DW_TAG_rvalue_reference_type:
226 case dwarf::DW_TAG_const_type:
227 case dwarf::DW_TAG_volatile_type:
228 case dwarf::DW_TAG_restrict_type:
229 case dwarf::DW_TAG_member:
230 case dwarf::DW_TAG_inheritance:
231 case dwarf::DW_TAG_friend:
232 return true;
233 default:
234 // CompositeTypes are currently modelled as DerivedTypes.
235 return isCompositeType();
236 }
237}
238
Bill Wendling523bea82013-11-08 08:13:15 +0000239bool DIDescriptor::isCompositeType() const {
240 if (!DbgNode)
241 return false;
242 switch (getTag()) {
243 case dwarf::DW_TAG_array_type:
244 case dwarf::DW_TAG_structure_type:
245 case dwarf::DW_TAG_union_type:
246 case dwarf::DW_TAG_enumeration_type:
247 case dwarf::DW_TAG_subroutine_type:
248 case dwarf::DW_TAG_class_type:
249 return true;
250 default:
251 return false;
252 }
253}
254
Bill Wendling523bea82013-11-08 08:13:15 +0000255bool DIDescriptor::isVariable() const {
256 if (!DbgNode)
257 return false;
258 switch (getTag()) {
259 case dwarf::DW_TAG_auto_variable:
260 case dwarf::DW_TAG_arg_variable:
261 return true;
262 default:
263 return false;
264 }
265}
266
Bill Wendling523bea82013-11-08 08:13:15 +0000267bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000268 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000269}
270
Bill Wendling523bea82013-11-08 08:13:15 +0000271bool DIDescriptor::isSubprogram() const {
272 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
273}
274
Bill Wendling523bea82013-11-08 08:13:15 +0000275bool DIDescriptor::isGlobalVariable() const {
Duncan P. N. Exon Smithb407bb22015-02-09 22:48:04 +0000276 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Bill Wendling523bea82013-11-08 08:13:15 +0000277}
278
Bill Wendling523bea82013-11-08 08:13:15 +0000279bool DIDescriptor::isScope() const {
280 if (!DbgNode)
281 return false;
282 switch (getTag()) {
283 case dwarf::DW_TAG_compile_unit:
284 case dwarf::DW_TAG_lexical_block:
285 case dwarf::DW_TAG_subprogram:
286 case dwarf::DW_TAG_namespace:
287 case dwarf::DW_TAG_file_type:
288 return true;
289 default:
290 break;
291 }
292 return isType();
293}
294
Bill Wendling523bea82013-11-08 08:13:15 +0000295bool DIDescriptor::isTemplateTypeParameter() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
297}
298
Bill Wendling523bea82013-11-08 08:13:15 +0000299bool DIDescriptor::isTemplateValueParameter() const {
300 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
301 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
302 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
303}
304
Bill Wendling523bea82013-11-08 08:13:15 +0000305bool DIDescriptor::isCompileUnit() const {
306 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
307}
308
Bill Wendling523bea82013-11-08 08:13:15 +0000309bool DIDescriptor::isFile() const {
310 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
311}
312
Bill Wendling523bea82013-11-08 08:13:15 +0000313bool DIDescriptor::isNameSpace() const {
314 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
315}
316
Bill Wendling523bea82013-11-08 08:13:15 +0000317bool DIDescriptor::isLexicalBlockFile() const {
318 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000319 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000320}
321
Bill Wendling523bea82013-11-08 08:13:15 +0000322bool DIDescriptor::isLexicalBlock() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000323 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
324 // something relies on this returning true for DILexicalBlockFile.
Bill Wendling523bea82013-11-08 08:13:15 +0000325 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000326 DbgNode->getNumOperands() == 3 &&
327 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000328}
329
Bill Wendling523bea82013-11-08 08:13:15 +0000330bool DIDescriptor::isSubrange() const {
331 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
332}
333
Bill Wendling523bea82013-11-08 08:13:15 +0000334bool DIDescriptor::isEnumerator() const {
335 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
336}
337
Bill Wendling523bea82013-11-08 08:13:15 +0000338bool DIDescriptor::isObjCProperty() const {
339 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
340}
341
Bill Wendling523bea82013-11-08 08:13:15 +0000342bool DIDescriptor::isImportedEntity() const {
343 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
344 getTag() == dwarf::DW_TAG_imported_declaration);
345}
346
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000347bool DIDescriptor::isExpression() const {
348 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
349}
350
Bill Wendling523bea82013-11-08 08:13:15 +0000351//===----------------------------------------------------------------------===//
352// Simple Descriptor Constructors and other Methods
353//===----------------------------------------------------------------------===//
354
Frederic Riss36acf0f2014-09-15 07:50:36 +0000355void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000356
357 assert(DbgNode && "Trying to replace an unverified type!");
358
359 // Since we use a TrackingVH for the node, its easy for clients to manufacture
360 // legitimate situations where they want to replaceAllUsesWith() on something
361 // which, due to uniquing, has merged with the source. We shield clients from
362 // this detail by allowing a value to be replaced with replaceAllUsesWith()
363 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000364 const MDNode *DN = D;
365 if (DbgNode == DN) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000366 SmallVector<Metadata *, 10> Ops(DbgNode->op_begin(), DbgNode->op_end());
David Blaikied3f094a2014-05-06 03:41:57 +0000367 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000368 }
David Blaikied3f094a2014-05-06 03:41:57 +0000369
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000370 assert(DbgNode->isTemporary() && "Expected temporary node");
371 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000372 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
David Blaikied3f094a2014-05-06 03:41:57 +0000373 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000374 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000375}
376
Frederic Riss36acf0f2014-09-15 07:50:36 +0000377void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000378 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000379 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000380 assert(DbgNode->isTemporary() && "Expected temporary node");
381 auto *Node = const_cast<MDNode *>(DbgNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000382 Node->replaceAllUsesWith(D);
David Blaikied3f094a2014-05-06 03:41:57 +0000383 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000384}
385
Bill Wendling523bea82013-11-08 08:13:15 +0000386bool DICompileUnit::Verify() const {
387 if (!isCompileUnit())
388 return false;
389
390 // Don't bother verifying the compilation directory or producer string
391 // as those could be empty.
392 if (getFilename().empty())
393 return false;
394
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000395 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000396}
397
Bill Wendling523bea82013-11-08 08:13:15 +0000398bool DIObjCProperty::Verify() const {
399 if (!isObjCProperty())
400 return false;
401
402 // Don't worry about the rest of the strings for now.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000403 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000404}
405
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000406/// \brief Check if a field at position Elt of a MDNode is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000407static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000408 Metadata *Fld = getField(DbgNode, Elt);
Duncan P. N. Exon Smitha55dcaf2015-02-17 22:34:15 +0000409 return !Fld || isa<MDNode>(Fld);
Bill Wendling523bea82013-11-08 08:13:15 +0000410}
411
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000412/// \brief Check if a field at position Elt of a MDNode is a MDString.
Bill Wendling523bea82013-11-08 08:13:15 +0000413static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000414 Metadata *Fld = getField(DbgNode, Elt);
Bill Wendling523bea82013-11-08 08:13:15 +0000415 return !Fld || isa<MDString>(Fld);
416}
417
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000418/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000419static bool isTypeRef(const Metadata *MD) {
420 if (!MD)
421 return true;
422 if (auto *S = dyn_cast<MDString>(MD))
423 return !S->getString().empty();
424 if (auto *N = dyn_cast<MDNode>(MD))
425 return DIType(N).isType();
426 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000427}
428
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000429/// \brief Check if referenced field might be a type.
Bill Wendling523bea82013-11-08 08:13:15 +0000430static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000431 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000432}
433
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000434/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000435static bool isScopeRef(const Metadata *MD) {
436 if (!MD)
437 return true;
438 if (auto *S = dyn_cast<MDString>(MD))
439 return !S->getString().empty();
Duncan P. N. Exon Smith8551d252015-02-18 19:46:02 +0000440 if (auto *N = dyn_cast<MDNode>(MD))
441 return DIScope(N).isScope();
442 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000443}
444
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000445/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
Bill Wendling523bea82013-11-08 08:13:15 +0000446static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000447 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000448}
449
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000450#ifndef NDEBUG
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000451/// \brief Check if a value can be a DescriptorRef.
452static bool isDescriptorRef(const Metadata *MD) {
453 if (!MD)
454 return true;
455 if (auto *S = dyn_cast<MDString>(MD))
456 return !S->getString().empty();
457 return isa<MDNode>(MD);
458}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000459#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000460
Bill Wendling523bea82013-11-08 08:13:15 +0000461bool DIType::Verify() const {
462 if (!isType())
463 return false;
464 // Make sure Context @ field 2 is MDNode.
465 if (!fieldIsScopeRef(DbgNode, 2))
466 return false;
467
468 // FIXME: Sink this into the various subclass verifies.
469 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000470 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000471 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
472 Tag != dwarf::DW_TAG_ptr_to_member_type &&
473 Tag != dwarf::DW_TAG_reference_type &&
474 Tag != dwarf::DW_TAG_rvalue_reference_type &&
475 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
476 Tag != dwarf::DW_TAG_enumeration_type &&
477 Tag != dwarf::DW_TAG_subroutine_type &&
478 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
479 getFilename().empty())
480 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000481
Bill Wendling523bea82013-11-08 08:13:15 +0000482 // DIType is abstract, it should be a BasicType, a DerivedType or
483 // a CompositeType.
484 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000485 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000486 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000487 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000488 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000489 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000490 else
491 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000492}
493
Bill Wendling523bea82013-11-08 08:13:15 +0000494bool DIBasicType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000495 return isBasicType() && DbgNode->getNumOperands() == 3 &&
496 getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000497}
498
Bill Wendling523bea82013-11-08 08:13:15 +0000499bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000500 // Make sure DerivedFrom @ field 3 is TypeRef.
501 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000502 return false;
503 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000504 // Make sure ClassType @ field 4 is a TypeRef.
505 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000506 return false;
507
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000508 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
509 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
510 getNumHeaderFields() <= 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000511}
512
Bill Wendling523bea82013-11-08 08:13:15 +0000513bool DICompositeType::Verify() const {
514 if (!isCompositeType())
515 return false;
516
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000517 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
518 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000519 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000520 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000521 return false;
522
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000523 // Make sure the type identifier at field 7 is MDString, it can be null.
524 if (!fieldIsMDString(DbgNode, 7))
Bill Wendling523bea82013-11-08 08:13:15 +0000525 return false;
526
Adrian Prantl99c7af22013-12-18 21:48:19 +0000527 // A subroutine type can't be both & and &&.
528 if (isLValueReference() && isRValueReference())
529 return false;
530
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000531 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
Bill Wendling523bea82013-11-08 08:13:15 +0000532}
533
Bill Wendling523bea82013-11-08 08:13:15 +0000534bool DISubprogram::Verify() const {
535 if (!isSubprogram())
536 return false;
537
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000538 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000539 if (!fieldIsScopeRef(DbgNode, 2))
540 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000541 if (!fieldIsMDNode(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000542 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000543 // Containing type @ field 4.
544 if (!fieldIsTypeRef(DbgNode, 4))
Bill Wendling523bea82013-11-08 08:13:15 +0000545 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000546
547 // A subprogram can't be both & and &&.
548 if (isLValueReference() && isRValueReference())
549 return false;
550
David Blaikie3dfe4782014-10-14 18:22:52 +0000551 // If a DISubprogram has an llvm::Function*, then scope chains from all
552 // instructions within the function should lead to this DISubprogram.
553 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000554 for (auto &BB : *F) {
555 for (auto &I : BB) {
556 DebugLoc DL = I.getDebugLoc();
557 if (DL.isUnknown())
558 continue;
559
560 MDNode *Scope = nullptr;
561 MDNode *IA = nullptr;
562 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000563 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000564 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000565 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000566 if (!Scope)
567 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000568 assert(!IA);
569 while (!DIDescriptor(Scope).isSubprogram()) {
570 DILexicalBlockFile D(Scope);
571 Scope = D.isLexicalBlockFile()
572 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000573 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000574 if (!Scope)
575 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000576 }
577 if (!DISubprogram(Scope).describes(F))
578 return false;
579 }
580 }
581 }
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000582 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
Bill Wendling523bea82013-11-08 08:13:15 +0000583}
584
Bill Wendling523bea82013-11-08 08:13:15 +0000585bool DIGlobalVariable::Verify() const {
586 if (!isGlobalVariable())
587 return false;
588
589 if (getDisplayName().empty())
590 return false;
Manman Renf0a582b2014-11-21 19:55:23 +0000591 // Make sure context @ field 1 is an MDNode.
592 if (!fieldIsMDNode(DbgNode, 1))
Bill Wendling523bea82013-11-08 08:13:15 +0000593 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000594 // Make sure that type @ field 3 is a DITypeRef.
595 if (!fieldIsTypeRef(DbgNode, 3))
Bill Wendling523bea82013-11-08 08:13:15 +0000596 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000597 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
598 if (!fieldIsMDNode(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000599 return false;
600
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000601 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
Bill Wendling523bea82013-11-08 08:13:15 +0000602}
603
Bill Wendling523bea82013-11-08 08:13:15 +0000604bool DIVariable::Verify() const {
605 if (!isVariable())
606 return false;
607
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000608 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000609 if (!fieldIsMDNode(DbgNode, 1))
610 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000611 // Make sure that type @ field 3 is a DITypeRef.
612 if (!fieldIsTypeRef(DbgNode, 3))
613 return false;
614
615 // Check the number of header fields, which is common between complex and
616 // simple variables.
617 if (getNumHeaderFields() != 4)
Bill Wendling523bea82013-11-08 08:13:15 +0000618 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000619
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000620 // Variable without an inline location.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000621 if (DbgNode->getNumOperands() == 4)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000622 return true;
623
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000624 // Variable with an inline location.
625 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000626}
627
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000628bool DIExpression::Verify() const {
629 // Empty DIExpressions may be represented as a nullptr.
630 if (!DbgNode)
631 return true;
632
Adrian Prantl9260cca2015-01-22 00:00:52 +0000633 if (!(isExpression() && DbgNode->getNumOperands() == 1))
634 return false;
635
Adrian Prantl70f2a732015-01-23 23:40:47 +0000636 for (auto Op : *this)
637 switch (Op) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000638 case DW_OP_bit_piece:
Adrian Prantl9260cca2015-01-22 00:00:52 +0000639 // Must be the last element of the expression.
Adrian Prantl70f2a732015-01-23 23:40:47 +0000640 return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000641 case DW_OP_plus:
Adrian Prantl70f2a732015-01-23 23:40:47 +0000642 if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000643 return false;
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000644 break;
Adrian Prantl9260cca2015-01-22 00:00:52 +0000645 case DW_OP_deref:
646 break;
647 default:
648 // Other operators are not yet supported by the backend.
649 return false;
650 }
651 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000652}
653
Bill Wendling523bea82013-11-08 08:13:15 +0000654bool DILocation::Verify() const {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000655 return DbgNode && isa<MDLocation>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000656}
657
Bill Wendling523bea82013-11-08 08:13:15 +0000658bool DINameSpace::Verify() const {
659 if (!isNameSpace())
660 return false;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000661 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000662}
663
Bill Wendling523bea82013-11-08 08:13:15 +0000664MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
665
Bill Wendling523bea82013-11-08 08:13:15 +0000666bool DIFile::Verify() const {
667 return isFile() && DbgNode->getNumOperands() == 2;
668}
669
Bill Wendling523bea82013-11-08 08:13:15 +0000670bool DIEnumerator::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000671 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
672 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000673}
674
Bill Wendling523bea82013-11-08 08:13:15 +0000675bool DISubrange::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000676 return isSubrange() && DbgNode->getNumOperands() == 1 &&
677 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000678}
679
Bill Wendling523bea82013-11-08 08:13:15 +0000680bool DILexicalBlock::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000681 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
682 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000683}
684
Bill Wendling523bea82013-11-08 08:13:15 +0000685bool DILexicalBlockFile::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000686 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
687 getNumHeaderFields() == 2;
Bill Wendling523bea82013-11-08 08:13:15 +0000688}
689
Bill Wendling523bea82013-11-08 08:13:15 +0000690bool DITemplateTypeParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000691 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
692 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000693}
694
Bill Wendling523bea82013-11-08 08:13:15 +0000695bool DITemplateValueParameter::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000696 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
697 getNumHeaderFields() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000698}
699
Bill Wendling523bea82013-11-08 08:13:15 +0000700bool DIImportedEntity::Verify() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000701 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
702 getNumHeaderFields() == 3;
Bill Wendling523bea82013-11-08 08:13:15 +0000703}
704
Bill Wendling523bea82013-11-08 08:13:15 +0000705MDNode *DIDerivedType::getObjCProperty() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000706 return getNodeField(DbgNode, 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000707}
708
709MDString *DICompositeType::getIdentifier() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000710 return cast_or_null<MDString>(getField(DbgNode, 7));
Bill Wendling523bea82013-11-08 08:13:15 +0000711}
712
713#ifndef NDEBUG
714static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
715 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
716 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000717 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
Bill Wendling523bea82013-11-08 08:13:15 +0000718 continue;
719 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
720 bool found = false;
721 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
Hans Wennborge242e8b2014-12-09 20:39:15 +0000722 found = (E == cast<MDNode>(RHS->getOperand(j)));
Bill Wendling523bea82013-11-08 08:13:15 +0000723 assert(found && "Losing a member during member list replacement");
724 }
725}
726#endif
727
Manman Ren1a125c92014-07-28 19:33:20 +0000728void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000729 TrackingMDNodeRef N(*this);
Bill Wendling523bea82013-11-08 08:13:15 +0000730 if (Elements) {
731#ifndef NDEBUG
732 // Check that the new list of members contains all the old members as well.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000733 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
Bill Wendling523bea82013-11-08 08:13:15 +0000734 VerifySubsetOf(El, Elements);
735#endif
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000736 N->replaceOperandWith(4, Elements);
Bill Wendling523bea82013-11-08 08:13:15 +0000737 }
738 if (TParams)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000739 N->replaceOperandWith(6, TParams);
Bill Wendling523bea82013-11-08 08:13:15 +0000740 DbgNode = N;
741}
742
Bill Wendling523bea82013-11-08 08:13:15 +0000743DIScopeRef DIScope::getRef() const {
744 if (!isCompositeType())
745 return DIScopeRef(*this);
746 DICompositeType DTy(DbgNode);
747 if (!DTy.getIdentifier())
748 return DIScopeRef(*this);
749 return DIScopeRef(DTy.getIdentifier());
750}
751
Bill Wendling523bea82013-11-08 08:13:15 +0000752void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000753 TrackingMDNodeRef N(*this);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000754 N->replaceOperandWith(5, ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000755 DbgNode = N;
756}
757
Bill Wendling523bea82013-11-08 08:13:15 +0000758bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
759 assert(CurFn && "Invalid function");
760 if (!getContext().isSubprogram())
761 return false;
762 // This variable is not inlined function argument if its scope
763 // does not describe current function.
764 return !DISubprogram(getContext()).describes(CurFn);
765}
766
Bill Wendling523bea82013-11-08 08:13:15 +0000767bool DISubprogram::describes(const Function *F) {
768 assert(F && "Invalid function");
769 if (F == getFunction())
770 return true;
771 StringRef Name = getLinkageName();
772 if (Name.empty())
773 Name = getName();
774 if (F->getName() == Name)
775 return true;
776 return false;
777}
778
Bill Wendling523bea82013-11-08 08:13:15 +0000779MDNode *DISubprogram::getVariablesNodes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000780 return getNodeField(DbgNode, 8);
Bill Wendling523bea82013-11-08 08:13:15 +0000781}
782
783DIArray DISubprogram::getVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000784 return DIArray(getNodeField(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000785}
786
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000787Metadata *DITemplateValueParameter::getValue() const {
788 return DbgNode->getOperand(3);
Bill Wendling523bea82013-11-08 08:13:15 +0000789}
790
Bill Wendling523bea82013-11-08 08:13:15 +0000791DIScopeRef DIScope::getContext() const {
792
793 if (isType())
794 return DIType(DbgNode).getContext();
795
796 if (isSubprogram())
797 return DIScopeRef(DISubprogram(DbgNode).getContext());
798
799 if (isLexicalBlock())
800 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
801
802 if (isLexicalBlockFile())
803 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
804
805 if (isNameSpace())
806 return DIScopeRef(DINameSpace(DbgNode).getContext());
807
808 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000809 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000810}
811
Bill Wendling523bea82013-11-08 08:13:15 +0000812StringRef DIScope::getName() const {
813 if (isType())
814 return DIType(DbgNode).getName();
815 if (isSubprogram())
816 return DISubprogram(DbgNode).getName();
817 if (isNameSpace())
818 return DINameSpace(DbgNode).getName();
819 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
820 isCompileUnit()) &&
821 "Unhandled type of scope.");
822 return StringRef();
823}
824
825StringRef DIScope::getFilename() const {
826 if (!DbgNode)
827 return StringRef();
828 return ::getStringField(getNodeField(DbgNode, 1), 0);
829}
830
831StringRef DIScope::getDirectory() const {
832 if (!DbgNode)
833 return StringRef();
834 return ::getStringField(getNodeField(DbgNode, 1), 1);
835}
836
837DIArray DICompileUnit::getEnumTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000838 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000839 return DIArray();
840
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000841 return DIArray(getNodeField(DbgNode, 2));
Bill Wendling523bea82013-11-08 08:13:15 +0000842}
843
844DIArray DICompileUnit::getRetainedTypes() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000845 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000846 return DIArray();
847
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000848 return DIArray(getNodeField(DbgNode, 3));
Bill Wendling523bea82013-11-08 08:13:15 +0000849}
850
851DIArray DICompileUnit::getSubprograms() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000852 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000853 return DIArray();
854
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000855 return DIArray(getNodeField(DbgNode, 4));
Bill Wendling523bea82013-11-08 08:13:15 +0000856}
857
858DIArray DICompileUnit::getGlobalVariables() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000859 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000860 return DIArray();
861
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000862 return DIArray(getNodeField(DbgNode, 5));
Bill Wendling523bea82013-11-08 08:13:15 +0000863}
864
865DIArray DICompileUnit::getImportedEntities() const {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000866 if (!DbgNode || DbgNode->getNumOperands() < 7)
Bill Wendling523bea82013-11-08 08:13:15 +0000867 return DIArray();
868
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000869 return DIArray(getNodeField(DbgNode, 6));
870}
871
872void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
873 assert(Verify() && "Expected compile unit");
874 if (Subprograms == getSubprograms())
875 return;
876
877 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
878}
879
880void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
881 assert(Verify() && "Expected compile unit");
882 if (GlobalVariables == getGlobalVariables())
883 return;
884
885 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
Bill Wendling523bea82013-11-08 08:13:15 +0000886}
887
Diego Novillof5041ce2014-03-03 20:06:11 +0000888DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000889 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000890 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000891 assert(NewScope && "Expected valid scope");
892
893 const auto *Old = cast<MDLocation>(DbgNode);
894 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
895 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000896}
897
Diego Novillof5041ce2014-03-03 20:06:11 +0000898unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
899 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
900 return ++Ctx.pImpl->DiscriminatorTable[Key];
901}
902
Bill Wendling523bea82013-11-08 08:13:15 +0000903DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
904 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000905 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
906 if (!InlinedScope)
907 return cleanseInlinedVariable(DV, VMContext);
908
909 // Insert inlined scope.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000910 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
911 DV->op_begin() + DIVariableInlinedAtIndex);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000912 Elts.push_back(InlinedScope);
913
914 DIVariable Inlined(MDNode::get(VMContext, Elts));
915 assert(Inlined.Verify() && "Expected to create a DIVariable");
916 return Inlined;
Bill Wendling523bea82013-11-08 08:13:15 +0000917}
918
Bill Wendling523bea82013-11-08 08:13:15 +0000919DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000920 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
921 if (!DIVariable(DV).getInlinedAt())
922 return DIVariable(DV);
923
924 // Remove inlined scope.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000925 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
926 DV->op_begin() + DIVariableInlinedAtIndex);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000927
928 DIVariable Cleansed(MDNode::get(VMContext, Elts));
929 assert(Cleansed.Verify() && "Expected to create a DIVariable");
930 return Cleansed;
Bill Wendling523bea82013-11-08 08:13:15 +0000931}
932
Bill Wendling523bea82013-11-08 08:13:15 +0000933DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
934 DIDescriptor D(Scope);
935 if (D.isSubprogram())
936 return DISubprogram(Scope);
937
938 if (D.isLexicalBlockFile())
939 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
940
941 if (D.isLexicalBlock())
942 return getDISubprogram(DILexicalBlock(Scope).getContext());
943
944 return DISubprogram();
945}
946
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000947DISubprogram llvm::getDISubprogram(const Function *F) {
948 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000949 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000950 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
951 return !Inst.getDebugLoc().isUnknown();
952 });
953 if (Inst == BB.end())
954 continue;
955 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000956 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000957 DISubprogram Subprogram = getDISubprogram(Scope);
958 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000959 }
960
961 return DISubprogram();
962}
963
Bill Wendling523bea82013-11-08 08:13:15 +0000964DICompositeType llvm::getDICompositeType(DIType T) {
965 if (T.isCompositeType())
966 return DICompositeType(T);
967
968 if (T.isDerivedType()) {
969 // This function is currently used by dragonegg and dragonegg does
970 // not generate identifier for types, so using an empty map to resolve
971 // DerivedFrom should be fine.
972 DITypeIdentifierMap EmptyMap;
973 return getDICompositeType(
974 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
975 }
976
977 return DICompositeType();
978}
979
Bill Wendling523bea82013-11-08 08:13:15 +0000980DITypeIdentifierMap
981llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
982 DITypeIdentifierMap Map;
983 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000984 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000985 DIArray Retain = CU.getRetainedTypes();
986 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
987 if (!Retain.getElement(Ti).isCompositeType())
988 continue;
989 DICompositeType Ty(Retain.getElement(Ti));
990 if (MDString *TypeId = Ty.getIdentifier()) {
991 // Definition has priority over declaration.
992 // Try to insert (TypeId, Ty) to Map.
993 std::pair<DITypeIdentifierMap::iterator, bool> P =
994 Map.insert(std::make_pair(TypeId, Ty));
995 // If TypeId already exists in Map and this is a definition, replace
996 // whatever we had (declaration or definition) with the definition.
997 if (!P.second && !Ty.isForwardDecl())
998 P.first->second = Ty;
999 }
1000 }
1001 }
1002 return Map;
1003}
1004
1005//===----------------------------------------------------------------------===//
1006// DebugInfoFinder implementations.
1007//===----------------------------------------------------------------------===//
1008
1009void DebugInfoFinder::reset() {
1010 CUs.clear();
1011 SPs.clear();
1012 GVs.clear();
1013 TYs.clear();
1014 Scopes.clear();
1015 NodesSeen.clear();
1016 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +00001017 TypeMapInitialized = false;
1018}
1019
Manman Renb46e5502013-11-17 19:35:03 +00001020void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001021 if (!TypeMapInitialized)
1022 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1023 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1024 TypeMapInitialized = true;
1025 }
Bill Wendling523bea82013-11-08 08:13:15 +00001026}
1027
Bill Wendling523bea82013-11-08 08:13:15 +00001028void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001029 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001030 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001031 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001032 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +00001033 addCompileUnit(CU);
1034 DIArray GVs = CU.getGlobalVariables();
1035 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1036 DIGlobalVariable DIG(GVs.getElement(i));
1037 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +00001038 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001039 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001040 }
1041 }
1042 DIArray SPs = CU.getSubprograms();
1043 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1044 processSubprogram(DISubprogram(SPs.getElement(i)));
1045 DIArray EnumTypes = CU.getEnumTypes();
1046 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1047 processType(DIType(EnumTypes.getElement(i)));
1048 DIArray RetainedTypes = CU.getRetainedTypes();
1049 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1050 processType(DIType(RetainedTypes.getElement(i)));
1051 DIArray Imports = CU.getImportedEntities();
1052 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1053 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001054 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001055 if (Entity.isType())
1056 processType(DIType(Entity));
1057 else if (Entity.isSubprogram())
1058 processSubprogram(DISubprogram(Entity));
1059 else if (Entity.isNameSpace())
1060 processScope(DINameSpace(Entity).getContext());
1061 }
1062 }
1063 }
1064}
1065
Manman Ren2085ccc2013-11-17 18:42:37 +00001066void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001067 if (!Loc)
1068 return;
Manman Renb46e5502013-11-17 19:35:03 +00001069 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001070 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001071 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001072}
1073
Bill Wendling523bea82013-11-08 08:13:15 +00001074void DebugInfoFinder::processType(DIType DT) {
1075 if (!addType(DT))
1076 return;
1077 processScope(DT.getContext().resolve(TypeIdentifierMap));
1078 if (DT.isCompositeType()) {
1079 DICompositeType DCT(DT);
1080 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001081 if (DT.isSubroutineType()) {
1082 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1083 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1084 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1085 return;
1086 }
Manman Renab8ffba2014-07-28 19:14:13 +00001087 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001088 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1089 DIDescriptor D = DA.getElement(i);
1090 if (D.isType())
1091 processType(DIType(D));
1092 else if (D.isSubprogram())
1093 processSubprogram(DISubprogram(D));
1094 }
1095 } else if (DT.isDerivedType()) {
1096 DIDerivedType DDT(DT);
1097 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1098 }
1099}
1100
1101void DebugInfoFinder::processScope(DIScope Scope) {
1102 if (Scope.isType()) {
1103 DIType Ty(Scope);
1104 processType(Ty);
1105 return;
1106 }
1107 if (Scope.isCompileUnit()) {
1108 addCompileUnit(DICompileUnit(Scope));
1109 return;
1110 }
1111 if (Scope.isSubprogram()) {
1112 processSubprogram(DISubprogram(Scope));
1113 return;
1114 }
1115 if (!addScope(Scope))
1116 return;
1117 if (Scope.isLexicalBlock()) {
1118 DILexicalBlock LB(Scope);
1119 processScope(LB.getContext());
1120 } else if (Scope.isLexicalBlockFile()) {
1121 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1122 processScope(LBF.getScope());
1123 } else if (Scope.isNameSpace()) {
1124 DINameSpace NS(Scope);
1125 processScope(NS.getContext());
1126 }
1127}
1128
Bill Wendling523bea82013-11-08 08:13:15 +00001129void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1130 if (!addSubprogram(SP))
1131 return;
1132 processScope(SP.getContext().resolve(TypeIdentifierMap));
1133 processType(SP.getType());
1134 DIArray TParams = SP.getTemplateParams();
1135 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1136 DIDescriptor Element = TParams.getElement(I);
1137 if (Element.isTemplateTypeParameter()) {
1138 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +00001139 processType(TType.getType().resolve(TypeIdentifierMap));
1140 } else if (Element.isTemplateValueParameter()) {
1141 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +00001142 processType(TVal.getType().resolve(TypeIdentifierMap));
1143 }
1144 }
1145}
1146
Manman Ren2085ccc2013-11-17 18:42:37 +00001147void DebugInfoFinder::processDeclare(const Module &M,
1148 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001149 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1150 if (!N)
1151 return;
Manman Renb46e5502013-11-17 19:35:03 +00001152 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001153
1154 DIDescriptor DV(N);
1155 if (!DV.isVariable())
1156 return;
1157
David Blaikie70573dc2014-11-19 07:49:26 +00001158 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001159 return;
1160 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001161 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001162}
1163
Manman Ren2085ccc2013-11-17 18:42:37 +00001164void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001165 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1166 if (!N)
1167 return;
Manman Renb46e5502013-11-17 19:35:03 +00001168 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001169
1170 DIDescriptor DV(N);
1171 if (!DV.isVariable())
1172 return;
1173
David Blaikie70573dc2014-11-19 07:49:26 +00001174 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001175 return;
1176 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001177 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001178}
1179
Bill Wendling523bea82013-11-08 08:13:15 +00001180bool DebugInfoFinder::addType(DIType DT) {
1181 if (!DT)
1182 return false;
1183
David Blaikie70573dc2014-11-19 07:49:26 +00001184 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001185 return false;
1186
1187 TYs.push_back(DT);
1188 return true;
1189}
1190
Bill Wendling523bea82013-11-08 08:13:15 +00001191bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1192 if (!CU)
1193 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001194 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001195 return false;
1196
1197 CUs.push_back(CU);
1198 return true;
1199}
1200
Bill Wendling523bea82013-11-08 08:13:15 +00001201bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1202 if (!DIG)
1203 return false;
1204
David Blaikie70573dc2014-11-19 07:49:26 +00001205 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001206 return false;
1207
1208 GVs.push_back(DIG);
1209 return true;
1210}
1211
Bill Wendling523bea82013-11-08 08:13:15 +00001212bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1213 if (!SP)
1214 return false;
1215
David Blaikie70573dc2014-11-19 07:49:26 +00001216 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001217 return false;
1218
1219 SPs.push_back(SP);
1220 return true;
1221}
1222
1223bool DebugInfoFinder::addScope(DIScope Scope) {
1224 if (!Scope)
1225 return false;
1226 // FIXME: Ocaml binding generates a scope with no content, we treat it
1227 // as null for now.
1228 if (Scope->getNumOperands() == 0)
1229 return false;
David Blaikie70573dc2014-11-19 07:49:26 +00001230 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +00001231 return false;
1232 Scopes.push_back(Scope);
1233 return true;
1234}
1235
1236//===----------------------------------------------------------------------===//
1237// DIDescriptor: dump routines for all descriptors.
1238//===----------------------------------------------------------------------===//
1239
Bill Wendling523bea82013-11-08 08:13:15 +00001240void DIDescriptor::dump() const {
1241 print(dbgs());
1242 dbgs() << '\n';
1243}
1244
Bill Wendling523bea82013-11-08 08:13:15 +00001245void DIDescriptor::print(raw_ostream &OS) const {
1246 if (!DbgNode)
1247 return;
1248
1249 if (const char *Tag = dwarf::TagString(getTag()))
1250 OS << "[ " << Tag << " ]";
1251
1252 if (this->isSubrange()) {
1253 DISubrange(DbgNode).printInternal(OS);
1254 } else if (this->isCompileUnit()) {
1255 DICompileUnit(DbgNode).printInternal(OS);
1256 } else if (this->isFile()) {
1257 DIFile(DbgNode).printInternal(OS);
1258 } else if (this->isEnumerator()) {
1259 DIEnumerator(DbgNode).printInternal(OS);
1260 } else if (this->isBasicType()) {
1261 DIType(DbgNode).printInternal(OS);
1262 } else if (this->isDerivedType()) {
1263 DIDerivedType(DbgNode).printInternal(OS);
1264 } else if (this->isCompositeType()) {
1265 DICompositeType(DbgNode).printInternal(OS);
1266 } else if (this->isSubprogram()) {
1267 DISubprogram(DbgNode).printInternal(OS);
1268 } else if (this->isGlobalVariable()) {
1269 DIGlobalVariable(DbgNode).printInternal(OS);
1270 } else if (this->isVariable()) {
1271 DIVariable(DbgNode).printInternal(OS);
1272 } else if (this->isObjCProperty()) {
1273 DIObjCProperty(DbgNode).printInternal(OS);
1274 } else if (this->isNameSpace()) {
1275 DINameSpace(DbgNode).printInternal(OS);
1276 } else if (this->isScope()) {
1277 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001278 } else if (this->isExpression()) {
1279 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001280 }
1281}
1282
1283void DISubrange::printInternal(raw_ostream &OS) const {
1284 int64_t Count = getCount();
1285 if (Count != -1)
1286 OS << " [" << getLo() << ", " << Count - 1 << ']';
1287 else
1288 OS << " [unbounded]";
1289}
1290
1291void DIScope::printInternal(raw_ostream &OS) const {
1292 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1293}
1294
1295void DICompileUnit::printInternal(raw_ostream &OS) const {
1296 DIScope::printInternal(OS);
1297 OS << " [";
1298 unsigned Lang = getLanguage();
1299 if (const char *LangStr = dwarf::LanguageString(Lang))
1300 OS << LangStr;
1301 else
1302 (OS << "lang 0x").write_hex(Lang);
1303 OS << ']';
1304}
1305
1306void DIEnumerator::printInternal(raw_ostream &OS) const {
1307 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1308}
1309
1310void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001311 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001312 return;
1313
1314 StringRef Res = getName();
1315 if (!Res.empty())
1316 OS << " [" << Res << "]";
1317
1318 // TODO: Print context?
1319
1320 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1321 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1322 if (isBasicType())
1323 if (const char *Enc =
1324 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1325 OS << ", enc " << Enc;
1326 OS << "]";
1327
1328 if (isPrivate())
1329 OS << " [private]";
1330 else if (isProtected())
1331 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001332 else if (isPublic())
1333 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001334
1335 if (isArtificial())
1336 OS << " [artificial]";
1337
1338 if (isForwardDecl())
1339 OS << " [decl]";
1340 else if (getTag() == dwarf::DW_TAG_structure_type ||
1341 getTag() == dwarf::DW_TAG_union_type ||
1342 getTag() == dwarf::DW_TAG_enumeration_type ||
1343 getTag() == dwarf::DW_TAG_class_type)
1344 OS << " [def]";
1345 if (isVector())
1346 OS << " [vector]";
1347 if (isStaticMember())
1348 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001349
1350 if (isLValueReference())
1351 OS << " [reference]";
1352
1353 if (isRValueReference())
1354 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001355}
1356
1357void DIDerivedType::printInternal(raw_ostream &OS) const {
1358 DIType::printInternal(OS);
1359 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1360}
1361
1362void DICompositeType::printInternal(raw_ostream &OS) const {
1363 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001364 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001365 OS << " [" << A.getNumElements() << " elements]";
1366}
1367
1368void DINameSpace::printInternal(raw_ostream &OS) const {
1369 StringRef Name = getName();
1370 if (!Name.empty())
1371 OS << " [" << Name << ']';
1372
1373 OS << " [line " << getLineNumber() << ']';
1374}
1375
1376void DISubprogram::printInternal(raw_ostream &OS) const {
1377 // TODO : Print context
1378 OS << " [line " << getLineNumber() << ']';
1379
1380 if (isLocalToUnit())
1381 OS << " [local]";
1382
1383 if (isDefinition())
1384 OS << " [def]";
1385
1386 if (getScopeLineNumber() != getLineNumber())
1387 OS << " [scope " << getScopeLineNumber() << "]";
1388
1389 if (isPrivate())
1390 OS << " [private]";
1391 else if (isProtected())
1392 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001393 else if (isPublic())
1394 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001395
Adrian Prantl99c7af22013-12-18 21:48:19 +00001396 if (isLValueReference())
1397 OS << " [reference]";
1398
1399 if (isRValueReference())
1400 OS << " [rvalue reference]";
1401
Bill Wendling523bea82013-11-08 08:13:15 +00001402 StringRef Res = getName();
1403 if (!Res.empty())
1404 OS << " [" << Res << ']';
1405}
1406
1407void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1408 StringRef Res = getName();
1409 if (!Res.empty())
1410 OS << " [" << Res << ']';
1411
1412 OS << " [line " << getLineNumber() << ']';
1413
1414 // TODO : Print context
1415
1416 if (isLocalToUnit())
1417 OS << " [local]";
1418
1419 if (isDefinition())
1420 OS << " [def]";
1421}
1422
1423void DIVariable::printInternal(raw_ostream &OS) const {
1424 StringRef Res = getName();
1425 if (!Res.empty())
1426 OS << " [" << Res << ']';
1427
1428 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001429}
Adrian Prantlb1416832014-08-01 22:11:58 +00001430
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001431void DIExpression::printInternal(raw_ostream &OS) const {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001432 for (auto Op : *this) {
1433 OS << " [" << OperationEncodingString(Op);
1434 switch (Op) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001435 case DW_OP_plus: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001436 OS << " " << Op.getArg(1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001437 break;
1438 }
Adrian Prantl27bd01f2015-02-09 23:57:15 +00001439 case DW_OP_bit_piece: {
Adrian Prantl4bd0a0c2015-01-23 21:24:41 +00001440 OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001441 break;
1442 }
Adrian Prantlb9a88e22014-12-05 18:19:38 +00001443 case DW_OP_deref:
1444 // No arguments.
1445 break;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001446 default:
Adrian Prantl0d7d8e42015-01-22 16:55:22 +00001447 llvm_unreachable("unhandled operation");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001448 }
1449 OS << "]";
1450 }
Bill Wendling523bea82013-11-08 08:13:15 +00001451}
1452
1453void DIObjCProperty::printInternal(raw_ostream &OS) const {
1454 StringRef Name = getObjCPropertyName();
1455 if (!Name.empty())
1456 OS << " [" << Name << ']';
1457
1458 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1459 << ']';
1460}
1461
1462static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1463 const LLVMContext &Ctx) {
1464 if (!DL.isUnknown()) { // Print source line info.
1465 DIScope Scope(DL.getScope(Ctx));
1466 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1467 // Omit the directory, because it's likely to be long and uninteresting.
1468 CommentOS << Scope.getFilename();
1469 CommentOS << ':' << DL.getLine();
1470 if (DL.getCol() != 0)
1471 CommentOS << ':' << DL.getCol();
1472 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1473 if (!InlinedAtDL.isUnknown()) {
1474 CommentOS << " @[ ";
1475 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1476 CommentOS << " ]";
1477 }
1478 }
1479}
1480
1481void DIVariable::printExtendedName(raw_ostream &OS) const {
1482 const LLVMContext &Ctx = DbgNode->getContext();
1483 StringRef Res = getName();
1484 if (!Res.empty())
1485 OS << Res << "," << getLineNumber();
1486 if (MDNode *InlinedAt = getInlinedAt()) {
1487 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1488 if (!InlinedAtDL.isUnknown()) {
1489 OS << " @[";
1490 printDebugLoc(InlinedAtDL, OS, Ctx);
1491 OS << "]";
1492 }
1493 }
1494}
1495
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +00001496template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
1497 assert(isDescriptorRef(V) &&
1498 "DIDescriptorRef should be a MDString or MDNode");
1499}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001500template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001501 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1502}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001503template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +00001504 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1505}
1506
Bill Wendling523bea82013-11-08 08:13:15 +00001507template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +00001508DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
1509 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1510}
1511template <>
Bill Wendling523bea82013-11-08 08:13:15 +00001512DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001513 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001514}
Bill Wendling523bea82013-11-08 08:13:15 +00001515template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +00001516 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +00001517}
Manman Rencb14bbc2013-11-22 22:06:31 +00001518
Manman Rencb14bbc2013-11-22 22:06:31 +00001519bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +00001520 bool Changed = false;
1521
1522 // Remove all of the calls to the debugger intrinsics, and remove them from
1523 // the module.
1524 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1525 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001526 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001527 CI->eraseFromParent();
1528 }
1529 Declare->eraseFromParent();
1530 Changed = true;
1531 }
1532
1533 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1534 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001535 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001536 CI->eraseFromParent();
1537 }
1538 DbgVal->eraseFromParent();
1539 Changed = true;
1540 }
1541
1542 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1543 NME = M.named_metadata_end(); NMI != NME;) {
1544 NamedMDNode *NMD = NMI;
1545 ++NMI;
1546 if (NMD->getName().startswith("llvm.dbg.")) {
1547 NMD->eraseFromParent();
1548 Changed = true;
1549 }
1550 }
1551
1552 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1553 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1554 ++FI)
1555 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1556 ++BI) {
1557 if (!BI->getDebugLoc().isUnknown()) {
1558 Changed = true;
1559 BI->setDebugLoc(DebugLoc());
1560 }
1561 }
1562
1563 return Changed;
1564}
Manman Ren8b4306c2013-12-02 21:29:56 +00001565
Manman Renbd4daf82013-12-03 00:12:14 +00001566unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +00001567 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001568 M.getModuleFlag("Debug Info Version")))
1569 return Val->getZExtValue();
1570 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001571}
David Blaikie6876b3b2014-07-01 20:05:26 +00001572
David Blaikiea8c35092014-07-02 18:30:05 +00001573llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1574llvm::makeSubprogramMap(const Module &M) {
1575 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001576
1577 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1578 if (!CU_Nodes)
1579 return R;
1580
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001581 for (MDNode *N : CU_Nodes->operands()) {
1582 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001583 DIArray SPs = CUNode.getSubprograms();
1584 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1585 DISubprogram SP(SPs.getElement(i));
1586 if (Function *F = SP.getFunction())
1587 R.insert(std::make_pair(F, SP));
1588 }
1589 }
1590 return R;
1591}