mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 11:41:06 +01:00
Add a FatalUserError macro to help print user-caused errors. (#668)
This commit is contained in:
@@ -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"],
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user