xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/tools/generator2/testdata/tests.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Test file trying to cover as much of DWARF entry combinations as possible.
16 // Base for testing.
17 // As we are tracking types related to function calls, types of interest should
18 // be passed as arguments, returned by function or be part of structure
19 // dependency chain
20 
21 #include "sandboxed_api/tools/generator2/testdata/tests.h"
22 
23 namespace a {
24 namespace b {
25 
26 class ExampleClass {
27  private:
28   int a_;
29   int b_;
30 
31  public:
GetSum() const32   int GetSum() const { return a_ + b_; }
33 };
34 
35 }  // namespace b
36 }  // namespace a
37 
38 extern "C" {
39 
40 // Simple types
function_using_simple_types(unsigned char a1,signed char a2,unsigned short a3,signed short a4,unsigned int a5,signed int a6,unsigned long a7,signed long a8,unsigned long long a9,signed long long a10)41 bool function_using_simple_types(unsigned char a1,       // NOLINT
42                                  signed char a2,         // NOLINT
43                                  unsigned short a3,      // NOLINT
44                                  signed short a4,        // NOLINT
45                                  unsigned int a5,        // NOLINT
46                                  signed int a6,          // NOLINT
47                                  unsigned long a7,       // NOLINT
48                                  signed long a8,         // NOLINT
49                                  unsigned long long a9,  // NOLINT
50                                  signed long long a10    // NOLINT
51 ) {
52   return a1 ? true : false;
53 }
54 
function_using_simple_types_continued(float a1,double a2,long double a3)55 bool function_using_simple_types_continued(float a1, double a2,
56                                            long double a3) {
57   return a1 ? true : false;
58 }
59 
60 // Class usage
function_using_class(const a::b::ExampleClass * ptr_to_class)61 int function_using_class(const a::b::ExampleClass* ptr_to_class) {
62   return ptr_to_class->GetSum();
63 }
64 
65 // Typedef usage
66 typedef unsigned int uint;
67 typedef uint* uint_p;
68 typedef uint_p* uint_pp;
69 typedef char** char_pp;
70 typedef long int long_arr[8];  // NOLINT
71 typedef void (*function_p)(uint, uint_p, uint_pp);
72 typedef void (*function_p2)(void (*)(int, char), void*);
73 typedef int function_3(int a, int b);
74 
75 typedef union {
76   int a;
77   char b;
78 } union_1;
79 
80 typedef struct {
81   function_p a;
82   function_p2 b;
83   void (*c)(int, long);  // NOLINT
84   uint d;
85   uint_pp e;
86   struct struct_2* f;
87 } struct_t;
88 
89 // Using defined types so these end up in debug symbols
function_using_typedefs(uint_p a1,uint_pp a2,function_p a3,function_p2 * a4,struct_t * a5,char_pp a6,long_arr * a7,function_3 * a8)90 uint function_using_typedefs(uint_p a1, uint_pp a2, function_p a3,
91                              function_p2* a4, struct_t* a5, char_pp a6,
92                              long_arr* a7, function_3* a8) {
93   return 1337 + a5->d + a8(1, 3);
94 }
95 
function_using_union(union_1 * a1)96 int function_using_union(union_1* a1) { return a1->a; }
97 
98 // Pointer usage
function_using_pointers(int * a1,unsigned char * a2,unsigned char a3,const char * a4)99 unsigned char* function_using_pointers(int* a1, unsigned char* a2,
100                                        unsigned char a3, const char* a4) {
101   return a2;
102 }
103 
function_returning_pointer()104 uint* function_returning_pointer() { return reinterpret_cast<uint*>(0x1337); }
105 
function_returning_void(int * a)106 void function_returning_void(int* a) { *a = 1337; }
107 
108 // Structures
109 struct __attribute__((__packed__)) struct_1 {
110   uint a;
111   char b;
112   uint c;
113   char d;
114 };
115 
116 struct struct_2 {
117   uint a;
118   char b;
119   uint c;
120   char d;
121 };
122 
123 struct struct_3 {
124   uint partially_defined_struct_so_field_is_invisible;
125 };
126 
127 #define COEF_BITS_SIZE 16
128 struct struct_4 {
129   char a[4];
130   int b;
131   union {
132     uint a;
133     char* b;
134   } c;
135   struct {
136     uint a;
137     char* b;
138   } d;
139   function_p func_1;
140   // tests for const + ptr issues
141   const char* const* const_1;
142   const char** const_2;
143   char* const* const_3;
144   int (*coef_bits)[COEF_BITS_SIZE];
145 };
146 
function_using_structures(struct struct_1 * a1,struct struct_2 * a2,struct struct_3 * a3,struct struct_4 * a4)147 int function_using_structures(struct struct_1* a1, struct struct_2* a2,
148                               struct struct_3* a3, struct struct_4* a4) {
149   return a1->a + a2->a + a4->b;
150 }
151 
152 // Tests type loop case typedef -> struct -> fn_ptr -> typedef
153 struct struct_6_def;
154 typedef struct struct_6_def struct_6;
155 typedef struct_6* struct_6p;
156 typedef void (*function_p3)(struct_6p);
157 struct struct_6_def {
158   function_p3 fn;
159 };
160 
function_using_type_loop(struct_6p a1)161 void function_using_type_loop(struct_6p a1) { a1->fn(a1); }
162 
163 // Tests struct-in-struct case that fails if we generate forward declarations
164 // for every structure
165 struct struct_7_part_def {
166   int x;
167   int y;
168   void (*fn)(void);
169 };
170 typedef struct struct_7_part_def s7part;
171 
172 struct struct_7_def {
173   s7part part;
174   int x;
175 };
176 
177 typedef struct struct_7_def* s7p;
178 
function_using_incomplete(s7p a1)179 void function_using_incomplete(s7p a1) { a1->part.fn(); }
180 
181 // Tests for enums
182 enum Enumeration { ONE, TWO, THREE };
183 typedef enum Numbers { UNKNOWN, FIVE = 5, SE7EN = 7 } Nums;
184 typedef enum { SIX = 6, TEN = 10 } SixOrTen;
185 enum class Color : long long { RED, GREEN = 20, BLUE };  // NOLINT
186 enum struct Direction { LEFT = 'l', RIGHT = 'r' };
187 
function_using_enums(Enumeration a1,SixOrTen a2,Color a3,Direction a4,Nums a5)188 int function_using_enums(Enumeration a1, SixOrTen a2, Color a3, Direction a4,
189                          Nums a5) {
190   switch (a1) {
191     case Enumeration::ONE:
192       return Numbers::SE7EN;
193     case Enumeration::TWO:
194       return a2;
195     default:
196       return FIVE;
197   }
198 }
199 
200 }  // extern "C"
201