mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 15:21:04 +01:00
Switch executable semantics to use ErrorBuilder directly and relocate macros (#1184)
This commit is contained in:
@@ -13,23 +13,21 @@ cc_library(
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "error",
|
||||
hdrs = ["error.h"],
|
||||
name = "error_builders",
|
||||
hdrs = ["error_builders.h"],
|
||||
deps = [
|
||||
"//common:check",
|
||||
":source_location",
|
||||
"//common:error",
|
||||
"//executable_semantics/ast:source_location",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "error_test",
|
||||
srcs = ["error_test.cpp"],
|
||||
name = "error_builders_test",
|
||||
srcs = ["error_builders_test.cpp"],
|
||||
deps = [
|
||||
":error",
|
||||
":error_builders",
|
||||
":source_location",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -38,6 +36,14 @@ cc_library(
|
||||
hdrs = ["nonnull.h"],
|
||||
deps = [
|
||||
"//common:check",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "source_location",
|
||||
hdrs = ["source_location.h"],
|
||||
deps = [
|
||||
":nonnull",
|
||||
"//common:ostream",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
// 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 <optional>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "common/error.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Signals.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// A helper class for accumulating error message and converting to
|
||||
// `Carbon::Error`/`Carbon::ErrorOr<T>`.
|
||||
class ErrorBuilder {
|
||||
public:
|
||||
explicit ErrorBuilder(std::optional<SourceLocation> loc = std::nullopt)
|
||||
: out_(message_) {
|
||||
if (loc.has_value()) {
|
||||
out_ << *loc << ": ";
|
||||
}
|
||||
}
|
||||
|
||||
// Accumulates string message.
|
||||
template <typename T>
|
||||
[[nodiscard]] auto operator<<(const T& message) -> ErrorBuilder& {
|
||||
out_ << message;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
|
||||
operator Error() { return Error(message_); }
|
||||
|
||||
template <typename V>
|
||||
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
|
||||
operator ErrorOr<V>() {
|
||||
return Error(message_);
|
||||
}
|
||||
|
||||
std::string message_;
|
||||
llvm::raw_string_ostream out_;
|
||||
};
|
||||
|
||||
// Builds a Carbon::Error instance with the specified message. This should be
|
||||
// used for non-recoverable errors with user input.
|
||||
//
|
||||
// For example:
|
||||
// return FATAL_PROGRAM_ERROR(line_num) << "Line is bad!";
|
||||
// return FATAL_PROGRAM_ERROR_NO_LINE() << "Application is bad!";
|
||||
//
|
||||
// Where possible, try to identify the error as a compilation or
|
||||
// runtime error. Use CHECK/FATAL for internal errors. The generic program
|
||||
// error option is provided as a fallback for cases that don't fit those
|
||||
// classifications.
|
||||
//
|
||||
// TODO: replace below macro invocations with direct `return ErrorBuilder() <<
|
||||
// xx` calls.
|
||||
|
||||
#define FATAL_PROGRAM_ERROR_NO_LINE() \
|
||||
Carbon::ErrorBuilder() << "PROGRAM ERROR: "
|
||||
|
||||
#define FATAL_PROGRAM_ERROR(line) \
|
||||
FATAL_PROGRAM_ERROR_NO_LINE() << (line) << ": "
|
||||
|
||||
#define FATAL_COMPILATION_ERROR_NO_LINE() \
|
||||
Carbon::ErrorBuilder() << "COMPILATION ERROR: "
|
||||
|
||||
#define FATAL_COMPILATION_ERROR(line) \
|
||||
FATAL_COMPILATION_ERROR_NO_LINE() << (line) << ": "
|
||||
|
||||
#define FATAL_RUNTIME_ERROR_NO_LINE() \
|
||||
Carbon::ErrorBuilder() << "RUNTIME ERROR: "
|
||||
|
||||
#define FATAL_RUNTIME_ERROR(line) \
|
||||
FATAL_RUNTIME_ERROR_NO_LINE() << (line) << ": "
|
||||
|
||||
// Macro hackery to get a unique variable name.
|
||||
#define MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c
|
||||
#define MAKE_UNIQUE_NAME(a, b, c) MAKE_UNIQUE_NAME_IMPL(a, b, c)
|
||||
|
||||
#define RETURN_IF_ERROR_IMPL(unique_name, expr) \
|
||||
if (auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \
|
||||
!(unique_name).ok()) { \
|
||||
return std::move(unique_name).error(); \
|
||||
}
|
||||
|
||||
#define RETURN_IF_ERROR(expr) \
|
||||
RETURN_IF_ERROR_IMPL( \
|
||||
MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr)
|
||||
|
||||
#define ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \
|
||||
auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \
|
||||
if (!(unique_name).ok()) { \
|
||||
return std::move(unique_name).error(); \
|
||||
} \
|
||||
var = std::move(*(unique_name)); /* NOLINT(bugprone-macro-parentheses) */
|
||||
|
||||
#define ASSIGN_OR_RETURN(var, expr) \
|
||||
ASSIGN_OR_RETURN_IMPL( \
|
||||
MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), var, expr)
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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_BUILDERS_H_
|
||||
#define EXECUTABLE_SEMANTICS_COMMON_ERROR_BUILDERS_H_
|
||||
|
||||
#include "common/error.h"
|
||||
#include "executable_semantics/common/source_location.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Builds an Error instance with the specified message. This should be used for
|
||||
// non-recoverable errors with user input.
|
||||
//
|
||||
// For example:
|
||||
// return ProgramError(line_num) << "Line is bad!";
|
||||
// return ProgramError() << "Application is bad!";
|
||||
//
|
||||
// Where possible, try to identify the error as a compilation or runtime error.
|
||||
// Use CHECK/FATAL for internal errors. The generic program error option is
|
||||
// provided as a fallback for cases that don't fit those classifications.
|
||||
|
||||
inline auto CompilationError(SourceLocation loc) -> ErrorBuilder {
|
||||
ErrorBuilder builder;
|
||||
(void)(builder << "COMPILATION ERROR: " << loc << ": ");
|
||||
return builder;
|
||||
}
|
||||
|
||||
inline auto ProgramError(SourceLocation loc) -> ErrorBuilder {
|
||||
ErrorBuilder builder;
|
||||
(void)(builder << "PROGRAM ERROR: " << loc << ": ");
|
||||
return builder;
|
||||
}
|
||||
|
||||
inline auto RuntimeError(SourceLocation loc) -> ErrorBuilder {
|
||||
ErrorBuilder builder;
|
||||
(void)(builder << "RUNTIME ERROR: " << loc << ": ");
|
||||
return builder;
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_BUILDERS_H_
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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_builders.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "executable_semantics/common/source_location.h"
|
||||
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
TEST(ErrorBuildersTest, CompilationError) {
|
||||
Error err = CompilationError(SourceLocation("x", 1)) << "test";
|
||||
EXPECT_EQ(err.message(), "COMPILATION ERROR: x:1: test");
|
||||
}
|
||||
|
||||
TEST(ErrorBuildersTest, ProgramError) {
|
||||
Error err = ProgramError(SourceLocation("x", 1)) << "test";
|
||||
EXPECT_EQ(err.message(), "PROGRAM ERROR: x:1: test");
|
||||
}
|
||||
|
||||
TEST(ErrorBuildersTest, RuntimeError) {
|
||||
Error err = RuntimeError(SourceLocation("x", 1)) << "test";
|
||||
EXPECT_EQ(err.message(), "RUNTIME ERROR: x:1: test");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
@@ -1,101 +0,0 @@
|
||||
// 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::Testing {
|
||||
namespace {
|
||||
|
||||
auto MakeSuccess() -> ErrorOr<Success> { return Success(); }
|
||||
|
||||
auto MakeError(std::string_view message) -> ErrorOr<Success> {
|
||||
return Error(message);
|
||||
}
|
||||
|
||||
auto MakeInt(int value) -> ErrorOr<int> { return value; }
|
||||
|
||||
auto MakeFailedInt(std::string_view message) -> ErrorOr<int> {
|
||||
return Error(message);
|
||||
}
|
||||
|
||||
auto ErrorToString(const Error& e) -> std::string { return e.message(); }
|
||||
|
||||
template <typename V>
|
||||
auto ErrorToString(const ErrorOr<V>& e) -> std::string {
|
||||
return e.error().message();
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalProgramError) {
|
||||
EXPECT_EQ(ErrorToString(FATAL_PROGRAM_ERROR_NO_LINE() << "test"),
|
||||
"PROGRAM ERROR: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalRuntimeError) {
|
||||
EXPECT_EQ(ErrorToString(FATAL_RUNTIME_ERROR_NO_LINE() << "test"),
|
||||
"RUNTIME ERROR: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalCompilationError) {
|
||||
EXPECT_EQ(ErrorToString(FATAL_COMPILATION_ERROR_NO_LINE() << "test"),
|
||||
"COMPILATION ERROR: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalProgramErrorLine) {
|
||||
EXPECT_EQ(ErrorToString(FATAL_PROGRAM_ERROR(1) << "test"),
|
||||
"PROGRAM ERROR: 1: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, ReturnIfErrorNoError) {
|
||||
auto result = []() -> ErrorOr<Success> {
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
return Success();
|
||||
}();
|
||||
EXPECT_TRUE(result.ok());
|
||||
}
|
||||
|
||||
TEST(ErrorTest, ReturnIfErrorHasError) {
|
||||
auto result = []() -> ErrorOr<Success> {
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
RETURN_IF_ERROR(MakeError("error"));
|
||||
return Success();
|
||||
}();
|
||||
ASSERT_FALSE(result.ok());
|
||||
EXPECT_EQ(ErrorToString(result), "error");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, AssignOrReturnNoError) {
|
||||
auto result = []() -> ErrorOr<int> {
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
ASSIGN_OR_RETURN(int a, MakeInt(1));
|
||||
ASSIGN_OR_RETURN(const int b, MakeInt(2));
|
||||
int c = 0;
|
||||
ASSIGN_OR_RETURN(c, MakeInt(3));
|
||||
return a + b + c;
|
||||
}();
|
||||
ASSERT_TRUE(result.ok());
|
||||
EXPECT_EQ(6, *result);
|
||||
}
|
||||
|
||||
TEST(ErrorTest, AssignOrReturnHasDirectError) {
|
||||
auto result = []() -> ErrorOr<int> {
|
||||
RETURN_IF_ERROR(MakeError("error"));
|
||||
return 0;
|
||||
}();
|
||||
ASSERT_FALSE(result.ok());
|
||||
}
|
||||
|
||||
TEST(ErrorTest, AssignOrReturnHasErrorInExpected) {
|
||||
auto result = []() -> ErrorOr<int> {
|
||||
ASSIGN_OR_RETURN(int a, MakeFailedInt("error"));
|
||||
return a;
|
||||
}();
|
||||
ASSERT_FALSE(result.ok());
|
||||
EXPECT_EQ(ErrorToString(result), "error");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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_SOURCE_LOCATION_H_
|
||||
#define EXECUTABLE_SEMANTICS_COMMON_SOURCE_LOCATION_H_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class SourceLocation {
|
||||
public:
|
||||
// The filename should be eternal or arena-allocated to eliminate copies.
|
||||
constexpr SourceLocation(const char* filename, int line_num)
|
||||
: filename_(filename), line_num_(line_num) {}
|
||||
SourceLocation(Nonnull<const std::string*> filename, int line_num)
|
||||
: filename_(filename->c_str()), line_num_(line_num) {}
|
||||
|
||||
SourceLocation(const SourceLocation&) = default;
|
||||
SourceLocation(SourceLocation&&) = default;
|
||||
auto operator=(const SourceLocation&) -> SourceLocation& = default;
|
||||
auto operator=(SourceLocation&&) -> SourceLocation& = default;
|
||||
|
||||
auto operator==(SourceLocation other) const -> bool {
|
||||
return filename_ == other.filename_ && line_num_ == other.line_num_;
|
||||
}
|
||||
|
||||
void Print(llvm::raw_ostream& out) const {
|
||||
out << filename_ << ":" << line_num_;
|
||||
}
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
|
||||
private:
|
||||
std::string_view filename_;
|
||||
int line_num_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_COMMON_SOURCE_LOCATION_H_
|
||||
Reference in New Issue
Block a user