xref: /freebsd/contrib/llvm-project/llvm/lib/Support/BinaryStreamError.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamError.h"
10*04eeddc0SDimitry Andric #include "llvm/Support/raw_ostream.h"
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric using namespace llvm;
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric char BinaryStreamError::ID = 0;
150b57cec5SDimitry Andric 
BinaryStreamError(stream_error_code C)160b57cec5SDimitry Andric BinaryStreamError::BinaryStreamError(stream_error_code C)
170b57cec5SDimitry Andric     : BinaryStreamError(C, "") {}
180b57cec5SDimitry Andric 
BinaryStreamError(StringRef Context)190b57cec5SDimitry Andric BinaryStreamError::BinaryStreamError(StringRef Context)
200b57cec5SDimitry Andric     : BinaryStreamError(stream_error_code::unspecified, Context) {}
210b57cec5SDimitry Andric 
BinaryStreamError(stream_error_code C,StringRef Context)220b57cec5SDimitry Andric BinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context)
230b57cec5SDimitry Andric     : Code(C) {
240b57cec5SDimitry Andric   ErrMsg = "Stream Error: ";
250b57cec5SDimitry Andric   switch (C) {
260b57cec5SDimitry Andric   case stream_error_code::unspecified:
270b57cec5SDimitry Andric     ErrMsg += "An unspecified error has occurred.";
280b57cec5SDimitry Andric     break;
290b57cec5SDimitry Andric   case stream_error_code::stream_too_short:
300b57cec5SDimitry Andric     ErrMsg += "The stream is too short to perform the requested operation.";
310b57cec5SDimitry Andric     break;
320b57cec5SDimitry Andric   case stream_error_code::invalid_array_size:
330b57cec5SDimitry Andric     ErrMsg += "The buffer size is not a multiple of the array element size.";
340b57cec5SDimitry Andric     break;
350b57cec5SDimitry Andric   case stream_error_code::invalid_offset:
360b57cec5SDimitry Andric     ErrMsg += "The specified offset is invalid for the current stream.";
370b57cec5SDimitry Andric     break;
380b57cec5SDimitry Andric   case stream_error_code::filesystem_error:
390b57cec5SDimitry Andric     ErrMsg += "An I/O error occurred on the file system.";
400b57cec5SDimitry Andric     break;
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   if (!Context.empty()) {
440b57cec5SDimitry Andric     ErrMsg += "  ";
450b57cec5SDimitry Andric     ErrMsg += Context;
460b57cec5SDimitry Andric   }
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric 
log(raw_ostream & OS) const490b57cec5SDimitry Andric void BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg; }
500b57cec5SDimitry Andric 
getErrorMessage() const510b57cec5SDimitry Andric StringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; }
520b57cec5SDimitry Andric 
convertToErrorCode() const530b57cec5SDimitry Andric std::error_code BinaryStreamError::convertToErrorCode() const {
540b57cec5SDimitry Andric   return inconvertibleErrorCode();
550b57cec5SDimitry Andric }
56