From 788592d85ac15f0806a7049669529f40179f87ba Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Fri, 31 Mar 2023 14:17:47 -0400 Subject: [PATCH] Remove ccache from compile_commands.json It confuses vscode's intellisense, causing it to not understand GNUC syntaxes for example. The bug was reported upstream: https://github.com/microsoft/vscode-cpptools/issues/7616 --- src/extension.ts | 13 ++++++++++++- src/utils.ts | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 8610fd60..dd50b09a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -16,7 +16,8 @@ import { workspaceRelative, extensionConfigurationSet, getTargetName, - genEnvFile + genEnvFile, + patchCompileCommands } from "./utils"; import { getMesonTargets, @@ -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; @@ -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'); @@ -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); @@ -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) { diff --git a/src/utils.ts b/src/utils.ts index f0f4b25a..5613450d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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, " ")); + } +}