-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Dump
functions to Check, Parse, and Lex
#4669
Changes from 18 commits
b5d253a
9cc9ac4
e28d533
30bb063
19fddbc
08907b1
1526f1d
bdc9c01
b0b1cdf
91bc8a0
506a504
fb7840f
228273f
a8da68b
3ed5149
2bec55b
d5a2374
1964585
4fa1b64
c9d28da
3365c6c
baa3dda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ cc_library( | |
"//common:array_stack", | ||
"//common:check", | ||
"//common:map", | ||
"//common:ostream", | ||
"//common:vlog", | ||
"//toolchain/base:index_base", | ||
"//toolchain/base:kind_switch", | ||
|
@@ -90,6 +91,24 @@ cc_library( | |
"//toolchain/sem_ir:typed_insts", | ||
"@llvm-project//llvm:Support", | ||
], | ||
alwayslink = 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a comment why this is here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this one was missed, I also wondered if it's actually needed since dump was moved to another cc_library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I wasn't looking closely enough because I just assumed I'd already removed this set. Removed. |
||
) | ||
|
||
cc_library( | ||
name = "dump_id", | ||
srcs = ["dump_id.cpp"], | ||
copts = ["-Wno-missing-prototypes"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a comment why this is here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
deps = [ | ||
":context", | ||
"//common:check", | ||
"//common:ostream", | ||
"//toolchain/lex:dump_id", | ||
"//toolchain/lex:tokenized_buffer", | ||
"//toolchain/parse:dump_id", | ||
"//toolchain/parse:tree", | ||
"//toolchain/sem_ir:file", | ||
], | ||
alwayslink = 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a comment why this is here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
) | ||
|
||
cc_library( | ||
|
@@ -109,6 +128,7 @@ cc_library( | |
hdrs = ["check.h"], | ||
deps = [ | ||
":context", | ||
":dump_id", | ||
":impl", | ||
":interface", | ||
":pointer_dereference", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#define CARBON_TOOLCHAIN_CHECK_CONTEXT_H_ | ||
|
||
#include "common/map.h" | ||
#include "common/ostream.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer used in this PR and can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing |
||
#include "llvm/ADT/FoldingSet.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "toolchain/check/decl_introducer_state.h" | ||
|
@@ -483,10 +484,15 @@ class Context { | |
} | ||
|
||
auto sem_ir() -> SemIR::File& { return *sem_ir_; } | ||
auto sem_ir() const -> const SemIR::File& { return *sem_ir_; } | ||
|
||
auto parse_tree() -> const Parse::Tree& { return sem_ir_->parse_tree(); } | ||
auto parse_tree() const -> const Parse::Tree& { | ||
return sem_ir_->parse_tree(); | ||
} | ||
|
||
auto tokens() -> const Lex::TokenizedBuffer& { return parse_tree().tokens(); } | ||
auto tokens() const -> const Lex::TokenizedBuffer& { | ||
return parse_tree().tokens(); | ||
} | ||
|
||
auto node_stack() -> NodeStack& { return node_stack_; } | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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/lex/dump_id.h" | ||
|
||
#include "common/check.h" | ||
#include "common/ostream.h" | ||
#include "toolchain/check/context.h" | ||
#include "toolchain/lex/tokenized_buffer.h" | ||
#include "toolchain/parse/dump_id.h" | ||
#include "toolchain/parse/tree.h" | ||
#include "toolchain/sem_ir/file.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
static auto DumpIdImpl(const Context& context, SemIR::LocId loc_id) -> void { | ||
if (!loc_id.is_valid()) { | ||
llvm::errs() << "LocId(invalid)"; | ||
return; | ||
} | ||
|
||
if (loc_id.is_node_id()) { | ||
auto token = context.parse_tree().node_token(loc_id.node_id()); | ||
auto line = context.tokens().GetLineNumber(token); | ||
auto col = context.tokens().GetColumnNumber(token); | ||
const char* implicit = loc_id.is_implicit() ? " implicit" : ""; | ||
llvm::errs() << "LocId("; | ||
llvm::errs().write_escaped(context.sem_ir().filename()); | ||
llvm::errs() << ":" << line << ":" << col << implicit << ")"; | ||
} else { | ||
CARBON_CHECK(loc_id.is_import_ir_inst_id()); | ||
|
||
auto import_ir_id = context.sem_ir() | ||
.import_ir_insts() | ||
.Get(loc_id.import_ir_inst_id()) | ||
.ir_id; | ||
const auto* import_file = | ||
context.sem_ir().import_irs().Get(import_ir_id).sem_ir; | ||
llvm::errs() << "LocId(import from \""; | ||
llvm::errs().write_escaped(import_file->filename()); | ||
llvm::errs() << "\")"; | ||
} | ||
} | ||
|
||
// A set of DumpId() overloads that dump an object to stderr, useful for | ||
// calling inside a debugger. | ||
LLVM_DUMP_METHOD auto DumpId(const Context& context, Lex::TokenIndex token) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR description says "this provides Check::Dump(context, arg) and similar" which is a typo (no Id) that we keep making. And we'll want to dump things other than Ids (like PendingBlock). So maybe we should drop the Id suffix. This is also fine for now, but I'm not happy with the naming forever yet. I was also talking with Geoff about maybe renaming "Dump" in Printable (Dump->Print and Print->PrintTo?) or something to avoid the name collisions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLVM typically calls it https://github.com/search?q=repo%3Allvm%2Fllvm-project%20LLVM_DUMP_METHOD&type=code (ed: to be clear, I think there's significant value in consistency) You're correct the PR description doesn't match, I'll change the call though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right yeah I don't want to diverge from LLVM too much either. My full opinion state here is that I'd prefer Dump to be used for the place that includes as much info as possible. The dump() in LLVM does include comprehensive amount of info generally everything you need when debugging, similarly. I'd personally be fine with renaming or removing the Dump function from Printable. I haven't had a lot of use of calling Dump explicitly on Printable types, as they are already streamable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (to be sure, I'm assuming this'll be resolved separately) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, me too |
||
-> void { | ||
Lex::DumpId(context.tokens(), token); | ||
} | ||
LLVM_DUMP_METHOD auto DumpId(const Context& context, Parse::NodeId node_id) | ||
-> void { | ||
Parse::DumpId(context.parse_tree(), node_id); | ||
} | ||
LLVM_DUMP_METHOD auto DumpId(const Context& context, SemIR::LocId loc_id) | ||
-> void { | ||
DumpIdImpl(context, loc_id); | ||
llvm::errs() << '\n'; | ||
} | ||
|
||
} // namespace Carbon::Check | ||
|
||
#endif // NDEBUG |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,7 @@ cc_library( | |
hdrs = ["lex.h"], | ||
deps = [ | ||
":character_set", | ||
":dump_id", | ||
":helpers", | ||
":numeric_literal", | ||
":string_literal", | ||
|
@@ -198,6 +199,17 @@ cc_library( | |
], | ||
) | ||
|
||
cc_library( | ||
name = "dump_id", | ||
srcs = ["dump_id.cpp"], | ||
hdrs = ["dump_id.h"], | ||
deps = [ | ||
":tokenized_buffer", | ||
"//common:ostream", | ||
], | ||
alwayslink = 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a comment why this is here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
) | ||
|
||
cc_library( | ||
name = "token_index", | ||
hdrs = ["token_index.h"], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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/lex/dump_id.h" | ||
|
||
#include "common/ostream.h" | ||
|
||
namespace Carbon::Lex { | ||
|
||
auto DumpIdImpl(const TokenizedBuffer& tokens, TokenIndex token) -> void { | ||
if (!token.is_valid()) { | ||
llvm::errs() << "TokenIndex(invalid)"; | ||
return; | ||
} | ||
|
||
auto kind = tokens.GetKind(token); | ||
auto line = tokens.GetLineNumber(token); | ||
auto col = tokens.GetColumnNumber(token); | ||
|
||
llvm::errs() << "TokenIndex(kind: "; | ||
kind.Print(llvm::errs()); | ||
llvm::errs() << ", loc: "; | ||
llvm::errs().write_escaped(tokens.source().filename()); | ||
llvm::errs() << ":" << line << ":" << col << ")"; | ||
} | ||
|
||
LLVM_DUMP_METHOD auto DumpId(const Lex::TokenizedBuffer& tokens, | ||
Lex::TokenIndex token) -> void { | ||
DumpIdImpl(tokens, token); | ||
llvm::errs() << '\n'; | ||
} | ||
|
||
} // namespace Carbon::Lex | ||
|
||
#endif // NDEBUG |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 CARBON_TOOLCHAIN_LEX_DUMP_ID_H_ | ||
#define CARBON_TOOLCHAIN_LEX_DUMP_ID_H_ | ||
|
||
#ifndef NDEBUG | ||
|
||
#include "toolchain/lex/tokenized_buffer.h" | ||
|
||
namespace Carbon::Lex { | ||
|
||
class TokenizedBuffer; | ||
|
||
auto DumpIdImpl(const TokenizedBuffer& tokens, TokenIndex token) -> void; | ||
|
||
// A set of DumpId() overloads that dump an object to stderr, useful for | ||
// calling inside a debugger. | ||
auto DumpId(const Lex::TokenizedBuffer& tokens, Lex::TokenIndex token) -> void; | ||
|
||
} // namespace Carbon::Lex | ||
|
||
#endif // NDEBUG | ||
|
||
#endif // CARBON_TOOLCHAIN_LEX_DUMP_ID_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ cc_library( | |
], | ||
deps = [ | ||
":context", | ||
":dump_id", | ||
":node_kind", | ||
":state", | ||
":tree", | ||
|
@@ -102,6 +103,18 @@ cc_library( | |
], | ||
) | ||
|
||
cc_library( | ||
name = "dump_id", | ||
srcs = ["dump_id.cpp"], | ||
hdrs = ["dump_id.h"], | ||
deps = [ | ||
":tree", | ||
"//common:ostream", | ||
"//toolchain/lex:dump_id", | ||
], | ||
alwayslink = 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment why this is here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
) | ||
|
||
cc_library( | ||
name = "state", | ||
srcs = ["state.cpp"], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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_id.h" | ||
|
||
#include "common/ostream.h" | ||
#include "toolchain/lex/dump_id.h" | ||
|
||
namespace Carbon::Parse { | ||
|
||
auto DumpIdImpl(const Tree& tree, NodeId node_id) -> void { | ||
if (!node_id.is_valid()) { | ||
llvm::errs() << "NodeId(invalid)"; | ||
return; | ||
} | ||
|
||
auto kind = tree.node_kind(node_id); | ||
auto token = tree.node_token(node_id); | ||
|
||
llvm::errs() << "NodeId(kind: "; | ||
kind.Print(llvm::errs()); | ||
llvm::errs() << ", token: "; | ||
Lex::DumpIdImpl(tree.tokens(), token); | ||
llvm::errs() << ")"; | ||
} | ||
|
||
// A set of DumpId() overloads that dump an object to stderr, useful for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Lex, this is moved to the header, should this comment move to the header here too? Or can we make it consistent either way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a more complete comment to each header. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also a mention in docs/ |
||
// calling inside a debugger. | ||
LLVM_DUMP_METHOD auto DumpId(const Parse::Tree& tree, Lex::TokenIndex token) | ||
-> void { | ||
Lex::DumpId(tree.tokens(), token); | ||
} | ||
|
||
LLVM_DUMP_METHOD auto DumpId(const Parse::Tree& tree, Parse::NodeId node_id) | ||
-> void { | ||
DumpIdImpl(tree, node_id); | ||
llvm::errs() << '\n'; | ||
} | ||
|
||
} // namespace Carbon::Parse | ||
|
||
#endif // NDEBUG |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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 CARBON_TOOLCHAIN_PARSE_DUMP_ID_H_ | ||
#define CARBON_TOOLCHAIN_PARSE_DUMP_ID_H_ | ||
|
||
#ifndef NDEBUG | ||
|
||
#include "toolchain/parse/tree.h" | ||
|
||
namespace Carbon::Parse { | ||
|
||
auto DumpIdImpl(const Tree& tree, NodeId node_id) -> void; | ||
|
||
auto DumpId(const Tree& tree, Lex::TokenIndex token) -> void; | ||
|
||
auto DumpId(const Tree& tree, NodeId node_id) -> void; | ||
|
||
} // namespace Carbon::Parse | ||
|
||
#endif // NDEBUG | ||
|
||
#endif // CARBON_TOOLCHAIN_PARSE_DUMP_ID_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer used in this PR and can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing