Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b0fd474150bb305a8bf4fef684350656f2934cfe (plain) (blame)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*******************************************************************************
 * Copyright (c) 2009, 2019 Andrew Gvozdev and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Andrew Gvozdev - initial API and implementation
 *     Marc-Andre Laperle - Adapted to MSVC
 *******************************************************************************/
package org.eclipse.cdt.internal.msw.build.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.util.List;

import org.eclipse.cdt.core.language.settings.providers.LanguageSettingsManager;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.CIncludeFileEntry;
import org.eclipse.cdt.core.settings.model.CIncludePathEntry;
import org.eclipse.cdt.core.settings.model.CMacroEntry;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.cdt.core.testplugin.ResourceHelper;
import org.eclipse.cdt.internal.core.language.settings.providers.LanguageSettingsProvidersSerializer;
import org.eclipse.cdt.managedbuilder.language.settings.providers.AbstractBuildCommandParser;
import org.eclipse.cdt.msw.build.core.MSVCBuildCommandParser;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.Job;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Test cases to test build command parsers.
 */
public class MSVCBuildCommandParserTests {
	// ID of the parser taken from the extension point
	private static final String MSVC_BUILD_COMMAND_PARSER_EXT = "org.eclipse.cdt.msw.build.core.MSVCBuildCommandParser"; //$NON-NLS-1$
	private static final String PROJECT_NAME = "MSVCBuildCommandParserTest";

	@Before
	public void setUp() {
		//We can only run this on Windows because of Windows-style paths, i.e. C:\
		assumeTrue(Platform.getOS().equals(Platform.OS_WIN32));
	}

	@After
	public void tearDown() throws Exception {
		try {
			Job.getJobManager().join(AbstractBuildCommandParser.JOB_FAMILY_BUILD_COMMAND_PARSER, null);
			Job.getJobManager().join(LanguageSettingsProvidersSerializer.JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_PROJECT,
					null);
			Job.getJobManager()
					.join(LanguageSettingsProvidersSerializer.JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_WORKSPACE, null);
		} catch (Exception e) {
			// ignore
		}
		ResourceHelper.cleanUp(PROJECT_NAME);
	}

	/**
	 * Helper method to fetch configuration descriptions.
	 */
	private ICConfigurationDescription[] getConfigurationDescriptions(IProject project) {
		CoreModel coreModel = CoreModel.getDefault();
		ICProjectDescriptionManager mngr = coreModel.getProjectDescriptionManager();
		// project description
		ICProjectDescription projectDescription = mngr.getProjectDescription(project, false);
		assertNotNull(projectDescription);
		assertEquals(1, projectDescription.getConfigurations().length);
		// configuration description
		ICConfigurationDescription[] cfgDescriptions = projectDescription.getConfigurations();
		return cfgDescriptions;
	}

