Skip to content

Commit

Permalink
v0.0.0-0.nightly.2024.09.13
Browse files Browse the repository at this point in the history
  • Loading branch information
ogra committed Sep 13, 2024
2 parents 4fbd82c + ec63d2b commit dd3397e
Show file tree
Hide file tree
Showing 144 changed files with 1,304 additions and 1,175 deletions.
2 changes: 2 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ cc_library(
hdrs = ["check.h"],
deps = [
":ostream",
":template_string",
"@llvm-project//llvm:Support",
],
)
Expand Down Expand Up @@ -329,6 +330,7 @@ cc_library(
hdrs = ["raw_hashtable_metadata_group.h"],
deps = [
":check",
":ostream",
"@llvm-project//llvm:Support",
],
)
Expand Down
8 changes: 4 additions & 4 deletions common/array_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ class ArrayStack {

// Appends a value to the top array on the stack.
auto AppendToTop(ValueT value) -> void {
CARBON_CHECK(!array_offsets_.empty())
<< "Must call PushArray before PushValue.";
CARBON_CHECK(!array_offsets_.empty(),
"Must call PushArray before PushValue.");
values_.push_back(value);
}

// Adds multiple values to the top array on the stack.
auto AppendToTop(llvm::ArrayRef<ValueT> values) -> void {
CARBON_CHECK(!array_offsets_.empty())
<< "Must call PushArray before PushValues.";
CARBON_CHECK(!array_offsets_.empty(),
"Must call PushArray before PushValues.");
values_.append(values.begin(), values.end());
}

Expand Down
42 changes: 26 additions & 16 deletions common/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,40 @@ namespace Carbon {
// a bug in the application.
//
// For example:
// CARBON_CHECK(is_valid) << "Data is not valid!";
#define CARBON_CHECK(...) \
(__VA_ARGS__) ? (void)0 \
: CARBON_CHECK_INTERNAL_STREAM() \
<< "CHECK failure at " << __FILE__ << ":" << __LINE__ \
<< ": " #__VA_ARGS__ \
<< Carbon::Internal::ExitingStream::AddSeparator()
// CARBON_CHECK(is_valid, "Data is not valid!");
//
// The condition must be parenthesized if it contains top-level commas, for
// example in a template argument list:
// CARBON_CHECK((inst.IsOneOf<Call, TupleLiteral>()),
// "Unexpected inst {0}", inst);
#define CARBON_CHECK(condition, ...) \
(condition) ? (void)0 \
: CARBON_INTERNAL_CHECK(condition __VA_OPT__(, ) __VA_ARGS__)

// DCHECK calls CHECK in debug mode, and does nothing otherwise.
#ifndef NDEBUG
#define CARBON_DCHECK(...) CARBON_CHECK(__VA_ARGS__)
#define CARBON_DCHECK(condition, ...) \
CARBON_CHECK(condition __VA_OPT__(, ) __VA_ARGS__)
#else
#define CARBON_DCHECK(...) CARBON_CHECK(true || (__VA_ARGS__))
// When in a debug build we want to preserve as much as we can of how the
// parameters are used, other than making them be trivially in dead code and
// eliminated by the optimizer. As a consequence we preserve the condition but
// prefix it with a short-circuit operator, and we still emit the (dead) call to
// the check implementation. But we use a special implementation that reduces
// the compile time cost.
#define CARBON_DCHECK(condition, ...) \
(true || (condition)) \
? (void)0 \
: CARBON_INTERNAL_DEAD_DCHECK(condition __VA_OPT__(, ) __VA_ARGS__)
#endif

// This is similar to CHECK, but is unconditional. Writing CARBON_FATAL() is
// clearer than CARBON_CHECK(false) because it avoids confusion about control
// flow.
// This is similar to CHECK, but is unconditional. Writing
// `CARBON_FATAL("message")` is clearer than `CARBON_CHECK(false, "message")
// because it avoids confusion about control flow.
//
// For example:
// CARBON_FATAL() << "Unreachable!";
#define CARBON_FATAL() \
CARBON_CHECK_INTERNAL_STREAM() \
<< "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
// CARBON_FATAL("Unreachable!");
#define CARBON_FATAL(...) CARBON_INTERNAL_FATAL(__VA_ARGS__)

} // namespace Carbon

Expand Down
30 changes: 14 additions & 16 deletions common/check_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "common/check_internal.h"

#include "llvm/Support/ErrorHandling.h"
#include "common/ostream.h"
#include "llvm/Support/Signals.h"

namespace Carbon::Internal {
Expand All @@ -14,22 +14,20 @@ static auto PrintAfterStackTrace(void* str) -> void {
llvm::errs() << reinterpret_cast<char*>(str);
}

ExitingStream::~ExitingStream() {
llvm_unreachable(
"Exiting streams should only be constructed by check.h macros that "
"ensure the special operator| exits the program prior to their "
"destruction!");
}

auto ExitingStream::Done() -> void {
buffer_ << "\n";
buffer_.flush();

// Register another signal handler to print the buffered message. This is
// because we want it at the bottom of output, after LLVM's builtin stack
// output, rather than the top.
auto CheckFailImpl(const char* kind, const char* file, int line,
const char* condition_str, llvm::StringRef extra_message)
-> void {
// Render the final check string here.
std::string message = llvm::formatv(
"{0} failure at {1}:{2}{3}{4}{5}{6}\n", kind, file, line,
llvm::StringRef(condition_str).empty() ? "" : ": ", condition_str,
extra_message.empty() ? "" : ": ", extra_message);

// Register another signal handler to print the message. This is because we
// want it at the bottom of output, after LLVM's builtin stack output, rather
// than the top.
llvm::sys::AddSignalHandler(PrintAfterStackTrace,
const_cast<char*>(buffer_str_.c_str()));
const_cast<char*>(message.c_str()));
// It's useful to exit the program with `std::abort()` for integration with
// debuggers and other tools. We also assume LLVM's exit handling is
// installed, which will stack trace on `std::abort()`.
Expand Down
155 changes: 94 additions & 61 deletions common/check_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,106 @@
#ifndef CARBON_COMMON_CHECK_INTERNAL_H_
#define CARBON_COMMON_CHECK_INTERNAL_H_

#include "common/ostream.h"
#include "common/template_string.h"
#include "llvm/Support/FormatVariadic.h"

namespace Carbon::Internal {

// Wraps a stream and exiting for fatal errors. Should only be used by check.h
// macros.
class ExitingStream {
public:
// A tag type that renders as ": " in an ExitingStream, but only if it is
// followed by additional output. Otherwise, it renders as "". Primarily used
// when building macros around these streams.
struct AddSeparator {};

// Internal type used in macros to dispatch to the `operator|` overload.
struct Helper {};

ExitingStream()
// Prefix the buffer with the current bug report message.
: buffer_(buffer_str_) {}

// Never called.
[[noreturn]] ~ExitingStream();

// If the bool cast occurs, it's because the condition is false. This supports
// && short-circuiting the creation of ExitingStream.
explicit operator bool() const { return true; }

// Forward output to llvm::errs.
template <typename T>
auto operator<<(const T& message) -> ExitingStream& {
if (separator_) {
buffer_ << ": ";
separator_ = false;
}
buffer_ << message;
return *this;
}

auto operator<<(AddSeparator /*add_separator*/) -> ExitingStream& {
separator_ = true;
return *this;
}

// Low-precedence binary operator overload used in check.h macros to flush the
// output and exit the program. We do this in a binary operator rather than
// the destructor to ensure good debug info and backtraces for errors.
[[noreturn]] friend auto operator|(Helper /*helper*/, ExitingStream& stream)
-> void {
stream.Done();
// Implements the check failure message printing.
//
// This is out-of-line and will arrange to stop the program, print any debugging
// information and this string.
//
// This API uses `const char*` C string arguments rather than `llvm::StringRef`
// because we know that these are available as C strings and passing them that
// way lets the code size of calling it be smaller: it only needs to materialize
// a single pointer argument for each. The runtime cost of re-computing the size
// should be minimal. The extra message however might not be compile-time
// guaranteed to be a C string so we use a normal `StringRef` there.
[[noreturn]] auto CheckFailImpl(const char* kind, const char* file, int line,
const char* condition_str,
llvm::StringRef extra_message) -> void;

// Prints a check failure, including rendering any user-provided message using
// a format string.
//
// Most of the parameters are passed as compile-time template strings to avoid
// runtime cost of parameter setup in optimized builds. Each of these are passed
// along to the underlying implementation to include in the final printed
// message.
//
// Any user-provided format string and values are directly passed to
// `llvm::formatv` which handles all of the formatting of output.
template <TemplateString Kind, TemplateString File, int Line,
TemplateString ConditionStr, TemplateString FormatStr, typename... Ts>
[[noreturn, gnu::cold, clang::noinline, clang::preserve_most]] auto CheckFail(
Ts&&... values) -> void {
if constexpr (llvm::StringRef(FormatStr).empty()) {
// Skip the format string rendering if empty. Note that we don't skip it
// even if there are no values as we want to have consistent handling of
// `{}`s in the format string. This case is about when there is no message
// at all, just the condition.
CheckFailImpl(Kind.c_str(), File.c_str(), Line, ConditionStr.c_str(), "");
} else {
CheckFailImpl(
Kind.c_str(), File.c_str(), Line, ConditionStr.c_str(),
llvm::formatv(FormatStr.c_str(), std::forward<Ts>(values)...).str());
}

private:
[[noreturn]] auto Done() -> void;

// Whether a separator should be printed if << is used again.
bool separator_ = false;

std::string buffer_str_;
llvm::raw_string_ostream buffer_;
};
}

} // namespace Carbon::Internal

// Raw exiting stream. This should be used when building forms of exiting
// macros. It evaluates to a temporary `ExitingStream` object that can be
// manipulated, streamed into, and then will exit the program.
#define CARBON_CHECK_INTERNAL_STREAM() \
Carbon::Internal::ExitingStream::Helper() | Carbon::Internal::ExitingStream()
// Implements check messages without any formatted values.
//
// Passes each of the provided components of the message to the template
// parameters of the check failure printing function above, including an empty
// string for the format string. Because there are multiple template arguments,
// the entire call is wrapped in parentheses.
#define CARBON_INTERNAL_CHECK_IMPL(kind, file, line, condition_str) \
(Carbon::Internal::CheckFail<kind, file, line, condition_str, "">())

// Implements check messages with a format string and potentially formatted
// values.
//
// Each of the main components is passed as a template arguments, and then any
// formatted values are passed as arguments. Because there are multiple template
// arguments, the entire call is wrapped in parentheses.
#define CARBON_INTERNAL_CHECK_IMPL_FORMAT(kind, file, line, condition_str, \
format_str, ...) \
(Carbon::Internal::CheckFail<kind, file, line, condition_str, format_str>( \
__VA_ARGS__))

// Implements the failure of a check.
//
// Collects all the metadata about the failure to be printed, such as source
// location and stringified condition, and passes those, any format string and
// formatted arguments to the correct implementation macro above.
#define CARBON_INTERNAL_CHECK(condition, ...) \
CARBON_INTERNAL_CHECK_IMPL##__VA_OPT__(_FORMAT)( \
"CHECK", __FILE__, __LINE__, #condition __VA_OPT__(, ) __VA_ARGS__)

// Implements the fatal macro.
//
// Similar to the check failure macro, but tags the message as a fatal one and
// leaves the stringified condition empty.
#define CARBON_INTERNAL_FATAL(...) \
CARBON_INTERNAL_CHECK_IMPL##__VA_OPT__(_FORMAT)( \
"FATAL", __FILE__, __LINE__, "" __VA_OPT__(, ) __VA_ARGS__)

#ifdef NDEBUG
// For `DCHECK` in optimized builds we have a dead check that we want to
// potentially "use" arguments, but otherwise have the minimal overhead. We
// avoid forming interesting format strings here so that we don't have to
// repeatedly instantiate the `Check` function above. This format string would
// be an error if actually used.
#define CARBON_INTERNAL_DEAD_DCHECK(condition, ...) \
CARBON_INTERNAL_DEAD_DCHECK_IMPL##__VA_OPT__(_FORMAT)(__VA_ARGS__)

#define CARBON_INTERNAL_DEAD_DCHECK_IMPL() \
Carbon::Internal::CheckFail<"", "", 0, "", "">()

#define CARBON_INTERNAL_DEAD_DCHECK_IMPL_FORMAT(format_str, ...) \
Carbon::Internal::CheckFail<"", "", 0, "", "">(__VA_ARGS__)
#endif

#endif // CARBON_COMMON_CHECK_INTERNAL_H_
21 changes: 15 additions & 6 deletions common/check_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,41 @@ TEST(CheckTest, CheckTrueCallbackNotUsed) {
called = true;
return "called";
};
CARBON_CHECK(true) << callback();
CARBON_CHECK(true, "{0}", callback());
EXPECT_FALSE(called);
}

TEST(CheckTest, CheckFalseMessage) {
ASSERT_DEATH({ CARBON_CHECK(false) << "msg"; },
ASSERT_DEATH({ CARBON_CHECK(false, "msg"); },
"\nCHECK failure at common/check_test.cpp:.+: false: msg\n");
}

TEST(CheckTest, CheckFalseFormattedMessage) {
const char msg[] = "msg";
std::string str = "str";
int i = 1;
ASSERT_DEATH(
{ CARBON_CHECK(false, "{0} {1} {2} {3}", msg, str, i, 0); },
"\nCHECK failure at common/check_test.cpp:.+: false: msg str 1 0\n");
}

TEST(CheckTest, CheckOutputForms) {
const char msg[] = "msg";
std::string str = "str";
int i = 1;
CARBON_CHECK(true) << msg << str << i << 0;
CARBON_CHECK(true, "{0} {1} {2} {3}", msg, str, i, 0);
}

TEST(CheckTest, Fatal) {
ASSERT_DEATH({ CARBON_FATAL() << "msg"; },
ASSERT_DEATH({ CARBON_FATAL("msg"); },
"\nFATAL failure at common/check_test.cpp:.+: msg\n");
}

TEST(CheckTest, FatalHasStackDump) {
ASSERT_DEATH({ CARBON_FATAL() << "msg"; }, "\nStack dump:\n");
ASSERT_DEATH({ CARBON_FATAL("msg"); }, "\nStack dump:\n");
}

auto FatalNoReturnRequired() -> int { CARBON_FATAL() << "msg"; }
auto FatalNoReturnRequired() -> int { CARBON_FATAL("msg"); }

TEST(ErrorTest, FatalNoReturnRequired) {
ASSERT_DEATH({ FatalNoReturnRequired(); },
Expand Down
Loading

0 comments on commit dd3397e

Please sign in to comment.