xref: /llvm-project/llvm/lib/DebugInfo/PDB/DIA/DIAError.cpp (revision 990504e625a3bf3f3276576f42e07dfdf9f74c4c)
1 #include "llvm/DebugInfo/PDB/DIA/DIAError.h"
2 #include "llvm/Support/ErrorHandling.h"
3 #include "llvm/Support/ManagedStatic.h"
4 
5 using namespace llvm;
6 using namespace llvm::pdb;
7 
8 // FIXME: This class is only here to support the transition to llvm::Error. It
9 // will be removed once this transition is complete. Clients should prefer to
10 // deal with the Error value directly, rather than converting to error_code.
11 class DIAErrorCategory : public std::error_category {
12 public:
13   const char *name() const noexcept override { return "llvm.pdb.dia"; }
14 
15   std::string message(int Condition) const override {
16     switch (static_cast<dia_error_code>(Condition)) {
17     case dia_error_code::could_not_create_impl:
18       return "Failed to connect to DIA at runtime.  Verify that Visual Studio "
19              "is properly installed, or that msdiaXX.dll is in your PATH.";
20     case dia_error_code::invalid_file_format:
21       return "Unable to load PDB.  The file has an unrecognized format.";
22     case dia_error_code::invalid_parameter:
23       return "The parameter is incorrect.";
24     case dia_error_code::already_loaded:
25       return "Unable to load the PDB or EXE, because it is already loaded.";
26     case dia_error_code::debug_info_mismatch:
27       return "The PDB file and the EXE file do not match.";
28     case dia_error_code::unspecified:
29       return "An unknown error has occurred.";
30     }
31     llvm_unreachable("Unrecognized DIAErrorCode");
32   }
33 };
34 
35 static ManagedStatic<DIAErrorCategory> Category;
36 
37 char DIAError::ID = 0;
38 
39 DIAError::DIAError(dia_error_code C) : DIAError(C, "") {}
40 
41 DIAError::DIAError(StringRef Context)
42     : DIAError(dia_error_code::unspecified, Context) {}
43 
44 DIAError::DIAError(dia_error_code C, StringRef Context) : Code(C) {
45   ErrMsg = "DIA Error: ";
46   std::error_code EC = convertToErrorCode();
47   ErrMsg += EC.message() + "  ";
48   if (!Context.empty())
49     ErrMsg += Context;
50 }
51 
52 void DIAError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
53 
54 StringRef DIAError::getErrorMessage() const { return ErrMsg; }
55 
56 std::error_code DIAError::convertToErrorCode() const {
57   return std::error_code(static_cast<int>(Code), *Category);
58 }
59