Skip to content

Commit 7f89aa8

Browse files
committed
Avoid computing generated source notes on child notes
This could cause us to double up on the same note for nested macro expansions, as we'd generate the note, then when recursing back through the function we'd compute the same note again. Also remove the seemingly unused `lastBufferID` param.
1 parent 6b18eeb commit 7f89aa8

File tree

4 files changed

+9
-28
lines changed

4 files changed

+9
-28
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,8 +1169,7 @@ namespace swift {
11691169

11701170
/// Retrieve the set of child notes that describe how the generated
11711171
/// source buffer was derived, e.g., a macro expansion backtrace.
1172-
std::vector<Diagnostic> getGeneratedSourceBufferNotes(
1173-
SourceLoc loc, Optional<unsigned> &lastBufferID);
1172+
std::vector<Diagnostic> getGeneratedSourceBufferNotes(SourceLoc loc);
11741173

11751174
/// Handle a new diagnostic, which will either be emitted, or added to an
11761175
/// active transaction.

lib/AST/DiagnosticEngine.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,9 +1272,8 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
12721272
diagnostic.isChildNote());
12731273
}
12741274

1275-
std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
1276-
SourceLoc loc, Optional<unsigned> &lastBufferID
1277-
) {
1275+
std::vector<Diagnostic>
1276+
DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
12781277
// The set of child notes we're building up.
12791278
std::vector<Diagnostic> childNotes;
12801279

@@ -1285,12 +1284,6 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
12851284
// If we already emitted these notes for a prior part of the diagnostic,
12861285
// don't do so again.
12871286
auto currentBufferID = SourceMgr.findBufferContainingLoc(loc);
1288-
if (currentBufferID == lastBufferID)
1289-
return childNotes;
1290-
1291-
// Keep track of the last buffer ID we considered.
1292-
lastBufferID = currentBufferID;
1293-
12941287
SourceLoc currentLoc = loc;
12951288
do {
12961289
auto generatedInfo = SourceMgr.getGeneratedSourceInfo(currentBufferID);
@@ -1337,15 +1330,18 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
13371330
}
13381331

13391332
void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
1340-
Optional<unsigned> lastBufferID;
13411333

13421334
ArrayRef<Diagnostic> childNotes = diagnostic.getChildNotes();
13431335
std::vector<Diagnostic> extendedChildNotes;
13441336

13451337
if (auto info = diagnosticInfoForDiagnostic(diagnostic)) {
13461338
// If the diagnostic location is within a buffer containing generated
13471339
// source code, add child notes showing where the generation occurred.
1348-
extendedChildNotes = getGeneratedSourceBufferNotes(info->Loc, lastBufferID);
1340+
// We need to avoid doing this if this is itself a child note, as otherwise
1341+
// we'd end up doubling up on notes.
1342+
if (!info->IsChildNote) {
1343+
extendedChildNotes = getGeneratedSourceBufferNotes(info->Loc);
1344+
}
13491345
if (!extendedChildNotes.empty()) {
13501346
extendedChildNotes.insert(extendedChildNotes.end(),
13511347
childNotes.begin(), childNotes.end());

test/Macros/macro_expand_other.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func testFreestandingExpansionOfOther() {
3131

3232
#if TEST_DIAGNOSTICS
3333
#recurseThrough(true)
34-
// expected-note@-1 2{{in expansion of macro 'recurseThrough' here}}
34+
// expected-note@-1 {{in expansion of macro 'recurseThrough' here}}
3535
#endif
3636
}
3737

test/SourceKit/Macros/diags.swift.response

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,6 @@
8484
}
8585
]
8686
},
87-
{
88-
key.line: 17,
89-
key.column: 11,
90-
key.filepath: diags.swift,
91-
key.severity: source.diagnostic.severity.note,
92-
key.id: "in_macro_expansion",
93-
key.description: "in expansion of macro 'stringify' here",
94-
key.ranges: [
95-
{
96-
key.offset: 674,
97-
key.length: 29
98-
}
99-
]
100-
},
10187
{
10288
key.line: 17,
10389
key.column: 11,

0 commit comments

Comments
 (0)