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:
Jon Ross-Perkins
2025-05-02 18:47:02 +00:00
committed by GitHub
parent 34a9e24920
commit 1f268b5d8b
7 changed files with 48 additions and 56 deletions
+6 -9
View File
@@ -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;