From d946c9ac8de04398172f60c1f5ad2701d68a5faf Mon Sep 17 00:00:00 2001 From: "Bob Brown (DEVDIV)" Date: Sat, 15 Feb 2020 18:13:20 -0800 Subject: [PATCH 1/4] Update IntelliSense configuration based on target selected --- src/cpptools.ts | 41 ++++++++++++++++---- src/extension.ts | 6 ++- test/unit-tests/cpptools.test.ts | 64 +++++++++++++++++++++++++++++++- 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/cpptools.ts b/src/cpptools.ts index 3eb243d59..2395673f7 100644 --- a/src/cpptools.ts +++ b/src/cpptools.ts @@ -31,6 +31,7 @@ export interface CompileFlagInformation { class MissingCompilerException extends Error {} interface TargetDefaults { + name: string; includePath: string[]; compileFlags: string[]; defines: string[]; @@ -170,6 +171,10 @@ export interface CodeModelParams { * property. */ clCompilerPath?: string|null; + /** + * The active target + */ + activeTarget: string|null; } /** @@ -202,7 +207,12 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider */ private _getConfiguration(uri: vscode.Uri): cpt.SourceFileConfigurationItem|undefined { const norm_path = util.platformNormalizePath(uri.fsPath); - return this._fileIndex.get(norm_path); + const configurations = this._fileIndex.get(norm_path); + if (this._activeTarget && configurations?.has(this._activeTarget)) { + return configurations!.get(this._activeTarget); + } else { + return configurations?.values().next().value; // Any value is fine if the target doesn't match + } } /** @@ -243,9 +253,14 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider /** * Index of files to configurations, using the normalized path to the file - * as the key. + * as the key to the . + */ + private readonly _fileIndex = new Map>(); + + /** + * If a source file configuration exists for the active target, we will prefer that one when asked. */ - private readonly _fileIndex = new Map(); + private _activeTarget: string|null = null; /** * Create a source file configuration for the given file group. @@ -313,10 +328,19 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider for (const src of grp.sources) { const abs = path.isAbsolute(src) ? src : path.join(sourceDir, src); const abs_norm = util.platformNormalizePath(abs); - this._fileIndex.set(abs_norm, { - uri: vscode.Uri.file(abs).toString(), - configuration, - }); + if (this._fileIndex.has(abs_norm)) { + this._fileIndex.get(abs_norm)!.set(target.name, { + uri: vscode.Uri.file(abs).toString(), + configuration + }); + } else { + const data = new Map(); + data.set(target.name, { + uri: vscode.Uri.file(abs).toString(), + configuration, + }); + this._fileIndex.set(abs_norm, data); + } const dir = path.dirname(abs_norm); if (this._workspaceBrowseConfiguration.browsePath.indexOf(dir) < 0) { this._workspaceBrowseConfiguration.browsePath.push(dir); @@ -331,6 +355,7 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider updateConfigurationData(opts: CodeModelParams) { let hadMissingCompilers = false; this._workspaceBrowseConfiguration = {browsePath: []}; + this._activeTarget = opts.activeTarget; for (const config of opts.codeModel.configurations) { for (const project of config.projects) { for (const target of project.targets) { @@ -345,12 +370,12 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider const sysroot = target.sysroot || ''; for (const grp of target.fileGroups || []) { try { - this._updateFileGroup( target.sourceDirectory || '', grp, opts, { + name: target.name, compileFlags, includePath, defines, diff --git a/src/extension.ts b/src/extension.ts index c23966615..60ac5cff7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -450,6 +450,7 @@ class ExtensionManager implements vscode.Disposable { } } + private cpptoolsReady: boolean = false; private _updateCodeModel(folder: CMakeToolsFolder) { const cmt = folder.cmakeTools; this._projectOutlineProvider.updateCodeModel( @@ -479,10 +480,11 @@ class ExtensionManager implements vscode.Disposable { const opts = drv ? drv.expansionOptions : undefined; const env = await effectiveKitEnvironment(kit, opts); const clCompilerPath = await findCLCompilerPath(env); - this._configProvider.updateConfigurationData({cache, codeModel, clCompilerPath}); + this._configProvider.updateConfigurationData({cache, codeModel, clCompilerPath, activeTarget: cmt.defaultBuildTarget}); await this.ensureCppToolsProviderRegistered(); - if (cpptools.notifyReady) { + if (cpptools.notifyReady && !this.cpptoolsReady) { cpptools.notifyReady(this._configProvider); + this.cpptoolsReady = true; } else { cpptools.didChangeCustomConfiguration(this._configProvider); } diff --git a/test/unit-tests/cpptools.test.ts b/test/unit-tests/cpptools.test.ts index 27cb3d3b7..e1bb9ba27 100644 --- a/test/unit-tests/cpptools.test.ts +++ b/test/unit-tests/cpptools.test.ts @@ -1,8 +1,17 @@ -import {parseCompileFlags} from '@cmt/cpptools'; +import {parseCompileFlags, CppConfigurationProvider} from '@cmt/cpptools'; import {expect} from '@test/util'; +import { CMakeCache } from '@cmt/cache'; +import * as path from 'path'; +import * as codemodel_api from '@cmt/drivers/codemodel-driver-interface'; +import * as vscode from 'vscode'; // tslint:disable:no-unused-expression +const here = __dirname; +function getTestResourceFilePath(filename: string): string { + return path.normalize(path.join(here, '../../../test/unit-tests', filename)); +} + suite('CppTools tests', () => { test('Parse some compiler flags', () => { let info = parseCompileFlags(['-DFOO=BAR']); @@ -15,4 +24,57 @@ suite('CppTools tests', () => { info = parseCompileFlags(['-std=c++03']); expect(info.standard).to.eql('c++03'); }); + + test('Validate code model', async () => { + const provider = new CppConfigurationProvider(); + const cache = await CMakeCache.fromPath(getTestResourceFilePath('TestCMakeCache.txt')); + const sourceFile = path.join(here, 'main.cpp'); + const uri = vscode.Uri.file(sourceFile); + const codeModel: codemodel_api.CodeModelContent = { + configurations: [{ + projects: [{ + name: 'cpptools-test', + sourceDirectory: here, + targets: [ + { + name: 'target1', + type: 'EXECUTABLE', + fileGroups: [{ + sources: [sourceFile], + isGenerated: false, + compileFlags: '-DFLAG1', + language: 'CXX' + }] + }, + { + name: 'target2', + type: 'EXECUTABLE', + fileGroups: [{ + sources: [sourceFile], + isGenerated: false, + compileFlags: '-DFLAG2', + language: 'CXX' + }] + } + ] + }] + }] + }; + + provider.updateConfigurationData({cache, codeModel, activeTarget: 'target1'}); + let configurations = await provider.provideConfigurations([uri]); + expect(configurations.length).to.eq(1); + expect(configurations[0].configuration.defines).to.contain('FLAG1'); + + provider.updateConfigurationData({cache, codeModel, activeTarget: 'target2'}); + configurations = await provider.provideConfigurations([uri]); + expect(configurations.length).to.eq(1); + expect(configurations[0].configuration.defines).to.contain('FLAG2'); + + provider.updateConfigurationData({cache, codeModel, activeTarget: 'all'}); + configurations = await provider.provideConfigurations([uri]); + expect(configurations.length).to.eq(1); + expect(configurations[0].configuration.defines.some(def => def === 'FLAG1' || def === 'FLAG2')).to.be.true; + expect(configurations[0].configuration.defines).to.not.have.all.members(['FLAG1', 'FLAG2']); + }); }); From da28add6ed8fe4b39e93ecb900666554241af2af Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 16 Mar 2020 05:21:12 +0000 Subject: [PATCH 2/4] Bump minimist from 1.2.0 to 1.2.2 Bumps [minimist](https://github.com/substack/minimist) from 1.2.0 to 1.2.2. - [Release notes](https://github.com/substack/minimist/releases) - [Commits](https://github.com/substack/minimist/compare/1.2.0...1.2.2) Signed-off-by: dependabot[bot] --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index ebbaa1128..e20677a77 100644 --- a/package.json +++ b/package.json @@ -1268,7 +1268,7 @@ "gulp-sourcemaps": "^2.6.5", "gulp-typescript": "^5.0.1", "jsonc-parser": "^2.1.1", - "minimist": "^1.2.0", + "minimist": "^1.2.2", "mocha": "^4.1.0", "module-alias": "^2.2.1", "sinon": "~5.0.7", diff --git a/yarn.lock b/yarn.lock index 9cf7826a3..760fa351f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3224,10 +3224,10 @@ minimist@0.0.8: resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0= -minimist@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" - integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ= +minimist@^1.2.0, minimist@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.2.tgz#b00a00230a1108c48c169e69a291aafda3aacd63" + integrity sha512-rIqbOrKb8GJmx/5bc2M0QchhUouMXSpd1RTclXsB41JdL+VtnojfaJR+h7F9k18/4kHUsBFgk80Uk+q569vjPA== minimist@~0.0.1: version "0.0.10" From 13195fcf1912c7ee774b14c79450f6f61c72f246 Mon Sep 17 00:00:00 2001 From: Bob Brown Date: Tue, 17 Mar 2020 09:26:51 -0700 Subject: [PATCH 3/4] clear the terminal for compile active file --- src/drivers/driver.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/drivers/driver.ts b/src/drivers/driver.ts index f6a0df57f..dc0b49b6d 100644 --- a/src/drivers/driver.ts +++ b/src/drivers/driver.ts @@ -269,8 +269,13 @@ export abstract class CMakeDriver implements vscode.Disposable { const env = this.getEffectiveSubprocessEnvironment(); const key = `${cmd.directory}${JSON.stringify(env)}`; let existing = this._compileTerms.get(key); - const shellPath = process.platform === 'win32' ? 'cmd.exe' : undefined; + if (existing && this.config.clearOutputBeforeBuild) { + this._compileTerms.delete(key); + existing.dispose(); + existing = undefined; + } if (!existing) { + const shellPath = process.platform === 'win32' ? 'cmd.exe' : undefined; const term = vscode.window.createTerminal({ name: localize('file.compilation', 'File Compilation'), cwd: cmd.directory, From 517e6cbbf4d82cb6e0bf6d2b2ceb15f72a5aaf62 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Mar 2020 17:58:57 +0000 Subject: [PATCH 4/4] Bump acorn from 5.7.3 to 5.7.4 Bumps [acorn](https://github.com/acornjs/acorn) from 5.7.3 to 5.7.4. - [Release notes](https://github.com/acornjs/acorn/releases) - [Commits](https://github.com/acornjs/acorn/compare/5.7.3...5.7.4) Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 760fa351f..3d4989188 100644 --- a/yarn.lock +++ b/yarn.lock @@ -371,9 +371,9 @@ integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ== acorn@5.X, acorn@^5.0.3: - version "5.7.3" - resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.3.tgz#67aa231bf8812974b85235a96771eb6bd07ea279" - integrity sha512-T/zvzYRfbVojPWahDsE5evJdHb3oJoQfFbsrKM7w5Zcs++Tr257tia3BmMP8XYVjp1S9RZXQMh7gao96BlqZOw== + version "5.7.4" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.4.tgz#3e8d8a9947d0599a1796d10225d7432f4a4acf5e" + integrity sha512-1D++VG7BhrtvQpNbBzovKNc1FLGGEE/oGe7b9xJm/RFHMBeUaUGpluV9RLjZa47YFdPcDAenEYuq9pQPcMdLJg== acorn@^6.2.1: version "6.4.0"