1819e77d1SZachary Turner //===- Error.cpp - system_error extensions for PDB --------------*- C++ -*-===//
2819e77d1SZachary Turner //
3819e77d1SZachary Turner //                     The LLVM Compiler Infrastructure
4819e77d1SZachary Turner //
5819e77d1SZachary Turner // This file is distributed under the University of Illinois Open Source
6819e77d1SZachary Turner // License. See LICENSE.TXT for details.
7819e77d1SZachary Turner //
8819e77d1SZachary Turner //===----------------------------------------------------------------------===//
9819e77d1SZachary Turner 
10819e77d1SZachary Turner #include "llvm/DebugInfo/PDB/GenericError.h"
11819e77d1SZachary Turner #include "llvm/Support/ErrorHandling.h"
12819e77d1SZachary Turner #include "llvm/Support/ManagedStatic.h"
13819e77d1SZachary Turner 
14819e77d1SZachary Turner using namespace llvm;
15819e77d1SZachary Turner using namespace llvm::pdb;
16819e77d1SZachary Turner 
17a65b610bSBenjamin Kramer namespace {
184718f8b5SPeter Collingbourne // FIXME: This class is only here to support the transition to llvm::Error. It
194718f8b5SPeter Collingbourne // will be removed once this transition is complete. Clients should prefer to
204718f8b5SPeter Collingbourne // deal with the Error value directly, rather than converting to error_code.
21819e77d1SZachary Turner class GenericErrorCategory : public std::error_category {
22819e77d1SZachary Turner public:
23*990504e6SReid Kleckner   const char *name() const noexcept override { return "llvm.pdb"; }
24819e77d1SZachary Turner 
25819e77d1SZachary Turner   std::string message(int Condition) const override {
26819e77d1SZachary Turner     switch (static_cast<generic_error_code>(Condition)) {
27819e77d1SZachary Turner     case generic_error_code::unspecified:
28819e77d1SZachary Turner       return "An unknown error has occurred.";
29819e77d1SZachary Turner     case generic_error_code::dia_sdk_not_present:
30819e77d1SZachary Turner       return "LLVM was not compiled with support for DIA.  This usually means "
31819e77d1SZachary Turner              "that you are are not using MSVC, or your Visual Studio "
32819e77d1SZachary Turner              "installation "
33819e77d1SZachary Turner              "is corrupt.";
34819e77d1SZachary Turner     case generic_error_code::invalid_path:
35819e77d1SZachary Turner       return "Unable to load PDB.  Make sure the file exists and is readable.";
36819e77d1SZachary Turner     }
37819e77d1SZachary Turner     llvm_unreachable("Unrecognized generic_error_code");
38819e77d1SZachary Turner   }
39819e77d1SZachary Turner };
40a65b610bSBenjamin Kramer } // end anonymous namespace
41819e77d1SZachary Turner 
42819e77d1SZachary Turner static ManagedStatic<GenericErrorCategory> Category;
43819e77d1SZachary Turner 
44819e77d1SZachary Turner char GenericError::ID = 0;
45819e77d1SZachary Turner 
46819e77d1SZachary Turner GenericError::GenericError(generic_error_code C) : GenericError(C, "") {}
47819e77d1SZachary Turner 
4838380323SZachary Turner GenericError::GenericError(StringRef Context)
49819e77d1SZachary Turner     : GenericError(generic_error_code::unspecified, Context) {}
50819e77d1SZachary Turner 
5138380323SZachary Turner GenericError::GenericError(generic_error_code C, StringRef Context) : Code(C) {
52819e77d1SZachary Turner   ErrMsg = "PDB Error: ";
53819e77d1SZachary Turner   std::error_code EC = convertToErrorCode();
54819e77d1SZachary Turner   if (Code != generic_error_code::unspecified)
55819e77d1SZachary Turner     ErrMsg += EC.message() + "  ";
56819e77d1SZachary Turner   if (!Context.empty())
57819e77d1SZachary Turner     ErrMsg += Context;
58819e77d1SZachary Turner }
59819e77d1SZachary Turner 
60819e77d1SZachary Turner void GenericError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
61819e77d1SZachary Turner 
6238380323SZachary Turner StringRef GenericError::getErrorMessage() const { return ErrMsg; }
63819e77d1SZachary Turner 
64819e77d1SZachary Turner std::error_code GenericError::convertToErrorCode() const {
65819e77d1SZachary Turner   return std::error_code(static_cast<int>(Code), *Category);
66819e77d1SZachary Turner }
67