mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
I'm doing this to avoid macro name conflicts, following https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros: "If you do export a macro from a header, it must have a globally unique name. To achieve this, it must be named with a prefix consisting of your project's namespace name (but upper case)." Commands run: ``` sed -i 's/\(DCHECK\|CHECK\|FATAL\|MAKE_UNIQUE_NAME\|MAKE_UNIQUE_NAME_IMPL\|RAW_EXITING_STREAM\|RETURN_IF_ERROR\|RETURN_IF_ERROR_IMPL\|ASSIGN_OR_RETURN\|ASSIGN_OR_RETURN_IMPL\|DIAGNOSTIC_KIND\|RETURN_IF_STACK_LIMITED\)(/CARBON_\1(/g' $(git ls-files *.cpp *.h *.lpp *.ypp *.def ':!third_party') sed -i 's/#undef DIAGNOSTIC_KIND/#undef CARBON_DIAGNOSTIC_KIND/' toolchain/diagnostics/diagnostic_registry.def ``` Note this isn't *quite* everything, but it's intended to be a large pass at everything: ``` ╚╡git grep '#define ' *.cpp *.h *.lpp *.ypp *.def ':!third_party' | grep -v '#define CARBON' | grep -v _H_ explorer/syntax/lexer.lpp: #define YY_USER_ACTION \ explorer/syntax/lexer.lpp: #define SIMPLE_TOKEN(name) \ explorer/syntax/lexer.lpp: #define ARG_TOKEN(name, arg) \ explorer/syntax/parse_and_lex_context.h:#define YY_DECL \ migrate_cpp/cpp_refactoring/var_decl.cpp:#define ABSTRACT_TYPE(Class, Base) migrate_cpp/cpp_refactoring/var_decl.cpp:#define TYPE(Class, Base) \ ``` We may in particular want to do a pass to clean up #ifdef guards and make them be CARBON_ rooted.
164 lines
4.9 KiB
C++
164 lines
4.9 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 COMMON_ERROR_H_
|
|
#define COMMON_ERROR_H_
|
|
|
|
#include <string>
|
|
|
|
#include "common/check.h"
|
|
#include "common/ostream.h"
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Success values should be represented as the presence of a value in ErrorOr,
|
|
// using `ErrorOr<Success>` and `return Success();` if no value needs to be
|
|
// returned.
|
|
struct Success {};
|
|
|
|
// Tracks an error message.
|
|
class [[nodiscard]] Error {
|
|
public:
|
|
// Represents an error state.
|
|
explicit Error(llvm::Twine message) : message_(message.str()) {
|
|
CARBON_CHECK(!message_.empty()) << "Errors must have a message.";
|
|
}
|
|
|
|
Error(Error&& other) noexcept : message_(std::move(other.message_)) {}
|
|
|
|
// Prints the error string. Note this marks as used.
|
|
void Print(llvm::raw_ostream& out) const { out << message(); }
|
|
|
|
// Returns the error message.
|
|
auto message() const -> const std::string& { return message_; }
|
|
|
|
private:
|
|
// The error message.
|
|
std::string message_;
|
|
};
|
|
|
|
// Holds a value of type `T`, or an Error explaining why the value is
|
|
// unavailable.
|
|
template <typename T>
|
|
class [[nodiscard]] ErrorOr {
|
|
public:
|
|
// Constructs with an error; the error must not be Error::Success().
|
|
// Implicit for easy construction on returns.
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
ErrorOr(Error err) : val_(std::move(err)) {}
|
|
|
|
// Constructs with a value.
|
|
// Implicit for easy construction on returns.
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
ErrorOr(T val) : val_(std::move(val)) {}
|
|
|
|
// Moves held state.
|
|
ErrorOr(ErrorOr&& other) noexcept : val_(std::move(other.val_)) {}
|
|
|
|
// Returns true for success.
|
|
auto ok() const -> bool { return std::holds_alternative<T>(val_); }
|
|
|
|
// Returns the contained error.
|
|
// REQUIRES: `ok()` is false.
|
|
auto error() const& -> const Error& {
|
|
CARBON_CHECK(!ok());
|
|
return std::get<Error>(val_);
|
|
}
|
|
auto error() && -> Error {
|
|
CARBON_CHECK(!ok());
|
|
return std::get<Error>(std::move(val_));
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator*() -> T& {
|
|
CARBON_CHECK(ok());
|
|
return std::get<T>(val_);
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator*() const -> const T& {
|
|
CARBON_CHECK(ok());
|
|
return std::get<T>(val_);
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator->() -> T* {
|
|
CARBON_CHECK(ok());
|
|
return &std::get<T>(val_);
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator->() const -> const T* {
|
|
CARBON_CHECK(ok());
|
|
return &std::get<T>(val_);
|
|
}
|
|
|
|
private:
|
|
// Either an error message or
|
|
std::variant<Error, T> val_;
|
|
};
|
|
|
|
// A helper class for accumulating error message and converting to
|
|
// `Error` and `ErrorOr<T>`.
|
|
class ErrorBuilder {
|
|
public:
|
|
ErrorBuilder() : out_(std::make_unique<llvm::raw_string_ostream>(message_)) {}
|
|
|
|
// 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 T>
|
|
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
|
|
operator ErrorOr<T>() {
|
|
return Error(message_);
|
|
}
|
|
|
|
private:
|
|
std::string message_;
|
|
// Use a pointer to allow move construction.
|
|
std::unique_ptr<llvm::raw_string_ostream> out_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
// Macro hackery to get a unique variable name.
|
|
#define CARBON_MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c
|
|
#define CARBON_MAKE_UNIQUE_NAME(a, b, c) CARBON_MAKE_UNIQUE_NAME_IMPL(a, b, c)
|
|
|
|
#define CARBON_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 CARBON_RETURN_IF_ERROR(expr) \
|
|
CARBON_RETURN_IF_ERROR_IMPL( \
|
|
CARBON_MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr)
|
|
|
|
#define CARBON_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 CARBON_ASSIGN_OR_RETURN(var, expr) \
|
|
CARBON_ASSIGN_OR_RETURN_IMPL( \
|
|
CARBON_MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), \
|
|
var, expr)
|
|
|
|
#endif // COMMON_ERROR_H_
|