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