blob: 548289fff3dfeee12c4192e025dce10245ece939 [file] [log] [blame]
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00001#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turner819e77d2016-05-06 20:51:57 +00002#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:
Reid Kleckner990504e2016-10-19 23:52:38 +000014 const char *name() const noexcept override { return "llvm.pdb.raw"; }
Zachary Turner819e77d2016-05-06 20:51:57 +000015
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.";
Zachary Turnerd218c262016-07-22 15:46:37 +000022 case raw_error_code::invalid_format:
23 return "The record is in an unexpected format.";
Zachary Turner819e77d2016-05-06 20:51:57 +000024 case raw_error_code::corrupt_file:
25 return "The PDB file is corrupt.";
26 case raw_error_code::insufficient_buffer:
27 return "The buffer is not large enough to read the requested number of "
28 "bytes.";
Zachary Turner3df1bfa2016-06-03 05:52:57 +000029 case raw_error_code::no_stream:
30 return "The specified stream could not be loaded.";
31 case raw_error_code::index_out_of_bounds:
32 return "The specified item does not exist in the array.";
Zachary Turner5acb4ac2016-06-10 05:09:12 +000033 case raw_error_code::invalid_block_address:
34 return "The specified block address is not valid.";
Zachary Turnerd218c262016-07-22 15:46:37 +000035 case raw_error_code::duplicate_entry:
36 return "The entry already exists.";
37 case raw_error_code::no_entry:
38 return "The entry does not exist.";
Zachary Turner5acb4ac2016-06-10 05:09:12 +000039 case raw_error_code::not_writable:
40 return "The PDB does not support writing.";
Zachary Turnerea4e6072017-03-15 22:18:53 +000041 case raw_error_code::stream_too_long:
42 return "The stream was longer than expected.";
Zachary Turner01ee3dae2016-06-16 18:22:27 +000043 case raw_error_code::invalid_tpi_hash:
44 return "The Type record has an invalid hash value.";
Zachary Turner819e77d2016-05-06 20:51:57 +000045 }
46 llvm_unreachable("Unrecognized raw_error_code");
47 }
48};
Benjamin Kramera65b6102016-05-15 15:18:11 +000049} // end anonymous namespace
Zachary Turner819e77d2016-05-06 20:51:57 +000050
51static ManagedStatic<RawErrorCategory> Category;
52
53char RawError::ID = 0;
54
55RawError::RawError(raw_error_code C) : RawError(C, "") {}
56
57RawError::RawError(const std::string &Context)
58 : RawError(raw_error_code::unspecified, Context) {}
59
60RawError::RawError(raw_error_code C, const std::string &Context) : Code(C) {
61 ErrMsg = "Native PDB Error: ";
62 std::error_code EC = convertToErrorCode();
63 if (Code != raw_error_code::unspecified)
64 ErrMsg += EC.message() + " ";
65 if (!Context.empty())
66 ErrMsg += Context;
67}
68
69void RawError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
70
71const std::string &RawError::getErrorMessage() const { return ErrMsg; }
72
73std::error_code RawError::convertToErrorCode() const {
74 return std::error_code(static_cast<int>(Code), *Category);
75}