Debian Codegraph @

gcc/testsuite/gdc.test/runnable_cxx/extra-files/cppb.cpp

raw
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <assert.h>
4 #include <exception>
5 #include <cstdarg>
6
7 #include <string>
8
9 #include "cppb.h"
10
11 /**************************************/
12
13 int foo(int i, int j, int k);
14
15 int foob(int i, int j, int k)
16 {
17 printf("i = %d\n", i);
18 printf("j = %d\n", j);
19 printf("k = %d\n", k);
20 assert(i == 1);
21 assert(j == 2);
22 assert(k == 3);
23
24 foo(i, j, k);
25
26 return 7;
27 }
28
29 /**************************************/
30
31 class D *dthis;
32
33 class D
34 {
35 public:
36 virtual int bar(int i, int j, int k)
37 {
38 printf("this = %p\n", this);
39 assert(this == dthis);
40 printf("D.bar: i = %d\n", i);
41 printf("D.bar: j = %d\n", j);
42 printf("D.bar: k = %d\n", k);
43 assert(i == 9);
44 assert(j == 10);
45 assert(k == 11);
46 return 8;
47 }
48 };
49
50
51 D* getD()
52 {
53 D *d = new D();
54 dthis = d;
55 return d;
56 }
57
58 /**************************************/
59
60 class E
61 {
62 public:
63 virtual int bar(int i, int j, int k);
64 };
65
66
67 int callE(E *e)
68 {
69 return e->bar(11,12,13);
70 }
71
72 /**************************************/
73
74 void foo4(char *p)
75 {
76 }
77
78 /**************************************/
79
80 struct foo5 { int i; int j; void *p; };
81
82 class bar5
83 {
84 public:
85 virtual foo5 getFoo(int i){
86 printf("This = %p\n", this);
87 foo5 f;
88 f.i = 1;
89 f.j = 2 + i;
90 f.p = (void*)this;
91 return f;
92 }
93 };
94
95 bar5* newBar()
96 {
97 bar5* b = new bar5();
98 printf("bar = %p\n", b);
99 return b;
100 }
101
102
103 /**************************************/
104
105 struct A11802;
106 struct B11802;
107
108 class C11802
109 {
110 public:
111 virtual void fun(A11802 *);
112 virtual void fun(B11802 *);
113 };
114
115 class D11802 : public C11802
116 {
117 public:
118 void fun(A11802 *);
119 void fun(B11802 *);
120 };
121
122 void test11802x(D11802 *c)
123 {
124 c->fun((A11802 *)0);
125 c->fun((B11802 *)0);
126 }
127
128 /**************************************/
129
130 typedef struct
131 {
132 int i;
133 double d;
134 } S6;
135
136 union S6_2
137 {
138 int i;
139 double d;
140 };
141
142 enum S6_3
143 {
144 A, B
145 };
146
147
148 S6 foo6(void)
149 {
150 S6 s;
151 s.i = 42;
152 s.d = 2.5;
153 return s;
154 }
155
156 S6_2 foo6_2(void)
157 {
158 S6_2 s;
159 s.i = 42;
160 return s;
161 }
162
163 S6_3 foo6_3(void)
164 {
165 S6_3 s = A;
166 return s;
167 }
168
169 extern "C" { int foosize6()
170 {
171 return sizeof(S6);
172 }
173 }
174
175 /**************************************/
176
177 typedef struct
178 {
179 int i;
180 long long d;
181 } S7;
182
183 extern "C" { int foo7()
184 {
185 return sizeof(S7);
186 }
187 }
188
189 /**************************************/
190
191 struct S13955a
192 {
193 float a;
194 double b;
195 };
196
197 struct S13955b
198 {
199 double a;
200 float b;
201 };
202
203 struct S13955c
204 {
205 float a;
206 float b;
207 };
208
209 struct S13955d
210 {
211 double a;
212 double b;
213 };
214
215 void check13955(S13955a a, S13955b b, S13955c c, S13955d d);
216
217 void func13955(S13955a a, S13955b b, S13955c c, S13955d d)
218 {
219 check13955(a, b, c, d);
220 }
221
222 /**************************************/
223
224 struct Struct10071
225 {
226 void *p;
227 long double r;
228 };
229
230 size_t offset10071()
231 {
232 Struct10071 s;
233 return (char *)&s.r - (char *)&s;
234 }
235
236 /**************************************/
237
238 void foo8(const char *p)
239 {
240 }
241
242 /**************************************/
243 // https://issues.dlang.org/show_bug.cgi?id=4059
244
245 struct elem9 { };
246 void foobar9(elem9*, elem9*) { }
247
248 /**************************************/
249 // https://issues.dlang.org/show_bug.cgi?id=5148
250
251 void foo10(const char*, const char*) { }
252 void foo10(const int, const int) { }
253 void foo10(const char, const char) { }
254 void foo10(bool, bool) { }
255
256 struct MyStructType { };
257 void foo10(const MyStructType s, const MyStructType t) { }
258
259 enum MyEnumType { onemember };
260 void foo10(const MyEnumType s, const MyEnumType t) { }
261
262 /**************************************/
263
264 namespace N11 { namespace M { void bar11() { } } }
265
266 namespace A11 { namespace B { namespace C { void bar() { } } } }
267
268 /**************************************/
269
270 void myvprintfx(const char* format, va_list);
271
272 void myvprintf(const char* format, va_list va)
273 {
274 myvprintfx(format, va);
275 }
276
277 /**************************************/
278
279 class C13161
280 {
281 public:
282 virtual void dummyfunc() {}
283 long long val_5;
284 unsigned val_9;
285 };
286
287 class Test : public C13161
288 {
289 public:
290 unsigned val_0;
291 long long val_1;
292 };
293
294 size_t getoffset13161()
295 {
296 Test s;
297 return (char *)&s.val_0 - (char *)&s;
298 }
299
300 class C13161a
301 {
302 public:
303 virtual void dummyfunc() {}
304 long double val_5;
305 unsigned val_9;
306 };
307
308 class Testa : public C13161a
309 {
310 public:
311 bool val_0;
312 };
313
314 size_t getoffset13161a()
315 {
316 Testa s;
317 return (char *)&s.val_0 - (char *)&s;
318 }
319
320 /****************************************************/
321
322 #if __linux__
323 #include <memory>
324 #include <vector>
325
326 #if __linux__
327 template struct std::allocator<int>;
328 template struct std::vector<int>;
329
330 void foo15()
331 {
332 std::allocator<int>* p;
333 p->deallocate(0, 0);
334 }
335
336 #endif
337
338 // _Z5foo14PSt6vectorIiSaIiEE
339 void foo14(std::vector<int, std::allocator<int> > *p) { }
340
341 void foo14a(std::basic_string<char> *p) { }
342 void foo14b(std::basic_string<int> *p) { }
343 void foo14c(std::basic_istream<char> *p) { }
344 void foo14d(std::basic_ostream<char> *p) { }
345 void foo14e(std::basic_iostream<char> *p) { }
346
347 void foo14f(std::char_traits<char>* x, std::basic_string<char> *p, std::basic_string<char> *q) { }
348
349 #endif
350
351 /**************************************/
352
353 struct S13956
354 {
355 };
356
357 void check13956(S13956 arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);
358
359 void func13956(S13956 arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)
360 {
361 check13956(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
362 }
363
364 /**************************************/
365
366 wchar_t f13289_cpp_wchar_t(wchar_t ch)
367 {
368 if (ch <= L'z' && ch >= L'a')
369 {
370 return ch - (L'a' - L'A');
371 }
372 else
373 {
374 return ch;
375 }
376 }
377 #if defined (_MSC_VER) //&& _MSC_VER <= 1800
378 // MSVC2013 doesn't support char16_t/char32_t
379 #else
380 #define TEST_UNICODE
381 #endif
382 #ifdef TEST_UNICODE
383 char16_t f13289_d_wchar(char16_t ch);
384 char32_t f13289_d_dchar(char32_t ch);
385 #endif
386 wchar_t f13289_d_wchar_t(wchar_t ch);
387
388 bool f13289_cpp_test()
389 {
390 if (!(f13289_d_wchar_t(L'e') == L'E')) return false;
391 if (!(f13289_d_wchar_t(L'F') == L'F')) return false;
392 #ifdef TEST_UNICODE
393 if (!(f13289_d_wchar(u'c') == u'C')) return false;
394 if (!(f13289_d_wchar(u'D') == u'D')) return false;
395 if (!(f13289_d_dchar(U'e') == U'E')) return false;
396 if (!(f13289_d_dchar(U'F') == U'F')) return false;
397 #endif
398 return true;
399 }
400
401 /******************************************/
402
403 long double testld(long double ld)
404 {
405 assert(ld == 5);
406 return ld + 1;
407 }
408
409 long double testldld(long double ld1, long double ld2)
410 {
411 assert(ld1 == 5);
412 return ld2 + 1;
413 }
414
415 long testl(long lng)
416 {
417 assert(lng == 5);
418 return lng + sizeof(long);
419 }
420
421 unsigned long testul(unsigned long ul)
422 {
423 assert(ul == 5);
424 return ul + sizeof(unsigned long);
425 }
426
427 /******************************************/
428
429 struct S13707
430 {
431 void* a;
432 void* b;
433 S13707(void *a, void* b)
434 {
435 this->a = a;
436 this->b = b;
437 }
438 };
439
440 S13707 func13707()
441 {
442 S13707 pt(NULL, NULL);
443 return pt;
444 }
445
446 /******************************************/
447
448 template <int x> struct S13932
449 {
450 int member;
451 };
452
453 void func13932(S13932<-1> s) {}
454
455 /******************************************/
456
457 namespace N13337 {
458 namespace M13337 {
459 struct S13337 { };
460 void foo13337(S13337 s) { }
461 }
462 }
463
464 /****************************************/
465 // https://issues.dlang.org/show_bug.cgi?id=14195
466
467 template <typename T>
468 struct Delegate1 {};
469
470 template <typename R1>
471 struct Delegate1 < R1() > {};
472
473 template <typename T1, typename T2>
474 struct Delegate2 {};
475
476 template < typename R1, typename T1, typename T2, typename R2, typename T3, typename T4 >
477 struct Delegate2<R1(T1, T2), R2(T3, T4)> {};
478
479 void test14195a(Delegate1<void()> func) {}
480
481 void test14195b(Delegate2<int(float, double), int(float, double)> func) {}
482
483 /******************************************/
484 // https://issues.dlang.org/show_bug.cgi?id=14200
485
486 void test14200a(int a) {};
487 void test14200b(float a, int b, double c) {};
488
489 /******************************************/
490 // https://issues.dlang.org/show_bug.cgi?id=14956
491
492 namespace std {
493 namespace N14956 {
494 struct S14956 { };
495 }
496 }
497
498 void test14956(std::N14956::S14956 s) { }
499
500 /******************************************/
501 // check order of overloads in vtable
502
503 class Statement;
504 class ErrorStatement;
505 class PeelStatement;
506 class ExpStatement;
507 class DtorExpStatement;
508
509 class Visitor
510 {
511 public:
512 virtual int visit(Statement*) { return 1; }
513 virtual int visit(ErrorStatement*) { return 2; }
514 virtual int visit(PeelStatement*) { return 3; }
515 };
516
517 class Visitor2 : public Visitor
518 {
519 public:
520 virtual int visit2(ExpStatement*) { return 4; }
521 virtual int visit2(DtorExpStatement*) { return 5; }
522 };
523
524 bool testVtableCpp(Visitor2* sv)
525 {
526 if (sv->visit((Statement*)0) != 1) return false;
527 if (sv->visit((ErrorStatement*)0) != 2) return false;
528 if (sv->visit((PeelStatement*)0) != 3) return false;
529 if (sv->visit2((ExpStatement*)0) != 4) return false;
530 if (sv->visit2((DtorExpStatement*)0) != 5) return false;
531 return true;
532 }
533
534 Visitor2 inst;
535
536 Visitor2* getVisitor2()
537 {
538 return &inst;
539 }
540
541 /******************************************/
542 // issues detected by fuzzer
543
544 void fuzz1_checkValues(int64_t arg10, int64_t arg11, bool arg12);
545 void fuzz1_cppvararg(int64_t arg10, int64_t arg11, bool arg12)
546 {
547 fuzz1_checkValues(arg10, arg11, arg12);
548 }
549
550 void fuzz2_checkValues(uint64_t arg10, uint64_t arg11, bool arg12);
551 void fuzz2_cppvararg(uint64_t arg10, uint64_t arg11, bool arg12)
552 {
553 fuzz2_checkValues(arg10, arg11, arg12);
554 }
555
556 #ifdef TEST_UNICODE
557 void fuzz3_checkValues(char16_t arg10, char32_t arg11, bool arg12);
558 void fuzz3_cppvararg(char16_t arg10, char32_t arg11, bool arg12)
559 {
560 fuzz3_checkValues(arg10, arg11, arg12);
561 }
562 #endif
563
564 /******************************************/
565
566 void throwit()
567 {
568 #if _WIN32
569 #else
570 std::exception se;
571 throw se;
572 #endif
573 }
574
575 /******************************************/
576
577 #if __linux__
578 #include <stdexcept>
579
580 void throwle()
581 {
582 std::logic_error le("test");
583 throw le;
584 }
585
586 #endif
587
588 /******************************************/
589 // https://issues.dlang.org/show_bug.cgi?id=15579
590
591 /******************************************/
592 // https://issues.dlang.org/show_bug.cgi?id=15579
593
594 class Base
595 {
596 public:
597 //virtual ~Base() {}
598 virtual void base();
599 unsigned char x;
600 };
601
602 class Interface
603 {
604 public:
605 virtual int MethodCPP() = 0;
606 virtual int MethodD() = 0;
607 };
608
609 class Derived : public Base, public Interface
610 {
611 public:
612 Derived();
613 short y;
614 int MethodCPP();
615 #if _WIN32 || _WIN64
616 int MethodD();
617 virtual int Method();
618 #else
619 int MethodD() { return 3; } // need def or vtbl[] is not generated
620 virtual int Method() { return 6; } // need def or vtbl[] is not generated
621 #endif
622 };
623
624 void Base::base() { }
625 int Derived::MethodCPP() {
626 printf("Derived::MethodCPP() this = %p, x = %d, y = %d\n", this, x, y);
627 assert(x == 4 || x == 7);
628 assert(y == 5 || y == 8);
629 return 30;
630 }
631 Derived::Derived() { }
632
633
634 Derived *cppfoo(Derived *d)
635 {
636 printf("cppfoo(): d = %p\n", d);
637 assert(d->x == 4);
638 assert(d->y == 5);
639 assert(d->MethodD() == 3);
640 assert(d->MethodCPP() == 30);
641 assert(d->Method() == 6);
642
643 d = new Derived();
644 d->x = 7;
645 d->y = 8;
646 assert(d->MethodD() == 3);
647 assert(d->MethodCPP() == 30);
648 assert(d->Method() == 6);
649 printf("d1 = %p\n", d);
650 return d;
651 }
652
653 Interface *cppfooi(Interface *i)
654 {
655 printf("cppfooi(): i = %p\n", i);
656 assert(i->MethodD() == 3);
657 assert(i->MethodCPP() == 30);
658
659 Derived *d = new Derived();
660 d->x = 7;
661 d->y = 8;
662 printf("d = %p, i = %p\n", d, (Interface *)d);
663 return d;
664 }
665
666 /******************************************/
667 // https://issues.dlang.org/show_bug.cgi?id=15610
668
669 class Base2
670 {
671 public:
672 int i;
673 virtual void baser() { }
674 };
675
676 class Interface2
677 {
678 public:
679 virtual void f() = 0;
680 };
681
682 class Derived2 : public Base2, public Interface2
683 {
684 public:
685 void f();
686 };
687
688 void Derived2::f()
689 {
690 printf("Derived2::f() this = %p i = %d\n", this, i);
691 assert(i == 3);
692 }
693
694 /******************************************/
695 // https://issues.dlang.org/show_bug.cgi?id=15455
696
697 struct X6
698 {
699 unsigned short a;
700 unsigned short b;
701 unsigned char c;
702 unsigned char d;
703 };
704
705 struct X8
706 {
707 unsigned short a;
708 X6 b;
709 };
710
711 void test15455b(X8 s)
712 {
713 assert(sizeof(X6) == 6);
714 assert(sizeof(X8) == 8);
715 assert(s.a == 1);
716 assert(s.b.a == 2);
717 assert(s.b.b == 3);
718 assert(s.b.c == 4);
719 assert(s.b.d == 5);
720 }
721
722 /******************************************/
723 // https://issues.dlang.org/show_bug.cgi?id=15372
724
725 template <typename T>
726 int foo15372(int value)
727 {
728 return value;
729 }
730
731 void test15372b()
732 {
733 int t = foo15372<int>(1);
734 }
735
736 /****************************************/
737 // https://issues.dlang.org/show_bug.cgi?id=15576
738
739 namespace ns15576
740 {
741 int global15576;
742
743 namespace ns
744 {
745 int n_global15576;
746 }
747 }
748
749 /****************************************/
750 // https://issues.dlang.org/show_bug.cgi?id=15802
751
752 template <typename T>
753 class Foo15802
754 {
755 public:
756 static int boo(int value)
757 {
758 return value;
759 }
760 };
761
762 void test15802b()
763 {
764 int t = Foo15802<int>::boo(1);
765 }
766
767
768 /****************************************/
769 // https://issues.dlang.org/show_bug.cgi?id=16536
770 // mangling mismatch on OSX
771
772 #if defined(__APPLE__)
773 uint64_t pass16536(uint64_t a)
774 {
775 return a;
776 }
777 #endif
778
779 /****************************************/
780 // https://issues.dlang.org/show_bug.cgi?id=15589
781 // extern(C++) virtual destructors are not put in vtbl[]
782
783 class A15589
784 {
785 public:
786 struct S
787 {
788 public:
789 int x;
790 };
791 virtual int foo();
792 virtual ~A15589();
793 S s1;
794 S s2;
795 };
796 class B15589 : public A15589
797 {
798 public:
799 virtual int bar();
800 virtual ~B15589();
801 S s3;
802 };
803
804 void test15589b(A15589 *p)
805 {
806 assert(p->foo() == 100);
807 assert(((B15589*)p)->bar() == 200);
808 p->~A15589();
809 }
810
811
812 /////////////////
813 void trace15589(int ch);
814
815 Cpp15589Base::~Cpp15589Base()
816 {
817 trace15589('b');
818 }
819
820 Cpp15589Derived::Cpp15589Derived()
821 {
822 b = 1;
823 }
824
825 Cpp15589Derived::~Cpp15589Derived()
826 {
827 trace15589('B');
828 }
829
830 Cpp15589BaseVirtual::Cpp15589BaseVirtual()
831 {
832 c = 2;
833 }
834
835 Cpp15589BaseVirtual::~Cpp15589BaseVirtual()
836 {
837 trace15589('v');
838 }
839
840 Cpp15589DerivedVirtual::Cpp15589DerivedVirtual()
841 {
842 d = 3;
843 }
844
845 Cpp15589DerivedVirtual::~Cpp15589DerivedVirtual()
846 {
847 trace15589('V');
848 }
849
850 Cpp15589IntroducingVirtual::Cpp15589IntroducingVirtual()
851 {
852 e = 4;
853 }
854
855 Cpp15589IntroducingVirtual::~Cpp15589IntroducingVirtual()
856 {
857 trace15589('I');
858 }
859
860 Cpp15589Struct::~Cpp15589Struct()
861 {
862 trace15589('s');
863 }
864
865 /****************************************/
866 // https://issues.dlang.org/show_bug.cgi?id=18928
867
868 struct Small18928
869 {
870 int x;
871 };
872
873 class CC18928
874 {
875 public:
876 virtual Small18928 getVirtual();
877 Small18928 getFinal();
878 static Small18928 getStatic();
879 };
880
881 Small18928 CC18928::getVirtual() { Small18928 s = {3}; return s; }
882 Small18928 CC18928::getFinal() { Small18928 s = {4}; return s; }
883 Small18928 CC18928::getStatic() { Small18928 s = {5}; return s; }
884
885 CC18928* newCC18928()
886 {
887 return new CC18928();
888 }
889
890 /****************************************/
891 // https://issues.dlang.org/show_bug.cgi?id=18966
892 Base18966::Base18966() { x = 10; }
893 Base18966::~Base18966() {}
894 void Base18966::vf()
895 {
896 x = 100;
897 }
898
899 A18966::A18966() : calledOverloads(/*zero-init*/), i(0) { foo(); }
900 void A18966::foo() { calledOverloads[i++] = 'A'; }
901
902 B18966::B18966() { foo(); }
903 void B18966::foo() { calledOverloads[i++] = 'B'; }
904
905 void callback18955(const std::string& s);
906
907 void test18955()
908 {
909 std::string s;
910 // TODO: on OSX and FreeBSD, std is mangled as std::__1
911 #if !__APPLE__ && !__FreeBSD__
912 callback18955(s);
913 #endif
914 }
915
916 void previewInFunction(const int& a, const std::string& b, const std::string& c);
917
918 void testPreviewIn()
919 {
920 std::string s = "Hello World";
921 previewInFunction(42, s, s);
922 }