]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/lib/Core/Error.cpp
Bring lld (release_39 branch, r279477) to contrib
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / lib / Core / Error.cpp
1 //===- Error.cpp - system_error extensions for lld --------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lld/Core/Error.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include <mutex>
14 #include <string>
15 #include <vector>
16
17 using namespace lld;
18
19 class _YamlReaderErrorCategory : public std::error_category {
20 public:
21   const char* name() const LLVM_NOEXCEPT override {
22     return "lld.yaml.reader";
23   }
24
25   std::string message(int ev) const override {
26     switch (static_cast<YamlReaderError>(ev)) {
27     case YamlReaderError::unknown_keyword:
28       return "Unknown keyword found in yaml file";
29     case YamlReaderError::illegal_value:
30       return "Bad value found in yaml file";
31     }
32     llvm_unreachable("An enumerator of YamlReaderError does not have a "
33                      "message defined.");
34   }
35 };
36
37 const std::error_category &lld::YamlReaderCategory() {
38   static _YamlReaderErrorCategory o;
39   return o;
40 }
41
42 namespace lld {
43
44 /// Temporary class to enable make_dynamic_error_code() until
45 /// llvm::ErrorOr<> is updated to work with error encapsulations
46 /// other than error_code.
47 class dynamic_error_category : public std::error_category {
48 public:
49   ~dynamic_error_category() override = default;
50
51   const char *name() const LLVM_NOEXCEPT override {
52     return "lld.dynamic_error";
53   }
54
55   std::string message(int ev) const override {
56     assert(ev >= 0);
57     assert(ev < (int)_messages.size());
58     // The value is an index into the string vector.
59     return _messages[ev];
60   }
61
62   int add(std::string msg) {
63     std::lock_guard<std::recursive_mutex> lock(_mutex);
64     // Value zero is always the successs value.
65     if (_messages.empty())
66       _messages.push_back("Success");
67     _messages.push_back(msg);
68     // Return the index of the string just appended.
69     return _messages.size() - 1;
70   }
71
72 private:
73   std::vector<std::string> _messages;
74   std::recursive_mutex _mutex;
75 };
76
77 static dynamic_error_category categorySingleton;
78
79 std::error_code make_dynamic_error_code(StringRef msg) {
80   return std::error_code(categorySingleton.add(msg), categorySingleton);
81 }
82
83 char GenericError::ID = 0;
84
85 GenericError::GenericError(Twine Msg) : Msg(Msg.str()) { }
86
87 void GenericError::log(raw_ostream &OS) const {
88   OS << Msg;
89 }
90
91 } // namespace lld