fix some warnings in optimized mode
[libfirm] / ir / libcore / lc_opts_enum.c
1 /**
2  * @file   lc_opts_enum.c
3  * @date   24.11.2005
4  * @author Sebastian Hack
5  *
6  * Copyright (C) 2005 Universitaet Karlsruhe
7  * Released under the GPL
8  *
9  * Enum callback and dump implementation.
10  */
11
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 #if defined(__FreeBSD__)
18 #include <stdlib.h>
19 #elif defined(_WIN32)
20 #include <malloc.h>
21 #else
22 #include <alloca.h>
23 #endif
24
25 #include "lc_opts_t.h"
26 #include "lc_opts_enum.h"
27
28 static const char *delim                        = " \t|,";
29
30 #define DECL_CB(N, op) \
31 int lc_opt_enum_ ## N ## _cb(LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, size_t len, ...) \
32 { \
33         lc_opt_enum_ ## N ## _var_t *var                                                = data; \
34         const lc_opt_enum_ ## N ## _items_t *items      = var->items; \
35  \
36         va_list args; \
37         char *s, *tmp; \
38         size_t begin, end; \
39         const char *arg; \
40         int res = 0; \
41  \
42         va_start(args, len); \
43         arg = va_arg(args, const char *); \
44         va_end(args); \
45         \
46         end     = strlen(arg); \
47         tmp = s = malloc((end + 1) * sizeof(arg[0])); \
48         strcpy(s, arg); \
49         s[end]  = '\0'; \
50         \
51         end = 0; \
52         while(arg[end] != '\0') { \
53                 unsigned int i; \
54                 \
55                 begin  = end + strspn(arg + end, delim); \
56                 end    = begin + strcspn(arg + begin, delim); \
57                 s      = tmp + begin; \
58                 s[end - begin] = '\0'; \
59                 \
60                 for(i = 0; items[i].name != NULL; ++i) { \
61                         if(strcmp(s, items[i].name) == 0) { \
62                                 *var->value op items[i].value; \
63                                 res = 1; \
64                         } \
65                 } \
66         } \
67         free(tmp); \
68         return res; \
69 } \
70
71 DECL_CB(int, =)
72 DECL_CB(mask, |=)
73 DECL_CB(ptr, =)
74 DECL_CB(const_ptr, =)
75 DECL_CB(func_ptr, =)
76
77 #define DECL_DUMP(T, N, cond) \
78 int lc_opt_enum_ ## N ## _dump(char *buf, size_t n, LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, LC_UNUSED(size_t len)) \
79 { \
80         lc_opt_enum_ ## N ## _var_t *var                                                = data; \
81         const lc_opt_enum_ ## N ## _items_t *items      = var->items; \
82         const char *prefix                                                              = "";                    \
83         TYPE(value) = *var->value; \
84         int i; \
85  \
86         for(i = 0; items[i].name != NULL; ++i) { \
87                 TYPE(item_value) = items[i].value; \
88                 if(cond) { \
89                         strncat(buf, prefix, n); \
90                         strncat(buf, items[i].name, n); \
91                         prefix = ", "; \
92                 } \
93         } \
94  \
95         return strlen(buf); \
96 } \
97
98
99 #define DECL_DUMP_VALS(T, N) \
100 int lc_opt_enum_ ## N ## _dump_vals(char *buf, size_t n, LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, LC_UNUSED(size_t len)) \
101 { \
102         lc_opt_enum_ ## N ## _var_t *var                                                = data; \
103         const lc_opt_enum_ ## N ## _items_t *items      = var->items; \
104         const char *prefix                                                              = "";                    \
105         int i; \
106  \
107         for(i = 0; items[i].name != NULL; ++i) { \
108                 strncat(buf, prefix, n); \
109                 strncat(buf, items[i].name, n); \
110                 prefix = ", "; \
111         } \
112  \
113         return strlen(buf); \
114 } \
115
116
117
118 #define TYPE(x) int x
119 DECL_DUMP(int, int, item_value == value)
120 DECL_DUMP_VALS(int, int)
121 #undef TYPE
122
123 #define TYPE(x) unsigned x
124 DECL_DUMP(unsigned, mask, (item_value & value) == item_value)
125 DECL_DUMP_VALS(unsigned, mask)
126 #undef TYPE
127
128 #define TYPE(x) void *x
129 DECL_DUMP(void *, ptr, item_value == value)
130 DECL_DUMP_VALS(void *, ptr)
131 #undef TYPE
132
133 #define TYPE(x) const void *x
134 DECL_DUMP(const void *, const_ptr, item_value == value)
135 DECL_DUMP_VALS(const void *, const_ptr)
136 #undef TYPE
137
138 #define TYPE(x) int (*x)(void)
139 DECL_DUMP(int (*)(void), func_ptr, item_value == value)
140 DECL_DUMP_VALS(int (*)(void), func_ptr)
141 #undef TYPE