summaryrefslogtreecommitdiffstats
path: root/lld/lib/Core/Error.cpp
diff options
context:
space:
mode:
authorNick Kledzik <kledzik@apple.com>2014-05-27 19:35:41 +0000
committerNick Kledzik <kledzik@apple.com>2014-05-27 19:35:41 +0000
commit181ce5ee037c0ec227708d083b92202960d5ad69 (patch)
tree9ec730e6c8776a9c0160e5bdcaac77deae02cb9c /lld/lib/Core/Error.cpp
parentb85f0080e7c8188a998fb08b05944572f7b39b9e (diff)
downloadbcm5719-llvm-181ce5ee037c0ec227708d083b92202960d5ad69.tar.gz
bcm5719-llvm-181ce5ee037c0ec227708d083b92202960d5ad69.zip
Add make_dynamic_error_code().
This is a short-term fix to allow lld Readers to return error messages with dynamic content. The long term fix will be to enhance ErrorOr<> to work with errors other than error_code. Or to change the interface to Readers to pass down a diagnostics object through which all error messages are written. llvm-svn: 209681
Diffstat (limited to 'lld/lib/Core/Error.cpp')
-rw-r--r--lld/lib/Core/Error.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/lld/lib/Core/Error.cpp b/lld/lib/Core/Error.cpp
index 32496b91f47..d8b6560e518 100644
--- a/lld/lib/Core/Error.cpp
+++ b/lld/lib/Core/Error.cpp
@@ -9,7 +9,12 @@
#include "lld/Core/Error.h"
+#include "llvm/ADT/Twine.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Mutex.h"
+
+#include <string>
+#include <vector>
using namespace lld;
@@ -153,3 +158,52 @@ const llvm::error_category &lld::ReaderErrorCategory() {
static _ReaderErrorCategory i;
return i;
}
+
+
+
+
+namespace lld {
+
+
+/// Temporary class to enable make_dynamic_error_code() until
+/// llvm::ErrorOr<> is updated to work with error encapsulations
+/// other than error_code.
+class dynamic_error_category : public llvm::_do_message {
+public:
+ const char *name() const override { return "lld.dynamic_error"; }
+
+ 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;
+
+
+llvm::error_code make_dynamic_error_code(StringRef msg) {
+ return llvm::error_code(categorySingleton.add(msg), categorySingleton);
+}
+
+llvm::error_code make_dynamic_error_code(const Twine &msg) {
+ return llvm::error_code(categorySingleton.add(msg.str()), categorySingleton);
+}
+
+}
+
OpenPOWER on IntegriCloud