Skip to content

Commit 15de5c5

Browse files
authored
Stop UTILITY targets from breaking IntelliSense (#4404) (#4405)
* Stop UTILITY targets from affecting IntelliSense Some `UTILITY` targets, such as the ones created by `Doxygen.cmake`, have a list of associated sources but don't have any useful `includePath`, `defines`, etc. If the selected Build Target is `all` or some other non-artifact target, these targets could be chosen as a fallback source of configuration instead of a more complete one. Filter out targets with empty configurations before selecting a fallback, so that doesn't happen. * changelog: move update to 1.22
1 parent 0bd2ab8 commit 15de5c5

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Bugs:
2121
- Ensure Visual Studio developer environment propagation preserves `VCPKG_ROOT`, enabling vcpkg-dependent configure runs after using the Set Visual Studio Developer Environment command. [microsoft/vscode-cpptools#14083](https://github.com/microsoft/vscode-cpptools/issues/14083)
2222
- Fix auto-focusing the "Search" input field in the CMake Cache view. [#4552](https://github.com/microsoft/vscode-cmake-tools/pull/4552) [@simhof-basyskom](https://github.com/simhof-basyskom)
2323
- Remove the demangling feature in the code coverage implementation for now since it doesn't work properly.
24-
24+
- Fix incorrect IntelliSense configuration when a `UTILITY` has source files. [#4404](https://github.com/microsoft/vscode-cmake-tools/issues/4404)
2525

2626
## 1.21
2727

src/cpptools.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,27 @@ export function getIntelliSenseMode(cptVersion: cpptools.Version, compilerPath:
310310
}
311311
}
312312

313+
/**
314+
* Try to find a target configuration with some populated properties.
315+
*
316+
* All targets get defaults for `compilerPath`, `compilerArgs`, and
317+
* `compilerFragments`, even `UTILITY` targets defined with
318+
* `add_custom_command()` that provide no other useful configuration, so if
319+
* possible, return one with more than just those populated.
320+
*/
321+
function fallbackConfiguration(configurations: Map<string, cpptools.SourceFileConfigurationItem> | undefined) {
322+
if (!configurations) {
323+
return undefined;
324+
}
325+
for (const item of configurations.values()) {
326+
const { configuration: { includePath, defines, intelliSenseMode, standard} } = item;
327+
if (includePath.length || defines.length || intelliSenseMode || standard) {
328+
return item;
329+
}
330+
}
331+
return configurations.values().next().value;
332+
}
333+
313334
/**
314335
* The actual class that provides information to the cpptools extension. See
315336
* the `CustomConfigurationProvider` interface for information on how this class
@@ -345,7 +366,7 @@ export class CppConfigurationProvider implements cpptools.CustomConfigurationPro
345366
if (this.activeTarget && configurations?.has(this.activeTarget)) {
346367
return configurations!.get(this.activeTarget);
347368
} else {
348-
return configurations?.values().next().value; // Any value is fine if the target doesn't match
369+
return fallbackConfiguration(configurations);
349370
}
350371
}
351372

test/unit-tests/cpptools.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,21 @@ suite('CppTools tests', () => {
335335
name: 'cpptools-test2',
336336
sourceDirectory: smokeFolder,
337337
targets: [
338+
{
339+
name: 'utilityTarget',
340+
type: 'UTILITY',
341+
fileGroups: [{
342+
sources: [sourceFile3],
343+
isGenerated: false
344+
}]
345+
},
338346
{
339347
name: 'target3',
340348
type: 'EXECUTABLE',
341349
fileGroups: [{
342350
sources: [sourceFile3],
343351
isGenerated: false,
352+
defines: ['DEFINE3'], // make this a more attractive fallback than utilityTarget
344353
compileCommandFragments: ['-DFRAGMENT3'],
345354
language: 'CXX'
346355
}]
@@ -390,7 +399,8 @@ suite('CppTools tests', () => {
390399
// Verify the browsePath with a different folder.
391400
const configurations2 = await provider.provideConfigurations([uri3]);
392401
expect(configurations2.length).to.eq(1);
393-
expect(configurations2[0].configuration.defines).to.be.empty;
402+
expect(configurations2[0].configuration.defines.length).to.eq(1);
403+
expect(configurations2[0].configuration.defines).to.contain('DEFINE3');
394404
expect(configurations2[0].configuration.compilerFragments).to.contain('-DFRAGMENT3');
395405
const browseConfig2 = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(smokeFolder));
396406
expect(browseConfig2?.browsePath.length).to.eq(1);

0 commit comments

Comments
 (0)