Add a DiagnosticBuilder to support context on diagnostics. (#2490)

This is currently used once for PreviousDefinition in semantics.

This PR doesn't just add a builder, it also adds support to the emitter itself to collect notes attached to a diagnostic, and to the consumers and emitters to print all of them.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Jon Ross-Perkins
2022-12-28 10:24:19 -08:00
committed by GitHub
co-authored by Chandler Carruth
parent 149107965c
commit 6c9b7cba55
7 changed files with 136 additions and 33 deletions
@@ -108,15 +108,13 @@ auto SemanticsParseTreeHandler::BindName(ParseTree::Node name_node,
} else {
CARBON_DIAGNOSTIC(NameRedefined, Error, "Redefining {0} in the same scope.",
llvm::StringRef);
emitter_->Emit(name_node, NameRedefined, name_str);
// TODO: This should be a note and sorted with the above diagnostic.
// But that depends on more diagnostic support we currently don't have.
CARBON_DIAGNOSTIC(PreviousDefinition, Note, "Previous definition is here.");
auto prev_def_id = name_lookup_[name_id].back();
auto prev_def = semantics_->GetNode(prev_def_id);
CARBON_DIAGNOSTIC(PreviousDefinition, Error,
"Previous definition is here.");
emitter_->Emit(prev_def.parse_node(), PreviousDefinition);
emitter_->Build(name_node, NameRedefined, name_str)
.Note(prev_def.parse_node(), PreviousDefinition)
.Emit();
}
}