mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +01:00
Because this is an `__attribute__((used))` method in a templated base class it forces a _huge_ amount of template instantiation in every translation unit. Often this was just printing the members of the type, which is still useful in some cases (such as test output), but adds no value in the debugger. A more successful pattern for dumping has been namespace level functions, and particularly static ones that more transparently don't expand the non-debugger API surface. Add the few missing functions there that cover `Printable` types with more interesting contents. For several of these, it just gives us a "dump the whole thing" function as a compliment to "dump this entity in the thing". These probably aren't especially high value, but moving them here they become cheap, so I've left them in. For a couple, this expands the rich dumping support of SemIR constructs, which should be substantially more useful than the previous `Dump` behavior. This reduces `check` cumulative object file size by another 14%. Assisted-by: Antigravity with Gemini
54 lines
1.3 KiB
C++
54 lines
1.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 NDEBUG
|
|
|
|
#include "toolchain/parse/dump.h"
|
|
|
|
#include <string>
|
|
|
|
#include "common/raw_string_ostream.h"
|
|
#include "toolchain/lex/dump.h"
|
|
#include "toolchain/parse/context.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
static LLVM_DUMP_METHOD auto Dump(const Tree& tree) -> std::string {
|
|
return PrintToString(tree);
|
|
}
|
|
|
|
LLVM_DUMP_METHOD auto Dump(const Tree& tree, Lex::TokenIndex token)
|
|
-> std::string {
|
|
return Lex::Dump(tree.tokens(), token);
|
|
}
|
|
|
|
LLVM_DUMP_METHOD auto Dump(const Tree& tree, NodeId node_id) -> std::string {
|
|
RawStringOstream out;
|
|
if (!node_id.has_value()) {
|
|
out << "NodeId(<none>)";
|
|
return out.TakeStr();
|
|
}
|
|
|
|
auto kind = tree.node_kind(node_id);
|
|
auto token = tree.node_token(node_id);
|
|
|
|
out << "NodeId(kind: " << kind
|
|
<< ", token: " << Lex::Dump(tree.tokens(), token) << ")";
|
|
return out.TakeStr();
|
|
}
|
|
|
|
static LLVM_DUMP_METHOD auto Dump(const Context& context, Lex::TokenIndex token)
|
|
-> std::string {
|
|
return Dump(context.tree(), token);
|
|
}
|
|
|
|
static LLVM_DUMP_METHOD auto Dump(const Context& context, NodeId node_id)
|
|
-> std::string {
|
|
return Dump(context.tree(), node_id);
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|
|
|
|
#endif // NDEBUG
|