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:
233ca95b02SDimitry Andric   const char *name() const LLVM_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.";
363ca95b02SDimitry Andric     }
373ca95b02SDimitry Andric     llvm_unreachable("Unrecognized cv_error_code");
383ca95b02SDimitry Andric   }
393ca95b02SDimitry Andric };
403ca95b02SDimitry Andric } // end anonymous namespace
413ca95b02SDimitry Andric 
423ca95b02SDimitry Andric static ManagedStatic<CodeViewErrorCategory> Category;
433ca95b02SDimitry Andric 
443ca95b02SDimitry Andric char CodeViewError::ID = 0;
453ca95b02SDimitry Andric 
463ca95b02SDimitry Andric CodeViewError::CodeViewError(cv_error_code C) : CodeViewError(C, "") {}
473ca95b02SDimitry Andric 
483ca95b02SDimitry Andric CodeViewError::CodeViewError(const std::string &Context)
493ca95b02SDimitry Andric     : CodeViewError(cv_error_code::unspecified, Context) {}
503ca95b02SDimitry Andric 
513ca95b02SDimitry Andric CodeViewError::CodeViewError(cv_error_code C, const std::string &Context)
523ca95b02SDimitry Andric     : Code(C) {
533ca95b02SDimitry Andric   ErrMsg = "CodeView Error: ";
543ca95b02SDimitry Andric   std::error_code EC = convertToErrorCode();
553ca95b02SDimitry Andric   if (Code != cv_error_code::unspecified)
563ca95b02SDimitry Andric     ErrMsg += EC.message() + "  ";
573ca95b02SDimitry Andric   if (!Context.empty())
583ca95b02SDimitry Andric     ErrMsg += Context;
593ca95b02SDimitry Andric }
603ca95b02SDimitry Andric 
613ca95b02SDimitry Andric void CodeViewError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
623ca95b02SDimitry Andric 
633ca95b02SDimitry Andric const std::string &CodeViewError::getErrorMessage() const { return ErrMsg; }
643ca95b02SDimitry Andric 
653ca95b02SDimitry Andric std::error_code CodeViewError::convertToErrorCode() const {
663ca95b02SDimitry Andric   return std::error_code(static_cast<int>(Code), *Category);
673ca95b02SDimitry Andric }
68