blob: 8de266b836b4b851d059a61c55fc199452a17f0c [file] [log] [blame]
Zachary Turnerd5d37dc2016-05-25 20:37:03 +00001//===- 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
14using namespace llvm;
15using namespace llvm::codeview;
16
17namespace {
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.
21class CodeViewErrorCategory : public std::error_category {
22public:
Reid Kleckner990504e2016-10-19 23:52:38 +000023 const char *name() const noexcept override { return "llvm.codeview"; }
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000024
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.";
Zachary Turner7b327d02017-02-16 23:35:45 +000034 case cv_error_code::no_records:
35 return "There are no records";
Zachary Turner5acb4ac2016-06-10 05:09:12 +000036 case cv_error_code::operation_unsupported:
37 return "The requested operation is not supported.";
Zachary Turner5e3e4bb2016-08-05 21:45:34 +000038 case cv_error_code::unknown_member_record:
39 return "The member record is of an unknown type.";
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000040 }
41 llvm_unreachable("Unrecognized cv_error_code");
42 }
43};
44} // end anonymous namespace
45
46static ManagedStatic<CodeViewErrorCategory> Category;
47
48char CodeViewError::ID = 0;
49
50CodeViewError::CodeViewError(cv_error_code C) : CodeViewError(C, "") {}
51
52CodeViewError::CodeViewError(const std::string &Context)
53 : CodeViewError(cv_error_code::unspecified, Context) {}
54
55CodeViewError::CodeViewError(cv_error_code C, const std::string &Context)
56 : Code(C) {
57 ErrMsg = "CodeView Error: ";
58 std::error_code EC = convertToErrorCode();
59 if (Code != cv_error_code::unspecified)
60 ErrMsg += EC.message() + " ";
61 if (!Context.empty())
62 ErrMsg += Context;
63}
64
65void CodeViewError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
66
67const std::string &CodeViewError::getErrorMessage() const { return ErrMsg; }
68
69std::error_code CodeViewError::convertToErrorCode() const {
70 return std::error_code(static_cast<int>(Code), *Category);
71}