Skip to content

Commit 0f58452

Browse files
committed
feature / #77 show warnings for included header files along with source files
1 parent 73708f7 commit 0f58452

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

src/extension.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ async function runCppcheckOnFileXML(
272272
return arg;
273273
});
274274

275-
let proc;
275+
let usingProjectFile = false;
276276
const args = [
277277
'--enable=all',
278278
'--inline-suppr',
@@ -283,10 +283,13 @@ async function runCppcheckOnFileXML(
283283
...argsParsed,
284284
].filter(Boolean);
285285
if (processedArgs.includes("--project")) {
286+
usingProjectFile = true;
286287
args.push(`--file-filter=${filePath}`);
287288
} else {
288289
args.push(filePath);
289290
}
291+
292+
let proc;
290293
const cwd = findWorkspaceRoot();
291294
proc = cp.spawn(commandPath, args, {
292295
cwd,
@@ -312,14 +315,14 @@ async function runCppcheckOnFileXML(
312315
vscode.window.showErrorMessage(errorMessage);
313316
}
314317
const parser = new xml2js.Parser({ explicitArray: true });
315-
parser.parseString(xmlOutput, (err, result) => {
318+
parser.parseString(xmlOutput, async (err, result) => {
316319
if (err) {
317320
console.error("XML parse error:", err);
318321
return;
319322
}
320323

321324
const errors = result.results?.errors?.[0]?.error || [];
322-
const diagnostics: vscode.Diagnostic[] = [];
325+
const diagnostics: Record<string, vscode.Diagnostic[]> = {};
323326

324327
for (const e of errors) {
325328
const isCriticalError = criticalWarningTypes.includes(e.$.id);
@@ -330,8 +333,8 @@ async function runCppcheckOnFileXML(
330333

331334
const mainLoc = locations[locations.length - 1].$;
332335

333-
// If main location is not current file, then skip displaying warning unless it is critical
334-
if (!isCriticalError && !filePath.endsWith(mainLoc.file)) {
336+
// If main location is not current file, we are not using a project file and warning is not critical then skip displaying warning
337+
if (!isCriticalError && usingProjectFile && !filePath.endsWith(mainLoc.file)) {
335338
continue;
336339
}
337340

@@ -386,20 +389,36 @@ async function runCppcheckOnFileXML(
386389
lLine, lCol,
387390
lLine, document.lineAt(lLine).text.length
388391
);
389-
392+
const relatedDocument = await vscode.workspace.openTextDocument(loc.file);
390393
relatedInfos.push(
391394
new vscode.DiagnosticRelatedInformation(
392-
new vscode.Location(document.uri, relatedRange),
395+
new vscode.Location(relatedDocument?.uri ?? '', relatedRange),
393396
msg
394397
)
395398
);
396399
}
397400
if (relatedInfos.length > 0) {
398401
diagnostic.relatedInformation = relatedInfos;
399402
}
400-
diagnostics.push(diagnostic);
403+
const diagnosticFile = mainLoc.file;
404+
if (diagnosticFile === document.fileName) {
405+
const uri = document.uri.toString();
406+
if (diagnostics[uri] === null || diagnostics[uri] === undefined) {
407+
diagnostics[uri] = [];
408+
}
409+
diagnostics[uri].push(diagnostic);
410+
} else {
411+
const relatedDocument = await vscode.workspace.openTextDocument(mainLoc.file);
412+
const uri = relatedDocument.uri.toString();
413+
if (diagnostics[uri] === null || diagnostics[uri] === undefined) {
414+
diagnostics[uri] = [];
415+
}
416+
diagnostics[uri].push(diagnostic);
417+
}
418+
}
419+
for (const uri of Object.keys(diagnostics)) {
420+
diagnosticCollection.set(vscode.Uri.parse(uri), diagnostics[uri]);
401421
}
402-
diagnosticCollection.set(document.uri, diagnostics);
403422
});
404423

405424
// If checks have run without error, save hashed document content to memory

0 commit comments

Comments
 (0)