aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/infcall-nested-structs.c
blob: f024ac61639ce6b4004ba8d1ae7d77dbeae28908 (plain)
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
/* This testcase is part of GDB, the GNU debugger.

   Copyright 2018 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* This file is used for testing GDBs ability to pass structures to, and
   return structures from, functions.  All of the structures in this test
   are special in that they are small structures containing only 1 or 2
   scalar fields, the fields can be inside nested structures, and there can
   be empty structures around too.

   This test was originally written for RiscV which has some special ABI
   rules for structures like these, however, there should be no harm in
   running these tests on other targets, though in many cases the
   structures will treated no differently to the structures already covered
   in the structs.exp test script.  */

#include <string.h>

/* Useful abreviations.  */
typedef char tc;
typedef short ts;
typedef int ti;
typedef long tl;
typedef long long tll;
typedef float tf;
typedef double td;
typedef long double tld;

#ifdef TEST_COMPLEX
typedef float _Complex tfc;
typedef double _Complex tdc;
typedef long double _Complex tldc;
#endif /* TEST_COMPLEX */

#define MAKE_CHECK_FUNCS(TYPE)					\
  int								\
  check_arg_ ## TYPE (struct TYPE arg)				\
  {								\
    return cmp_ ## TYPE (arg, ref_val_ ## TYPE);		\
  }								\
								\
  struct TYPE							\
  rtn_str_ ## TYPE (void)					\
  {								\
    return (ref_val_ ## TYPE);					\
  }

#define REF_VAL(NAME) struct NAME ref_val_ ## NAME
#define ES(NAME) struct { } NAME

#if defined tA && ! defined tB

/* Structures with a single field nested to various depths, along with
   some empty structures.  */
struct struct01 { ES(es1); struct { struct { tA a; } s1; } s2; };
struct struct02 { tA a; struct { struct { ES(es1); } s1; } s2; };
struct struct03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4;};
struct struct04 { ES(es1); ES(es2); tA a; ES(es3); };

int cmp_struct01 (struct struct01 a, struct struct01 b)
{ return a.s2.s1.a == b.s2.s1.a; }

int cmp_struct02 (struct struct02 a, struct struct02 b)
{ return a.a == b.a; }

int cmp_struct03 (struct struct03 a, struct struct03 b)
{ return a.s4.s3.a == b.s4.s3.a; }

int cmp_struct04 (struct struct04 a, struct struct04 b)
{ return a.a == b.a; }

REF_VAL(struct01) = { {}, { { 'a' } } };
REF_VAL(struct02) = { 'a', { { {} } } };
REF_VAL(struct03) = { { { {} } }, {}, { { 'a' } } };
REF_VAL(struct04) = { {}, {}, 'a', {} };

#elif defined tA && defined tB

/* These structures all have 2 fields, nested to various depths, along
   with some empty structures.  */
struct struct01 { struct { tA a; } s1; ES (e1); struct { struct { tB b; } s2;} s3;};
struct struct02 { struct { struct { tA a; } s1; ES(e1); } s2; struct { struct { tB b; } s3;} s4; ES(e2);};
struct struct03 { ES(e1); tA a; ES (e2); struct { struct { tB b; } s2;} s3;};
struct struct04 { ES(e1); ES (e2); struct { struct { struct { tA a; struct { ES(e3); } s1; tB b; } s2; } s3;} s4;};

int cmp_struct01 (struct struct01 a, struct struct01 b)
{ return a.s1.a == b.s1.a && a.s3.s2.b == b.s3.s2.b; }

int cmp_struct02 (struct struct02 a, struct struct02 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s4.s3.b == b.s4.s3.b; }

int cmp_struct03 (struct struct03 a, struct struct03 b)
{ return a.a == b.a && a.s3.s2.b == b.s3.s2.b; }

int cmp_struct04 (struct struct04 a, struct struct04 b)
{ return a.s4.s3.s2.a == b.s4.s3.s2.a && a.s4.s3.s2.b == b.s4.s3.s2.b; }

REF_VAL(struct01) = { { 'a' }, {}, { { '1' } } };
REF_VAL(struct02) = { { { 'a' }, {} }, { { '1' } }, {} };
REF_VAL(struct03) = { {}, 'a', {}, { { '1' } } };
REF_VAL(struct04) = { {}, {}, { { { 'a', {}, '1'} } } } ;

#else

#error "Incorrect configuration of tA and tB defines"

#endif

/* Create all of the functions GDB will call to check functionality.  */
MAKE_CHECK_FUNCS(struct01)
MAKE_CHECK_FUNCS(struct02)
MAKE_CHECK_FUNCS(struct03)
MAKE_CHECK_FUNCS(struct04)

#define CALL_LINE(NAME) val += check_arg_ ## NAME (rtn_str_ ## NAME ())

int
call_all ()
{
  int val;

  CALL_LINE(struct01);
  CALL_LINE(struct02);
  CALL_LINE(struct03);
  CALL_LINE(struct04);

  return (val != 4);
}

void
breakpt (void)
{
  /* Nothing.  */
}

int
main ()
{
  int res;

  res = call_all ();
  breakpt (); /* Break Here.  */
  return res;
}