Switch to PrettyStackTrace for CHECK/FATAL (#2373)

At present, CHECK/FATAL print their own stack trace. This switches to just using std::abort for the stack trace, as well as the CHECK printing more completely.

This has a few consequences:

1) I'm now buffering the FATAL strings in order to print it later.
2) We now print the bug report message and program arguments on failure. This is part of pretty printing and was elided before.
3) We can now have pretty printing on FATAL, e.g. to show the stacks we're building in the parser.
This commit is contained in:
Jon Ross-Perkins authored and GitHub committed 2022-11-04 14:59:39 -07:00
1 parent cef93fba5e
commit 8c354ca232
7 files changed
+94 -36

No files matched your search

+25
View File
@@ -9,6 +9,7 @@
#include "common/check.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "toolchain/lexer/token_kind.h"
#include "toolchain/lexer/tokenized_buffer.h"
#include "toolchain/parser/parse_node_kind.h"
@@ -16,6 +17,26 @@
namespace Carbon {
class Parser2::PrettyStackTraceParseState : public llvm::PrettyStackTraceEntry {
public:
explicit PrettyStackTraceParseState(const Parser2* parser)
: parser_(parser) {}
~PrettyStackTraceParseState() override = default;
auto print(llvm::raw_ostream& output) const -> void override {
output << "Parser stack:\n";
for (int i = 0; i < static_cast<int>(parser_->state_stack_.size()); ++i) {
const auto& entry = parser_->state_stack_[i];
output << "\t" << i << ".\t" << entry.state << " @ " << entry.start_token
<< ":" << parser_->tokens_.GetKind(entry.start_token).Name()
<< "\n";
}
}
private:
const Parser2* parser_;
};
Parser2::Parser2(ParseTree& tree_arg, TokenizedBuffer& tokens_arg,
TokenDiagnosticEmitter& emitter)
: tree_(tree_arg),
@@ -71,6 +92,10 @@ auto Parser2::ConsumeIf(TokenKind kind)
}
auto Parser2::Parse() -> void {
#ifndef NDEBUG
PrettyStackTraceParseState pretty_stack(this);
#endif
PushState(ParserState::Declaration());
while (!state_stack_.empty()) {
switch (state_stack_.back().state) {