@@ -19,8 +19,12 @@ namespace Carbon::Check {
19
19
auto DiagnosticEmitter::ConvertLoc (LocIdForDiagnostics loc_id,
20
20
ContextFnT context_fn) const
21
21
-> Diagnostics::ConvertedLoc {
22
- auto converted =
23
- ConvertLocImpl (loc_id.loc_id (), loc_id.is_token_only (), context_fn);
22
+ auto [imports, converted] = loc_converter_.ConvertWithImports (
23
+ loc_id.loc_id (), loc_id.is_token_only ());
24
+ for (const auto & import : imports) {
25
+ CARBON_DIAGNOSTIC (InImport, LocationInfo, " in import" );
26
+ context_fn (import .loc , InImport);
27
+ }
24
28
25
29
// Use the token when possible, but -1 is the default value.
26
30
auto last_offset = -1 ;
@@ -40,73 +44,6 @@ auto DiagnosticEmitter::ConvertLoc(LocIdForDiagnostics loc_id,
40
44
return converted;
41
45
}
42
46
43
- auto DiagnosticEmitter::ConvertLocImpl (SemIR::LocId loc_id, bool is_token_only,
44
- ContextFnT context_fn) const
45
- -> Diagnostics::ConvertedLoc {
46
- llvm::SmallVector<SemIR::AbsoluteNodeId> absolute_node_ids =
47
- SemIR::GetAbsoluteNodeId (sem_ir_, loc_id);
48
-
49
- auto final_node_id = absolute_node_ids.pop_back_val ();
50
- for (const auto & absolute_node_id : absolute_node_ids) {
51
- if (!absolute_node_id.node_id ().has_value ()) {
52
- // TODO: Add an "In implicit import of prelude." note for the case where
53
- // we don't have a location.
54
- continue ;
55
- }
56
- // TODO: Include the name of the imported library in the diagnostic.
57
- auto diag_loc =
58
- ConvertLocInFile (absolute_node_id, is_token_only, context_fn);
59
- AddInImport (diag_loc.loc , context_fn);
60
- }
61
-
62
- return ConvertLocInFile (final_node_id, is_token_only, context_fn);
63
- }
64
-
65
- auto DiagnosticEmitter::ConvertLocInFile (SemIR::AbsoluteNodeId absolute_node_id,
66
- bool token_only,
67
- ContextFnT context_fn) const
68
- -> Diagnostics::ConvertedLoc {
69
- if (absolute_node_id.check_ir_id () == SemIR::CheckIRId::Cpp) {
70
- // Special handling of Clang source locations.
71
- CARBON_CHECK (sem_ir_->import_cpps ().size () > 0 );
72
- // TODO: Use information on the specific C++ import extract from Clang error
73
- // message and propagated here instead of using first C++ import
74
- // arbitrarily.
75
- Parse::NodeId import_node_id =
76
- sem_ir_->import_cpps ().values ().begin ()->node_id ;
77
- AddInImport (ConvertLocInCarbonFile (sem_ir_->check_ir_id (), import_node_id,
78
- /* token_only=*/ false )
79
- .loc ,
80
- context_fn);
81
-
82
- clang::SourceLocation clang_loc = sem_ir_->clang_source_locs ().Get (
83
- absolute_node_id.clang_source_loc_id ());
84
-
85
- CARBON_CHECK (sem_ir_->cpp_ast ());
86
- clang::PresumedLoc presumed_loc =
87
- sem_ir_->cpp_ast ()->getSourceManager ().getPresumedLoc (clang_loc);
88
-
89
- return Diagnostics::ConvertedLoc{
90
- .loc = {.filename = presumed_loc.getFilename (),
91
- .line_number = static_cast <int32_t >(presumed_loc.getLine ())},
92
- // TODO: Set `last_byte_offset` based on the `import Cpp` location.
93
- .last_byte_offset = 0 };
94
- }
95
-
96
- return ConvertLocInCarbonFile (absolute_node_id.check_ir_id (),
97
- absolute_node_id.node_id (), token_only);
98
- }
99
-
100
- auto DiagnosticEmitter::ConvertLocInCarbonFile (SemIR::CheckIRId check_ir_id,
101
- Parse::NodeId node_id,
102
- bool token_only) const
103
- -> Diagnostics::ConvertedLoc {
104
- CARBON_CHECK (check_ir_id != SemIR::CheckIRId::Cpp);
105
- const auto & tree_and_subtrees =
106
- tree_and_subtrees_getters_[check_ir_id.index ]();
107
- return tree_and_subtrees.NodeToDiagnosticLoc (node_id, token_only);
108
- }
109
-
110
47
auto DiagnosticEmitter::ConvertArg (llvm::Any arg) const -> llvm::Any {
111
48
if (auto * library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
112
49
std::string library_name;
@@ -178,10 +115,4 @@ auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
178
115
return DiagnosticEmitterBase::ConvertArg (arg);
179
116
}
180
117
181
- auto DiagnosticEmitter::AddInImport (Diagnostics::Loc loc, ContextFnT context_fn)
182
- -> void {
183
- CARBON_DIAGNOSTIC (InImport, LocationInfo, " in import" );
184
- context_fn (loc, InImport);
185
- }
186
-
187
118
} // namespace Carbon::Check
0 commit comments