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