Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / testsuite / gcc.dg / torture / pr55888.c
1 /* { dg-do compile } */
2
3 typedef unsigned _GCC_ATTR_ALIGN_u32t;
4 typedef _GCC_ATTR_ALIGN_u32t _Uint32t __attribute__ ((__aligned__ (4)));
5 typedef unsigned int _GCC_ATTR_ALIGN_u8t __attribute__ ((__mode__ (__QI__)));
6 typedef _GCC_ATTR_ALIGN_u8t _Uint8t __attribute__ ((__aligned__ (1)));
7 typedef unsigned _Sizet;
8 typedef _Sizet size_t;
9 typedef _Uint8t uint8_t;
10 typedef _Uint32t uint32_t;
11 typedef enum
12 {
13   PROTOBUF_C_LABEL_REQUIRED, PROTOBUF_C_LABEL_OPTIONAL,
14     PROTOBUF_C_LABEL_REPEATED
15 }
16 ProtobufCLabel;
17 typedef enum
18 {
19   PROTOBUF_C_TYPE_INT32, PROTOBUF_C_TYPE_SINT32, PROTOBUF_C_TYPE_SFIXED32,
20     PROTOBUF_C_TYPE_INT64, PROTOBUF_C_TYPE_SINT64, PROTOBUF_C_TYPE_SFIXED64,
21     PROTOBUF_C_TYPE_UINT32, PROTOBUF_C_TYPE_FIXED32, PROTOBUF_C_TYPE_UINT64,
22     PROTOBUF_C_TYPE_FIXED64, PROTOBUF_C_TYPE_FLOAT, PROTOBUF_C_TYPE_DOUBLE,
23     PROTOBUF_C_TYPE_BOOL, PROTOBUF_C_TYPE_ENUM, PROTOBUF_C_TYPE_STRING,
24     PROTOBUF_C_TYPE_BYTES, PROTOBUF_C_TYPE_MESSAGE,
25 }
26 ProtobufCType;
27 typedef struct _ProtobufCBinaryData ProtobufCBinaryData;
28 struct _ProtobufCBinaryData
29 {
30   size_t len;
31 };
32 typedef struct _ProtobufCMessageDescriptor ProtobufCMessageDescriptor;
33 typedef struct _ProtobufCFieldDescriptor ProtobufCFieldDescriptor;
34 typedef struct _ProtobufCMessage ProtobufCMessage;
35 struct _ProtobufCFieldDescriptor
36 {
37   uint32_t id;
38   ProtobufCLabel label;
39   ProtobufCType type;
40   unsigned offset;
41 };
42 struct _ProtobufCMessageDescriptor
43 {
44   unsigned n_fields;
45   const ProtobufCFieldDescriptor *fields;
46 };
47 struct _ProtobufCMessage
48 {
49   const ProtobufCMessageDescriptor *descriptor;
50 };
51 typedef enum
52 {
53   PROTOBUF_C_WIRE_TYPE_VARINT, PROTOBUF_C_WIRE_TYPE_64BIT,
54     PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED, PROTOBUF_C_WIRE_TYPE_START_GROUP,
55     PROTOBUF_C_WIRE_TYPE_END_GROUP, PROTOBUF_C_WIRE_TYPE_32BIT
56 }
57 ProtobufCWireType;
58 static inline size_t
59 uint32_pack (uint32_t value, uint8_t * out)
60 {
61   unsigned rv = 0;
62   if (value >= 0x80)
63     {
64       if (value >= 0x80)
65         {
66           value >>= 7;
67         }
68     }
69   out[rv++] = value;
70 }
71
72 static inline size_t
73 binary_data_pack (const ProtobufCBinaryData * bd, uint8_t * out)
74 {
75   size_t len = bd->len;
76   size_t rv = uint32_pack (len, out);
77   return rv + len;
78 }
79
80 static size_t
81 required_field_pack (const ProtobufCFieldDescriptor * field,
82                      const void *member, uint8_t * out)
83 {
84   size_t rv = tag_pack (field->id, out);
85   switch (field->type)
86     {
87     case PROTOBUF_C_TYPE_BYTES:
88       {
89         const ProtobufCBinaryData *bd =
90           ((const ProtobufCBinaryData *) member);
91         out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
92         return rv + binary_data_pack (bd, out + rv);
93       }
94     case PROTOBUF_C_TYPE_MESSAGE:
95       {
96         out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
97         return rv +
98           prefixed_message_pack (*(ProtobufCMessage * const *) member,
99                                  out + rv);
100       }
101     }
102 }
103
104 size_t
105 protobuf_c_message_pack (const ProtobufCMessage * message, uint8_t * out)
106 {
107   unsigned i;
108   size_t rv = 0;
109   for (i = 0; i < message->descriptor->n_fields; i++)
110     {
111       const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
112       const void *member = ((const char *) message) + field->offset;
113       if (field->label == PROTOBUF_C_LABEL_REQUIRED)
114         rv += required_field_pack (field, member, out + rv);
115     }
116 }