blob: 246beb9b0de0e3e0b7a6f8caee7e78e3be3fca17 [file] [log] [blame]
Zachary Turner334aec42016-07-29 18:38:47 +00001//===- MsfError.cpp - Error extensions for Msf files ------------*- C++ -*-===//
Zachary Turnerbac69d32016-07-22 19:56:05 +00002//
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 Turner334aec42016-07-29 18:38:47 +000010#include "llvm/DebugInfo/Msf/MsfError.h"
Zachary Turnerbac69d32016-07-22 19:56:05 +000011#include "llvm/Support/ErrorHandling.h"
12#include "llvm/Support/ManagedStatic.h"
13
14using namespace llvm;
15using namespace llvm::msf;
16
17namespace {
18// FIXME: This class is only here to support the transition to llvm::Error. It
19// will be removed once this transition is complete. Clients should prefer to
20// deal with the Error value directly, rather than converting to error_code.
Zachary Turner334aec42016-07-29 18:38:47 +000021class MsfErrorCategory : public std::error_category {
Zachary Turnerbac69d32016-07-22 19:56:05 +000022public:
23 const char *name() const LLVM_NOEXCEPT override { return "llvm.msf"; }
24
25 std::string message(int Condition) const override {
26 switch (static_cast<msf_error_code>(Condition)) {
27 case msf_error_code::unspecified:
28 return "An unknown error has occurred.";
29 case msf_error_code::insufficient_buffer:
30 return "The buffer is not large enough to read the requested number of "
31 "bytes.";
32 case msf_error_code::not_writable:
33 return "The specified stream is not writable.";
34 case msf_error_code::no_stream:
35 return "The specified stream does not exist.";
36 case msf_error_code::invalid_format:
37 return "The data is in an unexpected format.";
38 case msf_error_code::block_in_use:
39 return "The block is already in use.";
40 }
41 llvm_unreachable("Unrecognized msf_error_code");
42 }
43};
44} // end anonymous namespace
45
Zachary Turner334aec42016-07-29 18:38:47 +000046static ManagedStatic<MsfErrorCategory> Category;
Zachary Turnerbac69d32016-07-22 19:56:05 +000047
Zachary Turner334aec42016-07-29 18:38:47 +000048char MsfError::ID = 0;
Zachary Turnerbac69d32016-07-22 19:56:05 +000049
Zachary Turner334aec42016-07-29 18:38:47 +000050MsfError::MsfError(msf_error_code C) : MsfError(C, "") {}
Zachary Turnerbac69d32016-07-22 19:56:05 +000051
Zachary Turner334aec42016-07-29 18:38:47 +000052MsfError::MsfError(const std::string &Context)
53 : MsfError(msf_error_code::unspecified, Context) {}
Zachary Turnerbac69d32016-07-22 19:56:05 +000054
Zachary Turner334aec42016-07-29 18:38:47 +000055MsfError::MsfError(msf_error_code C, const std::string &Context) : Code(C) {
56 ErrMsg = "Msf Error: ";
Zachary Turnerbac69d32016-07-22 19:56:05 +000057 std::error_code EC = convertToErrorCode();
58 if (Code != msf_error_code::unspecified)
59 ErrMsg += EC.message() + " ";
60 if (!Context.empty())
61 ErrMsg += Context;
62}
63
Zachary Turner334aec42016-07-29 18:38:47 +000064void MsfError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
Zachary Turnerbac69d32016-07-22 19:56:05 +000065
Zachary Turner334aec42016-07-29 18:38:47 +000066const std::string &MsfError::getErrorMessage() const { return ErrMsg; }
Zachary Turnerbac69d32016-07-22 19:56:05 +000067
Zachary Turner334aec42016-07-29 18:38:47 +000068std::error_code MsfError::convertToErrorCode() const {
Zachary Turnerbac69d32016-07-22 19:56:05 +000069 return std::error_code(static_cast<int>(Code), *Category);
70}