Skip to content

Commit 0d11d29

Browse files
committed
add-dump-content
1 parent 130a47c commit 0d11d29

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

toolchain/check/dump_id.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "toolchain/check/dump_id.h"
66

7+
#include "common/check.h"
78
#include "llvm/Support/raw_ostream.h"
89
#include "toolchain/check/context.h"
910
#include "toolchain/lex/tokenized_buffer.h"
@@ -13,14 +14,21 @@
1314
namespace Carbon::Check::DumpIdOverloads {
1415

1516
auto DumpId(SemIR::LocId loc_id, const Context& context) -> void {
17+
if (!loc_id.is_valid()) {
18+
llvm::errs() << "LocId(invalid)";
19+
return;
20+
}
21+
1622
if (loc_id.is_node_id()) {
1723
auto token = context.parse_tree().node_token(loc_id.node_id());
1824
auto line = context.tokens().GetLineNumber(token);
1925
auto col = context.tokens().GetColumnNumber(token);
2026
const char* implicit = loc_id.is_implicit() ? " implicit" : "";
2127
llvm::errs() << "LocId(line: " << line << ", col: " << col << implicit
2228
<< ")";
23-
} else if (loc_id.is_import_ir_inst_id()) {
29+
} else {
30+
CARBON_CHECK(loc_id.is_import_ir_inst_id());
31+
2432
auto import_ir_id = context.sem_ir()
2533
.import_ir_insts()
2634
.Get(loc_id.import_ir_inst_id())
@@ -30,8 +38,6 @@ auto DumpId(SemIR::LocId loc_id, const Context& context) -> void {
3038
llvm::errs() << "LocId(import from \"";
3139
llvm::errs().write_escaped(import_file->filename());
3240
llvm::errs() << "\")";
33-
} else {
34-
llvm::errs() << "LocId(invalid)";
3541
}
3642
}
3743

toolchain/lex/dump_id.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@
99

1010
namespace Carbon::Lex::DumpIdOverloads {
1111

12-
auto DumpId(TokenIndex /*token*/, const TokenizedBuffer& /*buffer*/) -> void {
13-
llvm::errs() << "TokenIndex(?)";
12+
auto DumpId(TokenIndex token, const TokenizedBuffer& buffer) -> void {
13+
if (!token.is_valid()) {
14+
llvm::errs() << "TokenIndex(invalid)";
15+
return;
16+
}
17+
18+
auto kind = buffer.GetKind(token);
19+
auto line = buffer.GetLineNumber(token);
20+
auto col = buffer.GetColumnNumber(token);
21+
22+
llvm::errs() << "TokenIndex(kind: ";
23+
kind.Print(llvm::errs());
24+
llvm::errs() << ", line: " << line << ", col: " << col << ")";
1425
}
1526

1627
} // namespace Carbon::Lex::DumpIdOverloads

toolchain/parse/dump_id.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
#include "toolchain/parse/dump_id.h"
66

77
#include "llvm/Support/raw_ostream.h"
8+
#include "toolchain/lex/dump_id.h"
89
#include "toolchain/parse/tree.h"
910

1011
namespace Carbon::Parse::DumpIdOverloads {
1112

1213
auto DumpId(NodeId node_id, const Tree& tree) -> void {
1314
if (!node_id.is_valid()) {
1415
llvm::errs() << "NodeId(invalid)";
16+
return;
1517
}
1618

17-
llvm::errs() << "NodeId(";
19+
auto kind = tree.node_kind(node_id);
1820
auto token = tree.node_token(node_id);
19-
llvm::errs() << "token = ";
20-
tree.DumpId(token);
21-
llvm::errs() << ")";
2221

23-
llvm::errs() << "A node id";
22+
llvm::errs() << "NodeId(kind: ";
23+
kind.Print(llvm::errs());
24+
llvm::errs() << ", token: ";
25+
Lex::DumpIdOverloads::DumpId(token, tree.tokens());
26+
llvm::errs() << ")";
2427
}
2528

2629
} // namespace Carbon::Parse::DumpIdOverloads

toolchain/parse/dump_id.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DumpIdMethods {
2929

3030
public:
3131
LLVM_DUMP_METHOD auto DumpId(Lex::TokenIndex token) const -> void {
32-
static_cast<const Tree&>(*this).tokens_->DumpId(token);
32+
static_cast<const Tree&>(*this).tokens().DumpId(token);
3333
}
3434
LLVM_DUMP_METHOD auto DumpId(NodeId node_id) const -> void {
3535
DumpIdOverloads::DumpId(node_id, static_cast<const Tree&>(*this));

toolchain/parse/tree.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ struct File;
8181
// The DumpIdMethods parent class provides a `DumpId(x)` method for many types
8282
// across the toolchain.
8383
class Tree : public Printable<Tree>, public DumpIdMethods<Tree> {
84-
friend class DumpIdMethods<Tree>; // Access to `tokens_`.
85-
8684
public:
8785
class PostorderIterator;
8886

@@ -137,6 +135,8 @@ class Tree : public Printable<Tree>, public DumpIdMethods<Tree> {
137135
// Returns the token the given parse tree node models.
138136
auto node_token(NodeId n) const -> Lex::TokenIndex;
139137

138+
auto tokens() const -> const Lex::TokenizedBuffer& { return *tokens_; }
139+
140140
// Returns whether this node is a valid node of the specified type.
141141
template <typename T>
142142
auto IsValid(NodeId node_id) const -> bool {

0 commit comments

Comments
 (0)