@@ -9,6 +9,8 @@ import { looksLikePath, resolvePath, findWorkspaceRoot } from './util/path';
99
1010// To keep track of document changes we save hashed versions of their content to this record
1111let documentHashMemory : Record < string , string > = { } ;
12+ // To keep track of header warnings created from analysis of source file we save their relations to headerSourceFileRelationMap
13+ let headerSourceFileRelationMap : Record < string , Set < string > > = { } ;
1214
1315let previewAnalysisTimer : NodeJS . Timeout | undefined ;
1416let previewedDocument : vscode . TextDocument | undefined ;
@@ -114,6 +116,21 @@ export async function activate(context: vscode.ExtensionContext) {
114116 const diagnosticCollection = vscode . languages . createDiagnosticCollection ( "Cppcheck" ) ;
115117 context . subscriptions . push ( diagnosticCollection ) ;
116118
119+ function clearDiagnosticForDoc ( doc : vscode . TextDocument ) : void {
120+ diagnosticCollection . delete ( doc . uri ) ;
121+ // If we are displaying header errors generated from analysing doc (and only from this), clear these
122+ for ( const headerUri of Object . keys ( headerSourceFileRelationMap ) ) {
123+ if ( headerSourceFileRelationMap [ headerUri ] . has ( doc . uri . toString ( ) ) ) {
124+ if ( headerSourceFileRelationMap [ headerUri ] . size === 1 ) {
125+ diagnosticCollection . delete ( vscode . Uri . parse ( headerUri ) ) ;
126+ } else {
127+ headerSourceFileRelationMap [ headerUri ] . delete ( doc . uri . toString ( ) ) ;
128+ }
129+ }
130+ }
131+ documentHashMemory [ doc . fileName ] = '' ;
132+ }
133+
117134 async function handleDocument ( document : vscode . TextDocument ) {
118135 // Only process C/C++ files.
119136 if ( ! [ "c" , "cpp" ] . includes ( document . languageId ) ) {
@@ -164,8 +181,7 @@ export async function activate(context: vscode.ExtensionContext) {
164181
165182 // If disabled, clear any existing diagnostics for this doc.
166183 if ( ! isEnabled ) {
167- diagnosticCollection . delete ( document . uri ) ;
168- documentHashMemory [ document . fileName ] = '' ;
184+ clearDiagnosticForDoc ( document ) ;
169185 return ;
170186 }
171187
@@ -217,13 +233,22 @@ export async function activate(context: vscode.ExtensionContext) {
217233 }
218234 }
219235 }
236+ for ( const tab of e . closed ) {
237+ if ( tab . input instanceof vscode . TabInputText ) {
238+ const uri = tab . input . uri ;
239+ const document =
240+ vscode . workspace . textDocuments . find (
241+ doc => doc . uri . toString ( ) === uri . toString ( )
242+ ) ?? await vscode . workspace . openTextDocument ( uri ) ;
243+ clearDiagnosticForDoc ( document ) ;
244+ }
245+ }
220246 } , null , context . subscriptions ) ;
221247
222248 // Clear diagnostics of previewed files when no longer viewed
223249 vscode . window . onDidChangeActiveTextEditor ( ( ) => {
224250 if ( previewedDocument ) {
225- diagnosticCollection . delete ( previewedDocument . uri ) ;
226- documentHashMemory [ previewedDocument . fileName ] = '' ;
251+ clearDiagnosticForDoc ( previewedDocument ) ;
227252 previewedDocument = undefined ;
228253 }
229254 } ) ;
@@ -238,8 +263,7 @@ export async function activate(context: vscode.ExtensionContext) {
238263
239264 // Clean up diagnostics when a file is closed
240265 vscode . workspace . onDidCloseTextDocument ( ( document : vscode . TextDocument ) => {
241- diagnosticCollection . delete ( document . uri ) ;
242- documentHashMemory [ document . fileName ] = '' ;
266+ clearDiagnosticForDoc ( document ) ;
243267 } , null , context . subscriptions ) ;
244268}
245269
@@ -416,8 +440,15 @@ async function runCppcheckOnFileXML(
416440 diagnostics [ uri ] . push ( diagnostic ) ;
417441 }
418442 }
443+ const sourceDocumentUri = document . uri . toString ( ) ;
419444 for ( const uri of Object . keys ( diagnostics ) ) {
420445 diagnosticCollection . set ( vscode . Uri . parse ( uri ) , diagnostics [ uri ] ) ;
446+ if ( uri !== sourceDocumentUri ) {
447+ if ( headerSourceFileRelationMap [ uri ] === null || headerSourceFileRelationMap [ uri ] === undefined ) {
448+ headerSourceFileRelationMap [ uri ] = new Set ;
449+ }
450+ headerSourceFileRelationMap [ uri ] . add ( sourceDocumentUri ) ;
451+ }
421452 }
422453 } ) ;
423454
0 commit comments