Skip to content

Fix location for diagnostics generated from macros via swiftc #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion assets/test/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
"-DTEST_ARGUMENT_SET_VIA_TEST_BUILD_ARGUMENTS_SETTING"
],
"lldb.verboseLogging": true

}
3 changes: 3 additions & 0 deletions assets/test/diagnostics/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ repeat {
line = readLine()
print(line ?? "nil")
} while line != nil;

import Testing
#expect(try myFunc() != 0)
36 changes: 34 additions & 2 deletions src/DiagnosticsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

import * as vscode from "vscode";
import * as fs from "fs";
// eslint-disable-next-line @typescript-eslint/no-require-imports
import stripAnsi = require("strip-ansi");
import configuration from "./configuration";
Expand Down Expand Up @@ -255,6 +256,7 @@ export class DiagnosticsManager implements vscode.Disposable {
};
let remainingData: string | undefined;
let lastDiagnostic: vscode.Diagnostic | undefined;
let lastDiagnosticNeedsSaving = false;
disposables.push(
swiftExecution.onDidWrite(data => {
const sanitizedData = (remainingData || "") + stripAnsi(data);
Expand Down Expand Up @@ -293,6 +295,18 @@ export class DiagnosticsManager implements vscode.Disposable {
lastDiagnostic.relatedInformation = (
lastDiagnostic.relatedInformation || []
).concat(relatedInformation);

if (lastDiagnosticNeedsSaving) {
const expandedUri = relatedInformation.location.uri.fsPath;
const currentUriDiagnostics = diagnostics.get(expandedUri) || [];
lastDiagnostic.range = relatedInformation.location.range;
diagnostics.set(expandedUri, [
...currentUriDiagnostics,
lastDiagnostic,
]);

lastDiagnosticNeedsSaving = false;
}
continue;
}
const { uri, diagnostic } = result as ParsedDiagnostic;
Expand All @@ -312,18 +326,36 @@ export class DiagnosticsManager implements vscode.Disposable {
continue;
}
lastDiagnostic = diagnostic;
diagnostics.set(uri, [...currentUriDiagnostics, diagnostic]);

// If the diagnostic comes from a macro expansion the URI is going to be an invalid URI.
// Save the diagnostic for when we get the related information which has the macro expansion location
// that should be used as the correct URI.
if (this.isValidUri(uri)) {
diagnostics.set(uri, [...currentUriDiagnostics, diagnostic]);
} else {
lastDiagnosticNeedsSaving = true;
}
}
}),
swiftExecution.onDidClose(done)
);
});
}

private isValidUri(uri: string): boolean {
try {
fs.accessSync(uri, fs.constants.F_OK);
return true;
} catch {
return false;
}
}

private parseDiagnostic(
line: string
): ParsedDiagnostic | vscode.DiagnosticRelatedInformation | undefined {
const diagnosticRegex = /^(.*?):(\d+)(?::(\d+))?:\s+(warning|error|note):\s+([^\\[]*)/g;
const diagnosticRegex =
/^(?:\S+\s+)?(.*?):(\d+)(?::(\d+))?:\s+(warning|error|note):\s+([^\\[]*)/g;
const match = diagnosticRegex.exec(line);
if (!match) {
return;
Expand Down
26 changes: 24 additions & 2 deletions test/integration-tests/DiagnosticsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function assertHasDiagnostic(uri: vscode.Uri, expected: vscode.Diagnostic): vsco
assert.notEqual(
diagnostic,
undefined,
`Could not find diagnostic matching:\n${JSON.stringify(expected)}`
`Could not find diagnostic matching:\n${JSON.stringify(expected)}\nDiagnostics:\n${JSON.stringify(diagnostics)}`
);
return diagnostic!;
}
Expand All @@ -66,7 +66,7 @@ function assertWithoutDiagnostic(uri: vscode.Uri, expected: vscode.Diagnostic) {
assert.equal(
diagnostics.find(findDiagnostic(expected)),
undefined,
`Unexpected diagnostic matching:\n${JSON.stringify(expected)}`
`Unexpected diagnostic matching:\n${JSON.stringify(expected)}\nDiagnostics:\n${JSON.stringify(diagnostics)}`
);
}

Expand Down Expand Up @@ -136,6 +136,23 @@ suite("DiagnosticsManager Test Suite", async function () {
);
expectedFuncErrorDiagnostic.source = "swiftc";

const expectedMacroDiagnostic = new vscode.Diagnostic(
new vscode.Range(new vscode.Position(16, 26), new vscode.Position(16, 26)),
"No calls to throwing functions occur within 'try' expression",
vscode.DiagnosticSeverity.Warning
);

expectedMacroDiagnostic.source = "swiftc";
expectedMacroDiagnostic.relatedInformation = [
{
location: {
uri: mainUri,
range: expectedMacroDiagnostic.range,
},
message: "Expanded code originates here",
},
];

// SourceKit-LSP sometimes sends diagnostics
// after first build and can cause intermittent
// failure if `swiftc` diagnostic is fixed
Expand All @@ -161,6 +178,7 @@ suite("DiagnosticsManager Test Suite", async function () {
// Should have parsed correct severity
assertHasDiagnostic(mainUri, expectedWarningDiagnostic);
assertHasDiagnostic(mainUri, expectedMainErrorDiagnostic);
assertHasDiagnostic(mainUri, expectedMacroDiagnostic);
// Check parsed for other file
assertHasDiagnostic(funcUri, expectedFuncErrorDiagnostic);
}).timeout(2 * 60 * 1000); // Allow 2 minutes to build
Expand All @@ -183,6 +201,7 @@ suite("DiagnosticsManager Test Suite", async function () {
// Should have parsed severity
assertHasDiagnostic(mainUri, expectedWarningDiagnostic);
assertHasDiagnostic(mainUri, expectedMainErrorDiagnostic);
assertHasDiagnostic(mainUri, expectedMacroDiagnostic);
// Check parsed for other file
assertHasDiagnostic(funcUri, expectedFuncErrorDiagnostic);
}).timeout(2 * 60 * 1000); // Allow 2 minutes to build
Expand All @@ -198,6 +217,9 @@ suite("DiagnosticsManager Test Suite", async function () {

// Should have parsed severity
assertHasDiagnostic(mainUri, expectedWarningDiagnostic);

// llvm style doesn't do macro diagnostics
assertWithoutDiagnostic(mainUri, expectedMacroDiagnostic);
const diagnostic = assertHasDiagnostic(mainUri, expectedMainErrorDiagnostic);
// Should have parsed related note
assert.equal(diagnostic.relatedInformation?.length, 1);
Expand Down
Loading