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