blob: 60f5e21f041ab34aea836dc65966321cebff1374 [file] [log] [blame]
Zachary Turnerd0b44fa2017-02-28 17:49:34 +00001//===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===//
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 Turnerd9dc2822017-03-02 20:52:51 +000010#include "llvm/Support/BinaryStreamError.h"
Zachary Turnerd0b44fa2017-02-28 17:49:34 +000011#include "llvm/Support/ErrorHandling.h"
12
13using namespace llvm;
14
15char BinaryStreamError::ID = 0;
16
17BinaryStreamError::BinaryStreamError(stream_error_code C)
18 : BinaryStreamError(C, "") {}
19
20BinaryStreamError::BinaryStreamError(StringRef Context)
21 : BinaryStreamError(stream_error_code::unspecified, Context) {}
22
23BinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context)
24 : Code(C) {
25 ErrMsg = "Stream Error: ";
26 switch (C) {
27 case stream_error_code::unspecified:
28 ErrMsg += "An unspecified error has occurred.";
29 break;
30 case stream_error_code::stream_too_short:
31 ErrMsg += "The stream is too short to perform the requested operation.";
32 break;
33 case stream_error_code::invalid_array_size:
34 ErrMsg += "The buffer size is not a multiple of the array element size.";
35 break;
36 case stream_error_code::invalid_offset:
37 ErrMsg += "The specified offset is invalid for the current stream.";
38 break;
39 case stream_error_code::filesystem_error:
40 ErrMsg += "An I/O error occurred on the file system.";
41 break;
Zachary Turnerd0b44fa2017-02-28 17:49:34 +000042 }
43
44 if (!Context.empty()) {
45 ErrMsg += " ";
46 ErrMsg += Context;
47 }
48}
49
50void BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
51
52StringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; }
53
54std::error_code BinaryStreamError::convertToErrorCode() const {
55 return inconvertibleErrorCode();
56}