mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:34:45 +01:00
Consolidate token-related range handling to one struct (#5399)
This consolidates Lex::TokenizedBuffer::DumpSemIRRange and
Parse::TreeAndSubtrees::TokenRange into a single InclusiveTokenRange,
also making the OverlapsWithDumpSemIRRange function take the new struct.
I considered switching to `llvm::iterator_range<Lex::TokenIterator>`,
but we often want to see if the range is size one. Using `TokenIterator`
just looked like it'd add a bunch of offsetting to make it work; I view
that as low-value overhead.
For example:
```
Lex::InclusiveTokenRange token_range = GetSubtreeTokenRange(node_id);
auto begin_loc = tree_->tokens().TokenToDiagnosticLoc(token_range.begin);
if (token_range.begin == token_range.end) {
return begin_loc;
}
auto end_loc = tree_->tokens().TokenToDiagnosticLoc(token_range.end);
```
would become:
```
llvm::iterator_range<Lex::TokenIterator> token_range = GetSubtreeTokenRange(node_id);
auto begin_loc = tree_->tokens().TokenToDiagnosticLoc(*token_range.begin());
if (token_range.begin() + 1 == token_range.end()) {
return begin_loc;
}
auto end_loc = tree_->tokens().TokenToDiagnosticLoc(*(token_range.end() - 1));
```
So I'm keeping the bespoke struct.
This commit is contained in:
@@ -440,18 +440,15 @@ auto TokenizedBuffer::TokenToDiagnosticLoc(TokenIndex token) const
|
||||
return converted;
|
||||
}
|
||||
|
||||
auto TokenizedBuffer::OverlapsWithDumpSemIRRange(TokenIndex begin,
|
||||
TokenIndex inclusive_end) const
|
||||
-> bool {
|
||||
if (dump_sem_ir_ranges_.empty()) {
|
||||
return true;
|
||||
}
|
||||
auto TokenizedBuffer::OverlapsWithDumpSemIRRange(
|
||||
Lex::InclusiveTokenRange range) const -> bool {
|
||||
CARBON_CHECK(!dump_sem_ir_ranges_.empty());
|
||||
|
||||
// Ranges are ordered, so we can decide overlap as soon as we find a range
|
||||
// that ends after `begin`.
|
||||
for (auto range : dump_sem_ir_ranges_) {
|
||||
if (range.end > begin) {
|
||||
return range.begin <= inclusive_end;
|
||||
for (auto dump_range : dump_sem_ir_ranges_) {
|
||||
if (dump_range.end >= range.begin) {
|
||||
return dump_range.begin <= range.end;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user