-
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 DumpId() methods to Check::Context, Parse::Tree, Lex::TokenizedBuffer #4620
Closed
Closed
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b5d253a
Add DumpId() methods to Check::Context, Parse::Tree, Lex::TokenizedBu…
danakj 9cc9ac4
add-missing-files
danakj e28d533
parse-tree
danakj 30bb063
iwyu
danakj 19fddbc
dump import locations
danakj 08907b1
dump-methods-in-base-classes
danakj 1526f1d
add-parse-and-overload-of-lex
danakj bdc9c01
use-if-constexpr
danakj b0b1cdf
simplify
danakj 91bc8a0
simply
danakj 506a504
add-dump-content
danakj fb7840f
review-feedback
danakj 228273f
write-newline
danakj a8da68b
Merge branch 'trunk' into dump
danakj 3ed5149
dont-avoid-include-ostream
danakj 2bec55b
remove-base-class
danakj d5a2374
Merge remote-tracking branch 'refs/remotes/origin/trunk' into dump
danakj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 | ||
|
||
#include "toolchain/check/dump_id.h" | ||
|
||
#include "common/check.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "toolchain/check/context.h" | ||
#include "toolchain/lex/tokenized_buffer.h" | ||
#include "toolchain/parse/tree.h" | ||
#include "toolchain/sem_ir/file.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
auto DumpIdImpl(SemIR::LocId loc_id, const Context& context) -> 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() << "\")"; | ||
} | ||
} | ||
|
||
template <> | ||
auto DumpIdMethods<Context>::WriteNewline() const -> void { | ||
llvm::errs() << "\n"; | ||
} | ||
|
||
} // namespace Carbon::Check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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_CHECK_DUMP_ID_H_ | ||
#define CARBON_TOOLCHAIN_CHECK_DUMP_ID_H_ | ||
|
||
#include "toolchain/lex/token_kind.h" | ||
#include "toolchain/lex/tokenized_buffer.h" | ||
#include "toolchain/parse/tree.h" | ||
#include "toolchain/sem_ir/ids.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
class Context; | ||
|
||
auto DumpIdImpl(SemIR::LocId loc_id, const Context& context) -> void; | ||
|
||
// A set of DumpId() overloads that dump an object to stderr, useful for calling | ||
// inside a debugger. These are all exposed as part of the `Check::Context` API. | ||
// | ||
// This class is inherited by `Check::Context`, which provides itself as the | ||
// template parameter. The methods are provided here instead of on `Context` | ||
// directly to avoid cluttering the `Context` class with overloads for every | ||
// dumpable id type. | ||
template <class Context> | ||
class DumpIdMethods { | ||
static_assert(std::same_as<Context, ::Carbon::Check::Context>); | ||
|
||
public: | ||
LLVM_DUMP_METHOD auto DumpId(Lex::TokenIndex token) const -> void { | ||
static_cast<const Context&>(*this).tokens().DumpId(token); | ||
} | ||
LLVM_DUMP_METHOD auto DumpId(Parse::NodeId node_id) const -> void { | ||
static_cast<const Context&>(*this).parse_tree().DumpId(node_id); | ||
} | ||
LLVM_DUMP_METHOD auto DumpId(SemIR::LocId loc_id) const -> void { | ||
DumpIdImpl(loc_id, static_cast<const Context&>(*this)); | ||
WriteNewline(); | ||
} | ||
|
||
private: | ||
// Helper to keep stream includes out of the header file. | ||
auto WriteNewline() const -> void; | ||
geoffromer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
} // namespace Carbon::Check | ||
|
||
#endif // CARBON_TOOLCHAIN_CHECK_DUMP_ID_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 | ||
|
||
#include "toolchain/lex/dump_id.h" | ||
|
||
#include "llvm/Support/raw_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. BTW, to the point about common/ostream.h -- please include it instead of llvm/Support/raw_ostream.h. As noted in common/ostream.h, I'm trying to avoid having people include raw_ostream.h directly, in order to get more consistent behavior. |
||
#include "toolchain/lex/tokenized_buffer.h" | ||
|
||
namespace Carbon::Lex { | ||
|
||
auto DumpIdImpl(TokenIndex token, const TokenizedBuffer& buffer) -> void { | ||
if (!token.is_valid()) { | ||
llvm::errs() << "TokenIndex(invalid)"; | ||
return; | ||
} | ||
|
||
auto kind = buffer.GetKind(token); | ||
auto line = buffer.GetLineNumber(token); | ||
auto col = buffer.GetColumnNumber(token); | ||
|
||
llvm::errs() << "TokenIndex(kind: "; | ||
kind.Print(llvm::errs()); | ||
llvm::errs() << ", loc: "; | ||
llvm::errs().write_escaped(buffer.source().filename()); | ||
llvm::errs() << ":" << line << ":" << col << ")"; | ||
} | ||
|
||
template <> | ||
auto DumpIdMethods<TokenizedBuffer>::WriteNewline() const -> void { | ||
llvm::errs() << "\n"; | ||
} | ||
|
||
} // namespace Carbon::Lex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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_ | ||
|
||
#include "toolchain/lex/token_index.h" | ||
|
||
namespace Carbon::Lex { | ||
|
||
class TokenizedBuffer; | ||
|
||
auto DumpIdImpl(TokenIndex token, const TokenizedBuffer& buffer) -> void; | ||
|
||
// A set of DumpId() overloads that dump an object to stderr, useful for calling | ||
// inside a debugger. These are all exposed as part of the | ||
// `Lex::TokenizedBuffer` API. | ||
// | ||
// This class is inherited by `Lex::TokenizedBuffer`, which provides itself as | ||
// the template parameter. The methods are provided here instead of on | ||
// `TokenizedBuffer` directly to avoid cluttering the `TokenizedBuffer` class | ||
// with overloads for every dumpable id type. | ||
template <class TokenizedBuffer> | ||
class DumpIdMethods { | ||
static_assert(std::same_as<TokenizedBuffer, ::Carbon::Lex::TokenizedBuffer>); | ||
|
||
public: | ||
LLVM_DUMP_METHOD auto DumpId(TokenIndex token) const -> void { | ||
DumpIdImpl(token, static_cast<const TokenizedBuffer&>(*this)); | ||
WriteNewline(); | ||
} | ||
|
||
private: | ||
// Helper to keep stream includes out of the header file. | ||
auto WriteNewline() const -> void; | ||
}; | ||
|
||
} // namespace Carbon::Lex | ||
|
||
#endif // CARBON_TOOLCHAIN_LEX_DUMP_ID_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 | ||
|
||
#include "toolchain/parse/dump_id.h" | ||
|
||
#include "llvm/Support/raw_ostream.h" | ||
#include "toolchain/lex/dump_id.h" | ||
#include "toolchain/parse/tree.h" | ||
|
||
namespace Carbon::Parse { | ||
|
||
auto DumpIdImpl(NodeId node_id, const Tree& tree) -> 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(token, tree.tokens()); | ||
llvm::errs() << ")"; | ||
} | ||
|
||
template <> | ||
auto DumpIdMethods<Tree>::WriteNewline() const -> void { | ||
llvm::errs() << "\n"; | ||
} | ||
|
||
} // namespace Carbon::Parse |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think this is a good tradeoff. The clutter of making these methods of
Context
seems minimal compared to the complexity associated with the CRTP mixin (especially given how many methodsContext
already has).