2012-02-01 05:47:13 +08: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-28 03:35:41 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2012-02-01 05:47:13 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-05-28 03:35:41 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-02-01 05:47:13 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
class _NativeReaderErrorCategory : public std::error_category {
|
2012-02-01 05:47:13 +08:00
|
|
|
public:
|
2014-06-11 05:26:18 +08:00
|
|
|
const char* name() const LLVM_NOEXCEPT override {
|
2012-02-01 05:47:13 +08:00
|
|
|
return "lld.native.reader";
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
std::string message(int ev) const override {
|
2014-10-06 05:30:32 +08:00
|
|
|
switch (static_cast<NativeReaderError>(ev)) {
|
|
|
|
case NativeReaderError::success:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "Success";
|
2014-10-06 05:30:32 +08:00
|
|
|
case NativeReaderError::unknown_file_format:
|
2013-07-24 06:17:47 +08:00
|
|
|
return "Unknown file format";
|
2014-10-06 05:30:32 +08:00
|
|
|
case NativeReaderError::file_too_short:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "file truncated";
|
2014-10-06 05:30:32 +08:00
|
|
|
case NativeReaderError::file_malformed:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "file malformed";
|
2014-10-06 05:30:32 +08:00
|
|
|
case NativeReaderError::memory_error:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "out of memory";
|
2014-10-06 05:30:32 +08:00
|
|
|
case NativeReaderError::unknown_chunk_type:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "unknown chunk type";
|
2014-10-06 07:43:59 +08:00
|
|
|
case NativeReaderError::conflicting_target_machine:
|
|
|
|
return "conflicting target machine";
|
2014-10-06 05:30:32 +08:00
|
|
|
}
|
2013-10-09 08:57:22 +08:00
|
|
|
llvm_unreachable("An enumerator of NativeReaderError does not have a "
|
2013-10-08 08:43:50 +08:00
|
|
|
"message defined.");
|
2012-02-01 05:47:13 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
const std::error_category &lld::native_reader_category() {
|
2013-10-09 08:57:22 +08:00
|
|
|
static _NativeReaderErrorCategory o;
|
2012-02-01 05:47:13 +08:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
class _YamlReaderErrorCategory : public std::error_category {
|
2012-02-01 05:47:13 +08:00
|
|
|
public:
|
2014-06-11 05:26:18 +08:00
|
|
|
const char* name() const LLVM_NOEXCEPT override {
|
2012-02-01 05:47:13 +08:00
|
|
|
return "lld.yaml.reader";
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
std::string message(int ev) const override {
|
2014-10-06 05:30:32 +08:00
|
|
|
switch (static_cast<YamlReaderError>(ev)) {
|
|
|
|
case YamlReaderError::success:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "Success";
|
2014-10-06 05:30:32 +08:00
|
|
|
case YamlReaderError::unknown_keyword:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "Unknown keyword found in yaml file";
|
2014-10-06 05:30:32 +08:00
|
|
|
case YamlReaderError::illegal_value:
|
2012-02-01 05:47:13 +08:00
|
|
|
return "Bad value found in yaml file";
|
2014-10-06 05:30:32 +08:00
|
|
|
}
|
2013-10-09 08:57:22 +08:00
|
|
|
llvm_unreachable("An enumerator of YamlReaderError does not have a "
|
2013-10-08 08:43:50 +08:00
|
|
|
"message defined.");
|
2012-02-01 05:47:13 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
const std::error_category &lld::YamlReaderCategory() {
|
2013-10-09 08:57:22 +08:00
|
|
|
static _YamlReaderErrorCategory o;
|
2012-02-01 05:47:13 +08:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
class _LinkerScriptReaderErrorCategory : public std::error_category {
|
2013-03-01 08:03:36 +08:00
|
|
|
public:
|
2014-06-11 05:26:18 +08:00
|
|
|
const char *name() const LLVM_NOEXCEPT override {
|
|
|
|
return "lld.linker-script.reader";
|
|
|
|
}
|
2013-03-01 08:03:36 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
std::string message(int ev) const override {
|
2014-10-06 05:30:32 +08:00
|
|
|
switch (static_cast<LinkerScriptReaderError>(ev)) {
|
|
|
|
case LinkerScriptReaderError::success:
|
2013-03-01 08:03:36 +08:00
|
|
|
return "Success";
|
2014-10-06 05:30:32 +08:00
|
|
|
case LinkerScriptReaderError::parse_error:
|
2013-03-01 08:03:36 +08:00
|
|
|
return "Error parsing linker script";
|
2014-10-06 05:30:32 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("An enumerator of LinkerScriptReaderError does not have a "
|
|
|
|
"message defined.");
|
2013-03-01 08:03:36 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
const std::error_category &lld::LinkerScriptReaderCategory() {
|
2013-10-09 08:57:22 +08:00
|
|
|
static _LinkerScriptReaderErrorCategory o;
|
2013-03-01 08:03:36 +08:00
|
|
|
return o;
|
2012-02-01 05:47:13 +08:00
|
|
|
}
|
2013-03-01 08:03:36 +08:00
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
class _InputGraphErrorCategory : public std::error_category {
|
2013-10-07 10:47:09 +08:00
|
|
|
public:
|
2014-06-11 05:26:18 +08:00
|
|
|
const char *name() const LLVM_NOEXCEPT override {
|
|
|
|
return "lld.inputGraph.parse";
|
|
|
|
}
|
2013-10-07 10:47:09 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
std::string message(int ev) const override {
|
2014-10-06 05:30:32 +08:00
|
|
|
switch (static_cast<InputGraphError>(ev)) {
|
|
|
|
case InputGraphError::success:
|
2013-10-07 10:47:09 +08:00
|
|
|
return "Success";
|
2014-10-06 05:30:32 +08:00
|
|
|
case InputGraphError::failure:
|
|
|
|
return "failure";
|
|
|
|
case InputGraphError::no_more_elements:
|
|
|
|
return "no more elements";
|
|
|
|
case InputGraphError::no_more_files:
|
|
|
|
return "no more files";
|
|
|
|
}
|
2013-10-09 08:57:22 +08:00
|
|
|
llvm_unreachable("An enumerator of InputGraphError does not have a "
|
2013-10-08 08:43:50 +08:00
|
|
|
"message defined.");
|
2013-10-07 10:47:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
const std::error_category &lld::InputGraphErrorCategory() {
|
2013-10-09 08:57:22 +08:00
|
|
|
static _InputGraphErrorCategory i;
|
2013-10-07 10:47:09 +08:00
|
|
|
return i;
|
|
|
|
}
|
2013-11-25 07:12:36 +08:00
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
class _ReaderErrorCategory : public std::error_category {
|
2013-11-25 07:12:36 +08:00
|
|
|
public:
|
2014-06-11 05:26:18 +08:00
|
|
|
const char *name() const LLVM_NOEXCEPT override {
|
|
|
|
return "lld.inputGraph.parse";
|
|
|
|
}
|
2013-11-25 07:12:36 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
std::string message(int ev) const override {
|
2014-10-06 05:30:32 +08:00
|
|
|
switch (static_cast<ReaderError>(ev)) {
|
|
|
|
case ReaderError::success:
|
2013-11-25 07:12:36 +08:00
|
|
|
return "Success";
|
2014-10-06 05:30:32 +08:00
|
|
|
case ReaderError::unknown_file_format:
|
2013-11-25 07:12:36 +08:00
|
|
|
return "File format for the input file is not recognized by this flavor";
|
2014-10-06 05:30:32 +08:00
|
|
|
}
|
2013-11-25 07:12:36 +08:00
|
|
|
llvm_unreachable("An enumerator of ReaderError does not have a "
|
|
|
|
"message defined.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 09:44:19 +08:00
|
|
|
const std::error_category &lld::ReaderErrorCategory() {
|
2013-11-25 07:12:36 +08:00
|
|
|
static _ReaderErrorCategory i;
|
|
|
|
return i;
|
|
|
|
}
|
2014-05-28 03:35:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
|
|
|
|
|
|
|
|
/// Temporary class to enable make_dynamic_error_code() until
|
|
|
|
/// llvm::ErrorOr<> is updated to work with error encapsulations
|
|
|
|
/// other than error_code.
|
2014-06-12 09:44:19 +08:00
|
|
|
class dynamic_error_category : public std::error_category {
|
2014-05-28 03:35:41 +08:00
|
|
|
public:
|
2014-10-17 20:55:49 +08:00
|
|
|
~dynamic_error_category() LLVM_NOEXCEPT {}
|
|
|
|
|
2014-06-11 05:26:18 +08:00
|
|
|
const char *name() const LLVM_NOEXCEPT override {
|
|
|
|
return "lld.dynamic_error";
|
|
|
|
}
|
2014-05-28 03:35:41 +08: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];
|
|
|
|
}
|
|
|
|
|
|
|
|
int add(std::string msg) {
|
|
|
|
llvm::sys::SmartScopedLock<true> lock(_mutex);
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::string> _messages;
|
|
|
|
llvm::sys::SmartMutex<true> _mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
static dynamic_error_category categorySingleton;
|
|
|
|
|
2014-06-12 22:04:54 +08:00
|
|
|
std::error_code make_dynamic_error_code(StringRef msg) {
|
|
|
|
return std::error_code(categorySingleton.add(msg), categorySingleton);
|
2014-05-28 03:35:41 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 22:04:54 +08:00
|
|
|
std::error_code make_dynamic_error_code(const Twine &msg) {
|
|
|
|
return std::error_code(categorySingleton.add(msg.str()), categorySingleton);
|
2014-05-28 03:35:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|