Files

94 lines
2.5 KiB
C++
Raw Permalink Normal View History

2012-01-31 21:47:13 +00:00
//===- Error.cpp - system_error extensions for lld --------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Error.h"
2014-05-27 19:35:41 +00:00
#include "llvm/ADT/Twine.h"
2012-01-31 21:47:13 +00:00
#include "llvm/Support/ErrorHandling.h"
#include <mutex>
2014-05-27 19:35:41 +00:00
#include <string>
#include <vector>
2012-01-31 21:47:13 +00:00
using namespace lld;
namespace {
2014-06-12 01:44:19 +00:00
class _YamlReaderErrorCategory : public std::error_category {
2012-01-31 21:47:13 +00:00
public:
const char* name() const noexcept override {
2012-01-31 21:47:13 +00:00
return "lld.yaml.reader";
}
std::string message(int ev) const override {
2014-10-05 21:30:32 +00:00
switch (static_cast<YamlReaderError>(ev)) {
case YamlReaderError::unknown_keyword:
2012-01-31 21:47:13 +00:00
return "Unknown keyword found in yaml file";
2014-10-05 21:30:32 +00:00
case YamlReaderError::illegal_value:
2012-01-31 21:47:13 +00:00
return "Bad value found in yaml file";
2014-10-05 21:30:32 +00:00
}
llvm_unreachable("An enumerator of YamlReaderError does not have a "
2013-10-08 00:43:50 +00:00
"message defined.");
2012-01-31 21:47:13 +00:00
}
};
} // end anonymous namespace
2012-01-31 21:47:13 +00:00
2014-06-12 01:44:19 +00:00
const std::error_category &lld::YamlReaderCategory() {
static _YamlReaderErrorCategory o;
2012-01-31 21:47:13 +00:00
return o;
}
2014-05-27 19:35:41 +00:00
namespace lld {
/// Temporary class to enable make_dynamic_error_code() until
/// llvm::ErrorOr<> is updated to work with error encapsulations
2014-05-27 19:35:41 +00:00
/// other than error_code.
2014-06-12 01:44:19 +00:00
class dynamic_error_category : public std::error_category {
2014-05-27 19:35:41 +00:00
public:
~dynamic_error_category() override = default;
const char *name() const noexcept override {
2014-06-10 21:26:18 +00:00
return "lld.dynamic_error";
}
2014-05-27 19:35:41 +00:00
std::string message(int ev) const override {
assert(ev >= 0);
assert(ev < (int)_messages.size());
// The value is an index into the string vector.
return _messages[ev];
}
2014-05-27 19:35:41 +00:00
int add(std::string msg) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
2014-05-27 19:35:41 +00:00
// Value zero is always the successs value.
if (_messages.empty())
_messages.push_back("Success");
_messages.push_back(msg);
// Return the index of the string just appended.
return _messages.size() - 1;
}
2014-05-27 19:35:41 +00:00
private:
std::vector<std::string> _messages;
std::recursive_mutex _mutex;
2014-05-27 19:35:41 +00:00
};
static dynamic_error_category categorySingleton;
std::error_code make_dynamic_error_code(StringRef msg) {
return std::error_code(categorySingleton.add(msg), categorySingleton);
2014-05-27 19:35:41 +00:00
}
char GenericError::ID = 0;
GenericError::GenericError(Twine Msg) : Msg(Msg.str()) { }
void GenericError::log(raw_ostream &OS) const {
OS << Msg;
}
} // namespace lld