| File: | src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Bitcode/Reader/MetadataLoader.cpp |
| Warning: | line 771, column 17 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | ||||
| 9 | #include "MetadataLoader.h" | |||
| 10 | #include "ValueList.h" | |||
| 11 | ||||
| 12 | #include "llvm/ADT/APFloat.h" | |||
| 13 | #include "llvm/ADT/APInt.h" | |||
| 14 | #include "llvm/ADT/ArrayRef.h" | |||
| 15 | #include "llvm/ADT/DenseMap.h" | |||
| 16 | #include "llvm/ADT/DenseSet.h" | |||
| 17 | #include "llvm/ADT/None.h" | |||
| 18 | #include "llvm/ADT/STLExtras.h" | |||
| 19 | #include "llvm/ADT/SmallString.h" | |||
| 20 | #include "llvm/ADT/Statistic.h" | |||
| 21 | #include "llvm/ADT/StringRef.h" | |||
| 22 | #include "llvm/ADT/Twine.h" | |||
| 23 | #include "llvm/Bitcode/BitcodeReader.h" | |||
| 24 | #include "llvm/Bitstream/BitstreamReader.h" | |||
| 25 | #include "llvm/Bitcode/LLVMBitCodes.h" | |||
| 26 | #include "llvm/IR/Argument.h" | |||
| 27 | #include "llvm/IR/Attributes.h" | |||
| 28 | #include "llvm/IR/AutoUpgrade.h" | |||
| 29 | #include "llvm/IR/BasicBlock.h" | |||
| 30 | #include "llvm/IR/CallingConv.h" | |||
| 31 | #include "llvm/IR/Comdat.h" | |||
| 32 | #include "llvm/IR/Constant.h" | |||
| 33 | #include "llvm/IR/Constants.h" | |||
| 34 | #include "llvm/IR/DebugInfo.h" | |||
| 35 | #include "llvm/IR/DebugInfoMetadata.h" | |||
| 36 | #include "llvm/IR/DebugLoc.h" | |||
| 37 | #include "llvm/IR/DerivedTypes.h" | |||
| 38 | #include "llvm/IR/DiagnosticPrinter.h" | |||
| 39 | #include "llvm/IR/Function.h" | |||
| 40 | #include "llvm/IR/GVMaterializer.h" | |||
| 41 | #include "llvm/IR/GlobalAlias.h" | |||
| 42 | #include "llvm/IR/GlobalIFunc.h" | |||
| 43 | #include "llvm/IR/GlobalIndirectSymbol.h" | |||
| 44 | #include "llvm/IR/GlobalObject.h" | |||
| 45 | #include "llvm/IR/GlobalValue.h" | |||
| 46 | #include "llvm/IR/GlobalVariable.h" | |||
| 47 | #include "llvm/IR/InlineAsm.h" | |||
| 48 | #include "llvm/IR/InstrTypes.h" | |||
| 49 | #include "llvm/IR/Instruction.h" | |||
| 50 | #include "llvm/IR/Instructions.h" | |||
| 51 | #include "llvm/IR/IntrinsicInst.h" | |||
| 52 | #include "llvm/IR/Intrinsics.h" | |||
| 53 | #include "llvm/IR/LLVMContext.h" | |||
| 54 | #include "llvm/IR/Module.h" | |||
| 55 | #include "llvm/IR/ModuleSummaryIndex.h" | |||
| 56 | #include "llvm/IR/OperandTraits.h" | |||
| 57 | #include "llvm/IR/TrackingMDRef.h" | |||
| 58 | #include "llvm/IR/Type.h" | |||
| 59 | #include "llvm/IR/ValueHandle.h" | |||
| 60 | #include "llvm/Support/AtomicOrdering.h" | |||
| 61 | #include "llvm/Support/Casting.h" | |||
| 62 | #include "llvm/Support/CommandLine.h" | |||
| 63 | #include "llvm/Support/Compiler.h" | |||
| 64 | #include "llvm/Support/Debug.h" | |||
| 65 | #include "llvm/Support/ErrorHandling.h" | |||
| 66 | #include "llvm/Support/ManagedStatic.h" | |||
| 67 | #include "llvm/Support/MemoryBuffer.h" | |||
| 68 | #include "llvm/Support/raw_ostream.h" | |||
| 69 | #include <algorithm> | |||
| 70 | #include <cassert> | |||
| 71 | #include <cstddef> | |||
| 72 | #include <cstdint> | |||
| 73 | #include <deque> | |||
| 74 | #include <limits> | |||
| 75 | #include <map> | |||
| 76 | #include <string> | |||
| 77 | #include <system_error> | |||
| 78 | #include <tuple> | |||
| 79 | #include <utility> | |||
| 80 | #include <vector> | |||
| 81 | ||||
| 82 | using namespace llvm; | |||
| 83 | ||||
| 84 | #define DEBUG_TYPE"bitcode-reader" "bitcode-reader" | |||
| 85 | ||||
| 86 | STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded")static llvm::Statistic NumMDStringLoaded = {"bitcode-reader", "NumMDStringLoaded", "Number of MDStrings loaded"}; | |||
| 87 | STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created")static llvm::Statistic NumMDNodeTemporary = {"bitcode-reader" , "NumMDNodeTemporary", "Number of MDNode::Temporary created" }; | |||
| 88 | STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded")static llvm::Statistic NumMDRecordLoaded = {"bitcode-reader", "NumMDRecordLoaded", "Number of Metadata records loaded"}; | |||
| 89 | ||||
| 90 | /// Flag whether we need to import full type definitions for ThinLTO. | |||
| 91 | /// Currently needed for Darwin and LLDB. | |||
| 92 | static cl::opt<bool> ImportFullTypeDefinitions( | |||
| 93 | "import-full-type-definitions", cl::init(false), cl::Hidden, | |||
| 94 | cl::desc("Import full type definitions for ThinLTO.")); | |||
| 95 | ||||
| 96 | static cl::opt<bool> DisableLazyLoading( | |||
| 97 | "disable-ondemand-mds-loading", cl::init(false), cl::Hidden, | |||
| 98 | cl::desc("Force disable the lazy-loading on-demand of metadata when " | |||
| 99 | "loading bitcode for importing.")); | |||
| 100 | ||||
| 101 | namespace { | |||
| 102 | ||||
| 103 | static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; } | |||
| 104 | ||||
| 105 | class BitcodeReaderMetadataList { | |||
| 106 | /// Array of metadata references. | |||
| 107 | /// | |||
| 108 | /// Don't use std::vector here. Some versions of libc++ copy (instead of | |||
| 109 | /// move) on resize, and TrackingMDRef is very expensive to copy. | |||
| 110 | SmallVector<TrackingMDRef, 1> MetadataPtrs; | |||
| 111 | ||||
| 112 | /// The set of indices in MetadataPtrs above of forward references that were | |||
| 113 | /// generated. | |||
| 114 | SmallDenseSet<unsigned, 1> ForwardReference; | |||
| 115 | ||||
| 116 | /// The set of indices in MetadataPtrs above of Metadata that need to be | |||
| 117 | /// resolved. | |||
| 118 | SmallDenseSet<unsigned, 1> UnresolvedNodes; | |||
| 119 | ||||
| 120 | /// Structures for resolving old type refs. | |||
| 121 | struct { | |||
| 122 | SmallDenseMap<MDString *, TempMDTuple, 1> Unknown; | |||
| 123 | SmallDenseMap<MDString *, DICompositeType *, 1> Final; | |||
| 124 | SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls; | |||
| 125 | SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays; | |||
| 126 | } OldTypeRefs; | |||
| 127 | ||||
| 128 | LLVMContext &Context; | |||
| 129 | ||||
| 130 | /// Maximum number of valid references. Forward references exceeding the | |||
| 131 | /// maximum must be invalid. | |||
| 132 | unsigned RefsUpperBound; | |||
| 133 | ||||
| 134 | public: | |||
| 135 | BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound) | |||
| 136 | : Context(C), | |||
| 137 | RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(), | |||
| 138 | RefsUpperBound)) {} | |||
| 139 | ||||
| 140 | // vector compatibility methods | |||
| 141 | unsigned size() const { return MetadataPtrs.size(); } | |||
| 142 | void resize(unsigned N) { MetadataPtrs.resize(N); } | |||
| 143 | void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); } | |||
| 144 | void clear() { MetadataPtrs.clear(); } | |||
| 145 | Metadata *back() const { return MetadataPtrs.back(); } | |||
| 146 | void pop_back() { MetadataPtrs.pop_back(); } | |||
| 147 | bool empty() const { return MetadataPtrs.empty(); } | |||
| 148 | ||||
| 149 | Metadata *operator[](unsigned i) const { | |||
| 150 | assert(i < MetadataPtrs.size())((void)0); | |||
| 151 | return MetadataPtrs[i]; | |||
| 152 | } | |||
| 153 | ||||
| 154 | Metadata *lookup(unsigned I) const { | |||
| 155 | if (I < MetadataPtrs.size()) | |||
| 156 | return MetadataPtrs[I]; | |||
| 157 | return nullptr; | |||
| 158 | } | |||
| 159 | ||||
| 160 | void shrinkTo(unsigned N) { | |||
| 161 | assert(N <= size() && "Invalid shrinkTo request!")((void)0); | |||
| 162 | assert(ForwardReference.empty() && "Unexpected forward refs")((void)0); | |||
| 163 | assert(UnresolvedNodes.empty() && "Unexpected unresolved node")((void)0); | |||
| 164 | MetadataPtrs.resize(N); | |||
| 165 | } | |||
| 166 | ||||
| 167 | /// Return the given metadata, creating a replaceable forward reference if | |||
| 168 | /// necessary. | |||
| 169 | Metadata *getMetadataFwdRef(unsigned Idx); | |||
| 170 | ||||
| 171 | /// Return the given metadata only if it is fully resolved. | |||
| 172 | /// | |||
| 173 | /// Gives the same result as \a lookup(), unless \a MDNode::isResolved() | |||
| 174 | /// would give \c false. | |||
| 175 | Metadata *getMetadataIfResolved(unsigned Idx); | |||
| 176 | ||||
| 177 | MDNode *getMDNodeFwdRefOrNull(unsigned Idx); | |||
| 178 | void assignValue(Metadata *MD, unsigned Idx); | |||
| 179 | void tryToResolveCycles(); | |||
| 180 | bool hasFwdRefs() const { return !ForwardReference.empty(); } | |||
| 181 | int getNextFwdRef() { | |||
| 182 | assert(hasFwdRefs())((void)0); | |||
| 183 | return *ForwardReference.begin(); | |||
| 184 | } | |||
| 185 | ||||
| 186 | /// Upgrade a type that had an MDString reference. | |||
| 187 | void addTypeRef(MDString &UUID, DICompositeType &CT); | |||
| 188 | ||||
| 189 | /// Upgrade a type that had an MDString reference. | |||
| 190 | Metadata *upgradeTypeRef(Metadata *MaybeUUID); | |||
| 191 | ||||
| 192 | /// Upgrade a type ref array that may have MDString references. | |||
| 193 | Metadata *upgradeTypeRefArray(Metadata *MaybeTuple); | |||
| 194 | ||||
| 195 | private: | |||
| 196 | Metadata *resolveTypeRefArray(Metadata *MaybeTuple); | |||
| 197 | }; | |||
| 198 | ||||
| 199 | void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) { | |||
| 200 | if (auto *MDN = dyn_cast<MDNode>(MD)) | |||
| 201 | if (!MDN->isResolved()) | |||
| 202 | UnresolvedNodes.insert(Idx); | |||
| 203 | ||||
| 204 | if (Idx == size()) { | |||
| 205 | push_back(MD); | |||
| 206 | return; | |||
| 207 | } | |||
| 208 | ||||
| 209 | if (Idx >= size()) | |||
| 210 | resize(Idx + 1); | |||
| 211 | ||||
| 212 | TrackingMDRef &OldMD = MetadataPtrs[Idx]; | |||
| 213 | if (!OldMD) { | |||
| 214 | OldMD.reset(MD); | |||
| 215 | return; | |||
| 216 | } | |||
| 217 | ||||
| 218 | // If there was a forward reference to this value, replace it. | |||
| 219 | TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); | |||
| 220 | PrevMD->replaceAllUsesWith(MD); | |||
| 221 | ForwardReference.erase(Idx); | |||
| 222 | } | |||
| 223 | ||||
| 224 | Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) { | |||
| 225 | // Bail out for a clearly invalid value. | |||
| 226 | if (Idx >= RefsUpperBound) | |||
| 227 | return nullptr; | |||
| 228 | ||||
| 229 | if (Idx >= size()) | |||
| 230 | resize(Idx + 1); | |||
| 231 | ||||
| 232 | if (Metadata *MD = MetadataPtrs[Idx]) | |||
| 233 | return MD; | |||
| 234 | ||||
| 235 | // Track forward refs to be resolved later. | |||
| 236 | ForwardReference.insert(Idx); | |||
| 237 | ||||
| 238 | // Create and return a placeholder, which will later be RAUW'd. | |||
| 239 | ++NumMDNodeTemporary; | |||
| 240 | Metadata *MD = MDNode::getTemporary(Context, None).release(); | |||
| 241 | MetadataPtrs[Idx].reset(MD); | |||
| 242 | return MD; | |||
| 243 | } | |||
| 244 | ||||
| 245 | Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) { | |||
| 246 | Metadata *MD = lookup(Idx); | |||
| 247 | if (auto *N = dyn_cast_or_null<MDNode>(MD)) | |||
| 248 | if (!N->isResolved()) | |||
| 249 | return nullptr; | |||
| 250 | return MD; | |||
| 251 | } | |||
| 252 | ||||
| 253 | MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) { | |||
| 254 | return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx)); | |||
| 255 | } | |||
| 256 | ||||
| 257 | void BitcodeReaderMetadataList::tryToResolveCycles() { | |||
| 258 | if (!ForwardReference.empty()) | |||
| 259 | // Still forward references... can't resolve cycles. | |||
| 260 | return; | |||
| 261 | ||||
| 262 | // Give up on finding a full definition for any forward decls that remain. | |||
| 263 | for (const auto &Ref : OldTypeRefs.FwdDecls) | |||
| 264 | OldTypeRefs.Final.insert(Ref); | |||
| 265 | OldTypeRefs.FwdDecls.clear(); | |||
| 266 | ||||
| 267 | // Upgrade from old type ref arrays. In strange cases, this could add to | |||
| 268 | // OldTypeRefs.Unknown. | |||
| 269 | for (const auto &Array : OldTypeRefs.Arrays) | |||
| 270 | Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get())); | |||
| 271 | OldTypeRefs.Arrays.clear(); | |||
| 272 | ||||
| 273 | // Replace old string-based type refs with the resolved node, if possible. | |||
| 274 | // If we haven't seen the node, leave it to the verifier to complain about | |||
| 275 | // the invalid string reference. | |||
| 276 | for (const auto &Ref : OldTypeRefs.Unknown) { | |||
| 277 | if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first)) | |||
| 278 | Ref.second->replaceAllUsesWith(CT); | |||
| 279 | else | |||
| 280 | Ref.second->replaceAllUsesWith(Ref.first); | |||
| 281 | } | |||
| 282 | OldTypeRefs.Unknown.clear(); | |||
| 283 | ||||
| 284 | if (UnresolvedNodes.empty()) | |||
| 285 | // Nothing to do. | |||
| 286 | return; | |||
| 287 | ||||
| 288 | // Resolve any cycles. | |||
| 289 | for (unsigned I : UnresolvedNodes) { | |||
| 290 | auto &MD = MetadataPtrs[I]; | |||
| 291 | auto *N = dyn_cast_or_null<MDNode>(MD); | |||
| 292 | if (!N) | |||
| 293 | continue; | |||
| 294 | ||||
| 295 | assert(!N->isTemporary() && "Unexpected forward reference")((void)0); | |||
| 296 | N->resolveCycles(); | |||
| 297 | } | |||
| 298 | ||||
| 299 | // Make sure we return early again until there's another unresolved ref. | |||
| 300 | UnresolvedNodes.clear(); | |||
| 301 | } | |||
| 302 | ||||
| 303 | void BitcodeReaderMetadataList::addTypeRef(MDString &UUID, | |||
| 304 | DICompositeType &CT) { | |||
| 305 | assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID")((void)0); | |||
| 306 | if (CT.isForwardDecl()) | |||
| 307 | OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT)); | |||
| 308 | else | |||
| 309 | OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT)); | |||
| 310 | } | |||
| 311 | ||||
| 312 | Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) { | |||
| 313 | auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID); | |||
| 314 | if (LLVM_LIKELY(!UUID)__builtin_expect((bool)(!UUID), true)) | |||
| 315 | return MaybeUUID; | |||
| 316 | ||||
| 317 | if (auto *CT = OldTypeRefs.Final.lookup(UUID)) | |||
| 318 | return CT; | |||
| 319 | ||||
| 320 | auto &Ref = OldTypeRefs.Unknown[UUID]; | |||
| 321 | if (!Ref) | |||
| 322 | Ref = MDNode::getTemporary(Context, None); | |||
| 323 | return Ref.get(); | |||
| 324 | } | |||
| 325 | ||||
| 326 | Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) { | |||
| 327 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); | |||
| 328 | if (!Tuple || Tuple->isDistinct()) | |||
| 329 | return MaybeTuple; | |||
| 330 | ||||
| 331 | // Look through the array immediately if possible. | |||
| 332 | if (!Tuple->isTemporary()) | |||
| 333 | return resolveTypeRefArray(Tuple); | |||
| 334 | ||||
| 335 | // Create and return a placeholder to use for now. Eventually | |||
| 336 | // resolveTypeRefArrays() will be resolve this forward reference. | |||
| 337 | OldTypeRefs.Arrays.emplace_back( | |||
| 338 | std::piecewise_construct, std::forward_as_tuple(Tuple), | |||
| 339 | std::forward_as_tuple(MDTuple::getTemporary(Context, None))); | |||
| 340 | return OldTypeRefs.Arrays.back().second.get(); | |||
| 341 | } | |||
| 342 | ||||
| 343 | Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) { | |||
| 344 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); | |||
| 345 | if (!Tuple || Tuple->isDistinct()) | |||
| 346 | return MaybeTuple; | |||
| 347 | ||||
| 348 | // Look through the DITypeRefArray, upgrading each DIType *. | |||
| 349 | SmallVector<Metadata *, 32> Ops; | |||
| 350 | Ops.reserve(Tuple->getNumOperands()); | |||
| 351 | for (Metadata *MD : Tuple->operands()) | |||
| 352 | Ops.push_back(upgradeTypeRef(MD)); | |||
| 353 | ||||
| 354 | return MDTuple::get(Context, Ops); | |||
| 355 | } | |||
| 356 | ||||
| 357 | namespace { | |||
| 358 | ||||
| 359 | class PlaceholderQueue { | |||
| 360 | // Placeholders would thrash around when moved, so store in a std::deque | |||
| 361 | // instead of some sort of vector. | |||
| 362 | std::deque<DistinctMDOperandPlaceholder> PHs; | |||
| 363 | ||||
| 364 | public: | |||
| 365 | ~PlaceholderQueue() { | |||
| 366 | assert(empty() && "PlaceholderQueue hasn't been flushed before being destroyed")((void)0); | |||
| 367 | } | |||
| 368 | bool empty() const { return PHs.empty(); } | |||
| 369 | DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID); | |||
| 370 | void flush(BitcodeReaderMetadataList &MetadataList); | |||
| 371 | ||||
| 372 | /// Return the list of temporaries nodes in the queue, these need to be | |||
| 373 | /// loaded before we can flush the queue. | |||
| 374 | void getTemporaries(BitcodeReaderMetadataList &MetadataList, | |||
| 375 | DenseSet<unsigned> &Temporaries) { | |||
| 376 | for (auto &PH : PHs) { | |||
| 377 | auto ID = PH.getID(); | |||
| 378 | auto *MD = MetadataList.lookup(ID); | |||
| 379 | if (!MD) { | |||
| 380 | Temporaries.insert(ID); | |||
| 381 | continue; | |||
| 382 | } | |||
| 383 | auto *N = dyn_cast_or_null<MDNode>(MD); | |||
| 384 | if (N && N->isTemporary()) | |||
| 385 | Temporaries.insert(ID); | |||
| 386 | } | |||
| 387 | } | |||
| 388 | }; | |||
| 389 | ||||
| 390 | } // end anonymous namespace | |||
| 391 | ||||
| 392 | DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) { | |||
| 393 | PHs.emplace_back(ID); | |||
| 394 | return PHs.back(); | |||
| 395 | } | |||
| 396 | ||||
| 397 | void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) { | |||
| 398 | while (!PHs.empty()) { | |||
| 399 | auto *MD = MetadataList.lookup(PHs.front().getID()); | |||
| 400 | assert(MD && "Flushing placeholder on unassigned MD")((void)0); | |||
| 401 | #ifndef NDEBUG1 | |||
| 402 | if (auto *MDN = dyn_cast<MDNode>(MD)) | |||
| 403 | assert(MDN->isResolved() &&((void)0) | |||
| 404 | "Flushing Placeholder while cycles aren't resolved")((void)0); | |||
| 405 | #endif | |||
| 406 | PHs.front().replaceUseWith(MD); | |||
| 407 | PHs.pop_front(); | |||
| 408 | } | |||
| 409 | } | |||
| 410 | ||||
| 411 | } // anonymous namespace | |||
| 412 | ||||
| 413 | static Error error(const Twine &Message) { | |||
| 414 | return make_error<StringError>( | |||
| 415 | Message, make_error_code(BitcodeError::CorruptedBitcode)); | |||
| 416 | } | |||
| 417 | ||||
| 418 | class MetadataLoader::MetadataLoaderImpl { | |||
| 419 | BitcodeReaderMetadataList MetadataList; | |||
| 420 | BitcodeReaderValueList &ValueList; | |||
| 421 | BitstreamCursor &Stream; | |||
| 422 | LLVMContext &Context; | |||
| 423 | Module &TheModule; | |||
| 424 | std::function<Type *(unsigned)> getTypeByID; | |||
| 425 | ||||
| 426 | /// Cursor associated with the lazy-loading of Metadata. This is the easy way | |||
| 427 | /// to keep around the right "context" (Abbrev list) to be able to jump in | |||
| 428 | /// the middle of the metadata block and load any record. | |||
| 429 | BitstreamCursor IndexCursor; | |||
| 430 | ||||
| 431 | /// Index that keeps track of MDString values. | |||
| 432 | std::vector<StringRef> MDStringRef; | |||
| 433 | ||||
| 434 | /// On-demand loading of a single MDString. Requires the index above to be | |||
| 435 | /// populated. | |||
| 436 | MDString *lazyLoadOneMDString(unsigned Idx); | |||
| 437 | ||||
| 438 | /// Index that keeps track of where to find a metadata record in the stream. | |||
| 439 | std::vector<uint64_t> GlobalMetadataBitPosIndex; | |||
| 440 | ||||
| 441 | /// Cursor position of the start of the global decl attachments, to enable | |||
| 442 | /// loading using the index built for lazy loading, instead of forward | |||
| 443 | /// references. | |||
| 444 | uint64_t GlobalDeclAttachmentPos = 0; | |||
| 445 | ||||
| 446 | #ifndef NDEBUG1 | |||
| 447 | /// Sanity check that we end up parsing all of the global decl attachments. | |||
| 448 | unsigned NumGlobalDeclAttachSkipped = 0; | |||
| 449 | unsigned NumGlobalDeclAttachParsed = 0; | |||
| 450 | #endif | |||
| 451 | ||||
| 452 | /// Load the global decl attachments, using the index built for lazy loading. | |||
| 453 | Expected<bool> loadGlobalDeclAttachments(); | |||
| 454 | ||||
| 455 | /// Populate the index above to enable lazily loading of metadata, and load | |||
| 456 | /// the named metadata as well as the transitively referenced global | |||
| 457 | /// Metadata. | |||
| 458 | Expected<bool> lazyLoadModuleMetadataBlock(); | |||
| 459 | ||||
| 460 | /// On-demand loading of a single metadata. Requires the index above to be | |||
| 461 | /// populated. | |||
| 462 | void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders); | |||
| 463 | ||||
| 464 | // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to | |||
| 465 | // point from SP to CU after a block is completly parsed. | |||
| 466 | std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms; | |||
| 467 | ||||
| 468 | /// Functions that need to be matched with subprograms when upgrading old | |||
| 469 | /// metadata. | |||
| 470 | SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs; | |||
| 471 | ||||
| 472 | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. | |||
| 473 | DenseMap<unsigned, unsigned> MDKindMap; | |||
| 474 | ||||
| 475 | bool StripTBAA = false; | |||
| 476 | bool HasSeenOldLoopTags = false; | |||
| 477 | bool NeedUpgradeToDIGlobalVariableExpression = false; | |||
| 478 | bool NeedDeclareExpressionUpgrade = false; | |||
| 479 | ||||
| 480 | /// True if metadata is being parsed for a module being ThinLTO imported. | |||
| 481 | bool IsImporting = false; | |||
| 482 | ||||
| 483 | Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code, | |||
| 484 | PlaceholderQueue &Placeholders, StringRef Blob, | |||
| 485 | unsigned &NextMetadataNo); | |||
| 486 | Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob, | |||
| 487 | function_ref<void(StringRef)> CallBack); | |||
| 488 | Error parseGlobalObjectAttachment(GlobalObject &GO, | |||
| 489 | ArrayRef<uint64_t> Record); | |||
| 490 | Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record); | |||
| 491 | ||||
| 492 | void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders); | |||
| 493 | ||||
| 494 | /// Upgrade old-style CU <-> SP pointers to point from SP to CU. | |||
| 495 | void upgradeCUSubprograms() { | |||
| 496 | for (auto CU_SP : CUSubprograms) | |||
| 497 | if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second)) | |||
| 498 | for (auto &Op : SPs->operands()) | |||
| 499 | if (auto *SP = dyn_cast_or_null<DISubprogram>(Op)) | |||
| 500 | SP->replaceUnit(CU_SP.first); | |||
| 501 | CUSubprograms.clear(); | |||
| 502 | } | |||
| 503 | ||||
| 504 | /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions. | |||
| 505 | void upgradeCUVariables() { | |||
| 506 | if (!NeedUpgradeToDIGlobalVariableExpression) | |||
| 507 | return; | |||
| 508 | ||||
| 509 | // Upgrade list of variables attached to the CUs. | |||
| 510 | if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) | |||
| 511 | for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) { | |||
| 512 | auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I)); | |||
| 513 | if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables())) | |||
| 514 | for (unsigned I = 0; I < GVs->getNumOperands(); I++) | |||
| 515 | if (auto *GV = | |||
| 516 | dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) { | |||
| 517 | auto *DGVE = DIGlobalVariableExpression::getDistinct( | |||
| 518 | Context, GV, DIExpression::get(Context, {})); | |||
| 519 | GVs->replaceOperandWith(I, DGVE); | |||
| 520 | } | |||
| 521 | } | |||
| 522 | ||||
| 523 | // Upgrade variables attached to globals. | |||
| 524 | for (auto &GV : TheModule.globals()) { | |||
| 525 | SmallVector<MDNode *, 1> MDs; | |||
| 526 | GV.getMetadata(LLVMContext::MD_dbg, MDs); | |||
| 527 | GV.eraseMetadata(LLVMContext::MD_dbg); | |||
| 528 | for (auto *MD : MDs) | |||
| 529 | if (auto *DGV = dyn_cast<DIGlobalVariable>(MD)) { | |||
| 530 | auto *DGVE = DIGlobalVariableExpression::getDistinct( | |||
| 531 | Context, DGV, DIExpression::get(Context, {})); | |||
| 532 | GV.addMetadata(LLVMContext::MD_dbg, *DGVE); | |||
| 533 | } else | |||
| 534 | GV.addMetadata(LLVMContext::MD_dbg, *MD); | |||
| 535 | } | |||
| 536 | } | |||
| 537 | ||||
| 538 | /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that | |||
| 539 | /// describes a function argument. | |||
| 540 | void upgradeDeclareExpressions(Function &F) { | |||
| 541 | if (!NeedDeclareExpressionUpgrade) | |||
| 542 | return; | |||
| 543 | ||||
| 544 | for (auto &BB : F) | |||
| 545 | for (auto &I : BB) | |||
| 546 | if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) | |||
| 547 | if (auto *DIExpr = DDI->getExpression()) | |||
| 548 | if (DIExpr->startsWithDeref() && | |||
| 549 | dyn_cast_or_null<Argument>(DDI->getAddress())) { | |||
| 550 | SmallVector<uint64_t, 8> Ops; | |||
| 551 | Ops.append(std::next(DIExpr->elements_begin()), | |||
| 552 | DIExpr->elements_end()); | |||
| 553 | DDI->setExpression(DIExpression::get(Context, Ops)); | |||
| 554 | } | |||
| 555 | } | |||
| 556 | ||||
| 557 | /// Upgrade the expression from previous versions. | |||
| 558 | Error upgradeDIExpression(uint64_t FromVersion, | |||
| 559 | MutableArrayRef<uint64_t> &Expr, | |||
| 560 | SmallVectorImpl<uint64_t> &Buffer) { | |||
| 561 | auto N = Expr.size(); | |||
| 562 | switch (FromVersion) { | |||
| 563 | default: | |||
| 564 | return error("Invalid record"); | |||
| 565 | case 0: | |||
| 566 | if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece) | |||
| 567 | Expr[N - 3] = dwarf::DW_OP_LLVM_fragment; | |||
| 568 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
| 569 | case 1: | |||
| 570 | // Move DW_OP_deref to the end. | |||
| 571 | if (N && Expr[0] == dwarf::DW_OP_deref) { | |||
| 572 | auto End = Expr.end(); | |||
| 573 | if (Expr.size() >= 3 && | |||
| 574 | *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment) | |||
| 575 | End = std::prev(End, 3); | |||
| 576 | std::move(std::next(Expr.begin()), End, Expr.begin()); | |||
| 577 | *std::prev(End) = dwarf::DW_OP_deref; | |||
| 578 | } | |||
| 579 | NeedDeclareExpressionUpgrade = true; | |||
| 580 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
| 581 | case 2: { | |||
| 582 | // Change DW_OP_plus to DW_OP_plus_uconst. | |||
| 583 | // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus | |||
| 584 | auto SubExpr = ArrayRef<uint64_t>(Expr); | |||
| 585 | while (!SubExpr.empty()) { | |||
| 586 | // Skip past other operators with their operands | |||
| 587 | // for this version of the IR, obtained from | |||
| 588 | // from historic DIExpression::ExprOperand::getSize(). | |||
| 589 | size_t HistoricSize; | |||
| 590 | switch (SubExpr.front()) { | |||
| 591 | default: | |||
| 592 | HistoricSize = 1; | |||
| 593 | break; | |||
| 594 | case dwarf::DW_OP_constu: | |||
| 595 | case dwarf::DW_OP_minus: | |||
| 596 | case dwarf::DW_OP_plus: | |||
| 597 | HistoricSize = 2; | |||
| 598 | break; | |||
| 599 | case dwarf::DW_OP_LLVM_fragment: | |||
| 600 | HistoricSize = 3; | |||
| 601 | break; | |||
| 602 | } | |||
| 603 | ||||
| 604 | // If the expression is malformed, make sure we don't | |||
| 605 | // copy more elements than we should. | |||
| 606 | HistoricSize = std::min(SubExpr.size(), HistoricSize); | |||
| 607 | ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize-1); | |||
| 608 | ||||
| 609 | switch (SubExpr.front()) { | |||
| 610 | case dwarf::DW_OP_plus: | |||
| 611 | Buffer.push_back(dwarf::DW_OP_plus_uconst); | |||
| 612 | Buffer.append(Args.begin(), Args.end()); | |||
| 613 | break; | |||
| 614 | case dwarf::DW_OP_minus: | |||
| 615 | Buffer.push_back(dwarf::DW_OP_constu); | |||
| 616 | Buffer.append(Args.begin(), Args.end()); | |||
| 617 | Buffer.push_back(dwarf::DW_OP_minus); | |||
| 618 | break; | |||
| 619 | default: | |||
| 620 | Buffer.push_back(*SubExpr.begin()); | |||
| 621 | Buffer.append(Args.begin(), Args.end()); | |||
| 622 | break; | |||
| 623 | } | |||
| 624 | ||||
| 625 | // Continue with remaining elements. | |||
| 626 | SubExpr = SubExpr.slice(HistoricSize); | |||
| 627 | } | |||
| 628 | Expr = MutableArrayRef<uint64_t>(Buffer); | |||
| 629 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
| 630 | } | |||
| 631 | case 3: | |||
| 632 | // Up-to-date! | |||
| 633 | break; | |||
| 634 | } | |||
| 635 | ||||
| 636 | return Error::success(); | |||
| 637 | } | |||
| 638 | ||||
| 639 | void upgradeDebugInfo() { | |||
| 640 | upgradeCUSubprograms(); | |||
| 641 | upgradeCUVariables(); | |||
| 642 | } | |||
| 643 | ||||
| 644 | public: | |||
| 645 | MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule, | |||
| 646 | BitcodeReaderValueList &ValueList, | |||
| 647 | std::function<Type *(unsigned)> getTypeByID, | |||
| 648 | bool IsImporting) | |||
| 649 | : MetadataList(TheModule.getContext(), Stream.SizeInBytes()), | |||
| 650 | ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()), | |||
| 651 | TheModule(TheModule), getTypeByID(std::move(getTypeByID)), | |||
| 652 | IsImporting(IsImporting) {} | |||
| 653 | ||||
| 654 | Error parseMetadata(bool ModuleLevel); | |||
| 655 | ||||
| 656 | bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); } | |||
| 657 | ||||
| 658 | Metadata *getMetadataFwdRefOrLoad(unsigned ID) { | |||
| 659 | if (ID < MDStringRef.size()) | |||
| 660 | return lazyLoadOneMDString(ID); | |||
| 661 | if (auto *MD = MetadataList.lookup(ID)) | |||
| 662 | return MD; | |||
| 663 | // If lazy-loading is enabled, we try recursively to load the operand | |||
| 664 | // instead of creating a temporary. | |||
| 665 | if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { | |||
| 666 | PlaceholderQueue Placeholders; | |||
| 667 | lazyLoadOneMetadata(ID, Placeholders); | |||
| 668 | resolveForwardRefsAndPlaceholders(Placeholders); | |||
| 669 | return MetadataList.lookup(ID); | |||
| 670 | } | |||
| 671 | return MetadataList.getMetadataFwdRef(ID); | |||
| 672 | } | |||
| 673 | ||||
| 674 | DISubprogram *lookupSubprogramForFunction(Function *F) { | |||
| 675 | return FunctionsWithSPs.lookup(F); | |||
| 676 | } | |||
| 677 | ||||
| 678 | bool hasSeenOldLoopTags() const { return HasSeenOldLoopTags; } | |||
| 679 | ||||
| 680 | Error parseMetadataAttachment( | |||
| 681 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList); | |||
| 682 | ||||
| 683 | Error parseMetadataKinds(); | |||
| 684 | ||||
| 685 | void setStripTBAA(bool Value) { StripTBAA = Value; } | |||
| 686 | bool isStrippingTBAA() const { return StripTBAA; } | |||
| 687 | ||||
| 688 | unsigned size() const { return MetadataList.size(); } | |||
| 689 | void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); } | |||
| 690 | void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); } | |||
| 691 | }; | |||
| 692 | ||||
| 693 | Expected<bool> | |||
| 694 | MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() { | |||
| 695 | IndexCursor = Stream; | |||
| 696 | SmallVector<uint64_t, 64> Record; | |||
| 697 | GlobalDeclAttachmentPos = 0; | |||
| 698 | // Get the abbrevs, and preload record positions to make them lazy-loadable. | |||
| 699 | while (true) { | |||
| ||||
| 700 | uint64_t SavedPos = IndexCursor.GetCurrentBitNo(); | |||
| 701 | Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks( | |||
| 702 | BitstreamCursor::AF_DontPopBlockAtEnd); | |||
| 703 | if (!MaybeEntry) | |||
| 704 | return MaybeEntry.takeError(); | |||
| 705 | BitstreamEntry Entry = MaybeEntry.get(); | |||
| 706 | ||||
| 707 | switch (Entry.Kind) { | |||
| 708 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
| 709 | case BitstreamEntry::Error: | |||
| 710 | return error("Malformed block"); | |||
| 711 | case BitstreamEntry::EndBlock: { | |||
| 712 | return true; | |||
| 713 | } | |||
| 714 | case BitstreamEntry::Record: { | |||
| 715 | // The interesting case. | |||
| 716 | ++NumMDRecordLoaded; | |||
| 717 | uint64_t CurrentPos = IndexCursor.GetCurrentBitNo(); | |||
| 718 | Expected<unsigned> MaybeCode = IndexCursor.skipRecord(Entry.ID); | |||
| 719 | if (!MaybeCode) | |||
| 720 | return MaybeCode.takeError(); | |||
| 721 | unsigned Code = MaybeCode.get(); | |||
| 722 | switch (Code) { | |||
| 723 | case bitc::METADATA_STRINGS: { | |||
| 724 | // Rewind and parse the strings. | |||
| 725 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) | |||
| 726 | return std::move(Err); | |||
| 727 | StringRef Blob; | |||
| 728 | Record.clear(); | |||
| 729 | if (Expected<unsigned> MaybeRecord = | |||
| 730 | IndexCursor.readRecord(Entry.ID, Record, &Blob)) | |||
| 731 | ; | |||
| 732 | else | |||
| 733 | return MaybeRecord.takeError(); | |||
| 734 | unsigned NumStrings = Record[0]; | |||
| 735 | MDStringRef.reserve(NumStrings); | |||
| 736 | auto IndexNextMDString = [&](StringRef Str) { | |||
| 737 | MDStringRef.push_back(Str); | |||
| 738 | }; | |||
| 739 | if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString)) | |||
| 740 | return std::move(Err); | |||
| 741 | break; | |||
| 742 | } | |||
| 743 | case bitc::METADATA_INDEX_OFFSET: { | |||
| 744 | // This is the offset to the index, when we see this we skip all the | |||
| 745 | // records and load only an index to these. | |||
| 746 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) | |||
| 747 | return std::move(Err); | |||
| 748 | Record.clear(); | |||
| 749 | if (Expected<unsigned> MaybeRecord = | |||
| 750 | IndexCursor.readRecord(Entry.ID, Record)) | |||
| 751 | ; | |||
| 752 | else | |||
| 753 | return MaybeRecord.takeError(); | |||
| 754 | if (Record.size() != 2) | |||
| 755 | return error("Invalid record"); | |||
| 756 | auto Offset = Record[0] + (Record[1] << 32); | |||
| 757 | auto BeginPos = IndexCursor.GetCurrentBitNo(); | |||
| 758 | if (Error Err = IndexCursor.JumpToBit(BeginPos + Offset)) | |||
| 759 | return std::move(Err); | |||
| 760 | Expected<BitstreamEntry> MaybeEntry = | |||
| 761 | IndexCursor.advanceSkippingSubblocks( | |||
| 762 | BitstreamCursor::AF_DontPopBlockAtEnd); | |||
| 763 | if (!MaybeEntry) | |||
| 764 | return MaybeEntry.takeError(); | |||
| 765 | Entry = MaybeEntry.get(); | |||
| 766 | assert(Entry.Kind == BitstreamEntry::Record &&((void)0) | |||
| 767 | "Corrupted bitcode: Expected `Record` when trying to find the "((void)0) | |||
| 768 | "Metadata index")((void)0); | |||
| 769 | Record.clear(); | |||
| 770 | if (Expected<unsigned> MaybeCode = | |||
| 771 | IndexCursor.readRecord(Entry.ID, Record)) | |||
| ||||
| 772 | assert(MaybeCode.get() == bitc::METADATA_INDEX &&((void)0) | |||
| 773 | "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "((void)0) | |||
| 774 | "find the Metadata index")((void)0); | |||
| 775 | else | |||
| 776 | return MaybeCode.takeError(); | |||
| 777 | // Delta unpack | |||
| 778 | auto CurrentValue = BeginPos; | |||
| 779 | GlobalMetadataBitPosIndex.reserve(Record.size()); | |||
| 780 | for (auto &Elt : Record) { | |||
| 781 | CurrentValue += Elt; | |||
| 782 | GlobalMetadataBitPosIndex.push_back(CurrentValue); | |||
| 783 | } | |||
| 784 | break; | |||
| 785 | } | |||
| 786 | case bitc::METADATA_INDEX: | |||
| 787 | // We don't expect to get there, the Index is loaded when we encounter | |||
| 788 | // the offset. | |||
| 789 | return error("Corrupted Metadata block"); | |||
| 790 | case bitc::METADATA_NAME: { | |||
| 791 | // Named metadata need to be materialized now and aren't deferred. | |||
| 792 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) | |||
| 793 | return std::move(Err); | |||
| 794 | Record.clear(); | |||
| 795 | ||||
| 796 | unsigned Code; | |||
| 797 | if (Expected<unsigned> MaybeCode = | |||
| 798 | IndexCursor.readRecord(Entry.ID, Record)) { | |||
| 799 | Code = MaybeCode.get(); | |||
| 800 | assert(Code == bitc::METADATA_NAME)((void)0); | |||
| 801 | } else | |||
| 802 | return MaybeCode.takeError(); | |||
| 803 | ||||
| 804 | // Read name of the named metadata. | |||
| 805 | SmallString<8> Name(Record.begin(), Record.end()); | |||
| 806 | if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode()) | |||
| 807 | Code = MaybeCode.get(); | |||
| 808 | else | |||
| 809 | return MaybeCode.takeError(); | |||
| 810 | ||||
| 811 | // Named Metadata comes in two parts, we expect the name to be followed | |||
| 812 | // by the node | |||
| 813 | Record.clear(); | |||
| 814 | if (Expected<unsigned> MaybeNextBitCode = | |||
| 815 | IndexCursor.readRecord(Code, Record)) | |||
| 816 | assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE)((void)0); | |||
| 817 | else | |||
| 818 | return MaybeNextBitCode.takeError(); | |||
| 819 | ||||
| 820 | // Read named metadata elements. | |||
| 821 | unsigned Size = Record.size(); | |||
| 822 | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); | |||
| 823 | for (unsigned i = 0; i != Size; ++i) { | |||
| 824 | // FIXME: We could use a placeholder here, however NamedMDNode are | |||
| 825 | // taking MDNode as operand and not using the Metadata infrastructure. | |||
| 826 | // It is acknowledged by 'TODO: Inherit from Metadata' in the | |||
| 827 | // NamedMDNode class definition. | |||
| 828 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); | |||
| 829 | assert(MD && "Invalid metadata: expect fwd ref to MDNode")((void)0); | |||
| 830 | NMD->addOperand(MD); | |||
| 831 | } | |||
| 832 | break; | |||
| 833 | } | |||
| 834 | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { | |||
| 835 | if (!GlobalDeclAttachmentPos) | |||
| 836 | GlobalDeclAttachmentPos = SavedPos; | |||
| 837 | #ifndef NDEBUG1 | |||
| 838 | NumGlobalDeclAttachSkipped++; | |||
| 839 | #endif | |||
| 840 | break; | |||
| 841 | } | |||
| 842 | case bitc::METADATA_KIND: | |||
| 843 | case bitc::METADATA_STRING_OLD: | |||
| 844 | case bitc::METADATA_OLD_FN_NODE: | |||
| 845 | case bitc::METADATA_OLD_NODE: | |||
| 846 | case bitc::METADATA_VALUE: | |||
| 847 | case bitc::METADATA_DISTINCT_NODE: | |||
| 848 | case bitc::METADATA_NODE: | |||
| 849 | case bitc::METADATA_LOCATION: | |||
| 850 | case bitc::METADATA_GENERIC_DEBUG: | |||
| 851 | case bitc::METADATA_SUBRANGE: | |||
| 852 | case bitc::METADATA_ENUMERATOR: | |||
| 853 | case bitc::METADATA_BASIC_TYPE: | |||
| 854 | case bitc::METADATA_STRING_TYPE: | |||
| 855 | case bitc::METADATA_DERIVED_TYPE: | |||
| 856 | case bitc::METADATA_COMPOSITE_TYPE: | |||
| 857 | case bitc::METADATA_SUBROUTINE_TYPE: | |||
| 858 | case bitc::METADATA_MODULE: | |||
| 859 | case bitc::METADATA_FILE: | |||
| 860 | case bitc::METADATA_COMPILE_UNIT: | |||
| 861 | case bitc::METADATA_SUBPROGRAM: | |||
| 862 | case bitc::METADATA_LEXICAL_BLOCK: | |||
| 863 | case bitc::METADATA_LEXICAL_BLOCK_FILE: | |||
| 864 | case bitc::METADATA_NAMESPACE: | |||
| 865 | case bitc::METADATA_COMMON_BLOCK: | |||
| 866 | case bitc::METADATA_MACRO: | |||
| 867 | case bitc::METADATA_MACRO_FILE: | |||
| 868 | case bitc::METADATA_TEMPLATE_TYPE: | |||
| 869 | case bitc::METADATA_TEMPLATE_VALUE: | |||
| 870 | case bitc::METADATA_GLOBAL_VAR: | |||
| 871 | case bitc::METADATA_LOCAL_VAR: | |||
| 872 | case bitc::METADATA_LABEL: | |||
| 873 | case bitc::METADATA_EXPRESSION: | |||
| 874 | case bitc::METADATA_OBJC_PROPERTY: | |||
| 875 | case bitc::METADATA_IMPORTED_ENTITY: | |||
| 876 | case bitc::METADATA_GLOBAL_VAR_EXPR: | |||
| 877 | case bitc::METADATA_GENERIC_SUBRANGE: | |||
| 878 | // We don't expect to see any of these, if we see one, give up on | |||
| 879 | // lazy-loading and fallback. | |||
| 880 | MDStringRef.clear(); | |||
| 881 | GlobalMetadataBitPosIndex.clear(); | |||
| 882 | return false; | |||
| 883 | } | |||
| 884 | break; | |||
| 885 | } | |||
| 886 | } | |||
| 887 | } | |||
| 888 | } | |||
| 889 | ||||
| 890 | // Load the global decl attachments after building the lazy loading index. | |||
| 891 | // We don't load them "lazily" - all global decl attachments must be | |||
| 892 | // parsed since they aren't materialized on demand. However, by delaying | |||
| 893 | // their parsing until after the index is created, we can use the index | |||
| 894 | // instead of creating temporaries. | |||
| 895 | Expected<bool> MetadataLoader::MetadataLoaderImpl::loadGlobalDeclAttachments() { | |||
| 896 | // Nothing to do if we didn't find any of these metadata records. | |||
| 897 | if (!GlobalDeclAttachmentPos) | |||
| 898 | return true; | |||
| 899 | // Use a temporary cursor so that we don't mess up the main Stream cursor or | |||
| 900 | // the lazy loading IndexCursor (which holds the necessary abbrev ids). | |||
| 901 | BitstreamCursor TempCursor = Stream; | |||
| 902 | SmallVector<uint64_t, 64> Record; | |||
| 903 | // Jump to the position before the first global decl attachment, so we can | |||
| 904 | // scan for the first BitstreamEntry record. | |||
| 905 | if (Error Err = TempCursor.JumpToBit(GlobalDeclAttachmentPos)) | |||
| 906 | return std::move(Err); | |||
| 907 | while (true) { | |||
| 908 | Expected<BitstreamEntry> MaybeEntry = TempCursor.advanceSkippingSubblocks( | |||
| 909 | BitstreamCursor::AF_DontPopBlockAtEnd); | |||
| 910 | if (!MaybeEntry) | |||
| 911 | return MaybeEntry.takeError(); | |||
| 912 | BitstreamEntry Entry = MaybeEntry.get(); | |||
| 913 | ||||
| 914 | switch (Entry.Kind) { | |||
| 915 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
| 916 | case BitstreamEntry::Error: | |||
| 917 | return error("Malformed block"); | |||
| 918 | case BitstreamEntry::EndBlock: | |||
| 919 | // Sanity check that we parsed them all. | |||
| 920 | assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed)((void)0); | |||
| 921 | return true; | |||
| 922 | case BitstreamEntry::Record: | |||
| 923 | break; | |||
| 924 | } | |||
| 925 | uint64_t CurrentPos = TempCursor.GetCurrentBitNo(); | |||
| 926 | Expected<unsigned> MaybeCode = TempCursor.skipRecord(Entry.ID); | |||
| 927 | if (!MaybeCode) | |||
| 928 | return MaybeCode.takeError(); | |||
| 929 | if (MaybeCode.get() != bitc::METADATA_GLOBAL_DECL_ATTACHMENT) { | |||
| 930 | // Anything other than a global decl attachment signals the end of | |||
| 931 | // these records. sanity check that we parsed them all. | |||
| 932 | assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed)((void)0); | |||
| 933 | return true; | |||
| 934 | } | |||
| 935 | #ifndef NDEBUG1 | |||
| 936 | NumGlobalDeclAttachParsed++; | |||
| 937 | #endif | |||
| 938 | // FIXME: we need to do this early because we don't materialize global | |||
| 939 | // value explicitly. | |||
| 940 | if (Error Err = TempCursor.JumpToBit(CurrentPos)) | |||
| 941 | return std::move(Err); | |||
| 942 | Record.clear(); | |||
| 943 | if (Expected<unsigned> MaybeRecord = | |||
| 944 | TempCursor.readRecord(Entry.ID, Record)) | |||
| 945 | ; | |||
| 946 | else | |||
| 947 | return MaybeRecord.takeError(); | |||
| 948 | if (Record.size() % 2 == 0) | |||
| 949 | return error("Invalid record"); | |||
| 950 | unsigned ValueID = Record[0]; | |||
| 951 | if (ValueID >= ValueList.size()) | |||
| 952 | return error("Invalid record"); | |||
| 953 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) { | |||
| 954 | // Need to save and restore the current position since | |||
| 955 | // parseGlobalObjectAttachment will resolve all forward references which | |||
| 956 | // would require parsing from locations stored in the index. | |||
| 957 | CurrentPos = TempCursor.GetCurrentBitNo(); | |||
| 958 | if (Error Err = parseGlobalObjectAttachment( | |||
| 959 | *GO, ArrayRef<uint64_t>(Record).slice(1))) | |||
| 960 | return std::move(Err); | |||
| 961 | if (Error Err = TempCursor.JumpToBit(CurrentPos)) | |||
| 962 | return std::move(Err); | |||
| 963 | } | |||
| 964 | } | |||
| 965 | } | |||
| 966 | ||||
| 967 | /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing | |||
| 968 | /// module level metadata. | |||
| 969 | Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) { | |||
| 970 | if (!ModuleLevel && MetadataList.hasFwdRefs()) | |||
| 971 | return error("Invalid metadata: fwd refs into function blocks"); | |||
| 972 | ||||
| 973 | // Record the entry position so that we can jump back here and efficiently | |||
| 974 | // skip the whole block in case we lazy-load. | |||
| 975 | auto EntryPos = Stream.GetCurrentBitNo(); | |||
| 976 | ||||
| 977 | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) | |||
| 978 | return Err; | |||
| 979 | ||||
| 980 | SmallVector<uint64_t, 64> Record; | |||
| 981 | PlaceholderQueue Placeholders; | |||
| 982 | ||||
| 983 | // We lazy-load module-level metadata: we build an index for each record, and | |||
| 984 | // then load individual record as needed, starting with the named metadata. | |||
| 985 | if (ModuleLevel && IsImporting && MetadataList.empty() && | |||
| 986 | !DisableLazyLoading) { | |||
| 987 | auto SuccessOrErr = lazyLoadModuleMetadataBlock(); | |||
| 988 | if (!SuccessOrErr) | |||
| 989 | return SuccessOrErr.takeError(); | |||
| 990 | if (SuccessOrErr.get()) { | |||
| 991 | // An index was successfully created and we will be able to load metadata | |||
| 992 | // on-demand. | |||
| 993 | MetadataList.resize(MDStringRef.size() + | |||
| 994 | GlobalMetadataBitPosIndex.size()); | |||
| 995 | ||||
| 996 | // Now that we have built the index, load the global decl attachments | |||
| 997 | // that were deferred during that process. This avoids creating | |||
| 998 | // temporaries. | |||
| 999 | SuccessOrErr = loadGlobalDeclAttachments(); | |||
| 1000 | if (!SuccessOrErr) | |||
| 1001 | return SuccessOrErr.takeError(); | |||
| 1002 | assert(SuccessOrErr.get())((void)0); | |||
| 1003 | ||||
| 1004 | // Reading the named metadata created forward references and/or | |||
| 1005 | // placeholders, that we flush here. | |||
| 1006 | resolveForwardRefsAndPlaceholders(Placeholders); | |||
| 1007 | upgradeDebugInfo(); | |||
| 1008 | // Return at the beginning of the block, since it is easy to skip it | |||
| 1009 | // entirely from there. | |||
| 1010 | Stream.ReadBlockEnd(); // Pop the abbrev block context. | |||
| 1011 | if (Error Err = IndexCursor.JumpToBit(EntryPos)) | |||
| 1012 | return Err; | |||
| 1013 | if (Error Err = Stream.SkipBlock()) { | |||
| 1014 | // FIXME this drops the error on the floor, which | |||
| 1015 | // ThinLTO/X86/debuginfo-cu-import.ll relies on. | |||
| 1016 | consumeError(std::move(Err)); | |||
| 1017 | return Error::success(); | |||
| 1018 | } | |||
| 1019 | return Error::success(); | |||
| 1020 | } | |||
| 1021 | // Couldn't load an index, fallback to loading all the block "old-style". | |||
| 1022 | } | |||
| 1023 | ||||
| 1024 | unsigned NextMetadataNo = MetadataList.size(); | |||
| 1025 | ||||
| 1026 | // Read all the records. | |||
| 1027 | while (true) { | |||
| 1028 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | |||
| 1029 | if (!MaybeEntry) | |||
| 1030 | return MaybeEntry.takeError(); | |||
| 1031 | BitstreamEntry Entry = MaybeEntry.get(); | |||
| 1032 | ||||
| 1033 | switch (Entry.Kind) { | |||
| 1034 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
| 1035 | case BitstreamEntry::Error: | |||
| 1036 | return error("Malformed block"); | |||
| 1037 | case BitstreamEntry::EndBlock: | |||
| 1038 | resolveForwardRefsAndPlaceholders(Placeholders); | |||
| 1039 | upgradeDebugInfo(); | |||
| 1040 | return Error::success(); | |||
| 1041 | case BitstreamEntry::Record: | |||
| 1042 | // The interesting case. | |||
| 1043 | break; | |||
| 1044 | } | |||
| 1045 | ||||
| 1046 | // Read a record. | |||
| 1047 | Record.clear(); | |||
| 1048 | StringRef Blob; | |||
| 1049 | ++NumMDRecordLoaded; | |||
| 1050 | if (Expected<unsigned> MaybeCode = | |||
| 1051 | Stream.readRecord(Entry.ID, Record, &Blob)) { | |||
| 1052 | if (Error Err = parseOneMetadata(Record, MaybeCode.get(), Placeholders, | |||
| 1053 | Blob, NextMetadataNo)) | |||
| 1054 | return Err; | |||
| 1055 | } else | |||
| 1056 | return MaybeCode.takeError(); | |||
| 1057 | } | |||
| 1058 | } | |||
| 1059 | ||||
| 1060 | MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) { | |||
| 1061 | ++NumMDStringLoaded; | |||
| 1062 | if (Metadata *MD = MetadataList.lookup(ID)) | |||
| 1063 | return cast<MDString>(MD); | |||
| 1064 | auto MDS = MDString::get(Context, MDStringRef[ID]); | |||
| 1065 | MetadataList.assignValue(MDS, ID); | |||
| 1066 | return MDS; | |||
| 1067 | } | |||
| 1068 | ||||
| 1069 | void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata( | |||
| 1070 | unsigned ID, PlaceholderQueue &Placeholders) { | |||
| 1071 | assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size())((void)0); | |||
| 1072 | assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString")((void)0); | |||
| 1073 | // Lookup first if the metadata hasn't already been loaded. | |||
| 1074 | if (auto *MD = MetadataList.lookup(ID)) { | |||
| 1075 | auto *N = cast<MDNode>(MD); | |||
| 1076 | if (!N->isTemporary()) | |||
| 1077 | return; | |||
| 1078 | } | |||
| 1079 | SmallVector<uint64_t, 64> Record; | |||
| 1080 | StringRef Blob; | |||
| 1081 | if (Error Err = IndexCursor.JumpToBit( | |||
| 1082 | GlobalMetadataBitPosIndex[ID - MDStringRef.size()])) | |||
| 1083 | report_fatal_error("lazyLoadOneMetadata failed jumping: " + | |||
| 1084 | toString(std::move(Err))); | |||
| 1085 | Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks(); | |||
| 1086 | if (!MaybeEntry) | |||
| 1087 | // FIXME this drops the error on the floor. | |||
| 1088 | report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " + | |||
| 1089 | toString(MaybeEntry.takeError())); | |||
| 1090 | BitstreamEntry Entry = MaybeEntry.get(); | |||
| 1091 | ++NumMDRecordLoaded; | |||
| 1092 | if (Expected<unsigned> MaybeCode = | |||
| 1093 | IndexCursor.readRecord(Entry.ID, Record, &Blob)) { | |||
| 1094 | if (Error Err = | |||
| 1095 | parseOneMetadata(Record, MaybeCode.get(), Placeholders, Blob, ID)) | |||
| 1096 | report_fatal_error("Can't lazyload MD, parseOneMetadata: " + | |||
| 1097 | toString(std::move(Err))); | |||
| 1098 | } else | |||
| 1099 | report_fatal_error("Can't lazyload MD: " + toString(MaybeCode.takeError())); | |||
| 1100 | } | |||
| 1101 | ||||
| 1102 | /// Ensure that all forward-references and placeholders are resolved. | |||
| 1103 | /// Iteratively lazy-loading metadata on-demand if needed. | |||
| 1104 | void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders( | |||
| 1105 | PlaceholderQueue &Placeholders) { | |||
| 1106 | DenseSet<unsigned> Temporaries; | |||
| 1107 | while (1) { | |||
| 1108 | // Populate Temporaries with the placeholders that haven't been loaded yet. | |||
| 1109 | Placeholders.getTemporaries(MetadataList, Temporaries); | |||
| 1110 | ||||
| 1111 | // If we don't have any temporary, or FwdReference, we're done! | |||
| 1112 | if (Temporaries.empty() && !MetadataList.hasFwdRefs()) | |||
| 1113 | break; | |||
| 1114 | ||||
| 1115 | // First, load all the temporaries. This can add new placeholders or | |||
| 1116 | // forward references. | |||
| 1117 | for (auto ID : Temporaries) | |||
| 1118 | lazyLoadOneMetadata(ID, Placeholders); | |||
| 1119 | Temporaries.clear(); | |||
| 1120 | ||||
| 1121 | // Second, load the forward-references. This can also add new placeholders | |||
| 1122 | // or forward references. | |||
| 1123 | while (MetadataList.hasFwdRefs()) | |||
| 1124 | lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders); | |||
| 1125 | } | |||
| 1126 | // At this point we don't have any forward reference remaining, or temporary | |||
| 1127 | // that haven't been loaded. We can safely drop RAUW support and mark cycles | |||
| 1128 | // as resolved. | |||
| 1129 | MetadataList.tryToResolveCycles(); | |||
| 1130 | ||||
| 1131 | // Finally, everything is in place, we can replace the placeholders operands | |||
| 1132 | // with the final node they refer to. | |||
| 1133 | Placeholders.flush(MetadataList); | |||
| 1134 | } | |||
| 1135 | ||||
| 1136 | Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( | |||
| 1137 | SmallVectorImpl<uint64_t> &Record, unsigned Code, | |||
| 1138 | PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) { | |||
| 1139 | ||||
| 1140 | bool IsDistinct = false; | |||
| 1141 | auto getMD = [&](unsigned ID) -> Metadata * { | |||
| 1142 | if (ID < MDStringRef.size()) | |||
| 1143 | return lazyLoadOneMDString(ID); | |||
| 1144 | if (!IsDistinct) { | |||
| 1145 | if (auto *MD = MetadataList.lookup(ID)) | |||
| 1146 | return MD; | |||
| 1147 | // If lazy-loading is enabled, we try recursively to load the operand | |||
| 1148 | // instead of creating a temporary. | |||
| 1149 | if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { | |||
| 1150 | // Create a temporary for the node that is referencing the operand we | |||
| 1151 | // will lazy-load. It is needed before recursing in case there are | |||
| 1152 | // uniquing cycles. | |||
| 1153 | MetadataList.getMetadataFwdRef(NextMetadataNo); | |||
| 1154 | lazyLoadOneMetadata(ID, Placeholders); | |||
| 1155 | return MetadataList.lookup(ID); | |||
| 1156 | } | |||
| 1157 | // Return a temporary. | |||
| 1158 | return MetadataList.getMetadataFwdRef(ID); | |||
| 1159 | } | |||
| 1160 | if (auto *MD = MetadataList.getMetadataIfResolved(ID)) | |||
| 1161 | return MD; | |||
| 1162 | return &Placeholders.getPlaceholderOp(ID); | |||
| 1163 | }; | |||
| 1164 | auto getMDOrNull = [&](unsigned ID) -> Metadata * { | |||
| 1165 | if (ID) | |||
| 1166 | return getMD(ID - 1); | |||
| 1167 | return nullptr; | |||
| 1168 | }; | |||
| 1169 | auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * { | |||
| 1170 | if (ID) | |||
| 1171 | return MetadataList.getMetadataFwdRef(ID - 1); | |||
| 1172 | return nullptr; | |||
| 1173 | }; | |||
| 1174 | auto getMDString = [&](unsigned ID) -> MDString * { | |||
| 1175 | // This requires that the ID is not really a forward reference. In | |||
| 1176 | // particular, the MDString must already have been resolved. | |||
| 1177 | auto MDS = getMDOrNull(ID); | |||
| 1178 | return cast_or_null<MDString>(MDS); | |||
| 1179 | }; | |||
| 1180 | ||||
| 1181 | // Support for old type refs. | |||
| 1182 | auto getDITypeRefOrNull = [&](unsigned ID) { | |||
| 1183 | return MetadataList.upgradeTypeRef(getMDOrNull(ID)); | |||
| 1184 | }; | |||
| 1185 | ||||
| 1186 | #define GET_OR_DISTINCT(CLASS, ARGS) \ | |||
| 1187 | (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) | |||
| 1188 | ||||
| 1189 | switch (Code) { | |||
| 1190 | default: // Default behavior: ignore. | |||
| 1191 | break; | |||
| 1192 | case bitc::METADATA_NAME: { | |||
| 1193 | // Read name of the named metadata. | |||
| 1194 | SmallString<8> Name(Record.begin(), Record.end()); | |||
| 1195 | Record.clear(); | |||
| 1196 | Expected<unsigned> MaybeCode = Stream.ReadCode(); | |||
| 1197 | if (!MaybeCode) | |||
| 1198 | return MaybeCode.takeError(); | |||
| 1199 | Code = MaybeCode.get(); | |||
| 1200 | ||||
| 1201 | ++NumMDRecordLoaded; | |||
| 1202 | if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) { | |||
| 1203 | if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE) | |||
| 1204 | return error("METADATA_NAME not followed by METADATA_NAMED_NODE"); | |||
| 1205 | } else | |||
| 1206 | return MaybeNextBitCode.takeError(); | |||
| 1207 | ||||
| 1208 | // Read named metadata elements. | |||
| 1209 | unsigned Size = Record.size(); | |||
| 1210 | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); | |||
| 1211 | for (unsigned i = 0; i != Size; ++i) { | |||
| 1212 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); | |||
| 1213 | if (!MD) | |||
| 1214 | return error("Invalid named metadata: expect fwd ref to MDNode"); | |||
| 1215 | NMD->addOperand(MD); | |||
| 1216 | } | |||
| 1217 | break; | |||
| 1218 | } | |||
| 1219 | case bitc::METADATA_OLD_FN_NODE: { | |||
| 1220 | // Deprecated, but still needed to read old bitcode files. | |||
| 1221 | // This is a LocalAsMetadata record, the only type of function-local | |||
| 1222 | // metadata. | |||
| 1223 | if (Record.size() % 2 == 1) | |||
| 1224 | return error("Invalid record"); | |||
| 1225 | ||||
| 1226 | // If this isn't a LocalAsMetadata record, we're dropping it. This used | |||
| 1227 | // to be legal, but there's no upgrade path. | |||
| 1228 | auto dropRecord = [&] { | |||
| 1229 | MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo); | |||
| 1230 | NextMetadataNo++; | |||
| 1231 | }; | |||
| 1232 | if (Record.size() != 2) { | |||
| 1233 | dropRecord(); | |||
| 1234 | break; | |||
| 1235 | } | |||
| 1236 | ||||
| 1237 | Type *Ty = getTypeByID(Record[0]); | |||
| 1238 | if (Ty->isMetadataTy() || Ty->isVoidTy()) { | |||
| 1239 | dropRecord(); | |||
| 1240 | break; | |||
| 1241 | } | |||
| 1242 | ||||
| 1243 | MetadataList.assignValue( | |||
| 1244 | LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), | |||
| 1245 | NextMetadataNo); | |||
| 1246 | NextMetadataNo++; | |||
| 1247 | break; | |||
| 1248 | } | |||
| 1249 | case bitc::METADATA_OLD_NODE: { | |||
| 1250 | // Deprecated, but still needed to read old bitcode files. | |||
| 1251 | if (Record.size() % 2 == 1) | |||
| 1252 | return error("Invalid record"); | |||
| 1253 | ||||
| 1254 | unsigned Size = Record.size(); | |||
| 1255 | SmallVector<Metadata *, 8> Elts; | |||
| 1256 | for (unsigned i = 0; i != Size; i += 2) { | |||
| 1257 | Type *Ty = getTypeByID(Record[i]); | |||
| 1258 | if (!Ty) | |||
| 1259 | return error("Invalid record"); | |||
| 1260 | if (Ty->isMetadataTy()) | |||
| 1261 | Elts.push_back(getMD(Record[i + 1])); | |||
| 1262 | else if (!Ty->isVoidTy()) { | |||
| 1263 | auto *MD = | |||
| 1264 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); | |||
| 1265 | assert(isa<ConstantAsMetadata>(MD) &&((void)0) | |||
| 1266 | "Expected non-function-local metadata")((void)0); | |||
| 1267 | Elts.push_back(MD); | |||
| 1268 | } else | |||
| 1269 | Elts.push_back(nullptr); | |||
| 1270 | } | |||
| 1271 | MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo); | |||
| 1272 | NextMetadataNo++; | |||
| 1273 | break; | |||
| 1274 | } | |||
| 1275 | case bitc::METADATA_VALUE: { | |||
| 1276 | if (Record.size() != 2) | |||
| 1277 | return error("Invalid record"); | |||
| 1278 | ||||
| 1279 | Type *Ty = getTypeByID(Record[0]); | |||
| 1280 | if (Ty->isMetadataTy() || Ty->isVoidTy()) | |||
| 1281 | return error("Invalid record"); | |||
| 1282 | ||||
| 1283 | MetadataList.assignValue( | |||
| 1284 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), | |||
| 1285 | NextMetadataNo); | |||
| 1286 | NextMetadataNo++; | |||
| 1287 | break; | |||
| 1288 | } | |||
| 1289 | case bitc::METADATA_DISTINCT_NODE: | |||
| 1290 | IsDistinct = true; | |||
| 1291 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
| 1292 | case bitc::METADATA_NODE: { | |||
| 1293 | SmallVector<Metadata *, 8> Elts; | |||
| 1294 | Elts.reserve(Record.size()); | |||
| 1295 | for (unsigned ID : Record) | |||
| 1296 | Elts.push_back(getMDOrNull(ID)); | |||
| 1297 | MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) | |||
| 1298 | : MDNode::get(Context, Elts), | |||
| 1299 | NextMetadataNo); | |||
| 1300 | NextMetadataNo++; | |||
| 1301 | break; | |||
| 1302 | } | |||
| 1303 | case bitc::METADATA_LOCATION: { | |||
| 1304 | if (Record.size() != 5 && Record.size() != 6) | |||
| 1305 | return error("Invalid record"); | |||
| 1306 | ||||
| 1307 | IsDistinct = Record[0]; | |||
| 1308 | unsigned Line = Record[1]; | |||
| 1309 | unsigned Column = Record[2]; | |||
| 1310 | Metadata *Scope = getMD(Record[3]); | |||
| 1311 | Metadata *InlinedAt = getMDOrNull(Record[4]); | |||
| 1312 | bool ImplicitCode = Record.size() == 6 && Record[5]; | |||
| 1313 | MetadataList.assignValue( | |||
| 1314 | GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt, | |||
| 1315 | ImplicitCode)), | |||
| 1316 | NextMetadataNo); | |||
| 1317 | NextMetadataNo++; | |||
| 1318 | break; | |||
| 1319 | } | |||
| 1320 | case bitc::METADATA_GENERIC_DEBUG: { | |||
| 1321 | if (Record.size() < 4) | |||
| 1322 | return error("Invalid record"); | |||
| 1323 | ||||
| 1324 | IsDistinct = Record[0]; | |||
| 1325 | unsigned Tag = Record[1]; | |||
| 1326 | unsigned Version = Record[2]; | |||
| 1327 | ||||
| 1328 | if (Tag >= 1u << 16 || Version != 0) | |||
| 1329 | return error("Invalid record"); | |||
| 1330 | ||||
| 1331 | auto *Header = getMDString(Record[3]); | |||
| 1332 | SmallVector<Metadata *, 8> DwarfOps; | |||
| 1333 | for (unsigned I = 4, E = Record.size(); I != E; ++I) | |||
| 1334 | DwarfOps.push_back(getMDOrNull(Record[I])); | |||
| 1335 | MetadataList.assignValue( | |||
| 1336 | GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)), | |||
| 1337 | NextMetadataNo); | |||
| 1338 | NextMetadataNo++; | |||
| 1339 | break; | |||
| 1340 | } | |||
| 1341 | case bitc::METADATA_SUBRANGE: { | |||
| 1342 | Metadata *Val = nullptr; | |||
| 1343 | // Operand 'count' is interpreted as: | |||
| 1344 | // - Signed integer (version 0) | |||
| 1345 | // - Metadata node (version 1) | |||
| 1346 | // Operand 'lowerBound' is interpreted as: | |||
| 1347 | // - Signed integer (version 0 and 1) | |||
| 1348 | // - Metadata node (version 2) | |||
| 1349 | // Operands 'upperBound' and 'stride' are interpreted as: | |||
| 1350 | // - Metadata node (version 2) | |||
| 1351 | switch (Record[0] >> 1) { | |||
| 1352 | case 0: | |||
| 1353 | Val = GET_OR_DISTINCT(DISubrange, | |||
| 1354 | (Context, Record[1], unrotateSign(Record[2]))); | |||
| 1355 | break; | |||
| 1356 | case 1: | |||
| 1357 | Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]), | |||
| 1358 | unrotateSign(Record[2]))); | |||
| 1359 | break; | |||
| 1360 | case 2: | |||
| 1361 | Val = GET_OR_DISTINCT( | |||
| 1362 | DISubrange, (Context, getMDOrNull(Record[1]), getMDOrNull(Record[2]), | |||
| 1363 | getMDOrNull(Record[3]), getMDOrNull(Record[4]))); | |||
| 1364 | break; | |||
| 1365 | default: | |||
| 1366 | return error("Invalid record: Unsupported version of DISubrange"); | |||
| 1367 | } | |||
| 1368 | ||||
| 1369 | MetadataList.assignValue(Val, NextMetadataNo); | |||
| 1370 | IsDistinct = Record[0] & 1; | |||
| 1371 | NextMetadataNo++; | |||
| 1372 | break; | |||
| 1373 | } | |||
| 1374 | case bitc::METADATA_GENERIC_SUBRANGE: { | |||
| 1375 | Metadata *Val = nullptr; | |||
| 1376 | Val = GET_OR_DISTINCT(DIGenericSubrange, | |||
| 1377 | (Context, getMDOrNull(Record[1]), | |||
| 1378 | getMDOrNull(Record[2]), getMDOrNull(Record[3]), | |||
| 1379 | getMDOrNull(Record[4]))); | |||
| 1380 | ||||
| 1381 | MetadataList.assignValue(Val, NextMetadataNo); | |||
| 1382 | IsDistinct = Record[0] & 1; | |||
| 1383 | NextMetadataNo++; | |||
| 1384 | break; | |||
| 1385 | } | |||
| 1386 | case bitc::METADATA_ENUMERATOR: { | |||
| 1387 | if (Record.size() < 3) | |||
| 1388 | return error("Invalid record"); | |||
| 1389 | ||||
| 1390 | IsDistinct = Record[0] & 1; | |||
| 1391 | bool IsUnsigned = Record[0] & 2; | |||
| 1392 | bool IsBigInt = Record[0] & 4; | |||
| 1393 | APInt Value; | |||
| 1394 | ||||
| 1395 | if (IsBigInt) { | |||
| 1396 | const uint64_t BitWidth = Record[1]; | |||
| 1397 | const size_t NumWords = Record.size() - 3; | |||
| 1398 | Value = readWideAPInt(makeArrayRef(&Record[3], NumWords), BitWidth); | |||
| 1399 | } else | |||
| 1400 | Value = APInt(64, unrotateSign(Record[1]), !IsUnsigned); | |||
| 1401 | ||||
| 1402 | MetadataList.assignValue( | |||
| 1403 | GET_OR_DISTINCT(DIEnumerator, | |||
| 1404 | (Context, Value, IsUnsigned, getMDString(Record[2]))), | |||
| 1405 | NextMetadataNo); | |||
| 1406 | NextMetadataNo++; | |||
| 1407 | break; | |||
| 1408 | } | |||
| 1409 | case bitc::METADATA_BASIC_TYPE: { | |||
| 1410 | if (Record.size() < 6 || Record.size() > 7) | |||
| 1411 | return error("Invalid record"); | |||
| 1412 | ||||
| 1413 | IsDistinct = Record[0]; | |||
| 1414 | DINode::DIFlags Flags = (Record.size() > 6) ? | |||
| 1415 | static_cast<DINode::DIFlags>(Record[6]) : DINode::FlagZero; | |||
| 1416 | ||||
| 1417 | MetadataList.assignValue( | |||
| 1418 | GET_OR_DISTINCT(DIBasicType, | |||
| 1419 | (Context, Record[1], getMDString(Record[2]), Record[3], | |||
| 1420 | Record[4], Record[5], Flags)), | |||
| 1421 | NextMetadataNo); | |||
| 1422 | NextMetadataNo++; | |||
| 1423 | break; | |||
| 1424 | } | |||
| 1425 | case bitc::METADATA_STRING_TYPE: { | |||
| 1426 | if (Record.size() != 8) | |||
| 1427 | return error("Invalid record"); | |||
| 1428 | ||||
| 1429 | IsDistinct = Record[0]; | |||
| 1430 | MetadataList.assignValue( | |||
| 1431 | GET_OR_DISTINCT(DIStringType, | |||
| 1432 | (Context, Record[1], getMDString(Record[2]), | |||
| 1433 | getMDOrNull(Record[3]), getMDOrNull(Record[4]), | |||
| 1434 | Record[5], Record[6], Record[7])), | |||
| 1435 | NextMetadataNo); | |||
| 1436 | NextMetadataNo++; | |||
| 1437 | break; | |||
| 1438 | } | |||
| 1439 | case bitc::METADATA_DERIVED_TYPE: { | |||
| 1440 | if (Record.size() < 12 || Record.size() > 13) | |||
| 1441 | return error("Invalid record"); | |||
| 1442 | ||||
| 1443 | // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means | |||
| 1444 | // that there is no DWARF address space associated with DIDerivedType. | |||
| 1445 | Optional<unsigned> DWARFAddressSpace; | |||
| 1446 | if (Record.size() > 12 && Record[12]) | |||
| 1447 | DWARFAddressSpace = Record[12] - 1; | |||
| 1448 | ||||
| 1449 | IsDistinct = Record[0]; | |||
| 1450 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); | |||
| 1451 | MetadataList.assignValue( | |||
| 1452 | GET_OR_DISTINCT(DIDerivedType, | |||
| 1453 | (Context, Record[1], getMDString(Record[2]), | |||
| 1454 | getMDOrNull(Record[3]), Record[4], | |||
| 1455 | getDITypeRefOrNull(Record[5]), | |||
| 1456 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], | |||
| 1457 | Record[9], DWARFAddressSpace, Flags, | |||
| 1458 | getDITypeRefOrNull(Record[11]))), | |||
| 1459 | NextMetadataNo); | |||
| 1460 | NextMetadataNo++; | |||
| 1461 | break; | |||
| 1462 | } | |||
| 1463 | case bitc::METADATA_COMPOSITE_TYPE: { | |||
| 1464 | if (Record.size() < 16 || Record.size() > 21) | |||
| 1465 | return error("Invalid record"); | |||
| 1466 | ||||
| 1467 | // If we have a UUID and this is not a forward declaration, lookup the | |||
| 1468 | // mapping. | |||
| 1469 | IsDistinct = Record[0] & 0x1; | |||
| 1470 | bool IsNotUsedInTypeRef = Record[0] >= 2; | |||
| 1471 | unsigned Tag = Record[1]; | |||
| 1472 | MDString *Name = getMDString(Record[2]); | |||
| 1473 | Metadata *File = getMDOrNull(Record[3]); | |||
| 1474 | unsigned Line = Record[4]; | |||
| 1475 | Metadata *Scope = getDITypeRefOrNull(Record[5]); | |||
| 1476 | Metadata *BaseType = nullptr; | |||
| 1477 | uint64_t SizeInBits = Record[7]; | |||
| 1478 | if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max()) | |||
| 1479 | return error("Alignment value is too large"); | |||
| 1480 | uint32_t AlignInBits = Record[8]; | |||
| 1481 | uint64_t OffsetInBits = 0; | |||
| 1482 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); | |||
| 1483 | Metadata *Elements = nullptr; | |||
| 1484 | unsigned RuntimeLang = Record[12]; | |||
| 1485 | Metadata *VTableHolder = nullptr; | |||
| 1486 | Metadata *TemplateParams = nullptr; | |||
| 1487 | Metadata *Discriminator = nullptr; | |||
| 1488 | Metadata *DataLocation = nullptr; | |||
| 1489 | Metadata *Associated = nullptr; | |||
| 1490 | Metadata *Allocated = nullptr; | |||
| 1491 | Metadata *Rank = nullptr; | |||
| 1492 | auto *Identifier = getMDString(Record[15]); | |||
| 1493 | // If this module is being parsed so that it can be ThinLTO imported | |||
| 1494 | // into another module, composite types only need to be imported | |||
| 1495 | // as type declarations (unless full type definitions requested). | |||
| 1496 | // Create type declarations up front to save memory. Also, buildODRType | |||
| 1497 | // handles the case where this is type ODRed with a definition needed | |||
| 1498 | // by the importing module, in which case the existing definition is | |||
| 1499 | // used. | |||
| 1500 | if (IsImporting && !ImportFullTypeDefinitions && Identifier && | |||
| 1501 | (Tag == dwarf::DW_TAG_enumeration_type || | |||
| 1502 | Tag == dwarf::DW_TAG_class_type || | |||
| 1503 | Tag == dwarf::DW_TAG_structure_type || | |||
| 1504 | Tag == dwarf::DW_TAG_union_type)) { | |||
| 1505 | Flags = Flags | DINode::FlagFwdDecl; | |||
| 1506 | } else { | |||
| 1507 | BaseType = getDITypeRefOrNull(Record[6]); | |||
| 1508 | OffsetInBits = Record[9]; | |||
| 1509 | Elements = getMDOrNull(Record[11]); | |||
| 1510 | VTableHolder = getDITypeRefOrNull(Record[13]); | |||
| 1511 | TemplateParams = getMDOrNull(Record[14]); | |||
| 1512 | if (Record.size() > 16) | |||
| 1513 | Discriminator = getMDOrNull(Record[16]); | |||
| 1514 | if (Record.size() > 17) | |||
| 1515 | DataLocation = getMDOrNull(Record[17]); | |||
| 1516 | if (Record.size() > 19) { | |||
| 1517 | Associated = getMDOrNull(Record[18]); | |||
| 1518 | Allocated = getMDOrNull(Record[19]); | |||
| 1519 | } | |||
| 1520 | if (Record.size() > 20) { | |||
| 1521 | Rank = getMDOrNull(Record[20]); | |||
| 1522 | } | |||
| 1523 | } | |||
| 1524 | DICompositeType *CT = nullptr; | |||
| 1525 | if (Identifier) | |||
| 1526 | CT = DICompositeType::buildODRType( | |||
| 1527 | Context, *Identifier, Tag, Name, File, Line, Scope, BaseType, | |||
| 1528 | SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, | |||
| 1529 | VTableHolder, TemplateParams, Discriminator, DataLocation, Associated, | |||
| 1530 | Allocated, Rank); | |||
| 1531 | ||||
| 1532 | // Create a node if we didn't get a lazy ODR type. | |||
| 1533 | if (!CT) | |||
| 1534 | CT = GET_OR_DISTINCT(DICompositeType, | |||
| 1535 | (Context, Tag, Name, File, Line, Scope, BaseType, | |||
| 1536 | SizeInBits, AlignInBits, OffsetInBits, Flags, | |||
| 1537 | Elements, RuntimeLang, VTableHolder, TemplateParams, | |||
| 1538 | Identifier, Discriminator, DataLocation, Associated, | |||
| 1539 | Allocated, Rank)); | |||
| 1540 | if (!IsNotUsedInTypeRef && Identifier) | |||
| 1541 | MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT)); | |||
| 1542 | ||||
| 1543 | MetadataList.assignValue(CT, NextMetadataNo); | |||
| 1544 | NextMetadataNo++; | |||
| 1545 | break; | |||
| 1546 | } | |||
| 1547 | case bitc::METADATA_SUBROUTINE_TYPE: { | |||
| 1548 | if (Record.size() < 3 || Record.size() > 4) | |||
| 1549 | return error("Invalid record"); | |||
| 1550 | bool IsOldTypeRefArray = Record[0] < 2; | |||
| 1551 | unsigned CC = (Record.size() > 3) ? Record[3] : 0; | |||
| 1552 | ||||
| 1553 | IsDistinct = Record[0] & 0x1; | |||
| 1554 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]); | |||
| 1555 | Metadata *Types = getMDOrNull(Record[2]); | |||
| 1556 | if (LLVM_UNLIKELY(IsOldTypeRefArray)__builtin_expect((bool)(IsOldTypeRefArray), false)) | |||
| 1557 | Types = MetadataList.upgradeTypeRefArray(Types); | |||
| 1558 | ||||
| 1559 | MetadataList.assignValue( | |||
| 1560 | GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)), | |||
| 1561 | NextMetadataNo); | |||
| 1562 | NextMetadataNo++; | |||
| 1563 | break; | |||
| 1564 | } | |||
| 1565 | ||||
| 1566 | case bitc::METADATA_MODULE: { | |||
| 1567 | if (Record.size() < 5 || Record.size() > 9) | |||
| 1568 | return error("Invalid record"); | |||
| 1569 | ||||
| 1570 | unsigned Offset = Record.size() >= 8 ? 2 : 1; | |||
| 1571 | IsDistinct = Record[0]; | |||
| 1572 | MetadataList.assignValue( | |||
| 1573 | GET_OR_DISTINCT( | |||
| 1574 | DIModule, | |||
| 1575 | (Context, Record.size() >= 8 ? getMDOrNull(Record[1]) : nullptr, | |||
| 1576 | getMDOrNull(Record[0 + Offset]), getMDString(Record[1 + Offset]), | |||
| 1577 | getMDString(Record[2 + Offset]), getMDString(Record[3 + Offset]), | |||
| 1578 | getMDString(Record[4 + Offset]), | |||
| 1579 | Record.size() <= 7 ? 0 : Record[7], | |||
| 1580 | Record.size() <= 8 ? false : Record[8])), | |||
| 1581 | NextMetadataNo); | |||
| 1582 | NextMetadataNo++; | |||
| 1583 | break; | |||
| 1584 | } | |||
| 1585 | ||||
| 1586 | case bitc::METADATA_FILE: { | |||
| 1587 | if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6) | |||
| 1588 | return error("Invalid record"); | |||
| 1589 | ||||
| 1590 | IsDistinct = Record[0]; | |||
| 1591 | Optional<DIFile::ChecksumInfo<MDString *>> Checksum; | |||
| 1592 | // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum | |||
| 1593 | // is not present. This matches up with the old internal representation, | |||
| 1594 | // and the old encoding for CSK_None in the ChecksumKind. The new | |||
| 1595 | // representation reserves the value 0 in the ChecksumKind to continue to | |||
| 1596 | // encode None in a backwards-compatible way. | |||
| 1597 | if (Record.size() > 4 && Record[3] && Record[4]) | |||
| 1598 | Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]), | |||
| 1599 | getMDString(Record[4])); | |||
| 1600 | MetadataList.assignValue( | |||
| 1601 | GET_OR_DISTINCT( | |||
| 1602 | DIFile, | |||
| 1603 | (Context, getMDString(Record[1]), getMDString(Record[2]), Checksum, | |||
| 1604 | Record.size() > 5 ? Optional<MDString *>(getMDString(Record[5])) | |||
| 1605 | : None)), | |||
| 1606 | NextMetadataNo); | |||
| 1607 | NextMetadataNo++; | |||
| 1608 | break; | |||
| 1609 | } | |||
| 1610 | case bitc::METADATA_COMPILE_UNIT: { | |||
| 1611 | if (Record.size() < 14 || Record.size() > 22) | |||
| 1612 | return error("Invalid record"); | |||
| 1613 | ||||
| 1614 | // Ignore Record[0], which indicates whether this compile unit is | |||
| 1615 | // distinct. It's always distinct. | |||
| 1616 | IsDistinct = true; | |||
| 1617 | auto *CU = DICompileUnit::getDistinct( | |||
| 1618 | Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]), | |||
| 1619 | Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]), | |||
| 1620 | Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]), | |||
| 1621 | getMDOrNull(Record[12]), getMDOrNull(Record[13]), | |||
| 1622 | Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]), | |||
| 1623 | Record.size() <= 14 ? 0 : Record[14], | |||
| 1624 | Record.size() <= 16 ? true : Record[16], | |||
| 1625 | Record.size() <= 17 ? false : Record[17], | |||
| 1626 | Record.size() <= 18 ? 0 : Record[18], | |||
| 1627 | Record.size() <= 19 ? 0 : Record[19], | |||
| 1628 | Record.size() <= 20 ? nullptr : getMDString(Record[20]), | |||
| 1629 | Record.size() <= 21 ? nullptr : getMDString(Record[21])); | |||
| 1630 | ||||
| 1631 | MetadataList.assignValue(CU, NextMetadataNo); | |||
| 1632 | NextMetadataNo++; | |||
| 1633 | ||||
| 1634 | // Move the Upgrade the list of subprograms. | |||
| 1635 | if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11])) | |||
| 1636 | CUSubprograms.push_back({CU, SPs}); | |||
| 1637 | break; | |||
| 1638 | } | |||
| 1639 | case bitc::METADATA_SUBPROGRAM: { | |||
| 1640 | if (Record.size() < 18 || Record.size() > 21) | |||
| 1641 | return error("Invalid record"); | |||
| 1642 | ||||
| 1643 | bool HasSPFlags = Record[0] & 4; | |||
| 1644 | ||||
| 1645 | DINode::DIFlags Flags; | |||
| 1646 | DISubprogram::DISPFlags SPFlags; | |||
| 1647 | if (!HasSPFlags) | |||
| 1648 | Flags = static_cast<DINode::DIFlags>(Record[11 + 2]); | |||
| 1649 | else { | |||
| 1650 | Flags = static_cast<DINode::DIFlags>(Record[11]); | |||
| 1651 | SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]); | |||
| 1652 | } | |||
| 1653 | ||||
| 1654 | // Support for old metadata when | |||
| 1655 | // subprogram specific flags are placed in DIFlags. | |||
| 1656 | const unsigned DIFlagMainSubprogram = 1 << 21; | |||
| 1657 | bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram; | |||
| 1658 | if (HasOldMainSubprogramFlag) | |||
| 1659 | // Remove old DIFlagMainSubprogram from DIFlags. | |||
| 1660 | // Note: This assumes that any future use of bit 21 defaults to it | |||
| 1661 | // being 0. | |||
| 1662 | Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram); | |||
| 1663 | ||||
| 1664 | if (HasOldMainSubprogramFlag && HasSPFlags) | |||
| 1665 | SPFlags |= DISubprogram::SPFlagMainSubprogram; | |||
| 1666 | else if (!HasSPFlags) | |||
| 1667 | SPFlags = DISubprogram::toSPFlags( | |||
| 1668 | /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8], | |||
| 1669 | /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11], | |||
| 1670 | /*DIFlagMainSubprogram*/HasOldMainSubprogramFlag); | |||
| 1671 | ||||
| 1672 | // All definitions should be distinct. | |||
| 1673 | IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition); | |||
| 1674 | // Version 1 has a Function as Record[15]. | |||
| 1675 | // Version 2 has removed Record[15]. | |||
| 1676 | // Version 3 has the Unit as Record[15]. | |||
| 1677 | // Version 4 added thisAdjustment. | |||
| 1678 | // Version 5 repacked flags into DISPFlags, changing many element numbers. | |||
| 1679 | bool HasUnit = Record[0] & 2; | |||
| 1680 | if (!HasSPFlags && HasUnit && Record.size() < 19) | |||
| 1681 | return error("Invalid record"); | |||
| 1682 | if (HasSPFlags && !HasUnit) | |||
| 1683 | return error("Invalid record"); | |||
| 1684 | // Accommodate older formats. | |||
| 1685 | bool HasFn = false; | |||
| 1686 | bool HasThisAdj = true; | |||
| 1687 | bool HasThrownTypes = true; | |||
| 1688 | unsigned OffsetA = 0; | |||
| 1689 | unsigned OffsetB = 0; | |||
| 1690 | if (!HasSPFlags) { | |||
| 1691 | OffsetA = 2; | |||
| 1692 | OffsetB = 2; | |||
| 1693 | if (Record.size() >= 19) { | |||
| 1694 | HasFn = !HasUnit; | |||
| 1695 | OffsetB++; | |||
| 1696 | } | |||
| 1697 | HasThisAdj = Record.size() >= 20; | |||
| 1698 | HasThrownTypes = Record.size() >= 21; | |||
| 1699 | } | |||
| 1700 | Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]); | |||
| 1701 | DISubprogram *SP = GET_OR_DISTINCT( | |||
| 1702 | DISubprogram, | |||
| 1703 | (Context, | |||
| 1704 | getDITypeRefOrNull(Record[1]), // scope | |||
| 1705 | getMDString(Record[2]), // name | |||
| 1706 | getMDString(Record[3]), // linkageName | |||
| 1707 | getMDOrNull(Record[4]), // file | |||
| 1708 | Record[5], // line | |||
| 1709 | getMDOrNull(Record[6]), // type | |||
| 1710 | Record[7 + OffsetA], // scopeLine | |||
| 1711 | getDITypeRefOrNull(Record[8 + OffsetA]), // containingType | |||
| 1712 | Record[10 + OffsetA], // virtualIndex | |||
| 1713 | HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment | |||
| 1714 | Flags, // flags | |||
| 1715 | SPFlags, // SPFlags | |||
| 1716 | HasUnit ? CUorFn : nullptr, // unit | |||
| 1717 | getMDOrNull(Record[13 + OffsetB]), // templateParams | |||
| 1718 | getMDOrNull(Record[14 + OffsetB]), // declaration | |||
| 1719 | getMDOrNull(Record[15 + OffsetB]), // retainedNodes | |||
| 1720 | HasThrownTypes ? getMDOrNull(Record[17 + OffsetB]) | |||
| 1721 | : nullptr // thrownTypes | |||
| 1722 | )); | |||
| 1723 | MetadataList.assignValue(SP, NextMetadataNo); | |||
| 1724 | NextMetadataNo++; | |||
| 1725 | ||||
| 1726 | // Upgrade sp->function mapping to function->sp mapping. | |||
| 1727 | if (HasFn) { | |||
| 1728 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn)) | |||
| 1729 | if (auto *F = dyn_cast<Function>(CMD->getValue())) { | |||
| 1730 | if (F->isMaterializable()) | |||
| 1731 | // Defer until materialized; unmaterialized functions may not have | |||
| 1732 | // metadata. | |||
| 1733 | FunctionsWithSPs[F] = SP; | |||
| 1734 | else if (!F->empty()) | |||
| 1735 | F->setSubprogram(SP); | |||
| 1736 | } | |||
| 1737 | } | |||
| 1738 | break; | |||
| 1739 | } | |||
| 1740 | case bitc::METADATA_LEXICAL_BLOCK: { | |||
| 1741 | if (Record.size() != 5) | |||
| 1742 | return error("Invalid record"); | |||
| 1743 | ||||
| 1744 | IsDistinct = Record[0]; | |||
| 1745 | MetadataList.assignValue( | |||
| 1746 | GET_OR_DISTINCT(DILexicalBlock, | |||
| 1747 | (Context, getMDOrNull(Record[1]), | |||
| 1748 | getMDOrNull(Record[2]), Record[3], Record[4])), | |||
| 1749 | NextMetadataNo); | |||
| 1750 | NextMetadataNo++; | |||
| 1751 | break; | |||
| 1752 | } | |||
| 1753 | case bitc::METADATA_LEXICAL_BLOCK_FILE: { | |||
| 1754 | if (Record.size() != 4) | |||
| 1755 | return error("Invalid record"); | |||
| 1756 | ||||
| 1757 | IsDistinct = Record[0]; | |||
| 1758 | MetadataList.assignValue( | |||
| 1759 | GET_OR_DISTINCT(DILexicalBlockFile, | |||
| 1760 | (Context, getMDOrNull(Record[1]), | |||
| 1761 | getMDOrNull(Record[2]), Record[3])), | |||
| 1762 | NextMetadataNo); | |||
| 1763 | NextMetadataNo++; | |||
| 1764 | break; | |||
| 1765 | } | |||
| 1766 | case bitc::METADATA_COMMON_BLOCK: { | |||
| 1767 | IsDistinct = Record[0] & 1; | |||
| 1768 | MetadataList.assignValue( | |||
| 1769 | GET_OR_DISTINCT(DICommonBlock, | |||
| 1770 | (Context, getMDOrNull(Record[1]), | |||
| 1771 | getMDOrNull(Record[2]), getMDString(Record[3]), | |||
| 1772 | getMDOrNull(Record[4]), Record[5])), | |||
| 1773 | NextMetadataNo); | |||
| 1774 | NextMetadataNo++; | |||
| 1775 | break; | |||
| 1776 | } | |||
| 1777 | case bitc::METADATA_NAMESPACE: { | |||
| 1778 | // Newer versions of DINamespace dropped file and line. | |||
| 1779 | MDString *Name; | |||
| 1780 | if (Record.size() == 3) | |||
| 1781 | Name = getMDString(Record[2]); | |||
| 1782 | else if (Record.size() == 5) | |||
| 1783 | Name = getMDString(Record[3]); | |||
| 1784 | else | |||
| 1785 | return error("Invalid record"); | |||
| 1786 | ||||
| 1787 | IsDistinct = Record[0] & 1; | |||
| 1788 | bool ExportSymbols = Record[0] & 2; | |||
| 1789 | MetadataList.assignValue( | |||
| 1790 | GET_OR_DISTINCT(DINamespace, | |||
| 1791 | (Context, getMDOrNull(Record[1]), Name, ExportSymbols)), | |||
| 1792 | NextMetadataNo); | |||
| 1793 | NextMetadataNo++; | |||
| 1794 | break; | |||
| 1795 | } | |||
| 1796 | case bitc::METADATA_MACRO: { | |||
| 1797 | if (Record.size() != 5) | |||
| 1798 | return error("Invalid record"); | |||
| 1799 | ||||
| 1800 | IsDistinct = Record[0]; | |||
| 1801 | MetadataList.assignValue( | |||
| 1802 | GET_OR_DISTINCT(DIMacro, | |||
| 1803 | (Context, Record[1], Record[2], getMDString(Record[3]), | |||
| 1804 | getMDString(Record[4]))), | |||
| 1805 | NextMetadataNo); | |||
| 1806 | NextMetadataNo++; | |||
| 1807 | break; | |||
| 1808 | } | |||
| 1809 | case bitc::METADATA_MACRO_FILE: { | |||
| 1810 | if (Record.size() != 5) | |||
| 1811 | return error("Invalid record"); | |||
| 1812 | ||||
| 1813 | IsDistinct = Record[0]; | |||
| 1814 | MetadataList.assignValue( | |||
| 1815 | GET_OR_DISTINCT(DIMacroFile, | |||
| 1816 | (Context, Record[1], Record[2], getMDOrNull(Record[3]), | |||
| 1817 | getMDOrNull(Record[4]))), | |||
| 1818 | NextMetadataNo); | |||
| 1819 | NextMetadataNo++; | |||
| 1820 | break; | |||
| 1821 | } | |||
| 1822 | case bitc::METADATA_TEMPLATE_TYPE: { | |||
| 1823 | if (Record.size() < 3 || Record.size() > 4) | |||
| 1824 | return error("Invalid record"); | |||
| 1825 | ||||
| 1826 | IsDistinct = Record[0]; | |||
| 1827 | MetadataList.assignValue( | |||
| 1828 | GET_OR_DISTINCT(DITemplateTypeParameter, | |||
| 1829 | (Context, getMDString(Record[1]), | |||
| 1830 | getDITypeRefOrNull(Record[2]), | |||
| 1831 | (Record.size() == 4) ? getMDOrNull(Record[3]) | |||
| 1832 | : getMDOrNull(false))), | |||
| 1833 | NextMetadataNo); | |||
| 1834 | NextMetadataNo++; | |||
| 1835 | break; | |||
| 1836 | } | |||
| 1837 | case bitc::METADATA_TEMPLATE_VALUE: { | |||
| 1838 | if (Record.size() < 5 || Record.size() > 6) | |||
| 1839 | return error("Invalid record"); | |||
| 1840 | ||||
| 1841 | IsDistinct = Record[0]; | |||
| 1842 | ||||
| 1843 | MetadataList.assignValue( | |||
| 1844 | GET_OR_DISTINCT( | |||
| 1845 | DITemplateValueParameter, | |||
| 1846 | (Context, Record[1], getMDString(Record[2]), | |||
| 1847 | getDITypeRefOrNull(Record[3]), | |||
| 1848 | (Record.size() == 6) ? getMDOrNull(Record[4]) : getMDOrNull(false), | |||
| 1849 | (Record.size() == 6) ? getMDOrNull(Record[5]) | |||
| 1850 | : getMDOrNull(Record[4]))), | |||
| 1851 | NextMetadataNo); | |||
| 1852 | NextMetadataNo++; | |||
| 1853 | break; | |||
| 1854 | } | |||
| 1855 | case bitc::METADATA_GLOBAL_VAR: { | |||
| 1856 | if (Record.size() < 11 || Record.size() > 13) | |||
| 1857 | return error("Invalid record"); | |||
| 1858 | ||||
| 1859 | IsDistinct = Record[0] & 1; | |||
| 1860 | unsigned Version = Record[0] >> 1; | |||
| 1861 | ||||
| 1862 | if (Version == 2) { | |||
| 1863 | MetadataList.assignValue( | |||
| 1864 | GET_OR_DISTINCT( | |||
| 1865 | DIGlobalVariable, | |||
| 1866 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), | |||
| 1867 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], | |||
| 1868 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], | |||
| 1869 | getMDOrNull(Record[9]), getMDOrNull(Record[10]), Record[11])), | |||
| 1870 | NextMetadataNo); | |||
| 1871 | ||||
| 1872 | NextMetadataNo++; | |||
| 1873 | } else if (Version == 1) { | |||
| 1874 | // No upgrade necessary. A null field will be introduced to indicate | |||
| 1875 | // that no parameter information is available. | |||
| 1876 | MetadataList.assignValue( | |||
| 1877 | GET_OR_DISTINCT(DIGlobalVariable, | |||
| 1878 | (Context, getMDOrNull(Record[1]), | |||
| 1879 | getMDString(Record[2]), getMDString(Record[3]), | |||
| 1880 | getMDOrNull(Record[4]), Record[5], | |||
| 1881 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], | |||
| 1882 | getMDOrNull(Record[10]), nullptr, Record[11])), | |||
| 1883 | NextMetadataNo); | |||
| 1884 | ||||
| 1885 | NextMetadataNo++; | |||
| 1886 | } else if (Version == 0) { | |||
| 1887 | // Upgrade old metadata, which stored a global variable reference or a | |||
| 1888 | // ConstantInt here. | |||
| 1889 | NeedUpgradeToDIGlobalVariableExpression = true; | |||
| 1890 | Metadata *Expr = getMDOrNull(Record[9]); | |||
| 1891 | uint32_t AlignInBits = 0; | |||
| 1892 | if (Record.size() > 11) { | |||
| 1893 | if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max()) | |||
| 1894 | return error("Alignment value is too large"); | |||
| 1895 | AlignInBits = Record[11]; | |||
| 1896 | } | |||
| 1897 | GlobalVariable *Attach = nullptr; | |||
| 1898 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) { | |||
| 1899 | if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) { | |||
| 1900 | Attach = GV; | |||
| 1901 | Expr = nullptr; | |||
| 1902 | } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) { | |||
| 1903 | Expr = DIExpression::get(Context, | |||
| 1904 | {dwarf::DW_OP_constu, CI->getZExtValue(), | |||
| 1905 | dwarf::DW_OP_stack_value}); | |||
| 1906 | } else { | |||
| 1907 | Expr = nullptr; | |||
| 1908 | } | |||
| 1909 | } | |||
| 1910 | DIGlobalVariable *DGV = GET_OR_DISTINCT( | |||
| 1911 | DIGlobalVariable, | |||
| 1912 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), | |||
| 1913 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], | |||
| 1914 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], | |||
| 1915 | getMDOrNull(Record[10]), nullptr, AlignInBits)); | |||
| 1916 | ||||
| 1917 | DIGlobalVariableExpression *DGVE = nullptr; | |||
| 1918 | if (Attach || Expr) | |||
| 1919 | DGVE = DIGlobalVariableExpression::getDistinct( | |||
| 1920 | Context, DGV, Expr ? Expr : DIExpression::get(Context, {})); | |||
| 1921 | if (Attach) | |||
| 1922 | Attach->addDebugInfo(DGVE); | |||
| 1923 | ||||
| 1924 | auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV); | |||
| 1925 | MetadataList.assignValue(MDNode, NextMetadataNo); | |||
| 1926 | NextMetadataNo++; | |||
| 1927 | } else | |||
| 1928 | return error("Invalid record"); | |||
| 1929 | ||||
| 1930 | break; | |||
| 1931 | } | |||
| 1932 | case bitc::METADATA_LOCAL_VAR: { | |||
| 1933 | // 10th field is for the obseleted 'inlinedAt:' field. | |||
| 1934 | if (Record.size() < 8 || Record.size() > 10) | |||
| 1935 | return error("Invalid record"); | |||
| 1936 | ||||
| 1937 | IsDistinct = Record[0] & 1; | |||
| 1938 | bool HasAlignment = Record[0] & 2; | |||
| 1939 | // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or | |||
| 1940 | // DW_TAG_arg_variable, if we have alignment flag encoded it means, that | |||
| 1941 | // this is newer version of record which doesn't have artificial tag. | |||
| 1942 | bool HasTag = !HasAlignment && Record.size() > 8; | |||
| 1943 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]); | |||
| 1944 | uint32_t AlignInBits = 0; | |||
| 1945 | if (HasAlignment) { | |||
| 1946 | if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max()) | |||
| 1947 | return error("Alignment value is too large"); | |||
| 1948 | AlignInBits = Record[8 + HasTag]; | |||
| 1949 | } | |||
| 1950 | MetadataList.assignValue( | |||
| 1951 | GET_OR_DISTINCT(DILocalVariable, | |||
| 1952 | (Context, getMDOrNull(Record[1 + HasTag]), | |||
| 1953 | getMDString(Record[2 + HasTag]), | |||
| 1954 | getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag], | |||
| 1955 | getDITypeRefOrNull(Record[5 + HasTag]), | |||
| 1956 | Record[6 + HasTag], Flags, AlignInBits)), | |||
| 1957 | NextMetadataNo); | |||
| 1958 | NextMetadataNo++; | |||
| 1959 | break; | |||
| 1960 | } | |||
| 1961 | case bitc::METADATA_LABEL: { | |||
| 1962 | if (Record.size() != 5) | |||
| 1963 | return error("Invalid record"); | |||
| 1964 | ||||
| 1965 | IsDistinct = Record[0] & 1; | |||
| 1966 | MetadataList.assignValue( | |||
| 1967 | GET_OR_DISTINCT(DILabel, | |||
| 1968 | (Context, getMDOrNull(Record[1]), | |||
| 1969 | getMDString(Record[2]), | |||
| 1970 | getMDOrNull(Record[3]), Record[4])), | |||
| 1971 | NextMetadataNo); | |||
| 1972 | NextMetadataNo++; | |||
| 1973 | break; | |||
| 1974 | } | |||
| 1975 | case bitc::METADATA_EXPRESSION: { | |||
| 1976 | if (Record.size() < 1) | |||
| 1977 | return error("Invalid record"); | |||
| 1978 | ||||
| 1979 | IsDistinct = Record[0] & 1; | |||
| 1980 | uint64_t Version = Record[0] >> 1; | |||
| 1981 | auto Elts = MutableArrayRef<uint64_t>(Record).slice(1); | |||
| 1982 | ||||
| 1983 | SmallVector<uint64_t, 6> Buffer; | |||
| 1984 | if (Error Err = upgradeDIExpression(Version, Elts, Buffer)) | |||
| 1985 | return Err; | |||
| 1986 | ||||
| 1987 | MetadataList.assignValue( | |||
| 1988 | GET_OR_DISTINCT(DIExpression, (Context, Elts)), NextMetadataNo); | |||
| 1989 | NextMetadataNo++; | |||
| 1990 | break; | |||
| 1991 | } | |||
| 1992 | case bitc::METADATA_GLOBAL_VAR_EXPR: { | |||
| 1993 | if (Record.size() != 3) | |||
| 1994 | return error("Invalid record"); | |||
| 1995 | ||||
| 1996 | IsDistinct = Record[0]; | |||
| 1997 | Metadata *Expr = getMDOrNull(Record[2]); | |||
| 1998 | if (!Expr) | |||
| 1999 | Expr = DIExpression::get(Context, {}); | |||
| 2000 | MetadataList.assignValue( | |||
| 2001 | GET_OR_DISTINCT(DIGlobalVariableExpression, | |||
| 2002 | (Context, getMDOrNull(Record[1]), Expr)), | |||
| 2003 | NextMetadataNo); | |||
| 2004 | NextMetadataNo++; | |||
| 2005 | break; | |||
| 2006 | } | |||
| 2007 | case bitc::METADATA_OBJC_PROPERTY: { | |||
| 2008 | if (Record.size() != 8) | |||
| 2009 | return error("Invalid record"); | |||
| 2010 | ||||
| 2011 | IsDistinct = Record[0]; | |||
| 2012 | MetadataList.assignValue( | |||
| 2013 | GET_OR_DISTINCT(DIObjCProperty, | |||
| 2014 | (Context, getMDString(Record[1]), | |||
| 2015 | getMDOrNull(Record[2]), Record[3], | |||
| 2016 | getMDString(Record[4]), getMDString(Record[5]), | |||
| 2017 | Record[6], getDITypeRefOrNull(Record[7]))), | |||
| 2018 | NextMetadataNo); | |||
| 2019 | NextMetadataNo++; | |||
| 2020 | break; | |||
| 2021 | } | |||
| 2022 | case bitc::METADATA_IMPORTED_ENTITY: { | |||
| 2023 | if (Record.size() != 6 && Record.size() != 7) | |||
| 2024 | return error("Invalid record"); | |||
| 2025 | ||||
| 2026 | IsDistinct = Record[0]; | |||
| 2027 | bool HasFile = (Record.size() == 7); | |||
| 2028 | MetadataList.assignValue( | |||
| 2029 | GET_OR_DISTINCT(DIImportedEntity, | |||
| 2030 | (Context, Record[1], getMDOrNull(Record[2]), | |||
| 2031 | getDITypeRefOrNull(Record[3]), | |||
| 2032 | HasFile ? getMDOrNull(Record[6]) : nullptr, | |||
| 2033 | HasFile ? Record[4] : 0, getMDString(Record[5]))), | |||
| 2034 | NextMetadataNo); | |||
| 2035 | NextMetadataNo++; | |||
| 2036 | break; | |||
| 2037 | } | |||
| 2038 | case bitc::METADATA_STRING_OLD: { | |||
| 2039 | std::string String(Record.begin(), Record.end()); | |||
| 2040 | ||||
| 2041 | // Test for upgrading !llvm.loop. | |||
| 2042 | HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String); | |||
| 2043 | ++NumMDStringLoaded; | |||
| 2044 | Metadata *MD = MDString::get(Context, String); | |||
| 2045 | MetadataList.assignValue(MD, NextMetadataNo); | |||
| 2046 | NextMetadataNo++; | |||
| 2047 | break; | |||
| 2048 | } | |||
| 2049 | case bitc::METADATA_STRINGS: { | |||
| 2050 | auto CreateNextMDString = [&](StringRef Str) { | |||
| 2051 | ++NumMDStringLoaded; | |||
| 2052 | MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo); | |||
| 2053 | NextMetadataNo++; | |||
| 2054 | }; | |||
| 2055 | if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString)) | |||
| 2056 | return Err; | |||
| 2057 | break; | |||
| 2058 | } | |||
| 2059 | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { | |||
| 2060 | if (Record.size() % 2 == 0) | |||
| 2061 | return error("Invalid record"); | |||
| 2062 | unsigned ValueID = Record[0]; | |||
| 2063 | if (ValueID >= ValueList.size()) | |||
| 2064 | return error("Invalid record"); | |||
| 2065 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) | |||
| 2066 | if (Error Err = parseGlobalObjectAttachment( | |||
| 2067 | *GO, ArrayRef<uint64_t>(Record).slice(1))) | |||
| 2068 | return Err; | |||
| 2069 | break; | |||
| 2070 | } | |||
| 2071 | case bitc::METADATA_KIND: { | |||
| 2072 | // Support older bitcode files that had METADATA_KIND records in a | |||
| 2073 | // block with METADATA_BLOCK_ID. | |||
| 2074 | if (Error Err = parseMetadataKindRecord(Record)) | |||
| 2075 | return Err; | |||
| 2076 | break; | |||
| 2077 | } | |||
| 2078 | case bitc::METADATA_ARG_LIST: { | |||
| 2079 | SmallVector<ValueAsMetadata *, 4> Elts; | |||
| 2080 | Elts.reserve(Record.size()); | |||
| 2081 | for (uint64_t Elt : Record) { | |||
| 2082 | Metadata *MD = getMD(Elt); | |||
| 2083 | if (isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) | |||
| 2084 | return error( | |||
| 2085 | "Invalid record: DIArgList should not contain forward refs"); | |||
| 2086 | if (!isa<ValueAsMetadata>(MD)) | |||
| 2087 | return error("Invalid record"); | |||
| 2088 | Elts.push_back(cast<ValueAsMetadata>(MD)); | |||
| 2089 | } | |||
| 2090 | ||||
| 2091 | MetadataList.assignValue(DIArgList::get(Context, Elts), NextMetadataNo); | |||
| 2092 | NextMetadataNo++; | |||
| 2093 | break; | |||
| 2094 | } | |||
| 2095 | } | |||
| 2096 | return Error::success(); | |||
| 2097 | #undef GET_OR_DISTINCT | |||
| 2098 | } | |||
| 2099 | ||||
| 2100 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings( | |||
| 2101 | ArrayRef<uint64_t> Record, StringRef Blob, | |||
| 2102 | function_ref<void(StringRef)> CallBack) { | |||
| 2103 | // All the MDStrings in the block are emitted together in a single | |||
| 2104 | // record. The strings are concatenated and stored in a blob along with | |||
| 2105 | // their sizes. | |||
| 2106 | if (Record.size() != 2) | |||
| 2107 | return error("Invalid record: metadata strings layout"); | |||
| 2108 | ||||
| 2109 | unsigned NumStrings = Record[0]; | |||
| 2110 | unsigned StringsOffset = Record[1]; | |||
| 2111 | if (!NumStrings) | |||
| 2112 | return error("Invalid record: metadata strings with no strings"); | |||
| 2113 | if (StringsOffset > Blob.size()) | |||
| 2114 | return error("Invalid record: metadata strings corrupt offset"); | |||
| 2115 | ||||
| 2116 | StringRef Lengths = Blob.slice(0, StringsOffset); | |||
| 2117 | SimpleBitstreamCursor R(Lengths); | |||
| 2118 | ||||
| 2119 | StringRef Strings = Blob.drop_front(StringsOffset); | |||
| 2120 | do { | |||
| 2121 | if (R.AtEndOfStream()) | |||
| 2122 | return error("Invalid record: metadata strings bad length"); | |||
| 2123 | ||||
| 2124 | Expected<uint32_t> MaybeSize = R.ReadVBR(6); | |||
| 2125 | if (!MaybeSize) | |||
| 2126 | return MaybeSize.takeError(); | |||
| 2127 | uint32_t Size = MaybeSize.get(); | |||
| 2128 | if (Strings.size() < Size) | |||
| 2129 | return error("Invalid record: metadata strings truncated chars"); | |||
| 2130 | ||||
| 2131 | CallBack(Strings.slice(0, Size)); | |||
| 2132 | Strings = Strings.drop_front(Size); | |||
| 2133 | } while (--NumStrings); | |||
| 2134 | ||||
| 2135 | return Error::success(); | |||
| 2136 | } | |||
| 2137 | ||||
| 2138 | Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment( | |||
| 2139 | GlobalObject &GO, ArrayRef<uint64_t> Record) { | |||
| 2140 | assert(Record.size() % 2 == 0)((void)0); | |||
| 2141 | for (unsigned I = 0, E = Record.size(); I != E; I += 2) { | |||
| 2142 | auto K = MDKindMap.find(Record[I]); | |||
| 2143 | if (K == MDKindMap.end()) | |||
| 2144 | return error("Invalid ID"); | |||
| 2145 | MDNode *MD = | |||
| 2146 | dyn_cast_or_null<MDNode>(getMetadataFwdRefOrLoad(Record[I + 1])); | |||
| 2147 | if (!MD) | |||
| 2148 | return error("Invalid metadata attachment: expect fwd ref to MDNode"); | |||
| 2149 | GO.addMetadata(K->second, *MD); | |||
| 2150 | } | |||
| 2151 | return Error::success(); | |||
| 2152 | } | |||
| 2153 | ||||
| 2154 | /// Parse metadata attachments. | |||
| 2155 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment( | |||
| 2156 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { | |||
| 2157 | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) | |||
| 2158 | return Err; | |||
| 2159 | ||||
| 2160 | SmallVector<uint64_t, 64> Record; | |||
| 2161 | PlaceholderQueue Placeholders; | |||
| 2162 | ||||
| 2163 | while (true) { | |||
| 2164 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | |||
| 2165 | if (!MaybeEntry) | |||
| 2166 | return MaybeEntry.takeError(); | |||
| 2167 | BitstreamEntry Entry = MaybeEntry.get(); | |||
| 2168 | ||||
| 2169 | switch (Entry.Kind) { | |||
| 2170 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
| 2171 | case BitstreamEntry::Error: | |||
| 2172 | return error("Malformed block"); | |||
| 2173 | case BitstreamEntry::EndBlock: | |||
| 2174 | resolveForwardRefsAndPlaceholders(Placeholders); | |||
| 2175 | return Error::success(); | |||
| 2176 | case BitstreamEntry::Record: | |||
| 2177 | // The interesting case. | |||
| 2178 | break; | |||
| 2179 | } | |||
| 2180 | ||||
| 2181 | // Read a metadata attachment record. | |||
| 2182 | Record.clear(); | |||
| 2183 | ++NumMDRecordLoaded; | |||
| 2184 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | |||
| 2185 | if (!MaybeRecord) | |||
| 2186 | return MaybeRecord.takeError(); | |||
| 2187 | switch (MaybeRecord.get()) { | |||
| 2188 | default: // Default behavior: ignore. | |||
| 2189 | break; | |||
| 2190 | case bitc::METADATA_ATTACHMENT: { | |||
| 2191 | unsigned RecordLength = Record.size(); | |||
| 2192 | if (Record.empty()) | |||
| 2193 | return error("Invalid record"); | |||
| 2194 | if (RecordLength % 2 == 0) { | |||
| 2195 | // A function attachment. | |||
| 2196 | if (Error Err = parseGlobalObjectAttachment(F, Record)) | |||
| 2197 | return Err; | |||
| 2198 | continue; | |||
| 2199 | } | |||
| 2200 | ||||
| 2201 | // An instruction attachment. | |||
| 2202 | Instruction *Inst = InstructionList[Record[0]]; | |||
| 2203 | for (unsigned i = 1; i != RecordLength; i = i + 2) { | |||
| 2204 | unsigned Kind = Record[i]; | |||
| 2205 | DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind); | |||
| 2206 | if (I == MDKindMap.end()) | |||
| 2207 | return error("Invalid ID"); | |||
| 2208 | if (I->second == LLVMContext::MD_tbaa && StripTBAA) | |||
| 2209 | continue; | |||
| 2210 | ||||
| 2211 | auto Idx = Record[i + 1]; | |||
| 2212 | if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) && | |||
| 2213 | !MetadataList.lookup(Idx)) { | |||
| 2214 | // Load the attachment if it is in the lazy-loadable range and hasn't | |||
| 2215 | // been loaded yet. | |||
| 2216 | lazyLoadOneMetadata(Idx, Placeholders); | |||
| 2217 | resolveForwardRefsAndPlaceholders(Placeholders); | |||
| 2218 | } | |||
| 2219 | ||||
| 2220 | Metadata *Node = MetadataList.getMetadataFwdRef(Idx); | |||
| 2221 | if (isa<LocalAsMetadata>(Node)) | |||
| 2222 | // Drop the attachment. This used to be legal, but there's no | |||
| 2223 | // upgrade path. | |||
| 2224 | break; | |||
| 2225 | MDNode *MD = dyn_cast_or_null<MDNode>(Node); | |||
| 2226 | if (!MD) | |||
| 2227 | return error("Invalid metadata attachment"); | |||
| 2228 | ||||
| 2229 | if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop) | |||
| 2230 | MD = upgradeInstructionLoopAttachment(*MD); | |||
| 2231 | ||||
| 2232 | if (I->second == LLVMContext::MD_tbaa) { | |||
| 2233 | assert(!MD->isTemporary() && "should load MDs before attachments")((void)0); | |||
| 2234 | MD = UpgradeTBAANode(*MD); | |||
| 2235 | } | |||
| 2236 | Inst->setMetadata(I->second, MD); | |||
| 2237 | } | |||
| 2238 | break; | |||
| 2239 | } | |||
| 2240 | } | |||
| 2241 | } | |||
| 2242 | } | |||
| 2243 | ||||
| 2244 | /// Parse a single METADATA_KIND record, inserting result in MDKindMap. | |||
| 2245 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord( | |||
| 2246 | SmallVectorImpl<uint64_t> &Record) { | |||
| 2247 | if (Record.size() < 2) | |||
| 2248 | return error("Invalid record"); | |||
| 2249 | ||||
| 2250 | unsigned Kind = Record[0]; | |||
| 2251 | SmallString<8> Name(Record.begin() + 1, Record.end()); | |||
| 2252 | ||||
| 2253 | unsigned NewKind = TheModule.getMDKindID(Name.str()); | |||
| 2254 | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) | |||
| 2255 | return error("Conflicting METADATA_KIND records"); | |||
| 2256 | return Error::success(); | |||
| 2257 | } | |||
| 2258 | ||||
| 2259 | /// Parse the metadata kinds out of the METADATA_KIND_BLOCK. | |||
| 2260 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() { | |||
| 2261 | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID)) | |||
| 2262 | return Err; | |||
| 2263 | ||||
| 2264 | SmallVector<uint64_t, 64> Record; | |||
| 2265 | ||||
| 2266 | // Read all the records. | |||
| 2267 | while (true) { | |||
| 2268 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | |||
| 2269 | if (!MaybeEntry) | |||
| 2270 | return MaybeEntry.takeError(); | |||
| 2271 | BitstreamEntry Entry = MaybeEntry.get(); | |||
| 2272 | ||||
| 2273 | switch (Entry.Kind) { | |||
| 2274 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
| 2275 | case BitstreamEntry::Error: | |||
| 2276 | return error("Malformed block"); | |||
| 2277 | case BitstreamEntry::EndBlock: | |||
| 2278 | return Error::success(); | |||
| 2279 | case BitstreamEntry::Record: | |||
| 2280 | // The interesting case. | |||
| 2281 | break; | |||
| 2282 | } | |||
| 2283 | ||||
| 2284 | // Read a record. | |||
| 2285 | Record.clear(); | |||
| 2286 | ++NumMDRecordLoaded; | |||
| 2287 | Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record); | |||
| 2288 | if (!MaybeCode) | |||
| 2289 | return MaybeCode.takeError(); | |||
| 2290 | switch (MaybeCode.get()) { | |||
| 2291 | default: // Default behavior: ignore. | |||
| 2292 | break; | |||
| 2293 | case bitc::METADATA_KIND: { | |||
| 2294 | if (Error Err = parseMetadataKindRecord(Record)) | |||
| 2295 | return Err; | |||
| 2296 | break; | |||
| 2297 | } | |||
| 2298 | } | |||
| 2299 | } | |||
| 2300 | } | |||
| 2301 | ||||
| 2302 | MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) { | |||
| 2303 | Pimpl = std::move(RHS.Pimpl); | |||
| 2304 | return *this; | |||
| 2305 | } | |||
| 2306 | MetadataLoader::MetadataLoader(MetadataLoader &&RHS) | |||
| 2307 | : Pimpl(std::move(RHS.Pimpl)) {} | |||
| 2308 | ||||
| 2309 | MetadataLoader::~MetadataLoader() = default; | |||
| 2310 | MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule, | |||
| 2311 | BitcodeReaderValueList &ValueList, | |||
| 2312 | bool IsImporting, | |||
| 2313 | std::function<Type *(unsigned)> getTypeByID) | |||
| 2314 | : Pimpl(std::make_unique<MetadataLoaderImpl>( | |||
| 2315 | Stream, TheModule, ValueList, std::move(getTypeByID), IsImporting)) {} | |||
| 2316 | ||||
| 2317 | Error MetadataLoader::parseMetadata(bool ModuleLevel) { | |||
| 2318 | return Pimpl->parseMetadata(ModuleLevel); | |||
| 2319 | } | |||
| 2320 | ||||
| 2321 | bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); } | |||
| 2322 | ||||
| 2323 | /// Return the given metadata, creating a replaceable forward reference if | |||
| 2324 | /// necessary. | |||
| 2325 | Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) { | |||
| 2326 | return Pimpl->getMetadataFwdRefOrLoad(Idx); | |||
| 2327 | } | |||
| 2328 | ||||
| 2329 | DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) { | |||
| 2330 | return Pimpl->lookupSubprogramForFunction(F); | |||
| 2331 | } | |||
| 2332 | ||||
| 2333 | Error MetadataLoader::parseMetadataAttachment( | |||
| 2334 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { | |||
| 2335 | return Pimpl->parseMetadataAttachment(F, InstructionList); | |||
| 2336 | } | |||
| 2337 | ||||
| 2338 | Error MetadataLoader::parseMetadataKinds() { | |||
| 2339 | return Pimpl->parseMetadataKinds(); | |||
| 2340 | } | |||
| 2341 | ||||
| 2342 | void MetadataLoader::setStripTBAA(bool StripTBAA) { | |||
| 2343 | return Pimpl->setStripTBAA(StripTBAA); | |||
| 2344 | } | |||
| 2345 | ||||
| 2346 | bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); } | |||
| 2347 | ||||
| 2348 | unsigned MetadataLoader::size() const { return Pimpl->size(); } | |||
| 2349 | void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); } | |||
| 2350 | ||||
| 2351 | void MetadataLoader::upgradeDebugIntrinsics(Function &F) { | |||
| 2352 | return Pimpl->upgradeDebugIntrinsics(F); | |||
| 2353 | } |
| 1 | //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines an API used to report recoverable errors. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_ERROR_H |
| 14 | #define LLVM_SUPPORT_ERROR_H |
| 15 | |
| 16 | #include "llvm-c/Error.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/ADT/Twine.h" |
| 21 | #include "llvm/Config/abi-breaking.h" |
| 22 | #include "llvm/Support/AlignOf.h" |
| 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/ErrorOr.h" |
| 27 | #include "llvm/Support/Format.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <cstdint> |
| 32 | #include <cstdlib> |
| 33 | #include <functional> |
| 34 | #include <memory> |
| 35 | #include <new> |
| 36 | #include <string> |
| 37 | #include <system_error> |
| 38 | #include <type_traits> |
| 39 | #include <utility> |
| 40 | #include <vector> |
| 41 | |
| 42 | namespace llvm { |
| 43 | |
| 44 | class ErrorSuccess; |
| 45 | |
| 46 | /// Base class for error info classes. Do not extend this directly: Extend |
| 47 | /// the ErrorInfo template subclass instead. |
| 48 | class ErrorInfoBase { |
| 49 | public: |
| 50 | virtual ~ErrorInfoBase() = default; |
| 51 | |
| 52 | /// Print an error message to an output stream. |
| 53 | virtual void log(raw_ostream &OS) const = 0; |
| 54 | |
| 55 | /// Return the error message as a string. |
| 56 | virtual std::string message() const { |
| 57 | std::string Msg; |
| 58 | raw_string_ostream OS(Msg); |
| 59 | log(OS); |
| 60 | return OS.str(); |
| 61 | } |
| 62 | |
| 63 | /// Convert this error to a std::error_code. |
| 64 | /// |
| 65 | /// This is a temporary crutch to enable interaction with code still |
| 66 | /// using std::error_code. It will be removed in the future. |
| 67 | virtual std::error_code convertToErrorCode() const = 0; |
| 68 | |
| 69 | // Returns the class ID for this type. |
| 70 | static const void *classID() { return &ID; } |
| 71 | |
| 72 | // Returns the class ID for the dynamic type of this ErrorInfoBase instance. |
| 73 | virtual const void *dynamicClassID() const = 0; |
| 74 | |
| 75 | // Check whether this instance is a subclass of the class identified by |
| 76 | // ClassID. |
| 77 | virtual bool isA(const void *const ClassID) const { |
| 78 | return ClassID == classID(); |
| 79 | } |
| 80 | |
| 81 | // Check whether this instance is a subclass of ErrorInfoT. |
| 82 | template <typename ErrorInfoT> bool isA() const { |
| 83 | return isA(ErrorInfoT::classID()); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | virtual void anchor(); |
| 88 | |
| 89 | static char ID; |
| 90 | }; |
| 91 | |
| 92 | /// Lightweight error class with error context and mandatory checking. |
| 93 | /// |
| 94 | /// Instances of this class wrap a ErrorInfoBase pointer. Failure states |
| 95 | /// are represented by setting the pointer to a ErrorInfoBase subclass |
| 96 | /// instance containing information describing the failure. Success is |
| 97 | /// represented by a null pointer value. |
| 98 | /// |
| 99 | /// Instances of Error also contains a 'Checked' flag, which must be set |
| 100 | /// before the destructor is called, otherwise the destructor will trigger a |
| 101 | /// runtime error. This enforces at runtime the requirement that all Error |
| 102 | /// instances be checked or returned to the caller. |
| 103 | /// |
| 104 | /// There are two ways to set the checked flag, depending on what state the |
| 105 | /// Error instance is in. For Error instances indicating success, it |
| 106 | /// is sufficient to invoke the boolean conversion operator. E.g.: |
| 107 | /// |
| 108 | /// @code{.cpp} |
| 109 | /// Error foo(<...>); |
| 110 | /// |
| 111 | /// if (auto E = foo(<...>)) |
| 112 | /// return E; // <- Return E if it is in the error state. |
| 113 | /// // We have verified that E was in the success state. It can now be safely |
| 114 | /// // destroyed. |
| 115 | /// @endcode |
| 116 | /// |
| 117 | /// A success value *can not* be dropped. For example, just calling 'foo(<...>)' |
| 118 | /// without testing the return value will raise a runtime error, even if foo |
| 119 | /// returns success. |
| 120 | /// |
| 121 | /// For Error instances representing failure, you must use either the |
| 122 | /// handleErrors or handleAllErrors function with a typed handler. E.g.: |
| 123 | /// |
| 124 | /// @code{.cpp} |
| 125 | /// class MyErrorInfo : public ErrorInfo<MyErrorInfo> { |
| 126 | /// // Custom error info. |
| 127 | /// }; |
| 128 | /// |
| 129 | /// Error foo(<...>) { return make_error<MyErrorInfo>(...); } |
| 130 | /// |
| 131 | /// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo. |
| 132 | /// auto NewE = |
| 133 | /// handleErrors(E, |
| 134 | /// [](const MyErrorInfo &M) { |
| 135 | /// // Deal with the error. |
| 136 | /// }, |
| 137 | /// [](std::unique_ptr<OtherError> M) -> Error { |
| 138 | /// if (canHandle(*M)) { |
| 139 | /// // handle error. |
| 140 | /// return Error::success(); |
| 141 | /// } |
| 142 | /// // Couldn't handle this error instance. Pass it up the stack. |
| 143 | /// return Error(std::move(M)); |
| 144 | /// ); |
| 145 | /// // Note - we must check or return NewE in case any of the handlers |
| 146 | /// // returned a new error. |
| 147 | /// @endcode |
| 148 | /// |
| 149 | /// The handleAllErrors function is identical to handleErrors, except |
| 150 | /// that it has a void return type, and requires all errors to be handled and |
| 151 | /// no new errors be returned. It prevents errors (assuming they can all be |
| 152 | /// handled) from having to be bubbled all the way to the top-level. |
| 153 | /// |
| 154 | /// *All* Error instances must be checked before destruction, even if |
| 155 | /// they're moved-assigned or constructed from Success values that have already |
| 156 | /// been checked. This enforces checking through all levels of the call stack. |
| 157 | class LLVM_NODISCARD[[clang::warn_unused_result]] Error { |
| 158 | // ErrorList needs to be able to yank ErrorInfoBase pointers out of Errors |
| 159 | // to add to the error list. It can't rely on handleErrors for this, since |
| 160 | // handleErrors does not support ErrorList handlers. |
| 161 | friend class ErrorList; |
| 162 | |
| 163 | // handleErrors needs to be able to set the Checked flag. |
| 164 | template <typename... HandlerTs> |
| 165 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
| 166 | |
| 167 | // Expected<T> needs to be able to steal the payload when constructed from an |
| 168 | // error. |
| 169 | template <typename T> friend class Expected; |
| 170 | |
| 171 | // wrap needs to be able to steal the payload. |
| 172 | friend LLVMErrorRef wrap(Error); |
| 173 | |
| 174 | protected: |
| 175 | /// Create a success value. Prefer using 'Error::success()' for readability |
| 176 | Error() { |
| 177 | setPtr(nullptr); |
| 178 | setChecked(false); |
| 179 | } |
| 180 | |
| 181 | public: |
| 182 | /// Create a success value. |
| 183 | static ErrorSuccess success(); |
| 184 | |
| 185 | // Errors are not copy-constructable. |
| 186 | Error(const Error &Other) = delete; |
| 187 | |
| 188 | /// Move-construct an error value. The newly constructed error is considered |
| 189 | /// unchecked, even if the source error had been checked. The original error |
| 190 | /// becomes a checked Success value, regardless of its original state. |
| 191 | Error(Error &&Other) { |
| 192 | setChecked(true); |
| 193 | *this = std::move(Other); |
| 194 | } |
| 195 | |
| 196 | /// Create an error value. Prefer using the 'make_error' function, but |
| 197 | /// this constructor can be useful when "re-throwing" errors from handlers. |
| 198 | Error(std::unique_ptr<ErrorInfoBase> Payload) { |
| 199 | setPtr(Payload.release()); |
| 200 | setChecked(false); |
| 201 | } |
| 202 | |
| 203 | // Errors are not copy-assignable. |
| 204 | Error &operator=(const Error &Other) = delete; |
| 205 | |
| 206 | /// Move-assign an error value. The current error must represent success, you |
| 207 | /// you cannot overwrite an unhandled error. The current error is then |
| 208 | /// considered unchecked. The source error becomes a checked success value, |
| 209 | /// regardless of its original state. |
| 210 | Error &operator=(Error &&Other) { |
| 211 | // Don't allow overwriting of unchecked values. |
| 212 | assertIsChecked(); |
| 213 | setPtr(Other.getPtr()); |
| 214 | |
| 215 | // This Error is unchecked, even if the source error was checked. |
| 216 | setChecked(false); |
| 217 | |
| 218 | // Null out Other's payload and set its checked bit. |
| 219 | Other.setPtr(nullptr); |
| 220 | Other.setChecked(true); |
| 221 | |
| 222 | return *this; |
| 223 | } |
| 224 | |
| 225 | /// Destroy a Error. Fails with a call to abort() if the error is |
| 226 | /// unchecked. |
| 227 | ~Error() { |
| 228 | assertIsChecked(); |
| 229 | delete getPtr(); |
| 230 | } |
| 231 | |
| 232 | /// Bool conversion. Returns true if this Error is in a failure state, |
| 233 | /// and false if it is in an accept state. If the error is in a Success state |
| 234 | /// it will be considered checked. |
| 235 | explicit operator bool() { |
| 236 | setChecked(getPtr() == nullptr); |
| 237 | return getPtr() != nullptr; |
| 238 | } |
| 239 | |
| 240 | /// Check whether one error is a subclass of another. |
| 241 | template <typename ErrT> bool isA() const { |
| 242 | return getPtr() && getPtr()->isA(ErrT::classID()); |
| 243 | } |
| 244 | |
| 245 | /// Returns the dynamic class id of this error, or null if this is a success |
| 246 | /// value. |
| 247 | const void* dynamicClassID() const { |
| 248 | if (!getPtr()) |
| 249 | return nullptr; |
| 250 | return getPtr()->dynamicClassID(); |
| 251 | } |
| 252 | |
| 253 | private: |
| 254 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 255 | // assertIsChecked() happens very frequently, but under normal circumstances |
| 256 | // is supposed to be a no-op. So we want it to be inlined, but having a bunch |
| 257 | // of debug prints can cause the function to be too large for inlining. So |
| 258 | // it's important that we define this function out of line so that it can't be |
| 259 | // inlined. |
| 260 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
| 261 | void fatalUncheckedError() const; |
| 262 | #endif |
| 263 | |
| 264 | void assertIsChecked() { |
| 265 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 266 | if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false)) |
| 267 | fatalUncheckedError(); |
| 268 | #endif |
| 269 | } |
| 270 | |
| 271 | ErrorInfoBase *getPtr() const { |
| 272 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 273 | return reinterpret_cast<ErrorInfoBase*>( |
| 274 | reinterpret_cast<uintptr_t>(Payload) & |
| 275 | ~static_cast<uintptr_t>(0x1)); |
| 276 | #else |
| 277 | return Payload; |
| 278 | #endif |
| 279 | } |
| 280 | |
| 281 | void setPtr(ErrorInfoBase *EI) { |
| 282 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 283 | Payload = reinterpret_cast<ErrorInfoBase*>( |
| 284 | (reinterpret_cast<uintptr_t>(EI) & |
| 285 | ~static_cast<uintptr_t>(0x1)) | |
| 286 | (reinterpret_cast<uintptr_t>(Payload) & 0x1)); |
| 287 | #else |
| 288 | Payload = EI; |
| 289 | #endif |
| 290 | } |
| 291 | |
| 292 | bool getChecked() const { |
| 293 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 294 | return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; |
| 295 | #else |
| 296 | return true; |
| 297 | #endif |
| 298 | } |
| 299 | |
| 300 | void setChecked(bool V) { |
| 301 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 302 | Payload = reinterpret_cast<ErrorInfoBase*>( |
| 303 | (reinterpret_cast<uintptr_t>(Payload) & |
| 304 | ~static_cast<uintptr_t>(0x1)) | |
| 305 | (V ? 0 : 1)); |
| 306 | #endif |
| 307 | } |
| 308 | |
| 309 | std::unique_ptr<ErrorInfoBase> takePayload() { |
| 310 | std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); |
| 311 | setPtr(nullptr); |
| 312 | setChecked(true); |
| 313 | return Tmp; |
| 314 | } |
| 315 | |
| 316 | friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { |
| 317 | if (auto P = E.getPtr()) |
| 318 | P->log(OS); |
| 319 | else |
| 320 | OS << "success"; |
| 321 | return OS; |
| 322 | } |
| 323 | |
| 324 | ErrorInfoBase *Payload = nullptr; |
| 325 | }; |
| 326 | |
| 327 | /// Subclass of Error for the sole purpose of identifying the success path in |
| 328 | /// the type system. This allows to catch invalid conversion to Expected<T> at |
| 329 | /// compile time. |
| 330 | class ErrorSuccess final : public Error {}; |
| 331 | |
| 332 | inline ErrorSuccess Error::success() { return ErrorSuccess(); } |
| 333 | |
| 334 | /// Make a Error instance representing failure using the given error info |
| 335 | /// type. |
| 336 | template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { |
| 337 | return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); |
| 338 | } |
| 339 | |
| 340 | /// Base class for user error types. Users should declare their error types |
| 341 | /// like: |
| 342 | /// |
| 343 | /// class MyError : public ErrorInfo<MyError> { |
| 344 | /// .... |
| 345 | /// }; |
| 346 | /// |
| 347 | /// This class provides an implementation of the ErrorInfoBase::kind |
| 348 | /// method, which is used by the Error RTTI system. |
| 349 | template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> |
| 350 | class ErrorInfo : public ParentErrT { |
| 351 | public: |
| 352 | using ParentErrT::ParentErrT; // inherit constructors |
| 353 | |
| 354 | static const void *classID() { return &ThisErrT::ID; } |
| 355 | |
| 356 | const void *dynamicClassID() const override { return &ThisErrT::ID; } |
| 357 | |
| 358 | bool isA(const void *const ClassID) const override { |
| 359 | return ClassID == classID() || ParentErrT::isA(ClassID); |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | /// Special ErrorInfo subclass representing a list of ErrorInfos. |
| 364 | /// Instances of this class are constructed by joinError. |
| 365 | class ErrorList final : public ErrorInfo<ErrorList> { |
| 366 | // handleErrors needs to be able to iterate the payload list of an |
| 367 | // ErrorList. |
| 368 | template <typename... HandlerTs> |
| 369 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
| 370 | |
| 371 | // joinErrors is implemented in terms of join. |
| 372 | friend Error joinErrors(Error, Error); |
| 373 | |
| 374 | public: |
| 375 | void log(raw_ostream &OS) const override { |
| 376 | OS << "Multiple errors:\n"; |
| 377 | for (auto &ErrPayload : Payloads) { |
| 378 | ErrPayload->log(OS); |
| 379 | OS << "\n"; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | std::error_code convertToErrorCode() const override; |
| 384 | |
| 385 | // Used by ErrorInfo::classID. |
| 386 | static char ID; |
| 387 | |
| 388 | private: |
| 389 | ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, |
| 390 | std::unique_ptr<ErrorInfoBase> Payload2) { |
| 391 | assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((void)0) |
| 392 | "ErrorList constructor payloads should be singleton errors")((void)0); |
| 393 | Payloads.push_back(std::move(Payload1)); |
| 394 | Payloads.push_back(std::move(Payload2)); |
| 395 | } |
| 396 | |
| 397 | static Error join(Error E1, Error E2) { |
| 398 | if (!E1) |
| 399 | return E2; |
| 400 | if (!E2) |
| 401 | return E1; |
| 402 | if (E1.isA<ErrorList>()) { |
| 403 | auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); |
| 404 | if (E2.isA<ErrorList>()) { |
| 405 | auto E2Payload = E2.takePayload(); |
| 406 | auto &E2List = static_cast<ErrorList &>(*E2Payload); |
| 407 | for (auto &Payload : E2List.Payloads) |
| 408 | E1List.Payloads.push_back(std::move(Payload)); |
| 409 | } else |
| 410 | E1List.Payloads.push_back(E2.takePayload()); |
| 411 | |
| 412 | return E1; |
| 413 | } |
| 414 | if (E2.isA<ErrorList>()) { |
| 415 | auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); |
| 416 | E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); |
| 417 | return E2; |
| 418 | } |
| 419 | return Error(std::unique_ptr<ErrorList>( |
| 420 | new ErrorList(E1.takePayload(), E2.takePayload()))); |
| 421 | } |
| 422 | |
| 423 | std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; |
| 424 | }; |
| 425 | |
| 426 | /// Concatenate errors. The resulting Error is unchecked, and contains the |
| 427 | /// ErrorInfo(s), if any, contained in E1, followed by the |
| 428 | /// ErrorInfo(s), if any, contained in E2. |
| 429 | inline Error joinErrors(Error E1, Error E2) { |
| 430 | return ErrorList::join(std::move(E1), std::move(E2)); |
| 431 | } |
| 432 | |
| 433 | /// Tagged union holding either a T or a Error. |
| 434 | /// |
| 435 | /// This class parallels ErrorOr, but replaces error_code with Error. Since |
| 436 | /// Error cannot be copied, this class replaces getError() with |
| 437 | /// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the |
| 438 | /// error class type. |
| 439 | /// |
| 440 | /// Example usage of 'Expected<T>' as a function return type: |
| 441 | /// |
| 442 | /// @code{.cpp} |
| 443 | /// Expected<int> myDivide(int A, int B) { |
| 444 | /// if (B == 0) { |
| 445 | /// // return an Error |
| 446 | /// return createStringError(inconvertibleErrorCode(), |
| 447 | /// "B must not be zero!"); |
| 448 | /// } |
| 449 | /// // return an integer |
| 450 | /// return A / B; |
| 451 | /// } |
| 452 | /// @endcode |
| 453 | /// |
| 454 | /// Checking the results of to a function returning 'Expected<T>': |
| 455 | /// @code{.cpp} |
| 456 | /// if (auto E = Result.takeError()) { |
| 457 | /// // We must consume the error. Typically one of: |
| 458 | /// // - return the error to our caller |
| 459 | /// // - toString(), when logging |
| 460 | /// // - consumeError(), to silently swallow the error |
| 461 | /// // - handleErrors(), to distinguish error types |
| 462 | /// errs() << "Problem with division " << toString(std::move(E)) << "\n"; |
| 463 | /// return; |
| 464 | /// } |
| 465 | /// // use the result |
| 466 | /// outs() << "The answer is " << *Result << "\n"; |
| 467 | /// @endcode |
| 468 | /// |
| 469 | /// For unit-testing a function returning an 'Expceted<T>', see the |
| 470 | /// 'EXPECT_THAT_EXPECTED' macros in llvm/Testing/Support/Error.h |
| 471 | |
| 472 | template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected { |
| 473 | template <class T1> friend class ExpectedAsOutParameter; |
| 474 | template <class OtherT> friend class Expected; |
| 475 | |
| 476 | static constexpr bool isRef = std::is_reference<T>::value; |
| 477 | |
| 478 | using wrap = std::reference_wrapper<std::remove_reference_t<T>>; |
| 479 | |
| 480 | using error_type = std::unique_ptr<ErrorInfoBase>; |
| 481 | |
| 482 | public: |
| 483 | using storage_type = std::conditional_t<isRef, wrap, T>; |
| 484 | using value_type = T; |
| 485 | |
| 486 | private: |
| 487 | using reference = std::remove_reference_t<T> &; |
| 488 | using const_reference = const std::remove_reference_t<T> &; |
| 489 | using pointer = std::remove_reference_t<T> *; |
| 490 | using const_pointer = const std::remove_reference_t<T> *; |
| 491 | |
| 492 | public: |
| 493 | /// Create an Expected<T> error value from the given Error. |
| 494 | Expected(Error Err) |
| 495 | : HasError(true) |
| 496 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 497 | // Expected is unchecked upon construction in Debug builds. |
| 498 | , Unchecked(true) |
| 499 | #endif |
| 500 | { |
| 501 | assert(Err && "Cannot create Expected<T> from Error success value.")((void)0); |
| 502 | new (getErrorStorage()) error_type(Err.takePayload()); |
| 503 | } |
| 504 | |
| 505 | /// Forbid to convert from Error::success() implicitly, this avoids having |
| 506 | /// Expected<T> foo() { return Error::success(); } which compiles otherwise |
| 507 | /// but triggers the assertion above. |
| 508 | Expected(ErrorSuccess) = delete; |
| 509 | |
| 510 | /// Create an Expected<T> success value from the given OtherT value, which |
| 511 | /// must be convertible to T. |
| 512 | template <typename OtherT> |
| 513 | Expected(OtherT &&Val, |
| 514 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) |
| 515 | : HasError(false) |
| 516 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 517 | // Expected is unchecked upon construction in Debug builds. |
| 518 | , |
| 519 | Unchecked(true) |
| 520 | #endif |
| 521 | { |
| 522 | new (getStorage()) storage_type(std::forward<OtherT>(Val)); |
| 523 | } |
| 524 | |
| 525 | /// Move construct an Expected<T> value. |
| 526 | Expected(Expected &&Other) { moveConstruct(std::move(Other)); } |
| 527 | |
| 528 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
| 529 | /// must be convertible to T. |
| 530 | template <class OtherT> |
| 531 | Expected( |
| 532 | Expected<OtherT> &&Other, |
| 533 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) { |
| 534 | moveConstruct(std::move(Other)); |
| 535 | } |
| 536 | |
| 537 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
| 538 | /// isn't convertible to T. |
| 539 | template <class OtherT> |
| 540 | explicit Expected( |
| 541 | Expected<OtherT> &&Other, |
| 542 | std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) { |
| 543 | moveConstruct(std::move(Other)); |
| 544 | } |
| 545 | |
| 546 | /// Move-assign from another Expected<T>. |
| 547 | Expected &operator=(Expected &&Other) { |
| 548 | moveAssign(std::move(Other)); |
| 549 | return *this; |
| 550 | } |
| 551 | |
| 552 | /// Destroy an Expected<T>. |
| 553 | ~Expected() { |
| 554 | assertIsChecked(); |
| 555 | if (!HasError) |
| 556 | getStorage()->~storage_type(); |
| 557 | else |
| 558 | getErrorStorage()->~error_type(); |
| 559 | } |
| 560 | |
| 561 | /// Return false if there is an error. |
| 562 | explicit operator bool() { |
| 563 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 564 | Unchecked = HasError; |
| 565 | #endif |
| 566 | return !HasError; |
| 567 | } |
| 568 | |
| 569 | /// Returns a reference to the stored T value. |
| 570 | reference get() { |
| 571 | assertIsChecked(); |
| 572 | return *getStorage(); |
| 573 | } |
| 574 | |
| 575 | /// Returns a const reference to the stored T value. |
| 576 | const_reference get() const { |
| 577 | assertIsChecked(); |
| 578 | return const_cast<Expected<T> *>(this)->get(); |
| 579 | } |
| 580 | |
| 581 | /// Check that this Expected<T> is an error of type ErrT. |
| 582 | template <typename ErrT> bool errorIsA() const { |
| 583 | return HasError && (*getErrorStorage())->template isA<ErrT>(); |
| 584 | } |
| 585 | |
| 586 | /// Take ownership of the stored error. |
| 587 | /// After calling this the Expected<T> is in an indeterminate state that can |
| 588 | /// only be safely destructed. No further calls (beside the destructor) should |
| 589 | /// be made on the Expected<T> value. |
| 590 | Error takeError() { |
| 591 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 592 | Unchecked = false; |
| 593 | #endif |
| 594 | return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); |
| 595 | } |
| 596 | |
| 597 | /// Returns a pointer to the stored T value. |
| 598 | pointer operator->() { |
| 599 | assertIsChecked(); |
| 600 | return toPointer(getStorage()); |
| 601 | } |
| 602 | |
| 603 | /// Returns a const pointer to the stored T value. |
| 604 | const_pointer operator->() const { |
| 605 | assertIsChecked(); |
| 606 | return toPointer(getStorage()); |
| 607 | } |
| 608 | |
| 609 | /// Returns a reference to the stored T value. |
| 610 | reference operator*() { |
| 611 | assertIsChecked(); |
| 612 | return *getStorage(); |
| 613 | } |
| 614 | |
| 615 | /// Returns a const reference to the stored T value. |
| 616 | const_reference operator*() const { |
| 617 | assertIsChecked(); |
| 618 | return *getStorage(); |
| 619 | } |
| 620 | |
| 621 | private: |
| 622 | template <class T1> |
| 623 | static bool compareThisIfSameType(const T1 &a, const T1 &b) { |
| 624 | return &a == &b; |
| 625 | } |
| 626 | |
| 627 | template <class T1, class T2> |
| 628 | static bool compareThisIfSameType(const T1 &, const T2 &) { |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { |
| 633 | HasError = Other.HasError; |
| 634 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 635 | Unchecked = true; |
| 636 | Other.Unchecked = false; |
| 637 | #endif |
| 638 | |
| 639 | if (!HasError) |
| 640 | new (getStorage()) storage_type(std::move(*Other.getStorage())); |
| 641 | else |
| 642 | new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); |
| 643 | } |
| 644 | |
| 645 | template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { |
| 646 | assertIsChecked(); |
| 647 | |
| 648 | if (compareThisIfSameType(*this, Other)) |
| 649 | return; |
| 650 | |
| 651 | this->~Expected(); |
| 652 | new (this) Expected(std::move(Other)); |
| 653 | } |
| 654 | |
| 655 | pointer toPointer(pointer Val) { return Val; } |
| 656 | |
| 657 | const_pointer toPointer(const_pointer Val) const { return Val; } |
| 658 | |
| 659 | pointer toPointer(wrap *Val) { return &Val->get(); } |
| 660 | |
| 661 | const_pointer toPointer(const wrap *Val) const { return &Val->get(); } |
| 662 | |
| 663 | storage_type *getStorage() { |
| 664 | assert(!HasError && "Cannot get value when an error exists!")((void)0); |
| 665 | return reinterpret_cast<storage_type *>(&TStorage); |
| 666 | } |
| 667 | |
| 668 | const storage_type *getStorage() const { |
| 669 | assert(!HasError && "Cannot get value when an error exists!")((void)0); |
| 670 | return reinterpret_cast<const storage_type *>(&TStorage); |
| 671 | } |
| 672 | |
| 673 | error_type *getErrorStorage() { |
| 674 | assert(HasError && "Cannot get error when a value exists!")((void)0); |
| 675 | return reinterpret_cast<error_type *>(&ErrorStorage); |
| 676 | } |
| 677 | |
| 678 | const error_type *getErrorStorage() const { |
| 679 | assert(HasError && "Cannot get error when a value exists!")((void)0); |
| 680 | return reinterpret_cast<const error_type *>(&ErrorStorage); |
| 681 | } |
| 682 | |
| 683 | // Used by ExpectedAsOutParameter to reset the checked flag. |
| 684 | void setUnchecked() { |
| 685 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 686 | Unchecked = true; |
| 687 | #endif |
| 688 | } |
| 689 | |
| 690 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 691 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
| 692 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) |
| 693 | void fatalUncheckedExpected() const { |
| 694 | dbgs() << "Expected<T> must be checked before access or destruction.\n"; |
| 695 | if (HasError) { |
| 696 | dbgs() << "Unchecked Expected<T> contained error:\n"; |
| 697 | (*getErrorStorage())->log(dbgs()); |
| 698 | } else |
| 699 | dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " |
| 700 | "values in success mode must still be checked prior to being " |
| 701 | "destroyed).\n"; |
| 702 | abort(); |
| 703 | } |
| 704 | #endif |
| 705 | |
| 706 | void assertIsChecked() const { |
| 707 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 708 | if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false)) |
| 709 | fatalUncheckedExpected(); |
| 710 | #endif |
| 711 | } |
| 712 | |
| 713 | union { |
| 714 | AlignedCharArrayUnion<storage_type> TStorage; |
| 715 | AlignedCharArrayUnion<error_type> ErrorStorage; |
| 716 | }; |
| 717 | bool HasError : 1; |
| 718 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS0 |
| 719 | bool Unchecked : 1; |
| 720 | #endif |
| 721 | }; |
| 722 | |
| 723 | /// Report a serious error, calling any installed error handler. See |
| 724 | /// ErrorHandling.h. |
| 725 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err, |
| 726 | bool gen_crash_diag = true); |
| 727 | |
| 728 | /// Report a fatal error if Err is a failure value. |
| 729 | /// |
| 730 | /// This function can be used to wrap calls to fallible functions ONLY when it |
| 731 | /// is known that the Error will always be a success value. E.g. |
| 732 | /// |
| 733 | /// @code{.cpp} |
| 734 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
| 735 | /// // true. If DoFallibleOperation is false then foo always returns |
| 736 | /// // Error::success(). |
| 737 | /// Error foo(bool DoFallibleOperation); |
| 738 | /// |
| 739 | /// cantFail(foo(false)); |
| 740 | /// @endcode |
| 741 | inline void cantFail(Error Err, const char *Msg = nullptr) { |
| 742 | if (Err) { |
| 743 | if (!Msg) |
| 744 | Msg = "Failure value returned from cantFail wrapped call"; |
| 745 | #ifndef NDEBUG1 |
| 746 | std::string Str; |
| 747 | raw_string_ostream OS(Str); |
| 748 | OS << Msg << "\n" << Err; |
| 749 | Msg = OS.str().c_str(); |
| 750 | #endif |
| 751 | llvm_unreachable(Msg)__builtin_unreachable(); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
| 756 | /// returns the contained value. |
| 757 | /// |
| 758 | /// This function can be used to wrap calls to fallible functions ONLY when it |
| 759 | /// is known that the Error will always be a success value. E.g. |
| 760 | /// |
| 761 | /// @code{.cpp} |
| 762 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
| 763 | /// // true. If DoFallibleOperation is false then foo always returns an int. |
| 764 | /// Expected<int> foo(bool DoFallibleOperation); |
| 765 | /// |
| 766 | /// int X = cantFail(foo(false)); |
| 767 | /// @endcode |
| 768 | template <typename T> |
| 769 | T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { |
| 770 | if (ValOrErr) |
| 771 | return std::move(*ValOrErr); |
| 772 | else { |
| 773 | if (!Msg) |
| 774 | Msg = "Failure value returned from cantFail wrapped call"; |
| 775 | #ifndef NDEBUG1 |
| 776 | std::string Str; |
| 777 | raw_string_ostream OS(Str); |
| 778 | auto E = ValOrErr.takeError(); |
| 779 | OS << Msg << "\n" << E; |
| 780 | Msg = OS.str().c_str(); |
| 781 | #endif |
| 782 | llvm_unreachable(Msg)__builtin_unreachable(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
| 787 | /// returns the contained reference. |
| 788 | /// |
| 789 | /// This function can be used to wrap calls to fallible functions ONLY when it |
| 790 | /// is known that the Error will always be a success value. E.g. |
| 791 | /// |
| 792 | /// @code{.cpp} |
| 793 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
| 794 | /// // true. If DoFallibleOperation is false then foo always returns a Bar&. |
| 795 | /// Expected<Bar&> foo(bool DoFallibleOperation); |
| 796 | /// |
| 797 | /// Bar &X = cantFail(foo(false)); |
| 798 | /// @endcode |
| 799 | template <typename T> |
| 800 | T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { |
| 801 | if (ValOrErr) |
| 802 | return *ValOrErr; |
| 803 | else { |
| 804 | if (!Msg) |
| 805 | Msg = "Failure value returned from cantFail wrapped call"; |
| 806 | #ifndef NDEBUG1 |
| 807 | std::string Str; |
| 808 | raw_string_ostream OS(Str); |
| 809 | auto E = ValOrErr.takeError(); |
| 810 | OS << Msg << "\n" << E; |
| 811 | Msg = OS.str().c_str(); |
| 812 | #endif |
| 813 | llvm_unreachable(Msg)__builtin_unreachable(); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /// Helper for testing applicability of, and applying, handlers for |
| 818 | /// ErrorInfo types. |
| 819 | template <typename HandlerT> |
| 820 | class ErrorHandlerTraits |
| 821 | : public ErrorHandlerTraits<decltype( |
| 822 | &std::remove_reference<HandlerT>::type::operator())> {}; |
| 823 | |
| 824 | // Specialization functions of the form 'Error (const ErrT&)'. |
| 825 | template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { |
| 826 | public: |
| 827 | static bool appliesTo(const ErrorInfoBase &E) { |
| 828 | return E.template isA<ErrT>(); |
| 829 | } |
| 830 | |
| 831 | template <typename HandlerT> |
| 832 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
| 833 | assert(appliesTo(*E) && "Applying incorrect handler")((void)0); |
| 834 | return H(static_cast<ErrT &>(*E)); |
| 835 | } |
| 836 | }; |
| 837 | |
| 838 | // Specialization functions of the form 'void (const ErrT&)'. |
| 839 | template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { |
| 840 | public: |
| 841 | static bool appliesTo(const ErrorInfoBase &E) { |
| 842 | return E.template isA<ErrT>(); |
| 843 | } |
| 844 | |
| 845 | template <typename HandlerT> |
| 846 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
| 847 | assert(appliesTo(*E) && "Applying incorrect handler")((void)0); |
| 848 | H(static_cast<ErrT &>(*E)); |
| 849 | return Error::success(); |
| 850 | } |
| 851 | }; |
| 852 | |
| 853 | /// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. |
| 854 | template <typename ErrT> |
| 855 | class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { |
| 856 | public: |
| 857 | static bool appliesTo(const ErrorInfoBase &E) { |
| 858 | return E.template isA<ErrT>(); |
| 859 | } |
| 860 | |
| 861 | template <typename HandlerT> |
| 862 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
| 863 | assert(appliesTo(*E) && "Applying incorrect handler")((void)0); |
| 864 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
| 865 | return H(std::move(SubE)); |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | /// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. |
| 870 | template <typename ErrT> |
| 871 | class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { |
| 872 | public: |
| 873 | static bool appliesTo(const ErrorInfoBase &E) { |
| 874 | return E.template isA<ErrT>(); |
| 875 | } |
| 876 | |
| 877 | template <typename HandlerT> |
| 878 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
| 879 | assert(appliesTo(*E) && "Applying incorrect handler")((void)0); |
| 880 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
| 881 | H(std::move(SubE)); |
| 882 | return Error::success(); |
| 883 | } |
| 884 | }; |
| 885 | |
| 886 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
| 887 | template <typename C, typename RetT, typename ErrT> |
| 888 | class ErrorHandlerTraits<RetT (C::*)(ErrT &)> |
| 889 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
| 890 | |
| 891 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
| 892 | template <typename C, typename RetT, typename ErrT> |
| 893 | class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> |
| 894 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
| 895 | |
| 896 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
| 897 | template <typename C, typename RetT, typename ErrT> |
| 898 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> |
| 899 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
| 900 | |
| 901 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
| 902 | template <typename C, typename RetT, typename ErrT> |
| 903 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> |
| 904 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
| 905 | |
| 906 | /// Specialization for member functions of the form |
| 907 | /// 'RetT (std::unique_ptr<ErrT>)'. |
| 908 | template <typename C, typename RetT, typename ErrT> |
| 909 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> |
| 910 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
| 911 | |
| 912 | /// Specialization for member functions of the form |
| 913 | /// 'RetT (std::unique_ptr<ErrT>) const'. |
| 914 | template <typename C, typename RetT, typename ErrT> |
| 915 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> |
| 916 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
| 917 | |
| 918 | inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { |
| 919 | return Error(std::move(Payload)); |
| 920 | } |
| 921 | |
| 922 | template <typename HandlerT, typename... HandlerTs> |
| 923 | Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, |
| 924 | HandlerT &&Handler, HandlerTs &&... Handlers) { |
| 925 | if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) |
| 926 | return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), |
| 927 | std::move(Payload)); |
| 928 | return handleErrorImpl(std::move(Payload), |
| 929 | std::forward<HandlerTs>(Handlers)...); |
| 930 | } |
| 931 | |
| 932 | /// Pass the ErrorInfo(s) contained in E to their respective handlers. Any |
| 933 | /// unhandled errors (or Errors returned by handlers) are re-concatenated and |
| 934 | /// returned. |
| 935 | /// Because this function returns an error, its result must also be checked |
| 936 | /// or returned. If you intend to handle all errors use handleAllErrors |
| 937 | /// (which returns void, and will abort() on unhandled errors) instead. |
| 938 | template <typename... HandlerTs> |
| 939 | Error handleErrors(Error E, HandlerTs &&... Hs) { |
| 940 | if (!E) |
| 941 | return Error::success(); |
| 942 | |
| 943 | std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); |
| 944 | |
| 945 | if (Payload->isA<ErrorList>()) { |
| 946 | ErrorList &List = static_cast<ErrorList &>(*Payload); |
| 947 | Error R; |
| 948 | for (auto &P : List.Payloads) |
| 949 | R = ErrorList::join( |
| 950 | std::move(R), |
| 951 | handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); |
| 952 | return R; |
| 953 | } |
| 954 | |
| 955 | return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); |
| 956 | } |
| 957 | |
| 958 | /// Behaves the same as handleErrors, except that by contract all errors |
| 959 | /// *must* be handled by the given handlers (i.e. there must be no remaining |
| 960 | /// errors after running the handlers, or llvm_unreachable is called). |
| 961 | template <typename... HandlerTs> |
| 962 | void handleAllErrors(Error E, HandlerTs &&... Handlers) { |
| 963 | cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); |
| 964 | } |
| 965 | |
| 966 | /// Check that E is a non-error, then drop it. |
| 967 | /// If E is an error, llvm_unreachable will be called. |
| 968 | inline void handleAllErrors(Error E) { |
| 969 | cantFail(std::move(E)); |
| 970 | } |
| 971 | |
| 972 | /// Handle any errors (if present) in an Expected<T>, then try a recovery path. |
| 973 | /// |
| 974 | /// If the incoming value is a success value it is returned unmodified. If it |
| 975 | /// is a failure value then it the contained error is passed to handleErrors. |
| 976 | /// If handleErrors is able to handle the error then the RecoveryPath functor |
| 977 | /// is called to supply the final result. If handleErrors is not able to |
| 978 | /// handle all errors then the unhandled errors are returned. |
| 979 | /// |
| 980 | /// This utility enables the follow pattern: |
| 981 | /// |
| 982 | /// @code{.cpp} |
| 983 | /// enum FooStrategy { Aggressive, Conservative }; |
| 984 | /// Expected<Foo> foo(FooStrategy S); |
| 985 | /// |
| 986 | /// auto ResultOrErr = |
| 987 | /// handleExpected( |
| 988 | /// foo(Aggressive), |
| 989 | /// []() { return foo(Conservative); }, |
| 990 | /// [](AggressiveStrategyError&) { |
| 991 | /// // Implicitly conusme this - we'll recover by using a conservative |
| 992 | /// // strategy. |
| 993 | /// }); |
| 994 | /// |
| 995 | /// @endcode |
| 996 | template <typename T, typename RecoveryFtor, typename... HandlerTs> |
| 997 | Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, |
| 998 | HandlerTs &&... Handlers) { |
| 999 | if (ValOrErr) |
| 1000 | return ValOrErr; |
| 1001 | |
| 1002 | if (auto Err = handleErrors(ValOrErr.takeError(), |
| 1003 | std::forward<HandlerTs>(Handlers)...)) |
| 1004 | return std::move(Err); |
| 1005 | |
| 1006 | return RecoveryPath(); |
| 1007 | } |
| 1008 | |
| 1009 | /// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner |
| 1010 | /// will be printed before the first one is logged. A newline will be printed |
| 1011 | /// after each error. |
| 1012 | /// |
| 1013 | /// This function is compatible with the helpers from Support/WithColor.h. You |
| 1014 | /// can pass any of them as the OS. Please consider using them instead of |
| 1015 | /// including 'error: ' in the ErrorBanner. |
| 1016 | /// |
| 1017 | /// This is useful in the base level of your program to allow clean termination |
| 1018 | /// (allowing clean deallocation of resources, etc.), while reporting error |
| 1019 | /// information to the user. |
| 1020 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); |
| 1021 | |
| 1022 | /// Write all error messages (if any) in E to a string. The newline character |
| 1023 | /// is used to separate error messages. |
| 1024 | inline std::string toString(Error E) { |
| 1025 | SmallVector<std::string, 2> Errors; |
| 1026 | handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { |
| 1027 | Errors.push_back(EI.message()); |
| 1028 | }); |
| 1029 | return join(Errors.begin(), Errors.end(), "\n"); |
| 1030 | } |
| 1031 | |
| 1032 | /// Consume a Error without doing anything. This method should be used |
| 1033 | /// only where an error can be considered a reasonable and expected return |
| 1034 | /// value. |
| 1035 | /// |
| 1036 | /// Uses of this method are potentially indicative of design problems: If it's |
| 1037 | /// legitimate to do nothing while processing an "error", the error-producer |
| 1038 | /// might be more clearly refactored to return an Optional<T>. |
| 1039 | inline void consumeError(Error Err) { |
| 1040 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); |
| 1041 | } |
| 1042 | |
| 1043 | /// Convert an Expected to an Optional without doing anything. This method |
| 1044 | /// should be used only where an error can be considered a reasonable and |
| 1045 | /// expected return value. |
| 1046 | /// |
| 1047 | /// Uses of this method are potentially indicative of problems: perhaps the |
| 1048 | /// error should be propagated further, or the error-producer should just |
| 1049 | /// return an Optional in the first place. |
| 1050 | template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) { |
| 1051 | if (E) |
| 1052 | return std::move(*E); |
| 1053 | consumeError(E.takeError()); |
| 1054 | return None; |
| 1055 | } |
| 1056 | |
| 1057 | /// Helper for converting an Error to a bool. |
| 1058 | /// |
| 1059 | /// This method returns true if Err is in an error state, or false if it is |
| 1060 | /// in a success state. Puts Err in a checked state in both cases (unlike |
| 1061 | /// Error::operator bool(), which only does this for success states). |
| 1062 | inline bool errorToBool(Error Err) { |
| 1063 | bool IsError = static_cast<bool>(Err); |
| 1064 | if (IsError) |
| 1065 | consumeError(std::move(Err)); |
| 1066 | return IsError; |
| 1067 | } |
| 1068 | |
| 1069 | /// Helper for Errors used as out-parameters. |
| 1070 | /// |
| 1071 | /// This helper is for use with the Error-as-out-parameter idiom, where an error |
| 1072 | /// is passed to a function or method by reference, rather than being returned. |
| 1073 | /// In such cases it is helpful to set the checked bit on entry to the function |
| 1074 | /// so that the error can be written to (unchecked Errors abort on assignment) |
| 1075 | /// and clear the checked bit on exit so that clients cannot accidentally forget |
| 1076 | /// to check the result. This helper performs these actions automatically using |
| 1077 | /// RAII: |
| 1078 | /// |
| 1079 | /// @code{.cpp} |
| 1080 | /// Result foo(Error &Err) { |
| 1081 | /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set |
| 1082 | /// // <body of foo> |
| 1083 | /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. |
| 1084 | /// } |
| 1085 | /// @endcode |
| 1086 | /// |
| 1087 | /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be |
| 1088 | /// used with optional Errors (Error pointers that are allowed to be null). If |
| 1089 | /// ErrorAsOutParameter took an Error reference, an instance would have to be |
| 1090 | /// created inside every condition that verified that Error was non-null. By |
| 1091 | /// taking an Error pointer we can just create one instance at the top of the |
| 1092 | /// function. |
| 1093 | class ErrorAsOutParameter { |
| 1094 | public: |
| 1095 | ErrorAsOutParameter(Error *Err) : Err(Err) { |
| 1096 | // Raise the checked bit if Err is success. |
| 1097 | if (Err) |
| 1098 | (void)!!*Err; |
| 1099 | } |
| 1100 | |
| 1101 | ~ErrorAsOutParameter() { |
| 1102 | // Clear the checked bit. |
| 1103 | if (Err && !*Err) |
| 1104 | *Err = Error::success(); |
| 1105 | } |
| 1106 | |
| 1107 | private: |
| 1108 | Error *Err; |
| 1109 | }; |
| 1110 | |
| 1111 | /// Helper for Expected<T>s used as out-parameters. |
| 1112 | /// |
| 1113 | /// See ErrorAsOutParameter. |
| 1114 | template <typename T> |
| 1115 | class ExpectedAsOutParameter { |
| 1116 | public: |
| 1117 | ExpectedAsOutParameter(Expected<T> *ValOrErr) |
| 1118 | : ValOrErr(ValOrErr) { |
| 1119 | if (ValOrErr) |
| 1120 | (void)!!*ValOrErr; |
| 1121 | } |
| 1122 | |
| 1123 | ~ExpectedAsOutParameter() { |
| 1124 | if (ValOrErr) |
| 1125 | ValOrErr->setUnchecked(); |
| 1126 | } |
| 1127 | |
| 1128 | private: |
| 1129 | Expected<T> *ValOrErr; |
| 1130 | }; |
| 1131 | |
| 1132 | /// This class wraps a std::error_code in a Error. |
| 1133 | /// |
| 1134 | /// This is useful if you're writing an interface that returns a Error |
| 1135 | /// (or Expected) and you want to call code that still returns |
| 1136 | /// std::error_codes. |
| 1137 | class ECError : public ErrorInfo<ECError> { |
| 1138 | friend Error errorCodeToError(std::error_code); |
| 1139 | |
| 1140 | virtual void anchor() override; |
| 1141 | |
| 1142 | public: |
| 1143 | void setErrorCode(std::error_code EC) { this->EC = EC; } |
| 1144 | std::error_code convertToErrorCode() const override { return EC; } |
| 1145 | void log(raw_ostream &OS) const override { OS << EC.message(); } |
| 1146 | |
| 1147 | // Used by ErrorInfo::classID. |
| 1148 | static char ID; |
| 1149 | |
| 1150 | protected: |
| 1151 | ECError() = default; |
| 1152 | ECError(std::error_code EC) : EC(EC) {} |
| 1153 | |
| 1154 | std::error_code EC; |
| 1155 | }; |
| 1156 | |
| 1157 | /// The value returned by this function can be returned from convertToErrorCode |
| 1158 | /// for Error values where no sensible translation to std::error_code exists. |
| 1159 | /// It should only be used in this situation, and should never be used where a |
| 1160 | /// sensible conversion to std::error_code is available, as attempts to convert |
| 1161 | /// to/from this error will result in a fatal error. (i.e. it is a programmatic |
| 1162 | ///error to try to convert such a value). |
| 1163 | std::error_code inconvertibleErrorCode(); |
| 1164 | |
| 1165 | /// Helper for converting an std::error_code to a Error. |
| 1166 | Error errorCodeToError(std::error_code EC); |
| 1167 | |
| 1168 | /// Helper for converting an ECError to a std::error_code. |
| 1169 | /// |
| 1170 | /// This method requires that Err be Error() or an ECError, otherwise it |
| 1171 | /// will trigger a call to abort(). |
| 1172 | std::error_code errorToErrorCode(Error Err); |
| 1173 | |
| 1174 | /// Convert an ErrorOr<T> to an Expected<T>. |
| 1175 | template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { |
| 1176 | if (auto EC = EO.getError()) |
| 1177 | return errorCodeToError(EC); |
| 1178 | return std::move(*EO); |
| 1179 | } |
| 1180 | |
| 1181 | /// Convert an Expected<T> to an ErrorOr<T>. |
| 1182 | template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { |
| 1183 | if (auto Err = E.takeError()) |
| 1184 | return errorToErrorCode(std::move(Err)); |
| 1185 | return std::move(*E); |
| 1186 | } |
| 1187 | |
| 1188 | /// This class wraps a string in an Error. |
| 1189 | /// |
| 1190 | /// StringError is useful in cases where the client is not expected to be able |
| 1191 | /// to consume the specific error message programmatically (for example, if the |
| 1192 | /// error message is to be presented to the user). |
| 1193 | /// |
| 1194 | /// StringError can also be used when additional information is to be printed |
| 1195 | /// along with a error_code message. Depending on the constructor called, this |
| 1196 | /// class can either display: |
| 1197 | /// 1. the error_code message (ECError behavior) |
| 1198 | /// 2. a string |
| 1199 | /// 3. the error_code message and a string |
| 1200 | /// |
| 1201 | /// These behaviors are useful when subtyping is required; for example, when a |
| 1202 | /// specific library needs an explicit error type. In the example below, |
| 1203 | /// PDBError is derived from StringError: |
| 1204 | /// |
| 1205 | /// @code{.cpp} |
| 1206 | /// Expected<int> foo() { |
| 1207 | /// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, |
| 1208 | /// "Additional information"); |
| 1209 | /// } |
| 1210 | /// @endcode |
| 1211 | /// |
| 1212 | class StringError : public ErrorInfo<StringError> { |
| 1213 | public: |
| 1214 | static char ID; |
| 1215 | |
| 1216 | // Prints EC + S and converts to EC |
| 1217 | StringError(std::error_code EC, const Twine &S = Twine()); |
| 1218 | |
| 1219 | // Prints S and converts to EC |
| 1220 | StringError(const Twine &S, std::error_code EC); |
| 1221 | |
| 1222 | void log(raw_ostream &OS) const override; |
| 1223 | std::error_code convertToErrorCode() const override; |
| 1224 | |
| 1225 | const std::string &getMessage() const { return Msg; } |
| 1226 | |
| 1227 | private: |
| 1228 | std::string Msg; |
| 1229 | std::error_code EC; |
| 1230 | const bool PrintMsgOnly = false; |
| 1231 | }; |
| 1232 | |
| 1233 | /// Create formatted StringError object. |
| 1234 | template <typename... Ts> |
| 1235 | inline Error createStringError(std::error_code EC, char const *Fmt, |
| 1236 | const Ts &... Vals) { |
| 1237 | std::string Buffer; |
| 1238 | raw_string_ostream Stream(Buffer); |
| 1239 | Stream << format(Fmt, Vals...); |
| 1240 | return make_error<StringError>(Stream.str(), EC); |
| 1241 | } |
| 1242 | |
| 1243 | Error createStringError(std::error_code EC, char const *Msg); |
| 1244 | |
| 1245 | inline Error createStringError(std::error_code EC, const Twine &S) { |
| 1246 | return createStringError(EC, S.str().c_str()); |
| 1247 | } |
| 1248 | |
| 1249 | template <typename... Ts> |
| 1250 | inline Error createStringError(std::errc EC, char const *Fmt, |
| 1251 | const Ts &... Vals) { |
| 1252 | return createStringError(std::make_error_code(EC), Fmt, Vals...); |
| 1253 | } |
| 1254 | |
| 1255 | /// This class wraps a filename and another Error. |
| 1256 | /// |
| 1257 | /// In some cases, an error needs to live along a 'source' name, in order to |
| 1258 | /// show more detailed information to the user. |
| 1259 | class FileError final : public ErrorInfo<FileError> { |
| 1260 | |
| 1261 | friend Error createFileError(const Twine &, Error); |
| 1262 | friend Error createFileError(const Twine &, size_t, Error); |
| 1263 | |
| 1264 | public: |
| 1265 | void log(raw_ostream &OS) const override { |
| 1266 | assert(Err && !FileName.empty() && "Trying to log after takeError().")((void)0); |
| 1267 | OS << "'" << FileName << "': "; |
| 1268 | if (Line.hasValue()) |
| 1269 | OS << "line " << Line.getValue() << ": "; |
| 1270 | Err->log(OS); |
| 1271 | } |
| 1272 | |
| 1273 | StringRef getFileName() { return FileName; } |
| 1274 | |
| 1275 | Error takeError() { return Error(std::move(Err)); } |
| 1276 | |
| 1277 | std::error_code convertToErrorCode() const override; |
| 1278 | |
| 1279 | // Used by ErrorInfo::classID. |
| 1280 | static char ID; |
| 1281 | |
| 1282 | private: |
| 1283 | FileError(const Twine &F, Optional<size_t> LineNum, |
| 1284 | std::unique_ptr<ErrorInfoBase> E) { |
| 1285 | assert(E && "Cannot create FileError from Error success value.")((void)0); |
| 1286 | assert(!F.isTriviallyEmpty() &&((void)0) |
| 1287 | "The file name provided to FileError must not be empty.")((void)0); |
| 1288 | FileName = F.str(); |
| 1289 | Err = std::move(E); |
| 1290 | Line = std::move(LineNum); |
| 1291 | } |
| 1292 | |
| 1293 | static Error build(const Twine &F, Optional<size_t> Line, Error E) { |
| 1294 | std::unique_ptr<ErrorInfoBase> Payload; |
| 1295 | handleAllErrors(std::move(E), |
| 1296 | [&](std::unique_ptr<ErrorInfoBase> EIB) -> Error { |
| 1297 | Payload = std::move(EIB); |
| 1298 | return Error::success(); |
| 1299 | }); |
| 1300 | return Error( |
| 1301 | std::unique_ptr<FileError>(new FileError(F, Line, std::move(Payload)))); |
| 1302 | } |
| 1303 | |
| 1304 | std::string FileName; |
| 1305 | Optional<size_t> Line; |
| 1306 | std::unique_ptr<ErrorInfoBase> Err; |
| 1307 | }; |
| 1308 | |
| 1309 | /// Concatenate a source file path and/or name with an Error. The resulting |
| 1310 | /// Error is unchecked. |
| 1311 | inline Error createFileError(const Twine &F, Error E) { |
| 1312 | return FileError::build(F, Optional<size_t>(), std::move(E)); |
| 1313 | } |
| 1314 | |
| 1315 | /// Concatenate a source file path and/or name with line number and an Error. |
| 1316 | /// The resulting Error is unchecked. |
| 1317 | inline Error createFileError(const Twine &F, size_t Line, Error E) { |
| 1318 | return FileError::build(F, Optional<size_t>(Line), std::move(E)); |
| 1319 | } |
| 1320 | |
| 1321 | /// Concatenate a source file path and/or name with a std::error_code |
| 1322 | /// to form an Error object. |
| 1323 | inline Error createFileError(const Twine &F, std::error_code EC) { |
| 1324 | return createFileError(F, errorCodeToError(EC)); |
| 1325 | } |
| 1326 | |
| 1327 | /// Concatenate a source file path and/or name with line number and |
| 1328 | /// std::error_code to form an Error object. |
| 1329 | inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { |
| 1330 | return createFileError(F, Line, errorCodeToError(EC)); |
| 1331 | } |
| 1332 | |
| 1333 | Error createFileError(const Twine &F, ErrorSuccess) = delete; |
| 1334 | |
| 1335 | /// Helper for check-and-exit error handling. |
| 1336 | /// |
| 1337 | /// For tool use only. NOT FOR USE IN LIBRARY CODE. |
| 1338 | /// |
| 1339 | class ExitOnError { |
| 1340 | public: |
| 1341 | /// Create an error on exit helper. |
| 1342 | ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) |
| 1343 | : Banner(std::move(Banner)), |
| 1344 | GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} |
| 1345 | |
| 1346 | /// Set the banner string for any errors caught by operator(). |
| 1347 | void setBanner(std::string Banner) { this->Banner = std::move(Banner); } |
| 1348 | |
| 1349 | /// Set the exit-code mapper function. |
| 1350 | void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { |
| 1351 | this->GetExitCode = std::move(GetExitCode); |
| 1352 | } |
| 1353 | |
| 1354 | /// Check Err. If it's in a failure state log the error(s) and exit. |
| 1355 | void operator()(Error Err) const { checkError(std::move(Err)); } |
| 1356 | |
| 1357 | /// Check E. If it's in a success state then return the contained value. If |
| 1358 | /// it's in a failure state log the error(s) and exit. |
| 1359 | template <typename T> T operator()(Expected<T> &&E) const { |
| 1360 | checkError(E.takeError()); |
| 1361 | return std::move(*E); |
| 1362 | } |
| 1363 | |
| 1364 | /// Check E. If it's in a success state then return the contained reference. If |
| 1365 | /// it's in a failure state log the error(s) and exit. |
| 1366 | template <typename T> T& operator()(Expected<T&> &&E) const { |
| 1367 | checkError(E.takeError()); |
| 1368 | return *E; |
| 1369 | } |
| 1370 | |
| 1371 | private: |
| 1372 | void checkError(Error Err) const { |
| 1373 | if (Err) { |
| 1374 | int ExitCode = GetExitCode(Err); |
| 1375 | logAllUnhandledErrors(std::move(Err), errs(), Banner); |
| 1376 | exit(ExitCode); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | std::string Banner; |
| 1381 | std::function<int(const Error &)> GetExitCode; |
| 1382 | }; |
| 1383 | |
| 1384 | /// Conversion from Error to LLVMErrorRef for C error bindings. |
| 1385 | inline LLVMErrorRef wrap(Error Err) { |
| 1386 | return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); |
| 1387 | } |
| 1388 | |
| 1389 | /// Conversion from LLVMErrorRef to Error for C error bindings. |
| 1390 | inline Error unwrap(LLVMErrorRef ErrRef) { |
| 1391 | return Error(std::unique_ptr<ErrorInfoBase>( |
| 1392 | reinterpret_cast<ErrorInfoBase *>(ErrRef))); |
| 1393 | } |
| 1394 | |
| 1395 | } // end namespace llvm |
| 1396 | |
| 1397 | #endif // LLVM_SUPPORT_ERROR_H |