Rewrite Dump calls to use std::string returns (#5195)

Thought this might be interesting for you to allow more continuous
stream use. Also eliminates the need for `DumpNoNewline`.

```
expr Dump(context, complete_type_id)
(std::string) $0 = "type(inst1553): <builtin i32>; {kind: IntType, arg0: signed, arg1: inst1508, type: type(TypeType)}"
complete_type_id.Dump()
(std::string) $1 = "type(inst1553)"
expr Dump(context, specific_id)
(std::string) $2 = "specific166: {generic: generic0, args: inst_block772}"
expr Dump(context, query_self_const_id)
(std::string) $3 = "concrete_constant(inst1510): {kind: ClassType, arg0: class0, arg1: specific166, type: type(TypeType)}"
expr Dump(context, MakeFacetTypeId(arg))
(std::string) $4 = "facet_type22: {impls interface: interface10}
  - interface10: {name: name26, parent_scope: name_scope0} `BitAnd`
complete: complete_facet_type22
  - interface10: {name: name26, parent_scope: name_scope0} `BitAnd` (to impl)"
```

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
This commit is contained in:
Jon Ross-Perkins
2025-03-27 21:17:54 +00:00
committed by GitHub
co-authored by Dana Jansens
parent c7a338be59
commit 3ae62f8130
13 changed files with 391 additions and 379 deletions
+13 -16
View File
@@ -6,32 +6,29 @@
#include "toolchain/parse/dump.h"
#include "common/ostream.h"
#include "common/raw_string_ostream.h"
#include "toolchain/lex/dump.h"
namespace Carbon::Parse {
auto DumpNoNewline(const Tree& tree, NodeId node_id) -> void {
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()) {
llvm::errs() << "NodeId(invalid)";
return;
out << "NodeId(<none>)";
return out.TakeStr();
}
auto kind = tree.node_kind(node_id);
auto token = tree.node_token(node_id);
llvm::errs() << "NodeId(kind: " << kind << ", token: ";
Lex::DumpNoNewline(tree.tokens(), token);
llvm::errs() << ")";
}
LLVM_DUMP_METHOD auto Dump(const Tree& tree, Lex::TokenIndex token) -> void {
Lex::Dump(tree.tokens(), token);
}
LLVM_DUMP_METHOD auto Dump(const Tree& tree, NodeId node_id) -> void {
DumpNoNewline(tree, node_id);
llvm::errs() << '\n';
out << "NodeId(kind: " << kind
<< ", token: " << Lex::Dump(tree.tokens(), token) << ")";
return out.TakeStr();
}
} // namespace Carbon::Parse