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