Files
carbon-lang/executable_semantics/common/error.h
T
aa8a5f174d Replaced std::exit() with return Carbon::ErrorOr for expected errors like invalid syntax (#1120)
* Replaced std::exit() with return llvm::Expected/llvm::Error<T> for expected errors like invalid syntax.

* Use llvm::formatv() for formatting lexer error messages.
x

* Addresed merge errors.

* Fixed impl scope.

* Made ErrorBuilder::operator<< nodiscard, to catch code forgetting 'return' in 'return FATAL_COMPILATION_ERROR()'.

* FatalComplationError() -> ParseAndLexContext::RecordLexerError().
Other usages of ERROR_TOKEN in lexer.lpp were actually supposed to be END_OF_FILE.

* Update executable_semantics/syntax/parse_and_lex_context.h

Co-authored-by: Jon Meow <jperkins@google.com>

* Code review fixes.

* Update executable_semantics/syntax/parser.ypp

Co-authored-by: Jon Meow <jperkins@google.com>

* More code review fixes.

* Update executable_semantics/interpreter/type_checker.h

Co-authored-by: Jon Meow <jperkins@google.com>

* Yet more code review fixes...

* Update executable_semantics/syntax/lexer.lpp

Co-authored-by: Jon Meow <jperkins@google.com>

* code review comments

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* Apply suggestions from code review

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/syntax/lexer.lpp

Co-authored-by: Jon Meow <jperkins@google.com>

* code review

* code review

* Apply suggestions from code review

Co-authored-by: Jon Meow <jperkins@google.com>

* formatted code

* review comments

* Switched to the new ErrorOr<V> error implementation

* code review comments

* fixed comment

* restored ostream.h as #976 makes the change unnecesary

* review comments

Co-authored-by: Jon Meow <jperkins@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-03-22 16:17:04 -04:00

107 lines
3.3 KiB
C++

// 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;
}
operator Error() { return Error(message_); }
template <typename V>
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); !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); \
if (!unique_name.ok()) { \
return std::move(unique_name).error(); \
} \
var = std::move(*unique_name);
#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_