-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathcpptools.test.ts
More file actions
472 lines (435 loc) · 23.8 KB
/
cpptools.test.ts
File metadata and controls
472 lines (435 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* eslint-disable no-unused-expressions */
import { parseCompileFlags, getIntelliSenseMode, CppConfigurationProvider } from '@cmt/cpptools';
import { expect, getTestResourceFilePath } from '@test/util';
import { CMakeCache } from '@cmt/cache';
import * as path from 'path';
import * as codeModel from '@cmt/drivers/codeModel';
import * as vscode from 'vscode';
import { Version } from 'vscode-cpptools';
import * as util from '@cmt/util';
const here = __dirname;
suite('CppTools tests', () => {
test('Parse some compiler flags', () => {
// Parse definition
const cpptoolsVersion3 = Version.v3;
const cpptoolsVersion4 = Version.v4;
const cpptoolsVersion5 = Version.v5;
const cpptoolsVersion6 = Version.v6;
// Verify CppTools API version 6
let info = parseCompileFlags(cpptoolsVersion6, ['-std=c++23']);
expect(info.standard).to.eql(undefined);
info = parseCompileFlags(cpptoolsVersion6, ['-std=c++2b']);
expect(info.standard).to.eql(undefined);
info = parseCompileFlags(cpptoolsVersion6, ['-std=gnu++23']);
expect(info.standard).to.eql(undefined);
// Verify CppTools API version 5
info = parseCompileFlags(cpptoolsVersion5, ['-target', 'arm-arm-none-eabi']);
expect(info.targetArch).to.eql(undefined);
info = parseCompileFlags(cpptoolsVersion5, ['-std=c++23']);
expect(info.standard).to.eql('c++20');
info = parseCompileFlags(cpptoolsVersion5, ['-std=gnu++14']);
expect(info.standard).to.eql('gnu++14');
info = parseCompileFlags(cpptoolsVersion5, []);
expect(info.standard).to.eql(undefined);
info = parseCompileFlags(cpptoolsVersion5, ['/std:c++latest']);
expect(info.standard).to.eql('c++20');
// Verify CppTools API version 4
info = parseCompileFlags(cpptoolsVersion4, ['-DFOO=BAR']);
expect(info.extraDefinitions).to.eql(['FOO=BAR']);
info = parseCompileFlags(cpptoolsVersion4, ['-D', 'FOO=BAR']);
expect(info.extraDefinitions).to.eql(['FOO=BAR']);
info = parseCompileFlags(cpptoolsVersion4, ['-DFOO=BAR', '/D', 'BAZ=QUX']);
expect(info.extraDefinitions).to.eql(['FOO=BAR', 'BAZ=QUX']);
expect(info.standard).to.eql('c++17');
info = parseCompileFlags(cpptoolsVersion4, [], 'C');
expect(info.standard).to.eql('c11');
info = parseCompileFlags(cpptoolsVersion4, [], 'CXX');
expect(info.standard).to.eql('c++17');
info = parseCompileFlags(cpptoolsVersion4, [], 'CUDA');
expect(info.standard).to.eql('c++17');
// Parse language standard
info = parseCompileFlags(cpptoolsVersion4, ['-std=c++03']);
expect(info.standard).to.eql('c++03');
info = parseCompileFlags(cpptoolsVersion4, ['-std=gnu++14']);
expect(info.standard).to.eql('gnu++14');
info = parseCompileFlags(cpptoolsVersion4, ['-std=c17']);
expect(info.standard).to.eql('c17');
info = parseCompileFlags(cpptoolsVersion4, ['-std=c++123']);
expect(info.standard).to.eql('c++17');
info = parseCompileFlags(cpptoolsVersion4, ['-std=c123'], 'C');
expect(info.standard).to.eql('c11');
// Parse target architecture
info = parseCompileFlags(cpptoolsVersion4, ['--target=aarch64-arm-none-eabi']);
expect(info.targetArch).to.eql('arm64');
info = parseCompileFlags(cpptoolsVersion4, ['-target', 'arm64-arm-none-eabi']);
expect(info.targetArch).to.eql('arm64');
info = parseCompileFlags(cpptoolsVersion4, ['-target', 'arm-arm-none-eabi']);
expect(info.targetArch).to.eql('arm');
info = parseCompileFlags(cpptoolsVersion4, ['-arch=x86_64']);
expect(info.targetArch).to.eql('x64');
info = parseCompileFlags(cpptoolsVersion4, ['-arch', 'aarch64']);
expect(info.targetArch).to.eql('arm64');
info = parseCompileFlags(cpptoolsVersion4, ['-arch', 'arm64']);
expect(info.targetArch).to.eql('arm64');
info = parseCompileFlags(cpptoolsVersion4, ['-arch', 'arm']);
expect(info.targetArch).to.eql('arm');
info = parseCompileFlags(cpptoolsVersion4, ['-arch', 'i686']);
expect(info.targetArch).to.eql('x86');
info = parseCompileFlags(cpptoolsVersion4, ['/arch:x86_64']);
expect(info.targetArch).to.eql('x64');
info = parseCompileFlags(cpptoolsVersion4, ['-march=amd64']);
expect(info.targetArch).to.eql('x64');
info = parseCompileFlags(cpptoolsVersion4, ['-m32']);
expect(info.targetArch).to.eql('x86');
info = parseCompileFlags(cpptoolsVersion4, ['-m00']);
expect(info.targetArch).to.eql(undefined);
// Verify CppTools API version 3
info = parseCompileFlags(cpptoolsVersion3, ['-std=c++03']);
expect(info.standard).to.eql('c++03');
info = parseCompileFlags(cpptoolsVersion3, ['-std=gnu++14']);
expect(info.standard).to.eql('c++14');
info = parseCompileFlags(cpptoolsVersion3, ['-std=c17']);
expect(info.standard).to.eql('c11');
});
test('Get IntelliSenseMode', () => {
const cpptoolsVersion3 = Version.v3;
const cpptoolsVersion4 = Version.v4;
const cpptoolsVersion5 = Version.v5;
// Verify CppToolsAPI version 5
let mode = getIntelliSenseMode(cpptoolsVersion5, 'cl.exe', undefined);
expect(mode).to.eql(undefined);
mode = getIntelliSenseMode(cpptoolsVersion5, 'clang', undefined);
expect(mode).to.eql(undefined);
mode = getIntelliSenseMode(cpptoolsVersion5, 'clang', 'arm64');
expect(mode).to.eql('clang-arm64');
// Verify CppTools API version 4
mode = getIntelliSenseMode(cpptoolsVersion4, 'armclang', 'arm');
expect(mode).to.eql('clang-arm');
mode = getIntelliSenseMode(cpptoolsVersion4, 'armclang', 'arm64');
expect(mode).to.eql('clang-arm64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'armclang', undefined);
expect(mode).to.eql('clang-arm');
mode = getIntelliSenseMode(cpptoolsVersion4, 'clang', 'x64');
expect(mode).to.eql('clang-x64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'clang', 'arm64');
expect(mode).to.eql('clang-arm64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'clang', 'arm');
expect(mode).to.eql('clang-arm');
mode = getIntelliSenseMode(cpptoolsVersion4, 'gcc', undefined);
expect(mode).to.eql('gcc-x64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'gcc', 'arm64');
expect(mode).to.eql('gcc-arm64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'gcc', 'arm');
expect(mode).to.eql('gcc-arm');
mode = getIntelliSenseMode(cpptoolsVersion4, 'g++', 'x86');
expect(mode).to.eql('gcc-x86');
mode = getIntelliSenseMode(cpptoolsVersion4, 'arm-none-eabi-g++', undefined);
expect(mode).to.eql('gcc-arm');
mode = getIntelliSenseMode(cpptoolsVersion4, 'aarch64-linux-gnu-gcc', undefined);
expect(mode).to.eql('gcc-arm64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'bin//Hostx64//x64//cl.exe', undefined);
expect(mode).to.eql('msvc-x64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'bin//Hostx64//x86//cl.exe', undefined);
expect(mode).to.eql('msvc-x86');
mode = getIntelliSenseMode(cpptoolsVersion4, 'bin//Hostx64//ARM//cl.exe', undefined);
expect(mode).to.eql('msvc-arm');
mode = getIntelliSenseMode(cpptoolsVersion4, 'bin//Hostx64//ARM64//cl.exe', undefined);
expect(mode).to.eql('msvc-arm64');
mode = getIntelliSenseMode(cpptoolsVersion4, 'cl.exe', undefined);
expect(mode).to.eql('msvc-x64');
// Verify CppTools API version 3
mode = getIntelliSenseMode(cpptoolsVersion3, 'bin//Hostx64//arm//cl.exe', undefined);
expect(mode).to.eql('msvc-x86');
mode = getIntelliSenseMode(cpptoolsVersion3, 'bin//Hostx64//arm64//cl.exe', undefined);
expect(mode).to.eql('msvc-x64');
mode = getIntelliSenseMode(cpptoolsVersion3, 'arm-none-eabi-g++', undefined);
expect(mode).to.eql('gcc-x86');
mode = getIntelliSenseMode(cpptoolsVersion3, 'aarch64-linux-gnu-gcc', undefined);
expect(mode).to.eql('gcc-x64');
mode = getIntelliSenseMode(cpptoolsVersion3, 'clang', 'arm64');
expect(mode).to.eql('clang-x64');
mode = getIntelliSenseMode(cpptoolsVersion3, 'clang', 'arm');
expect(mode).to.eql('clang-x86');
});
// Validate codeModel using cppTools API V5
test('Validate code model V5', async () => {
const provider = new CppConfigurationProvider();
provider.cpptoolsVersion = Version.v5;
const cache = await CMakeCache.fromPath(getTestResourceFilePath('TestCMakeCache.txt'));
const sourceFile1 = path.join(here, 'main.cpp');
const uri1 = vscode.Uri.file(sourceFile1);
const codeModel1: codeModel.CodeModelContent = {
configurations: [{
name: "Release",
projects: [{
name: 'cpptools-test-1',
sourceDirectory: here,
targets: [
{
name: 'target1',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile1],
isGenerated: false,
compileCommandFragments: ['-DFLAG1'],
language: 'CXX'
}]
},
{
name: 'target2',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile1],
isGenerated: false,
compileCommandFragments: ['-DFLAG2'],
language: 'CXX'
}]
}
]
}]
}],
toolchains: new Map<string, codeModel.CodeModelToolchain>()
};
provider.updateConfigurationData({ cache, codeModelContent: codeModel1, activeTarget: 'target1', activeBuildTypeVariant: 'Release', folder: here });
// Update configuration with a 2nd workspace folder.
const smokeFolder = path.join(here, '../smoke');
const sourceFile3 = path.join(smokeFolder, 'main.cpp');
const uri3 = vscode.Uri.file(sourceFile3);
const codeModel3: codeModel.CodeModelContent = {
configurations: [{
name: 'Release',
projects: [{
name: 'cpptools-test-3',
sourceDirectory: smokeFolder,
targets: [
{
name: 'target3',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile3],
isGenerated: false,
compileCommandFragments: ['-DFLAG3'],
language: 'CXX'
}]
}]
}]
}],
toolchains: new Map<string, codeModel.CodeModelToolchain>([['CXX', { path: 'path_from_toolchain_object' }]])
};
provider.updateConfigurationData({ cache, codeModelContent: codeModel3, activeTarget: 'target3', activeBuildTypeVariant: 'Release', folder: smokeFolder });
let configurations = await provider.provideConfigurations([vscode.Uri.file(sourceFile3)]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.compilerPath).to.eq('path_from_toolchain_object');
configurations = await provider.provideConfigurations([uri1]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines).to.contain('FLAG1');
provider.updateConfigurationData({ cache, codeModelContent: codeModel1, activeTarget: 'target2', activeBuildTypeVariant: 'Release', folder: here });
configurations = await provider.provideConfigurations([uri1]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines).to.contain('FLAG2');
expect(configurations[0].configuration.compilerPath).to.eq('clang++');
provider.updateConfigurationData({ cache, codeModelContent: codeModel1, activeTarget: 'all', activeBuildTypeVariant: 'Release', folder: here });
configurations = await provider.provideConfigurations([uri1]);
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']);
// Verify the per-folder browsePath.
const canProvideBrowseConfigPerFolder: boolean = await provider.canProvideBrowseConfigurationsPerFolder();
expect(canProvideBrowseConfigPerFolder).to.eq(true);
const browseConfig = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(here));
expect(browseConfig?.browsePath.length).to.eq(1);
expect(browseConfig?.browsePath[0]).to.eq(util.platformNormalizePath(here));
// Verify the browsePath with a different folder.
const configurations2 = await provider.provideConfigurations([uri3]);
expect(configurations2.length).to.eq(1);
expect(configurations2[0].configuration.defines).to.contain('FLAG3');
const browseConfig2 = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(smokeFolder));
expect(browseConfig2?.browsePath.length).to.eq(1);
expect(browseConfig2?.browsePath[0]).to.eq(util.platformNormalizePath(smokeFolder));
});
// Validate codeModel using cppTools API V6
test('Validate code model latest', async () => {
const provider = new CppConfigurationProvider();
const cache = await CMakeCache.fromPath(getTestResourceFilePath('TestCMakeCache.txt'));
const sourceFile1 = path.join(here, 'main.cpp');
const headerFile1 = path.join(here, 'main.h');
const rcFile1 = path.join(here, 'main.rc');
const uri1 = vscode.Uri.file(sourceFile1);
const uri1_h = vscode.Uri.file(headerFile1);
const uri1_rc = vscode.Uri.file(rcFile1);
const codeModel1: codeModel.CodeModelContent = {
configurations: [{
name: "Release",
projects: [{
name: 'cpptools-test',
sourceDirectory: here,
targets: [
{
name: 'target1',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile1],
isGenerated: false,
defines: ['DEFINE1'],
compileCommandFragments: ['-DFRAGMENT1'],
language: 'CXX'
},
{
sources: [headerFile1],
isGenerated: false
},
{
sources: [rcFile1],
isGenerated: false,
compileCommandFragments: ['-DFRAGMENT_RC'],
language: 'RC'
}]
},
{
name: 'target2',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile1],
isGenerated: false,
defines: ['DEFINE2'],
compileCommandFragments: ['-DFRAGMENT2'],
language: 'CXX'
}]
}
]
}]
}],
toolchains: new Map<string, codeModel.CodeModelToolchain>()
};
provider.updateConfigurationData({ cache, codeModelContent: codeModel1, activeTarget: 'target1', activeBuildTypeVariant: 'Release', folder: here });
// Update configuration with a 2nd workspace folder.
const smokeFolder = path.join(here, '../smoke');
const sourceFile3 = path.join(smokeFolder, 'main.cpp');
const uri3 = vscode.Uri.file(sourceFile3);
const codeModel3: codeModel.CodeModelContent = {
configurations: [{
name: 'Release',
projects: [{
name: 'cpptools-test2',
sourceDirectory: smokeFolder,
targets: [
{
name: 'utilityTarget',
type: 'UTILITY',
fileGroups: [{
sources: [sourceFile3],
isGenerated: false
}]
},
{
name: 'target3',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile3],
isGenerated: false,
defines: ['DEFINE3'], // make this a more attractive fallback than utilityTarget
compileCommandFragments: ['-DFRAGMENT3'],
language: 'CXX'
}]
}]
}]
}],
toolchains: new Map<string, codeModel.CodeModelToolchain>([['CXX', { path: 'path_from_toolchain_object' }]])
};
provider.updateConfigurationData({ cache, codeModelContent: codeModel3, activeTarget: 'target3', activeBuildTypeVariant: 'Release', folder: smokeFolder });
let configurations = await provider.provideConfigurations([vscode.Uri.file(sourceFile3)]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.compilerPath).to.eq('path_from_toolchain_object');
configurations = await provider.provideConfigurations([uri1]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines).to.contain('DEFINE1');
expect(configurations[0].configuration.compilerFragments).to.contain('-DFRAGMENT1');
expect(configurations[0].configuration.compilerArgs).to.be.empty;
configurations = await provider.provideConfigurations([uri1_h]);
expect(configurations[0].configuration.compilerFragments).to.contain('-DFRAGMENT1');
configurations = await provider.provideConfigurations([uri1_rc]);
expect(configurations[0].configuration.compilerFragments).to.contain('-DFRAGMENT_RC');
provider.updateConfigurationData({ cache, codeModelContent: codeModel1, activeTarget: 'target2', activeBuildTypeVariant: 'Release', folder: here });
configurations = await provider.provideConfigurations([uri1]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines).to.contain('DEFINE2');
expect(configurations[0].configuration.compilerFragments).to.contain('-DFRAGMENT2');
expect(configurations[0].configuration.compilerPath).to.eq('clang++');
provider.updateConfigurationData({ cache, codeModelContent: codeModel1, activeTarget: 'all', activeBuildTypeVariant: 'Release', folder: here });
configurations = await provider.provideConfigurations([uri1]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines.some(def => def === 'DEFINE1' || def === 'DEFINE2')).to.be.true;
expect(configurations[0].configuration.defines).to.not.have.all.members(['DEFINE1', 'DEFINE2']);
// Verify the per-folder browsePath.
const canProvideBrowseConfigPerFolder: boolean = await provider.canProvideBrowseConfigurationsPerFolder();
expect(canProvideBrowseConfigPerFolder).to.eq(true);
const browseConfig = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(here));
expect(browseConfig?.browsePath.length).to.eq(1);
expect(browseConfig?.browsePath[0]).to.eq(util.platformNormalizePath(here));
// Verify the browsePath with a different folder.
const configurations2 = await provider.provideConfigurations([uri3]);
expect(configurations2.length).to.eq(1);
expect(configurations2[0].configuration.defines.length).to.eq(1);
expect(configurations2[0].configuration.defines).to.contain('DEFINE3');
expect(configurations2[0].configuration.compilerFragments).to.contain('-DFRAGMENT3');
const browseConfig2 = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(smokeFolder));
expect(browseConfig2?.browsePath.length).to.eq(1);
expect(browseConfig2?.browsePath[0]).to.eq(util.platformNormalizePath(smokeFolder));
});
test('Evicts stale file configurations after folder refresh', async () => {
const provider = new CppConfigurationProvider();
const cache = await CMakeCache.fromPath(getTestResourceFilePath('TestCMakeCache.txt'));
const folder = here;
const oldSourceFile = path.join(folder, 'stale_old.cpp');
const newSourceFile = path.join(folder, 'stale_new.cpp');
const oldCodeModel: codeModel.CodeModelContent = {
configurations: [{
name: 'Release',
projects: [{
name: 'stale-config-test',
sourceDirectory: folder,
targets: [{
name: 'oldTarget',
type: 'EXECUTABLE',
fileGroups: [{
sources: [oldSourceFile],
isGenerated: false,
defines: ['OLD'],
compileCommandFragments: ['-DOLD'],
language: 'CXX'
}]
}]
}]
}],
toolchains: new Map<string, codeModel.CodeModelToolchain>()
};
provider.updateConfigurationData({ cache, codeModelContent: oldCodeModel, activeTarget: 'oldTarget', activeBuildTypeVariant: 'Release', folder });
const newCodeModel: codeModel.CodeModelContent = {
configurations: [{
name: 'Release',
projects: [{
name: 'stale-config-test',
sourceDirectory: folder,
targets: [{
name: 'newTarget',
type: 'EXECUTABLE',
fileGroups: [{
sources: [newSourceFile],
isGenerated: false,
defines: ['NEW'],
compileCommandFragments: ['-DNEW'],
language: 'CXX'
}]
}]
}]
}],
toolchains: new Map<string, codeModel.CodeModelToolchain>()
};
provider.updateConfigurationData({ cache, codeModelContent: newCodeModel, activeTarget: 'newTarget', activeBuildTypeVariant: 'Release', folder });
const staleConfigurations = await provider.provideConfigurations([vscode.Uri.file(oldSourceFile)]);
expect(staleConfigurations.length).to.eq(0);
const activeConfigurations = await provider.provideConfigurations([vscode.Uri.file(newSourceFile)]);
expect(activeConfigurations.length).to.eq(1);
expect(activeConfigurations[0].configuration.defines).to.contain('NEW');
});
});