171d5a254SDimitry Andric //===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===// 271d5a254SDimitry Andric // 3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 671d5a254SDimitry Andric // 771d5a254SDimitry Andric //===----------------------------------------------------------------------===// 871d5a254SDimitry Andric 971d5a254SDimitry Andric #include "llvm/Support/BinaryStreamError.h" 106f8fc217SDimitry Andric #include "llvm/Support/raw_ostream.h" 1171d5a254SDimitry Andric 1271d5a254SDimitry Andric using namespace llvm; 1371d5a254SDimitry Andric 1471d5a254SDimitry Andric char BinaryStreamError::ID = 0; 1571d5a254SDimitry Andric BinaryStreamError(stream_error_code C)1671d5a254SDimitry AndricBinaryStreamError::BinaryStreamError(stream_error_code C) 1771d5a254SDimitry Andric : BinaryStreamError(C, "") {} 1871d5a254SDimitry Andric BinaryStreamError(StringRef Context)1971d5a254SDimitry AndricBinaryStreamError::BinaryStreamError(StringRef Context) 2071d5a254SDimitry Andric : BinaryStreamError(stream_error_code::unspecified, Context) {} 2171d5a254SDimitry Andric BinaryStreamError(stream_error_code C,StringRef Context)2271d5a254SDimitry AndricBinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context) 2371d5a254SDimitry Andric : Code(C) { 2471d5a254SDimitry Andric ErrMsg = "Stream Error: "; 2571d5a254SDimitry Andric switch (C) { 2671d5a254SDimitry Andric case stream_error_code::unspecified: 2771d5a254SDimitry Andric ErrMsg += "An unspecified error has occurred."; 2871d5a254SDimitry Andric break; 2971d5a254SDimitry Andric case stream_error_code::stream_too_short: 3071d5a254SDimitry Andric ErrMsg += "The stream is too short to perform the requested operation."; 3171d5a254SDimitry Andric break; 3271d5a254SDimitry Andric case stream_error_code::invalid_array_size: 3371d5a254SDimitry Andric ErrMsg += "The buffer size is not a multiple of the array element size."; 3471d5a254SDimitry Andric break; 3571d5a254SDimitry Andric case stream_error_code::invalid_offset: 3671d5a254SDimitry Andric ErrMsg += "The specified offset is invalid for the current stream."; 3771d5a254SDimitry Andric break; 3871d5a254SDimitry Andric case stream_error_code::filesystem_error: 3971d5a254SDimitry Andric ErrMsg += "An I/O error occurred on the file system."; 4071d5a254SDimitry Andric break; 4171d5a254SDimitry Andric } 4271d5a254SDimitry Andric 4371d5a254SDimitry Andric if (!Context.empty()) { 4471d5a254SDimitry Andric ErrMsg += " "; 4571d5a254SDimitry Andric ErrMsg += Context; 4671d5a254SDimitry Andric } 4771d5a254SDimitry Andric } 4871d5a254SDimitry Andric log(raw_ostream & OS) const49d8e91e46SDimitry Andricvoid BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg; } 5071d5a254SDimitry Andric getErrorMessage() const5171d5a254SDimitry AndricStringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; } 5271d5a254SDimitry Andric convertToErrorCode() const5371d5a254SDimitry Andricstd::error_code BinaryStreamError::convertToErrorCode() const { 5471d5a254SDimitry Andric return inconvertibleErrorCode(); 5571d5a254SDimitry Andric } 56