xref: /llvm-project/llvm/lib/DebugInfo/CodeView/CodeViewError.cpp (revision 990504e625a3bf3f3276576f42e07dfdf9f74c4c)
1 //===- CodeViewError.cpp - Error extensions for CodeView --------*- 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 
10 #include "llvm/DebugInfo/CodeView/CodeViewError.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/ManagedStatic.h"
13 
14 using namespace llvm;
15 using namespace llvm::codeview;
16 
17 namespace {
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.
21 class CodeViewErrorCategory : public std::error_category {
22 public:
23   const char *name() const noexcept override { return "llvm.codeview"; }
24 
25   std::string message(int Condition) const override {
26     switch (static_cast<cv_error_code>(Condition)) {
27     case cv_error_code::unspecified:
28       return "An unknown error has occurred.";
29     case cv_error_code::insufficient_buffer:
30       return "The buffer is not large enough to read the requested number of "
31              "bytes.";
32     case cv_error_code::corrupt_record:
33       return "The CodeView record is corrupted.";
34     case cv_error_code::operation_unsupported:
35       return "The requested operation is not supported.";
36     case cv_error_code::unknown_member_record:
37       return "The member record is of an unknown type.";
38     }
39     llvm_unreachable("Unrecognized cv_error_code");
40   }
41 };
42 } // end anonymous namespace
43 
44 static ManagedStatic<CodeViewErrorCategory> Category;
45 
46 char CodeViewError::ID = 0;
47 
48 CodeViewError::CodeViewError(cv_error_code C) : CodeViewError(C, "") {}
49 
50 CodeViewError::CodeViewError(const std::string &Context)
51     : CodeViewError(cv_error_code::unspecified, Context) {}
52 
53 CodeViewError::CodeViewError(cv_error_code C, const std::string &Context)
54     : Code(C) {
55   ErrMsg = "CodeView Error: ";
56   std::error_code EC = convertToErrorCode();
57   if (Code != cv_error_code::unspecified)
58     ErrMsg += EC.message() + "  ";
59   if (!Context.empty())
60     ErrMsg += Context;
61 }
62 
63 void CodeViewError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
64 
65 const std::string &CodeViewError::getErrorMessage() const { return ErrMsg; }
66 
67 std::error_code CodeViewError::convertToErrorCode() const {
68   return std::error_code(static_cast<int>(Code), *Category);
69 }
70