|
|
@@ -9,8 +9,35 @@
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
-namespace Carbon {
|
|
|
-namespace Internal {
|
|
|
+// Raw exiting stream. This should be used when building other forms of exiting
|
|
|
+// macros like those below. It evaluates to a temporary `ExitingStream` object
|
|
|
+// that can be manipulated, streamed into, and then will exit the program.
|
|
|
+#define RAW_EXITING_STREAM() \
|
|
|
+ Carbon::Internal::ExitingStream::Helper() | Carbon::Internal::ExitingStream()
|
|
|
+
|
|
|
+// Checks the given condition, and if it's false, prints a stack, streams the
|
|
|
+// error message, then exits. This should be used for unexpected errors, such as
|
|
|
+// a bug in the application.
|
|
|
+//
|
|
|
+// For example:
|
|
|
+// CHECK(is_valid) << "Data is not valid!";
|
|
|
+#define CHECK(condition) \
|
|
|
+ (condition) ? (void)0 \
|
|
|
+ : RAW_EXITING_STREAM().TreatAsBug() \
|
|
|
+ << "CHECK failure at " << __FILE__ << ":" << __LINE__ \
|
|
|
+ << ": " #condition \
|
|
|
+ << Carbon::Internal::ExitingStream::AddSeparator()
|
|
|
+
|
|
|
+// This is similar to CHECK, but is unconditional. Writing FATAL() is clearer
|
|
|
+// than CHECK(false) because it avoids confusion about control flow.
|
|
|
+//
|
|
|
+// For example:
|
|
|
+// FATAL() << "Unreachable!";
|
|
|
+#define FATAL() \
|
|
|
+ RAW_EXITING_STREAM().TreatAsBug() \
|
|
|
+ << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
|
|
|
+
|
|
|
+namespace Carbon::Internal {
|
|
|
|
|
|
// Wraps a stream and exiting for fatal errors. Should only be used by the
|
|
|
// macros below.
|
|
|
@@ -33,7 +60,7 @@ class ExitingStream {
|
|
|
|
|
|
// Indicates that the program is exiting due to a bug in the program, rather
|
|
|
// than, e.g., invalid input.
|
|
|
- ExitingStream& TreatAsBug() {
|
|
|
+ auto TreatAsBug() -> ExitingStream& {
|
|
|
treat_as_bug_ = true;
|
|
|
return *this;
|
|
|
}
|
|
|
@@ -44,7 +71,7 @@ class ExitingStream {
|
|
|
|
|
|
// Forward output to llvm::errs.
|
|
|
template <typename T>
|
|
|
- ExitingStream& operator<<(const T& message) {
|
|
|
+ auto operator<<(const T& message) -> ExitingStream& {
|
|
|
if (separator_) {
|
|
|
llvm::errs() << ": ";
|
|
|
separator_ = false;
|
|
|
@@ -53,7 +80,7 @@ class ExitingStream {
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
- ExitingStream& operator<<(AddSeparator /*unused*/) {
|
|
|
+ auto operator<<(AddSeparator /*unused*/) -> ExitingStream& {
|
|
|
separator_ = true;
|
|
|
return *this;
|
|
|
}
|
|
|
@@ -79,36 +106,6 @@ class ExitingStream {
|
|
|
bool treat_as_bug_ = false;
|
|
|
};
|
|
|
|
|
|
-} // namespace Internal
|
|
|
-
|
|
|
-// Raw exiting stream. This should be used when building other forms of exiting
|
|
|
-// macros like those below. It evaluates to a temporary `ExitingStream` object
|
|
|
-// that can be manipulated, streamed into, and then will exit the program.
|
|
|
-#define RAW_EXITING_STREAM() \
|
|
|
- Carbon::Internal::ExitingStream::Helper() | Carbon::Internal::ExitingStream()
|
|
|
-
|
|
|
-// Checks the given condition, and if it's false, prints a stack, streams the
|
|
|
-// error message, then exits. This should be used for unexpected errors, such as
|
|
|
-// a bug in the application.
|
|
|
-//
|
|
|
-// For example:
|
|
|
-// CHECK(is_valid) << "Data is not valid!";
|
|
|
-#define CHECK(condition) \
|
|
|
- (condition) ? (void)0 \
|
|
|
- : RAW_EXITING_STREAM().TreatAsBug() \
|
|
|
- << "CHECK failure at " << __FILE__ << ":" << __LINE__ \
|
|
|
- << ": " #condition \
|
|
|
- << Carbon::Internal::ExitingStream::AddSeparator()
|
|
|
-
|
|
|
-// This is similar to CHECK, but is unconditional. Writing FATAL() is clearer
|
|
|
-// than CHECK(false) because it avoids confusion about control flow.
|
|
|
-//
|
|
|
-// For example:
|
|
|
-// FATAL() << "Unreachable!";
|
|
|
-#define FATAL() \
|
|
|
- RAW_EXITING_STREAM().TreatAsBug() \
|
|
|
- << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
|
|
|
-
|
|
|
-} // namespace Carbon
|
|
|
+} // namespace Carbon::Internal
|
|
|
|
|
|
#endif // COMMON_CHECK_H_
|