Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 1 | //===- MSFError.cpp - Error extensions for MSF files ------------*- C++ -*-===// |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 10 | #include "llvm/DebugInfo/MSF/MSFError.h" |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorHandling.h" |
| 12 | #include "llvm/Support/ManagedStatic.h" |
| 13 | |
| 14 | using namespace llvm; |
| 15 | using namespace llvm::msf; |
| 16 | |
| 17 | namespace { |
| 18 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 19 | // will be removed once this transition is complete. Clients should prefer to |
| 20 | // deal with the Error value directly, rather than converting to error_code. |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 21 | class MSFErrorCategory : public std::error_category { |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 22 | public: |
| 23 | const char *name() const LLVM_NOEXCEPT override { return "llvm.msf"; } |
| 24 | |
| 25 | std::string message(int Condition) const override { |
| 26 | switch (static_cast<msf_error_code>(Condition)) { |
| 27 | case msf_error_code::unspecified: |
| 28 | return "An unknown error has occurred."; |
| 29 | case msf_error_code::insufficient_buffer: |
| 30 | return "The buffer is not large enough to read the requested number of " |
| 31 | "bytes."; |
| 32 | case msf_error_code::not_writable: |
| 33 | return "The specified stream is not writable."; |
| 34 | case msf_error_code::no_stream: |
| 35 | return "The specified stream does not exist."; |
| 36 | case msf_error_code::invalid_format: |
| 37 | return "The data is in an unexpected format."; |
| 38 | case msf_error_code::block_in_use: |
| 39 | return "The block is already in use."; |
| 40 | } |
| 41 | llvm_unreachable("Unrecognized msf_error_code"); |
| 42 | } |
| 43 | }; |
| 44 | } // end anonymous namespace |
| 45 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 46 | static ManagedStatic<MSFErrorCategory> Category; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 47 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 48 | char MSFError::ID = 0; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 49 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 50 | MSFError::MSFError(msf_error_code C) : MSFError(C, "") {} |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 51 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 52 | MSFError::MSFError(const std::string &Context) |
| 53 | : MSFError(msf_error_code::unspecified, Context) {} |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 54 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 55 | MSFError::MSFError(msf_error_code C, const std::string &Context) : Code(C) { |
| 56 | ErrMsg = "MSF Error: "; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 57 | std::error_code EC = convertToErrorCode(); |
| 58 | if (Code != msf_error_code::unspecified) |
| 59 | ErrMsg += EC.message() + " "; |
| 60 | if (!Context.empty()) |
| 61 | ErrMsg += Context; |
| 62 | } |
| 63 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 64 | void MSFError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; } |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 65 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 66 | const std::string &MSFError::getErrorMessage() const { return ErrMsg; } |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 67 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 68 | std::error_code MSFError::convertToErrorCode() const { |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 69 | return std::error_code(static_cast<int>(Code), *Category); |
| 70 | } |