Add a FatalUserError macro to help print user-caused errors. (#668)

This commit is contained in:
Jon Meow
2021-07-21 12:38:56 -07:00
committed by GitHub
parent f3cbfc04c6
commit 034f3600e3
15 changed files with 102 additions and 26 deletions
+18
View File
@@ -4,6 +4,24 @@
package(default_visibility = ["//executable_semantics:__subpackages__"])
cc_library(
name = "error",
hdrs = ["error.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "error_test",
srcs = ["error_test.cpp"],
deps = [
":error",
"@llvm-project//llvm:gtest",
"@llvm-project//llvm:gtest_main",
],
)
cc_library(
name = "tracing_flag",
srcs = ["tracing_flag.cpp"],
+38
View File
@@ -0,0 +1,38 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
#define EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
namespace Carbon {
// Prints an error and exits. This should be used for non-recoverable errors
// with user input.
//
// For example:
// FatalUserError() << "Input is not valid!";
class FatalUserError {
public:
FatalUserError() { llvm::errs() << "ERROR: "; }
~FatalUserError() {
// Finish with a newline.
llvm::errs() << "\n";
exit(-1);
}
// Forward output to llvm::errs.
template <typename T>
FatalUserError& operator<<(const T& message) {
llvm::errs() << message;
return *this;
}
};
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "executable_semantics/common/error.h"
#include "gtest/gtest.h"
namespace Carbon {
TEST(ErrorTest, FatalUserError) {
ASSERT_DEATH({ FatalUserError() << "test"; }, "ERROR: test\n");
}
} // namespace Carbon