Skip to content

Commit aea6d7d

Browse files
committed
[opt-remark] Teach opt-remark how to emit "NOTE" arguments that use the loc of the original remark instead of the args own.
This is useful if one wants to emit a note explanation (in my case that we couldn't find any values) but do not have any other source loc but the original remark.
1 parent 9a828bc commit aea6d7d

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

include/swift/SIL/OptimizationRemark.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ struct ArgumentKeyKind {
4141
// diagnostic when emitting diagnostics. Do nothing special
4242
// along the backend path.
4343
Note,
44+
45+
// Assume that this is a note that should be emitted as a separate
46+
// diagnostic but that doesn't have its own source loc: we should reuse the
47+
// one for the original remark.
48+
//
49+
// This is intended to be used in situations where one needs to emit a
50+
// "note" warning due to us not being able to infer a part of our
51+
// opt-remark.
52+
ParentLocNote,
4453
};
4554

4655
InnerTy innerValue;
@@ -60,6 +69,7 @@ struct ArgumentKeyKind {
6069
case InnerTy::Default:
6170
return false;
6271
case InnerTy::Note:
72+
case InnerTy::ParentLocNote:
6373
return true;
6474
}
6575

lib/SIL/Utils/OptimizationRemark.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,26 @@ static void emitRemark(SILModule &module, const Remark<RemarkT> &remark,
209209
// If diagnostics are enabled, first emit the main diagnostic and then loop
210210
// through our arguments and allow the arguments to add additional diagnostics
211211
// if they want.
212-
if (diagEnabled) {
213-
auto &Diags = module.getASTContext().Diags;
214-
Diags.diagnose(remark.getLocation(), id,
215-
remark.getMsg());
216-
for (auto &arg : remark.getArgs()) {
217-
switch (arg.key.kind) {
218-
case ArgumentKeyKind::Default:
219-
continue;
220-
case ArgumentKeyKind::Note:
221-
Diags.diagnose(arg.loc, diag::opt_remark_note,
222-
arg.key.data);
223-
continue;
224-
}
225-
226-
llvm_unreachable("Unhandled case?!");
227-
}
228-
}
212+
if (!diagEnabled)
213+
return;
214+
215+
auto &de = module.getASTContext().Diags;
216+
de.diagnoseWithNotes(
217+
de.diagnose(remark.getLocation(), id, remark.getMsg()), [&]() {
218+
for (auto &arg : remark.getArgs()) {
219+
switch (arg.key.kind) {
220+
case ArgumentKeyKind::Default:
221+
continue;
222+
case ArgumentKeyKind::Note:
223+
de.diagnose(arg.loc, diag::opt_remark_note, arg.val);
224+
continue;
225+
case ArgumentKeyKind::ParentLocNote:
226+
de.diagnose(remark.getLocation(), diag::opt_remark_note, arg.val);
227+
continue;
228+
}
229+
llvm_unreachable("Unhandled case?!");
230+
}
231+
});
229232
}
230233

231234
void Emitter::emit(const RemarkPassed &remark) {

0 commit comments

Comments
 (0)