Skip to content

Commit

Permalink
Remove ccache from compile_commands.json
Browse files Browse the repository at this point in the history
It confuses vscode's intellisense, causing it to not understand GNUC
syntaxes for example. The bug was reported upstream:
microsoft/vscode-cpptools#7616
  • Loading branch information
xclaesse committed Mar 31, 2023
1 parent 657fd07 commit 788592d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
workspaceRelative,
extensionConfigurationSet,
getTargetName,
genEnvFile
genEnvFile,
patchCompileCommands
} from "./utils";
import {
getMesonTargets,
Expand All @@ -39,6 +40,7 @@ import {
export let extensionPath: string;
let explorer: MesonProjectExplorer;
let watcher: vscode.FileSystemWatcher;
let compileCommandsWatcher: vscode.FileSystemWatcher;
let mesonWatcher: vscode.FileSystemWatcher;
let controller: vscode.TestController;

Expand All @@ -57,6 +59,7 @@ export async function activate(ctx: vscode.ExtensionContext) {

explorer = new MesonProjectExplorer(ctx, root, buildDir);
watcher = vscode.workspace.createFileSystemWatcher(`${workspaceRelative(extensionConfiguration("buildFolder"))}/build.ninja`, false, false, true);
compileCommandsWatcher = vscode.workspace.createFileSystemWatcher(`${workspaceRelative(extensionConfiguration("buildFolder"))}/compile_commands.json`, false, false, true);
mesonWatcher = vscode.workspace.createFileSystemWatcher("**/meson.build", false, true, false);
controller = vscode.tests.createTestController('meson-test-controller', 'Meson test controller');

Expand All @@ -67,6 +70,7 @@ export async function activate(ctx: vscode.ExtensionContext) {
);

ctx.subscriptions.push(watcher);
ctx.subscriptions.push(compileCommandsWatcher);
ctx.subscriptions.push(mesonWatcher);
ctx.subscriptions.push(controller);

Expand All @@ -92,6 +96,13 @@ export async function activate(ctx: vscode.ExtensionContext) {
watcher.onDidCreate(changeHandler);
await genEnvFile(buildDir);

let compileCommandsHandler = async () => {
await patchCompileCommands(buildDir);
};
compileCommandsWatcher.onDidChange(compileCommandsHandler);
compileCommandsWatcher.onDidCreate(compileCommandsHandler);
await patchCompileCommands(buildDir);

ctx.subscriptions.push(
vscode.tasks.registerTaskProvider("meson", {
provideTasks(token) {
Expand Down
24 changes: 24 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,27 @@ export async function genEnvFile(buildDir: string) {
// Ignore errors, Meson could be too old to support --dump-format.
}
}

export async function patchCompileCommands(buildDir: string) {
// Remove ccache from compile commands because they confuse Intellisense:
// https://github.com/microsoft/vscode-cpptools/issues/7616
try {
var filePath = path.join(buildDir, "compile_commands.json")
var db = JSON.parse(fs.readFileSync(filePath, "utf-8"));
} catch {
return;
}
var changed = false;
for (var i in db) {
// FIXME: This should use proper shlex.split() and shlex.join()
var cmd = db[i]['command'].split(" ");
if (cmd[0].endsWith("ccache")) {
cmd.shift();
db[i]["command"] = cmd.join(" ");
changed = true;
}
}
if (changed) {
fs.writeFileSync(filePath, JSON.stringify(db, null, " "));
}
}

0 comments on commit 788592d

Please sign in to comment.