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 17*a65b610bSBenjamin Kramer namespace { 18819e77d1SZachary Turner class GenericErrorCategory : public std::error_category { 19819e77d1SZachary Turner public: 20819e77d1SZachary Turner const char *name() const LLVM_NOEXCEPT override { return "llvm.pdb"; } 21819e77d1SZachary Turner 22819e77d1SZachary Turner std::string message(int Condition) const override { 23819e77d1SZachary Turner switch (static_cast<generic_error_code>(Condition)) { 24819e77d1SZachary Turner case generic_error_code::unspecified: 25819e77d1SZachary Turner return "An unknown error has occurred."; 26819e77d1SZachary Turner case generic_error_code::dia_sdk_not_present: 27819e77d1SZachary Turner return "LLVM was not compiled with support for DIA. This usually means " 28819e77d1SZachary Turner "that you are are not using MSVC, or your Visual Studio " 29819e77d1SZachary Turner "installation " 30819e77d1SZachary Turner "is corrupt."; 31819e77d1SZachary Turner case generic_error_code::invalid_path: 32819e77d1SZachary Turner return "Unable to load PDB. Make sure the file exists and is readable."; 33819e77d1SZachary Turner } 34819e77d1SZachary Turner llvm_unreachable("Unrecognized generic_error_code"); 35819e77d1SZachary Turner } 36819e77d1SZachary Turner }; 37*a65b610bSBenjamin Kramer } // end anonymous namespace 38819e77d1SZachary Turner 39819e77d1SZachary Turner static ManagedStatic<GenericErrorCategory> Category; 40819e77d1SZachary Turner 41819e77d1SZachary Turner char GenericError::ID = 0; 42819e77d1SZachary Turner 43819e77d1SZachary Turner GenericError::GenericError(generic_error_code C) : GenericError(C, "") {} 44819e77d1SZachary Turner 45819e77d1SZachary Turner GenericError::GenericError(const std::string &Context) 46819e77d1SZachary Turner : GenericError(generic_error_code::unspecified, Context) {} 47819e77d1SZachary Turner 48819e77d1SZachary Turner GenericError::GenericError(generic_error_code C, const std::string &Context) 49819e77d1SZachary Turner : Code(C) { 50819e77d1SZachary Turner ErrMsg = "PDB Error: "; 51819e77d1SZachary Turner std::error_code EC = convertToErrorCode(); 52819e77d1SZachary Turner if (Code != generic_error_code::unspecified) 53819e77d1SZachary Turner ErrMsg += EC.message() + " "; 54819e77d1SZachary Turner if (!Context.empty()) 55819e77d1SZachary Turner ErrMsg += Context; 56819e77d1SZachary Turner } 57819e77d1SZachary Turner 58819e77d1SZachary Turner void GenericError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; } 59819e77d1SZachary Turner 60819e77d1SZachary Turner const std::string &GenericError::getErrorMessage() const { return ErrMsg; } 61819e77d1SZachary Turner 62819e77d1SZachary Turner std::error_code GenericError::convertToErrorCode() const { 63819e77d1SZachary Turner return std::error_code(static_cast<int>(Code), *Category); 64819e77d1SZachary Turner } 65