Error.h revision 355940
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 42namespace llvm { 43 44class ErrorSuccess; 45 46/// Base class for error info classes. Do not extend this directly: Extend 47/// the ErrorInfo template subclass instead. 48class ErrorInfoBase { 49public: 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 86private: 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. 157class LLVM_NODISCARD Error { 158 // Both ErrorList and FileError need to be able to yank ErrorInfoBase 159 // pointers out of this class to add to the error list. 160 friend class ErrorList; 161 friend class FileError; 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 174protected: 175 /// Create a success value. Prefer using 'Error::success()' for readability 176 Error() { 177 setPtr(nullptr); 178 setChecked(false); 179 } 180 181public: 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 253private: 254#if LLVM_ENABLE_ABI_BREAKING_CHECKS 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 261 void fatalUncheckedError() const; 262#endif 263 264 void assertIsChecked() { 265#if LLVM_ENABLE_ABI_BREAKING_CHECKS 266 if (LLVM_UNLIKELY(!getChecked() || getPtr())) 267 fatalUncheckedError(); 268#endif 269 } 270 271 ErrorInfoBase *getPtr() const { 272 return reinterpret_cast<ErrorInfoBase*>( 273 reinterpret_cast<uintptr_t>(Payload) & 274 ~static_cast<uintptr_t>(0x1)); 275 } 276 277 void setPtr(ErrorInfoBase *EI) { 278#if LLVM_ENABLE_ABI_BREAKING_CHECKS 279 Payload = reinterpret_cast<ErrorInfoBase*>( 280 (reinterpret_cast<uintptr_t>(EI) & 281 ~static_cast<uintptr_t>(0x1)) | 282 (reinterpret_cast<uintptr_t>(Payload) & 0x1)); 283#else 284 Payload = EI; 285#endif 286 } 287 288 bool getChecked() const { 289#if LLVM_ENABLE_ABI_BREAKING_CHECKS 290 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; 291#else 292 return true; 293#endif 294 } 295 296 void setChecked(bool V) { 297 Payload = reinterpret_cast<ErrorInfoBase*>( 298 (reinterpret_cast<uintptr_t>(Payload) & 299 ~static_cast<uintptr_t>(0x1)) | 300 (V ? 0 : 1)); 301 } 302 303 std::unique_ptr<ErrorInfoBase> takePayload() { 304 std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); 305 setPtr(nullptr); 306 setChecked(true); 307 return Tmp; 308 } 309 310 friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { 311 if (auto P = E.getPtr()) 312 P->log(OS); 313 else 314 OS << "success"; 315 return OS; 316 } 317 318 ErrorInfoBase *Payload = nullptr; 319}; 320 321/// Subclass of Error for the sole purpose of identifying the success path in 322/// the type system. This allows to catch invalid conversion to Expected<T> at 323/// compile time. 324class ErrorSuccess final : public Error {}; 325 326inline ErrorSuccess Error::success() { return ErrorSuccess(); } 327 328/// Make a Error instance representing failure using the given error info 329/// type. 330template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { 331 return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); 332} 333 334/// Base class for user error types. Users should declare their error types 335/// like: 336/// 337/// class MyError : public ErrorInfo<MyError> { 338/// .... 339/// }; 340/// 341/// This class provides an implementation of the ErrorInfoBase::kind 342/// method, which is used by the Error RTTI system. 343template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> 344class ErrorInfo : public ParentErrT { 345public: 346 using ParentErrT::ParentErrT; // inherit constructors 347 348 static const void *classID() { return &ThisErrT::ID; } 349 350 const void *dynamicClassID() const override { return &ThisErrT::ID; } 351 352 bool isA(const void *const ClassID) const override { 353 return ClassID == classID() || ParentErrT::isA(ClassID); 354 } 355}; 356 357/// Special ErrorInfo subclass representing a list of ErrorInfos. 358/// Instances of this class are constructed by joinError. 359class ErrorList final : public ErrorInfo<ErrorList> { 360 // handleErrors needs to be able to iterate the payload list of an 361 // ErrorList. 362 template <typename... HandlerTs> 363 friend Error handleErrors(Error E, HandlerTs &&... Handlers); 364 365 // joinErrors is implemented in terms of join. 366 friend Error joinErrors(Error, Error); 367 368public: 369 void log(raw_ostream &OS) const override { 370 OS << "Multiple errors:\n"; 371 for (auto &ErrPayload : Payloads) { 372 ErrPayload->log(OS); 373 OS << "\n"; 374 } 375 } 376 377 std::error_code convertToErrorCode() const override; 378 379 // Used by ErrorInfo::classID. 380 static char ID; 381 382private: 383 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, 384 std::unique_ptr<ErrorInfoBase> Payload2) { 385 assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && 386 "ErrorList constructor payloads should be singleton errors"); 387 Payloads.push_back(std::move(Payload1)); 388 Payloads.push_back(std::move(Payload2)); 389 } 390 391 static Error join(Error E1, Error E2) { 392 if (!E1) 393 return E2; 394 if (!E2) 395 return E1; 396 if (E1.isA<ErrorList>()) { 397 auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); 398 if (E2.isA<ErrorList>()) { 399 auto E2Payload = E2.takePayload(); 400 auto &E2List = static_cast<ErrorList &>(*E2Payload); 401 for (auto &Payload : E2List.Payloads) 402 E1List.Payloads.push_back(std::move(Payload)); 403 } else 404 E1List.Payloads.push_back(E2.takePayload()); 405 406 return E1; 407 } 408 if (E2.isA<ErrorList>()) { 409 auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); 410 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); 411 return E2; 412 } 413 return Error(std::unique_ptr<ErrorList>( 414 new ErrorList(E1.takePayload(), E2.takePayload()))); 415 } 416 417 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; 418}; 419 420/// Concatenate errors. The resulting Error is unchecked, and contains the 421/// ErrorInfo(s), if any, contained in E1, followed by the 422/// ErrorInfo(s), if any, contained in E2. 423inline Error joinErrors(Error E1, Error E2) { 424 return ErrorList::join(std::move(E1), std::move(E2)); 425} 426 427/// Tagged union holding either a T or a Error. 428/// 429/// This class parallels ErrorOr, but replaces error_code with Error. Since 430/// Error cannot be copied, this class replaces getError() with 431/// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the 432/// error class type. 433template <class T> class LLVM_NODISCARD Expected { 434 template <class T1> friend class ExpectedAsOutParameter; 435 template <class OtherT> friend class Expected; 436 437 static const bool isRef = std::is_reference<T>::value; 438 439 using wrap = std::reference_wrapper<typename std::remove_reference<T>::type>; 440 441 using error_type = std::unique_ptr<ErrorInfoBase>; 442 443public: 444 using storage_type = typename std::conditional<isRef, wrap, T>::type; 445 using value_type = T; 446 447private: 448 using reference = typename std::remove_reference<T>::type &; 449 using const_reference = const typename std::remove_reference<T>::type &; 450 using pointer = typename std::remove_reference<T>::type *; 451 using const_pointer = const typename std::remove_reference<T>::type *; 452 453public: 454 /// Create an Expected<T> error value from the given Error. 455 Expected(Error Err) 456 : HasError(true) 457#if LLVM_ENABLE_ABI_BREAKING_CHECKS 458 // Expected is unchecked upon construction in Debug builds. 459 , Unchecked(true) 460#endif 461 { 462 assert(Err && "Cannot create Expected<T> from Error success value."); 463 new (getErrorStorage()) error_type(Err.takePayload()); 464 } 465 466 /// Forbid to convert from Error::success() implicitly, this avoids having 467 /// Expected<T> foo() { return Error::success(); } which compiles otherwise 468 /// but triggers the assertion above. 469 Expected(ErrorSuccess) = delete; 470 471 /// Create an Expected<T> success value from the given OtherT value, which 472 /// must be convertible to T. 473 template <typename OtherT> 474 Expected(OtherT &&Val, 475 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type 476 * = nullptr) 477 : HasError(false) 478#if LLVM_ENABLE_ABI_BREAKING_CHECKS 479 // Expected is unchecked upon construction in Debug builds. 480 , Unchecked(true) 481#endif 482 { 483 new (getStorage()) storage_type(std::forward<OtherT>(Val)); 484 } 485 486 /// Move construct an Expected<T> value. 487 Expected(Expected &&Other) { moveConstruct(std::move(Other)); } 488 489 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT 490 /// must be convertible to T. 491 template <class OtherT> 492 Expected(Expected<OtherT> &&Other, 493 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type 494 * = nullptr) { 495 moveConstruct(std::move(Other)); 496 } 497 498 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT 499 /// isn't convertible to T. 500 template <class OtherT> 501 explicit Expected( 502 Expected<OtherT> &&Other, 503 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * = 504 nullptr) { 505 moveConstruct(std::move(Other)); 506 } 507 508 /// Move-assign from another Expected<T>. 509 Expected &operator=(Expected &&Other) { 510 moveAssign(std::move(Other)); 511 return *this; 512 } 513 514 /// Destroy an Expected<T>. 515 ~Expected() { 516 assertIsChecked(); 517 if (!HasError) 518 getStorage()->~storage_type(); 519 else 520 getErrorStorage()->~error_type(); 521 } 522 523 /// Return false if there is an error. 524 explicit operator bool() { 525#if LLVM_ENABLE_ABI_BREAKING_CHECKS 526 Unchecked = HasError; 527#endif 528 return !HasError; 529 } 530 531 /// Returns a reference to the stored T value. 532 reference get() { 533 assertIsChecked(); 534 return *getStorage(); 535 } 536 537 /// Returns a const reference to the stored T value. 538 const_reference get() const { 539 assertIsChecked(); 540 return const_cast<Expected<T> *>(this)->get(); 541 } 542 543 /// Check that this Expected<T> is an error of type ErrT. 544 template <typename ErrT> bool errorIsA() const { 545 return HasError && (*getErrorStorage())->template isA<ErrT>(); 546 } 547 548 /// Take ownership of the stored error. 549 /// After calling this the Expected<T> is in an indeterminate state that can 550 /// only be safely destructed. No further calls (beside the destructor) should 551 /// be made on the Expected<T> vaule. 552 Error takeError() { 553#if LLVM_ENABLE_ABI_BREAKING_CHECKS 554 Unchecked = false; 555#endif 556 return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); 557 } 558 559 /// Returns a pointer to the stored T value. 560 pointer operator->() { 561 assertIsChecked(); 562 return toPointer(getStorage()); 563 } 564 565 /// Returns a const pointer to the stored T value. 566 const_pointer operator->() const { 567 assertIsChecked(); 568 return toPointer(getStorage()); 569 } 570 571 /// Returns a reference to the stored T value. 572 reference operator*() { 573 assertIsChecked(); 574 return *getStorage(); 575 } 576 577 /// Returns a const reference to the stored T value. 578 const_reference operator*() const { 579 assertIsChecked(); 580 return *getStorage(); 581 } 582 583private: 584 template <class T1> 585 static bool compareThisIfSameType(const T1 &a, const T1 &b) { 586 return &a == &b; 587 } 588 589 template <class T1, class T2> 590 static bool compareThisIfSameType(const T1 &a, const T2 &b) { 591 return false; 592 } 593 594 template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { 595 HasError = Other.HasError; 596#if LLVM_ENABLE_ABI_BREAKING_CHECKS 597 Unchecked = true; 598 Other.Unchecked = false; 599#endif 600 601 if (!HasError) 602 new (getStorage()) storage_type(std::move(*Other.getStorage())); 603 else 604 new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); 605 } 606 607 template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { 608 assertIsChecked(); 609 610 if (compareThisIfSameType(*this, Other)) 611 return; 612 613 this->~Expected(); 614 new (this) Expected(std::move(Other)); 615 } 616 617 pointer toPointer(pointer Val) { return Val; } 618 619 const_pointer toPointer(const_pointer Val) const { return Val; } 620 621 pointer toPointer(wrap *Val) { return &Val->get(); } 622 623 const_pointer toPointer(const wrap *Val) const { return &Val->get(); } 624 625 storage_type *getStorage() { 626 assert(!HasError && "Cannot get value when an error exists!"); 627 return reinterpret_cast<storage_type *>(TStorage.buffer); 628 } 629 630 const storage_type *getStorage() const { 631 assert(!HasError && "Cannot get value when an error exists!"); 632 return reinterpret_cast<const storage_type *>(TStorage.buffer); 633 } 634 635 error_type *getErrorStorage() { 636 assert(HasError && "Cannot get error when a value exists!"); 637 return reinterpret_cast<error_type *>(ErrorStorage.buffer); 638 } 639 640 const error_type *getErrorStorage() const { 641 assert(HasError && "Cannot get error when a value exists!"); 642 return reinterpret_cast<const error_type *>(ErrorStorage.buffer); 643 } 644 645 // Used by ExpectedAsOutParameter to reset the checked flag. 646 void setUnchecked() { 647#if LLVM_ENABLE_ABI_BREAKING_CHECKS 648 Unchecked = true; 649#endif 650 } 651 652#if LLVM_ENABLE_ABI_BREAKING_CHECKS 653 LLVM_ATTRIBUTE_NORETURN 654 LLVM_ATTRIBUTE_NOINLINE 655 void fatalUncheckedExpected() const { 656 dbgs() << "Expected<T> must be checked before access or destruction.\n"; 657 if (HasError) { 658 dbgs() << "Unchecked Expected<T> contained error:\n"; 659 (*getErrorStorage())->log(dbgs()); 660 } else 661 dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " 662 "values in success mode must still be checked prior to being " 663 "destroyed).\n"; 664 abort(); 665 } 666#endif 667 668 void assertIsChecked() { 669#if LLVM_ENABLE_ABI_BREAKING_CHECKS 670 if (LLVM_UNLIKELY(Unchecked)) 671 fatalUncheckedExpected(); 672#endif 673 } 674 675 union { 676 AlignedCharArrayUnion<storage_type> TStorage; 677 AlignedCharArrayUnion<error_type> ErrorStorage; 678 }; 679 bool HasError : 1; 680#if LLVM_ENABLE_ABI_BREAKING_CHECKS 681 bool Unchecked : 1; 682#endif 683}; 684 685/// Report a serious error, calling any installed error handler. See 686/// ErrorHandling.h. 687LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, 688 bool gen_crash_diag = true); 689 690/// Report a fatal error if Err is a failure value. 691/// 692/// This function can be used to wrap calls to fallible functions ONLY when it 693/// is known that the Error will always be a success value. E.g. 694/// 695/// @code{.cpp} 696/// // foo only attempts the fallible operation if DoFallibleOperation is 697/// // true. If DoFallibleOperation is false then foo always returns 698/// // Error::success(). 699/// Error foo(bool DoFallibleOperation); 700/// 701/// cantFail(foo(false)); 702/// @endcode 703inline void cantFail(Error Err, const char *Msg = nullptr) { 704 if (Err) { 705 if (!Msg) 706 Msg = "Failure value returned from cantFail wrapped call"; 707 llvm_unreachable(Msg); 708 } 709} 710 711/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and 712/// returns the contained value. 713/// 714/// This function can be used to wrap calls to fallible functions ONLY when it 715/// is known that the Error will always be a success value. E.g. 716/// 717/// @code{.cpp} 718/// // foo only attempts the fallible operation if DoFallibleOperation is 719/// // true. If DoFallibleOperation is false then foo always returns an int. 720/// Expected<int> foo(bool DoFallibleOperation); 721/// 722/// int X = cantFail(foo(false)); 723/// @endcode 724template <typename T> 725T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { 726 if (ValOrErr) 727 return std::move(*ValOrErr); 728 else { 729 if (!Msg) 730 Msg = "Failure value returned from cantFail wrapped call"; 731 llvm_unreachable(Msg); 732 } 733} 734 735/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and 736/// returns the contained reference. 737/// 738/// This function can be used to wrap calls to fallible functions ONLY when it 739/// is known that the Error will always be a success value. E.g. 740/// 741/// @code{.cpp} 742/// // foo only attempts the fallible operation if DoFallibleOperation is 743/// // true. If DoFallibleOperation is false then foo always returns a Bar&. 744/// Expected<Bar&> foo(bool DoFallibleOperation); 745/// 746/// Bar &X = cantFail(foo(false)); 747/// @endcode 748template <typename T> 749T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { 750 if (ValOrErr) 751 return *ValOrErr; 752 else { 753 if (!Msg) 754 Msg = "Failure value returned from cantFail wrapped call"; 755 llvm_unreachable(Msg); 756 } 757} 758 759/// Helper for testing applicability of, and applying, handlers for 760/// ErrorInfo types. 761template <typename HandlerT> 762class ErrorHandlerTraits 763 : public ErrorHandlerTraits<decltype( 764 &std::remove_reference<HandlerT>::type::operator())> {}; 765 766// Specialization functions of the form 'Error (const ErrT&)'. 767template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { 768public: 769 static bool appliesTo(const ErrorInfoBase &E) { 770 return E.template isA<ErrT>(); 771 } 772 773 template <typename HandlerT> 774 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { 775 assert(appliesTo(*E) && "Applying incorrect handler"); 776 return H(static_cast<ErrT &>(*E)); 777 } 778}; 779 780// Specialization functions of the form 'void (const ErrT&)'. 781template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { 782public: 783 static bool appliesTo(const ErrorInfoBase &E) { 784 return E.template isA<ErrT>(); 785 } 786 787 template <typename HandlerT> 788 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { 789 assert(appliesTo(*E) && "Applying incorrect handler"); 790 H(static_cast<ErrT &>(*E)); 791 return Error::success(); 792 } 793}; 794 795/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. 796template <typename ErrT> 797class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { 798public: 799 static bool appliesTo(const ErrorInfoBase &E) { 800 return E.template isA<ErrT>(); 801 } 802 803 template <typename HandlerT> 804 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { 805 assert(appliesTo(*E) && "Applying incorrect handler"); 806 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); 807 return H(std::move(SubE)); 808 } 809}; 810 811/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. 812template <typename ErrT> 813class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { 814public: 815 static bool appliesTo(const ErrorInfoBase &E) { 816 return E.template isA<ErrT>(); 817 } 818 819 template <typename HandlerT> 820 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { 821 assert(appliesTo(*E) && "Applying incorrect handler"); 822 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); 823 H(std::move(SubE)); 824 return Error::success(); 825 } 826}; 827 828// Specialization for member functions of the form 'RetT (const ErrT&)'. 829template <typename C, typename RetT, typename ErrT> 830class ErrorHandlerTraits<RetT (C::*)(ErrT &)> 831 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; 832 833// Specialization for member functions of the form 'RetT (const ErrT&) const'. 834template <typename C, typename RetT, typename ErrT> 835class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> 836 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; 837 838// Specialization for member functions of the form 'RetT (const ErrT&)'. 839template <typename C, typename RetT, typename ErrT> 840class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> 841 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; 842 843// Specialization for member functions of the form 'RetT (const ErrT&) const'. 844template <typename C, typename RetT, typename ErrT> 845class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> 846 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; 847 848/// Specialization for member functions of the form 849/// 'RetT (std::unique_ptr<ErrT>)'. 850template <typename C, typename RetT, typename ErrT> 851class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> 852 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; 853 854/// Specialization for member functions of the form 855/// 'RetT (std::unique_ptr<ErrT>) const'. 856template <typename C, typename RetT, typename ErrT> 857class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> 858 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; 859 860inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { 861 return Error(std::move(Payload)); 862} 863 864template <typename HandlerT, typename... HandlerTs> 865Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, 866 HandlerT &&Handler, HandlerTs &&... Handlers) { 867 if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) 868 return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), 869 std::move(Payload)); 870 return handleErrorImpl(std::move(Payload), 871 std::forward<HandlerTs>(Handlers)...); 872} 873 874/// Pass the ErrorInfo(s) contained in E to their respective handlers. Any 875/// unhandled errors (or Errors returned by handlers) are re-concatenated and 876/// returned. 877/// Because this function returns an error, its result must also be checked 878/// or returned. If you intend to handle all errors use handleAllErrors 879/// (which returns void, and will abort() on unhandled errors) instead. 880template <typename... HandlerTs> 881Error handleErrors(Error E, HandlerTs &&... Hs) { 882 if (!E) 883 return Error::success(); 884 885 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); 886 887 if (Payload->isA<ErrorList>()) { 888 ErrorList &List = static_cast<ErrorList &>(*Payload); 889 Error R; 890 for (auto &P : List.Payloads) 891 R = ErrorList::join( 892 std::move(R), 893 handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); 894 return R; 895 } 896 897 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); 898} 899 900/// Behaves the same as handleErrors, except that by contract all errors 901/// *must* be handled by the given handlers (i.e. there must be no remaining 902/// errors after running the handlers, or llvm_unreachable is called). 903template <typename... HandlerTs> 904void handleAllErrors(Error E, HandlerTs &&... Handlers) { 905 cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); 906} 907 908/// Check that E is a non-error, then drop it. 909/// If E is an error, llvm_unreachable will be called. 910inline void handleAllErrors(Error E) { 911 cantFail(std::move(E)); 912} 913 914/// Handle any errors (if present) in an Expected<T>, then try a recovery path. 915/// 916/// If the incoming value is a success value it is returned unmodified. If it 917/// is a failure value then it the contained error is passed to handleErrors. 918/// If handleErrors is able to handle the error then the RecoveryPath functor 919/// is called to supply the final result. If handleErrors is not able to 920/// handle all errors then the unhandled errors are returned. 921/// 922/// This utility enables the follow pattern: 923/// 924/// @code{.cpp} 925/// enum FooStrategy { Aggressive, Conservative }; 926/// Expected<Foo> foo(FooStrategy S); 927/// 928/// auto ResultOrErr = 929/// handleExpected( 930/// foo(Aggressive), 931/// []() { return foo(Conservative); }, 932/// [](AggressiveStrategyError&) { 933/// // Implicitly conusme this - we'll recover by using a conservative 934/// // strategy. 935/// }); 936/// 937/// @endcode 938template <typename T, typename RecoveryFtor, typename... HandlerTs> 939Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, 940 HandlerTs &&... Handlers) { 941 if (ValOrErr) 942 return ValOrErr; 943 944 if (auto Err = handleErrors(ValOrErr.takeError(), 945 std::forward<HandlerTs>(Handlers)...)) 946 return std::move(Err); 947 948 return RecoveryPath(); 949} 950 951/// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner 952/// will be printed before the first one is logged. A newline will be printed 953/// after each error. 954/// 955/// This function is compatible with the helpers from Support/WithColor.h. You 956/// can pass any of them as the OS. Please consider using them instead of 957/// including 'error: ' in the ErrorBanner. 958/// 959/// This is useful in the base level of your program to allow clean termination 960/// (allowing clean deallocation of resources, etc.), while reporting error 961/// information to the user. 962void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); 963 964/// Write all error messages (if any) in E to a string. The newline character 965/// is used to separate error messages. 966inline std::string toString(Error E) { 967 SmallVector<std::string, 2> Errors; 968 handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { 969 Errors.push_back(EI.message()); 970 }); 971 return join(Errors.begin(), Errors.end(), "\n"); 972} 973 974/// Consume a Error without doing anything. This method should be used 975/// only where an error can be considered a reasonable and expected return 976/// value. 977/// 978/// Uses of this method are potentially indicative of design problems: If it's 979/// legitimate to do nothing while processing an "error", the error-producer 980/// might be more clearly refactored to return an Optional<T>. 981inline void consumeError(Error Err) { 982 handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); 983} 984 985/// Helper for converting an Error to a bool. 986/// 987/// This method returns true if Err is in an error state, or false if it is 988/// in a success state. Puts Err in a checked state in both cases (unlike 989/// Error::operator bool(), which only does this for success states). 990inline bool errorToBool(Error Err) { 991 bool IsError = static_cast<bool>(Err); 992 if (IsError) 993 consumeError(std::move(Err)); 994 return IsError; 995} 996 997/// Helper for Errors used as out-parameters. 998/// 999/// This helper is for use with the Error-as-out-parameter idiom, where an error 1000/// is passed to a function or method by reference, rather than being returned. 1001/// In such cases it is helpful to set the checked bit on entry to the function 1002/// so that the error can be written to (unchecked Errors abort on assignment) 1003/// and clear the checked bit on exit so that clients cannot accidentally forget 1004/// to check the result. This helper performs these actions automatically using 1005/// RAII: 1006/// 1007/// @code{.cpp} 1008/// Result foo(Error &Err) { 1009/// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set 1010/// // <body of foo> 1011/// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. 1012/// } 1013/// @endcode 1014/// 1015/// ErrorAsOutParameter takes an Error* rather than Error& so that it can be 1016/// used with optional Errors (Error pointers that are allowed to be null). If 1017/// ErrorAsOutParameter took an Error reference, an instance would have to be 1018/// created inside every condition that verified that Error was non-null. By 1019/// taking an Error pointer we can just create one instance at the top of the 1020/// function. 1021class ErrorAsOutParameter { 1022public: 1023 ErrorAsOutParameter(Error *Err) : Err(Err) { 1024 // Raise the checked bit if Err is success. 1025 if (Err) 1026 (void)!!*Err; 1027 } 1028 1029 ~ErrorAsOutParameter() { 1030 // Clear the checked bit. 1031 if (Err && !*Err) 1032 *Err = Error::success(); 1033 } 1034 1035private: 1036 Error *Err; 1037}; 1038 1039/// Helper for Expected<T>s used as out-parameters. 1040/// 1041/// See ErrorAsOutParameter. 1042template <typename T> 1043class ExpectedAsOutParameter { 1044public: 1045 ExpectedAsOutParameter(Expected<T> *ValOrErr) 1046 : ValOrErr(ValOrErr) { 1047 if (ValOrErr) 1048 (void)!!*ValOrErr; 1049 } 1050 1051 ~ExpectedAsOutParameter() { 1052 if (ValOrErr) 1053 ValOrErr->setUnchecked(); 1054 } 1055 1056private: 1057 Expected<T> *ValOrErr; 1058}; 1059 1060/// This class wraps a std::error_code in a Error. 1061/// 1062/// This is useful if you're writing an interface that returns a Error 1063/// (or Expected) and you want to call code that still returns 1064/// std::error_codes. 1065class ECError : public ErrorInfo<ECError> { 1066 friend Error errorCodeToError(std::error_code); 1067 1068 virtual void anchor() override; 1069 1070public: 1071 void setErrorCode(std::error_code EC) { this->EC = EC; } 1072 std::error_code convertToErrorCode() const override { return EC; } 1073 void log(raw_ostream &OS) const override { OS << EC.message(); } 1074 1075 // Used by ErrorInfo::classID. 1076 static char ID; 1077 1078protected: 1079 ECError() = default; 1080 ECError(std::error_code EC) : EC(EC) {} 1081 1082 std::error_code EC; 1083}; 1084 1085/// The value returned by this function can be returned from convertToErrorCode 1086/// for Error values where no sensible translation to std::error_code exists. 1087/// It should only be used in this situation, and should never be used where a 1088/// sensible conversion to std::error_code is available, as attempts to convert 1089/// to/from this error will result in a fatal error. (i.e. it is a programmatic 1090///error to try to convert such a value). 1091std::error_code inconvertibleErrorCode(); 1092 1093/// Helper for converting an std::error_code to a Error. 1094Error errorCodeToError(std::error_code EC); 1095 1096/// Helper for converting an ECError to a std::error_code. 1097/// 1098/// This method requires that Err be Error() or an ECError, otherwise it 1099/// will trigger a call to abort(). 1100std::error_code errorToErrorCode(Error Err); 1101 1102/// Convert an ErrorOr<T> to an Expected<T>. 1103template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { 1104 if (auto EC = EO.getError()) 1105 return errorCodeToError(EC); 1106 return std::move(*EO); 1107} 1108 1109/// Convert an Expected<T> to an ErrorOr<T>. 1110template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { 1111 if (auto Err = E.takeError()) 1112 return errorToErrorCode(std::move(Err)); 1113 return std::move(*E); 1114} 1115 1116/// This class wraps a string in an Error. 1117/// 1118/// StringError is useful in cases where the client is not expected to be able 1119/// to consume the specific error message programmatically (for example, if the 1120/// error message is to be presented to the user). 1121/// 1122/// StringError can also be used when additional information is to be printed 1123/// along with a error_code message. Depending on the constructor called, this 1124/// class can either display: 1125/// 1. the error_code message (ECError behavior) 1126/// 2. a string 1127/// 3. the error_code message and a string 1128/// 1129/// These behaviors are useful when subtyping is required; for example, when a 1130/// specific library needs an explicit error type. In the example below, 1131/// PDBError is derived from StringError: 1132/// 1133/// @code{.cpp} 1134/// Expected<int> foo() { 1135/// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, 1136/// "Additional information"); 1137/// } 1138/// @endcode 1139/// 1140class StringError : public ErrorInfo<StringError> { 1141public: 1142 static char ID; 1143 1144 // Prints EC + S and converts to EC 1145 StringError(std::error_code EC, const Twine &S = Twine()); 1146 1147 // Prints S and converts to EC 1148 StringError(const Twine &S, std::error_code EC); 1149 1150 void log(raw_ostream &OS) const override; 1151 std::error_code convertToErrorCode() const override; 1152 1153 const std::string &getMessage() const { return Msg; } 1154 1155private: 1156 std::string Msg; 1157 std::error_code EC; 1158 const bool PrintMsgOnly = false; 1159}; 1160 1161/// Create formatted StringError object. 1162template <typename... Ts> 1163inline Error createStringError(std::error_code EC, char const *Fmt, 1164 const Ts &... Vals) { 1165 std::string Buffer; 1166 raw_string_ostream Stream(Buffer); 1167 Stream << format(Fmt, Vals...); 1168 return make_error<StringError>(Stream.str(), EC); 1169} 1170 1171Error createStringError(std::error_code EC, char const *Msg); 1172 1173template <typename... Ts> 1174inline Error createStringError(std::errc EC, char const *Fmt, 1175 const Ts &... Vals) { 1176 return createStringError(std::make_error_code(EC), Fmt, Vals...); 1177} 1178 1179/// This class wraps a filename and another Error. 1180/// 1181/// In some cases, an error needs to live along a 'source' name, in order to 1182/// show more detailed information to the user. 1183class FileError final : public ErrorInfo<FileError> { 1184 1185 friend Error createFileError(const Twine &, Error); 1186 friend Error createFileError(const Twine &, size_t, Error); 1187 1188public: 1189 void log(raw_ostream &OS) const override { 1190 assert(Err && !FileName.empty() && "Trying to log after takeError()."); 1191 OS << "'" << FileName << "': "; 1192 if (Line.hasValue()) 1193 OS << "line " << Line.getValue() << ": "; 1194 Err->log(OS); 1195 } 1196 1197 Error takeError() { return Error(std::move(Err)); } 1198 1199 std::error_code convertToErrorCode() const override; 1200 1201 // Used by ErrorInfo::classID. 1202 static char ID; 1203 1204private: 1205 FileError(const Twine &F, Optional<size_t> LineNum, 1206 std::unique_ptr<ErrorInfoBase> E) { 1207 assert(E && "Cannot create FileError from Error success value."); 1208 assert(!F.isTriviallyEmpty() && 1209 "The file name provided to FileError must not be empty."); 1210 FileName = F.str(); 1211 Err = std::move(E); 1212 Line = std::move(LineNum); 1213 } 1214 1215 static Error build(const Twine &F, Optional<size_t> Line, Error E) { 1216 return Error( 1217 std::unique_ptr<FileError>(new FileError(F, Line, E.takePayload()))); 1218 } 1219 1220 std::string FileName; 1221 Optional<size_t> Line; 1222 std::unique_ptr<ErrorInfoBase> Err; 1223}; 1224 1225/// Concatenate a source file path and/or name with an Error. The resulting 1226/// Error is unchecked. 1227inline Error createFileError(const Twine &F, Error E) { 1228 return FileError::build(F, Optional<size_t>(), std::move(E)); 1229} 1230 1231/// Concatenate a source file path and/or name with line number and an Error. 1232/// The resulting Error is unchecked. 1233inline Error createFileError(const Twine &F, size_t Line, Error E) { 1234 return FileError::build(F, Optional<size_t>(Line), std::move(E)); 1235} 1236 1237/// Concatenate a source file path and/or name with a std::error_code 1238/// to form an Error object. 1239inline Error createFileError(const Twine &F, std::error_code EC) { 1240 return createFileError(F, errorCodeToError(EC)); 1241} 1242 1243/// Concatenate a source file path and/or name with line number and 1244/// std::error_code to form an Error object. 1245inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { 1246 return createFileError(F, Line, errorCodeToError(EC)); 1247} 1248 1249Error createFileError(const Twine &F, ErrorSuccess) = delete; 1250 1251/// Helper for check-and-exit error handling. 1252/// 1253/// For tool use only. NOT FOR USE IN LIBRARY CODE. 1254/// 1255class ExitOnError { 1256public: 1257 /// Create an error on exit helper. 1258 ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) 1259 : Banner(std::move(Banner)), 1260 GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} 1261 1262 /// Set the banner string for any errors caught by operator(). 1263 void setBanner(std::string Banner) { this->Banner = std::move(Banner); } 1264 1265 /// Set the exit-code mapper function. 1266 void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { 1267 this->GetExitCode = std::move(GetExitCode); 1268 } 1269 1270 /// Check Err. If it's in a failure state log the error(s) and exit. 1271 void operator()(Error Err) const { checkError(std::move(Err)); } 1272 1273 /// Check E. If it's in a success state then return the contained value. If 1274 /// it's in a failure state log the error(s) and exit. 1275 template <typename T> T operator()(Expected<T> &&E) const { 1276 checkError(E.takeError()); 1277 return std::move(*E); 1278 } 1279 1280 /// Check E. If it's in a success state then return the contained reference. If 1281 /// it's in a failure state log the error(s) and exit. 1282 template <typename T> T& operator()(Expected<T&> &&E) const { 1283 checkError(E.takeError()); 1284 return *E; 1285 } 1286 1287private: 1288 void checkError(Error Err) const { 1289 if (Err) { 1290 int ExitCode = GetExitCode(Err); 1291 logAllUnhandledErrors(std::move(Err), errs(), Banner); 1292 exit(ExitCode); 1293 } 1294 } 1295 1296 std::string Banner; 1297 std::function<int(const Error &)> GetExitCode; 1298}; 1299 1300/// Conversion from Error to LLVMErrorRef for C error bindings. 1301inline LLVMErrorRef wrap(Error Err) { 1302 return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); 1303} 1304 1305/// Conversion from LLVMErrorRef to Error for C error bindings. 1306inline Error unwrap(LLVMErrorRef ErrRef) { 1307 return Error(std::unique_ptr<ErrorInfoBase>( 1308 reinterpret_cast<ErrorInfoBase *>(ErrRef))); 1309} 1310 1311} // end namespace llvm 1312 1313#endif // LLVM_SUPPORT_ERROR_H 1314