Add import context for locations. (#3807)

As discussed around #3792, identify the import a diagnostic message came
from prior to the diagnostic message itself. This occurs during location
translation so that the logic can be central.

I'd considered associating the parse node with ImportRef instructions,
but I realized about halfway through that because I need to store the
ImportDirectiveId on the ImportIR for cross-package imports, it's there
for use in location translation without extra work. That saves a fair
amount of stringing it through declarations, as well as an oddity where
ImportRef instructions would have a node that didn't really represent
them.
This commit is contained in:
Jon Ross-Perkins
2024-03-27 22:22:15 +00:00
committed by GitHub
parent 2a6c5255fb
commit 6c458ffe7e
29 changed files with 263 additions and 130 deletions
+4 -3
View File
@@ -346,7 +346,7 @@ auto TokenIterator::Print(llvm::raw_ostream& output) const -> void {
}
auto TokenizedBuffer::SourceBufferDiagnosticConverter::ConvertLocation(
const char* loc) const -> DiagnosticLocation {
const char* loc, ContextFnT /*context_fn*/) const -> DiagnosticLocation {
CARBON_CHECK(StringRefContainsPointer(buffer_->source_->text(), loc))
<< "location not within buffer";
int64_t offset = loc - buffer_->source_->text().begin();
@@ -390,7 +390,8 @@ auto TokenizedBuffer::SourceBufferDiagnosticConverter::ConvertLocation(
.column_number = column_number + 1};
}
auto TokenDiagnosticConverter::ConvertLocation(TokenIndex token) const
auto TokenDiagnosticConverter::ConvertLocation(TokenIndex token,
ContextFnT context_fn) const
-> DiagnosticLocation {
// Map the token location into a position within the source buffer.
const auto& token_info = buffer_->GetTokenInfo(token);
@@ -403,7 +404,7 @@ auto TokenDiagnosticConverter::ConvertLocation(TokenIndex token) const
// is a recovery token that doesn't correspond to the original source?
DiagnosticLocation loc =
TokenizedBuffer::SourceBufferDiagnosticConverter(buffer_).ConvertLocation(
token_start);
token_start, context_fn);
loc.length = buffer_->GetTokenText(token).size();
return loc;
}