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