[lld] r192148 - Convert error code structs to scoped enums.
Rui Ueyama
ruiu at google.com
Mon Oct 7 17:43:50 PDT 2013
Author: ruiu
Date: Mon Oct 7 19:43:50 2013
New Revision: 192148
URL: http://llvm.org/viewvc/llvm-project?rev=192148&view=rev
Log:
Convert error code structs to scoped enums.
Summary:
The original code with enum "_" is intended to emulate scoped enums.
Now we have real scoped enums, so use it.
Reviewers: Bigcheese
CC: llvm-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1852
Modified:
lld/trunk/include/lld/Core/Error.h
lld/trunk/lib/Core/Error.cpp
Modified: lld/trunk/include/lld/Core/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Error.h?rev=192148&r1=192147&r2=192148&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Error.h (original)
+++ lld/trunk/include/lld/Core/Error.h Mon Oct 7 19:43:50 2013
@@ -20,20 +20,13 @@ namespace lld {
const llvm::error_category &native_reader_category();
-struct native_reader_error {
- enum ErrorCode {
- success = 0,
- unknown_file_format,
- file_too_short,
- file_malformed,
- unknown_chunk_type,
- memory_error,
- };
- ErrorCode v_;
-
- native_reader_error(ErrorCode v) : v_(v) {}
- explicit native_reader_error(int v) : v_(ErrorCode(v)) {}
- operator int() const { return v_; }
+enum class native_reader_error {
+ success = 0,
+ unknown_file_format,
+ file_too_short,
+ file_malformed,
+ unknown_chunk_type,
+ memory_error,
};
inline llvm::error_code make_error_code(native_reader_error e) {
@@ -42,17 +35,10 @@ inline llvm::error_code make_error_code(
const llvm::error_category &yaml_reader_category();
-struct yaml_reader_error {
- enum ErrorCode {
- success = 0,
- unknown_keyword,
- illegal_value
- };
- ErrorCode v_;
-
- yaml_reader_error(ErrorCode v) : v_(v) {}
- explicit yaml_reader_error(int v) : v_(ErrorCode(v)) {}
- operator int() const { return v_; }
+enum class yaml_reader_error {
+ success = 0,
+ unknown_keyword,
+ illegal_value
};
inline llvm::error_code make_error_code(yaml_reader_error e) {
@@ -73,18 +59,11 @@ inline llvm::error_code make_error_code(
/// \brief Errors returned by InputGraph functionality
const llvm::error_category &input_graph_error_category();
-struct input_graph_error {
- enum ErrorCode {
- success = 0,
- failure = 1,
- no_more_elements,
- no_more_files,
- };
- ErrorCode v_;
-
- input_graph_error(ErrorCode v) : v_(v) {}
- explicit input_graph_error(int v) : v_(ErrorCode(v)) {}
- operator int() const { return v_; }
+enum class input_graph_error {
+ success = 0,
+ failure = 1,
+ no_more_elements,
+ no_more_files
};
inline llvm::error_code make_error_code(input_graph_error e) {
@@ -96,19 +75,10 @@ inline llvm::error_code make_error_code(
namespace llvm {
template <> struct is_error_code_enum<lld::native_reader_error> : true_type {};
-template <>
-struct is_error_code_enum<lld::native_reader_error::ErrorCode> : true_type {};
-
template <> struct is_error_code_enum<lld::yaml_reader_error> : true_type {};
template <>
-struct is_error_code_enum<lld::yaml_reader_error::ErrorCode> : true_type {};
-
-template <>
struct is_error_code_enum<lld::linker_script_reader_error> : true_type {};
-
template <> struct is_error_code_enum<lld::input_graph_error> : true_type {};
-template <>
-struct is_error_code_enum<lld::input_graph_error::ErrorCode> : true_type {};
} // end namespace llvm
#endif
Modified: lld/trunk/lib/Core/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/Error.cpp?rev=192148&r1=192147&r2=192148&view=diff
==============================================================================
--- lld/trunk/lib/Core/Error.cpp (original)
+++ lld/trunk/lib/Core/Error.cpp Mon Oct 7 19:43:50 2013
@@ -20,27 +20,24 @@ public:
}
virtual std::string message(int ev) const {
- switch (ev) {
- case native_reader_error::success:
+ if (native_reader_error(ev) == native_reader_error::success)
return "Success";
- case native_reader_error::unknown_file_format:
+ if (native_reader_error(ev) == native_reader_error::unknown_file_format)
return "Unknown file format";
- case native_reader_error::file_too_short:
+ if (native_reader_error(ev) == native_reader_error::file_too_short)
return "file truncated";
- case native_reader_error::file_malformed:
+ if (native_reader_error(ev) == native_reader_error::file_malformed)
return "file malformed";
- case native_reader_error::memory_error:
+ if (native_reader_error(ev) == native_reader_error::memory_error)
return "out of memory";
- case native_reader_error::unknown_chunk_type:
+ if (native_reader_error(ev) == native_reader_error::unknown_chunk_type)
return "unknown chunk type";
- default:
- llvm_unreachable("An enumerator of native_reader_error does not have a "
- "message defined.");
- }
+ llvm_unreachable("An enumerator of native_reader_error does not have a "
+ "message defined.");
}
virtual llvm::error_condition default_error_condition(int ev) const {
- if (ev == native_reader_error::success)
+ if (native_reader_error(ev) == native_reader_error::success)
return llvm::errc::success;
return llvm::errc::invalid_argument;
}
@@ -58,21 +55,18 @@ public:
}
virtual std::string message(int ev) const {
- switch (ev) {
- case yaml_reader_error::success:
+ if (yaml_reader_error(ev) == yaml_reader_error::success)
return "Success";
- case yaml_reader_error::unknown_keyword:
+ if (yaml_reader_error(ev) == yaml_reader_error::unknown_keyword)
return "Unknown keyword found in yaml file";
- case yaml_reader_error::illegal_value:
+ if (yaml_reader_error(ev) == yaml_reader_error::illegal_value)
return "Bad value found in yaml file";
- default:
- llvm_unreachable("An enumerator of yaml_reader_error does not have a "
- "message defined.");
- }
+ llvm_unreachable("An enumerator of yaml_reader_error does not have a "
+ "message defined.");
}
virtual llvm::error_condition default_error_condition(int ev) const {
- if (ev == yaml_reader_error::success)
+ if (yaml_reader_error(ev) == yaml_reader_error::success)
return llvm::errc::success;
return llvm::errc::invalid_argument;
}
@@ -88,20 +82,19 @@ public:
virtual const char *name() const { return "lld.linker-script.reader"; }
virtual std::string message(int ev) const {
- switch (ev) {
- case static_cast<int>(linker_script_reader_error::success):
+ linker_script_reader_error e = linker_script_reader_error(ev);
+ if (e == linker_script_reader_error::success)
return "Success";
- case static_cast<int>(linker_script_reader_error::parse_error):
+ if (e == linker_script_reader_error::parse_error)
return "Error parsing linker script";
- default:
- llvm_unreachable(
- "An enumerator of linker_script_reader_error does not have a "
- "message defined.");
- }
+ llvm_unreachable(
+ "An enumerator of linker_script_reader_error does not have a "
+ "message defined.");
}
virtual llvm::error_condition default_error_condition(int ev) const {
- if (ev == static_cast<int>(linker_script_reader_error::success))
+ linker_script_reader_error e = linker_script_reader_error(ev);
+ if (e == linker_script_reader_error::success)
return llvm::errc::success;
return llvm::errc::invalid_argument;
}
@@ -117,17 +110,14 @@ public:
virtual const char *name() const { return "lld.inputGraph.parse"; }
virtual std::string message(int ev) const {
- switch (ev) {
- case input_graph_error::success:
+ if (input_graph_error(ev) == input_graph_error::success)
return "Success";
- default:
- llvm_unreachable("An enumerator of input_graph_error does not have a "
- "message defined.");
- }
+ llvm_unreachable("An enumerator of input_graph_error does not have a "
+ "message defined.");
}
virtual llvm::error_condition default_error_condition(int ev) const {
- if (ev == input_graph_error::success)
+ if (input_graph_error(ev) == input_graph_error::success)
return llvm::errc::success;
return llvm::errc::invalid_argument;
}
More information about the llvm-commits
mailing list