-
Notifications
You must be signed in to change notification settings - Fork 187
/
AST2CPPTests.java
13824 lines (12394 loc) · 426 KB
/
AST2CPPTests.java
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
/*******************************************************************************
* 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:
* Andrew Niefer (IBM Corporation) - initial API and implementation
* Ed Swartz (Nokia)
* Markus Schorn (Wind River Systems)
* Andrew Ferguson (Symbian)
* Sergey Prigogin (Google)
* Thomas Corbat (IFS)
* Nathan Ridge
* Marc-Andre Laperle
* Richard Eames
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.LVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.XVALUE;
import static org.eclipse.cdt.core.parser.ParserLanguage.CPP;
import static org.eclipse.cdt.core.parser.tests.VisibilityAsserts.assertVisibility;
import static org.junit.Assert.assertNotEquals;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashSet;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTAttribute;
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.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
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.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
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.IASTImplicitName;
import org.eclipse.cdt.core.dom.ast.IASTImplicitNameOwner;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNameOwner;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTToken;
import org.eclipse.cdt.core.dom.ast.IASTTokenList;
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.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IProblemType;
import org.eclipse.cdt.core.dom.ast.IQualifierType;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConversionName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceAlias;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPPointerToMemberType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.dom.ast.cpp.SemanticQueries;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.util.AttributeUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassTemplate;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassTemplatePartialSpecialization;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPConstructor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunctionType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPMethod;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPPointerType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPQualifierType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVariable;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.OverloadableOperator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.index.IndexCPPSignatureUtil;
import org.eclipse.cdt.internal.core.parser.ParserException;
import junit.framework.TestSuite;
public class AST2CPPTests extends AST2CPPTestBase {
public AST2CPPTests() {
}
public AST2CPPTests(String name) {
super(name);
}
public static TestSuite suite() {
return suite(AST2CPPTests.class);
}
private void assertProblemBinding(int id, IBinding b) {
assertTrue(b instanceof IProblemBinding);
assertEquals(id, ((IProblemBinding) b).getID());
}
private void assertDefinition(ICPPBinding b) {
assertTrue(((IASTName) ((ICPPInternalBinding) b).getDefinition()).isDefinition());
}
private void assertDeclaration(ICPPBinding b) {
assertTrue(((IASTName) ((ICPPInternalBinding) b).getDeclarations()[0]).isDeclaration());
}
private ICPPMethod extractSingleMethod(IBinding[] bindings) {
assertEquals(1, bindings.length);
assertTrue(bindings[0] instanceof ICPPMethod);
return (ICPPMethod) bindings[0];
}
private ICPPClassType extractSingleClass(IBinding[] bindings) {
assertEquals(1, bindings.length);
assertTrue(bindings[0] instanceof ICPPClassType);
return (ICPPClassType) bindings[0];
}
private void checkNewExpression(IASTFunctionDefinition fdef, int i_expr, Class<?> placement, String type,
Class<?> init) {
IASTExpression expr;
ICPPASTNewExpression newExpr;
expr = getExpressionOfStatement(fdef, i_expr);
assertInstance(expr, ICPPASTNewExpression.class);
newExpr = (ICPPASTNewExpression) expr;
if (placement == null) {
assertNull(newExpr.getNewPlacement());
} else {
assertInstance(newExpr.getNewPlacement(), placement);
}
if (init == null) {
assertNull(newExpr.getNewInitializer());
} else {
assertInstance(newExpr.getNewInitializer(), init);
}
isTypeEqual(CPPVisitor.createType(newExpr.getTypeId()), type);
}
private void checkDeclDef(String[] declNames, String[] defNames, IASTDeclaration[] decls) {
int i = 0, j = 0;
for (IASTDeclaration decl : decls) {
if (decl instanceof ICPPASTLinkageSpecification) {
decl = ((ICPPASTLinkageSpecification) decl).getDeclarations()[0];
}
final IASTDeclarator[] dtors = ((IASTSimpleDeclaration) decl).getDeclarators();
for (IASTDeclarator dtor : dtors) {
final String name = dtor.getName().toString();
switch (dtor.getRoleForName(dtor.getName())) {
case IASTNameOwner.r_declaration:
assertTrue("Unexpected decl " + name, i < declNames.length);
assertEquals(declNames[i++], name);
break;
case IASTNameOwner.r_definition:
assertTrue("Unexpected decl " + name, j < defNames.length);
assertEquals(defNames[j++], name);
break;
default:
assertTrue(name, false);
}
}
}
assertEquals(declNames.length, i);
assertEquals(defNames.length, j);
}
protected static void assertSameType(IType first, IType second) {
assertNotNull(first);
assertNotNull(second);
assertTrue("Expected types to be the same, but first was: '" + first.toString() + "' and second was: '" + second
+ "'", first.isSameType(second));
}
private void checkUserDefinedLiteralIsType(String code, String type_name) throws Exception {
IASTTranslationUnit tu = parseAndCheckBindings(code, CPP);
IASTDeclaration[] declarations = tu.getDeclarations();
IASTDeclaration declaration = declarations[declarations.length - 1];
IASTInitializer init = ((IASTSimpleDeclaration) declaration).getDeclarators()[0].getInitializer();
IType type = ((IASTExpression) ((IASTEqualsInitializer) init).getInitializerClause()).getExpressionType();
assertEquals(type_name, type.toString());
}
private void checkUserDefinedLiteralIsRet(String code) throws Exception {
checkUserDefinedLiteralIsType(code, "Ret");
}
// int *zzz1 (char);
// int (*zzz2) (char);
// int ((*zzz3)) (char);
// int (*(zzz4)) (char);
public void testBug40768() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector col = new NameCollector();
tu.accept(col);
assertNoProblemBindings(col);
}
public void testBug40422() throws Exception {
IASTTranslationUnit tu = parse("class A { int y; }; int A::* x = 0;", CPP);
NameCollector col = new NameCollector();
tu.accept(col);
assertNoProblemBindings(col);
}
public void testBug43241() throws Exception {
parseAndCheckBindings("int m(int); int (*pm)(int) = &m; int f(){} int f(int); int x = f((*pm)(5));");
}
// class A { int m(int); };
// A a;
// int (A::*pm)(int) = &A::m;
// int f(){}
// int f(int);
// int x = f((a.*pm)(5));
public void testBug43242() throws Exception {
parseAndCheckBindings(getAboveComment());
}
// class A {
// int m;
// };
// A* a;
// int A::*pm;
// int f(){}
// int f(int);
// int x = f(a->*pm);
public void testBug43579() throws Exception {
parseAndCheckBindings();
}
public void testBug75189() throws Exception {
parseAndCheckBindings("struct A{};typedef int (*F) (A*);");
}
public void testBug75340() throws Exception {
IASTTranslationUnit tu = parseAndCheckBindings("void f(int i = 0, int * p = 0);");
IASTSimpleDeclaration sd = (IASTSimpleDeclaration) tu.getDeclarations()[0];
isParameterSignatureEqual(sd.getDeclarators()[0], "(int=0, int*=0)");
}
// bool f() {
// int first, last;
// if (first < 1 || last > 99)
// return false;
// }
public void testBug75858() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector col = new NameCollector();
tu.accept(col);
assertNoProblemBindings(col);
}
// enum type {A};
// enum type a, b;
// type c;
// enum type2 {A, B};
// enum type2 d, e;
public void testBug77967() throws Exception {
parseAndCheckBindings();
}
// int *p1; int *p2;
// union {
// struct {int a; int b;} A;
// struct {int a; int b;};
// } MyStruct;
// void test (void) {
// p1 = &MyStruct.A.a;
// p2 = &MyStruct.b;
// MyStruct.b = 1;
// }
public void testBug78103() throws Exception {
parseAndCheckBindings(getAboveComment());
}
// class B {
// public:
// B(int t);
// };
// class A : public B {
// public:
// A(int t);
// };
// A::A(int t) : B(t - 1) {}
public void testBug78883() throws Exception {
parseAndCheckBindings();
}
// #define REF_WRAP(e) class A { public: A (){ } A& foo2(e& v) { return *this; } }
// class B {
// REF_WRAP(B);
// B();
// void foo();
// };
public void testBug79540() throws Exception {
parseAndCheckBindings(getAboveComment());
}
public void testBug86282() throws Exception {
IASTTranslationUnit tu = parse("void foo() { int (* f[])() = new (int (*[10])()); }", CPP);
NameCollector col = new NameCollector();
tu.accept(col);
assertNoProblemBindings(col);
}
// class A {
// public:
// int x;
// A* next;
// };
// A* start;
// void test() {
// for(A *y = start; y->x != 0; y = y->next) {
// 42;
// }
// for(int x = 0 ; x < 10; x++) {
// }
// }
public void testBug95411() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector nameCol = new NameCollector();
tu.accept(nameCol);
assertNoProblemBindings(nameCol);
}
public void testBug95424() throws Exception {
IASTTranslationUnit tu = parse("void f(){ traits_type::copy(__r->_M_refdata(), __buf, __i); }", CPP,
ScannerKind.GNU, true);
tu = parse("void f(){ traits_type::copy(__r->_M_refdata(), __buf, __i); }", CPP, ScannerKind.STD, true);
IASTFunctionDefinition f = (IASTFunctionDefinition) tu.getDeclarations()[0];
IASTCompoundStatement cs = (IASTCompoundStatement) f.getBody();
IASTExpressionStatement es = (IASTExpressionStatement) cs.getStatements()[0];
assertTrue(es.getExpression() instanceof IASTFunctionCallExpression);
}
// #define CURLOPTTYPE_OBJECTPOINT 10000
// #define CINIT(name,type,number) CURLOPT_ ## name = CURLOPTTYPE_ ## type + number
// typedef enum {
// CINIT(FILE, OBJECTPOINT, 1),
// CINIT(URL, OBJECTPOINT, 2)
// } CURLoption;
public void testBug102825() throws Exception {
parseAndCheckBindings(getAboveComment());
}
// template <class T, int someConst=0>
// class WithTemplate {};
// int main () {
// WithTemplate <int, 10> normalInstance;
// const int localConst=10;
// WithTemplate <int, localConst> brokenInstance;
// return 0;
// }
public void testBug103578() throws Exception {
parseAndCheckBindings(getAboveComment());
}
// template <typename T>
// struct A {
// template <typename U> class B;
// typedef int type;
// };
//
// template <typename T>
// template <typename U>
// struct A<T>::B {
// typedef typename A::type type;
// };
public void testBug433556() throws Exception {
parseAndCheckBindings();
}
// class A { } a;
public void testSimpleClass() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTCompositeTypeSpecifier compTypeSpec = (IASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A = compTypeSpec.getName();
IASTDeclarator dtor = decl.getDeclarators()[0];
IASTName name_a = dtor.getName();
ICompositeType A = (ICompositeType) name_A.resolveBinding();
IVariable a = (IVariable) name_a.resolveBinding();
ICompositeType A_2 = (ICompositeType) a.getType();
assertNotNull(A);
assertNotNull(a);
assertSame(A, A_2);
}
// class A; class A {};
public void testClassForwardDecl() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTElaboratedTypeSpecifier elabSpec = (IASTElaboratedTypeSpecifier) decl.getDeclSpecifier();
IASTName name_elab = elabSpec.getName();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[1];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier compSpec = (IASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_comp = compSpec.getName();
ICompositeType A = (ICompositeType) name_elab.resolveBinding();
ICompositeType A_2 = (ICompositeType) name_comp.resolveBinding();
assertNotNull(A);
assertSame(A, A_2);
}
// class A {}; A a;
public void testVariable() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier compType = (IASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A = compType.getName();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[1];
IASTDeclarator dtor = decl.getDeclarators()[0];
IASTName name_a = dtor.getName();
IASTNamedTypeSpecifier namedSpec = (IASTNamedTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A2 = namedSpec.getName();
IVariable a = (IVariable) name_a.resolveBinding();
ICompositeType A1 = (ICompositeType) a.getType();
ICompositeType A2 = (ICompositeType) name_A2.resolveBinding();
ICompositeType A = (ICompositeType) name_A.resolveBinding();
assertNotNull(a);
assertNotNull(A);
assertSame(A, A1);
assertSame(A1, A2);
}
// class A { int f; };
public void testField() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
ICPPASTCompositeTypeSpecifier comp = (ICPPASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A = comp.getName();
decl = (IASTSimpleDeclaration) comp.getMembers()[0];
IASTDeclarator dtor = decl.getDeclarators()[0];
IASTName name_f = dtor.getName();
ICPPClassType A = (ICPPClassType) name_A.resolveBinding();
IField f = (IField) name_f.resolveBinding();
assertNotNull(A);
assertNotNull(f);
assertSame(f.getScope(), A.getCompositeScope());
}
// class A { int f(); };
public void testMethodDeclaration() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A = comp.getName();
decl = (IASTSimpleDeclaration) comp.getMembers()[0];
IASTDeclarator dtor = decl.getDeclarators()[0];
IASTName name_f = dtor.getName();
ICPPClassType A = (ICPPClassType) name_A.resolveBinding();
ICPPMethod f = (ICPPMethod) name_f.resolveBinding();
assertNotNull(A);
assertNotNull(f);
assertSame(f.getScope(), A.getCompositeScope());
}
// class A { void f(); };
// void A::f() { }
public void testMethodDefinition() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A = comp.getName();
decl = (IASTSimpleDeclaration) comp.getMembers()[0];
IASTDeclarator dtor = decl.getDeclarators()[0];
IASTName name_f1 = dtor.getName();
IASTFunctionDefinition def = (IASTFunctionDefinition) tu.getDeclarations()[1];
IASTFunctionDeclarator fdtor = def.getDeclarator();
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) fdtor.getName();
ICPPClassType A = (ICPPClassType) name_A.resolveBinding();
ICPPMethod f1 = (ICPPMethod) name_f1.resolveBinding();
ICPPMethod f2 = (ICPPMethod) name_f2.resolveBinding();
ICPPASTNameSpecifier[] qualifier = name_f2.getQualifier();
assertEquals(qualifier.length, 1);
IASTName qn1 = (IASTName) qualifier[0];
IASTName qn2 = name_f2.getLastName();
ICPPClassType A2 = (ICPPClassType) qn1.resolveBinding();
ICPPMethod f3 = (ICPPMethod) qn2.resolveBinding();
assertNotNull(A);
assertNotNull(f1);
assertSame(f1, f2);
assertSame(f2, f3);
assertSame(A, A2);
}
// class A { void f(); int i; };
// void A::f() { i; }
public void testMemberReference() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A = comp.getName();
decl = (IASTSimpleDeclaration) comp.getMembers()[0];
IASTDeclarator dtor = decl.getDeclarators()[0];
IASTName name_f1 = dtor.getName();
decl = (IASTSimpleDeclaration) comp.getMembers()[1];
dtor = decl.getDeclarators()[0];
IASTName name_i = dtor.getName();
IASTFunctionDefinition def = (IASTFunctionDefinition) tu.getDeclarations()[1];
IASTFunctionDeclarator fdtor = def.getDeclarator();
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) fdtor.getName();
IASTCompoundStatement compound = (IASTCompoundStatement) def.getBody();
IASTExpressionStatement statement = (IASTExpressionStatement) compound.getStatements()[0];
IASTIdExpression idExp = (IASTIdExpression) statement.getExpression();
IASTName name_i2 = idExp.getName();
ICPPClassType A = (ICPPClassType) name_A.resolveBinding();
ICPPMethod f1 = (ICPPMethod) name_f1.resolveBinding();
ICPPMethod f2 = (ICPPMethod) name_f2.resolveBinding();
ICPPField i1 = (ICPPField) name_i.resolveBinding();
ICPPField i2 = (ICPPField) name_i2.resolveBinding();
ICPPASTNameSpecifier[] qualifier = name_f2.getQualifier();
assertEquals(qualifier.length, 1);
IASTName qn1 = (IASTName) qualifier[0];
IASTName qn2 = name_f2.getLastName();
ICPPClassType A2 = (ICPPClassType) qn1.resolveBinding();
ICPPMethod f3 = (ICPPMethod) qn2.resolveBinding();
assertNotNull(A);
assertNotNull(f1);
assertNotNull(i1);
assertSame(f1, f2);
assertSame(f2, f3);
assertSame(A, A2);
assertSame(i1, i2);
}
// class A { int i; };
// class B : public A { void f(); };
// void B::f() { i; }
public void testBasicInheritance() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
ICPPASTCompositeTypeSpecifier comp = (ICPPASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_A1 = comp.getName();
decl = (IASTSimpleDeclaration) comp.getMembers()[0];
IASTName name_i1 = decl.getDeclarators()[0].getName();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[1];
comp = (ICPPASTCompositeTypeSpecifier) decl.getDeclSpecifier();
IASTName name_B1 = comp.getName();
ICPPASTBaseSpecifier base = comp.getBaseSpecifiers()[0];
IASTName name_A2 = (IASTName) base.getNameSpecifier();
decl = (IASTSimpleDeclaration) comp.getMembers()[0];
IASTName name_f1 = decl.getDeclarators()[0].getName();
IASTFunctionDefinition def = (IASTFunctionDefinition) tu.getDeclarations()[2];
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) def.getDeclarator().getName();
IASTName name_B2 = (IASTName) name_f2.getQualifier()[0];
IASTName name_f3 = name_f2.getLastName();
IASTCompoundStatement compound = (IASTCompoundStatement) def.getBody();
IASTExpressionStatement statement = (IASTExpressionStatement) compound.getStatements()[0];
IASTIdExpression idExp = (IASTIdExpression) statement.getExpression();
IASTName name_i2 = idExp.getName();
ICPPField i2 = (ICPPField) name_i2.resolveBinding();
ICPPField i1 = (ICPPField) name_i1.resolveBinding();
ICPPClassType A2 = (ICPPClassType) name_A2.resolveBinding();
ICPPClassType A1 = (ICPPClassType) name_A1.resolveBinding();
ICPPClassType B2 = (ICPPClassType) name_B2.resolveBinding();
ICPPClassType B1 = (ICPPClassType) name_B1.resolveBinding();
ICPPMethod f3 = (ICPPMethod) name_f3.resolveBinding();
ICPPMethod f2 = (ICPPMethod) name_f2.resolveBinding();
ICPPMethod f1 = (ICPPMethod) name_f1.resolveBinding();
assertNotNull(A1);
assertNotNull(B1);
assertNotNull(i1);
assertNotNull(f1);
assertSame(A1, A2);
assertSame(B1, B2);
assertSame(i1, i2);
assertSame(f1, f2);
assertSame(f2, f3);
}
// namespace A{
// int a;
// }
// namespace B{
// using namespace A;
// }
// namespace C{
// using namespace A;
// }
//
// namespace BC{
// using namespace B;
// using namespace C;
// }
//
// void f(){
// BC::a++; //ok
// }
public void testNamespaces() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
assertEquals(collector.size(), 13);
ICPPNamespace A = (ICPPNamespace) collector.getName(0).resolveBinding();
IVariable a = (IVariable) collector.getName(1).resolveBinding();
ICPPNamespace B = (ICPPNamespace) collector.getName(2).resolveBinding();
ICPPNamespace C = (ICPPNamespace) collector.getName(4).resolveBinding();
ICPPNamespace BC = (ICPPNamespace) collector.getName(6).resolveBinding();
IFunction f = (IFunction) collector.getName(9).resolveBinding();
assertInstances(collector, A, 3);
assertInstances(collector, a, 3);
assertInstances(collector, B, 2);
assertInstances(collector, C, 2);
assertInstances(collector, BC, 2);
assertInstances(collector, f, 1);
}
// int A;
// class A {};
// void f() {
// A++;
// class A a;
// }
public void testNameHiding() throws Exception {
String content = getAboveComment();
IASTTranslationUnit tu = parse(content, CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
assertEquals(collector.size(), 6);
IVariable vA = (IVariable) collector.getName(0).resolveBinding();
ICompositeType cA = (ICompositeType) collector.getName(1).resolveBinding();
IVariable a = (IVariable) collector.getName(5).resolveBinding();
assertSame(a.getType(), cA);
assertInstances(collector, vA, 2);
assertInstances(collector, cA, 2);
tu = parse(content, CPP);
collector = new NameCollector();
tu.accept(collector);
cA = (ICompositeType) collector.getName(1).resolveBinding();
IBinding A = collector.getName(3).resolveBinding();
vA = (IVariable) collector.getName(0).resolveBinding();
assertSame(vA, A);
}
// class A { void f(); };
// class B;
// void A::f() {
// B b;
// }
// int B;
public void testBlockTraversal() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
assertEquals(collector.size(), 9);
ICompositeType A = (ICompositeType) collector.getName(0).resolveBinding();
ICPPMethod f = (ICPPMethod) collector.getName(1).resolveBinding();
ICompositeType B = (ICompositeType) collector.getName(2).resolveBinding();
IVariable b = (IVariable) collector.getName(7).resolveBinding();
IVariable B2 = (IVariable) collector.getName(8).resolveBinding();
assertSame(b.getType(), B);
assertInstances(collector, A, 2);
assertInstances(collector, f, 3);
assertInstances(collector, B, 2);
assertInstances(collector, b, 1);
assertInstances(collector, B2, 1);
}
// void f(int i);
// void f(char c);
// void main() {
// f(1); //calls f(int);
// f('b');
// }
public void testFunctionResolution() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
IFunction f1 = (IFunction) collector.getName(0).resolveBinding();
IFunction f2 = (IFunction) collector.getName(2).resolveBinding();
assertInstances(collector, f1, 2);
assertInstances(collector, f2, 2);
}
// typedef struct {
// int x;
// } S;
// void f() {
// S myS;
// myS.x = 5;
// }
public void testSimpleStruct() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
ICPPClassType anonStruct = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPField x = (ICPPField) collector.getName(1).resolveBinding();
ITypedef S = (ITypedef) collector.getName(2).resolveBinding();
IFunction f = (IFunction) collector.getName(3).resolveBinding();
IVariable myS = (IVariable) collector.getName(5).resolveBinding();
assertInstances(collector, anonStruct, 1);
assertInstances(collector, x, 2);
assertInstances(collector, S, 2);
assertInstances(collector, f, 1);
assertInstances(collector, myS, 2);
}
// struct A;
// void f(){
// struct A;
// struct A * a;
// }
public void testStructureTags_a() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2).resolveBinding();
IVariable a = (IVariable) collector.getName(4).resolveBinding();
assertNotNull(a);
assertNotNull(A1);
assertNotNull(A2);
assertNotSame(A1, A2);
assertInstances(collector, A1, 1);
assertInstances(collector, A2, 2);
}
// struct A;
// void f(){
// struct A * a;
// }
public void testStructureTags_b() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2).resolveBinding();
IVariable a = (IVariable) collector.getName(3).resolveBinding();
assertNotNull(a);
assertNotNull(A1);
assertNotNull(A2);
assertSame(A1, A2);
assertInstances(collector, A1, 2);
}
// struct A;
// struct A * a;
// struct A { int i; };
// void f() {
// a->i;
// }
public void testStructureDef() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
IVariable a = (IVariable) collector.getName(2).resolveBinding();
ICPPField i = (ICPPField) collector.getName(4).resolveBinding();
assertInstances(collector, A1, 3);
assertInstances(collector, a, 2);
assertInstances(collector, i, 2);
}
// struct x {};
// void f(int x) {
// struct x i;
// }
public void testStructureNamespace() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
ICPPClassType x = (ICPPClassType) collector.getName(0).resolveBinding();
assertInstances(collector, x, 2);
}
// void f(int a);
// void f(int b){
// b;
// }
public void testFunctionDef() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
IFunction f = (IFunction) collector.getName(0).resolveBinding();
IParameter a = (IParameter) collector.getName(1).resolveBinding();
assertInstances(collector, f, 2);
assertInstances(collector, a, 3);
IScope scope = a.getScope();
assertNotNull(scope);
assertSame(scope.getParent(), f.getScope());
}
// void f();
// void g() {
// f();
// }
// void f(){ }
public void testSimpleFunctionCall() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
IFunction f = (IFunction) collector.getName(0).resolveBinding();
IFunction g = (IFunction) collector.getName(1).resolveBinding();
assertInstances(collector, f, 3);
assertInstances(collector, g, 1);
}
// void f() {
// for(int i = 0; i < 5; i++) {
// i;
// }
// }
public void testForLoop() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
IVariable i = (IVariable) collector.getName(1).resolveBinding();
assertInstances(collector, i, 4);
}
// struct A { int x; };
// void f(){
// ((struct A *) 1)->x;
// }
public void testExpressionFieldReference() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), CPP);
NameCollector collector = new NameCollector();
tu.accept(collector);
ICPPClassType A = (ICPPClassType) collector.getName(0).resolveBinding();
IField x = (IField) collector.getName(1).resolveBinding();
assertInstances(collector, A, 2);
assertInstances(collector, x, 2);
}
// enum hue { red, blue, green };
// enum hue col, *cp;
// void f() {
// col = blue;
// cp = &col;