• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "llvm/DebugInfo/PDB/Raw/RawError.h"
2 #include "llvm/Support/ErrorHandling.h"
3 #include "llvm/Support/ManagedStatic.h"
4 
5 using namespace llvm;
6 using namespace llvm::pdb;
7 
8 namespace {
9 // 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.
12 class RawErrorCategory : public std::error_category {
13 public:
name() const14   const char *name() const LLVM_NOEXCEPT override { return "llvm.pdb.raw"; }
15 
message(int Condition) const16   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.";
27     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.";
31     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.";
35     case raw_error_code::invalid_tpi_hash:
36       return "The Type record has an invalid hash value.";
37     }
38     llvm_unreachable("Unrecognized raw_error_code");
39   }
40 };
41 } // end anonymous namespace
42 
43 static ManagedStatic<RawErrorCategory> Category;
44 
45 char RawError::ID = 0;
46 
RawError(raw_error_code C)47 RawError::RawError(raw_error_code C) : RawError(C, "") {}
48 
RawError(const std::string & Context)49 RawError::RawError(const std::string &Context)
50     : RawError(raw_error_code::unspecified, Context) {}
51 
RawError(raw_error_code C,const std::string & Context)52 RawError::RawError(raw_error_code C, const std::string &Context) : Code(C) {
53   ErrMsg = "Native PDB Error: ";
54   std::error_code EC = convertToErrorCode();
55   if (Code != raw_error_code::unspecified)
56     ErrMsg += EC.message() + "  ";
57   if (!Context.empty())
58     ErrMsg += Context;
59 }
60 
log(raw_ostream & OS) const61 void RawError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
62 
getErrorMessage() const63 const std::string &RawError::getErrorMessage() const { return ErrMsg; }
64 
convertToErrorCode() const65 std::error_code RawError::convertToErrorCode() const {
66   return std::error_code(static_cast<int>(Code), *Category);
67 }
68