diff --git a/src/extension.ts b/src/extension.ts index 5629a6e..5e3834d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -254,7 +254,6 @@ async function runCppcheckOnFileXML( continue; } - // Cppcheck line number is 1-indexed, while VS Code uses 0-indexing const mainLoc = locations[locations.length - 1].$; // If main location is not current file, then skip displaying warning @@ -262,18 +261,25 @@ async function runCppcheckOnFileXML( continue; } + // Cppcheck line number is 1-indexed, while VS Code uses 0-indexing const line = Number(mainLoc.line) - 1; // Invalid line number usually means non-analysis output if (isNaN(line) || line < 0 || line >= document.lineCount) { continue; } + // Cppcheck col number is 1-indexed, while VS Code uses 0-indexing + let col = Number(mainLoc.column) - 1; + if (isNaN(col) || col < 0 || col > document.lineAt(line).text.length) { + col = 0; + } + const severity = parseSeverity(e.$.severity); if (severityToNumber(severity) < minSevNum) { continue; } - const range = new vscode.Range(line, 0, line, document.lineAt(line).text.length); + const range = new vscode.Range(line, col, line, document.lineAt(line).text.length); const diagnostic = new vscode.Diagnostic(range, e.$.msg, severity); diagnostic.source = "cppcheck"; diagnostic.code = e.$.id;