1*0b57cec5SDimitry Andric //===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamError.h"
10*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric using namespace llvm;
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric char BinaryStreamError::ID = 0;
15*0b57cec5SDimitry Andric 
BinaryStreamError(stream_error_code C)16*0b57cec5SDimitry Andric BinaryStreamError::BinaryStreamError(stream_error_code C)
17*0b57cec5SDimitry Andric     : BinaryStreamError(C, "") {}
18*0b57cec5SDimitry Andric 
BinaryStreamError(StringRef Context)19*0b57cec5SDimitry Andric BinaryStreamError::BinaryStreamError(StringRef Context)
20*0b57cec5SDimitry Andric     : BinaryStreamError(stream_error_code::unspecified, Context) {}
21*0b57cec5SDimitry Andric 
BinaryStreamError(stream_error_code C,StringRef Context)22*0b57cec5SDimitry Andric BinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context)
23*0b57cec5SDimitry Andric     : Code(C) {
24*0b57cec5SDimitry Andric   ErrMsg = "Stream Error: ";
25*0b57cec5SDimitry Andric   switch (C) {
26*0b57cec5SDimitry Andric   case stream_error_code::unspecified:
27*0b57cec5SDimitry Andric     ErrMsg += "An unspecified error has occurred.";
28*0b57cec5SDimitry Andric     break;
29*0b57cec5SDimitry Andric   case stream_error_code::stream_too_short:
30*0b57cec5SDimitry Andric     ErrMsg += "The stream is too short to perform the requested operation.";
31*0b57cec5SDimitry Andric     break;
32*0b57cec5SDimitry Andric   case stream_error_code::invalid_array_size:
33*0b57cec5SDimitry Andric     ErrMsg += "The buffer size is not a multiple of the array element size.";
34*0b57cec5SDimitry Andric     break;
35*0b57cec5SDimitry Andric   case stream_error_code::invalid_offset:
36*0b57cec5SDimitry Andric     ErrMsg += "The specified offset is invalid for the current stream.";
37*0b57cec5SDimitry Andric     break;
38*0b57cec5SDimitry Andric   case stream_error_code::filesystem_error:
39*0b57cec5SDimitry Andric     ErrMsg += "An I/O error occurred on the file system.";
40*0b57cec5SDimitry Andric     break;
41*0b57cec5SDimitry Andric   }
42*0b57cec5SDimitry Andric 
43*0b57cec5SDimitry Andric   if (!Context.empty()) {
44*0b57cec5SDimitry Andric     ErrMsg += "  ";
45*0b57cec5SDimitry Andric     ErrMsg += Context;
46*0b57cec5SDimitry Andric   }
47*0b57cec5SDimitry Andric }
48*0b57cec5SDimitry Andric 
log(raw_ostream & OS) const49*0b57cec5SDimitry Andric void BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg; }
50*0b57cec5SDimitry Andric 
getErrorMessage() const51*0b57cec5SDimitry Andric StringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; }
52*0b57cec5SDimitry Andric 
convertToErrorCode() const53*0b57cec5SDimitry Andric std::error_code BinaryStreamError::convertToErrorCode() const {
54*0b57cec5SDimitry Andric   return inconvertibleErrorCode();
55*0b57cec5SDimitry Andric }
56