xref: /aosp_15_r20/external/llvm/lib/DebugInfo/CodeView/CodeViewError.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- CodeViewError.cpp - Error extensions for CodeView --------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/CodeViewError.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker using namespace llvm;
15*9880d681SAndroid Build Coastguard Worker using namespace llvm::codeview;
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker namespace {
18*9880d681SAndroid Build Coastguard Worker // FIXME: This class is only here to support the transition to llvm::Error. It
19*9880d681SAndroid Build Coastguard Worker // will be removed once this transition is complete. Clients should prefer to
20*9880d681SAndroid Build Coastguard Worker // deal with the Error value directly, rather than converting to error_code.
21*9880d681SAndroid Build Coastguard Worker class CodeViewErrorCategory : public std::error_category {
22*9880d681SAndroid Build Coastguard Worker public:
name() const23*9880d681SAndroid Build Coastguard Worker   const char *name() const LLVM_NOEXCEPT override { return "llvm.codeview"; }
24*9880d681SAndroid Build Coastguard Worker 
message(int Condition) const25*9880d681SAndroid Build Coastguard Worker   std::string message(int Condition) const override {
26*9880d681SAndroid Build Coastguard Worker     switch (static_cast<cv_error_code>(Condition)) {
27*9880d681SAndroid Build Coastguard Worker     case cv_error_code::unspecified:
28*9880d681SAndroid Build Coastguard Worker       return "An unknown error has occurred.";
29*9880d681SAndroid Build Coastguard Worker     case cv_error_code::insufficient_buffer:
30*9880d681SAndroid Build Coastguard Worker       return "The buffer is not large enough to read the requested number of "
31*9880d681SAndroid Build Coastguard Worker              "bytes.";
32*9880d681SAndroid Build Coastguard Worker     case cv_error_code::corrupt_record:
33*9880d681SAndroid Build Coastguard Worker       return "The CodeView record is corrupted.";
34*9880d681SAndroid Build Coastguard Worker     case cv_error_code::operation_unsupported:
35*9880d681SAndroid Build Coastguard Worker       return "The requested operation is not supported.";
36*9880d681SAndroid Build Coastguard Worker     }
37*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Unrecognized cv_error_code");
38*9880d681SAndroid Build Coastguard Worker   }
39*9880d681SAndroid Build Coastguard Worker };
40*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker static ManagedStatic<CodeViewErrorCategory> Category;
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker char CodeViewError::ID = 0;
45*9880d681SAndroid Build Coastguard Worker 
CodeViewError(cv_error_code C)46*9880d681SAndroid Build Coastguard Worker CodeViewError::CodeViewError(cv_error_code C) : CodeViewError(C, "") {}
47*9880d681SAndroid Build Coastguard Worker 
CodeViewError(const std::string & Context)48*9880d681SAndroid Build Coastguard Worker CodeViewError::CodeViewError(const std::string &Context)
49*9880d681SAndroid Build Coastguard Worker     : CodeViewError(cv_error_code::unspecified, Context) {}
50*9880d681SAndroid Build Coastguard Worker 
CodeViewError(cv_error_code C,const std::string & Context)51*9880d681SAndroid Build Coastguard Worker CodeViewError::CodeViewError(cv_error_code C, const std::string &Context)
52*9880d681SAndroid Build Coastguard Worker     : Code(C) {
53*9880d681SAndroid Build Coastguard Worker   ErrMsg = "CodeView Error: ";
54*9880d681SAndroid Build Coastguard Worker   std::error_code EC = convertToErrorCode();
55*9880d681SAndroid Build Coastguard Worker   if (Code != cv_error_code::unspecified)
56*9880d681SAndroid Build Coastguard Worker     ErrMsg += EC.message() + "  ";
57*9880d681SAndroid Build Coastguard Worker   if (!Context.empty())
58*9880d681SAndroid Build Coastguard Worker     ErrMsg += Context;
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker 
log(raw_ostream & OS) const61*9880d681SAndroid Build Coastguard Worker void CodeViewError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
62*9880d681SAndroid Build Coastguard Worker 
getErrorMessage() const63*9880d681SAndroid Build Coastguard Worker const std::string &CodeViewError::getErrorMessage() const { return ErrMsg; }
64*9880d681SAndroid Build Coastguard Worker 
convertToErrorCode() const65*9880d681SAndroid Build Coastguard Worker std::error_code CodeViewError::convertToErrorCode() const {
66*9880d681SAndroid Build Coastguard Worker   return std::error_code(static_cast<int>(Code), *Category);
67*9880d681SAndroid Build Coastguard Worker }
68