	/**
	 * Test parsing of one typical entry.
	 */
	@Test
	public void testOneEntry() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl /IC:\\path0 file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		{
			CIncludePathEntry expected = new CIncludePathEntry("C:/path0", 0);
			CIncludePathEntry entry = (CIncludePathEntry) entries.get(0);
			assertEquals(expected.getName(), entry.getName());
			assertEquals(expected.getValue(), entry.getValue());
			assertEquals(expected.getKind(), entry.getKind());
			assertEquals(expected.getFlags(), entry.getFlags());
			assertEquals(expected, entry);
		}
	}

	/**
	 * Test possible variations of compiler command.
	 */
	@Test
	public void testClFlavors() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file1 = ResourceHelper.createFile(project, "file1.cpp");
		IFile file2 = ResourceHelper.createFile(project, "file2.cpp");
		IFile file3 = ResourceHelper.createFile(project, "file3.cpp");
		IFile file4 = ResourceHelper.createFile(project, "file4.cpp");
		IFile file5 = ResourceHelper.createFile(project, "file5.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file1.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl /IC:\\path0 file1.cpp");
		parser.processLine("cl.exe /IC:\\path0 file2.cpp");
		parser.processLine("clang-cl /IC:\\path0 file3.cpp");
		parser.processLine("clang-cl.exe /IC:\\path0 file4.cpp");
		parser.processLine("C:\\absolute\\path\\cl.exe /IC:\\path0 file5.cpp");
		parser.shutdown();

		// check populated entries
		{
			List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file1, languageId);
			assertEquals(new CIncludePathEntry("C:/path0", 0), entries.get(0));
		}
		{
			List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file2, languageId);
			assertEquals(new CIncludePathEntry("C:/path0", 0), entries.get(0));
		}
		{
			List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file3, languageId);
			assertEquals(new CIncludePathEntry("C:/path0", 0), entries.get(0));
		}
		{
			List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file4, languageId);
			assertEquals(new CIncludePathEntry("C:/path0", 0), entries.get(0));
		}
		{
			List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file5, languageId);
			assertEquals(new CIncludePathEntry("C:/path0", 0), entries.get(0));
		}
	}

	/**
	 * Parse variations of /I options.
	 */
	@Test
	public void testCIncludePathEntry() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl"
				// regular
				+ " /IC:\\path0 "
				// space after /I
				+ " /I C:\\path1 "
				// unknown option, should be ignored
				+ " /? "
				// double-quoted path with spaces
				+ " /I\"C:\\path with spaces\"" + " /I\"C:\\backslash at end\\\\\"" + " /I\"..\\..\\relative\""
				+ " /I\"..\\..\\relative with spaces\"" + " file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		CIncludePathEntry expected = new CIncludePathEntry("C:/path0", 0);
		CIncludePathEntry entry = (CIncludePathEntry) entries.get(0);
		assertEquals(expected.getName(), entry.getName());
		assertEquals(expected.getValue(), entry.getValue());
		assertEquals(expected.getKind(), entry.getKind());
		assertEquals(expected.getFlags(), entry.getFlags());
		assertEquals(expected, entry);

		assertEquals(new CIncludePathEntry("C:/path1", 0), entries.get(1));
		assertEquals(new CIncludePathEntry("C:/path with spaces", 0), entries.get(2));
		assertEquals(new CIncludePathEntry("C:/backslash at end", 0), entries.get(3));
		assertEquals(new CIncludePathEntry(project.getLocation().removeLastSegments(2).append("relative"), 0),
				entries.get(4));
		assertEquals(
				new CIncludePathEntry(project.getLocation().removeLastSegments(2).append("relative with spaces"), 0),
				entries.get(5));
	}

	/**
	 * Parse /imsvc (clang-cl)
	 */
	@Test
	public void testCIncludePathEntry_ClangCLSystemIncludePaths() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl" + " /imsvcC:\\path0 " + " /imsvc C:\\path1 " + " /imsvc\"C:\\path with spaces\""
				+ " /imsvc\"C:\\backslash at end\\\\\"" + " /imsvc\"..\\..\\relative\""
				+ " /imsvc\"..\\..\\relative with spaces\"" + " file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		CIncludePathEntry expected = new CIncludePathEntry("C:/path0", 0);
		CIncludePathEntry entry = (CIncludePathEntry) entries.get(0);
		assertEquals(expected.getName(), entry.getName());
		assertEquals(expected.getValue(), entry.getValue());
		assertEquals(expected.getKind(), entry.getKind());
		assertEquals(expected.getFlags(), entry.getFlags());
		assertEquals(expected, entry);

		assertEquals(new CIncludePathEntry("C:/path1", 0), entries.get(1));
		assertEquals(new CIncludePathEntry("C:/path with spaces", 0), entries.get(2));
		assertEquals(new CIncludePathEntry("C:/backslash at end", 0), entries.get(3));
		assertEquals(new CIncludePathEntry(project.getLocation().removeLastSegments(2).append("relative"), 0),
				entries.get(4));
		assertEquals(
				new CIncludePathEntry(project.getLocation().removeLastSegments(2).append("relative with spaces"), 0),
				entries.get(5));
	}

	/**
	 * Parse variations of /D options.
	 */
	@Test
	public void testCMacroEntry() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl " + " /DMACRO0" + " /DMACRO1=value   " + " /DMACRO2=\"value with spaces\"   "
				+ " /DMACRO3='c'   " + " /DMACRO4='\\\"'   " + " /DMACRO6=\\\"quotedvalue\\\"   "
				+ " /DMACRO7=\"'single-quotedvalue'\"   " + " /DMACRO8=\"\\\"escape-quoted value\\\"\"   "
				+ " /D MACRO9=\"\\\"with space\\\"\"   " + " /D\"MACROA=\\\"value\\\"\"   "
				+ " /D\"MACROB=\\\"val\\\"\\\"ue\\\"\"   " + " /D\"MACROC=\\\"\\\\\\\"\"   " + " /D\"MACROD\"   "
				+ " /D\"MACROE=\\\"with space\\\"\"   " + "-DMACROF -DMACROG" + " file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		CMacroEntry expected = new CMacroEntry("MACRO0", "1", 0);
		CMacroEntry entry = (CMacroEntry) entries.get(0);
		assertEquals(expected.getName(), entry.getName());
		assertEquals(expected.getValue(), entry.getValue());
		assertEquals(expected.getKind(), entry.getKind());
		assertEquals(expected.getFlags(), entry.getFlags());
		assertEquals(expected, entry);

		assertEquals(new CMacroEntry("MACRO1", "value", 0), entries.get(1));
		assertEquals(new CMacroEntry("MACRO2", "value with spaces", 0), entries.get(2));
		assertEquals(new CMacroEntry("MACRO3", "'c'", 0), entries.get(3));
		assertEquals(new CMacroEntry("MACRO4", "'\"'", 0), entries.get(4));
		assertEquals(new CMacroEntry("MACRO6", "\"quotedvalue\"", 0), entries.get(5));
		assertEquals(new CMacroEntry("MACRO7", "'single-quotedvalue'", 0), entries.get(6));
		assertEquals(new CMacroEntry("MACRO8", "\"escape-quoted value\"", 0), entries.get(7));
		assertEquals(new CMacroEntry("MACRO9", "\"with space\"", 0), entries.get(8));
		assertEquals(new CMacroEntry("MACROA", "\"value\"", 0), entries.get(9));
		assertEquals(new CMacroEntry("MACROB", "\"val\"\"ue\"", 0), entries.get(10));
		assertEquals(new CMacroEntry("MACROC", "\"\\\"", 0), entries.get(11));
		assertEquals(new CMacroEntry("MACROD", "1", 0), entries.get(12));
		assertEquals(new CMacroEntry("MACROE", "\"with space\"", 0), entries.get(13));
		assertEquals(new CMacroEntry("MACROF", "1", 0), entries.get(14));
		assertEquals(new CMacroEntry("MACROG", "1", 0), entries.get(15));
	}

	/**
	 * Parse /U option.
	 */
	@Test
	public void testCMacroEntry_undef() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl " + " /UMACRO /U MACRO2 /U\"MACRO3\" " + " file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		assertEquals(new CMacroEntry("MACRO", null, ICSettingEntry.UNDEFINED), entries.get(0));
		assertEquals(new CMacroEntry("MACRO2", null, ICSettingEntry.UNDEFINED), entries.get(1));
		assertEquals(new CMacroEntry("MACRO3", null, ICSettingEntry.UNDEFINED), entries.get(2));
		//		assertEquals(new CMacroEntry("MACRO4", null, ICSettingEntry.UNDEFINED), entries.get(3));
	}

	/**
	 * Parse variations of force include (/FI) options.
	 */
	@Test
	public void testCIncludeFileEntry() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl " + " /FI C:\\include.file1" + " /FI \"include.file with spaces\""
				+ " /FI \"C:\\include.file with spaces\"" + " /FI ..\\..\\include.file2" + " /FI include.file3"
				+ " file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		CIncludeFileEntry expected = new CIncludeFileEntry("C:/include.file1", 0);
		CIncludeFileEntry entry = (CIncludeFileEntry) entries.get(0);
		assertEquals(expected.getName(), entry.getName());
		assertEquals(expected.getValue(), entry.getValue());
		assertEquals(expected.getKind(), entry.getKind());
		assertEquals(expected.getFlags(), entry.getFlags());
		assertEquals(expected, entry);

		assertEquals(
				new CIncludeFileEntry("/${ProjName}/include.file with spaces", ICSettingEntry.VALUE_WORKSPACE_PATH),
				entries.get(1));
		assertEquals(new CIncludeFileEntry("C:/include.file with spaces", 0), entries.get(2));
		assertEquals(new CIncludeFileEntry(project.getLocation().removeLastSegments(2).append("include.file2"), 0),
				entries.get(3));
		assertEquals(new CIncludeFileEntry("/${ProjName}/include.file3", ICSettingEntry.VALUE_WORKSPACE_PATH),
				entries.get(4));
	}

	/**
	 * Parse command where resource is missing.
	 */
	@Test
	public void testFileMissing() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl /IC:\\path0 missing.cpp");
		parser.shutdown();

		// check entries
		assertTrue(parser.isEmpty());
	}

	/**
	 * Parsing of absolute path to the file being compiled.
	 */
	@Test
	public void testFileAbsolutePath() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		IFile file = ResourceHelper.createFile(project, "file.cpp");
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl " + "/IC:\\path0 " + "/I. " + '"' + file.getLocation().toOSString() + '"');
		parser.shutdown();

		// check entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		assertEquals(new CIncludePathEntry("C:/path0", 0), entries.get(0));
		assertEquals(new CIncludePathEntry("/${ProjName}/", ICSettingEntry.VALUE_WORKSPACE_PATH), entries.get(1));
	}

	/**
	 * Parsing where the name of the file being compiled contains spaces.
	 */
	@Test
	public void testFileWithSpaces() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file1 = ResourceHelper.createFile(project, "file with spaces 1.cpp");
		IFile file2 = ResourceHelper.createFile(project, "file with spaces 2.cpp");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file1.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl /IC:\\path0 \"file with spaces 1.cpp\"");
		parser.processLine("cl /IC:\\path0 \"" + file2.getLocation().toOSString() + "\"");
		parser.shutdown();

		// check populated entries
		// in double quotes
		assertEquals(new CIncludePathEntry("C:/path0", 0),
				parser.getSettingEntries(cfgDescription, file1, languageId).get(0));
		// Absolute path
		assertEquals(new CIncludePathEntry("C:/path0", 0),
				parser.getSettingEntries(cfgDescription, file2, languageId).get(0));
	}

	/**
	 * Ensure that duplicate paths are ignored.
	 */
	@Test
	public void testPathEntry_DuplicatePath() throws Exception {
		// Create model project and accompanied descriptions
		IProject project = ResourceHelper.createCDTProjectWithConfig(PROJECT_NAME);
		ICConfigurationDescription[] cfgDescriptions = getConfigurationDescriptions(project);
		ICConfigurationDescription cfgDescription = cfgDescriptions[0];

		IFile file = ResourceHelper.createFile(project, "file.cpp");
		IFolder folder = ResourceHelper.createFolder(project, "Folder");
		ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(file.getProjectRelativePath(), true);
		String languageId = ls.getLanguageId();

		// create MSVCBuildCommandParser
		MSVCBuildCommandParser parser = (MSVCBuildCommandParser) LanguageSettingsManager
				.getExtensionProviderCopy(MSVC_BUILD_COMMAND_PARSER_EXT, true);
		parser.setResolvingPaths(true);

		// parse line
		parser.startup(cfgDescription, null);
		parser.processLine("cl " + " /IFolder" + " /IFolder" + " file.cpp");
		parser.shutdown();

		// check populated entries
		List<ICLanguageSettingEntry> entries = parser.getSettingEntries(cfgDescription, file, languageId);
		assertEquals(new CIncludePathEntry("/${ProjName}/" + folder.getProjectRelativePath(),
				ICSettingEntry.VALUE_WORKSPACE_PATH), entries.get(0));
		assertEquals(1, entries.size());
	}
}

Back to the top