blob: 4db6b2941a6e606cd2e7168e8af54ab191b7430b [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:
23 const char *name() const LLVM_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 }
35 llvm_unreachable("Unrecognized cv_error_code");
36 }
37};
38} // end anonymous namespace
39
40static ManagedStatic<CodeViewErrorCategory> Category;
41
42char CodeViewError::ID = 0;
43
44CodeViewError::CodeViewError(cv_error_code C) : CodeViewError(C, "") {}
45
46CodeViewError::CodeViewError(const std::string &Context)
47 : CodeViewError(cv_error_code::unspecified, Context) {}
48
49CodeViewError::CodeViewError(cv_error_code C, const std::string &Context)
50 : Code(C) {
51 ErrMsg = "CodeView Error: ";
52 std::error_code EC = convertToErrorCode();
53 if (Code != cv_error_code::unspecified)
54 ErrMsg += EC.message() + " ";
55 if (!Context.empty())
56 ErrMsg += Context;
57}
58
59void CodeViewError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
60
61const std::string &CodeViewError::getErrorMessage() const { return ErrMsg; }
62
63std::error_code CodeViewError::convertToErrorCode() const {
64 return std::error_code(static_cast<int>(Code), *Category);
65}