13ca95b02SDimitry Andric //===- CodeViewError.cpp - Error extensions for CodeView --------*- C++ -*-===// 23ca95b02SDimitry Andric // 33ca95b02SDimitry Andric // The LLVM Compiler Infrastructure 43ca95b02SDimitry Andric // 53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source 63ca95b02SDimitry Andric // License. See LICENSE.TXT for details. 73ca95b02SDimitry Andric // 83ca95b02SDimitry Andric //===----------------------------------------------------------------------===// 93ca95b02SDimitry Andric 103ca95b02SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewError.h" 113ca95b02SDimitry Andric #include "llvm/Support/ErrorHandling.h" 123ca95b02SDimitry Andric #include "llvm/Support/ManagedStatic.h" 133ca95b02SDimitry Andric 143ca95b02SDimitry Andric using namespace llvm; 153ca95b02SDimitry Andric using namespace llvm::codeview; 163ca95b02SDimitry Andric 173ca95b02SDimitry Andric namespace { 183ca95b02SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 193ca95b02SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 203ca95b02SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 213ca95b02SDimitry Andric class CodeViewErrorCategory : public std::error_category { 223ca95b02SDimitry Andric public: 23d88c1a5aSDimitry Andric const char *name() const noexcept override { return "llvm.codeview"; } 243ca95b02SDimitry Andric 253ca95b02SDimitry Andric std::string message(int Condition) const override { 263ca95b02SDimitry Andric switch (static_cast<cv_error_code>(Condition)) { 273ca95b02SDimitry Andric case cv_error_code::unspecified: 283ca95b02SDimitry Andric return "An unknown error has occurred."; 293ca95b02SDimitry Andric case cv_error_code::insufficient_buffer: 303ca95b02SDimitry Andric return "The buffer is not large enough to read the requested number of " 313ca95b02SDimitry Andric "bytes."; 323ca95b02SDimitry Andric case cv_error_code::corrupt_record: 333ca95b02SDimitry Andric return "The CodeView record is corrupted."; 343ca95b02SDimitry Andric case cv_error_code::operation_unsupported: 353ca95b02SDimitry Andric return "The requested operation is not supported."; 36d88c1a5aSDimitry Andric case cv_error_code::unknown_member_record: 37d88c1a5aSDimitry Andric return "The member record is of an unknown type."; 383ca95b02SDimitry Andric } 393ca95b02SDimitry Andric llvm_unreachable("Unrecognized cv_error_code"); 403ca95b02SDimitry Andric } 413ca95b02SDimitry Andric }; 423ca95b02SDimitry Andric } // end anonymous namespace 433ca95b02SDimitry Andric 443ca95b02SDimitry Andric static ManagedStatic<CodeViewErrorCategory> Category; 453ca95b02SDimitry Andric 463ca95b02SDimitry Andric char CodeViewError::ID = 0; 473ca95b02SDimitry Andric 483ca95b02SDimitry Andric CodeViewError::CodeViewError(cv_error_code C) : CodeViewError(C, "") {} 493ca95b02SDimitry Andric 503ca95b02SDimitry Andric CodeViewError::CodeViewError(const std::string &Context) 513ca95b02SDimitry Andric : CodeViewError(cv_error_code::unspecified, Context) {} 523ca95b02SDimitry Andric 533ca95b02SDimitry Andric CodeViewError::CodeViewError(cv_error_code C, const std::string &Context) 543ca95b02SDimitry Andric : Code(C) { 553ca95b02SDimitry Andric ErrMsg = "CodeView Error: "; 563ca95b02SDimitry Andric std::error_code EC = convertToErrorCode(); 573ca95b02SDimitry Andric if (Code != cv_error_code::unspecified) 583ca95b02SDimitry Andric ErrMsg += EC.message() + " "; 593ca95b02SDimitry Andric if (!Context.empty()) 603ca95b02SDimitry Andric ErrMsg += Context; 613ca95b02SDimitry Andric } 623ca95b02SDimitry Andric 633ca95b02SDimitry Andric void CodeViewError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; } 643ca95b02SDimitry Andric 653ca95b02SDimitry Andric const std::string &CodeViewError::getErrorMessage() const { return ErrMsg; } 663ca95b02SDimitry Andric 673ca95b02SDimitry Andric std::error_code CodeViewError::convertToErrorCode() const { 683ca95b02SDimitry Andric return std::error_code(static_cast<int>(Code), *Category); 693ca95b02SDimitry Andric } 70