Skip to content

Commit f3f803d

Browse files
committed
hotfix/ analysis fails silently when cppcheck project file is used
1 parent 2db9257 commit f3f803d

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

src/extension.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,12 @@ async function runCppcheckOnFileXML(
363363
continue;
364364
}
365365

366-
const mainLocDocument = await vscode.workspace.openTextDocument(mainLoc.file);
366+
let mainLocDocument : vscode.TextDocument | undefined;
367+
try {
368+
mainLocDocument = await vscode.workspace.openTextDocument(mainLoc.file);
369+
} catch {
370+
// do nothing
371+
}
367372

368373
// Cppcheck line number is 1-indexed, while VS Code uses 0-indexing
369374
let line = Number(mainLoc.line) - 1;
@@ -378,7 +383,7 @@ async function runCppcheckOnFileXML(
378383

379384
// Cppcheck col number is 1-indexed, while VS Code uses 0-indexing
380385
let col = Number(mainLoc.column) - 1;
381-
if (isNaN(col) || col < 0 || col > mainLocDocument.lineAt(line).text.length) {
386+
if (isNaN(col) || col < 0 || !mainLocDocument || col > mainLocDocument.lineAt(line).text.length) {
382387
col = 0;
383388
}
384389

@@ -387,7 +392,7 @@ async function runCppcheckOnFileXML(
387392
continue;
388393
}
389394

390-
const range = new vscode.Range(line, col, line, mainLocDocument.lineAt(line).text.length);
395+
const range = new vscode.Range(line, col, line, mainLocDocument ? mainLocDocument.lineAt(line).text.length : col);
391396
const diagnostic = new vscode.Diagnostic(range, e.$.msg, severity);
392397
diagnostic.source = "cppcheck";
393398
// If we have a link to documentation, include it
@@ -412,36 +417,55 @@ async function runCppcheckOnFileXML(
412417
continue;
413418
}
414419

415-
const relatedDocument = await vscode.workspace.openTextDocument(loc.file);
420+
var relatedDocument : vscode.TextDocument | undefined;
421+
try {
422+
relatedDocument = await vscode.workspace.openTextDocument(loc.file);
423+
} catch {
424+
// Do nothing
425+
}
416426
const relatedRange = new vscode.Range(
417427
lLine, lCol,
418-
lLine, relatedDocument.lineAt(lLine).text.length
428+
lLine, relatedDocument ? relatedDocument.lineAt(lLine).text.length : lCol
419429
);
420430
relatedInfos.push(
421431
new vscode.DiagnosticRelatedInformation(
422-
new vscode.Location(relatedDocument?.uri ?? '', relatedRange),
432+
new vscode.Location(relatedDocument ? relatedDocument.uri : vscode.Uri.file(''), relatedRange),
423433
msg
424434
)
425435
);
426436
}
427-
428437
if (relatedInfos.length > 0) {
429438
diagnostic.relatedInformation = relatedInfos;
430439
}
431440
const diagnosticFile = mainLoc.file;
432-
if (diagnosticFile === document.fileName) {
441+
var diagnosticFileIsOpenDocument = diagnosticFile === document.fileName;
442+
if (!diagnosticFile.includes('/')) {
443+
// If we do not have file path but only name we asume diagnosed file is open document if they share name
444+
if (document.fileName.endsWith(diagnosticFile)) {
445+
diagnosticFileIsOpenDocument = true;
446+
}
447+
}
448+
if (diagnosticFileIsOpenDocument) {
433449
const uri = document.uri.toString();
434450
if (diagnostics[uri] === null || diagnostics[uri] === undefined) {
435451
diagnostics[uri] = [];
436452
}
437453
diagnostics[uri].push(diagnostic);
438454
} else {
439-
const relatedDocument = await vscode.workspace.openTextDocument(mainLoc.file);
440-
const uri = relatedDocument.uri.toString();
441-
if (diagnostics[uri] === null || diagnostics[uri] === undefined) {
442-
diagnostics[uri] = [];
455+
var relatedDocument : vscode.TextDocument | undefined;
456+
try {
457+
relatedDocument = await vscode.workspace.openTextDocument(mainLoc.file);
458+
} catch {
459+
// Do nothing
460+
}
461+
if (relatedDocument) {
462+
// Proceed if we are able to open the document
463+
const uri = relatedDocument.uri.toString();
464+
if (diagnostics[uri] === null || diagnostics[uri] === undefined) {
465+
diagnostics[uri] = [];
466+
}
467+
diagnostics[uri].push(diagnostic);
443468
}
444-
diagnostics[uri].push(diagnostic);
445469
}
446470
}
447471
const sourceDocumentUri = document.uri.toString();

0 commit comments

Comments
 (0)