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