Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41afc86d097b8c4960f36c4e00a8165b8c226f38 (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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/**********************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: 
 * IBM - Initial API and implementation
 **********************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;

import java.util.Collections;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
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.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
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.IASTInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserFactory;
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.parser2.QuickParser2Tests.ProblemCollector;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser2.ISourceCodeParser;
import org.eclipse.cdt.internal.core.parser2.c.ANSICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser2.c.CVisitor;
import org.eclipse.cdt.internal.core.parser2.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.parser2.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser2.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser2.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.parser2.cpp.ICPPParserExtensionConfiguration;

/**
 * Test the new AST.
 * 
 * @author Doug Schaefer
 */
public class AST2Tests extends TestCase {

	private static final ISourceElementRequestor NULL_REQUESTOR = new NullSourceElementRequestor();
    private static final IParserLogService NULL_LOG = new NullLogService();
    
	/**
     * @param string
     * @param c
     * @return
	 * @throws ParserException
     */
    protected IASTTranslationUnit parse(String code, ParserLanguage lang) throws ParserException {
        ProblemCollector collector = new ProblemCollector();
        IScanner scanner = ParserFactory.createScanner(new CodeReader(code
                .toCharArray()), new ScannerInfo(), ParserMode.COMPLETE_PARSE,
                lang, NULL_REQUESTOR,
                NULL_LOG, Collections.EMPTY_LIST);
        ISourceCodeParser parser2 = null;
        if( lang == ParserLanguage.CPP )
        {
            ICPPParserExtensionConfiguration config = null;
            config = new ANSICPPParserExtensionConfiguration();
            parser2 = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, collector,
                NULL_LOG,
                config );
        }
        else
        {
            ICParserExtensionConfiguration config = null;
             config = new ANSICParserExtensionConfiguration();
            
            parser2 = new GNUCSourceParser( scanner, ParserMode.COMPLETE_PARSE, collector, 
                NULL_LOG, config );
        }
        IASTTranslationUnit tu = parser2.parse();
        if( parser2.encounteredError() )
            throw new ParserException( "FAILURE"); //$NON-NLS-1$
        
        assertTrue( collector.hasNoProblems() );

        return tu;
    }


    public void testBasicFunction() throws ParserException {
		StringBuffer buff = new StringBuffer();
		buff.append("int x;\n"); //$NON-NLS-1$
		buff.append("void f(int y) {\n"); //$NON-NLS-1$
		buff.append("   int z = x + y;\n"); //$NON-NLS-1$
		buff.append("}\n"); //$NON-NLS-1$

		IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C );
		IScope globalScope = tu.getScope();

		List declarations = tu.getDeclarations();

		// int x
		IASTSimpleDeclaration decl_x = (IASTSimpleDeclaration) declarations
				.get(0);
		IASTSimpleDeclSpecifier declspec_x = (IASTSimpleDeclSpecifier) decl_x
				.getDeclSpecifier();
		assertEquals(IASTSimpleDeclSpecifier.t_int, declspec_x.getType());
		IASTDeclarator declor_x = (IASTDeclarator) decl_x.getDeclarators().get(
				0);
		IASTName name_x = declor_x.getName();
		assertEquals("x", name_x.toString()); //$NON-NLS-1$

		// function - void f()
		IASTFunctionDefinition funcdef_f = (IASTFunctionDefinition) declarations
				.get(1);
		IASTSimpleDeclSpecifier declspec_f = (IASTSimpleDeclSpecifier) funcdef_f
				.getDeclSpecifier();
		assertEquals(IASTSimpleDeclSpecifier.t_void, declspec_f.getType());
		IASTFunctionDeclarator declor_f = funcdef_f
				.getDeclarator();
		IASTName name_f = declor_f.getName();
		assertEquals("f", name_f.toString()); //$NON-NLS-1$

		// parameter - int y
		IASTParameterDeclaration decl_y = (IASTParameterDeclaration) declor_f
				.getParameters().get(0);
		IASTSimpleDeclSpecifier declspec_y = (IASTSimpleDeclSpecifier) decl_y
				.getDeclSpecifier();
		assertEquals(IASTSimpleDeclSpecifier.t_int, declspec_y.getType());
		IASTDeclarator declor_y = decl_y.getDeclarator();
		IASTName name_y = declor_y.getName();
		assertEquals("y", name_y.toString()); //$NON-NLS-1$

		// int z
		IASTCompoundStatement body_f = (IASTCompoundStatement) funcdef_f
				.getBody();
		IASTDeclarationStatement declstmt_z = (IASTDeclarationStatement) body_f
				.getStatements().get(0);
		IASTSimpleDeclaration decl_z = (IASTSimpleDeclaration) declstmt_z
				.getDeclaration();
		IASTSimpleDeclSpecifier declspec_z = (IASTSimpleDeclSpecifier) decl_z
				.getDeclSpecifier();
		assertEquals(IASTSimpleDeclSpecifier.t_int, declspec_z.getType());
		IASTDeclarator declor_z = (IASTDeclarator) decl_z.getDeclarators().get(
				0);
		IASTName name_z = declor_z.getName();
		assertEquals("z", name_z.toString()); //$NON-NLS-1$

		// = x + y
		IASTInitializerExpression initializer = (IASTInitializerExpression) declor_z.getInitializer();
		IASTBinaryExpression init_z = (IASTBinaryExpression) initializer.getExpression();
		assertEquals(IASTBinaryExpression.op_plus, init_z.getOperator());
		IASTIdExpression ref_x = (IASTIdExpression) init_z.getOperand1();
		IASTName name_ref_x = ref_x.getName();
		assertEquals("x", name_ref_x.toString()); //$NON-NLS-1$

		IASTIdExpression ref_y = (IASTIdExpression) init_z.getOperand2();
		IASTName name_ref_y = ref_y.getName();
		assertEquals("y", name_ref_y.toString()); //$NON-NLS-1$
		
		//BINDINGS
		// resolve the binding to get the variable object
		IVariable var_x = (IVariable) name_x.resolveBinding();
		assertEquals(globalScope, var_x.getScope());
		IFunction func_f = (IFunction) name_f.resolveBinding();
		assertEquals(globalScope, func_f.getScope());
		IParameter var_y = (IParameter) name_y.resolveBinding();
		assertEquals(func_f.getFunctionScope(), var_y.getScope());

		IVariable var_z = (IVariable) name_z.resolveBinding();
		assertEquals(func_f.getFunctionScope(), var_z.getScope());

		// make sure the variable referenced is the same one we declared above
		assertEquals(var_x, name_ref_x.resolveBinding());
		assertEquals(var_y, name_ref_y.resolveBinding());

	}

    public void testSimpleStruct() throws ParserException {
		StringBuffer buff = new StringBuffer();
		buff.append("typedef struct {\n"); //$NON-NLS-1$
		buff.append("    int x;\n"); //$NON-NLS-1$
		buff.append("} S;\n"); //$NON-NLS-1$

		buff.append("void f() {\n"); //$NON-NLS-1$
		buff.append("    S myS;\n"); //$NON-NLS-1$
		buff.append("    myS.x = 5;"); //$NON-NLS-1$
		buff.append("}"); //$NON-NLS-1$

		IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C );
		IASTSimpleDeclaration decl = (IASTSimpleDeclaration)tu.getDeclarations().get(0);
		IASTCompositeTypeSpecifier type = (IASTCompositeTypeSpecifier)decl.getDeclSpecifier();
		
		// it's a typedef
		assertEquals(IASTDeclSpecifier.sc_typedef, type.getStorageClass());
		// this an anonymous struct
		IASTName name_struct = type.getName();
		assertNull("", name_struct.toString()); //$NON-NLS-1$
		// member - x
		IASTSimpleDeclaration decl_x = (IASTSimpleDeclaration) type
				.getMembers().get(0);
		IASTSimpleDeclSpecifier spec_x = (IASTSimpleDeclSpecifier) decl_x
				.getDeclSpecifier();
		// it's an int
		assertEquals(IASTSimpleDeclSpecifier.t_int, spec_x.getType());
		IASTDeclarator tor_x = (IASTDeclarator) decl_x
				.getDeclarators().get(0);
		IASTName name_x = tor_x.getName();
		assertEquals("x", name_x.toString()); //$NON-NLS-1$

		// declarator S
		IASTDeclarator tor_S = (IASTDeclarator) decl.getDeclarators().get(0);
		IASTName name_S = tor_S.getName();
		assertEquals("S", name_S.toString()); //$NON-NLS-1$

		// function f
		IASTFunctionDefinition def_f = (IASTFunctionDefinition) tu
				.getDeclarations().get(1);
		// f's body
		IASTCompoundStatement body_f = (IASTCompoundStatement) def_f.getBody();
		// the declaration statement for myS
		IASTDeclarationStatement declstmt_myS = (IASTDeclarationStatement)body_f.getStatements().get(0);
		// the declaration for myS
		IASTSimpleDeclaration decl_myS = (IASTSimpleDeclaration)declstmt_myS.getDeclaration();
		// the type specifier for myS
		IASTTypedefNameSpecifier type_spec_myS = (IASTTypedefNameSpecifier)decl_myS.getDeclSpecifier();
		// the type name for myS
		IASTName name_type_myS = type_spec_myS.getName();
		// the declarator for myS
		IASTDeclarator tor_myS = (IASTDeclarator)decl_myS.getDeclarators().get(0);
		// the name for myS
		IASTName name_myS = tor_myS.getName();
		// the assignment expression statement
		IASTExpressionStatement exprstmt = (IASTExpressionStatement)body_f.getStatements().get(1);
		// the assignment expression
		IASTBinaryExpression assexpr = (IASTBinaryExpression)exprstmt.getExpression();
		// the field reference to myS.x
		IASTFieldReference fieldref = (IASTFieldReference)assexpr.getOperand1();
		// the reference to myS
		IASTIdExpression ref_myS = (IASTIdExpression)fieldref.getFieldOwner();
		IASTLiteralExpression lit_5 = (IASTLiteralExpression)assexpr.getOperand2();
		assertEquals("5", lit_5.toString()); //$NON-NLS-1$

		//Logical Bindings In Test
		ICompositeType type_struct = (ICompositeType) name_struct.resolveBinding();
		ITypedef typedef_S = (ITypedef) name_S.resolveBinding();
		// make sure the typedef is hooked up correctly
		assertEquals(type_struct, typedef_S.getType());
		// the typedef S for myS
		ITypedef typedef_myS = (ITypedef)name_type_myS.resolveBinding();
		assertEquals(typedef_S, typedef_myS);
		// get the real type for S which is our anonymous struct
		ICompositeType type_myS = (ICompositeType)typedef_myS.getType();
		assertEquals( type_myS, type_struct );
		// the variable myS
		IVariable var_myS = (IVariable)name_myS.resolveBinding();
		assertEquals(typedef_S, var_myS.getType());
		assertEquals(var_myS, ref_myS.getName().resolveBinding());
		IField field_x = (IField)name_x.resolveBinding();
		assertEquals(field_x, fieldref.getFieldName().resolveBinding());
	}
    
    public void testCExpressions() throws ParserException
    {
        validateSimpleUnaryExpressionC( "++x", IASTUnaryExpression.op_prefixIncr ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "--x", IASTUnaryExpression.op_prefixDecr ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "+x", IASTUnaryExpression.op_plus ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "-x", IASTUnaryExpression.op_minus ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "!x", IASTUnaryExpression.op_not ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "~x", IASTUnaryExpression.op_tilde ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "*x", IASTUnaryExpression.op_star ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "&x", IASTUnaryExpression.op_amper ); //$NON-NLS-1$
        validateSimpleUnaryExpressionC( "sizeof x", IASTUnaryExpression.op_sizeof ); //$NON-NLS-1$
        validateSimpleTypeIdExpressionC( "sizeof( int )", IASTTypeIdExpression.op_sizeof ); //$NON-NLS-1$
        validateSimpleUnaryTypeIdExpression( "(int)x", IASTUnaryTypeIdExpression.op_cast ); //$NON-NLS-1$
        validateSimplePostfixInitializerExpressionC( "(int) { 5 }"); //$NON-NLS-1$
        validateSimplePostfixInitializerExpressionC( "(int) { 5, }"); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x=y", IASTBinaryExpression.op_assign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x*=y", IASTBinaryExpression.op_multiplyAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x/=y", IASTBinaryExpression.op_divideAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x%=y", IASTBinaryExpression.op_moduloAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x+=y", IASTBinaryExpression.op_plusAssign); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x-=y", IASTBinaryExpression.op_minusAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x<<=y", IASTBinaryExpression.op_shiftLeftAssign); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x>>=y", IASTBinaryExpression.op_shiftRightAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x&=y", IASTBinaryExpression.op_binaryAndAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x^=y", IASTBinaryExpression.op_binaryXorAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x|=y", IASTBinaryExpression.op_binaryOrAssign ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x-y", IASTBinaryExpression.op_minus ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x+y", IASTBinaryExpression.op_plus ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x/y", IASTBinaryExpression.op_divide ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x*y", IASTBinaryExpression.op_multiply); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x%y", IASTBinaryExpression.op_modulo ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x<<y", IASTBinaryExpression.op_shiftLeft ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x>>y", IASTBinaryExpression.op_shiftRight ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x<y", IASTBinaryExpression.op_lessThan ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x>y", IASTBinaryExpression.op_greaterThan); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x<=y", IASTBinaryExpression.op_lessEqual ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x>=y", IASTBinaryExpression.op_greaterEqual ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x==y", IASTBinaryExpression.op_equals ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x!=y", IASTBinaryExpression.op_notequals ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x&y", IASTBinaryExpression.op_binaryAnd ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x^y", IASTBinaryExpression.op_binaryXor ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x|y", IASTBinaryExpression.op_binaryOr ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x&&y", IASTBinaryExpression.op_logicalAnd ); //$NON-NLS-1$
        validateSimpleBinaryExpressionC("x||y", IASTBinaryExpression.op_logicalOr ); //$NON-NLS-1$
        validateConditionalExpressionC( "x ? y : x" ); //$NON-NLS-1$
    }
    
    /**
     * @param string
     */
    protected void validateSimplePostfixInitializerExpressionC(String code ) throws ParserException  {
        ICASTTypeIdInitializerExpression e = (ICASTTypeIdInitializerExpression) getExpressionFromStatementInCode(code, ParserLanguage.C );
        assertNotNull( e );
        assertNotNull( e.getTypeId() );
        assertNotNull( e.getInitializer() );
    }


    /**
     * @param string
     * @throws ParserException
     */
    protected void validateSimpleUnaryTypeIdExpression(String code, int op ) throws ParserException {
        IASTUnaryTypeIdExpression e = (IASTUnaryTypeIdExpression) 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$
    }


    /**
     * @param code
     * @param op
     * @throws ParserException
     */
    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() );
    }


    /**
     * @param string
     * @param op_prefixIncr
     * @throws ParserException
     */
    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$
    }


    /**
     * @param code 
     * @throws ParserException
     */
    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() );
    }


    /**
     * @param operand
     * @throws ParserException
     */
    protected void validateSimpleBinaryExpressionC( String code, int operand) throws ParserException {
        IASTBinaryExpression e = (IASTBinaryExpression) getExpressionFromStatementInCode( code, ParserLanguage.C ); //$NON-NLS-1$
        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
    {
        StringBuffer buffer = new StringBuffer( "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().get(0);
        IASTCompoundStatement cs = (IASTCompoundStatement) f.getBody();
        IASTExpressionStatement s = (IASTExpressionStatement) cs.getStatements().get( 1 );
        return s.getExpression();
    }
    
    
    public void testMultipleDeclarators() throws Exception {
		IASTTranslationUnit tu = parse( "int r, s;" , ParserLanguage.C ); //$NON-NLS-1$
		IASTSimpleDeclaration decl = (IASTSimpleDeclaration)tu.getDeclarations().get(0);
		List declarators = decl.getDeclarators();
		assertEquals( 2, declarators.size() );
		
		IASTDeclarator dtor1 = (IASTDeclarator) declarators.get(0);
		IASTDeclarator dtor2 = (IASTDeclarator) declarators.get(1);
		
		IASTName name1 = dtor1.getName();
		IASTName name2 = dtor2.getName();
		
		assertEquals( name1.resolveBinding().getName(), "r" ); //$NON-NLS-1$
		assertEquals( name2.resolveBinding().getName(), "s" ); //$NON-NLS-1$
    }
    
    public void testStructureTagScoping_1() throws Exception{
    	StringBuffer buffer = new StringBuffer();
    	buffer.append( "struct A;             \n" ); //$NON-NLS-1$
    	buffer.append( "void f(){             \n" ); //$NON-NLS-1$
    	buffer.append( "   struct A;          \n" ); //$NON-NLS-1$
    	buffer.append( "   struct A * a;      \n" ); //$NON-NLS-1$
    	buffer.append( "}                     \n" ); //$NON-NLS-1$
    	
    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	//struct A;
    	IASTSimpleDeclaration decl1 = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
    	IASTElaboratedTypeSpecifier compTypeSpec = (IASTElaboratedTypeSpecifier) decl1.getDeclSpecifier();
    	assertEquals( 0, decl1.getDeclarators().size() );
    	IASTName nameA1 = compTypeSpec.getName();
    	
    	//void f() {
    	IASTFunctionDefinition fndef = (IASTFunctionDefinition) tu.getDeclarations().get(1);
    	IASTCompoundStatement compoundStatement = (IASTCompoundStatement) fndef.getBody();
    	assertEquals( 2, compoundStatement.getStatements().size() );
    	
    	//   struct A;
    	IASTDeclarationStatement declStatement = (IASTDeclarationStatement) compoundStatement.getStatements().get( 0 );
    	IASTSimpleDeclaration decl2 = (IASTSimpleDeclaration) declStatement.getDeclaration();
    	compTypeSpec = (IASTElaboratedTypeSpecifier) decl2.getDeclSpecifier();
    	assertEquals( 0, decl2.getDeclarators().size() );
    	IASTName nameA2 = compTypeSpec.getName();
    	
    	//   struct A * a;
    	declStatement = (IASTDeclarationStatement) compoundStatement.getStatements().get(1);
    	IASTSimpleDeclaration decl3 = (IASTSimpleDeclaration) declStatement.getDeclaration();
    	compTypeSpec = (IASTElaboratedTypeSpecifier) decl3.getDeclSpecifier();
    	IASTName nameA3 = compTypeSpec.getName();
    	IASTDeclarator dtor = (IASTDeclarator) decl3.getDeclarators().get(0);
    	IASTName namea = dtor.getName();
    	assertEquals( 1, dtor.getPointerOperators().size() );
    	assertTrue( dtor.getPointerOperators().get(0) instanceof ICASTPointer );

    	//bindings
    	ICompositeType str1 = (ICompositeType) nameA1.resolveBinding();
    	ICompositeType str2 = (ICompositeType) nameA2.resolveBinding();
    	IVariable var = (IVariable) namea.resolveBinding();
    	ICompositeType str3 = (ICompositeType) var.getType();
    	ICompositeType str4 = (ICompositeType) nameA3.resolveBinding();
    	assertNotNull( str1 );
    	assertNotNull( str2 );
    	assertNotSame( str1, str2 );
    	assertSame( str2, str3 );
    	assertSame( str3, str4 );
    }
    
    public void testStructureTagScoping_2() throws Exception{
    	StringBuffer buffer = new StringBuffer();
    	buffer.append( "struct A;             \n" ); //$NON-NLS-1$
    	buffer.append( "void f(){             \n" ); //$NON-NLS-1$
    	buffer.append( "   struct A * a;      \n" ); //$NON-NLS-1$
    	buffer.append( "}                     \r\n" ); //$NON-NLS-1$

    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	//struct A;
    	IASTSimpleDeclaration decl1 = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
    	IASTElaboratedTypeSpecifier compTypeSpec = (IASTElaboratedTypeSpecifier) decl1.getDeclSpecifier();
    	assertEquals( 0, decl1.getDeclarators().size() );
    	IASTName nameA1 = compTypeSpec.getName();
    	
    	//void f() {
    	IASTFunctionDefinition fndef = (IASTFunctionDefinition) tu.getDeclarations().get(1);
    	IASTCompoundStatement compoundStatement = (IASTCompoundStatement) fndef.getBody();
    	assertEquals( 1, compoundStatement.getStatements().size() );
    	  	
    	//   struct A * a;
    	IASTDeclarationStatement declStatement = (IASTDeclarationStatement) compoundStatement.getStatements().get(0);
    	IASTSimpleDeclaration decl2 = (IASTSimpleDeclaration) declStatement.getDeclaration();
    	compTypeSpec = (IASTElaboratedTypeSpecifier) decl2.getDeclSpecifier();
    	IASTName nameA2 = compTypeSpec.getName();  	
    	IASTDeclarator dtor = (IASTDeclarator) decl2.getDeclarators().get(0);
    	IASTName namea = dtor.getName();
    	assertEquals( 1, dtor.getPointerOperators().size() );
    	assertTrue( dtor.getPointerOperators().get(0) instanceof ICASTPointer );

    	//bindings
    	ICompositeType str1 = (ICompositeType) nameA1.resolveBinding();
    	ICompositeType str2 = (ICompositeType) nameA2.resolveBinding();
    	IVariable var = (IVariable) namea.resolveBinding();
    	ICompositeType str3 = (ICompositeType) var.getType();
    	assertNotNull( str1 );
    	assertSame( str1, str2 );
    	assertSame( str2, str3 );
    }
    
    public void testStructureDef() throws Exception{
    	StringBuffer buffer = new StringBuffer();
    	buffer.append( "struct A;                \r\n"); //$NON-NLS-1$
    	buffer.append( "struct A * a;            \n"); //$NON-NLS-1$
    	buffer.append( "struct A { int i; };     \n"); //$NON-NLS-1$
    	buffer.append( "void f() {               \n"); //$NON-NLS-1$
    	buffer.append( "   a->i;                 \n"); //$NON-NLS-1$
    	buffer.append( "}                        \n"); //$NON-NLS-1$
    	
    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	//struct A;
    	IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
    	IASTElaboratedTypeSpecifier elabTypeSpec = (IASTElaboratedTypeSpecifier) decl.getDeclSpecifier();
    	assertEquals( 0, decl.getDeclarators().size() );
    	IASTName name_A1 = elabTypeSpec.getName();
    	
    	//struct A * a;
    	decl = (IASTSimpleDeclaration) tu.getDeclarations().get(1);
    	elabTypeSpec = (IASTElaboratedTypeSpecifier) decl.getDeclSpecifier();
    	IASTName name_A2 = elabTypeSpec.getName();  	
    	IASTDeclarator dtor = (IASTDeclarator) decl.getDeclarators().get(0);
    	IASTName name_a = dtor.getName();
    	assertEquals( 1, dtor.getPointerOperators().size() );
    	assertTrue( dtor.getPointerOperators().get(0) instanceof ICASTPointer );
    	
    	//struct A {
    	decl = (IASTSimpleDeclaration) tu.getDeclarations().get(2);
    	ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) decl.getDeclSpecifier();
    	IASTName name_Adef = compTypeSpec.getName();
    	
    	//   int i;
    	decl = (IASTSimpleDeclaration) compTypeSpec.getMembers().get(0);
    	dtor = (IASTDeclarator) decl.getDeclarators().get(0);
    	IASTName name_i = dtor.getName();
    
    	//void f() {
    	IASTFunctionDefinition fndef = (IASTFunctionDefinition) tu.getDeclarations().get(3);
    	IASTCompoundStatement compoundStatement = (IASTCompoundStatement) fndef.getBody();
    	assertEquals( 1, compoundStatement.getStatements().size() );
    	
    	//   a->i;
		IASTExpressionStatement exprstmt = (IASTExpressionStatement)compoundStatement.getStatements().get(0);
		IASTFieldReference fieldref = (IASTFieldReference)exprstmt.getExpression();
		IASTIdExpression id_a = (IASTIdExpression) fieldref.getFieldOwner();
		IASTName name_aref = id_a.getName();
		IASTName name_iref = fieldref.getFieldName();

		//bindings
		IVariable var_a1 = (IVariable) name_aref.resolveBinding();
		IVariable var_i1 = (IVariable) name_iref.resolveBinding();
		ICompositeType structA_1 = (ICompositeType) var_a1.getType();
		ICompositeType structA_2 = (ICompositeType) name_A1.resolveBinding();
		ICompositeType structA_3 = (ICompositeType) name_A2.resolveBinding();
		ICompositeType structA_4 = (ICompositeType) name_Adef.resolveBinding();
		
		IVariable var_a2 = (IVariable) name_a.resolveBinding();
		IVariable var_i2 = (IVariable) name_i.resolveBinding();
		
		assertSame( var_a1, var_a2 );
		assertSame( var_i1, var_i2 );
		assertSame( structA_1, structA_2 );
		assertSame( structA_2, structA_3 );
		assertSame( structA_3, structA_4 );
    }
    
    public void testFunctionParameters() throws Exception {
    	StringBuffer buffer  = new StringBuffer();
    	buffer.append( "void f( int a );        \n"); //$NON-NLS-1$
    	buffer.append( "void f( int b ){        \n"); //$NON-NLS-1$
    	buffer.append( "   b;                   \n"); //$NON-NLS-1$
    	buffer.append( "}                       \n"); //$NON-NLS-1$
    	
    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	//void f(
    	IASTSimpleDeclaration f_decl = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
    	IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) f_decl.getDeclarators().get(0);
    	IASTName f_name1 = dtor.getName();
    	//        int a );
    	IASTParameterDeclaration param = (IASTParameterDeclaration) dtor.getParameters().get(0);
    	IASTDeclarator paramDtor = param.getDeclarator();
    	IASTName name_param1 = paramDtor.getName();
    	
    	//void f( 
    	IASTFunctionDefinition f_defn = (IASTFunctionDefinition) tu.getDeclarations().get(1);
    	dtor = f_defn.getDeclarator();
    	IASTName f_name2 = dtor.getName();
    	//        int b );
    	param = (IASTParameterDeclaration) dtor.getParameters().get(0);
    	paramDtor = param.getDeclarator();
    	IASTName name_param2 = paramDtor.getName();
    	
    	//   b;
    	IASTCompoundStatement compound = (IASTCompoundStatement) f_defn.getBody();
    	IASTExpressionStatement expStatement = (IASTExpressionStatement) compound.getStatements().get(0);
    	IASTIdExpression idexp = (IASTIdExpression) expStatement.getExpression();
    	IASTName name_param3 = idexp.getName();
    	
    	//bindings
    	IParameter param_1 = (IParameter) name_param3.resolveBinding();
    	IParameter param_2 = (IParameter) name_param2.resolveBinding();
    	IParameter param_3 = (IParameter) name_param1.resolveBinding();
    	IFunction  f_1 = (IFunction) f_name1.resolveBinding();
    	IFunction  f_2 = (IFunction) f_name2.resolveBinding();
    	
    	assertNotNull( param_1 );
    	assertNotNull( f_1 );
    	assertSame( param_1, param_2 );
    	assertSame( param_2, param_3 );
    	assertSame( f_1, f_2 );
    	
    	CVisitor.clearBindings( tu );
    	param_1 = (IParameter) name_param1.resolveBinding();
    	param_2 = (IParameter) name_param3.resolveBinding();
    	param_3 = (IParameter) name_param2.resolveBinding();
    	f_1 = (IFunction) f_name2.resolveBinding();
    	f_2 = (IFunction) f_name1.resolveBinding();
    	assertNotNull( param_1 );
    	assertNotNull( f_1 );
    	assertSame( param_1, param_2 );
    	assertSame( param_2, param_3 );
    	assertSame( f_1, f_2 );
    }
    
    public void testSimpleFunction() throws Exception {
    	StringBuffer buffer = new StringBuffer( "void f( int a, int b ) { }  \n" ); //$NON-NLS-1$
    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	IASTFunctionDefinition fDef = (IASTFunctionDefinition) tu.getDeclarations().get(0);
    	IASTFunctionDeclarator fDtor = fDef.getDeclarator();
    	IASTName fName = fDtor.getName();
    	
    	IASTParameterDeclaration a = (IASTParameterDeclaration) fDtor.getParameters().get( 0 );
    	IASTName name_a = a.getDeclarator().getName();
    	
    	IASTParameterDeclaration b = (IASTParameterDeclaration) fDtor.getParameters().get( 1 );
    	IASTName name_b = b.getDeclarator().getName();
    	
    	IFunction function = (IFunction) fName.resolveBinding();
    	IParameter param_a = (IParameter) name_a.resolveBinding();
    	IParameter param_b = (IParameter) name_b.resolveBinding();
    	
    	assertEquals( "f", function.getName() ); //$NON-NLS-1$
    	assertEquals( "a", param_a.getName() ); //$NON-NLS-1$
    	assertEquals( "b", param_b.getName() ); //$NON-NLS-1$
    	
    	List params = function.getParameters();
    	assertEquals( 2, params.size() );
    	assertSame( params.get(0), param_a );
    	assertSame( params.get(1), param_b );
    }
    
    public void testSimpleFunctionCall() throws Exception {
    	StringBuffer buffer = new StringBuffer();
    	buffer.append( "void f();              \n" ); //$NON-NLS-1$
    	buffer.append( "void g() {             \n" ); //$NON-NLS-1$
    	buffer.append( "   f();                \n" ); //$NON-NLS-1$
    	buffer.append( "}                      \n" ); //$NON-NLS-1$
    	buffer.append( "void f(){ }            \n" ); //$NON-NLS-1$
    	
    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	//void f();
    	IASTSimpleDeclaration fdecl = (IASTSimpleDeclaration) tu.getDeclarations().get(0);
    	IASTFunctionDeclarator fdtor = (IASTFunctionDeclarator) fdecl.getDeclarators().get(0);
    	IASTName name_f = fdtor.getName();
    	
    	//void g() {
    	IASTFunctionDefinition gdef = (IASTFunctionDefinition) tu.getDeclarations().get(1);
    	
    	//   f();
    	IASTCompoundStatement compound = (IASTCompoundStatement) gdef.getBody();
    	IASTExpressionStatement expStatement = (IASTExpressionStatement) compound.getStatements().get(0);
    	IASTFunctionCallExpression fcall = (IASTFunctionCallExpression) expStatement.getExpression();
    	IASTIdExpression fcall_id = (IASTIdExpression) fcall.getFunctionNameExpression();
    	IASTName name_fcall = fcall_id.getName();
    	assertNull( fcall.getParameterExpression() );
    	
    	//void f() {}
    	IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations().get(2);
    	fdtor = fdef.getDeclarator();
    	IASTName name_fdef = fdtor.getName();
    	
    	//bindings
    	IFunction function_1 = (IFunction) name_fcall.resolveBinding();
    	IFunction function_2 = (IFunction) name_f.resolveBinding();
    	IFunction function_3 =  (IFunction) name_fdef.resolveBinding();
    	
    	assertNotNull( function_1 );
    	assertSame( function_1, function_2 );
    	assertSame( function_2, function_3 );
    }
    
    public void testForLoop() throws Exception {
    	StringBuffer buffer = new StringBuffer();
    	buffer.append( "void f() {                         \n"); //$NON-NLS-1$
    	buffer.append( "   for( int i = 0; i < 5; i++ ) {  \n"); //$NON-NLS-1$         
    	buffer.append( "      i;                           \n"); //$NON-NLS-1$
    	buffer.append( "   }                               \n"); //$NON-NLS-1$
    	buffer.append( "}                                  \n"); //$NON-NLS-1$
    	
    	IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
    	
    	//void f() {
    	IASTFunctionDefinition fdef = (IASTFunctionDefinition) tu.getDeclarations().get(0);
    	IASTCompoundStatement compound = (IASTCompoundStatement) fdef.getBody();
    	
    	//   for( 
    	IASTForStatement for_stmt = (IASTForStatement) compound.getStatements().get(0);
    	//        int i = 0;
    	assertNull( for_stmt.getInitExpression() );
    	IASTSimpleDeclaration initDecl = (IASTSimpleDeclaration) for_stmt.getInitDeclaration();
    	IASTDeclarator dtor = (IASTDeclarator) initDecl.getDeclarators().get(0);
    	IASTName name_i = dtor.getName();
    	//                   i < 5;
    	IASTBinaryExpression exp = (IASTBinaryExpression) for_stmt.getCondition();
    	IASTIdExpression id_i = (IASTIdExpression) exp.getOperand1();
    	IASTName name_i2 = id_i.getName();
    	IASTLiteralExpression lit_5 = (IASTLiteralExpression) exp.getOperand2();
    	assertEquals( IASTLiteralExpression.lk_integer_constant, lit_5.getKind() );
    	//                           i++ ) {
    	IASTUnaryExpression un = (IASTUnaryExpression) for_stmt.getIterationExpression();
    	IASTIdExpression id_i2 = (IASTIdExpression) un.getOperand();
    	IASTName name_i3 = id_i2.getName();
    	assertEquals( IASTUnaryExpression.op_postFixIncr, un.getOperator() );
    	
    	//      i;
    	compound = (IASTCompoundStatement) for_stmt.getBody();
    	IASTExpressionStatement exprSt = (IASTExpressionStatement) compound.getStatements().get(0);
    	IASTIdExpression id_i3 = (IASTIdExpression) exprSt.getExpression();
    	IASTName name_i4 = id_i3.getName();
    	
    	//bindings
    	IVariable var_1 = (IVariable) name_i4.resolveBinding();
    	IVariable var_2 = (IVariable) name_i.resolveBinding();
    	IVariable var_3 = (IVariable) name_i2.resolveBinding();
    	IVariable var_4 = (IVariable) name_i3.resolveBinding();
    	
    	assertSame( var_1, var_2 );
    	assertSame( var_2, var_3 );
    	assertSame( var_3, var_4 );
    }
}

Back to the top