mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 06:51:05 +01:00
Clean up handling of incomplete line locations. (#3011)
Building on #3010, the handling of incomplete lines seems like it can be straightened out. Doing this separately because it seemed better to demonstrate tests aren't affected by the change.
This commit is contained in:
@@ -84,9 +84,9 @@ class TokenizedBuffer::Lexer {
|
||||
|
||||
Lexer(TokenizedBuffer& buffer, DiagnosticConsumer& consumer)
|
||||
: buffer_(&buffer),
|
||||
translator_(&buffer, ¤t_column_),
|
||||
translator_(&buffer),
|
||||
emitter_(translator_, consumer),
|
||||
token_translator_(&buffer, ¤t_column_),
|
||||
token_translator_(&buffer),
|
||||
token_emitter_(token_translator_, consumer),
|
||||
current_line_(buffer.AddLine(LineInfo(0))),
|
||||
current_line_info_(&buffer.GetLineInfo(current_line_)) {}
|
||||
@@ -897,8 +897,6 @@ auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation(
|
||||
const auto* line_it = std::partition_point(
|
||||
buffer_->line_infos_.begin(), buffer_->line_infos_.end(),
|
||||
[offset](const LineInfo& line) { return line.start <= offset; });
|
||||
bool incomplete_line_info = last_line_lexed_to_column_ != nullptr &&
|
||||
line_it == buffer_->line_infos_.end();
|
||||
|
||||
// Step back one line to find the line containing the given position.
|
||||
CARBON_CHECK(line_it != buffer_->line_infos_.begin())
|
||||
@@ -907,33 +905,23 @@ auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation(
|
||||
int line_number = line_it - buffer_->line_infos_.begin();
|
||||
int column_number = offset - line_it->start;
|
||||
|
||||
llvm::StringRef line;
|
||||
|
||||
// We might still be lexing the last line. If so, check to see if there are
|
||||
// any newline characters between the position we've finished lexing up to
|
||||
// and the given location.
|
||||
if (incomplete_line_info && column_number > *last_line_lexed_to_column_) {
|
||||
column_number = *last_line_lexed_to_column_;
|
||||
int64_t start = line_it->start;
|
||||
for (int64_t i = line_it->start + *last_line_lexed_to_column_; i != offset;
|
||||
++i) {
|
||||
if (buffer_->source_->text()[i] == '\n') {
|
||||
start = i;
|
||||
++line_number;
|
||||
column_number = 0;
|
||||
} else {
|
||||
++column_number;
|
||||
}
|
||||
// Start by grabbing the line from the buffer. If the line isn't fully lexed,
|
||||
// the length will be npos and the line will be grabbed from the known start
|
||||
// to the end of the buffer; we'll then adjust the length.
|
||||
llvm::StringRef line =
|
||||
buffer_->source_->text().substr(line_it->start, line_it->length);
|
||||
if (line_it->length == static_cast<int32_t>(llvm::StringRef::npos)) {
|
||||
CARBON_CHECK(line.take_front(column_number).count('\n') == 0)
|
||||
<< "Currently we assume no unlexed newlines prior to the error column, "
|
||||
"but there was one when erroring at "
|
||||
<< buffer_->source_->filename() << ":" << line_number << ":"
|
||||
<< column_number;
|
||||
// Look for the next newline since we don't know the length. We can start at
|
||||
// the column because prior newlines will have been lexed.
|
||||
auto end_newline_pos = line.find('\n', column_number);
|
||||
if (end_newline_pos != llvm::StringRef::npos) {
|
||||
line = line.take_front(end_newline_pos);
|
||||
}
|
||||
line = buffer_->source_->text().substr(start).take_until(
|
||||
[](char c) { return c == '\n'; });
|
||||
} else if (line_it->length < 0) {
|
||||
line =
|
||||
buffer_->source_->text().substr(line_it->start).take_until([](char c) {
|
||||
return c == '\n';
|
||||
});
|
||||
} else {
|
||||
line = buffer_->source_->text().substr(line_it->start, line_it->length);
|
||||
}
|
||||
|
||||
return {.file_name = buffer_->source_->filename(),
|
||||
@@ -953,8 +941,7 @@ auto TokenizedBuffer::TokenLocationTranslator::GetLocation(Token token)
|
||||
// Find the corresponding file location.
|
||||
// TODO: Should we somehow indicate in the diagnostic location if this token
|
||||
// is a recovery token that doesn't correspond to the original source?
|
||||
return SourceBufferLocationTranslator(buffer_, last_line_lexed_to_column_)
|
||||
.GetLocation(token_start);
|
||||
return SourceBufferLocationTranslator(buffer_).GetLocation(token_start);
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user