Skip to main content
summaryrefslogtreecommitdiffstats
blob: 46ebe372bc080894bfdcfc2d6c8e0f16b4314a2a (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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Andrew Ferguson (Symbian)
 *     Mike Kucera (IBM)
 *     Sergey Prigogin (Google)
 *     Nathan Ridge
 *******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;

import static org.eclipse.cdt.core.parser.ParserLanguage.CPP;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.parser.IScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.ANSICParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.tests.ASTComparer;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.model.ASTStringUtil;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser.scanner.CPreprocessor;

import junit.framework.AssertionFailedError;

/**
 * @author aniefer
 */
public class AST2TestBase extends SemanticTestBase {
	public final static String TEST_CODE = "<testcode>";
	protected static final IParserLogService NULL_LOG = new NullLogService();
	protected static boolean sValidateCopy;

	private static final ScannerInfo GNU_SCANNER_INFO = new ScannerInfo(getGnuMap());
	private static final ScannerInfo SCANNER_INFO = new ScannerInfo(getStdMap());

	private static Map<String, String> getGnuMap() {
		Map<String, String> map = new HashMap<>();
		map.put("__GNUC__", Integer.toString(GCC_MAJOR_VERSION_FOR_TESTS));
		map.put("__GNUC_MINOR__", Integer.toString(GCC_MINOR_VERSION_FOR_TESTS));
		map.put("__SIZEOF_SHORT__", "2");
		map.put("__SIZEOF_INT__", "4");
		map.put("__SIZEOF_LONG__", "8");
		map.put("__SIZEOF_DOUBLE__", "8");
		map.put("__SIZEOF_POINTER__", "8");
		return map;
	}

	private static Map<String, String> getStdMap() {
		Map<String, String> map = new HashMap<>();
		map.put("__SIZEOF_SHORT__", "2");
		map.put("__SIZEOF_INT__", "4");
		map.put("__SIZEOF_LONG__", "8");
		map.put("__SIZEOF_DOUBLE__", "8");
		map.put("__SIZEOF_POINTER__", "8");
		return map;
	}

	public AST2TestBase() {
		super();
	}

	public AST2TestBase(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		sValidateCopy = true;
		super.setUp();
	}

	protected IASTTranslationUnit parse(String code, ParserLanguage lang) throws ParserException {
		return parse(code, lang, false, true);
	}

	protected IASTTranslationUnit parse(String code, ParserLanguage lang, boolean useGNUExtensions)
			throws ParserException {
		return parse(code, lang, useGNUExtensions, true);
	}

	protected IASTTranslationUnit parse(String code, ParserLanguage lang, boolean useGNUExtensions,
			boolean expectNoProblems) throws ParserException {
		return parse(code, lang, useGNUExtensions, expectNoProblems, Integer.MAX_VALUE);
	}

	protected IASTTranslationUnit parse(String code, ParserLanguage lang, boolean useGNUExtensions,
			boolean expectNoProblems, int limitTrivialInitializers) throws ParserException {
		IScanner scanner = createScanner(FileContent.create(TEST_CODE, code.toCharArray()), lang,
				ParserMode.COMPLETE_PARSE, createScannerInfo(useGNUExtensions));
		configureScanner(scanner);
		AbstractGNUSourceCodeParser parser = null;
		if (lang == ParserLanguage.CPP) {
			ICPPParserExtensionConfiguration config = null;
			if (useGNUExtensions) {
				config = new GPPParserExtensionConfiguration();
			} else {
				config = new ANSICPPParserExtensionConfiguration();
			}
			parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config, null);
		} else {
			ICParserExtensionConfiguration config = null;

			if (useGNUExtensions) {
				config = new GCCParserExtensionConfiguration();
			} else {
				config = new ANSICParserExtensionConfiguration();
			}

			parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config, null);
		}
		parser.setMaximumTrivialExpressionsInAggregateInitializers(limitTrivialInitializers);

		IASTTranslationUnit tu = parser.parse();
		assertTrue(tu.isFrozen());
		if (sValidateCopy)
			validateCopy(tu);

		if (parser.encounteredError() && expectNoProblems)
			throw new ParserException("FAILURE"); //$NON-NLS-1$

		if (lang == ParserLanguage.C && expectNoProblems) {
			assertEquals(CVisitor.getProblems(tu).length, 0);
			assertEquals(tu.getPreprocessorProblems().length, 0);
		} else if (lang == ParserLanguage.CPP && expectNoProblems) {
			assertEquals(CPPVisitor.getProblems(tu).length, 0);
			assertEquals(0, tu.getPreprocessorProblems().length);
		}
		if (expectNoProblems)
			assertEquals(0, tu.getPreprocessorProblems().length);

		return tu;
	}

	public ScannerInfo createScannerInfo(boolean useGnu) {
		if (useGnu)
			return GNU_SCANNER_INFO;
		return SCANNER_INFO;
	}

	protected void configureScanner(IScanner scanner) {
	}

	public static IScanner createScanner(FileContent codeReader, ParserLanguage lang, ParserMode mode,
			IScannerInfo scannerInfo) {
		IScannerExtensionConfiguration configuration = null;
		if (lang == ParserLanguage.C) {
			configuration = GCCScannerExtensionConfiguration.getInstance(scannerInfo);
		} else {
			configuration = GPPScannerExtensionConfiguration.getInstance(scannerInfo);
		}
		IScanner scanner;
		scanner = new CPreprocessor(codeReader, scannerInfo, lang, NULL_LOG, configuration,
				IncludeFileContentProvider.getSavedFilesProvider());
		return scanner;
	}

	protected void validateSimplePostfixInitializerExpressionC(String code) throws ParserException {
		ICASTTypeIdInitializerExpression e = (ICASTTypeIdInitializerExpression) getExpressionFromStatementInCode(code,
				ParserLanguage.C);
		assertNotNull(e);
		assertNotNull(e.getTypeId());
		assertNotNull(e.getInitializer());
	}

	protected void validateSimpleUnaryTypeIdExpression(String code, int op) throws ParserException {
		IASTCastExpression e = (IASTCastExpression) getExpressionFromStatementInCode(code, ParserLanguage.C);
		assertNotNull(e);
		assertEquals(e.getOperator(), op);
		assertNotNull(e.getTypeId());
		IASTIdExpression x = (IASTIdExpression) e.getOperand();
		assertEquals(x.getName().toString(), "x"); //$NON-NLS-1$
	}

	protected void validateSimpleTypeIdExpressionC(String code, int op) throws ParserException {
		IASTTypeIdExpression e = (IASTTypeIdExpression) getExpressionFromStatementInCode(code, ParserLanguage.C);
		assertNotNull(e);
		assertEquals(e.getOperator(), op);
		assertNotNull(e.getTypeId());
	}

	protected void validateSimpleUnaryExpressionC(String code, int operator) throws ParserException {
		IASTUnaryExpression e = (IASTUnaryExpression) getExpressionFromStatementInCode(code, ParserLanguage.C);
		assertNotNull(e);
		assertEquals(e.getOperator(), operator);
		IASTIdExpression x = (IASTIdExpression) e.getOperand();
		assertEquals(x.getName().toString(), "x"); //$NON-NLS-1$
	}

	protected void validateConditionalExpressionC(String code) throws ParserException {
		IASTConditionalExpression e = (IASTConditionalExpression) getExpressionFromStatementInCode(code,
				ParserLanguage.C);
		assertNotNull(e);
		IASTIdExpression x = (IASTIdExpression) e.getLogicalConditionExpression();
		assertEquals(x.getName().toString(), "x"); //$NON-NLS-1$
		IASTIdExpression y = (IASTIdExpression) e.getPositiveResultExpression();
		assertEquals(y.getName().toString(), "y"); //$NON-NLS-1$
		IASTIdExpression x2 = (IASTIdExpression) e.getNegativeResultExpression();
		assertEquals(x.getName().toString(), x2.getName().toString());
	}

	protected void validateSimpleBinaryExpressionC(String code, int operand) throws ParserException {
		IASTBinaryExpression e = (IASTBinaryExpression) getExpressionFromStatementInCode(code, ParserLanguage.C);
		assertNotNull(e);
		assertEquals(e.getOperator(), operand);
		IASTIdExpression x = (IASTIdExpression) e.getOperand1();
		assertEquals(x.getName().toString(), "x"); //$NON-NLS-1$
		IASTIdExpression y = (IASTIdExpression) e.getOperand2();
		assertEquals(y.getName().toString(), "y"); //$NON-NLS-1$
	}

	protected IASTExpression getExpressionFromStatementInCode(String code, ParserLanguage language)
			throws ParserException {
		StringBuilder buffer = new StringBuilder("void f() { "); //$NON-NLS-1$
		buffer.append("int x, y;\n"); //$NON-NLS-1$
		buffer.append(code);
		buffer.append(";\n}"); //$NON-NLS-1$
		IASTTranslationUnit tu = parse(buffer.toString(), language);
		IASTFunctionDefinition f = (IASTFunctionDefinition) tu.getDeclarations()[0];
		IASTCompoundStatement cs = (IASTCompoundStatement) f.getBody();
		IASTExpressionStatement s = (IASTExpressionStatement) cs.getStatements()[1];
		return s.getExpression();
	}

	protected <T extends IASTNode> T validateCopy(T tu) {
		IASTNode copy = tu.copy();
		assertFalse(copy.isFrozen());
		ASTComparer.assertCopy(tu, copy);
		return (T) copy;
	}

	static protected class NameCollector extends ASTVisitor {
		public NameCollector() {
			this(false); // don't visit implicit names by default
		}

		public NameCollector(boolean shouldVisitImplicitNames) {
			this.shouldVisitNames = true;
			this.shouldVisitImplicitNames = shouldVisitImplicitNames;
		}

		public List<IASTName> nameList = new ArrayList<>();

		@Override
		public int visit(IASTName name) {
			nameList.add(name);
			return PROCESS_CONTINUE;
		}

		public IASTName getName(int idx) {
			if (idx < 0 || idx >= nameList.size())
				return null;
			return nameList.get(idx);
		}

		public int size() {
			return nameList.size();
		}

		public void dump() {
			for (int i = 0; i < size(); i++) {
				IASTName name = getName(i);
				String parent = name.getParent() != null ? name.getParent().getRawSignature() : "";
				System.out.println(i + ": #" + name.getRawSignature() + "# " + parent);
			}
		}
	}

	protected void assertInstances(NameCollector collector, IBinding binding, int num) throws Exception {
		int count = 0;
		for (int i = 0; i < collector.size(); i++) {
			if (collector.getName(i).resolveBinding() == binding)
				count++;
		}

		assertEquals(num, count);
	}

	protected void isExpressionStringEqual(IASTInitializerClause exp, String str) {
		String expressionString = ASTStringUtil.getExpressionString((IASTExpression) exp);
		assertEquals(str, expressionString);
	}

	protected void isExpressionStringEqual(IASTExpression exp, String str) {
		String expressionString = ASTStringUtil.getExpressionString(exp);
		assertEquals(str, expressionString);
	}

	protected void isParameterSignatureEqual(IASTDeclarator decltor, String str) {
		assertTrue(decltor instanceof IASTFunctionDeclarator);
		final String[] sigArray = ASTStringUtil.getParameterSignatureArray((IASTFunctionDeclarator) decltor);
		assertEquals(str, "(" + ASTStringUtil.join(sigArray, ", ") + ")");
	}

	protected void isSignatureEqual(IASTDeclarator declarator, String expected) {
		String signature = ASTStringUtil.getSignatureString(declarator);
		assertEquals(expected, signature);
	}

	protected void isSignatureEqual(IASTDeclSpecifier declSpec, String str) {
		assertEquals(str, ASTStringUtil.getSignatureString(declSpec, null));
	}

	protected void isSignatureEqual(IASTTypeId typeId, String str) {
		assertEquals(str, ASTStringUtil.getSignatureString(typeId.getDeclSpecifier(), typeId.getAbstractDeclarator()));
	}

	protected void isTypeEqual(IASTDeclarator decltor, String str) {
		assertEquals(str, ASTTypeUtil.getType(decltor));
	}

	protected void isTypeEqual(IASTTypeId typeId, String str) {
		assertEquals(str, ASTTypeUtil.getType(typeId));
	}

	protected void isTypeEqual(IType type, String str) {
		assertEquals(str, ASTTypeUtil.getType(type));
	}

	protected void isParameterTypeEqual(IFunctionType fType, String str) {
		assertEquals(str, ASTTypeUtil.getParameterTypeString(fType));
	}

	static protected class CNameResolver extends ASTVisitor {
		{
			shouldVisitNames = true;
		}
		public int numProblemBindings;
		public int numNullBindings;
		public List<IASTName> nameList = new ArrayList<>();

		@Override
		public int visit(IASTName name) {
			nameList.add(name);
			IBinding binding = name.resolveBinding();
			if (binding instanceof IProblemBinding)
				numProblemBindings++;
			if (binding == null)
				numNullBindings++;
			return PROCESS_CONTINUE;
		}

		public IASTName getName(int idx) {
			if (idx < 0 || idx >= nameList.size())
				return null;
			return nameList.get(idx);
		}

		public int size() {
			return nameList.size();
		}
	}

	static protected class CPPNameResolver extends ASTVisitor {
		{
			shouldVisitNames = true;
		}
		public int numProblemBindings;
		public int numNullBindings;
		public List<IASTName> nameList = new ArrayList<>();

		@Override
		public int visit(IASTName name) {
			nameList.add(name);
			IBinding binding = name.resolveBinding();
			if (binding instanceof IProblemBinding)
				numProblemBindings++;
			if (binding == null)
				numNullBindings++;
			return PROCESS_CONTINUE;
		}

		public IASTName getName(int idx) {
			if (idx < 0 || idx >= nameList.size())
				return null;
			return nameList.get(idx);
		}

		public int size() {
			return nameList.size();
		}
	}

	protected String getAboveComment() throws IOException {
		return getContents(1)[0].toString();
	}

	protected CharSequence[] getContents(int sections) throws IOException {
		CTestPlugin plugin = CTestPlugin.getDefault();
		if (plugin == null)
			throw new AssertionFailedError("This test must be run as a JUnit plugin test");
		return TestSourceReader.getContentsForTest(plugin.getBundle(), "parser", getClass(), getName(), sections);
	}

	protected static void assertField(IBinding binding, String fieldName, String ownerName) {
		assertInstance(binding, IField.class);
		assertEquals(fieldName, binding.getName());
		ICompositeType struct = ((IField) binding).getCompositeTypeOwner();
		assertEquals(ownerName, struct.getName());
	}

	protected static void assertConstantValue(long expected, IVariable constant) {
		IValue value = constant.getInitialValue();
		assertNotNull(value);
		Number numericalValue = value.numberValue();
		assertNotNull(numericalValue);
		assertEquals(expected, numericalValue.longValue());
	}

	protected class AST2AssertionHelper extends BindingAssertionHelper {
		protected boolean isCPP;

		public AST2AssertionHelper(String contents, boolean isCPP) throws ParserException {
			this(contents, isCPP ? ParserLanguage.CPP : ParserLanguage.C);
		}

		public AST2AssertionHelper(String contents, ParserLanguage lang) throws ParserException {
			super(contents, parse(contents, lang, true, false));
			this.isCPP = lang.isCPP();
		}
	}

	final protected IASTTranslationUnit parseAndCheckBindings(String code, ParserLanguage lang) throws Exception {
		return parseAndCheckBindings(code, lang, false);
	}

	final protected IASTTranslationUnit parseAndCheckBindings(String code, ParserLanguage lang,
			boolean useGnuExtensions) throws Exception {
		return parseAndCheckBindings(code, lang, useGnuExtensions, Integer.MAX_VALUE);
	}

	final protected IASTTranslationUnit parseAndCheckBindings(String code, ParserLanguage lang,
			boolean useGnuExtensions, int limitTrivialInitializers) throws Exception {
		IASTTranslationUnit tu = parse(code, lang, useGnuExtensions, true, limitTrivialInitializers);
		NameCollector col = new NameCollector();
		tu.accept(col);
		assertNoProblemBindings(col);
		return tu;
	}

	final protected IASTTranslationUnit parseAndCheckImplicitNameBindings() throws Exception {
		IASTTranslationUnit tu = parse(getAboveComment(), CPP, false, true);
		NameCollector col = new NameCollector(true /* Visit implicit names */);
		tu.accept(col);
		assertNoProblemBindings(col);
		return tu;
	}

	protected BindingAssertionHelper getAssertionHelper(ParserLanguage lang) throws ParserException, IOException {
		String code = getAboveComment();
		return new AST2AssertionHelper(code, lang);
	}

	final protected void assertNoProblemBindings(NameCollector col) {
		for (IASTName n : col.nameList) {
			if (n.getRawSignature().contains("waldo")) {
				n.resolveBinding();
			}
			assertFalse("ProblemBinding for " + n.getRawSignature(), n.resolveBinding() instanceof IProblemBinding);
		}
	}

	final protected void assertProblemBindings(NameCollector col, int count) {
		int sum = 0;
		for (IASTName n : col.nameList) {
			if (n.resolveBinding() instanceof IProblemBinding)
				++sum;
		}
		assertEquals(count, sum);
	}

	final protected <T extends IASTDeclaration> T getDeclaration(IASTTranslationUnit tu, int i_decl) {
		Class<T> tclass;
		IASTDeclaration[] decls = tu.getDeclarations();
		assertTrue(decls.length > i_decl);
		return (T) decls[i_decl];
	}

	final protected <T extends IASTDeclaration> T getDeclaration(ICPPASTNamespaceDefinition ns, int i_decl) {
		Class<T> tclass;
		IASTDeclaration[] decls = ns.getDeclarations();
		assertTrue(decls.length > i_decl);
		return (T) decls[i_decl];
	}

	final protected <T extends IASTDeclaration> T getDeclaration(ICPPASTLinkageSpecification ls, int i_decl) {
		Class<T> tclass;
		IASTDeclaration[] decls = ls.getDeclarations();
		assertTrue(decls.length > i_decl);
		return (T) decls[i_decl];
	}

	final protected <T extends IASTDeclaration> T getDeclaration(IASTCompositeTypeSpecifier ct, int i_decl) {
		Class<T> tclass;
		IASTDeclaration[] decls = ct.getMembers();
		assertTrue(decls.length > i_decl);
		return (T) decls[i_decl];
	}

	final protected <T extends IASTCompositeTypeSpecifier> T getCompositeType(IASTTranslationUnit tu, int i_decl) {
		IASTSimpleDeclaration sdecl = getDeclaration(tu, i_decl);
		return (T) sdecl.getDeclSpecifier();
	}

	final protected <T extends IASTStatement> T getStatement(IASTFunctionDefinition fdef, int i_stmt) {
		return getStatement((IASTCompoundStatement) fdef.getBody(), i_stmt);
	}

	final protected <T extends IASTStatement> T getStatement(IASTCompoundStatement compound, int i_stmt) {
		IASTStatement[] stmts = compound.getStatements();
		assertTrue(stmts.length > i_stmt);
		return (T) stmts[i_stmt];
	}

	final protected <T extends IASTExpression> T getExpressionOfStatement(IASTFunctionDefinition fdef, int i) {
		IASTStatement stmt = getStatement(fdef, i);
		assertInstance(stmt, IASTExpressionStatement.class);
		return (T) ((IASTExpressionStatement) stmt).getExpression();
	}

	/**
	 * Sort the given array of AST names lexicographically.
	 */
	protected static <T extends IASTName> void sortNames(T[] names) {
		Arrays.sort(names, new Comparator<IASTName>() {
			@Override
			public int compare(IASTName a, IASTName b) {
				return a.toString().compareTo(b.toString());
			}
		});
	}
}

Back to the top