Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 1 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 2 | #include "llvm/Support/ErrorHandling.h" |
| 3 | #include "llvm/Support/ManagedStatic.h" |
| 4 | |
| 5 | using namespace llvm; |
| 6 | using namespace llvm::pdb; |
| 7 | |
| 8 | class RawErrorCategory : public std::error_category { |
| 9 | public: |
| 10 | const char *name() const LLVM_NOEXCEPT override { return "llvm.pdb.raw"; } |
| 11 | |
| 12 | std::string message(int Condition) const override { |
| 13 | switch (static_cast<raw_error_code>(Condition)) { |
| 14 | case raw_error_code::unspecified: |
| 15 | return "An unknown error has occurred."; |
| 16 | case raw_error_code::feature_unsupported: |
| 17 | return "The feature is unsupported by the implementation."; |
| 18 | case raw_error_code::corrupt_file: |
| 19 | return "The PDB file is corrupt."; |
| 20 | case raw_error_code::insufficient_buffer: |
| 21 | return "The buffer is not large enough to read the requested number of " |
| 22 | "bytes."; |
| 23 | } |
| 24 | llvm_unreachable("Unrecognized raw_error_code"); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | static ManagedStatic<RawErrorCategory> Category; |
| 29 | |
| 30 | char RawError::ID = 0; |
| 31 | |
| 32 | RawError::RawError(raw_error_code C) : RawError(C, "") {} |
| 33 | |
| 34 | RawError::RawError(const std::string &Context) |
| 35 | : RawError(raw_error_code::unspecified, Context) {} |
| 36 | |
| 37 | RawError::RawError(raw_error_code C, const std::string &Context) : Code(C) { |
| 38 | ErrMsg = "Native PDB Error: "; |
| 39 | std::error_code EC = convertToErrorCode(); |
| 40 | if (Code != raw_error_code::unspecified) |
| 41 | ErrMsg += EC.message() + " "; |
| 42 | if (!Context.empty()) |
| 43 | ErrMsg += Context; |
| 44 | } |
| 45 | |
| 46 | void RawError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; } |
| 47 | |
| 48 | const std::string &RawError::getErrorMessage() const { return ErrMsg; } |
| 49 | |
| 50 | std::error_code RawError::convertToErrorCode() const { |
| 51 | return std::error_code(static_cast<int>(Code), *Category); |
| 52 | } |