blob: 07aebb7d739cc785d5d90abd9c6483a8559233d4 [file] [log] [blame]
Zachary Turner819e77d2016-05-06 20:51:57 +00001#include "llvm/DebugInfo/PDB/Raw/RawError.h"
2#include "llvm/Support/ErrorHandling.h"
3#include "llvm/Support/ManagedStatic.h"
4
5using namespace llvm;
6using namespace llvm::pdb;
7
Benjamin Kramera65b6102016-05-15 15:18:11 +00008namespace {
Peter Collingbourne4718f8b2016-05-24 20:13:46 +00009// FIXME: This class is only here to support the transition to llvm::Error. It
10// will be removed once this transition is complete. Clients should prefer to
11// deal with the Error value directly, rather than converting to error_code.
Zachary Turner819e77d2016-05-06 20:51:57 +000012class RawErrorCategory : public std::error_category {
13public:
14 const char *name() const LLVM_NOEXCEPT override { return "llvm.pdb.raw"; }
15
16 std::string message(int Condition) const override {
17 switch (static_cast<raw_error_code>(Condition)) {
18 case raw_error_code::unspecified:
19 return "An unknown error has occurred.";
20 case raw_error_code::feature_unsupported:
21 return "The feature is unsupported by the implementation.";
22 case raw_error_code::corrupt_file:
23 return "The PDB file is corrupt.";
24 case raw_error_code::insufficient_buffer:
25 return "The buffer is not large enough to read the requested number of "
26 "bytes.";
Zachary Turner3df1bfa2016-06-03 05:52:57 +000027 case raw_error_code::no_stream:
28 return "The specified stream could not be loaded.";
29 case raw_error_code::index_out_of_bounds:
30 return "The specified item does not exist in the array.";
Zachary Turner5acb4ac2016-06-10 05:09:12 +000031 case raw_error_code::invalid_block_address:
32 return "The specified block address is not valid.";
33 case raw_error_code::not_writable:
34 return "The PDB does not support writing.";
Zachary Turner819e77d2016-05-06 20:51:57 +000035 }
36 llvm_unreachable("Unrecognized raw_error_code");
37 }
38};
Benjamin Kramera65b6102016-05-15 15:18:11 +000039} // end anonymous namespace
Zachary Turner819e77d2016-05-06 20:51:57 +000040
41static ManagedStatic<RawErrorCategory> Category;
42
43char RawError::ID = 0;
44
45RawError::RawError(raw_error_code C) : RawError(C, "") {}
46
47RawError::RawError(const std::string &Context)
48 : RawError(raw_error_code::unspecified, Context) {}
49
50RawError::RawError(raw_error_code C, const std::string &Context) : Code(C) {
51 ErrMsg = "Native PDB Error: ";
52 std::error_code EC = convertToErrorCode();
53 if (Code != raw_error_code::unspecified)
54 ErrMsg += EC.message() + " ";
55 if (!Context.empty())
56 ErrMsg += Context;
57}
58
59void RawError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
60
61const std::string &RawError::getErrorMessage() const { return ErrMsg; }
62
63std::error_code RawError::convertToErrorCode() const {
64 return std::error_code(static_cast<int>(Code), *Category);
65}