Zachary Turner | d0b44fa | 2017-02-28 17:49:34 +0000 | [diff] [blame] | 1 | //===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Zachary Turner | d0b44fa | 2017-02-28 17:49:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 9 | #include "llvm/Support/BinaryStreamError.h" |
Zachary Turner | d0b44fa | 2017-02-28 17:49:34 +0000 | [diff] [blame] | 10 | #include "llvm/Support/ErrorHandling.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | |
| 14 | char BinaryStreamError::ID = 0; |
| 15 | |
| 16 | BinaryStreamError::BinaryStreamError(stream_error_code C) |
| 17 | : BinaryStreamError(C, "") {} |
| 18 | |
| 19 | BinaryStreamError::BinaryStreamError(StringRef Context) |
| 20 | : BinaryStreamError(stream_error_code::unspecified, Context) {} |
| 21 | |
| 22 | BinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context) |
| 23 | : Code(C) { |
| 24 | ErrMsg = "Stream Error: "; |
| 25 | switch (C) { |
| 26 | case stream_error_code::unspecified: |
| 27 | ErrMsg += "An unspecified error has occurred."; |
| 28 | break; |
| 29 | case stream_error_code::stream_too_short: |
| 30 | ErrMsg += "The stream is too short to perform the requested operation."; |
| 31 | break; |
| 32 | case stream_error_code::invalid_array_size: |
| 33 | ErrMsg += "The buffer size is not a multiple of the array element size."; |
| 34 | break; |
| 35 | case stream_error_code::invalid_offset: |
| 36 | ErrMsg += "The specified offset is invalid for the current stream."; |
| 37 | break; |
| 38 | case stream_error_code::filesystem_error: |
| 39 | ErrMsg += "An I/O error occurred on the file system."; |
| 40 | break; |
Zachary Turner | d0b44fa | 2017-02-28 17:49:34 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | if (!Context.empty()) { |
| 44 | ErrMsg += " "; |
| 45 | ErrMsg += Context; |
| 46 | } |
| 47 | } |
| 48 | |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 49 | void BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg; } |
Zachary Turner | d0b44fa | 2017-02-28 17:49:34 +0000 | [diff] [blame] | 50 | |
| 51 | StringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; } |
| 52 | |
| 53 | std::error_code BinaryStreamError::convertToErrorCode() const { |
| 54 | return inconvertibleErrorCode(); |
| 55 | } |