Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 268e68cacffd24ec5ef6f476163b5c99e63cc93e (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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
/*******************************************************************************
 * Copyright (c) 2002, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Rational Software - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.parser.tests;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Stack;

import junit.framework.TestCase;

import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTClassReference;
import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTCodeScope;
import org.eclipse.cdt.core.parser.ast.IASTCompilationUnit;
import org.eclipse.cdt.core.parser.ast.IASTDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerationReference;
import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerator;
import org.eclipse.cdt.core.parser.ast.IASTEnumeratorReference;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.core.parser.ast.IASTField;
import org.eclipse.cdt.core.parser.ast.IASTFieldReference;
import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTFunctionReference;
import org.eclipse.cdt.core.parser.ast.IASTInclusion;
import org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification;
import org.eclipse.cdt.core.parser.ast.IASTMacro;
import org.eclipse.cdt.core.parser.ast.IASTMethod;
import org.eclipse.cdt.core.parser.ast.IASTMethodReference;
import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
import org.eclipse.cdt.core.parser.ast.IASTNamespaceReference;
import org.eclipse.cdt.core.parser.ast.IASTNode;
import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement;
import org.eclipse.cdt.core.parser.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTParameterReference;
import org.eclipse.cdt.core.parser.ast.IASTQualifiedNameElement;
import org.eclipse.cdt.core.parser.ast.IASTReference;
import org.eclipse.cdt.core.parser.ast.IASTScope;
import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTTemplateInstantiation;
import org.eclipse.cdt.core.parser.ast.IASTTemplateParameter;
import org.eclipse.cdt.core.parser.ast.IASTTemplateParameterReference;
import org.eclipse.cdt.core.parser.ast.IASTTemplateSpecialization;
import org.eclipse.cdt.core.parser.ast.IASTTypeSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTTypedefReference;
import org.eclipse.cdt.core.parser.ast.IASTUsingDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTUsingDirective;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.core.parser.ast.IASTVariableReference;
import org.eclipse.cdt.internal.core.parser.Parser;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTClassReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTEnumerationReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTEnumeratorReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTFieldReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTFunctionReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTMethodReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTNamespaceReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTParameterReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTTemplateParameterReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTTypedefReference;
import org.eclipse.cdt.internal.core.parser.ast.complete.ASTVariableReference;

/**
 * @author jcamelon
 *
 */
public class CompleteParseBaseTest extends TestCase
{
    /**
     * 
     */
    public CompleteParseBaseTest()
    {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @param name
     */
    public CompleteParseBaseTest(String name)
    {
        super(name);
        // TODO Auto-generated constructor stub
    }
    public static class Scope implements IASTScope
    {
    	private List decls = new ArrayList(); 
    	private final IASTScope scope; 
    	public Scope( IASTScope scope )
    	{
    		this.scope = scope;
    	}
    	
    	public void addDeclaration( IASTDeclaration  d )
    	{
    		decls.add(d);
    	}
    	
    	public Iterator getDeclarations()
    	{
    		return decls.iterator();
    	}
    
        /**
         * @return
         */
        public IASTScope getScope()
        {
         
            return scope;
        }

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ast.IASTNode#lookup(java.lang.String, org.eclipse.cdt.core.parser.ast.IASTNode.LookupKind, org.eclipse.cdt.core.parser.ast.IASTNode)
		 */
		public ILookupResult lookup(String prefix, LookupKind[] kind, IASTNode context, IASTExpression functionParameters) {
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ast.IASTNode#getFileIndex()
		 */
		public int getFileIndex() {
			// TODO Auto-generated method stub
			return 0;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ast.IASTNode#setFileIndex(int)
		 */
		public void setFileIndex(int index) {
			// TODO Auto-generated method stub
			
		}

    }
    
    public static class CodeScope extends Scope implements IASTCodeScope
    {
		private List nestedScopes = new ArrayList(); 
        /**
         * @param scope
         */
        public CodeScope(IASTCodeScope scope)
        {
            super(scope);
        }

        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ast.IASTCodeScope#getOwnerCodeScope()
         */
        public IASTCodeScope getOwnerCodeScope()
        {
            return ((IASTCodeScope)getScope()).getOwnerCodeScope();
        }

        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate#acceptElement(org.eclipse.cdt.core.parser.ISourceElementRequestor)
         */
        public void acceptElement(ISourceElementRequestor requestor)
        {
        }

        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate#enterScope(org.eclipse.cdt.core.parser.ISourceElementRequestor)
         */
        public void enterScope(ISourceElementRequestor requestor)
        {
        }

        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate#exitScope(org.eclipse.cdt.core.parser.ISourceElementRequestor)
         */
        public void exitScope(ISourceElementRequestor requestor)
        {           
        }
    	
    	public void addNewScope( IASTCodeScope s )
    	{
    		nestedScopes.add( s );
    	}
    	
		public Iterator getCodeBlocks()
		{
			return nestedScopes.iterator();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ast.IASTCodeScope#getContainingFunction()
		 */
		public IASTFunction getContainingFunction() {
			// TODO Auto-generated method stub
			return null;
		}
    }
    
    public static class FullParseCallback implements ISourceElementRequestor 
    {
    	private List references = new ArrayList(); 
    	private List forewardDecls = new ArrayList();
        private Stack inclusions = new Stack();
        private Scope compilationUnit;
        
        public FullParseCallback()
        {
//        	System.out.println( "NEW");
//        	System.out.println();
        }
        
        public void finalize()
        {
//			System.out.println( );
        }
        
        public IASTScope getCompilationUnit()
        {
        	return compilationUnit;
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptVariable(org.eclipse.cdt.core.parser.ast.IASTVariable)
         */
        public void acceptVariable(IASTVariable variable)
        {
        	getCurrentScope().addDeclaration( variable );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptFunctionDeclaration(org.eclipse.cdt.core.parser.ast.IASTFunction)
         */
        public void acceptFunctionDeclaration(IASTFunction function)
        {
            getCurrentScope().addDeclaration(function);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptUsingDirective(org.eclipse.cdt.core.parser.ast.IASTUsingDirective)
         */
        public void acceptUsingDirective(IASTUsingDirective usageDirective)
        {
    		getCurrentScope().addDeclaration(usageDirective);
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptUsingDeclaration(org.eclipse.cdt.core.parser.ast.IASTUsingDeclaration)
         */
        public void acceptUsingDeclaration(IASTUsingDeclaration usageDeclaration)
        {
    		getCurrentScope().addDeclaration(usageDeclaration);
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptASMDefinition(org.eclipse.cdt.core.parser.ast.IASTASMDefinition)
         */
        public void acceptASMDefinition(IASTASMDefinition asmDefinition)
        {
    		getCurrentScope().addDeclaration(asmDefinition);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptTypedefDeclaration(org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration)
         */
        public void acceptTypedefDeclaration(IASTTypedefDeclaration typedef)
        {
            getCurrentScope().addDeclaration(typedef);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptEnumerationSpecifier(org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier)
         */
        public void acceptEnumerationSpecifier(IASTEnumerationSpecifier enumeration)
        {           
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptAbstractTypeSpecDeclaration(org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration)
         */
        public void acceptAbstractTypeSpecDeclaration(IASTAbstractTypeSpecifierDeclaration abstractDeclaration)
        {
            getCurrentScope().addDeclaration( abstractDeclaration );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterFunctionBody(org.eclipse.cdt.core.parser.ast.IASTFunction)
         */
        public void enterFunctionBody(IASTFunction function)
        {
            pushCodeScope( function );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitFunctionBody(org.eclipse.cdt.core.parser.ast.IASTFunction)
         */
        public void exitFunctionBody(IASTFunction function)
        {
            popScope();
    		getCurrentScope().addDeclaration(function);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterCompilationUnit(org.eclipse.cdt.core.parser.ast.IASTCompilationUnit)
         */
        public void enterCompilationUnit(IASTCompilationUnit cu)
        {
            pushScope( cu );
            this.compilationUnit = getCurrentScope();
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterInclusion(org.eclipse.cdt.core.parser.ast.IASTInclusion)
         */
        public void enterInclusion(IASTInclusion inclusion)
        {
            pushInclusion( inclusion );
        }
    
        /**
         * @param inclusion
         */
        private void pushInclusion(IASTInclusion inclusion)
        {
            inclusions.push( inclusion );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterNamespaceDefinition(org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition)
         */
        public void enterNamespaceDefinition(IASTNamespaceDefinition namespaceDefinition)
        {
            pushScope( namespaceDefinition );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#entesrClassSpecifier(org.eclipse.cdt.core.parser.ast.IASTClassSpecifier)
         */
        public void enterClassSpecifier(IASTClassSpecifier classSpecification)
        {
            pushScope( classSpecification );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterLinkageSpecification(org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification)
         */
        public void enterLinkageSpecification(IASTLinkageSpecification linkageSpec)
        {
        	pushScope( linkageSpec );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterTemplateDeclaration(org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration)
         */
        public void enterTemplateDeclaration(IASTTemplateDeclaration declaration)
        {
        	pushScope( declaration );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterTemplateSpecialization(org.eclipse.cdt.core.parser.ast.IASTTemplateSpecialization)
         */
        public void enterTemplateSpecialization(IASTTemplateSpecialization specialization)
        {
            // TODO Auto-generated method stub
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterTemplateInstantiation(org.eclipse.cdt.core.parser.ast.IASTTemplateInstantiation)
         */
        public void enterTemplateInstantiation(IASTTemplateInstantiation instantiation)
        {
            pushScope( instantiation );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptMethodDeclaration(org.eclipse.cdt.core.parser.ast.IASTMethod)
         */
        public void acceptMethodDeclaration(IASTMethod method)
        {
            getCurrentScope().addDeclaration( method );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterMethodBody(org.eclipse.cdt.core.parser.ast.IASTMethod)
         */
        public void enterMethodBody(IASTMethod method)
        {
            pushCodeScope(method);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitMethodBody(org.eclipse.cdt.core.parser.ast.IASTMethod)
         */
        public void exitMethodBody(IASTMethod method)
        {
            popScope();
    		getCurrentScope().addDeclaration(method);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptField(org.eclipse.cdt.core.parser.ast.IASTField)
         */
        public void acceptField(IASTField field)
        {
            getCurrentScope().addDeclaration(field);
            
        }
    
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitTemplateDeclaration(org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration)
         */
        public void exitTemplateDeclaration(IASTTemplateDeclaration declaration)
        {
            popScope();
            getCurrentScope().addDeclaration( declaration );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitTemplateSpecialization(org.eclipse.cdt.core.parser.ast.IASTTemplateSpecialization)
         */
        public void exitTemplateSpecialization(IASTTemplateSpecialization specialization)
        {
            // TODO Auto-generated method stub
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitTemplateExplicitInstantiation(org.eclipse.cdt.core.parser.ast.IASTTemplateInstantiation)
         */
        public void exitTemplateExplicitInstantiation(IASTTemplateInstantiation instantiation)
        {
        	popScope();
        	getCurrentScope().addDeclaration( instantiation );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitLinkageSpecification(org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification)
         */
        public void exitLinkageSpecification(IASTLinkageSpecification linkageSpec)
        {
            popScope();
            getCurrentScope().addDeclaration(linkageSpec);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitClassSpecifier(org.eclipse.cdt.core.parser.ast.IASTClassSpecifier)
         */
        public void exitClassSpecifier(IASTClassSpecifier classSpecification)
        {
            popScope();
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitNamespaceDefinition(org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition)
         */
        public void exitNamespaceDefinition(IASTNamespaceDefinition namespaceDefinition)
        {
            popScope();
            getCurrentScope().addDeclaration(namespaceDefinition);
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitInclusion(org.eclipse.cdt.core.parser.ast.IASTInclusion)
         */
        public void exitInclusion(IASTInclusion inclusion)
        {
            popInclusion(); 
        }
    
        /**
         * 
         */
        private void popInclusion()
        {
            inclusions.pop();
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitCompilationUnit(org.eclipse.cdt.core.parser.ast.IASTCompilationUnit)
         */
        public void exitCompilationUnit(IASTCompilationUnit cu )
        {
        	popScope();
        }
    
        
        
        private Stack scopes = new Stack();
        protected Scope getCurrentScope()
        {
        	return (Scope)scopes.peek();
        }
        
        protected CodeScope getCurrentCodeScope()
        {
        	if( scopes.peek() instanceof CodeScope )
        		return (CodeScope)scopes.peek();
        	return null;
        }
        
        protected Scope popScope()
        {
        	Scope s = (Scope)scopes.pop();
        	h.put( s.getScope(), s );
        	--depth;
        	return s; 
        }
        
        protected void pushScope( IASTScope scope )
        {
        	scopes.push( new Scope( scope ));
        	++depth;
        }
        
        Hashtable h = new Hashtable();
        
        public Scope lookup( IASTScope s)
        {
        	return (Scope)h.get(s);
        }
        
		public CodeScope lookup( IASTCodeScope s )
		{
			return (CodeScope)h.get(s);
		}
        
    
    	List problems = new ArrayList();
		private int depth = 0;
    	
    	public Iterator getProblems() { 
    		return problems.iterator(); 
    	}
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptProblem(org.eclipse.cdt.core.parser.IProblem)
         */
        public boolean acceptProblem(IProblem problem)
        {
            problems.add( problem );
            return true;
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptMacro(org.eclipse.cdt.core.parser.ast.IASTMacro)
         */
        public void acceptMacro(IASTMacro macro)
        {
            // TODO Auto-generated method stub
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptClassReference(org.eclipse.cdt.core.parser.ast.IASTClassReference)
         */
        public void acceptClassReference(IASTClassReference reference)
        {
            processReference( reference );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptTypedefReference(org.eclipse.cdt.core.parser.ast.IASTTypedefReference)
         */
        public void acceptTypedefReference(IASTTypedefReference reference)
        {
			processReference( reference );
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptNamespaceReference(org.eclipse.cdt.core.parser.ast.IASTNamespaceReference)
         */
        public void acceptNamespaceReference(IASTNamespaceReference reference)
        {
			processReference( reference );
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptEnumerationReference(org.eclipse.cdt.core.parser.ast.IASTEnumerationReference)
         */
        public void acceptEnumerationReference(IASTEnumerationReference reference)
        {
			processReference( reference );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptVariableReference(org.eclipse.cdt.core.parser.ast.IASTVariableReference)
         */
        public void acceptVariableReference(IASTVariableReference reference)
        {
			processReference( reference );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptFunctionReference(org.eclipse.cdt.core.parser.ast.IASTFunctionReference)
         */
        public void acceptFunctionReference(IASTFunctionReference reference)
        {
            processReference(reference);
        }
        
        protected void processReference(IASTReference reference)
        {
        	ISourceElementCallbackDelegate referencedElement = reference.getReferencedElement();
        	IASTReference r = null;
    		if (referencedElement instanceof IASTTypedefDeclaration)
    			r = new ASTTypedefReference(reference.getOffset(),
    					(IASTTypedefDeclaration) referencedElement);
    		if (referencedElement instanceof IASTEnumerationSpecifier)
    			r = new ASTEnumerationReference(reference.getOffset(),
    					(IASTEnumerationSpecifier) referencedElement);
    		if (referencedElement instanceof IASTTemplateParameter)
    			r = new ASTTemplateParameterReference(reference.getOffset(),
    					(IASTTemplateParameter) referencedElement);
    		if (referencedElement instanceof IASTParameterDeclaration)
    			r = new ASTParameterReference(reference.getOffset(),
    					(IASTParameterDeclaration) referencedElement);
    		if (referencedElement instanceof IASTTypeSpecifier)
    			r = new ASTClassReference(reference.getOffset(),
    					(IASTTypeSpecifier) referencedElement);
    		if (referencedElement instanceof IASTNamespaceDefinition)
    			r = new ASTNamespaceReference(reference.getOffset(),
    					(IASTNamespaceDefinition) referencedElement);
    		if (referencedElement instanceof IASTFunction)
    			r = new ASTFunctionReference(reference.getOffset(),
    					(IASTFunction) referencedElement);
    		if (referencedElement instanceof IASTMethod)
    			r = new ASTMethodReference(reference.getOffset(), (IASTMethod) referencedElement);
    		if (referencedElement instanceof IASTField)
    			r = new ASTFieldReference(reference.getOffset(), (IASTField) referencedElement);
    		if (referencedElement instanceof IASTVariable)
    			r = new ASTVariableReference(reference.getOffset(),
    					(IASTVariable) referencedElement);
    		if (referencedElement instanceof IASTEnumerator)
    			r = new ASTEnumeratorReference(reference.getOffset(),
    					(IASTEnumerator) referencedElement);
    		if( r != null )
    			references.add( r );
//            System.out.println( "Callback received Reference to " + reference.getName() + " @ offset " + reference.getOffset() );
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptFieldReference(org.eclipse.cdt.core.parser.ast.IASTFieldReference)
         */
        public void acceptFieldReference(IASTFieldReference reference)
        {
			processReference( reference );
            
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptMethodReference(org.eclipse.cdt.core.parser.ast.IASTMethodReference)
         */
        public void acceptMethodReference(IASTMethodReference reference)
        {
			processReference( reference );
            
        }
        
        public List getReferences()
        {
        	return references;
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptElaboratedForewardDeclaration(org.eclipse.cdt.core.parser.ast.IASTElaboratedTypeSpecifier)
         */
        public void acceptElaboratedForewardDeclaration(IASTElaboratedTypeSpecifier elaboratedType)
        {
            forewardDecls.add( elaboratedType );
        }
        /**
         * @return
         */
        public List getForewardDecls()
        {
            return forewardDecls;
        }

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterCodeBlock(org.eclipse.cdt.core.parser.ast.IASTScope)
		 */
		public void enterCodeBlock(IASTCodeScope scope) {
			pushCodeScope( scope );
		}

		/**
         * @param scope
         */
        protected void pushCodeScope(IASTCodeScope scope)
        {
			scopes.push( new CodeScope( scope ) );
			++depth;
        }

        /* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#exitCodeBlock(org.eclipse.cdt.core.parser.ast.IASTScope)
		 */
		public void exitCodeBlock(IASTCodeScope scope) {
			popScope();
			if( getCurrentCodeScope() != null )
				getCurrentCodeScope().addNewScope(scope);
		}

        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptEnumeratorReference(org.eclipse.cdt.core.parser.ast.IASTEnumerationReference)
         */
        public void acceptEnumeratorReference(IASTEnumeratorReference reference)
        {
			processReference( reference );
        }

        /* (non-Javadoc)
         * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptParameterReference(org.eclipse.cdt.internal.core.parser.ast.complete.ASTParameterReference)
         */
        public void acceptParameterReference(IASTParameterReference reference)
        {
			processReference( reference );
        }

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
		 */
		public CodeReader createReader(String finalPath, Iterator workingCopies) {
			return ParserUtil.createReader(finalPath,workingCopies);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptTemplateParameterReference(org.eclipse.cdt.core.parser.ast.IASTTemplateParameterReference)
		 */
		public void acceptTemplateParameterReference(IASTTemplateParameterReference reference) {
			processReference( reference );
			
		}
    
		/* (non-Javadoc)
		 * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#acceptFriendDeclaration(org.eclipse.cdt.core.parser.ast.IASTDeclaration)
		 */
		public void acceptFriendDeclaration(IASTDeclaration declaration) {
			getCurrentScope().addDeclaration( declaration );
		}

		/**
		 * @return
		 */
		public boolean isBalanced() {
			return depth == 0;
		}
    }
    
    protected Iterator getNestedScopes( IASTCodeScope scope )
    {
    	CodeScope s = callback.lookup( scope );
		if( s != null )
			return s.getCodeBlocks();
		return null;
 
    }
    protected Iterator getDeclarations(IASTScope scope)
    {
    	Scope s = callback.lookup( scope ); 
    	if( s != null )
    		return s.getDeclarations();
    	return null;
    }
    protected FullParseCallback callback;
    
    protected IASTScope parse( String code ) throws ParserException, ParserFactoryError
    {
    	return parse( code, true, ParserLanguage.CPP );
    }
    
    protected IASTScope parse( String code, boolean throwOnError ) throws ParserException, ParserFactoryError
    {
    	return parse( code, throwOnError, ParserLanguage.CPP );
    }
    
    protected IASTScope parse(String code, boolean throwOnError, ParserLanguage language) throws ParserException, ParserFactoryError
    {
    	callback = new FullParseCallback(); 
    	IParser parser = ParserFactory.createParser( 
    		ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), //$NON-NLS-1$
    			ParserMode.COMPLETE_PARSE, language, callback, new NullLogService(), null ), callback, ParserMode.COMPLETE_PARSE, language, null 	
    		);
    	boolean parseResult = parser.parse();
    	// throw exception if there are generated IProblems
		if( (! parseResult || callback.getProblems().hasNext() ) && throwOnError ) throw new ParserException( "FAILURE"); //$NON-NLS-1$
		if( parseResult  )
		{
			assertTrue( ((Parser)parser).validateCaches());
			assertTrue( callback.isBalanced() );
		}
        return callback.getCompilationUnit();
    }
        
    protected void assertReferences( 
    	ISourceElementCallbackDelegate element, 
    	int expectedDistinctReferenceCount, 
    	boolean allowDuplicates, boolean allowNameMatching )
    {
    	Set matches = new HashSet(); 
    	Iterator allReferences = callback.getReferences().iterator();
    	while( allReferences.hasNext() )
    	{
    		IASTReference r = (IASTReference)allReferences.next();
    		if( r.getReferencedElement() == element )
    		{
    			assertEquals( r.getName(), ((IASTOffsetableNamedElement)element).getName() );
    			if( ! matches.add( r ) && ! allowDuplicates )
    				fail( "Duplicate reference found for ISourceElementCallbackDelegate: " + element + " @ offset " + r.getOffset() ); //$NON-NLS-1$ //$NON-NLS-2$
    		}
    		else
    		{
    			if( r.getReferencedElement() instanceof IASTQualifiedNameElement && 
    				element instanceof IASTQualifiedNameElement &&
					allowNameMatching )
    			{
					if( qualifiedNamesEquals( 
						((IASTQualifiedNameElement)r.getReferencedElement()).getFullyQualifiedName(),
						((IASTQualifiedNameElement)element).getFullyQualifiedName() 
											) 
					  )
					  { 
					  
						if( ! matches.add( r ) && ! allowDuplicates )
							fail( "Duplicate reference found for ISourceElementCallbackDelegate: " + element + " @ offset " + r.getOffset() ); //$NON-NLS-1$ //$NON-NLS-2$
					  }
					
    			}
    		}
    	}
    	
    	assertEquals( expectedDistinctReferenceCount, matches.size() );
    }
    
    protected static class Task
    {
    	private final boolean allowNameMatching;
    	private final boolean unique;
        private final int count;
        private final ISourceElementCallbackDelegate element;
    	

        public Task( ISourceElementCallbackDelegate element, int referenceCount, boolean distinct, boolean matchNames ){
        	this.element = element;
    		this.count = referenceCount;
    		this.unique = distinct;
    		this.allowNameMatching = matchNames;
        }
        
        public Task( ISourceElementCallbackDelegate element, int referenceCount, boolean distinct )
    	{
        	this( element, referenceCount, distinct, true );
    	}
    	
		public Task( ISourceElementCallbackDelegate element, int referenceCount )
		{
			this( element, referenceCount, true );
		}
		
		public Task( ISourceElementCallbackDelegate element )
		{
			this( element, 1, false );
		}
		
        /**
         * @return
         */
        public int getCount()
        {
            return count;
        }

        /**
         * @return
         */
        public ISourceElementCallbackDelegate getElement()
        {
            return element;
        }

        /**
         * @return
         */
        public boolean isUnique()
        {
            return unique;
        }
        
        public boolean allowNameMatching(){
        	return allowNameMatching;
        }

    }
    
    protected void assertReferenceTask( Task task )
    {
		assertReferences( task.getElement(), task.getCount(), task.isUnique(), task.allowNameMatching() );    	
    }
    
    protected void assertAllReferences( int count, List tasks )
    {
		assertEquals( callback.getReferences().size(), count );
    	if( tasks == null ) return;
    	Iterator i = tasks.iterator();
    	while( i.hasNext() )
    	{
    		assertReferenceTask( (Task)i.next() );
    	}
    }

	protected List createTaskList( Task t1 )
	{
		List result = new ArrayList(); 
		result.add( t1 );
		return result;
	}
    
    protected List createTaskList( Task t1, Task t2 )
    {
    	List result = createTaskList(t1); 
		result.add( t2 );
		return result;
    }
    
	protected List createTaskList( Task t1, Task t2, Task t3 )
	{
		List result = createTaskList(t1, t2);
		result.add( t3 );
		return result;
	}

	protected List createTaskList( Task t1, Task t2, Task t3, Task t4 )
	{
		List result = createTaskList(t1, t2, t3);
		result.add( t4 );
		return result;
	}
	
	protected List createTaskList( Task t1, Task t2, Task t3, Task t4, Task t5 )
	{
		List result = createTaskList(t1, t2, t3, t4);
		result.add( t5 );
		return result;		
	}
    /**
         * @param task
         * @param task2
         * @param task3
         * @param task4
         * @param task5
         * @param task6
         * @return
         */
    protected List createTaskList(Task task, Task task2, Task task3, Task task4, Task task5, Task task6)
    {
        List result = createTaskList( task, task2, task3, task4, task5 );
        result.add( task6 );
        return result;
    }

	protected List createTaskList(Task task, Task task2, Task task3, Task task4, Task task5, Task task6, Task task7)
	{
		List result = createTaskList( task, task2, task3, task4, task5, task6 );
		result.add( task7 );
		return result;
	}
	
	protected List createTaskList(Task task, Task task2, Task task3, Task task4, Task task5, Task task6, Task task7, Task task8 )
	{
		List result = createTaskList( task, task2, task3, task4, task5, task6, task7 );
		result.add( task8 );
		return result;
	}

	protected List createTaskList(Task task, Task task2, Task task3, Task task4, Task task5, Task task6, Task task7, Task task8, Task task9 )
	{
		List result = createTaskList( task, task2, task3, task4, task5, task6, task7, task8 );
		result.add( task9 );
		return result;
	}

	protected List createTaskList(Task task, Task task2, Task task3, Task task4, Task task5, Task task6, Task task7, Task task8, Task task9, Task task10 )
	{
		List result = createTaskList( task, task2, task3, task4, task5, task6, task7, task8, task9 );
		result.add( task10 );
		return result;
	}
	
	public boolean qualifiedNamesEquals( String [] fromAST, String [] theTruth)
	{
		if( fromAST == null || theTruth == null ) return false;
		if( fromAST.length !=  theTruth.length ) return false;
		for( int i = 0; i < fromAST.length; ++i )
		{
			if( !( fromAST[i].equals( theTruth[i] ) ) )
				return false;
		}
		return true;
	}

	protected void assertQualifiedName(String [] fromAST, String [] theTruth)
	{
		assertTrue( qualifiedNamesEquals( fromAST, theTruth ));
	}


}

Back to the top