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: 23990504e6SReid 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."; 29*6597c28dSReid Kleckner case generic_error_code::type_server_not_found: 30*6597c28dSReid Kleckner return "Type server PDB was not found."; 31819e77d1SZachary Turner case generic_error_code::dia_sdk_not_present: 32819e77d1SZachary Turner return "LLVM was not compiled with support for DIA. This usually means " 33819e77d1SZachary Turner "that you are are not using MSVC, or your Visual Studio " 34819e77d1SZachary Turner "installation " 35819e77d1SZachary Turner "is corrupt."; 36819e77d1SZachary Turner case generic_error_code::invalid_path: 37819e77d1SZachary Turner return "Unable to load PDB. Make sure the file exists and is readable."; 38819e77d1SZachary Turner } 39819e77d1SZachary Turner llvm_unreachable("Unrecognized generic_error_code"); 40819e77d1SZachary Turner } 41819e77d1SZachary Turner }; 42a65b610bSBenjamin Kramer } // end anonymous namespace 43819e77d1SZachary Turner 44819e77d1SZachary Turner static ManagedStatic<GenericErrorCategory> Category; 45819e77d1SZachary Turner 46819e77d1SZachary Turner char GenericError::ID = 0; 47819e77d1SZachary Turner 48819e77d1SZachary Turner GenericError::GenericError(generic_error_code C) : GenericError(C, "") {} 49819e77d1SZachary Turner 5038380323SZachary Turner GenericError::GenericError(StringRef Context) 51819e77d1SZachary Turner : GenericError(generic_error_code::unspecified, Context) {} 52819e77d1SZachary Turner 5338380323SZachary Turner GenericError::GenericError(generic_error_code C, StringRef Context) : Code(C) { 54819e77d1SZachary Turner ErrMsg = "PDB Error: "; 55819e77d1SZachary Turner std::error_code EC = convertToErrorCode(); 56819e77d1SZachary Turner if (Code != generic_error_code::unspecified) 57819e77d1SZachary Turner ErrMsg += EC.message() + " "; 58819e77d1SZachary Turner if (!Context.empty()) 59819e77d1SZachary Turner ErrMsg += Context; 60819e77d1SZachary Turner } 61819e77d1SZachary Turner 62819e77d1SZachary Turner void GenericError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; } 63819e77d1SZachary Turner 6438380323SZachary Turner StringRef GenericError::getErrorMessage() const { return ErrMsg; } 65819e77d1SZachary Turner 66819e77d1SZachary Turner std::error_code GenericError::convertToErrorCode() const { 67819e77d1SZachary Turner return std::error_code(static_cast<int>(Code), *Category); 68819e77d1SZachary Turner } 69