1 /**
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: $
29 */
30
31 package cppast;
32
33 import java.io.BufferedReader;
34 import java.io.FileReader;
35 import java.io.IOException;
36 import java.io.StringReader;
37 import java.net.URL;
38 import junit.framework.TestCase;
39
40 /**
41 * @author Mathieu Champlon
42 */
43 public class ParserTest extends TestCase
44 {
45 private void parse( final String data ) throws ParseException
46 {
47 new Parser( new StringReader( data ) ).translation_unit();
48 }
49
50 public void testSemiColumExternalDeclaration() throws ParseException
51 {
52 parse( ";" );
53 }
54
55 public void testTypedefType() throws ParseException
56 {
57 parse( "typedef MyClass T_MyClass;" );
58 }
59
60 public void testTypedefTypeInNamespace() throws ParseException
61 {
62 parse( "typedef std::vector T_Vector;" );
63 }
64
65 public void testTypedefTemplateType() throws ParseException
66 {
67 parse( "typedef vector< MyClass > T_MyClass;" );
68 }
69
70 public void testTypedefTemplateNamespacedType() throws ParseException
71 {
72 parse( "typedef vector< nm::MyClass > T_MyClass;" );
73 }
74
75 public void testTypedefTemplateTypeInNamespace() throws ParseException
76 {
77 parse( "typedef std::vector< MyClass > T_MyClass;" );
78 }
79
80 public void testTypedefTemplateTypeOfTemplateType() throws ParseException
81 {
82 parse( "typedef std::vector< boost::shared_ptr< MyClass > > T_MyClass;" );
83 }
84
85 public void testTypedefTemplateTypeOfTemplateParameter() throws ParseException
86 {
87 parse( "typedef typename MyClass::template MySubClass< MyType >::Member T_Type;" );
88 }
89
90 public void testTemplateClassMemberTypeVariable() throws ParseException
91 {
92 parse( "MyClass< MyType >::Member< MyOtherType >::AnotherMember i;" );
93 }
94
95 public void testVariableType() throws ParseException
96 {
97 parse( "vector v;" );
98 }
99
100 public void testVariableTypeInNamespace() throws ParseException
101 {
102 parse( "std::vector v;" );
103 }
104
105 public void testVariableOfTemplateType() throws ParseException
106 {
107 parse( "auto_ptr< MyClass > pMyClass;" );
108 }
109
110 public void testVariableTemplateNamespacedType() throws ParseException
111 {
112 parse( "auto_ptr< nm::MyClass > pMyClass_;" );
113 }
114
115 public void testVariableTemplateTypeInNamespace() throws ParseException
116 {
117 parse( "std::auto_ptr< MyClass > pMyClass_;" );
118 }
119
120 public void testVariableTemplateNamespacedTypeInNamespace() throws ParseException
121 {
122 parse( "std::auto_ptr< nm::MyClass > pMyClass_;" );
123 }
124
125 public void testTypedefTypename() throws ParseException
126 {
127 parse( "typedef typename T_Elements::iterator IT_Elements;" );
128 }
129
130 public void testTypenameQualifier() throws ParseException
131 {
132 parse( "const typename CIT_Elements it;" );
133 }
134
135 public void testMutableQualifier() throws ParseException
136 {
137 parse( "mutable float f;" );
138 }
139
140 public void testTemplateFunctionDeclaration() throws ParseException
141 {
142 parse( "template< typename T > void MyFunction( T& t );" );
143 }
144
145 public void testTemplateFunctionDefinition() throws ParseException
146 {
147 parse( "template< typename T > void MyFunction( T& t ) { t.Something(); }" );
148 }
149
150 public void testTemplateMethodDeclaration() throws ParseException
151 {
152 parse( "class MyClass{ template< typename T > void MyMethod( T& t ); };" );
153 }
154
155 public void testUserTypeDefaultTemplateParameterClassDefinition() throws ParseException
156 {
157 parse( "template< typename T = DefaultType > class MyClass {};" );
158 }
159
160 public void testBaseTypeDefaultTemplateParameterClassDefinition() throws ParseException
161 {
162 parse( "template< typename T = int > class MyClass {};" );
163 }
164
165 public void testTemplateSpecializedMethodDeclaration() throws ParseException
166 {
167 parse( "class MyClass{ template<> void MyMethod< MyType >(); };" );
168 }
169
170 public void testTemplateSpecializedClassDefinition() throws ParseException
171 {
172 parse( "template<> class MyClass< MyType > : public MySuperClass {};" );
173 }
174
175 public void testTemplateSpecializedNamespacedClassDefinition() throws ParseException
176 {
177 parse( "template<> class my_namespace::MyClass< MyType > : public MySuperClass {};" );
178 }
179
180 public void testTemplateMethodDefinition() throws ParseException
181 {
182 parse( "class MyClass { template< typename T > void MyMethod() {} };" );
183 }
184
185 public void testTemplateSpecializedClassDeclaration() throws ParseException
186 {
187 parse( "template<> class MyClass< MyType > {};" );
188 }
189
190 public void testTemplateSpecializedSubClassTemplateDeclaration() throws ParseException
191 {
192 parse( "class MyClass{ template< typename T > class MySubClass< const T > {}; };" );
193 }
194
195 public void testTemplateSpecializedSubClassDeclaration() throws ParseException
196 {
197 parse( "class MyClass { template<> class MySubClass<true> {}; };" );
198 }
199
200 public void testTemplateClassInstanciation() throws ParseException
201 {
202 parse( "MyClass< int > instance;" );
203 }
204
205 public void testTypedTemplateClassInstanciation() throws ParseException
206 {
207 parse( "MyClass< MyType > instance;" );
208 }
209
210 public void testSpecializedTemplateClassInstanciation() throws ParseException
211 {
212 parse( "MyClass< 0 > instance;" );
213 }
214
215 public void testTemplateSpecializedClassInstanciation() throws ParseException
216 {
217 parse( "MyClass<> instance;" );
218 }
219
220 public void testTemplateMemberInitializer() throws ParseException
221 {
222 parse( "MyClass::MyClass() : my_namespace::MyType< AnotherType >( data ) {}" );
223 }
224
225 public void testTemplateTypeClassDeclaration() throws ParseException
226 {
227 parse( "template< int > class MyClass {};" );
228 }
229
230 public void testSpecializedTemplateTypeClassDeclaration() throws ParseException
231 {
232 parse( "template<> class MyClass< 0 > {};" );
233 }
234
235 public void testSpecializedTemplateMemberPointerClassDeclaration() throws ParseException
236 {
237 parse( "class MyClass< void (MyOtherClass::*)() > {};" );
238 }
239
240 public void testTemplateMethodOfTemplateClassSeparateDefinition() throws ParseException
241 {
242 parse( "template< typename T1 > class MyClass { template< typename T2 > void MyMethod(); };" + '\n'
243 + "template< typename T1 > template< typename T2 > void MyClass< T1 >::MyMethod() {}" );
244 }
245
246 public void testTemplateFunctionCall() throws ParseException
247 {
248 parse( "void MyFunction() { process< char* >(); }" );
249 parse( "void MyFunction() { process< int >( 2 ); }" );
250 parse( "void MyFunction() { stream.read( function< char* >( pData ) ); }" );
251 parse( "void MyFunction() { process< 1 >(); }" );
252 parse( "void MyFunction() { if( i < 0 && j > 0 ); }" );
253 parse( "void MyFunction() { process< (1 > 2) >(); }" );
254 }
255
256 public void testTemplateConstructorDefinition() throws ParseException
257 {
258 parse( "class MyClass { template< typename T > MyClass(); };" );
259 }
260
261 public void testTemplateConstructorSeparateDefinition() throws ParseException
262 {
263 parse( "class MyClass {}; template< typename T > MyClass< T >::MyClass() {}" );
264 }
265
266 public void testTemplateMethodCallWithTemplateParameter() throws ParseException
267 {
268 parse( "void MyFunction() { data.template Method< MyType >( 0 ); }" );
269 }
270
271 public void testTemplateStaticMethodCallWithTemplateParameter() throws ParseException
272 {
273 parse( "void MyFunction() { MyClass::template Method< MyType >( 0 ); }" );
274 }
275
276 public void testUnsupportedTemplateFunctionCallThrowsException()
277 {
278 try
279 {
280 parse( "void MyFunction() { process< 1 < 2 >(); }" );
281 }
282 catch( ParseException e )
283 {
284 return;
285 }
286 fail( "must throw an exception" );
287 }
288
289 public void testUnsupportedTemplateFunctionCall2ThrowsException()
290 {
291 try
292 {
293 parse( "void MyFunction() { process< 1 ? 2 : 3 >(); }" );
294 }
295 catch( ParseException e )
296 {
297 return;
298 }
299 fail( "must throw an exception" );
300 }
301
302 public void testBuiltInTypeCast() throws ParseException
303 {
304 parse( "void MyFunction() { char( 1 ); }" );
305 }
306
307 public void testUnsignedBuiltInTypeCast() throws ParseException
308 {
309 parse( "void MyFunction() { unsigned char( 1 ); }" );
310 }
311
312 public void testSignedBuiltInTypeCast() throws ParseException
313 {
314 parse( "void MyFunction() { signed char( 1 ); }" );
315 }
316
317 public void testUnsignedLongIntVariableDeclaration() throws ParseException
318 {
319 parse( "unsigned long int i;" );
320 }
321
322 public void testUnsignedLongIntTypeCast() throws ParseException
323 {
324 parse( "void MyFunction() { unsigned long int( 1 ); }" );
325 }
326
327 public void testTypeCastAssignmentExpression() throws ParseException
328 {
329 parse( "void MyFunction() { int( i = 0 ); }" );
330 }
331
332 public void testDestructorDefinitionWithoutClassDeclarationIsValid() throws ParseException
333 {
334 parse( "MyClass::~MyClass() {}" );
335 }
336
337 public void testTildeUnaryOperator() throws ParseException
338 {
339 parse( "int i = 0; int j = ~i;" );
340 }
341
342 public void testMethodCallWithinVariableInitialization() throws ParseException
343 {
344 parse( "void MyMethod() { int data( something.Method() ); }" );
345 }
346
347 public void testUsingSeveralNamespaces() throws ParseException
348 {
349 final Parser parser = new Parser( new StringReader( "namespace my_namespace { class MyClass{}; }" ) );
350 parser.translation_unit();
351 parser.ReInit( new StringReader( "using namespace my_namespace; using namespace another_namespace;"
352 + "MyClass::~MyClass() {}" ) );
353 parser.translation_unit();
354 }
355
356 public void testUsingNestedNamespacesFullPath() throws ParseException
357 {
358 final Parser parser = new Parser( new StringReader(
359 "namespace my_namespace { namespace inner { class MyClass{}; } }" ) );
360 parser.translation_unit();
361 parser.ReInit( new StringReader( "using namespace my_namespace::inner; MyClass::~MyClass() {}" ) );
362 parser.translation_unit();
363 }
364
365 public void testUsingNestedNamespacesHalfPath() throws ParseException
366 {
367 final Parser parser = new Parser( new StringReader(
368 "namespace my_namespace { namespace inner { class MyClass{}; } }" ) );
369 parser.translation_unit();
370 parser.ReInit( new StringReader( "using namespace my_namespace; inner::MyClass::~MyClass() {}" ) );
371 parser.translation_unit();
372 }
373
374 public void testUsingNamespaceInFunction() throws ParseException
375 {
376 parse( "void MyFunction() { using namespace my_namespace; }" );
377 }
378
379 public void testUsingClass() throws ParseException
380 {
381 parse( "using my_namespace::MyClass;" );
382 }
383
384 public void testMemberUsingClass() throws ParseException
385 {
386 parse( "class MyClass { using my_namespace::i; };" );
387 }
388
389 public void testFunctionUsingClass() throws ParseException
390 {
391 parse( "void MyFunction() { using my_namespace::i; }" );
392 }
393
394 public void testNamespaceAliasing() throws ParseException
395 {
396 parse( "namespace C = A::B;" );
397 }
398
399 public void testConstructorDefinitionThrowSpecification() throws ParseException
400 {
401 parse( "class MyClass{}; MyClass::MyClass() throw() {}" );
402 }
403
404 public void testNamespacedConstructorDefinition() throws ParseException
405 {
406 parse( "my_namespace::MyClass::MyClass() {}" );
407 }
408
409 public void testConversionOperator() throws ParseException
410 {
411 parse( "class MyClass{ operator const char* const() const {} };" );
412 }
413
414 public void testConversionOperatorOfTemplateClassSeparateDefinition() throws ParseException
415 {
416 parse( "template< typename T > MyClass<T>::operator bool() {}" );
417 }
418
419 public void testConstConversionOperatorSeparateDefinition() throws ParseException
420 {
421 parse( "MyClass::operator bool() const {}" );
422 }
423
424 public void testInlineOperatorSeparateDefinition() throws ParseException
425 {
426 parse( "inline MyClass::operator bool() {}" );
427 }
428
429 public void testInlinePureVirtualOperatorDeclaration() throws ParseException
430 {
431 parse( "class MyClass{ inline virtual operator bool() = 0; };" );
432 }
433
434 public void testFunctionReturningConstPointer() throws ParseException
435 {
436 parse( "const char* const MyFunction() { return 0; };" );
437 }
438
439 public void testMemberPointerWithAnonymousParameter() throws ParseException
440 {
441 parse( "typedef void (T::*M)( P& );" );
442 }
443
444 public void testMemberPointerWithParameterByCopy() throws ParseException
445 {
446 parse( "typedef void (T::*M)( P p );" );
447 }
448
449 public void testInnerTemplateClass() throws ParseException
450 {
451 parse( "class MyClass { template< typename T > class MyInnerClass {}; };" );
452 }
453
454 public void testSingleLineCommentAtEndOfFile() throws ParseException
455 {
456 parse( "// single line comment at end of file" );
457 }
458
459 public void testPreProcessorDirective() throws ParseException
460 {
461 parse( "#endif" + '\n' + ";" );
462 }
463
464 public void testPreProcessorDirectiveAtEndOfFile() throws ParseException
465 {
466 parse( "#endif" );
467 }
468
469 public void testPreProcessorOnSeveralLinesAtEndOfFile() throws ParseException
470 {
471 parse( "#define DEF anything \\" + '\n' + "should be escaped" );
472 }
473
474 public void testCommentAtEndOfFile() throws ParseException
475 {
476 parse( "/* should be escaped */" );
477 }
478
479 public void testEnumWithoutCommaAfterLastValue() throws ParseException
480 {
481 parse( "enum MyEnum { my_value };" );
482 }
483
484 public void testEnumWithTwoValues() throws ParseException
485 {
486 parse( "enum MyEnum { my_value, my_other_value };" );
487 }
488
489 public void testEnumWithCommaAfterLastValue() throws ParseException
490 {
491 parse( "enum MyEnum { my_value, };" );
492 }
493
494 public void testEnumWithTwoValuesWithCommaAfterLastValue() throws ParseException
495 {
496 parse( "enum MyEnum { my_value, my_other_value, };" );
497 }
498
499 public void testEmptyEnum() throws ParseException
500 {
501 parse( "enum MyEnum {};" );
502 }
503
504 public void testEmptyAnonymousEnum() throws ParseException
505 {
506 parse( "enum {};" );
507 }
508
509 public void testVariableParenthesisInitialization() throws ParseException
510 {
511 parse( "int var( 3 );" );
512 }
513
514 public void testNamespaceAliasingDeclaration() throws ParseException
515 {
516 parse( "namespace bfs = boost::filesystem;" );
517 }
518
519 public void testBitFieldDeclaration() throws ParseException
520 {
521 parse( "class MyClass { int my_value : 1; };" );
522 }
523
524 public void testStaticConstMemberDataInPlaceInitialization() throws ParseException
525 {
526 parse( "class MyClass { static const int data = 0x80; };" );
527 }
528
529 public void testExplicitConstructorDeclaration() throws ParseException
530 {
531 parse( "class MyClass { explicit MyClass(); };" );
532 }
533
534 public void testExplicitConstructorDefinition() throws ParseException
535 {
536 parse( "class MyClass { explicit MyClass() : data_( 0 ) {} };" );
537 }
538
539 public void testPureVirtualDestructorDeclaration() throws ParseException
540 {
541 parse( "class MyClass { virtual ~MyClass() = 0; };" );
542 }
543
544 public void testMemberDataList() throws ParseException
545 {
546 parse( "class MyClass{ float v1, v2; };" );
547 }
548
549 public void testReturnParenthezizedExpression() throws ParseException
550 {
551 parse( "int MyFunction() { return (stat); }" );
552 }
553
554 public void testReturnParenthezizedComplexExpression() throws ParseException
555 {
556 parse( "int MyFunction() { return (int)(2 * (stat)); }" );
557 }
558
559 public void testSizeOfBaseType() throws ParseException
560 {
561 parse( "void MyFunction() { sizeof( char ); }" );
562 }
563
564 public void testSizeOfUserType() throws ParseException
565 {
566 parse( "void MyFunction() { sizeof( MyType ); }" );
567 }
568
569 public void testSizeOfUserTypeReference() throws ParseException
570 {
571 parse( "void MyFunction() { sizeof( MyType& ); }" );
572 }
573
574 public void testSizeOfAnExpression() throws ParseException
575 {
576 parse( "void MyFunction() { sizeof( pValue->m ); }" );
577 }
578
579 public void testSizeOfFunctionCall() throws ParseException
580 {
581 parse( "void MyFunction() { sizeof( data.Method( 0 ) ); }" );
582 }
583
584 public void testSizeOfAssignment() throws ParseException
585 {
586 parse( "void MyFunction() { sizeof( i = 0 ); }" );
587 }
588
589 public void testSizeOfCommaExpression() throws ParseException
590 {
591 parse( "void MyFunction() { sizeof( 0, 1 ); }" );
592 }
593
594 public void testSizeOfWithoutParenthesis() throws ParseException
595 {
596 parse( "void MyFunction() { sizeof i; }" );
597 parse( "void MyFunction() { sizeof 9; }" );
598 parse( "void MyFunction() { sizeof -3; }" );
599 }
600
601 public void testGlobalScopeOverrideFunctionCall() throws ParseException
602 {
603 parse( "void MyFunction() { ::Call(); }" );
604 }
605
606 public void testStringJuxtaposition() throws ParseException
607 {
608 parse( "void MyFunction() { Call( \"abc\" \"def\" ); }" );
609 }
610
611 public void testInitializedArrayLastElementFollowedByComma() throws ParseException
612 {
613 parse( "void MyFunction() { static int data[] = { 0, 1, }; }" );
614 }
615
616 public void testIfStatementWithAssignment() throws ParseException
617 {
618 parse( "void MyFunction() { if( int i = 0 ); }" );
619 }
620
621 public void testConditionalExpression() throws ParseException
622 {
623 parse( "void MyFunction() { i ? x = 0 : x = 1; }" );
624 }
625
626 public void testDestructorWithExceptionSpecificationDeclaration() throws ParseException
627 {
628 parse( "class MyClass { ~MyClass() throw(); };" );
629 }
630
631 public void testDestructorWithExplicitVoidParameterDeclaration() throws ParseException
632 {
633 parse( "class MyClass { ~MyClass( void ); };" );
634 }
635
636 public void testMethodWithExceptionSpecificationDeclaration() throws ParseException
637 {
638 parse( "class MyClass { void MyMethod() throw(); };" );
639 }
640
641 public void testTypeidOfUserTypeReference() throws ParseException
642 {
643 parse( "void MyFunction() { typeid( MyType& ); }" );
644 }
645
646 public void testTypeidOfExpression() throws ParseException
647 {
648 parse( "void MyFunction() { typeid( data.Method( 0 ) ); }" );
649 }
650
651 public void testTypeidOfCommaExpression() throws ParseException
652 {
653 parse( "void MyFunction() { typeid( i, j ); }" );
654 }
655
656 public void testConditionWithFunctionCall() throws ParseException
657 {
658 parse( "void MyFunction() { if( data.Method( 0 ) ); }" );
659 }
660
661 public void testTypeidOfAssignmentExpression() throws ParseException
662 {
663 parse( "void MyFunction() { typeid( i = 0 ); }" );
664 }
665
666 public void testTypeidOfThis() throws ParseException
667 {
668 parse( "void MyFunction() { typeid( this ); }" );
669 }
670
671 public void testTypeidOfContentOfThis() throws ParseException
672 {
673 parse( "void MyFunction() { typeid( *this ); }" );
674 }
675
676 public void testCanRetrieveNameOfTypeidResult() throws ParseException
677 {
678 parse( "void MyFunction() { typeid( MyClass ).name(); }" );
679 }
680
681 public void testMutableMember() throws ParseException
682 {
683 parse( "class MyClass { mutable int i; };" );
684 }
685
686 public void testVolatileMember() throws ParseException
687 {
688 parse( "class MyClass { volatile int i; };" );
689 }
690
691 public void testVolatileVariable() throws ParseException
692 {
693 parse( "volatile int i;" );
694 }
695
696 public void testConstUnsignedIntVariable() throws ParseException
697 {
698 parse( "const unsigned int i;" );
699 }
700
701 public void testVolatileConstUnsignedIntVariable() throws ParseException
702 {
703 parse( "volatile const unsigned int i;" );
704 }
705
706 public void testFriendClassDeclaration() throws ParseException
707 {
708 parse( "friend MyClass;" );
709 }
710
711 public void testFriendMemberClassDeclaration() throws ParseException
712 {
713 parse( "class MyClass { friend AnotherClass; };" );
714 }
715
716 public void testVolatileMethodDeclaration() throws ParseException
717 {
718 parse( "class MyClass { void MyMethod() volatile; };" );
719 }
720
721 public void testVolatileMethodDefinition() throws ParseException
722 {
723 parse( "class MyClass { void MyMethod() volatile {} };" );
724 }
725
726 public void testVolatilePointerTypeConversionOperatorDeclaration() throws ParseException
727 {
728 parse( "class MyClass { operator AnotherClass* volatile(); };" );
729 }
730
731 public void testVolatileConversionOperatorDeclaration() throws ParseException
732 {
733 parse( "class MyClass { operator int() volatile; };" );
734 }
735
736 public void testInfiniteForLoop() throws ParseException
737 {
738 parse( "void MyFunction() { for(;;); }" );
739 }
740
741 public void testAnonymousEnumVariableDeclaration() throws ParseException
742 {
743 parse( "enum {} MyEnum;" );
744 }
745
746 public void testAnonymousClassVariableDeclaration() throws ParseException
747 {
748 parse( "class {} MyClass;" );
749 }
750
751 public void testAnonymousClassClassMember() throws ParseException
752 {
753 parse( "class MyClass{ class {}; };" );
754 }
755
756 public void testForWithFunctionCallsWithSeveralParametersAsInitialization() throws ParseException
757 {
758 parse( "void MyFunction() { for( call( i, j );; ); }" );
759 }
760
761 public void testTemplateFriendClassMemberDeclaration() throws ParseException
762 {
763 parse( "class MyClass{ template< typename T > friend class AnotherClass; };" );
764 }
765
766 public void testTemplateClassMemberDeclaration() throws ParseException
767 {
768 parse( "class MyClass{ template< typename T > friend class AnotherClass; };" );
769 }
770
771 public void testClassForwardDeclarationMember() throws ParseException
772 {
773 parse( "class MyClass{ class AnotherClass; };" );
774 }
775
776 public void testFriendClassForwardDeclarationMember() throws ParseException
777 {
778 parse( "class MyClass{ friend class AnotherClass; };" );
779 }
780
781 public void testTemplateMemberVariableOfTemplateType() throws ParseException
782 {
783 parse( "template< typename T > class MyClass{ typename MyType< T > t; };" );
784 }
785
786 public void testClassForwardDeclaration() throws ParseException
787 {
788 parse( "class MyClass;" );
789 }
790
791 public void testPureVirtualDestructorWithImplementation() throws ParseException
792 {
793 parse( "class MyClass{ virtual ~MyClass() = 0 {} };" );
794 }
795
796 public void testPureVirtualMethodWithImplementation() throws ParseException
797 {
798 parse( "class MyClass{ virtual void MyMethod() = 0 {} };" );
799 }
800
801 public void testPlacementNew() throws ParseException
802 {
803 parse( "void MyFunction() { new (where) MyClass(); }" );
804 }
805
806 public void testNewWithoutParameters() throws ParseException
807 {
808 parse( "void MyFunction() { new MyClass; }" );
809 }
810
811 public void testNewType() throws ParseException
812 {
813 parse( "void MyFunction() { new MyClass(); }" );
814 }
815
816 public void testNewTypeWithParameters() throws ParseException
817 {
818 parse( "void MyFunction() { new MyClass( 9, data.Method( i, j - 1 ) ); }" );
819 }
820
821 public void testAsmExpression() throws ParseException
822 {
823 parse( "void MyFunction() { asm( \"nop\" ); }" );
824 }
825
826 public void testThrowWithoutAnException() throws ParseException
827 {
828 parse( "void MyFunction() { throw; }" );
829 }
830
831 public void testThrowAnException() throws ParseException
832 {
833 parse( "void MyFunction() { throw e; }" );
834 }
835
836 public void testEmptyStatement() throws ParseException
837 {
838 parse( "void MyFunction() { ; }" );
839 }
840
841 public void testTemplateForwardDeclarationDoesNotRegisterTemplateTypeAsType() throws ParseException
842 {
843 parse( "template< typename T > Type1; template< typename T > Type2;" );
844 }
845
846 public void testOneVariableArgumentFunctionDeclaration() throws ParseException
847 {
848 parse( "void MyFunction( ... );" );
849 }
850
851 public void testVariableArgumentFunctionDeclaration() throws ParseException
852 {
853 parse( "void MyFunction( int x, ...);" );
854 }
855
856 public void testOneVariableArgumentFunctionDefinition() throws ParseException
857 {
858 parse( "void MyFunction( ... ) {}" );
859 }
860
861 public void testVariableArgumentFunctionDefinition() throws ParseException
862 {
863 parse( "void MyFunction( int x, ... ) {}" );
864 }
865
866 public void testIntegerParameterInTemplateDeclaration() throws ParseException
867 {
868 parse( "template< typename T, int n > void MyFunction();" );
869 }
870
871 public void testVoidDefaultTemplateParameterDeclaration() throws ParseException
872 {
873 parse( "template< typename T = void > void MyFunction();" );
874 }
875
876 public void testVoidPointerDefaultTemplateParameterDeclaration() throws ParseException
877 {
878 parse( "template< typename T = void* > void MyFunction();" );
879 }
880
881 public void testHexadecimalEscapeSequenceInString() throws ParseException
882 {
883 parse( "char* str = \"\\x02\\x03\";" );
884 }
885
886 public void testBackslashAtEndOfLineIsIgnored() throws ParseException
887 {
888 parse( "int i\\\n; int j\\\r;" );
889 }
890
891 public void testBackslashAtEndOfLineInStringIsIgnored() throws ParseException
892 {
893 parse( "char* str = \"abc\\\ndef\"; char* str2 = \"abc\\\rdef\"; char* str3 = \"abc\\\r\ndef\";" );
894 }
895
896 public void testCp1252NotAsciiCharacterInCommentIsValid() throws ParseException
897 {
898 parse( "// ’ € Œ" );
899 }
900
901 public void testCp1252NotAsciiCharacterInMultiCommentIsValid() throws ParseException
902 {
903 parse( "/* ’ € Œ */" );
904 }
905
906 public void testExternFunctionDefinitionIsValid() throws ParseException
907 {
908 parse( "extern \"C\" void MyFunction() {}" );
909 }
910
911 public void testSemiColonMemberDeclarationIsValid() throws ParseException
912 {
913 parse( "class MyClass{ ; };" );
914 }
915
916 public void testTemplateClassDestructorCallOnPointer() throws ParseException
917 {
918 parse( "void MyFunction() { pData->~MyClass< MyType >(); }" );
919 }
920
921 public void testTemplateClassDestructorCallOnInstance() throws ParseException
922 {
923 parse( "void MyFunction() { data.~MyClass< MyType >(); }" );
924 }
925
926 public void testFunctionReturningPointerOnFunctionDeclaration() throws ParseException
927 {
928 parse( "void (*MyFunction());" );
929 }
930
931 public void testFunctionReturningPointerOnFunctionDefinition() throws ParseException
932 {
933 parse( "void (*MyFunction())() {}" );
934 }
935
936 public void testFunctionWithCommentedParameterName() throws ParseException
937 {
938 parse( "void MyFunction( int /*i*/ ) {}" );
939 }
940
941 public void testTMP() throws IOException, ParseException
942 {
943 final Parser parser = new Parser( new StringReader( "" ) );
944 load( parser, "TMP.h" );
945 load( parser, "TMP.cpp" );
946 }
947
948 private void load( final Parser parser, final String name ) throws IOException, ParseException
949 {
950 final URL resource = ParserTest.class.getClassLoader().getResource( name );
951 if( resource == null )
952 throw new IOException( "resource not found : " + name );
953 parser.ReInit( new BufferedReader( new FileReader( resource.getFile() ) ) );
954 parser.translation_unit();
955 }
956 }