Skip to content

Commit cff6137

Browse files
authored
Show swiftc diagnostics with square brackets (#1325)
* Show swiftc diagnostics with square brackets Display the full diagnostic message instead of stopping at backslashes or square brackets and update the diagnostics test case to check for this. Issue: #1312 * Remove [-Wcode] warnings from swiftc diagnostics Remove any additional [-Wcode] warnings that swiftc adds to messages to allow for merging diagnostics with SourceKit.
1 parent 95a6e43 commit cff6137

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

assets/test/diagnostics/Sources/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ repeat {
1313
print(line ?? "nil")
1414
} while line != nil;
1515

16+
let dictionary: [String: String] = []
17+
1618
#if canImport(Testing)
1719
import Testing
1820
#expect(try myFunc() != 0)

assets/test/diagnosticsCpp/Sources/MyPoint/MyPoint.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ int func() {
66
p->y = bar;
77
p.z = 2
88
return p.x;
9-
}
9+
}
10+
11+
int main() {
12+
return;
13+
}

src/DiagnosticsManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,14 @@ export class DiagnosticsManager implements vscode.Disposable {
355355
line: string
356356
): ParsedDiagnostic | vscode.DiagnosticRelatedInformation | undefined {
357357
const diagnosticRegex =
358-
/^(?:\S+\s+)?(.*?):(\d+)(?::(\d+))?:\s+(warning|error|note):\s+([^\\[]*)/g;
358+
/^(?:\S+\s+)?(.*?):(\d+)(?::(\d+))?:\s+(warning|error|note):\s+(.*)$/g;
359+
const switfcExtraWarningsRegex = /\[-W.*?\]/g;
359360
const match = diagnosticRegex.exec(line);
360361
if (!match) {
361362
return;
362363
}
363364
const uri = vscode.Uri.file(match[1]).fsPath;
364-
const message = this.capitalize(match[5]).trim();
365+
const message = this.capitalize(match[5]).replace(switfcExtraWarningsRegex, "").trim();
365366
const range = this.range(match[2], match[3]);
366367
const severity = this.severity(match[4]);
367368
if (severity === vscode.DiagnosticSeverity.Information) {

test/integration-tests/DiagnosticsManager.test.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ suite("DiagnosticsManager Test Suite", async function () {
181181
);
182182
expectedMainErrorDiagnostic.source = "swiftc";
183183

184+
const expectedMainDictErrorDiagnostic = new vscode.Diagnostic(
185+
new vscode.Range(new vscode.Position(15, 35), new vscode.Position(15, 35)),
186+
"Use [:] to get an empty dictionary literal",
187+
vscode.DiagnosticSeverity.Error
188+
);
189+
expectedMainDictErrorDiagnostic.source = "swiftc";
190+
184191
const expectedFuncErrorDiagnostic: vscode.Diagnostic = new vscode.Diagnostic(
185192
new vscode.Range(new vscode.Position(1, 4), new vscode.Position(1, 4)),
186193
"Cannot find 'baz' in scope",
@@ -189,7 +196,7 @@ suite("DiagnosticsManager Test Suite", async function () {
189196
expectedFuncErrorDiagnostic.source = "swiftc";
190197

191198
const expectedMacroDiagnostic = new vscode.Diagnostic(
192-
new vscode.Range(new vscode.Position(17, 26), new vscode.Position(17, 26)),
199+
new vscode.Range(new vscode.Position(19, 26), new vscode.Position(19, 26)),
193200
"No calls to throwing functions occur within 'try' expression",
194201
vscode.DiagnosticSeverity.Warning
195202
);
@@ -238,6 +245,7 @@ suite("DiagnosticsManager Test Suite", async function () {
238245
[mainUri.fsPath]: [
239246
expectedWarningDiagnostic,
240247
expectedMainErrorDiagnostic,
248+
expectedMainDictErrorDiagnostic,
241249
...(workspaceContext.swiftVersion.isGreaterThanOrEqual(
242250
new Version(6, 0, 0)
243251
)
@@ -269,7 +277,11 @@ suite("DiagnosticsManager Test Suite", async function () {
269277

270278
await Promise.all([
271279
waitForDiagnostics({
272-
[mainUri.fsPath]: [expectedWarningDiagnostic, expectedMainErrorDiagnostic], // Should have parsed correct severity
280+
[mainUri.fsPath]: [
281+
expectedWarningDiagnostic,
282+
expectedMainErrorDiagnostic,
283+
expectedMainDictErrorDiagnostic,
284+
], // Should have parsed correct severity
273285
[funcUri.fsPath]: [expectedFuncErrorDiagnostic], // Check parsed for other file
274286
}),
275287
executeTaskAndWaitForResult(createBuildAllTask(folderContext)),
@@ -282,7 +294,11 @@ suite("DiagnosticsManager Test Suite", async function () {
282294

283295
await Promise.all([
284296
waitForDiagnostics({
285-
[mainUri.fsPath]: [expectedWarningDiagnostic, expectedMainErrorDiagnostic], // Should have parsed correct severity
297+
[mainUri.fsPath]: [
298+
expectedWarningDiagnostic,
299+
expectedMainErrorDiagnostic,
300+
expectedMainDictErrorDiagnostic,
301+
], // Should have parsed correct severity
286302
[funcUri.fsPath]: [expectedFuncErrorDiagnostic], // Check parsed for other file
287303
}),
288304
executeTaskAndWaitForResult(createBuildAllTask(folderContext)),
@@ -365,9 +381,22 @@ suite("DiagnosticsManager Test Suite", async function () {
365381
);
366382
expectedDiagnostic2.source = "swiftc";
367383

384+
// Message should not contain [-Wreturn-mismatch] so it can be merged with
385+
// SourceKit diagnostics if required
386+
const expectedDiagnostic3 = new vscode.Diagnostic(
387+
new vscode.Range(new vscode.Position(11, 4), new vscode.Position(11, 4)),
388+
"Non-void function 'main' should return a value",
389+
vscode.DiagnosticSeverity.Error
390+
);
391+
expectedDiagnostic3.source = "swiftc";
392+
368393
await Promise.all([
369394
waitForDiagnostics({
370-
[cppUri.fsPath]: [expectedDiagnostic1, expectedDiagnostic2],
395+
[cppUri.fsPath]: [
396+
expectedDiagnostic1,
397+
expectedDiagnostic2,
398+
expectedDiagnostic3,
399+
],
371400
}),
372401
executeTaskAndWaitForResult(createBuildAllTask(cppFolderContext)),
373402
]);

0 commit comments

Comments
 (0)