@@ -349,7 +349,7 @@ async function runCppcheckOnFileXML(
349349
350350 const errors = result . results ?. errors ?. [ 0 ] ?. error || [ ] ;
351351 const diagnostics : Record < string , vscode . Diagnostic [ ] > = { } ;
352-
352+ console . log ( 'errors from file' , document . fileName , errors ) ;
353353 for ( const e of errors ) {
354354 const isCriticalError = criticalWarningTypes . includes ( e . $ . id ) ;
355355 const locations = e . location || [ ] ;
@@ -377,7 +377,7 @@ async function runCppcheckOnFileXML(
377377
378378 // Cppcheck col number is 1-indexed, while VS Code uses 0-indexing
379379 let col = Number ( mainLoc . column ) - 1 ;
380- if ( isNaN ( col ) || col < 0 || col > document . lineAt ( line ) . text . length ) {
380+ if ( isNaN ( col ) || col < 0 ) {
381381 col = 0 ;
382382 }
383383
@@ -405,12 +405,16 @@ async function runCppcheckOnFileXML(
405405 const loc = locations [ locations . length - i ] . $ ;
406406 const msg = loc . info ;
407407 const lLine = Number ( loc . line ) - 1 ;
408- const lCol = Number ( loc . col ) - 1 ;
408+ var lCol = Number ( loc . col ) - 1 ;
409409
410410 if ( msg === null || msg === undefined || isNaN ( lLine ) || lLine < 0 || lLine >= document . lineCount ) {
411411 continue ;
412412 }
413413
414+ if ( isNaN ( lCol ) || lCol < 0 ) {
415+ lCol = 0 ;
416+ }
417+
414418 const relatedRange = new vscode . Range (
415419 lLine , lCol ,
416420 lLine , document . lineAt ( lLine ) . text . length
@@ -444,7 +448,26 @@ async function runCppcheckOnFileXML(
444448 }
445449 const sourceDocumentUri = document . uri . toString ( ) ;
446450 for ( const uri of Object . keys ( diagnostics ) ) {
447- diagnosticCollection . set ( vscode . Uri . parse ( uri ) , diagnostics [ uri ] ) ;
451+ const existingDiagnostics =
452+ diagnosticCollection . get ( vscode . Uri . parse ( uri ) ) ;
453+ // If file has existing diagnostics from analyzing other files we do not want to overwrite those
454+ const newDiagnostics = diagnostics [ uri ] ;
455+
456+ if ( existingDiagnostics ) {
457+ // Compare existing diagnostics to new diagnostics (error code & range) to only push unique ones
458+ for ( const diagnostic of existingDiagnostics ) {
459+ if ( ! newDiagnostics . some ( ( d ) => {
460+ if ( typeof ( diagnostic ?. code ) === "object" && typeof ( diagnostic ?. code ) !== null && typeof ( d ?. code ) === "object" && typeof ( d ?. code ) !== null ) {
461+ return diagnostic . code . value === d . code . value && diagnostic . range . isEqual ( d . range ) ;
462+ } else {
463+ return diagnostic . code === d . code && diagnostic . range . isEqual ( d . range ) ;
464+ }
465+ } ) ) {
466+ newDiagnostics . push ( diagnostic ) ;
467+ }
468+ }
469+ }
470+ diagnosticCollection . set ( vscode . Uri . parse ( uri ) , newDiagnostics ) ;
448471 if ( fileRelationMap [ uri ] === null || fileRelationMap [ uri ] === undefined ) {
449472 fileRelationMap [ uri ] = new Set ;
450473 }
0 commit comments