Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as xml2js from 'xml2js';
import * as crypto from 'crypto';

import { documentationLinkMap, getPremiumCertLink } from './util/documentation';
import { runCommand } from './util/scripts';
import { looksLikePath, resolvePath, findWorkspaceRoot } from './util/path';

// To keep track of document changes we save hashed versions of their content to this record
let documentHashMemory : Record<string, string> = {};

let cppcheckProgressIndicator: vscode.StatusBarItem;
let checksRunning = false;

Expand Down Expand Up @@ -76,6 +80,13 @@ function updateProgressIndicator(): void {
}
}

function getDocumentSha1(document: vscode.TextDocument): string {
return crypto
.createHash('sha1')
.update(document.getText(), 'utf8')
.digest('hex');
}

// This method is called when your extension is activated.
// Your extension is activated the very first time the command is executed.
export async function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -108,6 +119,15 @@ export async function activate(context: vscode.ExtensionContext) {
return;
}

if ((Object.keys(documentHashMemory) as Array<string>).includes(document.fileName)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can there be some situation when results are not shown for a file and user wants to save to trigger analysis? is cppcheck always executed when a file is opened? even if user doesn't open the file but vscode opens the file automatically during startup or something?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a good question, I am trying to think of a way to make it safe from ending up in a kind of stuck mode such as you describe, but I am not sure we can be completely safe from it happening.

Maybe this feature isn't worth the risk?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how much of a problem it is to run checks a little more than necessary tbh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you only calculate the hash when the file is saved we should be safe.. if you feel this is done then I am happy.

// Check file content against memory, if it has not changed since last check do early return
const newHash = getDocumentSha1(document);
const oldHash = documentHashMemory[document.fileName];
if (newHash === oldHash) {
return;
}
}

// Check if the document is visible in any editor
const isVisible = vscode.window.visibleTextEditors.some(editor =>
editor.document.uri.toString().replaceAll('\\', '/') === document.uri.toString().replaceAll('\\', '/'));
Expand Down Expand Up @@ -344,6 +364,12 @@ async function runCppcheckOnFileXML(
}
diagnosticCollection.set(document.uri, diagnostics);
});

// If checks have run without error, save hashed document content to memory
if (!code) {
const hashedContentOfFile = getDocumentSha1(document);
documentHashMemory[document.fileName] = hashedContentOfFile;
}
});

checksRunning = false;
Expand Down
Loading