GenericError.cpp revision 303231
1//===- Error.cpp - system_error extensions for PDB --------------*- 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/PDB/GenericError.h"
11#include "llvm/Support/ErrorHandling.h"
12#include "llvm/Support/ManagedStatic.h"
13
14using namespace llvm;
15using namespace llvm::pdb;
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 GenericErrorCategory : public std::error_category {
22public:
23  const char *name() const LLVM_NOEXCEPT override { return "llvm.pdb"; }
24
25  std::string message(int Condition) const override {
26    switch (static_cast<generic_error_code>(Condition)) {
27    case generic_error_code::unspecified:
28      return "An unknown error has occurred.";
29    case generic_error_code::dia_sdk_not_present:
30      return "LLVM was not compiled with support for DIA.  This usually means "
31             "that you are are not using MSVC, or your Visual Studio "
32             "installation "
33             "is corrupt.";
34    case generic_error_code::invalid_path:
35      return "Unable to load PDB.  Make sure the file exists and is readable.";
36    }
37    llvm_unreachable("Unrecognized generic_error_code");
38  }
39};
40} // end anonymous namespace
41
42static ManagedStatic<GenericErrorCategory> Category;
43
44char GenericError::ID = 0;
45
46GenericError::GenericError(generic_error_code C) : GenericError(C, "") {}
47
48GenericError::GenericError(const std::string &Context)
49    : GenericError(generic_error_code::unspecified, Context) {}
50
51GenericError::GenericError(generic_error_code C, const std::string &Context)
52    : Code(C) {
53  ErrMsg = "PDB Error: ";
54  std::error_code EC = convertToErrorCode();
55  if (Code != generic_error_code::unspecified)
56    ErrMsg += EC.message() + "  ";
57  if (!Context.empty())
58    ErrMsg += Context;
59}
60
61void GenericError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
62
63const std::string &GenericError::getErrorMessage() const { return ErrMsg; }
64
65std::error_code GenericError::convertToErrorCode() const {
66  return std::error_code(static_cast<int>(Code), *Category);
67}
68