Merge redhat.com:/garz/repo/sparse into redhat.com:/garz/repo/sparse.be
[smatch.git] / lib.h
blob5409e2c4dabca75bec80848ded2a3fdc92258d36
1 #ifndef LIB_H
2 #define LIB_H
3 /*
4 * Basic helper routine descriptions for 'sparse'.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 extern unsigned int hexval(unsigned int c);
14 struct position {
15 unsigned int type:6,
16 stream:10,
17 pos:14,
18 newline:1,
19 whitespace:1;
20 unsigned int line;
23 struct ident;
24 struct token;
25 struct symbol;
26 struct symbol_list;
27 struct statement;
28 struct statement_list;
29 struct expression;
30 struct expression_list;
32 struct token *skip_to(struct token *, int);
33 struct token *expect(struct token *, int, const char *);
34 extern void warn(struct position, const char *, ...);
35 extern void error(struct position, const char *, ...);
37 #define __DECLARE_ALLOCATOR(type, x) \
38 extern type *__alloc_##x(int); \
39 extern void show_##x##_alloc(void); \
40 extern void clear_##x##_alloc(void);
41 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
43 DECLARE_ALLOCATOR(ident);
44 DECLARE_ALLOCATOR(token);
45 DECLARE_ALLOCATOR(symbol);
46 DECLARE_ALLOCATOR(expression);
47 DECLARE_ALLOCATOR(statement);
48 DECLARE_ALLOCATOR(string);
49 DECLARE_ALLOCATOR(scope);
50 __DECLARE_ALLOCATOR(void, bytes);
52 #define LIST_NODE_NR (29)
54 struct ptr_list {
55 int nr;
56 struct ptr_list *prev;
57 struct ptr_list *next;
58 void *list[LIST_NODE_NR];
61 #define ITERATE_FIRST 1
62 #define ITERATE_LAST 2
63 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
64 extern void add_ptr_list(struct ptr_list **, void *);
65 extern void free_ptr_list(struct ptr_list **);
66 extern int ptr_list_size(struct ptr_list *);
67 extern char **handle_switch(char *arg, char **next);
68 extern void add_pre_buffer(const char *fmt, ...);
70 extern unsigned int pre_buffer_size;
71 extern unsigned char pre_buffer[8192];
72 extern int include_fd;
73 extern char *include;
74 extern int preprocess_only;
76 extern void create_builtin_stream(void);
78 #define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
79 #define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
80 #define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
82 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
84 add_ptr_list((struct ptr_list **)list, sym);
87 static inline void add_statement(struct statement_list **list, struct statement *stmt)
89 add_ptr_list((struct ptr_list **)list, stmt);
92 static inline void add_expression(struct expression_list **list, struct expression *expr)
94 add_ptr_list((struct ptr_list **)list, expr);
97 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
99 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
102 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
104 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
107 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
109 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
112 #define PREPARE_PTR_LIST(head, ptr) \
113 do { \
114 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
115 struct ptr_list *__list##ptr = __head##ptr; \
116 int __nr##ptr = 0; \
117 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
118 else ptr = NULL
120 #define NEXT_PTR_LIST(ptr) \
121 if (ptr) { \
122 if (++__nr##ptr < __list##ptr->nr) { \
123 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
124 } else { \
125 __list##ptr = __list##ptr->next; \
126 ptr = NULL; \
127 if (__list##ptr != __head##ptr) { \
128 __nr##ptr = 0; \
129 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
134 #define RESET_PTR_LIST(ptr) do { \
135 __nr##ptr = 0; \
136 __list##ptr = __head##ptr; \
137 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
138 } while (0)
141 #define FINISH_PTR_LIST(ptr) \
142 (void)(__nr##ptr); /* Sanity-check nesting */ \
143 } while (0)
145 #define FOR_EACH_PTR(head, ptr) do { \
146 struct ptr_list *__head = (struct ptr_list *) (head); \
147 struct ptr_list *__list = __head; \
148 int __flag = ITERATE_FIRST; \
149 if (__head) { \
150 do { int __i; \
151 for (__i = 0; __i < __list->nr; __i++) { \
152 if (__i == __list->nr-1 && __list->next == __head) \
153 __flag |= ITERATE_LAST; \
154 do { \
155 ptr = (__typeof__(ptr)) (__list->list[__i]); \
156 do {
158 #define END_FOR_EACH_PTR } while (0); \
159 } while (0); \
160 __flag = 0; \
162 } while ((__list = __list->next) != __head); \
164 } while (0)
166 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
167 struct ptr_list *__head = (struct ptr_list *) (head); \
168 struct ptr_list *__list = __head; \
169 int __flag = ITERATE_FIRST; \
170 if (__head) { \
171 do { int __i; \
172 __list = __list->prev; \
173 __i = __list->nr; \
174 while (--__i >= 0) { \
175 if (__i == 0 && __list == __head) \
176 __flag |= ITERATE_LAST; \
177 do { \
178 ptr = (__typeof__(ptr)) (__list->list[__i]); \
179 do {
181 #define END_FOR_EACH_PTR_REVERSE } while (0); \
182 } while (0); \
183 __flag = 0; \
185 } while (__list != __head); \
187 } while (0)
189 #define THIS_ADDRESS(x) \
190 ((__typeof__(&(x))) (__list->list + __i))
192 #endif