Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1 | //===--- 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 Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 17 | #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 Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DIBuilder.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 23 | #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 Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 28 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | using namespace llvm; |
| 33 | using namespace llvm::dwarf; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // DIDescriptor |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | bool 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 Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 52 | DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 55 | static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 56 | if (!DbgNode || Elt >= DbgNode->getNumOperands()) |
| 57 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 58 | return DbgNode->getOperand(Elt); |
| 59 | } |
| 60 | |
| 61 | static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { |
| 62 | return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); |
| 63 | } |
| 64 | |
| 65 | static 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 | |
| 71 | StringRef DIDescriptor::getStringField(unsigned Elt) const { |
| 72 | return ::getStringField(DbgNode, Elt); |
| 73 | } |
| 74 | |
| 75 | uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 76 | if (auto *C = getConstantField(Elt)) |
| 77 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 78 | return CI->getZExtValue(); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int64_t DIDescriptor::getInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 84 | if (auto *C = getConstantField(Elt)) |
| 85 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 86 | return CI->getZExtValue(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { |
| 92 | MDNode *Field = getNodeField(DbgNode, Elt); |
| 93 | return DIDescriptor(Field); |
| 94 | } |
| 95 | |
| 96 | GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 97 | return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Constant *DIDescriptor::getConstantField(unsigned Elt) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 101 | if (!DbgNode) |
| 102 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 103 | |
| 104 | if (Elt < DbgNode->getNumOperands()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 105 | if (auto *C = |
| 106 | dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt))) |
| 107 | return C->getValue(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 108 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | Function *DIDescriptor::getFunctionField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 112 | return dyn_cast_or_null<Function>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 116 | if (!DbgNode) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 117 | return; |
| 118 | |
| 119 | if (Elt < DbgNode->getNumOperands()) { |
| 120 | MDNode *Node = const_cast<MDNode *>(DbgNode); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 121 | Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 125 | static unsigned DIVariableInlinedAtIndex = 4; |
| 126 | MDNode *DIVariable::getInlinedAt() const { |
| 127 | return getNodeField(DbgNode, DIVariableInlinedAtIndex); |
| 128 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 129 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 130 | /// \brief Return the size reported by the variable's type. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 131 | unsigned 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 Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 143 | uint64_t DIExpression::getElement(unsigned Idx) const { |
| 144 | unsigned I = Idx + 1; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 145 | assert(I < getNumHeaderFields() && |
| 146 | "non-existing complex address element requested"); |
| 147 | return getHeaderFieldAs<int64_t>(I); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 148 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 149 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 150 | bool DIExpression::isBitPiece() const { |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 151 | unsigned N = getNumElements(); |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 152 | return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 155 | uint64_t DIExpression::getBitPieceOffset() const { |
| 156 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 157 | return getElement(getNumElements()-2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 160 | uint64_t DIExpression::getBitPieceSize() const { |
| 161 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 162 | return getElement(getNumElements()-1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 163 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 164 | |
Adrian Prantl | 2585a98 | 2015-01-22 16:55:20 +0000 | [diff] [blame] | 165 | DIExpression::iterator DIExpression::begin() const { |
| 166 | return DIExpression::iterator(*this); |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Adrian Prantl | 2585a98 | 2015-01-22 16:55:20 +0000 | [diff] [blame] | 169 | DIExpression::iterator DIExpression::end() const { |
| 170 | return DIExpression::iterator(); |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Benjamin Kramer | 4d50b6c | 2015-01-24 19:55:23 +0000 | [diff] [blame] | 173 | DIExpression::Operand DIExpression::Operand::getNext() const { |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 174 | iterator it(I); |
| 175 | return *(++it); |
| 176 | } |
| 177 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 178 | //===----------------------------------------------------------------------===// |
| 179 | // Predicates |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 182 | bool DIDescriptor::isSubroutineType() const { |
Yaron Keren | e827022 | 2014-12-19 22:15:09 +0000 | [diff] [blame] | 183 | return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type; |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 186 | bool DIDescriptor::isBasicType() const { |
| 187 | if (!DbgNode) |
| 188 | return false; |
| 189 | switch (getTag()) { |
| 190 | case dwarf::DW_TAG_base_type: |
| 191 | case dwarf::DW_TAG_unspecified_type: |
| 192 | return true; |
| 193 | default: |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 198 | bool DIDescriptor::isDerivedType() const { |
| 199 | if (!DbgNode) |
| 200 | return false; |
| 201 | switch (getTag()) { |
| 202 | case dwarf::DW_TAG_typedef: |
| 203 | case dwarf::DW_TAG_pointer_type: |
| 204 | case dwarf::DW_TAG_ptr_to_member_type: |
| 205 | case dwarf::DW_TAG_reference_type: |
| 206 | case dwarf::DW_TAG_rvalue_reference_type: |
| 207 | case dwarf::DW_TAG_const_type: |
| 208 | case dwarf::DW_TAG_volatile_type: |
| 209 | case dwarf::DW_TAG_restrict_type: |
| 210 | case dwarf::DW_TAG_member: |
| 211 | case dwarf::DW_TAG_inheritance: |
| 212 | case dwarf::DW_TAG_friend: |
| 213 | return true; |
| 214 | default: |
| 215 | // CompositeTypes are currently modelled as DerivedTypes. |
| 216 | return isCompositeType(); |
| 217 | } |
| 218 | } |
| 219 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 220 | bool DIDescriptor::isCompositeType() const { |
| 221 | if (!DbgNode) |
| 222 | return false; |
| 223 | switch (getTag()) { |
| 224 | case dwarf::DW_TAG_array_type: |
| 225 | case dwarf::DW_TAG_structure_type: |
| 226 | case dwarf::DW_TAG_union_type: |
| 227 | case dwarf::DW_TAG_enumeration_type: |
| 228 | case dwarf::DW_TAG_subroutine_type: |
| 229 | case dwarf::DW_TAG_class_type: |
| 230 | return true; |
| 231 | default: |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 236 | bool DIDescriptor::isVariable() const { |
| 237 | if (!DbgNode) |
| 238 | return false; |
| 239 | switch (getTag()) { |
| 240 | case dwarf::DW_TAG_auto_variable: |
| 241 | case dwarf::DW_TAG_arg_variable: |
| 242 | return true; |
| 243 | default: |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 248 | bool DIDescriptor::isType() const { |
Manman Ren | f93ac4b | 2014-07-29 18:20:39 +0000 | [diff] [blame] | 249 | return isBasicType() || isCompositeType() || isDerivedType(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 252 | bool DIDescriptor::isSubprogram() const { |
| 253 | return DbgNode && getTag() == dwarf::DW_TAG_subprogram; |
| 254 | } |
| 255 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 256 | bool DIDescriptor::isGlobalVariable() const { |
Duncan P. N. Exon Smith | b407bb2 | 2015-02-09 22:48:04 +0000 | [diff] [blame] | 257 | return DbgNode && getTag() == dwarf::DW_TAG_variable; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 260 | bool DIDescriptor::isScope() const { |
| 261 | if (!DbgNode) |
| 262 | return false; |
| 263 | switch (getTag()) { |
| 264 | case dwarf::DW_TAG_compile_unit: |
| 265 | case dwarf::DW_TAG_lexical_block: |
| 266 | case dwarf::DW_TAG_subprogram: |
| 267 | case dwarf::DW_TAG_namespace: |
| 268 | case dwarf::DW_TAG_file_type: |
| 269 | return true; |
| 270 | default: |
| 271 | break; |
| 272 | } |
| 273 | return isType(); |
| 274 | } |
| 275 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 276 | bool DIDescriptor::isTemplateTypeParameter() const { |
| 277 | return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter; |
| 278 | } |
| 279 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 280 | bool DIDescriptor::isTemplateValueParameter() const { |
| 281 | return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter || |
| 282 | getTag() == dwarf::DW_TAG_GNU_template_template_param || |
| 283 | getTag() == dwarf::DW_TAG_GNU_template_parameter_pack); |
| 284 | } |
| 285 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 286 | bool DIDescriptor::isCompileUnit() const { |
| 287 | return DbgNode && getTag() == dwarf::DW_TAG_compile_unit; |
| 288 | } |
| 289 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 290 | bool DIDescriptor::isFile() const { |
| 291 | return DbgNode && getTag() == dwarf::DW_TAG_file_type; |
| 292 | } |
| 293 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 294 | bool DIDescriptor::isNameSpace() const { |
| 295 | return DbgNode && getTag() == dwarf::DW_TAG_namespace; |
| 296 | } |
| 297 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 298 | bool DIDescriptor::isLexicalBlockFile() const { |
| 299 | return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 300 | DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 303 | bool DIDescriptor::isLexicalBlock() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 304 | // FIXME: There are always exactly 4 header fields in DILexicalBlock, but |
| 305 | // something relies on this returning true for DILexicalBlockFile. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 306 | return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 307 | DbgNode->getNumOperands() == 3 && |
| 308 | (getNumHeaderFields() == 2 || getNumHeaderFields() == 4); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 311 | bool DIDescriptor::isSubrange() const { |
| 312 | return DbgNode && getTag() == dwarf::DW_TAG_subrange_type; |
| 313 | } |
| 314 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 315 | bool DIDescriptor::isEnumerator() const { |
| 316 | return DbgNode && getTag() == dwarf::DW_TAG_enumerator; |
| 317 | } |
| 318 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 319 | bool DIDescriptor::isObjCProperty() const { |
| 320 | return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property; |
| 321 | } |
| 322 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 323 | bool DIDescriptor::isImportedEntity() const { |
| 324 | return DbgNode && (getTag() == dwarf::DW_TAG_imported_module || |
| 325 | getTag() == dwarf::DW_TAG_imported_declaration); |
| 326 | } |
| 327 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 328 | bool DIDescriptor::isExpression() const { |
| 329 | return DbgNode && (getTag() == dwarf::DW_TAG_expression); |
| 330 | } |
| 331 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 332 | //===----------------------------------------------------------------------===// |
| 333 | // Simple Descriptor Constructors and other Methods |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 336 | void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 337 | |
| 338 | assert(DbgNode && "Trying to replace an unverified type!"); |
| 339 | |
| 340 | // Since we use a TrackingVH for the node, its easy for clients to manufacture |
| 341 | // legitimate situations where they want to replaceAllUsesWith() on something |
| 342 | // which, due to uniquing, has merged with the source. We shield clients from |
| 343 | // this detail by allowing a value to be replaced with replaceAllUsesWith() |
| 344 | // itself. |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 345 | const MDNode *DN = D; |
| 346 | if (DbgNode == DN) { |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 347 | SmallVector<Metadata *, 10> Ops(DbgNode->op_begin(), DbgNode->op_end()); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 348 | DN = MDNode::get(VMContext, Ops); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 349 | } |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 350 | |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 351 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 352 | auto *Node = const_cast<MDNode *>(DbgNode); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 353 | Node->replaceAllUsesWith(const_cast<MDNode *>(DN)); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 354 | MDNode::deleteTemporary(Node); |
Frederic Riss | dd7aec5 | 2014-09-15 07:50:42 +0000 | [diff] [blame] | 355 | DbgNode = DN; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 358 | void DIDescriptor::replaceAllUsesWith(MDNode *D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 359 | assert(DbgNode && "Trying to replace an unverified type!"); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 360 | assert(DbgNode != D && "This replacement should always happen"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 361 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 362 | auto *Node = const_cast<MDNode *>(DbgNode); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 363 | Node->replaceAllUsesWith(D); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 364 | MDNode::deleteTemporary(Node); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 367 | bool DICompileUnit::Verify() const { |
| 368 | if (!isCompileUnit()) |
| 369 | return false; |
| 370 | |
| 371 | // Don't bother verifying the compilation directory or producer string |
| 372 | // as those could be empty. |
| 373 | if (getFilename().empty()) |
| 374 | return false; |
| 375 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 376 | return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 379 | bool DIObjCProperty::Verify() const { |
| 380 | if (!isObjCProperty()) |
| 381 | return false; |
| 382 | |
| 383 | // Don't worry about the rest of the strings for now. |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 384 | return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 387 | /// \brief Check if a field at position Elt of a MDNode is a MDNode. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 388 | static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 389 | Metadata *Fld = getField(DbgNode, Elt); |
Duncan P. N. Exon Smith | a55dcaf | 2015-02-17 22:34:15 +0000 | [diff] [blame] | 390 | return !Fld || isa<MDNode>(Fld); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 393 | /// \brief Check if a field at position Elt of a MDNode is a MDString. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 394 | static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 395 | Metadata *Fld = getField(DbgNode, Elt); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 396 | return !Fld || isa<MDString>(Fld); |
| 397 | } |
| 398 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 399 | /// \brief Check if a value can be a reference to a type. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 400 | static bool isTypeRef(const Metadata *MD) { |
| 401 | if (!MD) |
| 402 | return true; |
| 403 | if (auto *S = dyn_cast<MDString>(MD)) |
| 404 | return !S->getString().empty(); |
| 405 | if (auto *N = dyn_cast<MDNode>(MD)) |
| 406 | return DIType(N).isType(); |
| 407 | return false; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 410 | /// \brief Check if referenced field might be a type. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 411 | static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 412 | return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 415 | /// \brief Check if a value can be a ScopeRef. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 416 | static bool isScopeRef(const Metadata *MD) { |
| 417 | if (!MD) |
| 418 | return true; |
| 419 | if (auto *S = dyn_cast<MDString>(MD)) |
| 420 | return !S->getString().empty(); |
| 421 | return isa<MDNode>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 424 | /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 425 | static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 426 | return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 429 | bool DIType::Verify() const { |
| 430 | if (!isType()) |
| 431 | return false; |
| 432 | // Make sure Context @ field 2 is MDNode. |
| 433 | if (!fieldIsScopeRef(DbgNode, 2)) |
| 434 | return false; |
| 435 | |
| 436 | // FIXME: Sink this into the various subclass verifies. |
| 437 | uint16_t Tag = getTag(); |
Manman Ren | f93ac4b | 2014-07-29 18:20:39 +0000 | [diff] [blame] | 438 | if (!isBasicType() && Tag != dwarf::DW_TAG_const_type && |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 439 | Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type && |
| 440 | Tag != dwarf::DW_TAG_ptr_to_member_type && |
| 441 | Tag != dwarf::DW_TAG_reference_type && |
| 442 | Tag != dwarf::DW_TAG_rvalue_reference_type && |
| 443 | Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type && |
| 444 | Tag != dwarf::DW_TAG_enumeration_type && |
| 445 | Tag != dwarf::DW_TAG_subroutine_type && |
| 446 | Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend && |
| 447 | getFilename().empty()) |
| 448 | return false; |
Adrian Prantl | daedfda | 2014-08-29 22:44:07 +0000 | [diff] [blame] | 449 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 450 | // DIType is abstract, it should be a BasicType, a DerivedType or |
| 451 | // a CompositeType. |
| 452 | if (isBasicType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 453 | return DIBasicType(DbgNode).Verify(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 454 | else if (isCompositeType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 455 | return DICompositeType(DbgNode).Verify(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 456 | else if (isDerivedType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 457 | return DIDerivedType(DbgNode).Verify(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 458 | else |
| 459 | return false; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 462 | bool DIBasicType::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 463 | return isBasicType() && DbgNode->getNumOperands() == 3 && |
| 464 | getNumHeaderFields() == 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 467 | bool DIDerivedType::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 468 | // Make sure DerivedFrom @ field 3 is TypeRef. |
| 469 | if (!fieldIsTypeRef(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 470 | return false; |
| 471 | if (getTag() == dwarf::DW_TAG_ptr_to_member_type) |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 472 | // Make sure ClassType @ field 4 is a TypeRef. |
| 473 | if (!fieldIsTypeRef(DbgNode, 4)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 474 | return false; |
| 475 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 476 | return isDerivedType() && DbgNode->getNumOperands() >= 4 && |
| 477 | DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 && |
| 478 | getNumHeaderFields() <= 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 481 | bool DICompositeType::Verify() const { |
| 482 | if (!isCompositeType()) |
| 483 | return false; |
| 484 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 485 | // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef. |
| 486 | if (!fieldIsTypeRef(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 487 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 488 | if (!fieldIsTypeRef(DbgNode, 5)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 489 | return false; |
| 490 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 491 | // Make sure the type identifier at field 7 is MDString, it can be null. |
| 492 | if (!fieldIsMDString(DbgNode, 7)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 493 | return false; |
| 494 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 495 | // A subroutine type can't be both & and &&. |
| 496 | if (isLValueReference() && isRValueReference()) |
| 497 | return false; |
| 498 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 499 | return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 502 | bool DISubprogram::Verify() const { |
| 503 | if (!isSubprogram()) |
| 504 | return false; |
| 505 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 506 | // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 507 | if (!fieldIsScopeRef(DbgNode, 2)) |
| 508 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 509 | if (!fieldIsMDNode(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 510 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 511 | // Containing type @ field 4. |
| 512 | if (!fieldIsTypeRef(DbgNode, 4)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 513 | return false; |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 514 | |
| 515 | // A subprogram can't be both & and &&. |
| 516 | if (isLValueReference() && isRValueReference()) |
| 517 | return false; |
| 518 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 519 | // If a DISubprogram has an llvm::Function*, then scope chains from all |
| 520 | // instructions within the function should lead to this DISubprogram. |
| 521 | if (auto *F = getFunction()) { |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 522 | for (auto &BB : *F) { |
| 523 | for (auto &I : BB) { |
| 524 | DebugLoc DL = I.getDebugLoc(); |
| 525 | if (DL.isUnknown()) |
| 526 | continue; |
| 527 | |
| 528 | MDNode *Scope = nullptr; |
| 529 | MDNode *IA = nullptr; |
| 530 | // walk the inlined-at scopes |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 531 | while ((IA = DL.getInlinedAt())) |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 532 | DL = DebugLoc::getFromDILocation(IA); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 533 | DL.getScopeAndInlinedAt(Scope, IA); |
Adrian Prantl | de200df | 2015-01-20 22:37:25 +0000 | [diff] [blame] | 534 | if (!Scope) |
| 535 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 536 | assert(!IA); |
| 537 | while (!DIDescriptor(Scope).isSubprogram()) { |
| 538 | DILexicalBlockFile D(Scope); |
| 539 | Scope = D.isLexicalBlockFile() |
| 540 | ? D.getScope() |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 541 | : DebugLoc::getFromDILexicalBlock(Scope).getScope(); |
Adrian Prantl | 1292e24 | 2015-01-21 18:32:56 +0000 | [diff] [blame] | 542 | if (!Scope) |
| 543 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 544 | } |
| 545 | if (!DISubprogram(Scope).describes(F)) |
| 546 | return false; |
| 547 | } |
| 548 | } |
| 549 | } |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 550 | return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 553 | bool DIGlobalVariable::Verify() const { |
| 554 | if (!isGlobalVariable()) |
| 555 | return false; |
| 556 | |
| 557 | if (getDisplayName().empty()) |
| 558 | return false; |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 559 | // Make sure context @ field 1 is an MDNode. |
| 560 | if (!fieldIsMDNode(DbgNode, 1)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 561 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 562 | // Make sure that type @ field 3 is a DITypeRef. |
| 563 | if (!fieldIsTypeRef(DbgNode, 3)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 564 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 565 | // Make sure StaticDataMemberDeclaration @ field 5 is MDNode. |
| 566 | if (!fieldIsMDNode(DbgNode, 5)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 567 | return false; |
| 568 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 569 | return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 572 | bool DIVariable::Verify() const { |
| 573 | if (!isVariable()) |
| 574 | return false; |
| 575 | |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 576 | // Make sure context @ field 1 is an MDNode. |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 577 | if (!fieldIsMDNode(DbgNode, 1)) |
| 578 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 579 | // Make sure that type @ field 3 is a DITypeRef. |
| 580 | if (!fieldIsTypeRef(DbgNode, 3)) |
| 581 | return false; |
| 582 | |
| 583 | // Check the number of header fields, which is common between complex and |
| 584 | // simple variables. |
| 585 | if (getNumHeaderFields() != 4) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 586 | return false; |
Adrian Prantl | da7d92e | 2014-06-30 17:17:35 +0000 | [diff] [blame] | 587 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 588 | // Variable without an inline location. |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 589 | if (DbgNode->getNumOperands() == 4) |
Adrian Prantl | da7d92e | 2014-06-30 17:17:35 +0000 | [diff] [blame] | 590 | return true; |
| 591 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 592 | // Variable with an inline location. |
| 593 | return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 596 | bool DIExpression::Verify() const { |
| 597 | // Empty DIExpressions may be represented as a nullptr. |
| 598 | if (!DbgNode) |
| 599 | return true; |
| 600 | |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 601 | if (!(isExpression() && DbgNode->getNumOperands() == 1)) |
| 602 | return false; |
| 603 | |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 604 | for (auto Op : *this) |
| 605 | switch (Op) { |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 606 | case DW_OP_bit_piece: |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 607 | // Must be the last element of the expression. |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 608 | return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3; |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 609 | case DW_OP_plus: |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 610 | if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2) |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 611 | return false; |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 612 | break; |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 613 | case DW_OP_deref: |
| 614 | break; |
| 615 | default: |
| 616 | // Other operators are not yet supported by the backend. |
| 617 | return false; |
| 618 | } |
| 619 | return true; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 622 | bool DILocation::Verify() const { |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 623 | return DbgNode && isa<MDLocation>(DbgNode); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 626 | bool DINameSpace::Verify() const { |
| 627 | if (!isNameSpace()) |
| 628 | return false; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 629 | return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 632 | MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); } |
| 633 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 634 | bool DIFile::Verify() const { |
| 635 | return isFile() && DbgNode->getNumOperands() == 2; |
| 636 | } |
| 637 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 638 | bool DIEnumerator::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 639 | return isEnumerator() && DbgNode->getNumOperands() == 1 && |
| 640 | getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 643 | bool DISubrange::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 644 | return isSubrange() && DbgNode->getNumOperands() == 1 && |
| 645 | getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 648 | bool DILexicalBlock::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 649 | return isLexicalBlock() && DbgNode->getNumOperands() == 3 && |
| 650 | getNumHeaderFields() == 4; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 653 | bool DILexicalBlockFile::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 654 | return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 && |
| 655 | getNumHeaderFields() == 2; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 658 | bool DITemplateTypeParameter::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 659 | return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 && |
| 660 | getNumHeaderFields() == 4; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 663 | bool DITemplateValueParameter::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 664 | return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 && |
| 665 | getNumHeaderFields() == 4; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 668 | bool DIImportedEntity::Verify() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 669 | return isImportedEntity() && DbgNode->getNumOperands() == 3 && |
| 670 | getNumHeaderFields() == 3; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 673 | MDNode *DIDerivedType::getObjCProperty() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 674 | return getNodeField(DbgNode, 4); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | MDString *DICompositeType::getIdentifier() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 678 | return cast_or_null<MDString>(getField(DbgNode, 7)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | #ifndef NDEBUG |
| 682 | static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { |
| 683 | for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { |
| 684 | // Skip the 'empty' list (that's a single i32 0, rather than truly empty). |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 685 | if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i))) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 686 | continue; |
| 687 | const MDNode *E = cast<MDNode>(LHS->getOperand(i)); |
| 688 | bool found = false; |
| 689 | for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j) |
Hans Wennborg | e242e8b | 2014-12-09 20:39:15 +0000 | [diff] [blame] | 690 | found = (E == cast<MDNode>(RHS->getOperand(j))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 691 | assert(found && "Losing a member during member list replacement"); |
| 692 | } |
| 693 | } |
| 694 | #endif |
| 695 | |
Manman Ren | 1a125c9 | 2014-07-28 19:33:20 +0000 | [diff] [blame] | 696 | void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 697 | TrackingMDNodeRef N(*this); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 698 | if (Elements) { |
| 699 | #ifndef NDEBUG |
| 700 | // Check that the new list of members contains all the old members as well. |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 701 | if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4))) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 702 | VerifySubsetOf(El, Elements); |
| 703 | #endif |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 704 | N->replaceOperandWith(4, Elements); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 705 | } |
| 706 | if (TParams) |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 707 | N->replaceOperandWith(6, TParams); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 708 | DbgNode = N; |
| 709 | } |
| 710 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 711 | DIScopeRef DIScope::getRef() const { |
| 712 | if (!isCompositeType()) |
| 713 | return DIScopeRef(*this); |
| 714 | DICompositeType DTy(DbgNode); |
| 715 | if (!DTy.getIdentifier()) |
| 716 | return DIScopeRef(*this); |
| 717 | return DIScopeRef(DTy.getIdentifier()); |
| 718 | } |
| 719 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 720 | void DICompositeType::setContainingType(DICompositeType ContainingType) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 721 | TrackingMDNodeRef N(*this); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 722 | N->replaceOperandWith(5, ContainingType.getRef()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 723 | DbgNode = N; |
| 724 | } |
| 725 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 726 | bool DIVariable::isInlinedFnArgument(const Function *CurFn) { |
| 727 | assert(CurFn && "Invalid function"); |
| 728 | if (!getContext().isSubprogram()) |
| 729 | return false; |
| 730 | // This variable is not inlined function argument if its scope |
| 731 | // does not describe current function. |
| 732 | return !DISubprogram(getContext()).describes(CurFn); |
| 733 | } |
| 734 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 735 | bool DISubprogram::describes(const Function *F) { |
| 736 | assert(F && "Invalid function"); |
| 737 | if (F == getFunction()) |
| 738 | return true; |
| 739 | StringRef Name = getLinkageName(); |
| 740 | if (Name.empty()) |
| 741 | Name = getName(); |
| 742 | if (F->getName() == Name) |
| 743 | return true; |
| 744 | return false; |
| 745 | } |
| 746 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 747 | MDNode *DISubprogram::getVariablesNodes() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 748 | return getNodeField(DbgNode, 8); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | DIArray DISubprogram::getVariables() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 752 | return DIArray(getNodeField(DbgNode, 8)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 755 | Metadata *DITemplateValueParameter::getValue() const { |
| 756 | return DbgNode->getOperand(3); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 759 | DIScopeRef DIScope::getContext() const { |
| 760 | |
| 761 | if (isType()) |
| 762 | return DIType(DbgNode).getContext(); |
| 763 | |
| 764 | if (isSubprogram()) |
| 765 | return DIScopeRef(DISubprogram(DbgNode).getContext()); |
| 766 | |
| 767 | if (isLexicalBlock()) |
| 768 | return DIScopeRef(DILexicalBlock(DbgNode).getContext()); |
| 769 | |
| 770 | if (isLexicalBlockFile()) |
| 771 | return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); |
| 772 | |
| 773 | if (isNameSpace()) |
| 774 | return DIScopeRef(DINameSpace(DbgNode).getContext()); |
| 775 | |
| 776 | assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 777 | return DIScopeRef(nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 780 | StringRef DIScope::getName() const { |
| 781 | if (isType()) |
| 782 | return DIType(DbgNode).getName(); |
| 783 | if (isSubprogram()) |
| 784 | return DISubprogram(DbgNode).getName(); |
| 785 | if (isNameSpace()) |
| 786 | return DINameSpace(DbgNode).getName(); |
| 787 | assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || |
| 788 | isCompileUnit()) && |
| 789 | "Unhandled type of scope."); |
| 790 | return StringRef(); |
| 791 | } |
| 792 | |
| 793 | StringRef DIScope::getFilename() const { |
| 794 | if (!DbgNode) |
| 795 | return StringRef(); |
| 796 | return ::getStringField(getNodeField(DbgNode, 1), 0); |
| 797 | } |
| 798 | |
| 799 | StringRef DIScope::getDirectory() const { |
| 800 | if (!DbgNode) |
| 801 | return StringRef(); |
| 802 | return ::getStringField(getNodeField(DbgNode, 1), 1); |
| 803 | } |
| 804 | |
| 805 | DIArray DICompileUnit::getEnumTypes() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 806 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 807 | return DIArray(); |
| 808 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 809 | return DIArray(getNodeField(DbgNode, 2)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | DIArray DICompileUnit::getRetainedTypes() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 813 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 814 | return DIArray(); |
| 815 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 816 | return DIArray(getNodeField(DbgNode, 3)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | DIArray DICompileUnit::getSubprograms() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 820 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 821 | return DIArray(); |
| 822 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 823 | return DIArray(getNodeField(DbgNode, 4)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | DIArray DICompileUnit::getGlobalVariables() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 827 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 828 | return DIArray(); |
| 829 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 830 | return DIArray(getNodeField(DbgNode, 5)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | DIArray DICompileUnit::getImportedEntities() const { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 834 | if (!DbgNode || DbgNode->getNumOperands() < 7) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 835 | return DIArray(); |
| 836 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 837 | return DIArray(getNodeField(DbgNode, 6)); |
| 838 | } |
| 839 | |
| 840 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
| 841 | assert(Verify() && "Expected compile unit"); |
| 842 | if (Subprograms == getSubprograms()) |
| 843 | return; |
| 844 | |
| 845 | const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms); |
| 846 | } |
| 847 | |
| 848 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
| 849 | assert(Verify() && "Expected compile unit"); |
| 850 | if (GlobalVariables == getGlobalVariables()) |
| 851 | return; |
| 852 | |
| 853 | const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 856 | DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, |
David Blaikie | 2f3f76f | 2014-08-21 22:45:21 +0000 | [diff] [blame] | 857 | DILexicalBlockFile NewScope) { |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 858 | assert(Verify()); |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 859 | assert(NewScope && "Expected valid scope"); |
| 860 | |
| 861 | const auto *Old = cast<MDLocation>(DbgNode); |
| 862 | return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), |
| 863 | NewScope, Old->getInlinedAt())); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 866 | unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { |
| 867 | std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); |
| 868 | return ++Ctx.pImpl->DiscriminatorTable[Key]; |
| 869 | } |
| 870 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 871 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 872 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 873 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
| 874 | if (!InlinedScope) |
| 875 | return cleanseInlinedVariable(DV, VMContext); |
| 876 | |
| 877 | // Insert inlined scope. |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 878 | SmallVector<Metadata *, 8> Elts(DV->op_begin(), |
| 879 | DV->op_begin() + DIVariableInlinedAtIndex); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 880 | Elts.push_back(InlinedScope); |
| 881 | |
| 882 | DIVariable Inlined(MDNode::get(VMContext, Elts)); |
| 883 | assert(Inlined.Verify() && "Expected to create a DIVariable"); |
| 884 | return Inlined; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 887 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 888 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
| 889 | if (!DIVariable(DV).getInlinedAt()) |
| 890 | return DIVariable(DV); |
| 891 | |
| 892 | // Remove inlined scope. |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 893 | SmallVector<Metadata *, 8> Elts(DV->op_begin(), |
| 894 | DV->op_begin() + DIVariableInlinedAtIndex); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 895 | |
| 896 | DIVariable Cleansed(MDNode::get(VMContext, Elts)); |
| 897 | assert(Cleansed.Verify() && "Expected to create a DIVariable"); |
| 898 | return Cleansed; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 901 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
| 902 | DIDescriptor D(Scope); |
| 903 | if (D.isSubprogram()) |
| 904 | return DISubprogram(Scope); |
| 905 | |
| 906 | if (D.isLexicalBlockFile()) |
| 907 | return getDISubprogram(DILexicalBlockFile(Scope).getContext()); |
| 908 | |
| 909 | if (D.isLexicalBlock()) |
| 910 | return getDISubprogram(DILexicalBlock(Scope).getContext()); |
| 911 | |
| 912 | return DISubprogram(); |
| 913 | } |
| 914 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 915 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 916 | // We look for the first instr that has a debug annotation leading back to F. |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 917 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 918 | auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { |
| 919 | return !Inst.getDebugLoc().isUnknown(); |
| 920 | }); |
| 921 | if (Inst == BB.end()) |
| 922 | continue; |
| 923 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 924 | const MDNode *Scope = DLoc.getScopeNode(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 925 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 926 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | return DISubprogram(); |
| 930 | } |
| 931 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 932 | DICompositeType llvm::getDICompositeType(DIType T) { |
| 933 | if (T.isCompositeType()) |
| 934 | return DICompositeType(T); |
| 935 | |
| 936 | if (T.isDerivedType()) { |
| 937 | // This function is currently used by dragonegg and dragonegg does |
| 938 | // not generate identifier for types, so using an empty map to resolve |
| 939 | // DerivedFrom should be fine. |
| 940 | DITypeIdentifierMap EmptyMap; |
| 941 | return getDICompositeType( |
| 942 | DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); |
| 943 | } |
| 944 | |
| 945 | return DICompositeType(); |
| 946 | } |
| 947 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 948 | DITypeIdentifierMap |
| 949 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 950 | DITypeIdentifierMap Map; |
| 951 | for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 952 | DICompileUnit CU(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 953 | DIArray Retain = CU.getRetainedTypes(); |
| 954 | for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { |
| 955 | if (!Retain.getElement(Ti).isCompositeType()) |
| 956 | continue; |
| 957 | DICompositeType Ty(Retain.getElement(Ti)); |
| 958 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 959 | // Definition has priority over declaration. |
| 960 | // Try to insert (TypeId, Ty) to Map. |
| 961 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 962 | Map.insert(std::make_pair(TypeId, Ty)); |
| 963 | // If TypeId already exists in Map and this is a definition, replace |
| 964 | // whatever we had (declaration or definition) with the definition. |
| 965 | if (!P.second && !Ty.isForwardDecl()) |
| 966 | P.first->second = Ty; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | return Map; |
| 971 | } |
| 972 | |
| 973 | //===----------------------------------------------------------------------===// |
| 974 | // DebugInfoFinder implementations. |
| 975 | //===----------------------------------------------------------------------===// |
| 976 | |
| 977 | void DebugInfoFinder::reset() { |
| 978 | CUs.clear(); |
| 979 | SPs.clear(); |
| 980 | GVs.clear(); |
| 981 | TYs.clear(); |
| 982 | Scopes.clear(); |
| 983 | NodesSeen.clear(); |
| 984 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 985 | TypeMapInitialized = false; |
| 986 | } |
| 987 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 988 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 989 | if (!TypeMapInitialized) |
| 990 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 991 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 992 | TypeMapInitialized = true; |
| 993 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 996 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 997 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 998 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 999 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1000 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1001 | addCompileUnit(CU); |
| 1002 | DIArray GVs = CU.getGlobalVariables(); |
| 1003 | for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { |
| 1004 | DIGlobalVariable DIG(GVs.getElement(i)); |
| 1005 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 1006 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 1007 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | DIArray SPs = CU.getSubprograms(); |
| 1011 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) |
| 1012 | processSubprogram(DISubprogram(SPs.getElement(i))); |
| 1013 | DIArray EnumTypes = CU.getEnumTypes(); |
| 1014 | for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) |
| 1015 | processType(DIType(EnumTypes.getElement(i))); |
| 1016 | DIArray RetainedTypes = CU.getRetainedTypes(); |
| 1017 | for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) |
| 1018 | processType(DIType(RetainedTypes.getElement(i))); |
| 1019 | DIArray Imports = CU.getImportedEntities(); |
| 1020 | for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { |
| 1021 | DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); |
Adrian Prantl | d09ba23 | 2014-04-01 03:41:04 +0000 | [diff] [blame] | 1022 | DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1023 | if (Entity.isType()) |
| 1024 | processType(DIType(Entity)); |
| 1025 | else if (Entity.isSubprogram()) |
| 1026 | processSubprogram(DISubprogram(Entity)); |
| 1027 | else if (Entity.isNameSpace()) |
| 1028 | processScope(DINameSpace(Entity).getContext()); |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1034 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1035 | if (!Loc) |
| 1036 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1037 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1038 | processScope(Loc.getScope()); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1039 | processLocation(M, Loc.getOrigLocation()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1042 | void DebugInfoFinder::processType(DIType DT) { |
| 1043 | if (!addType(DT)) |
| 1044 | return; |
| 1045 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
| 1046 | if (DT.isCompositeType()) { |
| 1047 | DICompositeType DCT(DT); |
| 1048 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 1049 | if (DT.isSubroutineType()) { |
| 1050 | DITypeArray DTA = DISubroutineType(DT).getTypeArray(); |
| 1051 | for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) |
| 1052 | processType(DTA.getElement(i).resolve(TypeIdentifierMap)); |
| 1053 | return; |
| 1054 | } |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 1055 | DIArray DA = DCT.getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1056 | for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { |
| 1057 | DIDescriptor D = DA.getElement(i); |
| 1058 | if (D.isType()) |
| 1059 | processType(DIType(D)); |
| 1060 | else if (D.isSubprogram()) |
| 1061 | processSubprogram(DISubprogram(D)); |
| 1062 | } |
| 1063 | } else if (DT.isDerivedType()) { |
| 1064 | DIDerivedType DDT(DT); |
| 1065 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | void DebugInfoFinder::processScope(DIScope Scope) { |
| 1070 | if (Scope.isType()) { |
| 1071 | DIType Ty(Scope); |
| 1072 | processType(Ty); |
| 1073 | return; |
| 1074 | } |
| 1075 | if (Scope.isCompileUnit()) { |
| 1076 | addCompileUnit(DICompileUnit(Scope)); |
| 1077 | return; |
| 1078 | } |
| 1079 | if (Scope.isSubprogram()) { |
| 1080 | processSubprogram(DISubprogram(Scope)); |
| 1081 | return; |
| 1082 | } |
| 1083 | if (!addScope(Scope)) |
| 1084 | return; |
| 1085 | if (Scope.isLexicalBlock()) { |
| 1086 | DILexicalBlock LB(Scope); |
| 1087 | processScope(LB.getContext()); |
| 1088 | } else if (Scope.isLexicalBlockFile()) { |
| 1089 | DILexicalBlockFile LBF = DILexicalBlockFile(Scope); |
| 1090 | processScope(LBF.getScope()); |
| 1091 | } else if (Scope.isNameSpace()) { |
| 1092 | DINameSpace NS(Scope); |
| 1093 | processScope(NS.getContext()); |
| 1094 | } |
| 1095 | } |
| 1096 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1097 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 1098 | if (!addSubprogram(SP)) |
| 1099 | return; |
| 1100 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 1101 | processType(SP.getType()); |
| 1102 | DIArray TParams = SP.getTemplateParams(); |
| 1103 | for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { |
| 1104 | DIDescriptor Element = TParams.getElement(I); |
| 1105 | if (Element.isTemplateTypeParameter()) { |
| 1106 | DITemplateTypeParameter TType(Element); |
| 1107 | processScope(TType.getContext().resolve(TypeIdentifierMap)); |
| 1108 | processType(TType.getType().resolve(TypeIdentifierMap)); |
| 1109 | } else if (Element.isTemplateValueParameter()) { |
| 1110 | DITemplateValueParameter TVal(Element); |
| 1111 | processScope(TVal.getContext().resolve(TypeIdentifierMap)); |
| 1112 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1117 | void DebugInfoFinder::processDeclare(const Module &M, |
| 1118 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1119 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 1120 | if (!N) |
| 1121 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1122 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1123 | |
| 1124 | DIDescriptor DV(N); |
| 1125 | if (!DV.isVariable()) |
| 1126 | return; |
| 1127 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1128 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1129 | return; |
| 1130 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 1131 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 1134 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1135 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 1136 | if (!N) |
| 1137 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 1138 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1139 | |
| 1140 | DIDescriptor DV(N); |
| 1141 | if (!DV.isVariable()) |
| 1142 | return; |
| 1143 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1144 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1145 | return; |
| 1146 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 1147 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1150 | bool DebugInfoFinder::addType(DIType DT) { |
| 1151 | if (!DT) |
| 1152 | return false; |
| 1153 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1154 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1155 | return false; |
| 1156 | |
| 1157 | TYs.push_back(DT); |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1161 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 1162 | if (!CU) |
| 1163 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1164 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1165 | return false; |
| 1166 | |
| 1167 | CUs.push_back(CU); |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1171 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 1172 | if (!DIG) |
| 1173 | return false; |
| 1174 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1175 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1176 | return false; |
| 1177 | |
| 1178 | GVs.push_back(DIG); |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1182 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 1183 | if (!SP) |
| 1184 | return false; |
| 1185 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1186 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1187 | return false; |
| 1188 | |
| 1189 | SPs.push_back(SP); |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 1194 | if (!Scope) |
| 1195 | return false; |
| 1196 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 1197 | // as null for now. |
| 1198 | if (Scope->getNumOperands() == 0) |
| 1199 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1200 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1201 | return false; |
| 1202 | Scopes.push_back(Scope); |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | //===----------------------------------------------------------------------===// |
| 1207 | // DIDescriptor: dump routines for all descriptors. |
| 1208 | //===----------------------------------------------------------------------===// |
| 1209 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1210 | void DIDescriptor::dump() const { |
| 1211 | print(dbgs()); |
| 1212 | dbgs() << '\n'; |
| 1213 | } |
| 1214 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1215 | void DIDescriptor::print(raw_ostream &OS) const { |
| 1216 | if (!DbgNode) |
| 1217 | return; |
| 1218 | |
| 1219 | if (const char *Tag = dwarf::TagString(getTag())) |
| 1220 | OS << "[ " << Tag << " ]"; |
| 1221 | |
| 1222 | if (this->isSubrange()) { |
| 1223 | DISubrange(DbgNode).printInternal(OS); |
| 1224 | } else if (this->isCompileUnit()) { |
| 1225 | DICompileUnit(DbgNode).printInternal(OS); |
| 1226 | } else if (this->isFile()) { |
| 1227 | DIFile(DbgNode).printInternal(OS); |
| 1228 | } else if (this->isEnumerator()) { |
| 1229 | DIEnumerator(DbgNode).printInternal(OS); |
| 1230 | } else if (this->isBasicType()) { |
| 1231 | DIType(DbgNode).printInternal(OS); |
| 1232 | } else if (this->isDerivedType()) { |
| 1233 | DIDerivedType(DbgNode).printInternal(OS); |
| 1234 | } else if (this->isCompositeType()) { |
| 1235 | DICompositeType(DbgNode).printInternal(OS); |
| 1236 | } else if (this->isSubprogram()) { |
| 1237 | DISubprogram(DbgNode).printInternal(OS); |
| 1238 | } else if (this->isGlobalVariable()) { |
| 1239 | DIGlobalVariable(DbgNode).printInternal(OS); |
| 1240 | } else if (this->isVariable()) { |
| 1241 | DIVariable(DbgNode).printInternal(OS); |
| 1242 | } else if (this->isObjCProperty()) { |
| 1243 | DIObjCProperty(DbgNode).printInternal(OS); |
| 1244 | } else if (this->isNameSpace()) { |
| 1245 | DINameSpace(DbgNode).printInternal(OS); |
| 1246 | } else if (this->isScope()) { |
| 1247 | DIScope(DbgNode).printInternal(OS); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1248 | } else if (this->isExpression()) { |
| 1249 | DIExpression(DbgNode).printInternal(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | void DISubrange::printInternal(raw_ostream &OS) const { |
| 1254 | int64_t Count = getCount(); |
| 1255 | if (Count != -1) |
| 1256 | OS << " [" << getLo() << ", " << Count - 1 << ']'; |
| 1257 | else |
| 1258 | OS << " [unbounded]"; |
| 1259 | } |
| 1260 | |
| 1261 | void DIScope::printInternal(raw_ostream &OS) const { |
| 1262 | OS << " [" << getDirectory() << "/" << getFilename() << ']'; |
| 1263 | } |
| 1264 | |
| 1265 | void DICompileUnit::printInternal(raw_ostream &OS) const { |
| 1266 | DIScope::printInternal(OS); |
| 1267 | OS << " ["; |
| 1268 | unsigned Lang = getLanguage(); |
| 1269 | if (const char *LangStr = dwarf::LanguageString(Lang)) |
| 1270 | OS << LangStr; |
| 1271 | else |
| 1272 | (OS << "lang 0x").write_hex(Lang); |
| 1273 | OS << ']'; |
| 1274 | } |
| 1275 | |
| 1276 | void DIEnumerator::printInternal(raw_ostream &OS) const { |
| 1277 | OS << " [" << getName() << " :: " << getEnumValue() << ']'; |
| 1278 | } |
| 1279 | |
| 1280 | void DIType::printInternal(raw_ostream &OS) const { |
Manman Ren | f93ac4b | 2014-07-29 18:20:39 +0000 | [diff] [blame] | 1281 | if (!DbgNode) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1282 | return; |
| 1283 | |
| 1284 | StringRef Res = getName(); |
| 1285 | if (!Res.empty()) |
| 1286 | OS << " [" << Res << "]"; |
| 1287 | |
| 1288 | // TODO: Print context? |
| 1289 | |
| 1290 | OS << " [line " << getLineNumber() << ", size " << getSizeInBits() |
| 1291 | << ", align " << getAlignInBits() << ", offset " << getOffsetInBits(); |
| 1292 | if (isBasicType()) |
| 1293 | if (const char *Enc = |
| 1294 | dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding())) |
| 1295 | OS << ", enc " << Enc; |
| 1296 | OS << "]"; |
| 1297 | |
| 1298 | if (isPrivate()) |
| 1299 | OS << " [private]"; |
| 1300 | else if (isProtected()) |
| 1301 | OS << " [protected]"; |
Adrian Prantl | daedfda | 2014-08-29 22:44:07 +0000 | [diff] [blame] | 1302 | else if (isPublic()) |
| 1303 | OS << " [public]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (isArtificial()) |
| 1306 | OS << " [artificial]"; |
| 1307 | |
| 1308 | if (isForwardDecl()) |
| 1309 | OS << " [decl]"; |
| 1310 | else if (getTag() == dwarf::DW_TAG_structure_type || |
| 1311 | getTag() == dwarf::DW_TAG_union_type || |
| 1312 | getTag() == dwarf::DW_TAG_enumeration_type || |
| 1313 | getTag() == dwarf::DW_TAG_class_type) |
| 1314 | OS << " [def]"; |
| 1315 | if (isVector()) |
| 1316 | OS << " [vector]"; |
| 1317 | if (isStaticMember()) |
| 1318 | OS << " [static]"; |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (isLValueReference()) |
| 1321 | OS << " [reference]"; |
| 1322 | |
| 1323 | if (isRValueReference()) |
| 1324 | OS << " [rvalue reference]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | void DIDerivedType::printInternal(raw_ostream &OS) const { |
| 1328 | DIType::printInternal(OS); |
| 1329 | OS << " [from " << getTypeDerivedFrom().getName() << ']'; |
| 1330 | } |
| 1331 | |
| 1332 | void DICompositeType::printInternal(raw_ostream &OS) const { |
| 1333 | DIType::printInternal(OS); |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 1334 | DIArray A = getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1335 | OS << " [" << A.getNumElements() << " elements]"; |
| 1336 | } |
| 1337 | |
| 1338 | void DINameSpace::printInternal(raw_ostream &OS) const { |
| 1339 | StringRef Name = getName(); |
| 1340 | if (!Name.empty()) |
| 1341 | OS << " [" << Name << ']'; |
| 1342 | |
| 1343 | OS << " [line " << getLineNumber() << ']'; |
| 1344 | } |
| 1345 | |
| 1346 | void DISubprogram::printInternal(raw_ostream &OS) const { |
| 1347 | // TODO : Print context |
| 1348 | OS << " [line " << getLineNumber() << ']'; |
| 1349 | |
| 1350 | if (isLocalToUnit()) |
| 1351 | OS << " [local]"; |
| 1352 | |
| 1353 | if (isDefinition()) |
| 1354 | OS << " [def]"; |
| 1355 | |
| 1356 | if (getScopeLineNumber() != getLineNumber()) |
| 1357 | OS << " [scope " << getScopeLineNumber() << "]"; |
| 1358 | |
| 1359 | if (isPrivate()) |
| 1360 | OS << " [private]"; |
| 1361 | else if (isProtected()) |
| 1362 | OS << " [protected]"; |
Adrian Prantl | daedfda | 2014-08-29 22:44:07 +0000 | [diff] [blame] | 1363 | else if (isPublic()) |
| 1364 | OS << " [public]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1365 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 1366 | if (isLValueReference()) |
| 1367 | OS << " [reference]"; |
| 1368 | |
| 1369 | if (isRValueReference()) |
| 1370 | OS << " [rvalue reference]"; |
| 1371 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1372 | StringRef Res = getName(); |
| 1373 | if (!Res.empty()) |
| 1374 | OS << " [" << Res << ']'; |
| 1375 | } |
| 1376 | |
| 1377 | void DIGlobalVariable::printInternal(raw_ostream &OS) const { |
| 1378 | StringRef Res = getName(); |
| 1379 | if (!Res.empty()) |
| 1380 | OS << " [" << Res << ']'; |
| 1381 | |
| 1382 | OS << " [line " << getLineNumber() << ']'; |
| 1383 | |
| 1384 | // TODO : Print context |
| 1385 | |
| 1386 | if (isLocalToUnit()) |
| 1387 | OS << " [local]"; |
| 1388 | |
| 1389 | if (isDefinition()) |
| 1390 | OS << " [def]"; |
| 1391 | } |
| 1392 | |
| 1393 | void DIVariable::printInternal(raw_ostream &OS) const { |
| 1394 | StringRef Res = getName(); |
| 1395 | if (!Res.empty()) |
| 1396 | OS << " [" << Res << ']'; |
| 1397 | |
| 1398 | OS << " [line " << getLineNumber() << ']'; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1399 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1400 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1401 | void DIExpression::printInternal(raw_ostream &OS) const { |
Adrian Prantl | 4bd0a0c | 2015-01-23 21:24:41 +0000 | [diff] [blame] | 1402 | for (auto Op : *this) { |
| 1403 | OS << " [" << OperationEncodingString(Op); |
| 1404 | switch (Op) { |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1405 | case DW_OP_plus: { |
Adrian Prantl | 4bd0a0c | 2015-01-23 21:24:41 +0000 | [diff] [blame] | 1406 | OS << " " << Op.getArg(1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1407 | break; |
| 1408 | } |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 1409 | case DW_OP_bit_piece: { |
Adrian Prantl | 4bd0a0c | 2015-01-23 21:24:41 +0000 | [diff] [blame] | 1410 | OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | } |
Adrian Prantl | b9a88e2 | 2014-12-05 18:19:38 +0000 | [diff] [blame] | 1413 | case DW_OP_deref: |
| 1414 | // No arguments. |
| 1415 | break; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1416 | default: |
Adrian Prantl | 0d7d8e4 | 2015-01-22 16:55:22 +0000 | [diff] [blame] | 1417 | llvm_unreachable("unhandled operation"); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1418 | } |
| 1419 | OS << "]"; |
| 1420 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | void DIObjCProperty::printInternal(raw_ostream &OS) const { |
| 1424 | StringRef Name = getObjCPropertyName(); |
| 1425 | if (!Name.empty()) |
| 1426 | OS << " [" << Name << ']'; |
| 1427 | |
| 1428 | OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6) |
| 1429 | << ']'; |
| 1430 | } |
| 1431 | |
| 1432 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 1433 | const LLVMContext &Ctx) { |
| 1434 | if (!DL.isUnknown()) { // Print source line info. |
| 1435 | DIScope Scope(DL.getScope(Ctx)); |
| 1436 | assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); |
| 1437 | // Omit the directory, because it's likely to be long and uninteresting. |
| 1438 | CommentOS << Scope.getFilename(); |
| 1439 | CommentOS << ':' << DL.getLine(); |
| 1440 | if (DL.getCol() != 0) |
| 1441 | CommentOS << ':' << DL.getCol(); |
| 1442 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); |
| 1443 | if (!InlinedAtDL.isUnknown()) { |
| 1444 | CommentOS << " @[ "; |
| 1445 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 1446 | CommentOS << " ]"; |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 1452 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 1453 | StringRef Res = getName(); |
| 1454 | if (!Res.empty()) |
| 1455 | OS << Res << "," << getLineNumber(); |
| 1456 | if (MDNode *InlinedAt = getInlinedAt()) { |
| 1457 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); |
| 1458 | if (!InlinedAtDL.isUnknown()) { |
| 1459 | OS << " @["; |
| 1460 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 1461 | OS << "]"; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1466 | template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1467 | assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); |
| 1468 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1469 | template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1470 | assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); |
| 1471 | } |
| 1472 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1473 | template <> |
| 1474 | DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1475 | return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1476 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1477 | template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 1478 | return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1479 | } |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1480 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1481 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1482 | bool Changed = false; |
| 1483 | |
| 1484 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 1485 | // the module. |
| 1486 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 1487 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1488 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1489 | CI->eraseFromParent(); |
| 1490 | } |
| 1491 | Declare->eraseFromParent(); |
| 1492 | Changed = true; |
| 1493 | } |
| 1494 | |
| 1495 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 1496 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1497 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 1498 | CI->eraseFromParent(); |
| 1499 | } |
| 1500 | DbgVal->eraseFromParent(); |
| 1501 | Changed = true; |
| 1502 | } |
| 1503 | |
| 1504 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 1505 | NME = M.named_metadata_end(); NMI != NME;) { |
| 1506 | NamedMDNode *NMD = NMI; |
| 1507 | ++NMI; |
| 1508 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 1509 | NMD->eraseFromParent(); |
| 1510 | Changed = true; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
| 1515 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; |
| 1516 | ++FI) |
| 1517 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; |
| 1518 | ++BI) { |
| 1519 | if (!BI->getDebugLoc().isUnknown()) { |
| 1520 | Changed = true; |
| 1521 | BI->setDebugLoc(DebugLoc()); |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | return Changed; |
| 1526 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1527 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 1528 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 1529 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1530 | M.getModuleFlag("Debug Info Version"))) |
| 1531 | return Val->getZExtValue(); |
| 1532 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1533 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1534 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 1535 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 1536 | llvm::makeSubprogramMap(const Module &M) { |
| 1537 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1538 | |
| 1539 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 1540 | if (!CU_Nodes) |
| 1541 | return R; |
| 1542 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1543 | for (MDNode *N : CU_Nodes->operands()) { |
| 1544 | DICompileUnit CUNode(N); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1545 | DIArray SPs = CUNode.getSubprograms(); |
| 1546 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 1547 | DISubprogram SP(SPs.getElement(i)); |
| 1548 | if (Function *F = SP.getFunction()) |
| 1549 | R.insert(std::make_pair(F, SP)); |
| 1550 | } |
| 1551 | } |
| 1552 | return R; |
| 1553 | } |