xref: /illumos-gate/usr/src/tools/smatch/src/pre-process.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Do C preprocessing, based on a token list gathered by
3*1f5207b7SJohn Levon  * the tokenizer.
4*1f5207b7SJohn Levon  *
5*1f5207b7SJohn Levon  * This may not be the smartest preprocessor on the planet.
6*1f5207b7SJohn Levon  *
7*1f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
8*1f5207b7SJohn Levon  *               2003-2004 Linus Torvalds
9*1f5207b7SJohn Levon  *
10*1f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
11*1f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
12*1f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
13*1f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14*1f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
15*1f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
16*1f5207b7SJohn Levon  *
17*1f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
18*1f5207b7SJohn Levon  * all copies or substantial portions of the Software.
19*1f5207b7SJohn Levon  *
20*1f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21*1f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22*1f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23*1f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24*1f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25*1f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26*1f5207b7SJohn Levon  * THE SOFTWARE.
27*1f5207b7SJohn Levon  */
28*1f5207b7SJohn Levon #include <stdio.h>
29*1f5207b7SJohn Levon #include <stdlib.h>
30*1f5207b7SJohn Levon #include <stdarg.h>
31*1f5207b7SJohn Levon #include <stddef.h>
32*1f5207b7SJohn Levon #include <string.h>
33*1f5207b7SJohn Levon #include <ctype.h>
34*1f5207b7SJohn Levon #include <unistd.h>
35*1f5207b7SJohn Levon #include <fcntl.h>
36*1f5207b7SJohn Levon #include <limits.h>
37*1f5207b7SJohn Levon #include <time.h>
38*1f5207b7SJohn Levon #include <dirent.h>
39*1f5207b7SJohn Levon #include <sys/stat.h>
40*1f5207b7SJohn Levon 
41*1f5207b7SJohn Levon #include "lib.h"
42*1f5207b7SJohn Levon #include "allocate.h"
43*1f5207b7SJohn Levon #include "parse.h"
44*1f5207b7SJohn Levon #include "token.h"
45*1f5207b7SJohn Levon #include "symbol.h"
46*1f5207b7SJohn Levon #include "expression.h"
47*1f5207b7SJohn Levon #include "scope.h"
48*1f5207b7SJohn Levon 
49*1f5207b7SJohn Levon static struct ident_list *macros;	// only needed for -dD
50*1f5207b7SJohn Levon static int false_nesting = 0;
51*1f5207b7SJohn Levon static int counter_macro = 0;		// __COUNTER__ expansion
52*1f5207b7SJohn Levon 
53*1f5207b7SJohn Levon #define INCLUDEPATHS 300
54*1f5207b7SJohn Levon const char *includepath[INCLUDEPATHS+1] = {
55*1f5207b7SJohn Levon 	"",
56*1f5207b7SJohn Levon 	"/usr/include",
57*1f5207b7SJohn Levon 	"/usr/local/include",
58*1f5207b7SJohn Levon 	NULL
59*1f5207b7SJohn Levon };
60*1f5207b7SJohn Levon 
61*1f5207b7SJohn Levon static const char **quote_includepath = includepath;
62*1f5207b7SJohn Levon static const char **angle_includepath = includepath + 1;
63*1f5207b7SJohn Levon static const char **isys_includepath   = includepath + 1;
64*1f5207b7SJohn Levon static const char **sys_includepath   = includepath + 1;
65*1f5207b7SJohn Levon static const char **dirafter_includepath = includepath + 3;
66*1f5207b7SJohn Levon 
67*1f5207b7SJohn Levon #define dirty_stream(stream)				\
68*1f5207b7SJohn Levon 	do {						\
69*1f5207b7SJohn Levon 		if (!stream->dirty) {			\
70*1f5207b7SJohn Levon 			stream->dirty = 1;		\
71*1f5207b7SJohn Levon 			if (!stream->ifndef)		\
72*1f5207b7SJohn Levon 				stream->protect = NULL;	\
73*1f5207b7SJohn Levon 		}					\
74*1f5207b7SJohn Levon 	} while(0)
75*1f5207b7SJohn Levon 
76*1f5207b7SJohn Levon #define end_group(stream)					\
77*1f5207b7SJohn Levon 	do {							\
78*1f5207b7SJohn Levon 		if (stream->ifndef == stream->top_if) {		\
79*1f5207b7SJohn Levon 			stream->ifndef = NULL;			\
80*1f5207b7SJohn Levon 			if (!stream->dirty)			\
81*1f5207b7SJohn Levon 				stream->protect = NULL;		\
82*1f5207b7SJohn Levon 			else if (stream->protect)		\
83*1f5207b7SJohn Levon 				stream->dirty = 0;		\
84*1f5207b7SJohn Levon 		}						\
85*1f5207b7SJohn Levon 	} while(0)
86*1f5207b7SJohn Levon 
87*1f5207b7SJohn Levon #define nesting_error(stream)		\
88*1f5207b7SJohn Levon 	do {				\
89*1f5207b7SJohn Levon 		stream->dirty = 1;	\
90*1f5207b7SJohn Levon 		stream->ifndef = NULL;	\
91*1f5207b7SJohn Levon 		stream->protect = NULL;	\
92*1f5207b7SJohn Levon 	} while(0)
93*1f5207b7SJohn Levon 
94*1f5207b7SJohn Levon static struct token *alloc_token(struct position *pos)
95*1f5207b7SJohn Levon {
96*1f5207b7SJohn Levon 	struct token *token = __alloc_token(0);
97*1f5207b7SJohn Levon 
98*1f5207b7SJohn Levon 	token->pos.stream = pos->stream;
99*1f5207b7SJohn Levon 	token->pos.line = pos->line;
100*1f5207b7SJohn Levon 	token->pos.pos = pos->pos;
101*1f5207b7SJohn Levon 	token->pos.whitespace = 1;
102*1f5207b7SJohn Levon 	return token;
103*1f5207b7SJohn Levon }
104*1f5207b7SJohn Levon 
105*1f5207b7SJohn Levon /* Expand symbol 'sym' at '*list' */
106*1f5207b7SJohn Levon static int expand(struct token **, struct symbol *);
107*1f5207b7SJohn Levon 
108*1f5207b7SJohn Levon static void replace_with_string(struct token *token, const char *str)
109*1f5207b7SJohn Levon {
110*1f5207b7SJohn Levon 	int size = strlen(str) + 1;
111*1f5207b7SJohn Levon 	struct string *s = __alloc_string(size);
112*1f5207b7SJohn Levon 
113*1f5207b7SJohn Levon 	s->length = size;
114*1f5207b7SJohn Levon 	memcpy(s->data, str, size);
115*1f5207b7SJohn Levon 	token_type(token) = TOKEN_STRING;
116*1f5207b7SJohn Levon 	token->string = s;
117*1f5207b7SJohn Levon }
118*1f5207b7SJohn Levon 
119*1f5207b7SJohn Levon static void replace_with_integer(struct token *token, unsigned int val)
120*1f5207b7SJohn Levon {
121*1f5207b7SJohn Levon 	char *buf = __alloc_bytes(11);
122*1f5207b7SJohn Levon 	sprintf(buf, "%u", val);
123*1f5207b7SJohn Levon 	token_type(token) = TOKEN_NUMBER;
124*1f5207b7SJohn Levon 	token->number = buf;
125*1f5207b7SJohn Levon }
126*1f5207b7SJohn Levon 
127*1f5207b7SJohn Levon static struct symbol *lookup_macro(struct ident *ident)
128*1f5207b7SJohn Levon {
129*1f5207b7SJohn Levon 	struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
130*1f5207b7SJohn Levon 	if (sym && sym->namespace != NS_MACRO)
131*1f5207b7SJohn Levon 		sym = NULL;
132*1f5207b7SJohn Levon 	return sym;
133*1f5207b7SJohn Levon }
134*1f5207b7SJohn Levon 
135*1f5207b7SJohn Levon static int token_defined(struct token *token)
136*1f5207b7SJohn Levon {
137*1f5207b7SJohn Levon 	if (token_type(token) == TOKEN_IDENT) {
138*1f5207b7SJohn Levon 		struct symbol *sym = lookup_macro(token->ident);
139*1f5207b7SJohn Levon 		if (sym) {
140*1f5207b7SJohn Levon 			sym->used_in = file_scope;
141*1f5207b7SJohn Levon 			return 1;
142*1f5207b7SJohn Levon 		}
143*1f5207b7SJohn Levon 		return 0;
144*1f5207b7SJohn Levon 	}
145*1f5207b7SJohn Levon 
146*1f5207b7SJohn Levon 	sparse_error(token->pos, "expected preprocessor identifier");
147*1f5207b7SJohn Levon 	return 0;
148*1f5207b7SJohn Levon }
149*1f5207b7SJohn Levon 
150*1f5207b7SJohn Levon static void replace_with_defined(struct token *token)
151*1f5207b7SJohn Levon {
152*1f5207b7SJohn Levon 	static const char *string[] = { "0", "1" };
153*1f5207b7SJohn Levon 	int defined = token_defined(token);
154*1f5207b7SJohn Levon 
155*1f5207b7SJohn Levon 	token_type(token) = TOKEN_NUMBER;
156*1f5207b7SJohn Levon 	token->number = string[defined];
157*1f5207b7SJohn Levon }
158*1f5207b7SJohn Levon 
159*1f5207b7SJohn Levon static int expand_one_symbol(struct token **list)
160*1f5207b7SJohn Levon {
161*1f5207b7SJohn Levon 	struct token *token = *list;
162*1f5207b7SJohn Levon 	struct symbol *sym;
163*1f5207b7SJohn Levon 	static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
164*1f5207b7SJohn Levon 	static time_t t = 0;
165*1f5207b7SJohn Levon 
166*1f5207b7SJohn Levon 	if (token->pos.noexpand)
167*1f5207b7SJohn Levon 		return 1;
168*1f5207b7SJohn Levon 
169*1f5207b7SJohn Levon 	sym = lookup_macro(token->ident);
170*1f5207b7SJohn Levon 	if (sym) {
171*1f5207b7SJohn Levon 		store_macro_pos(token);
172*1f5207b7SJohn Levon 		sym->used_in = file_scope;
173*1f5207b7SJohn Levon 		return expand(list, sym);
174*1f5207b7SJohn Levon 	}
175*1f5207b7SJohn Levon 	if (token->ident == &__LINE___ident) {
176*1f5207b7SJohn Levon 		replace_with_integer(token, token->pos.line);
177*1f5207b7SJohn Levon 	} else if (token->ident == &__FILE___ident) {
178*1f5207b7SJohn Levon 		replace_with_string(token, stream_name(token->pos.stream));
179*1f5207b7SJohn Levon 	} else if (token->ident == &__DATE___ident) {
180*1f5207b7SJohn Levon 		if (!t)
181*1f5207b7SJohn Levon 			time(&t);
182*1f5207b7SJohn Levon 		strftime(buffer, 12, "%b %e %Y", localtime(&t));
183*1f5207b7SJohn Levon 		replace_with_string(token, buffer);
184*1f5207b7SJohn Levon 	} else if (token->ident == &__TIME___ident) {
185*1f5207b7SJohn Levon 		if (!t)
186*1f5207b7SJohn Levon 			time(&t);
187*1f5207b7SJohn Levon 		strftime(buffer, 9, "%T", localtime(&t));
188*1f5207b7SJohn Levon 		replace_with_string(token, buffer);
189*1f5207b7SJohn Levon 	} else if (token->ident == &__COUNTER___ident) {
190*1f5207b7SJohn Levon 		replace_with_integer(token, counter_macro++);
191*1f5207b7SJohn Levon 	}
192*1f5207b7SJohn Levon 	return 1;
193*1f5207b7SJohn Levon }
194*1f5207b7SJohn Levon 
195*1f5207b7SJohn Levon static inline struct token *scan_next(struct token **where)
196*1f5207b7SJohn Levon {
197*1f5207b7SJohn Levon 	struct token *token = *where;
198*1f5207b7SJohn Levon 	if (token_type(token) != TOKEN_UNTAINT)
199*1f5207b7SJohn Levon 		return token;
200*1f5207b7SJohn Levon 	do {
201*1f5207b7SJohn Levon 		token->ident->tainted = 0;
202*1f5207b7SJohn Levon 		token = token->next;
203*1f5207b7SJohn Levon 	} while (token_type(token) == TOKEN_UNTAINT);
204*1f5207b7SJohn Levon 	*where = token;
205*1f5207b7SJohn Levon 	return token;
206*1f5207b7SJohn Levon }
207*1f5207b7SJohn Levon 
208*1f5207b7SJohn Levon static void expand_list(struct token **list)
209*1f5207b7SJohn Levon {
210*1f5207b7SJohn Levon 	struct token *next;
211*1f5207b7SJohn Levon 	while (!eof_token(next = scan_next(list))) {
212*1f5207b7SJohn Levon 		if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
213*1f5207b7SJohn Levon 			list = &next->next;
214*1f5207b7SJohn Levon 	}
215*1f5207b7SJohn Levon }
216*1f5207b7SJohn Levon 
217*1f5207b7SJohn Levon static void preprocessor_line(struct stream *stream, struct token **line);
218*1f5207b7SJohn Levon 
219*1f5207b7SJohn Levon static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
220*1f5207b7SJohn Levon {
221*1f5207b7SJohn Levon 	struct stream *stream = input_streams + prev->pos.stream;
222*1f5207b7SJohn Levon 	struct token **p = &prev->next;
223*1f5207b7SJohn Levon 	struct token *next;
224*1f5207b7SJohn Levon 	int nesting = 0;
225*1f5207b7SJohn Levon 
226*1f5207b7SJohn Levon 	while (!eof_token(next = scan_next(p))) {
227*1f5207b7SJohn Levon 		if (next->pos.newline && match_op(next, '#')) {
228*1f5207b7SJohn Levon 			if (!next->pos.noexpand) {
229*1f5207b7SJohn Levon 				sparse_error(next->pos,
230*1f5207b7SJohn Levon 					     "directive in argument list");
231*1f5207b7SJohn Levon 				preprocessor_line(stream, p);
232*1f5207b7SJohn Levon 				__free_token(next);	/* Free the '#' token */
233*1f5207b7SJohn Levon 				continue;
234*1f5207b7SJohn Levon 			}
235*1f5207b7SJohn Levon 		}
236*1f5207b7SJohn Levon 		switch (token_type(next)) {
237*1f5207b7SJohn Levon 		case TOKEN_STREAMEND:
238*1f5207b7SJohn Levon 		case TOKEN_STREAMBEGIN:
239*1f5207b7SJohn Levon 			*p = &eof_token_entry;
240*1f5207b7SJohn Levon 			return next;
241*1f5207b7SJohn Levon 		case TOKEN_STRING:
242*1f5207b7SJohn Levon 		case TOKEN_WIDE_STRING:
243*1f5207b7SJohn Levon 			if (count > 1)
244*1f5207b7SJohn Levon 				next->string->immutable = 1;
245*1f5207b7SJohn Levon 			break;
246*1f5207b7SJohn Levon 		}
247*1f5207b7SJohn Levon 		if (false_nesting) {
248*1f5207b7SJohn Levon 			*p = next->next;
249*1f5207b7SJohn Levon 			__free_token(next);
250*1f5207b7SJohn Levon 			continue;
251*1f5207b7SJohn Levon 		}
252*1f5207b7SJohn Levon 		if (match_op(next, '(')) {
253*1f5207b7SJohn Levon 			nesting++;
254*1f5207b7SJohn Levon 		} else if (match_op(next, ')')) {
255*1f5207b7SJohn Levon 			if (!nesting--)
256*1f5207b7SJohn Levon 				break;
257*1f5207b7SJohn Levon 		} else if (match_op(next, ',') && !nesting && !vararg) {
258*1f5207b7SJohn Levon 			break;
259*1f5207b7SJohn Levon 		}
260*1f5207b7SJohn Levon 		next->pos.stream = pos->stream;
261*1f5207b7SJohn Levon 		next->pos.line = pos->line;
262*1f5207b7SJohn Levon 		next->pos.pos = pos->pos;
263*1f5207b7SJohn Levon 		p = &next->next;
264*1f5207b7SJohn Levon 	}
265*1f5207b7SJohn Levon 	*p = &eof_token_entry;
266*1f5207b7SJohn Levon 	return next;
267*1f5207b7SJohn Levon }
268*1f5207b7SJohn Levon 
269*1f5207b7SJohn Levon /*
270*1f5207b7SJohn Levon  * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
271*1f5207b7SJohn Levon  */
272*1f5207b7SJohn Levon 
273*1f5207b7SJohn Levon struct arg {
274*1f5207b7SJohn Levon 	struct token *arg;
275*1f5207b7SJohn Levon 	struct token *expanded;
276*1f5207b7SJohn Levon 	struct token *str;
277*1f5207b7SJohn Levon 	int n_normal;
278*1f5207b7SJohn Levon 	int n_quoted;
279*1f5207b7SJohn Levon 	int n_str;
280*1f5207b7SJohn Levon };
281*1f5207b7SJohn Levon 
282*1f5207b7SJohn Levon static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
283*1f5207b7SJohn Levon {
284*1f5207b7SJohn Levon 	int wanted = arglist->count.normal;
285*1f5207b7SJohn Levon 	struct token *next = NULL;
286*1f5207b7SJohn Levon 	int count = 0;
287*1f5207b7SJohn Levon 
288*1f5207b7SJohn Levon 	arglist = arglist->next;	/* skip counter */
289*1f5207b7SJohn Levon 
290*1f5207b7SJohn Levon 	if (!wanted) {
291*1f5207b7SJohn Levon 		next = collect_arg(start, 0, &what->pos, 0);
292*1f5207b7SJohn Levon 		if (eof_token(next))
293*1f5207b7SJohn Levon 			goto Eclosing;
294*1f5207b7SJohn Levon 		if (!eof_token(start->next) || !match_op(next, ')')) {
295*1f5207b7SJohn Levon 			count++;
296*1f5207b7SJohn Levon 			goto Emany;
297*1f5207b7SJohn Levon 		}
298*1f5207b7SJohn Levon 	} else {
299*1f5207b7SJohn Levon 		for (count = 0; count < wanted; count++) {
300*1f5207b7SJohn Levon 			struct argcount *p = &arglist->next->count;
301*1f5207b7SJohn Levon 			next = collect_arg(start, p->vararg, &what->pos, p->normal);
302*1f5207b7SJohn Levon 			if (eof_token(next))
303*1f5207b7SJohn Levon 				goto Eclosing;
304*1f5207b7SJohn Levon 			if (p->vararg && wanted == 1 && eof_token(start->next))
305*1f5207b7SJohn Levon 				break;
306*1f5207b7SJohn Levon 			arglist = arglist->next->next;
307*1f5207b7SJohn Levon 			args[count].arg = start->next;
308*1f5207b7SJohn Levon 			args[count].n_normal = p->normal;
309*1f5207b7SJohn Levon 			args[count].n_quoted = p->quoted;
310*1f5207b7SJohn Levon 			args[count].n_str = p->str;
311*1f5207b7SJohn Levon 			if (match_op(next, ')')) {
312*1f5207b7SJohn Levon 				count++;
313*1f5207b7SJohn Levon 				break;
314*1f5207b7SJohn Levon 			}
315*1f5207b7SJohn Levon 			start = next;
316*1f5207b7SJohn Levon 		}
317*1f5207b7SJohn Levon 		if (count == wanted && !match_op(next, ')'))
318*1f5207b7SJohn Levon 			goto Emany;
319*1f5207b7SJohn Levon 		if (count == wanted - 1) {
320*1f5207b7SJohn Levon 			struct argcount *p = &arglist->next->count;
321*1f5207b7SJohn Levon 			if (!p->vararg)
322*1f5207b7SJohn Levon 				goto Efew;
323*1f5207b7SJohn Levon 			args[count].arg = NULL;
324*1f5207b7SJohn Levon 			args[count].n_normal = p->normal;
325*1f5207b7SJohn Levon 			args[count].n_quoted = p->quoted;
326*1f5207b7SJohn Levon 			args[count].n_str = p->str;
327*1f5207b7SJohn Levon 		}
328*1f5207b7SJohn Levon 		if (count < wanted - 1)
329*1f5207b7SJohn Levon 			goto Efew;
330*1f5207b7SJohn Levon 	}
331*1f5207b7SJohn Levon 	what->next = next->next;
332*1f5207b7SJohn Levon 	return 1;
333*1f5207b7SJohn Levon 
334*1f5207b7SJohn Levon Efew:
335*1f5207b7SJohn Levon 	sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
336*1f5207b7SJohn Levon 		show_token(what), wanted, count);
337*1f5207b7SJohn Levon 	goto out;
338*1f5207b7SJohn Levon Emany:
339*1f5207b7SJohn Levon 	while (match_op(next, ',')) {
340*1f5207b7SJohn Levon 		next = collect_arg(next, 0, &what->pos, 0);
341*1f5207b7SJohn Levon 		count++;
342*1f5207b7SJohn Levon 	}
343*1f5207b7SJohn Levon 	if (eof_token(next))
344*1f5207b7SJohn Levon 		goto Eclosing;
345*1f5207b7SJohn Levon 	sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
346*1f5207b7SJohn Levon 		show_token(what), count, wanted);
347*1f5207b7SJohn Levon 	goto out;
348*1f5207b7SJohn Levon Eclosing:
349*1f5207b7SJohn Levon 	sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
350*1f5207b7SJohn Levon 		show_token(what));
351*1f5207b7SJohn Levon out:
352*1f5207b7SJohn Levon 	what->next = next->next;
353*1f5207b7SJohn Levon 	return 0;
354*1f5207b7SJohn Levon }
355*1f5207b7SJohn Levon 
356*1f5207b7SJohn Levon static struct token *dup_list(struct token *list)
357*1f5207b7SJohn Levon {
358*1f5207b7SJohn Levon 	struct token *res = NULL;
359*1f5207b7SJohn Levon 	struct token **p = &res;
360*1f5207b7SJohn Levon 
361*1f5207b7SJohn Levon 	while (!eof_token(list)) {
362*1f5207b7SJohn Levon 		struct token *newtok = __alloc_token(0);
363*1f5207b7SJohn Levon 		*newtok = *list;
364*1f5207b7SJohn Levon 		*p = newtok;
365*1f5207b7SJohn Levon 		p = &newtok->next;
366*1f5207b7SJohn Levon 		list = list->next;
367*1f5207b7SJohn Levon 	}
368*1f5207b7SJohn Levon 	return res;
369*1f5207b7SJohn Levon }
370*1f5207b7SJohn Levon 
371*1f5207b7SJohn Levon static const char *show_token_sequence(struct token *token, int quote)
372*1f5207b7SJohn Levon {
373*1f5207b7SJohn Levon 	static char buffer[MAX_STRING];
374*1f5207b7SJohn Levon 	char *ptr = buffer;
375*1f5207b7SJohn Levon 	int whitespace = 0;
376*1f5207b7SJohn Levon 
377*1f5207b7SJohn Levon 	if (!token && !quote)
378*1f5207b7SJohn Levon 		return "<none>";
379*1f5207b7SJohn Levon 	while (!eof_token(token)) {
380*1f5207b7SJohn Levon 		const char *val = quote ? quote_token(token) : show_token(token);
381*1f5207b7SJohn Levon 		int len = strlen(val);
382*1f5207b7SJohn Levon 
383*1f5207b7SJohn Levon 		if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
384*1f5207b7SJohn Levon 			sparse_error(token->pos, "too long token expansion");
385*1f5207b7SJohn Levon 			break;
386*1f5207b7SJohn Levon 		}
387*1f5207b7SJohn Levon 
388*1f5207b7SJohn Levon 		if (whitespace)
389*1f5207b7SJohn Levon 			*ptr++ = ' ';
390*1f5207b7SJohn Levon 		memcpy(ptr, val, len);
391*1f5207b7SJohn Levon 		ptr += len;
392*1f5207b7SJohn Levon 		token = token->next;
393*1f5207b7SJohn Levon 		whitespace = token->pos.whitespace;
394*1f5207b7SJohn Levon 	}
395*1f5207b7SJohn Levon 	*ptr = 0;
396*1f5207b7SJohn Levon 	return buffer;
397*1f5207b7SJohn Levon }
398*1f5207b7SJohn Levon 
399*1f5207b7SJohn Levon static struct token *stringify(struct token *arg)
400*1f5207b7SJohn Levon {
401*1f5207b7SJohn Levon 	const char *s = show_token_sequence(arg, 1);
402*1f5207b7SJohn Levon 	int size = strlen(s)+1;
403*1f5207b7SJohn Levon 	struct token *token = __alloc_token(0);
404*1f5207b7SJohn Levon 	struct string *string = __alloc_string(size);
405*1f5207b7SJohn Levon 
406*1f5207b7SJohn Levon 	memcpy(string->data, s, size);
407*1f5207b7SJohn Levon 	string->length = size;
408*1f5207b7SJohn Levon 	token->pos = arg->pos;
409*1f5207b7SJohn Levon 	token_type(token) = TOKEN_STRING;
410*1f5207b7SJohn Levon 	token->string = string;
411*1f5207b7SJohn Levon 	token->next = &eof_token_entry;
412*1f5207b7SJohn Levon 	return token;
413*1f5207b7SJohn Levon }
414*1f5207b7SJohn Levon 
415*1f5207b7SJohn Levon static void expand_arguments(int count, struct arg *args)
416*1f5207b7SJohn Levon {
417*1f5207b7SJohn Levon 	int i;
418*1f5207b7SJohn Levon 	for (i = 0; i < count; i++) {
419*1f5207b7SJohn Levon 		struct token *arg = args[i].arg;
420*1f5207b7SJohn Levon 		if (!arg)
421*1f5207b7SJohn Levon 			arg = &eof_token_entry;
422*1f5207b7SJohn Levon 		if (args[i].n_str)
423*1f5207b7SJohn Levon 			args[i].str = stringify(arg);
424*1f5207b7SJohn Levon 		if (args[i].n_normal) {
425*1f5207b7SJohn Levon 			if (!args[i].n_quoted) {
426*1f5207b7SJohn Levon 				args[i].expanded = arg;
427*1f5207b7SJohn Levon 				args[i].arg = NULL;
428*1f5207b7SJohn Levon 			} else if (eof_token(arg)) {
429*1f5207b7SJohn Levon 				args[i].expanded = arg;
430*1f5207b7SJohn Levon 			} else {
431*1f5207b7SJohn Levon 				args[i].expanded = dup_list(arg);
432*1f5207b7SJohn Levon 			}
433*1f5207b7SJohn Levon 			expand_list(&args[i].expanded);
434*1f5207b7SJohn Levon 		}
435*1f5207b7SJohn Levon 	}
436*1f5207b7SJohn Levon }
437*1f5207b7SJohn Levon 
438*1f5207b7SJohn Levon /*
439*1f5207b7SJohn Levon  * Possibly valid combinations:
440*1f5207b7SJohn Levon  *  - ident + ident -> ident
441*1f5207b7SJohn Levon  *  - ident + number -> ident unless number contains '.', '+' or '-'.
442*1f5207b7SJohn Levon  *  - 'L' + char constant -> wide char constant
443*1f5207b7SJohn Levon  *  - 'L' + string literal -> wide string literal
444*1f5207b7SJohn Levon  *  - number + number -> number
445*1f5207b7SJohn Levon  *  - number + ident -> number
446*1f5207b7SJohn Levon  *  - number + '.' -> number
447*1f5207b7SJohn Levon  *  - number + '+' or '-' -> number, if number used to end on [eEpP].
448*1f5207b7SJohn Levon  *  - '.' + number -> number, if number used to start with a digit.
449*1f5207b7SJohn Levon  *  - special + special -> either special or an error.
450*1f5207b7SJohn Levon  */
451*1f5207b7SJohn Levon static enum token_type combine(struct token *left, struct token *right, char *p)
452*1f5207b7SJohn Levon {
453*1f5207b7SJohn Levon 	int len;
454*1f5207b7SJohn Levon 	enum token_type t1 = token_type(left), t2 = token_type(right);
455*1f5207b7SJohn Levon 
456*1f5207b7SJohn Levon 	if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
457*1f5207b7SJohn Levon 		return TOKEN_ERROR;
458*1f5207b7SJohn Levon 
459*1f5207b7SJohn Levon 	if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
460*1f5207b7SJohn Levon 		if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
461*1f5207b7SJohn Levon 			return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
462*1f5207b7SJohn Levon 		if (t2 == TOKEN_STRING)
463*1f5207b7SJohn Levon 			return TOKEN_WIDE_STRING;
464*1f5207b7SJohn Levon 	}
465*1f5207b7SJohn Levon 
466*1f5207b7SJohn Levon 	if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
467*1f5207b7SJohn Levon 		return TOKEN_ERROR;
468*1f5207b7SJohn Levon 
469*1f5207b7SJohn Levon 	strcpy(p, show_token(left));
470*1f5207b7SJohn Levon 	strcat(p, show_token(right));
471*1f5207b7SJohn Levon 	len = strlen(p);
472*1f5207b7SJohn Levon 
473*1f5207b7SJohn Levon 	if (len >= 256)
474*1f5207b7SJohn Levon 		return TOKEN_ERROR;
475*1f5207b7SJohn Levon 
476*1f5207b7SJohn Levon 	if (t1 == TOKEN_IDENT) {
477*1f5207b7SJohn Levon 		if (t2 == TOKEN_SPECIAL)
478*1f5207b7SJohn Levon 			return TOKEN_ERROR;
479*1f5207b7SJohn Levon 		if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
480*1f5207b7SJohn Levon 			return TOKEN_ERROR;
481*1f5207b7SJohn Levon 		return TOKEN_IDENT;
482*1f5207b7SJohn Levon 	}
483*1f5207b7SJohn Levon 
484*1f5207b7SJohn Levon 	if (t1 == TOKEN_NUMBER) {
485*1f5207b7SJohn Levon 		if (t2 == TOKEN_SPECIAL) {
486*1f5207b7SJohn Levon 			switch (right->special) {
487*1f5207b7SJohn Levon 			case '.':
488*1f5207b7SJohn Levon 				break;
489*1f5207b7SJohn Levon 			case '+': case '-':
490*1f5207b7SJohn Levon 				if (strchr("eEpP", p[len - 2]))
491*1f5207b7SJohn Levon 					break;
492*1f5207b7SJohn Levon 			default:
493*1f5207b7SJohn Levon 				return TOKEN_ERROR;
494*1f5207b7SJohn Levon 			}
495*1f5207b7SJohn Levon 		}
496*1f5207b7SJohn Levon 		return TOKEN_NUMBER;
497*1f5207b7SJohn Levon 	}
498*1f5207b7SJohn Levon 
499*1f5207b7SJohn Levon 	if (p[0] == '.' && isdigit((unsigned char)p[1]))
500*1f5207b7SJohn Levon 		return TOKEN_NUMBER;
501*1f5207b7SJohn Levon 
502*1f5207b7SJohn Levon 	return TOKEN_SPECIAL;
503*1f5207b7SJohn Levon }
504*1f5207b7SJohn Levon 
505*1f5207b7SJohn Levon static int merge(struct token *left, struct token *right)
506*1f5207b7SJohn Levon {
507*1f5207b7SJohn Levon 	static char buffer[512];
508*1f5207b7SJohn Levon 	enum token_type res = combine(left, right, buffer);
509*1f5207b7SJohn Levon 	int n;
510*1f5207b7SJohn Levon 
511*1f5207b7SJohn Levon 	switch (res) {
512*1f5207b7SJohn Levon 	case TOKEN_IDENT:
513*1f5207b7SJohn Levon 		left->ident = built_in_ident(buffer);
514*1f5207b7SJohn Levon 		left->pos.noexpand = 0;
515*1f5207b7SJohn Levon 		return 1;
516*1f5207b7SJohn Levon 
517*1f5207b7SJohn Levon 	case TOKEN_NUMBER: {
518*1f5207b7SJohn Levon 		char *number = __alloc_bytes(strlen(buffer) + 1);
519*1f5207b7SJohn Levon 		memcpy(number, buffer, strlen(buffer) + 1);
520*1f5207b7SJohn Levon 		token_type(left) = TOKEN_NUMBER;	/* could be . + num */
521*1f5207b7SJohn Levon 		left->number = number;
522*1f5207b7SJohn Levon 		return 1;
523*1f5207b7SJohn Levon 	}
524*1f5207b7SJohn Levon 
525*1f5207b7SJohn Levon 	case TOKEN_SPECIAL:
526*1f5207b7SJohn Levon 		if (buffer[2] && buffer[3])
527*1f5207b7SJohn Levon 			break;
528*1f5207b7SJohn Levon 		for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
529*1f5207b7SJohn Levon 			if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
530*1f5207b7SJohn Levon 				left->special = n;
531*1f5207b7SJohn Levon 				return 1;
532*1f5207b7SJohn Levon 			}
533*1f5207b7SJohn Levon 		}
534*1f5207b7SJohn Levon 		break;
535*1f5207b7SJohn Levon 
536*1f5207b7SJohn Levon 	case TOKEN_WIDE_CHAR:
537*1f5207b7SJohn Levon 	case TOKEN_WIDE_STRING:
538*1f5207b7SJohn Levon 		token_type(left) = res;
539*1f5207b7SJohn Levon 		left->pos.noexpand = 0;
540*1f5207b7SJohn Levon 		left->string = right->string;
541*1f5207b7SJohn Levon 		return 1;
542*1f5207b7SJohn Levon 
543*1f5207b7SJohn Levon 	case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
544*1f5207b7SJohn Levon 		token_type(left) = res;
545*1f5207b7SJohn Levon 		left->pos.noexpand = 0;
546*1f5207b7SJohn Levon 		memcpy(left->embedded, right->embedded, 4);
547*1f5207b7SJohn Levon 		return 1;
548*1f5207b7SJohn Levon 
549*1f5207b7SJohn Levon 	default:
550*1f5207b7SJohn Levon 		;
551*1f5207b7SJohn Levon 	}
552*1f5207b7SJohn Levon 	sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
553*1f5207b7SJohn Levon 	return 0;
554*1f5207b7SJohn Levon }
555*1f5207b7SJohn Levon 
556*1f5207b7SJohn Levon static struct token *dup_token(struct token *token, struct position *streampos)
557*1f5207b7SJohn Levon {
558*1f5207b7SJohn Levon 	struct token *alloc = alloc_token(streampos);
559*1f5207b7SJohn Levon 	token_type(alloc) = token_type(token);
560*1f5207b7SJohn Levon 	alloc->pos.newline = token->pos.newline;
561*1f5207b7SJohn Levon 	alloc->pos.whitespace = token->pos.whitespace;
562*1f5207b7SJohn Levon 	alloc->number = token->number;
563*1f5207b7SJohn Levon 	alloc->pos.noexpand = token->pos.noexpand;
564*1f5207b7SJohn Levon 	return alloc;
565*1f5207b7SJohn Levon }
566*1f5207b7SJohn Levon 
567*1f5207b7SJohn Levon static struct token **copy(struct token **where, struct token *list, int *count)
568*1f5207b7SJohn Levon {
569*1f5207b7SJohn Levon 	int need_copy = --*count;
570*1f5207b7SJohn Levon 	while (!eof_token(list)) {
571*1f5207b7SJohn Levon 		struct token *token;
572*1f5207b7SJohn Levon 		if (need_copy)
573*1f5207b7SJohn Levon 			token = dup_token(list, &list->pos);
574*1f5207b7SJohn Levon 		else
575*1f5207b7SJohn Levon 			token = list;
576*1f5207b7SJohn Levon 		if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
577*1f5207b7SJohn Levon 			token->pos.noexpand = 1;
578*1f5207b7SJohn Levon 		*where = token;
579*1f5207b7SJohn Levon 		where = &token->next;
580*1f5207b7SJohn Levon 		list = list->next;
581*1f5207b7SJohn Levon 	}
582*1f5207b7SJohn Levon 	*where = &eof_token_entry;
583*1f5207b7SJohn Levon 	return where;
584*1f5207b7SJohn Levon }
585*1f5207b7SJohn Levon 
586*1f5207b7SJohn Levon static int handle_kludge(struct token **p, struct arg *args)
587*1f5207b7SJohn Levon {
588*1f5207b7SJohn Levon 	struct token *t = (*p)->next->next;
589*1f5207b7SJohn Levon 	while (1) {
590*1f5207b7SJohn Levon 		struct arg *v = &args[t->argnum];
591*1f5207b7SJohn Levon 		if (token_type(t->next) != TOKEN_CONCAT) {
592*1f5207b7SJohn Levon 			if (v->arg) {
593*1f5207b7SJohn Levon 				/* ignore the first ## */
594*1f5207b7SJohn Levon 				*p = (*p)->next;
595*1f5207b7SJohn Levon 				return 0;
596*1f5207b7SJohn Levon 			}
597*1f5207b7SJohn Levon 			/* skip the entire thing */
598*1f5207b7SJohn Levon 			*p = t;
599*1f5207b7SJohn Levon 			return 1;
600*1f5207b7SJohn Levon 		}
601*1f5207b7SJohn Levon 		if (v->arg && !eof_token(v->arg))
602*1f5207b7SJohn Levon 			return 0; /* no magic */
603*1f5207b7SJohn Levon 		t = t->next->next;
604*1f5207b7SJohn Levon 	}
605*1f5207b7SJohn Levon }
606*1f5207b7SJohn Levon 
607*1f5207b7SJohn Levon static struct token **substitute(struct token **list, struct token *body, struct arg *args)
608*1f5207b7SJohn Levon {
609*1f5207b7SJohn Levon 	struct position *base_pos = &(*list)->pos;
610*1f5207b7SJohn Levon 	int *count;
611*1f5207b7SJohn Levon 	enum {Normal, Placeholder, Concat} state = Normal;
612*1f5207b7SJohn Levon 
613*1f5207b7SJohn Levon 	for (; !eof_token(body); body = body->next) {
614*1f5207b7SJohn Levon 		struct token *added, *arg;
615*1f5207b7SJohn Levon 		struct token **tail;
616*1f5207b7SJohn Levon 		struct token *t;
617*1f5207b7SJohn Levon 
618*1f5207b7SJohn Levon 		switch (token_type(body)) {
619*1f5207b7SJohn Levon 		case TOKEN_GNU_KLUDGE:
620*1f5207b7SJohn Levon 			/*
621*1f5207b7SJohn Levon 			 * GNU kludge: if we had <comma>##<vararg>, behaviour
622*1f5207b7SJohn Levon 			 * depends on whether we had enough arguments to have
623*1f5207b7SJohn Levon 			 * a vararg.  If we did, ## is just ignored.  Otherwise
624*1f5207b7SJohn Levon 			 * both , and ## are ignored.  Worse, there can be
625*1f5207b7SJohn Levon 			 * an arbitrary number of ##<arg> in between; if all of
626*1f5207b7SJohn Levon 			 * those are empty, we act as if they hadn't been there,
627*1f5207b7SJohn Levon 			 * otherwise we act as if the kludge didn't exist.
628*1f5207b7SJohn Levon 			 */
629*1f5207b7SJohn Levon 			t = body;
630*1f5207b7SJohn Levon 			if (handle_kludge(&body, args)) {
631*1f5207b7SJohn Levon 				if (state == Concat)
632*1f5207b7SJohn Levon 					state = Normal;
633*1f5207b7SJohn Levon 				else
634*1f5207b7SJohn Levon 					state = Placeholder;
635*1f5207b7SJohn Levon 				continue;
636*1f5207b7SJohn Levon 			}
637*1f5207b7SJohn Levon 			added = dup_token(t, base_pos);
638*1f5207b7SJohn Levon 			token_type(added) = TOKEN_SPECIAL;
639*1f5207b7SJohn Levon 			tail = &added->next;
640*1f5207b7SJohn Levon 			break;
641*1f5207b7SJohn Levon 
642*1f5207b7SJohn Levon 		case TOKEN_STR_ARGUMENT:
643*1f5207b7SJohn Levon 			arg = args[body->argnum].str;
644*1f5207b7SJohn Levon 			count = &args[body->argnum].n_str;
645*1f5207b7SJohn Levon 			goto copy_arg;
646*1f5207b7SJohn Levon 
647*1f5207b7SJohn Levon 		case TOKEN_QUOTED_ARGUMENT:
648*1f5207b7SJohn Levon 			arg = args[body->argnum].arg;
649*1f5207b7SJohn Levon 			count = &args[body->argnum].n_quoted;
650*1f5207b7SJohn Levon 			if (!arg || eof_token(arg)) {
651*1f5207b7SJohn Levon 				if (state == Concat)
652*1f5207b7SJohn Levon 					state = Normal;
653*1f5207b7SJohn Levon 				else
654*1f5207b7SJohn Levon 					state = Placeholder;
655*1f5207b7SJohn Levon 				continue;
656*1f5207b7SJohn Levon 			}
657*1f5207b7SJohn Levon 			goto copy_arg;
658*1f5207b7SJohn Levon 
659*1f5207b7SJohn Levon 		case TOKEN_MACRO_ARGUMENT:
660*1f5207b7SJohn Levon 			arg = args[body->argnum].expanded;
661*1f5207b7SJohn Levon 			count = &args[body->argnum].n_normal;
662*1f5207b7SJohn Levon 			if (eof_token(arg)) {
663*1f5207b7SJohn Levon 				state = Normal;
664*1f5207b7SJohn Levon 				continue;
665*1f5207b7SJohn Levon 			}
666*1f5207b7SJohn Levon 		copy_arg:
667*1f5207b7SJohn Levon 			tail = copy(&added, arg, count);
668*1f5207b7SJohn Levon 			added->pos.newline = body->pos.newline;
669*1f5207b7SJohn Levon 			added->pos.whitespace = body->pos.whitespace;
670*1f5207b7SJohn Levon 			break;
671*1f5207b7SJohn Levon 
672*1f5207b7SJohn Levon 		case TOKEN_CONCAT:
673*1f5207b7SJohn Levon 			if (state == Placeholder)
674*1f5207b7SJohn Levon 				state = Normal;
675*1f5207b7SJohn Levon 			else
676*1f5207b7SJohn Levon 				state = Concat;
677*1f5207b7SJohn Levon 			continue;
678*1f5207b7SJohn Levon 
679*1f5207b7SJohn Levon 		case TOKEN_IDENT:
680*1f5207b7SJohn Levon 			added = dup_token(body, base_pos);
681*1f5207b7SJohn Levon 			if (added->ident->tainted)
682*1f5207b7SJohn Levon 				added->pos.noexpand = 1;
683*1f5207b7SJohn Levon 			tail = &added->next;
684*1f5207b7SJohn Levon 			break;
685*1f5207b7SJohn Levon 
686*1f5207b7SJohn Levon 		default:
687*1f5207b7SJohn Levon 			added = dup_token(body, base_pos);
688*1f5207b7SJohn Levon 			tail = &added->next;
689*1f5207b7SJohn Levon 			break;
690*1f5207b7SJohn Levon 		}
691*1f5207b7SJohn Levon 
692*1f5207b7SJohn Levon 		/*
693*1f5207b7SJohn Levon 		 * if we got to doing real concatenation, we already have
694*1f5207b7SJohn Levon 		 * added something into the list, so containing_token() is OK.
695*1f5207b7SJohn Levon 		 */
696*1f5207b7SJohn Levon 		if (state == Concat && merge(containing_token(list), added)) {
697*1f5207b7SJohn Levon 			*list = added->next;
698*1f5207b7SJohn Levon 			if (tail != &added->next)
699*1f5207b7SJohn Levon 				list = tail;
700*1f5207b7SJohn Levon 		} else {
701*1f5207b7SJohn Levon 			*list = added;
702*1f5207b7SJohn Levon 			list = tail;
703*1f5207b7SJohn Levon 		}
704*1f5207b7SJohn Levon 		state = Normal;
705*1f5207b7SJohn Levon 	}
706*1f5207b7SJohn Levon 	*list = &eof_token_entry;
707*1f5207b7SJohn Levon 	return list;
708*1f5207b7SJohn Levon }
709*1f5207b7SJohn Levon 
710*1f5207b7SJohn Levon static int expand(struct token **list, struct symbol *sym)
711*1f5207b7SJohn Levon {
712*1f5207b7SJohn Levon 	struct token *last;
713*1f5207b7SJohn Levon 	struct token *token = *list;
714*1f5207b7SJohn Levon 	struct ident *expanding = token->ident;
715*1f5207b7SJohn Levon 	struct token **tail;
716*1f5207b7SJohn Levon 	int nargs = sym->arglist ? sym->arglist->count.normal : 0;
717*1f5207b7SJohn Levon 	struct arg args[nargs];
718*1f5207b7SJohn Levon 
719*1f5207b7SJohn Levon 	if (expanding->tainted) {
720*1f5207b7SJohn Levon 		token->pos.noexpand = 1;
721*1f5207b7SJohn Levon 		return 1;
722*1f5207b7SJohn Levon 	}
723*1f5207b7SJohn Levon 
724*1f5207b7SJohn Levon 	if (sym->arglist) {
725*1f5207b7SJohn Levon 		if (!match_op(scan_next(&token->next), '('))
726*1f5207b7SJohn Levon 			return 1;
727*1f5207b7SJohn Levon 		if (!collect_arguments(token->next, sym->arglist, args, token))
728*1f5207b7SJohn Levon 			return 1;
729*1f5207b7SJohn Levon 		expand_arguments(nargs, args);
730*1f5207b7SJohn Levon 	}
731*1f5207b7SJohn Levon 
732*1f5207b7SJohn Levon 	expanding->tainted = 1;
733*1f5207b7SJohn Levon 
734*1f5207b7SJohn Levon 	last = token->next;
735*1f5207b7SJohn Levon 	tail = substitute(list, sym->expansion, args);
736*1f5207b7SJohn Levon 	/*
737*1f5207b7SJohn Levon 	 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
738*1f5207b7SJohn Levon 	 * We still can lose the newline flag if the sucker expands to nothing,
739*1f5207b7SJohn Levon 	 * but the price of dealing with that is probably too high (we'd need
740*1f5207b7SJohn Levon 	 * to collect the flags during scan_next())
741*1f5207b7SJohn Levon 	 */
742*1f5207b7SJohn Levon 	(*list)->pos.newline = token->pos.newline;
743*1f5207b7SJohn Levon 	(*list)->pos.whitespace = token->pos.whitespace;
744*1f5207b7SJohn Levon 	*tail = last;
745*1f5207b7SJohn Levon 
746*1f5207b7SJohn Levon 	return 0;
747*1f5207b7SJohn Levon }
748*1f5207b7SJohn Levon 
749*1f5207b7SJohn Levon static const char *token_name_sequence(struct token *token, int endop, struct token *start)
750*1f5207b7SJohn Levon {
751*1f5207b7SJohn Levon 	static char buffer[256];
752*1f5207b7SJohn Levon 	char *ptr = buffer;
753*1f5207b7SJohn Levon 
754*1f5207b7SJohn Levon 	while (!eof_token(token) && !match_op(token, endop)) {
755*1f5207b7SJohn Levon 		int len;
756*1f5207b7SJohn Levon 		const char *val = token->string->data;
757*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_STRING)
758*1f5207b7SJohn Levon 			val = show_token(token);
759*1f5207b7SJohn Levon 		len = strlen(val);
760*1f5207b7SJohn Levon 		memcpy(ptr, val, len);
761*1f5207b7SJohn Levon 		ptr += len;
762*1f5207b7SJohn Levon 		token = token->next;
763*1f5207b7SJohn Levon 	}
764*1f5207b7SJohn Levon 	*ptr = 0;
765*1f5207b7SJohn Levon 	if (endop && !match_op(token, endop))
766*1f5207b7SJohn Levon 		sparse_error(start->pos, "expected '>' at end of filename");
767*1f5207b7SJohn Levon 	return buffer;
768*1f5207b7SJohn Levon }
769*1f5207b7SJohn Levon 
770*1f5207b7SJohn Levon static int already_tokenized(const char *path)
771*1f5207b7SJohn Levon {
772*1f5207b7SJohn Levon 	int stream, next;
773*1f5207b7SJohn Levon 
774*1f5207b7SJohn Levon 	for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
775*1f5207b7SJohn Levon 		struct stream *s = input_streams + stream;
776*1f5207b7SJohn Levon 
777*1f5207b7SJohn Levon 		next = s->next_stream;
778*1f5207b7SJohn Levon 		if (s->once) {
779*1f5207b7SJohn Levon 			if (strcmp(path, s->name))
780*1f5207b7SJohn Levon 				continue;
781*1f5207b7SJohn Levon 			return 1;
782*1f5207b7SJohn Levon 		}
783*1f5207b7SJohn Levon 		if (s->constant != CONSTANT_FILE_YES)
784*1f5207b7SJohn Levon 			continue;
785*1f5207b7SJohn Levon 		if (strcmp(path, s->name))
786*1f5207b7SJohn Levon 			continue;
787*1f5207b7SJohn Levon 		if (s->protect && !lookup_macro(s->protect))
788*1f5207b7SJohn Levon 			continue;
789*1f5207b7SJohn Levon 		return 1;
790*1f5207b7SJohn Levon 	}
791*1f5207b7SJohn Levon 	return 0;
792*1f5207b7SJohn Levon }
793*1f5207b7SJohn Levon 
794*1f5207b7SJohn Levon /* Handle include of header files.
795*1f5207b7SJohn Levon  * The relevant options are made compatible with gcc. The only options that
796*1f5207b7SJohn Levon  * are not supported is -withprefix and friends.
797*1f5207b7SJohn Levon  *
798*1f5207b7SJohn Levon  * Three set of include paths are known:
799*1f5207b7SJohn Levon  * quote_includepath:	Path to search when using #include "file.h"
800*1f5207b7SJohn Levon  * angle_includepath:	Paths to search when using #include <file.h>
801*1f5207b7SJohn Levon  * isys_includepath:	Paths specified with -isystem, come before the
802*1f5207b7SJohn Levon  *			built-in system include paths. Gcc would suppress
803*1f5207b7SJohn Levon  *			warnings from system headers. Here we separate
804*1f5207b7SJohn Levon  *			them from the angle_ ones to keep search ordering.
805*1f5207b7SJohn Levon  *
806*1f5207b7SJohn Levon  * sys_includepath:	Built-in include paths.
807*1f5207b7SJohn Levon  * dirafter_includepath Paths added with -dirafter.
808*1f5207b7SJohn Levon  *
809*1f5207b7SJohn Levon  * The above is implemented as one array with pointers
810*1f5207b7SJohn Levon  *                         +--------------+
811*1f5207b7SJohn Levon  * quote_includepath --->  |              |
812*1f5207b7SJohn Levon  *                         +--------------+
813*1f5207b7SJohn Levon  *                         |              |
814*1f5207b7SJohn Levon  *                         +--------------+
815*1f5207b7SJohn Levon  * angle_includepath --->  |              |
816*1f5207b7SJohn Levon  *                         +--------------+
817*1f5207b7SJohn Levon  * isys_includepath  --->  |              |
818*1f5207b7SJohn Levon  *                         +--------------+
819*1f5207b7SJohn Levon  * sys_includepath   --->  |              |
820*1f5207b7SJohn Levon  *                         +--------------+
821*1f5207b7SJohn Levon  * dirafter_includepath -> |              |
822*1f5207b7SJohn Levon  *                         +--------------+
823*1f5207b7SJohn Levon  *
824*1f5207b7SJohn Levon  * -I dir insert dir just before isys_includepath and move the rest
825*1f5207b7SJohn Levon  * -I- makes all dirs specified with -I before to quote dirs only and
826*1f5207b7SJohn Levon  *   angle_includepath is set equal to isys_includepath.
827*1f5207b7SJohn Levon  * -nostdinc removes all sys dirs by storing NULL in entry pointed
828*1f5207b7SJohn Levon  *   to by * sys_includepath. Note that this will reset all dirs built-in
829*1f5207b7SJohn Levon  *   and added before -nostdinc by -isystem and -idirafter.
830*1f5207b7SJohn Levon  * -isystem dir adds dir where isys_includepath points adding this dir as
831*1f5207b7SJohn Levon  *   first systemdir
832*1f5207b7SJohn Levon  * -idirafter dir adds dir to the end of the list
833*1f5207b7SJohn Levon  */
834*1f5207b7SJohn Levon 
835*1f5207b7SJohn Levon static void set_stream_include_path(struct stream *stream)
836*1f5207b7SJohn Levon {
837*1f5207b7SJohn Levon 	const char *path = stream->path;
838*1f5207b7SJohn Levon 	if (!path) {
839*1f5207b7SJohn Levon 		const char *p = strrchr(stream->name, '/');
840*1f5207b7SJohn Levon 		path = "";
841*1f5207b7SJohn Levon 		if (p) {
842*1f5207b7SJohn Levon 			int len = p - stream->name + 1;
843*1f5207b7SJohn Levon 			char *m = malloc(len+1);
844*1f5207b7SJohn Levon 			/* This includes the final "/" */
845*1f5207b7SJohn Levon 			memcpy(m, stream->name, len);
846*1f5207b7SJohn Levon 			m[len] = 0;
847*1f5207b7SJohn Levon 			path = m;
848*1f5207b7SJohn Levon 		}
849*1f5207b7SJohn Levon 		stream->path = path;
850*1f5207b7SJohn Levon 	}
851*1f5207b7SJohn Levon 	includepath[0] = path;
852*1f5207b7SJohn Levon }
853*1f5207b7SJohn Levon 
854*1f5207b7SJohn Levon static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
855*1f5207b7SJohn Levon {
856*1f5207b7SJohn Levon 	int fd;
857*1f5207b7SJohn Levon 	int plen = strlen(path);
858*1f5207b7SJohn Levon 	static char fullname[PATH_MAX];
859*1f5207b7SJohn Levon 
860*1f5207b7SJohn Levon 	memcpy(fullname, path, plen);
861*1f5207b7SJohn Levon 	if (plen && path[plen-1] != '/') {
862*1f5207b7SJohn Levon 		fullname[plen] = '/';
863*1f5207b7SJohn Levon 		plen++;
864*1f5207b7SJohn Levon 	}
865*1f5207b7SJohn Levon 	memcpy(fullname+plen, filename, flen);
866*1f5207b7SJohn Levon 	if (already_tokenized(fullname))
867*1f5207b7SJohn Levon 		return 1;
868*1f5207b7SJohn Levon 	fd = open(fullname, O_RDONLY);
869*1f5207b7SJohn Levon 	if (fd >= 0) {
870*1f5207b7SJohn Levon 		char * streamname = __alloc_bytes(plen + flen);
871*1f5207b7SJohn Levon 		memcpy(streamname, fullname, plen + flen);
872*1f5207b7SJohn Levon 		*where = tokenize(streamname, fd, *where, next_path);
873*1f5207b7SJohn Levon 		close(fd);
874*1f5207b7SJohn Levon 		return 1;
875*1f5207b7SJohn Levon 	}
876*1f5207b7SJohn Levon 	return 0;
877*1f5207b7SJohn Levon }
878*1f5207b7SJohn Levon 
879*1f5207b7SJohn Levon static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
880*1f5207b7SJohn Levon {
881*1f5207b7SJohn Levon 	const char *path;
882*1f5207b7SJohn Levon 
883*1f5207b7SJohn Levon 	while ((path = *pptr++) != NULL) {
884*1f5207b7SJohn Levon 		if (!try_include(path, filename, flen, list, pptr))
885*1f5207b7SJohn Levon 			continue;
886*1f5207b7SJohn Levon 		return 1;
887*1f5207b7SJohn Levon 	}
888*1f5207b7SJohn Levon 	return 0;
889*1f5207b7SJohn Levon }
890*1f5207b7SJohn Levon 
891*1f5207b7SJohn Levon static int free_preprocessor_line(struct token *token)
892*1f5207b7SJohn Levon {
893*1f5207b7SJohn Levon 	while (token_type(token) != TOKEN_EOF) {
894*1f5207b7SJohn Levon 		struct token *free = token;
895*1f5207b7SJohn Levon 		token = token->next;
896*1f5207b7SJohn Levon 		__free_token(free);
897*1f5207b7SJohn Levon 	};
898*1f5207b7SJohn Levon 	return 1;
899*1f5207b7SJohn Levon }
900*1f5207b7SJohn Levon 
901*1f5207b7SJohn Levon const char *find_include(const char *skip, const char *look_for)
902*1f5207b7SJohn Levon {
903*1f5207b7SJohn Levon 	DIR *dp;
904*1f5207b7SJohn Levon 	struct dirent *entry;
905*1f5207b7SJohn Levon 	struct stat statbuf;
906*1f5207b7SJohn Levon 	const char *ret;
907*1f5207b7SJohn Levon 	char cwd[PATH_MAX];
908*1f5207b7SJohn Levon 	static char buf[PATH_MAX + 1];
909*1f5207b7SJohn Levon 
910*1f5207b7SJohn Levon 	dp = opendir(".");
911*1f5207b7SJohn Levon 	if (!dp)
912*1f5207b7SJohn Levon 		return NULL;
913*1f5207b7SJohn Levon 
914*1f5207b7SJohn Levon 	if (!getcwd(cwd, sizeof(cwd)))
915*1f5207b7SJohn Levon 		return NULL;
916*1f5207b7SJohn Levon 
917*1f5207b7SJohn Levon 	while ((entry = readdir(dp))) {
918*1f5207b7SJohn Levon 		lstat(entry->d_name, &statbuf);
919*1f5207b7SJohn Levon 
920*1f5207b7SJohn Levon 		if (strcmp(entry->d_name, look_for) == 0) {
921*1f5207b7SJohn Levon 			snprintf(buf, sizeof(buf), "%s/%s", cwd, entry->d_name);
922*1f5207b7SJohn Levon 			return buf;
923*1f5207b7SJohn Levon 		}
924*1f5207b7SJohn Levon 
925*1f5207b7SJohn Levon 		if (S_ISDIR(statbuf.st_mode)) {
926*1f5207b7SJohn Levon 			/* Found a directory, but ignore . and .. */
927*1f5207b7SJohn Levon 			if (strcmp(".", entry->d_name) == 0 ||
928*1f5207b7SJohn Levon 			    strcmp("..", entry->d_name) == 0 ||
929*1f5207b7SJohn Levon 			    strcmp(skip, entry->d_name) == 0)
930*1f5207b7SJohn Levon 				continue;
931*1f5207b7SJohn Levon 
932*1f5207b7SJohn Levon 			chdir(entry->d_name);
933*1f5207b7SJohn Levon 			ret = find_include("", look_for);
934*1f5207b7SJohn Levon 			chdir("..");
935*1f5207b7SJohn Levon 			if (ret)
936*1f5207b7SJohn Levon 				return ret;
937*1f5207b7SJohn Levon 		}
938*1f5207b7SJohn Levon 	}
939*1f5207b7SJohn Levon 	closedir(dp);
940*1f5207b7SJohn Levon 
941*1f5207b7SJohn Levon 	return NULL;
942*1f5207b7SJohn Levon }
943*1f5207b7SJohn Levon 
944*1f5207b7SJohn Levon const char *search_dir(const char *stop, const char *look_for)
945*1f5207b7SJohn Levon {
946*1f5207b7SJohn Levon 	char cwd[PATH_MAX];
947*1f5207b7SJohn Levon 	int len;
948*1f5207b7SJohn Levon 	const char *ret;
949*1f5207b7SJohn Levon 	int cnt = 0;
950*1f5207b7SJohn Levon 
951*1f5207b7SJohn Levon 	if (!getcwd(cwd, sizeof(cwd)))
952*1f5207b7SJohn Levon 		return NULL;
953*1f5207b7SJohn Levon 
954*1f5207b7SJohn Levon 	len = strlen(cwd);
955*1f5207b7SJohn Levon 	while (len >= 0) {
956*1f5207b7SJohn Levon 		ret = find_include(cnt++ ? cwd + len + 1 : "", look_for);
957*1f5207b7SJohn Levon 		if (ret)
958*1f5207b7SJohn Levon 			return ret;
959*1f5207b7SJohn Levon 
960*1f5207b7SJohn Levon 		if (strcmp(cwd, stop) == 0 ||
961*1f5207b7SJohn Levon 		    strcmp(cwd, "/usr/include") == 0 ||
962*1f5207b7SJohn Levon 		    strcmp(cwd, "/usr/local/include") == 0 ||
963*1f5207b7SJohn Levon 		    strlen(cwd) <= 10 ||  /* heck...  don't search /usr/lib/ */
964*1f5207b7SJohn Levon 		    strcmp(cwd, "/") == 0)
965*1f5207b7SJohn Levon 			return NULL;
966*1f5207b7SJohn Levon 
967*1f5207b7SJohn Levon 		while (--len >= 0) {
968*1f5207b7SJohn Levon 			if (cwd[len] == '/') {
969*1f5207b7SJohn Levon 				cwd[len] = '\0';
970*1f5207b7SJohn Levon 				break;
971*1f5207b7SJohn Levon 			}
972*1f5207b7SJohn Levon 		}
973*1f5207b7SJohn Levon 
974*1f5207b7SJohn Levon 		chdir("..");
975*1f5207b7SJohn Levon 	}
976*1f5207b7SJohn Levon 	return NULL;
977*1f5207b7SJohn Levon }
978*1f5207b7SJohn Levon 
979*1f5207b7SJohn Levon static void use_best_guess_header_file(struct token *token, const char *filename, struct token **list)
980*1f5207b7SJohn Levon {
981*1f5207b7SJohn Levon 	char cwd[PATH_MAX];
982*1f5207b7SJohn Levon 	char dir_part[PATH_MAX];
983*1f5207b7SJohn Levon 	const char *file_part;
984*1f5207b7SJohn Levon 	const char *include_name;
985*1f5207b7SJohn Levon 	int len;
986*1f5207b7SJohn Levon 
987*1f5207b7SJohn Levon 	if (!filename || filename[0] == '\0')
988*1f5207b7SJohn Levon 		return;
989*1f5207b7SJohn Levon 
990*1f5207b7SJohn Levon 	file_part = filename;
991*1f5207b7SJohn Levon 	while ((filename = strchr(filename, '/'))) {
992*1f5207b7SJohn Levon 		++filename;
993*1f5207b7SJohn Levon 		if (filename[0])
994*1f5207b7SJohn Levon 			file_part = filename;
995*1f5207b7SJohn Levon 	}
996*1f5207b7SJohn Levon 
997*1f5207b7SJohn Levon 	snprintf(dir_part, sizeof(dir_part), "%s", stream_name(token->pos.stream));
998*1f5207b7SJohn Levon 	len = strlen(dir_part);
999*1f5207b7SJohn Levon 	while (--len >= 0) {
1000*1f5207b7SJohn Levon 		if (dir_part[len] == '/') {
1001*1f5207b7SJohn Levon 			dir_part[len] = '\0';
1002*1f5207b7SJohn Levon 			break;
1003*1f5207b7SJohn Levon 		}
1004*1f5207b7SJohn Levon 	}
1005*1f5207b7SJohn Levon 	if (len < 0)
1006*1f5207b7SJohn Levon 		sprintf(dir_part, ".");
1007*1f5207b7SJohn Levon 
1008*1f5207b7SJohn Levon 	if (!getcwd(cwd, sizeof(cwd)))
1009*1f5207b7SJohn Levon 		return;
1010*1f5207b7SJohn Levon 
1011*1f5207b7SJohn Levon 	chdir(dir_part);
1012*1f5207b7SJohn Levon 	include_name = search_dir(cwd, file_part);
1013*1f5207b7SJohn Levon 	chdir(cwd);
1014*1f5207b7SJohn Levon 	if (!include_name)
1015*1f5207b7SJohn Levon 		return;
1016*1f5207b7SJohn Levon 	sparse_error(token->pos, "using '%s'", include_name);
1017*1f5207b7SJohn Levon 
1018*1f5207b7SJohn Levon 	try_include("", include_name, strlen(include_name), list, includepath);
1019*1f5207b7SJohn Levon }
1020*1f5207b7SJohn Levon 
1021*1f5207b7SJohn Levon static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
1022*1f5207b7SJohn Levon {
1023*1f5207b7SJohn Levon 	const char *filename;
1024*1f5207b7SJohn Levon 	struct token *next;
1025*1f5207b7SJohn Levon 	const char **path;
1026*1f5207b7SJohn Levon 	int expect;
1027*1f5207b7SJohn Levon 	int flen;
1028*1f5207b7SJohn Levon 
1029*1f5207b7SJohn Levon 	next = token->next;
1030*1f5207b7SJohn Levon 	expect = '>';
1031*1f5207b7SJohn Levon 	if (!match_op(next, '<')) {
1032*1f5207b7SJohn Levon 		expand_list(&token->next);
1033*1f5207b7SJohn Levon 		expect = 0;
1034*1f5207b7SJohn Levon 		next = token;
1035*1f5207b7SJohn Levon 		if (match_op(token->next, '<')) {
1036*1f5207b7SJohn Levon 			next = token->next;
1037*1f5207b7SJohn Levon 			expect = '>';
1038*1f5207b7SJohn Levon 		}
1039*1f5207b7SJohn Levon 	}
1040*1f5207b7SJohn Levon 
1041*1f5207b7SJohn Levon 	token = next->next;
1042*1f5207b7SJohn Levon 	filename = token_name_sequence(token, expect, token);
1043*1f5207b7SJohn Levon 	flen = strlen(filename) + 1;
1044*1f5207b7SJohn Levon 
1045*1f5207b7SJohn Levon 	/* Absolute path? */
1046*1f5207b7SJohn Levon 	if (filename[0] == '/') {
1047*1f5207b7SJohn Levon 		if (try_include("", filename, flen, list, includepath))
1048*1f5207b7SJohn Levon 			return 0;
1049*1f5207b7SJohn Levon 		goto out;
1050*1f5207b7SJohn Levon 	}
1051*1f5207b7SJohn Levon 
1052*1f5207b7SJohn Levon 	switch (how) {
1053*1f5207b7SJohn Levon 	case 1:
1054*1f5207b7SJohn Levon 		path = stream->next_path;
1055*1f5207b7SJohn Levon 		break;
1056*1f5207b7SJohn Levon 	case 2:
1057*1f5207b7SJohn Levon 		includepath[0] = "";
1058*1f5207b7SJohn Levon 		path = includepath;
1059*1f5207b7SJohn Levon 		break;
1060*1f5207b7SJohn Levon 	default:
1061*1f5207b7SJohn Levon 		/* Dir of input file is first dir to search for quoted includes */
1062*1f5207b7SJohn Levon 		set_stream_include_path(stream);
1063*1f5207b7SJohn Levon 		path = expect ? angle_includepath : quote_includepath;
1064*1f5207b7SJohn Levon 		break;
1065*1f5207b7SJohn Levon 	}
1066*1f5207b7SJohn Levon 	/* Check the standard include paths.. */
1067*1f5207b7SJohn Levon 	if (do_include_path(path, list, token, filename, flen))
1068*1f5207b7SJohn Levon 		return 0;
1069*1f5207b7SJohn Levon out:
1070*1f5207b7SJohn Levon 	sparse_error(token->pos, "unable to open '%s'", filename);
1071*1f5207b7SJohn Levon 	use_best_guess_header_file(token, filename, list);
1072*1f5207b7SJohn Levon 	return 0;
1073*1f5207b7SJohn Levon }
1074*1f5207b7SJohn Levon 
1075*1f5207b7SJohn Levon static int handle_include(struct stream *stream, struct token **list, struct token *token)
1076*1f5207b7SJohn Levon {
1077*1f5207b7SJohn Levon 	return handle_include_path(stream, list, token, 0);
1078*1f5207b7SJohn Levon }
1079*1f5207b7SJohn Levon 
1080*1f5207b7SJohn Levon static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
1081*1f5207b7SJohn Levon {
1082*1f5207b7SJohn Levon 	return handle_include_path(stream, list, token, 1);
1083*1f5207b7SJohn Levon }
1084*1f5207b7SJohn Levon 
1085*1f5207b7SJohn Levon static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
1086*1f5207b7SJohn Levon {
1087*1f5207b7SJohn Levon 	return handle_include_path(stream, list, token, 2);
1088*1f5207b7SJohn Levon }
1089*1f5207b7SJohn Levon 
1090*1f5207b7SJohn Levon static int token_different(struct token *t1, struct token *t2)
1091*1f5207b7SJohn Levon {
1092*1f5207b7SJohn Levon 	int different;
1093*1f5207b7SJohn Levon 
1094*1f5207b7SJohn Levon 	if (token_type(t1) != token_type(t2))
1095*1f5207b7SJohn Levon 		return 1;
1096*1f5207b7SJohn Levon 
1097*1f5207b7SJohn Levon 	switch (token_type(t1)) {
1098*1f5207b7SJohn Levon 	case TOKEN_IDENT:
1099*1f5207b7SJohn Levon 		different = t1->ident != t2->ident;
1100*1f5207b7SJohn Levon 		break;
1101*1f5207b7SJohn Levon 	case TOKEN_ARG_COUNT:
1102*1f5207b7SJohn Levon 	case TOKEN_UNTAINT:
1103*1f5207b7SJohn Levon 	case TOKEN_CONCAT:
1104*1f5207b7SJohn Levon 	case TOKEN_GNU_KLUDGE:
1105*1f5207b7SJohn Levon 		different = 0;
1106*1f5207b7SJohn Levon 		break;
1107*1f5207b7SJohn Levon 	case TOKEN_NUMBER:
1108*1f5207b7SJohn Levon 		different = strcmp(t1->number, t2->number);
1109*1f5207b7SJohn Levon 		break;
1110*1f5207b7SJohn Levon 	case TOKEN_SPECIAL:
1111*1f5207b7SJohn Levon 		different = t1->special != t2->special;
1112*1f5207b7SJohn Levon 		break;
1113*1f5207b7SJohn Levon 	case TOKEN_MACRO_ARGUMENT:
1114*1f5207b7SJohn Levon 	case TOKEN_QUOTED_ARGUMENT:
1115*1f5207b7SJohn Levon 	case TOKEN_STR_ARGUMENT:
1116*1f5207b7SJohn Levon 		different = t1->argnum != t2->argnum;
1117*1f5207b7SJohn Levon 		break;
1118*1f5207b7SJohn Levon 	case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
1119*1f5207b7SJohn Levon 	case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
1120*1f5207b7SJohn Levon 		different = memcmp(t1->embedded, t2->embedded, 4);
1121*1f5207b7SJohn Levon 		break;
1122*1f5207b7SJohn Levon 	case TOKEN_CHAR:
1123*1f5207b7SJohn Levon 	case TOKEN_WIDE_CHAR:
1124*1f5207b7SJohn Levon 	case TOKEN_STRING:
1125*1f5207b7SJohn Levon 	case TOKEN_WIDE_STRING: {
1126*1f5207b7SJohn Levon 		struct string *s1, *s2;
1127*1f5207b7SJohn Levon 
1128*1f5207b7SJohn Levon 		s1 = t1->string;
1129*1f5207b7SJohn Levon 		s2 = t2->string;
1130*1f5207b7SJohn Levon 		different = 1;
1131*1f5207b7SJohn Levon 		if (s1->length != s2->length)
1132*1f5207b7SJohn Levon 			break;
1133*1f5207b7SJohn Levon 		different = memcmp(s1->data, s2->data, s1->length);
1134*1f5207b7SJohn Levon 		break;
1135*1f5207b7SJohn Levon 	}
1136*1f5207b7SJohn Levon 	default:
1137*1f5207b7SJohn Levon 		different = 1;
1138*1f5207b7SJohn Levon 		break;
1139*1f5207b7SJohn Levon 	}
1140*1f5207b7SJohn Levon 	return different;
1141*1f5207b7SJohn Levon }
1142*1f5207b7SJohn Levon 
1143*1f5207b7SJohn Levon static int token_list_different(struct token *list1, struct token *list2)
1144*1f5207b7SJohn Levon {
1145*1f5207b7SJohn Levon 	for (;;) {
1146*1f5207b7SJohn Levon 		if (list1 == list2)
1147*1f5207b7SJohn Levon 			return 0;
1148*1f5207b7SJohn Levon 		if (!list1 || !list2)
1149*1f5207b7SJohn Levon 			return 1;
1150*1f5207b7SJohn Levon 		if (token_different(list1, list2))
1151*1f5207b7SJohn Levon 			return 1;
1152*1f5207b7SJohn Levon 		list1 = list1->next;
1153*1f5207b7SJohn Levon 		list2 = list2->next;
1154*1f5207b7SJohn Levon 	}
1155*1f5207b7SJohn Levon }
1156*1f5207b7SJohn Levon 
1157*1f5207b7SJohn Levon static inline void set_arg_count(struct token *token)
1158*1f5207b7SJohn Levon {
1159*1f5207b7SJohn Levon 	token_type(token) = TOKEN_ARG_COUNT;
1160*1f5207b7SJohn Levon 	token->count.normal = token->count.quoted =
1161*1f5207b7SJohn Levon 	token->count.str = token->count.vararg = 0;
1162*1f5207b7SJohn Levon }
1163*1f5207b7SJohn Levon 
1164*1f5207b7SJohn Levon static struct token *parse_arguments(struct token *list)
1165*1f5207b7SJohn Levon {
1166*1f5207b7SJohn Levon 	struct token *arg = list->next, *next = list;
1167*1f5207b7SJohn Levon 	struct argcount *count = &list->count;
1168*1f5207b7SJohn Levon 
1169*1f5207b7SJohn Levon 	set_arg_count(list);
1170*1f5207b7SJohn Levon 
1171*1f5207b7SJohn Levon 	if (match_op(arg, ')')) {
1172*1f5207b7SJohn Levon 		next = arg->next;
1173*1f5207b7SJohn Levon 		list->next = &eof_token_entry;
1174*1f5207b7SJohn Levon 		return next;
1175*1f5207b7SJohn Levon 	}
1176*1f5207b7SJohn Levon 
1177*1f5207b7SJohn Levon 	while (token_type(arg) == TOKEN_IDENT) {
1178*1f5207b7SJohn Levon 		if (arg->ident == &__VA_ARGS___ident)
1179*1f5207b7SJohn Levon 			goto Eva_args;
1180*1f5207b7SJohn Levon 		if (!++count->normal)
1181*1f5207b7SJohn Levon 			goto Eargs;
1182*1f5207b7SJohn Levon 		next = arg->next;
1183*1f5207b7SJohn Levon 
1184*1f5207b7SJohn Levon 		if (match_op(next, ',')) {
1185*1f5207b7SJohn Levon 			set_arg_count(next);
1186*1f5207b7SJohn Levon 			arg = next->next;
1187*1f5207b7SJohn Levon 			continue;
1188*1f5207b7SJohn Levon 		}
1189*1f5207b7SJohn Levon 
1190*1f5207b7SJohn Levon 		if (match_op(next, ')')) {
1191*1f5207b7SJohn Levon 			set_arg_count(next);
1192*1f5207b7SJohn Levon 			next = next->next;
1193*1f5207b7SJohn Levon 			arg->next->next = &eof_token_entry;
1194*1f5207b7SJohn Levon 			return next;
1195*1f5207b7SJohn Levon 		}
1196*1f5207b7SJohn Levon 
1197*1f5207b7SJohn Levon 		/* normal cases are finished here */
1198*1f5207b7SJohn Levon 
1199*1f5207b7SJohn Levon 		if (match_op(next, SPECIAL_ELLIPSIS)) {
1200*1f5207b7SJohn Levon 			if (match_op(next->next, ')')) {
1201*1f5207b7SJohn Levon 				set_arg_count(next);
1202*1f5207b7SJohn Levon 				next->count.vararg = 1;
1203*1f5207b7SJohn Levon 				next = next->next;
1204*1f5207b7SJohn Levon 				arg->next->next = &eof_token_entry;
1205*1f5207b7SJohn Levon 				return next->next;
1206*1f5207b7SJohn Levon 			}
1207*1f5207b7SJohn Levon 
1208*1f5207b7SJohn Levon 			arg = next;
1209*1f5207b7SJohn Levon 			goto Enotclosed;
1210*1f5207b7SJohn Levon 		}
1211*1f5207b7SJohn Levon 
1212*1f5207b7SJohn Levon 		if (eof_token(next)) {
1213*1f5207b7SJohn Levon 			goto Enotclosed;
1214*1f5207b7SJohn Levon 		} else {
1215*1f5207b7SJohn Levon 			arg = next;
1216*1f5207b7SJohn Levon 			goto Ebadstuff;
1217*1f5207b7SJohn Levon 		}
1218*1f5207b7SJohn Levon 	}
1219*1f5207b7SJohn Levon 
1220*1f5207b7SJohn Levon 	if (match_op(arg, SPECIAL_ELLIPSIS)) {
1221*1f5207b7SJohn Levon 		next = arg->next;
1222*1f5207b7SJohn Levon 		token_type(arg) = TOKEN_IDENT;
1223*1f5207b7SJohn Levon 		arg->ident = &__VA_ARGS___ident;
1224*1f5207b7SJohn Levon 		if (!match_op(next, ')'))
1225*1f5207b7SJohn Levon 			goto Enotclosed;
1226*1f5207b7SJohn Levon 		if (!++count->normal)
1227*1f5207b7SJohn Levon 			goto Eargs;
1228*1f5207b7SJohn Levon 		set_arg_count(next);
1229*1f5207b7SJohn Levon 		next->count.vararg = 1;
1230*1f5207b7SJohn Levon 		next = next->next;
1231*1f5207b7SJohn Levon 		arg->next->next = &eof_token_entry;
1232*1f5207b7SJohn Levon 		return next;
1233*1f5207b7SJohn Levon 	}
1234*1f5207b7SJohn Levon 
1235*1f5207b7SJohn Levon 	if (eof_token(arg)) {
1236*1f5207b7SJohn Levon 		arg = next;
1237*1f5207b7SJohn Levon 		goto Enotclosed;
1238*1f5207b7SJohn Levon 	}
1239*1f5207b7SJohn Levon 	if (match_op(arg, ','))
1240*1f5207b7SJohn Levon 		goto Emissing;
1241*1f5207b7SJohn Levon 	else
1242*1f5207b7SJohn Levon 		goto Ebadstuff;
1243*1f5207b7SJohn Levon 
1244*1f5207b7SJohn Levon 
1245*1f5207b7SJohn Levon Emissing:
1246*1f5207b7SJohn Levon 	sparse_error(arg->pos, "parameter name missing");
1247*1f5207b7SJohn Levon 	return NULL;
1248*1f5207b7SJohn Levon Ebadstuff:
1249*1f5207b7SJohn Levon 	sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1250*1f5207b7SJohn Levon 		show_token(arg));
1251*1f5207b7SJohn Levon 	return NULL;
1252*1f5207b7SJohn Levon Enotclosed:
1253*1f5207b7SJohn Levon 	sparse_error(arg->pos, "missing ')' in macro parameter list");
1254*1f5207b7SJohn Levon 	return NULL;
1255*1f5207b7SJohn Levon Eva_args:
1256*1f5207b7SJohn Levon 	sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1257*1f5207b7SJohn Levon 	return NULL;
1258*1f5207b7SJohn Levon Eargs:
1259*1f5207b7SJohn Levon 	sparse_error(arg->pos, "too many arguments in macro definition");
1260*1f5207b7SJohn Levon 	return NULL;
1261*1f5207b7SJohn Levon }
1262*1f5207b7SJohn Levon 
1263*1f5207b7SJohn Levon static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1264*1f5207b7SJohn Levon {
1265*1f5207b7SJohn Levon 	struct ident *ident = token->ident;
1266*1f5207b7SJohn Levon 	int nr;
1267*1f5207b7SJohn Levon 
1268*1f5207b7SJohn Levon 	if (!arglist || token_type(token) != TOKEN_IDENT)
1269*1f5207b7SJohn Levon 		return 0;
1270*1f5207b7SJohn Levon 
1271*1f5207b7SJohn Levon 	arglist = arglist->next;
1272*1f5207b7SJohn Levon 
1273*1f5207b7SJohn Levon 	for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1274*1f5207b7SJohn Levon 		if (arglist->ident == ident) {
1275*1f5207b7SJohn Levon 			struct argcount *count = &arglist->next->count;
1276*1f5207b7SJohn Levon 			int n;
1277*1f5207b7SJohn Levon 
1278*1f5207b7SJohn Levon 			token->argnum = nr;
1279*1f5207b7SJohn Levon 			token_type(token) = type;
1280*1f5207b7SJohn Levon 			switch (type) {
1281*1f5207b7SJohn Levon 			case TOKEN_MACRO_ARGUMENT:
1282*1f5207b7SJohn Levon 				n = ++count->normal;
1283*1f5207b7SJohn Levon 				break;
1284*1f5207b7SJohn Levon 			case TOKEN_QUOTED_ARGUMENT:
1285*1f5207b7SJohn Levon 				n = ++count->quoted;
1286*1f5207b7SJohn Levon 				break;
1287*1f5207b7SJohn Levon 			default:
1288*1f5207b7SJohn Levon 				n = ++count->str;
1289*1f5207b7SJohn Levon 			}
1290*1f5207b7SJohn Levon 			if (n)
1291*1f5207b7SJohn Levon 				return count->vararg ? 2 : 1;
1292*1f5207b7SJohn Levon 			/*
1293*1f5207b7SJohn Levon 			 * XXX - need saner handling of that
1294*1f5207b7SJohn Levon 			 * (>= 1024 instances of argument)
1295*1f5207b7SJohn Levon 			 */
1296*1f5207b7SJohn Levon 			token_type(token) = TOKEN_ERROR;
1297*1f5207b7SJohn Levon 			return -1;
1298*1f5207b7SJohn Levon 		}
1299*1f5207b7SJohn Levon 	}
1300*1f5207b7SJohn Levon 	return 0;
1301*1f5207b7SJohn Levon }
1302*1f5207b7SJohn Levon 
1303*1f5207b7SJohn Levon static struct token *handle_hash(struct token **p, struct token *arglist)
1304*1f5207b7SJohn Levon {
1305*1f5207b7SJohn Levon 	struct token *token = *p;
1306*1f5207b7SJohn Levon 	if (arglist) {
1307*1f5207b7SJohn Levon 		struct token *next = token->next;
1308*1f5207b7SJohn Levon 		if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1309*1f5207b7SJohn Levon 			goto Equote;
1310*1f5207b7SJohn Levon 		next->pos.whitespace = token->pos.whitespace;
1311*1f5207b7SJohn Levon 		__free_token(token);
1312*1f5207b7SJohn Levon 		token = *p = next;
1313*1f5207b7SJohn Levon 	} else {
1314*1f5207b7SJohn Levon 		token->pos.noexpand = 1;
1315*1f5207b7SJohn Levon 	}
1316*1f5207b7SJohn Levon 	return token;
1317*1f5207b7SJohn Levon 
1318*1f5207b7SJohn Levon Equote:
1319*1f5207b7SJohn Levon 	sparse_error(token->pos, "'#' is not followed by a macro parameter");
1320*1f5207b7SJohn Levon 	return NULL;
1321*1f5207b7SJohn Levon }
1322*1f5207b7SJohn Levon 
1323*1f5207b7SJohn Levon /* token->next is ## */
1324*1f5207b7SJohn Levon static struct token *handle_hashhash(struct token *token, struct token *arglist)
1325*1f5207b7SJohn Levon {
1326*1f5207b7SJohn Levon 	struct token *last = token;
1327*1f5207b7SJohn Levon 	struct token *concat;
1328*1f5207b7SJohn Levon 	int state = match_op(token, ',');
1329*1f5207b7SJohn Levon 
1330*1f5207b7SJohn Levon 	try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1331*1f5207b7SJohn Levon 
1332*1f5207b7SJohn Levon 	while (1) {
1333*1f5207b7SJohn Levon 		struct token *t;
1334*1f5207b7SJohn Levon 		int is_arg;
1335*1f5207b7SJohn Levon 
1336*1f5207b7SJohn Levon 		/* eat duplicate ## */
1337*1f5207b7SJohn Levon 		concat = token->next;
1338*1f5207b7SJohn Levon 		while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1339*1f5207b7SJohn Levon 			token->next = t;
1340*1f5207b7SJohn Levon 			__free_token(concat);
1341*1f5207b7SJohn Levon 			concat = t;
1342*1f5207b7SJohn Levon 		}
1343*1f5207b7SJohn Levon 		token_type(concat) = TOKEN_CONCAT;
1344*1f5207b7SJohn Levon 
1345*1f5207b7SJohn Levon 		if (eof_token(t))
1346*1f5207b7SJohn Levon 			goto Econcat;
1347*1f5207b7SJohn Levon 
1348*1f5207b7SJohn Levon 		if (match_op(t, '#')) {
1349*1f5207b7SJohn Levon 			t = handle_hash(&concat->next, arglist);
1350*1f5207b7SJohn Levon 			if (!t)
1351*1f5207b7SJohn Levon 				return NULL;
1352*1f5207b7SJohn Levon 		}
1353*1f5207b7SJohn Levon 
1354*1f5207b7SJohn Levon 		is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1355*1f5207b7SJohn Levon 
1356*1f5207b7SJohn Levon 		if (state == 1 && is_arg) {
1357*1f5207b7SJohn Levon 			state = is_arg;
1358*1f5207b7SJohn Levon 		} else {
1359*1f5207b7SJohn Levon 			last = t;
1360*1f5207b7SJohn Levon 			state = match_op(t, ',');
1361*1f5207b7SJohn Levon 		}
1362*1f5207b7SJohn Levon 
1363*1f5207b7SJohn Levon 		token = t;
1364*1f5207b7SJohn Levon 		if (!match_op(token->next, SPECIAL_HASHHASH))
1365*1f5207b7SJohn Levon 			break;
1366*1f5207b7SJohn Levon 	}
1367*1f5207b7SJohn Levon 	/* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1368*1f5207b7SJohn Levon 	if (state == 2)
1369*1f5207b7SJohn Levon 		token_type(last) = TOKEN_GNU_KLUDGE;
1370*1f5207b7SJohn Levon 	return token;
1371*1f5207b7SJohn Levon 
1372*1f5207b7SJohn Levon Econcat:
1373*1f5207b7SJohn Levon 	sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1374*1f5207b7SJohn Levon 	return NULL;
1375*1f5207b7SJohn Levon }
1376*1f5207b7SJohn Levon 
1377*1f5207b7SJohn Levon static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1378*1f5207b7SJohn Levon {
1379*1f5207b7SJohn Levon 	struct token *token = expansion;
1380*1f5207b7SJohn Levon 	struct token **p;
1381*1f5207b7SJohn Levon 
1382*1f5207b7SJohn Levon 	if (match_op(token, SPECIAL_HASHHASH))
1383*1f5207b7SJohn Levon 		goto Econcat;
1384*1f5207b7SJohn Levon 
1385*1f5207b7SJohn Levon 	for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1386*1f5207b7SJohn Levon 		if (match_op(token, '#')) {
1387*1f5207b7SJohn Levon 			token = handle_hash(p, arglist);
1388*1f5207b7SJohn Levon 			if (!token)
1389*1f5207b7SJohn Levon 				return NULL;
1390*1f5207b7SJohn Levon 		}
1391*1f5207b7SJohn Levon 		if (match_op(token->next, SPECIAL_HASHHASH)) {
1392*1f5207b7SJohn Levon 			token = handle_hashhash(token, arglist);
1393*1f5207b7SJohn Levon 			if (!token)
1394*1f5207b7SJohn Levon 				return NULL;
1395*1f5207b7SJohn Levon 		} else {
1396*1f5207b7SJohn Levon 			try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1397*1f5207b7SJohn Levon 		}
1398*1f5207b7SJohn Levon 		switch (token_type(token)) {
1399*1f5207b7SJohn Levon 		case TOKEN_ERROR:
1400*1f5207b7SJohn Levon 			goto Earg;
1401*1f5207b7SJohn Levon 
1402*1f5207b7SJohn Levon 		case TOKEN_STRING:
1403*1f5207b7SJohn Levon 		case TOKEN_WIDE_STRING:
1404*1f5207b7SJohn Levon 			token->string->immutable = 1;
1405*1f5207b7SJohn Levon 			break;
1406*1f5207b7SJohn Levon 		}
1407*1f5207b7SJohn Levon 	}
1408*1f5207b7SJohn Levon 	token = alloc_token(&expansion->pos);
1409*1f5207b7SJohn Levon 	token_type(token) = TOKEN_UNTAINT;
1410*1f5207b7SJohn Levon 	token->ident = name;
1411*1f5207b7SJohn Levon 	token->next = *p;
1412*1f5207b7SJohn Levon 	*p = token;
1413*1f5207b7SJohn Levon 	return expansion;
1414*1f5207b7SJohn Levon 
1415*1f5207b7SJohn Levon Econcat:
1416*1f5207b7SJohn Levon 	sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1417*1f5207b7SJohn Levon 	return NULL;
1418*1f5207b7SJohn Levon Earg:
1419*1f5207b7SJohn Levon 	sparse_error(token->pos, "too many instances of argument in body");
1420*1f5207b7SJohn Levon 	return NULL;
1421*1f5207b7SJohn Levon }
1422*1f5207b7SJohn Levon 
1423*1f5207b7SJohn Levon static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1424*1f5207b7SJohn Levon {
1425*1f5207b7SJohn Levon 	struct token *arglist, *expansion;
1426*1f5207b7SJohn Levon 	struct token *left = token->next;
1427*1f5207b7SJohn Levon 	struct symbol *sym;
1428*1f5207b7SJohn Levon 	struct ident *name;
1429*1f5207b7SJohn Levon 	int ret;
1430*1f5207b7SJohn Levon 
1431*1f5207b7SJohn Levon 	if (token_type(left) != TOKEN_IDENT) {
1432*1f5207b7SJohn Levon 		sparse_error(token->pos, "expected identifier to 'define'");
1433*1f5207b7SJohn Levon 		return 1;
1434*1f5207b7SJohn Levon 	}
1435*1f5207b7SJohn Levon 
1436*1f5207b7SJohn Levon 	name = left->ident;
1437*1f5207b7SJohn Levon 
1438*1f5207b7SJohn Levon 	arglist = NULL;
1439*1f5207b7SJohn Levon 	expansion = left->next;
1440*1f5207b7SJohn Levon 	if (!expansion->pos.whitespace) {
1441*1f5207b7SJohn Levon 		if (match_op(expansion, '(')) {
1442*1f5207b7SJohn Levon 			arglist = expansion;
1443*1f5207b7SJohn Levon 			expansion = parse_arguments(expansion);
1444*1f5207b7SJohn Levon 			if (!expansion)
1445*1f5207b7SJohn Levon 				return 1;
1446*1f5207b7SJohn Levon 		} else if (!eof_token(expansion)) {
1447*1f5207b7SJohn Levon 			warning(expansion->pos,
1448*1f5207b7SJohn Levon 				"no whitespace before object-like macro body");
1449*1f5207b7SJohn Levon 		}
1450*1f5207b7SJohn Levon 	}
1451*1f5207b7SJohn Levon 
1452*1f5207b7SJohn Levon 	expansion = parse_expansion(expansion, arglist, name);
1453*1f5207b7SJohn Levon 	if (!expansion)
1454*1f5207b7SJohn Levon 		return 1;
1455*1f5207b7SJohn Levon 
1456*1f5207b7SJohn Levon 	ret = 1;
1457*1f5207b7SJohn Levon 	sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1458*1f5207b7SJohn Levon 	if (sym) {
1459*1f5207b7SJohn Levon 		int clean;
1460*1f5207b7SJohn Levon 
1461*1f5207b7SJohn Levon 		if (attr < sym->attr)
1462*1f5207b7SJohn Levon 			goto out;
1463*1f5207b7SJohn Levon 
1464*1f5207b7SJohn Levon 		clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1465*1f5207b7SJohn Levon 
1466*1f5207b7SJohn Levon 		if (token_list_different(sym->expansion, expansion) ||
1467*1f5207b7SJohn Levon 		    token_list_different(sym->arglist, arglist)) {
1468*1f5207b7SJohn Levon 			ret = 0;
1469*1f5207b7SJohn Levon 			if ((clean && attr == SYM_ATTR_NORMAL)
1470*1f5207b7SJohn Levon 					|| sym->used_in == file_scope) {
1471*1f5207b7SJohn Levon 				warning(left->pos, "preprocessor token %.*s redefined",
1472*1f5207b7SJohn Levon 						name->len, name->name);
1473*1f5207b7SJohn Levon 				info(sym->pos, "this was the original definition");
1474*1f5207b7SJohn Levon 			}
1475*1f5207b7SJohn Levon 		} else if (clean)
1476*1f5207b7SJohn Levon 			goto out;
1477*1f5207b7SJohn Levon 	}
1478*1f5207b7SJohn Levon 
1479*1f5207b7SJohn Levon 	if (!sym || sym->scope != file_scope) {
1480*1f5207b7SJohn Levon 		sym = alloc_symbol(left->pos, SYM_NODE);
1481*1f5207b7SJohn Levon 		bind_symbol(sym, name, NS_MACRO);
1482*1f5207b7SJohn Levon 		add_ident(&macros, name);
1483*1f5207b7SJohn Levon 		ret = 0;
1484*1f5207b7SJohn Levon 	}
1485*1f5207b7SJohn Levon 
1486*1f5207b7SJohn Levon 	if (!ret) {
1487*1f5207b7SJohn Levon 		sym->expansion = expansion;
1488*1f5207b7SJohn Levon 		sym->arglist = arglist;
1489*1f5207b7SJohn Levon 		__free_token(token);	/* Free the "define" token, but not the rest of the line */
1490*1f5207b7SJohn Levon 	}
1491*1f5207b7SJohn Levon 
1492*1f5207b7SJohn Levon 	sym->namespace = NS_MACRO;
1493*1f5207b7SJohn Levon 	sym->used_in = NULL;
1494*1f5207b7SJohn Levon 	sym->attr = attr;
1495*1f5207b7SJohn Levon out:
1496*1f5207b7SJohn Levon 	return ret;
1497*1f5207b7SJohn Levon }
1498*1f5207b7SJohn Levon 
1499*1f5207b7SJohn Levon static int handle_define(struct stream *stream, struct token **line, struct token *token)
1500*1f5207b7SJohn Levon {
1501*1f5207b7SJohn Levon 	return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1502*1f5207b7SJohn Levon }
1503*1f5207b7SJohn Levon 
1504*1f5207b7SJohn Levon static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1505*1f5207b7SJohn Levon {
1506*1f5207b7SJohn Levon 	return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1507*1f5207b7SJohn Levon }
1508*1f5207b7SJohn Levon 
1509*1f5207b7SJohn Levon static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1510*1f5207b7SJohn Levon {
1511*1f5207b7SJohn Levon 	return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1512*1f5207b7SJohn Levon }
1513*1f5207b7SJohn Levon 
1514*1f5207b7SJohn Levon static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1515*1f5207b7SJohn Levon {
1516*1f5207b7SJohn Levon 	struct token *left = token->next;
1517*1f5207b7SJohn Levon 	struct symbol *sym;
1518*1f5207b7SJohn Levon 
1519*1f5207b7SJohn Levon 	if (token_type(left) != TOKEN_IDENT) {
1520*1f5207b7SJohn Levon 		sparse_error(token->pos, "expected identifier to 'undef'");
1521*1f5207b7SJohn Levon 		return 1;
1522*1f5207b7SJohn Levon 	}
1523*1f5207b7SJohn Levon 
1524*1f5207b7SJohn Levon 	sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1525*1f5207b7SJohn Levon 	if (sym) {
1526*1f5207b7SJohn Levon 		if (attr < sym->attr)
1527*1f5207b7SJohn Levon 			return 1;
1528*1f5207b7SJohn Levon 		if (attr == sym->attr && sym->namespace == NS_UNDEF)
1529*1f5207b7SJohn Levon 			return 1;
1530*1f5207b7SJohn Levon 	} else if (attr <= SYM_ATTR_NORMAL)
1531*1f5207b7SJohn Levon 		return 1;
1532*1f5207b7SJohn Levon 
1533*1f5207b7SJohn Levon 	if (!sym || sym->scope != file_scope) {
1534*1f5207b7SJohn Levon 		sym = alloc_symbol(left->pos, SYM_NODE);
1535*1f5207b7SJohn Levon 		bind_symbol(sym, left->ident, NS_MACRO);
1536*1f5207b7SJohn Levon 	}
1537*1f5207b7SJohn Levon 
1538*1f5207b7SJohn Levon 	sym->namespace = NS_UNDEF;
1539*1f5207b7SJohn Levon 	sym->used_in = NULL;
1540*1f5207b7SJohn Levon 	sym->attr = attr;
1541*1f5207b7SJohn Levon 
1542*1f5207b7SJohn Levon 	return 1;
1543*1f5207b7SJohn Levon }
1544*1f5207b7SJohn Levon 
1545*1f5207b7SJohn Levon static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1546*1f5207b7SJohn Levon {
1547*1f5207b7SJohn Levon 	return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1548*1f5207b7SJohn Levon }
1549*1f5207b7SJohn Levon 
1550*1f5207b7SJohn Levon static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1551*1f5207b7SJohn Levon {
1552*1f5207b7SJohn Levon 	return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1553*1f5207b7SJohn Levon }
1554*1f5207b7SJohn Levon 
1555*1f5207b7SJohn Levon static int preprocessor_if(struct stream *stream, struct token *token, int true)
1556*1f5207b7SJohn Levon {
1557*1f5207b7SJohn Levon 	token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1558*1f5207b7SJohn Levon 	free_preprocessor_line(token->next);
1559*1f5207b7SJohn Levon 	token->next = stream->top_if;
1560*1f5207b7SJohn Levon 	stream->top_if = token;
1561*1f5207b7SJohn Levon 	if (false_nesting || true != 1)
1562*1f5207b7SJohn Levon 		false_nesting++;
1563*1f5207b7SJohn Levon 	return 0;
1564*1f5207b7SJohn Levon }
1565*1f5207b7SJohn Levon 
1566*1f5207b7SJohn Levon static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1567*1f5207b7SJohn Levon {
1568*1f5207b7SJohn Levon 	struct token *next = token->next;
1569*1f5207b7SJohn Levon 	int arg;
1570*1f5207b7SJohn Levon 	if (token_type(next) == TOKEN_IDENT) {
1571*1f5207b7SJohn Levon 		arg = token_defined(next);
1572*1f5207b7SJohn Levon 	} else {
1573*1f5207b7SJohn Levon 		dirty_stream(stream);
1574*1f5207b7SJohn Levon 		if (!false_nesting)
1575*1f5207b7SJohn Levon 			sparse_error(token->pos, "expected preprocessor identifier");
1576*1f5207b7SJohn Levon 		arg = -1;
1577*1f5207b7SJohn Levon 	}
1578*1f5207b7SJohn Levon 	return preprocessor_if(stream, token, arg);
1579*1f5207b7SJohn Levon }
1580*1f5207b7SJohn Levon 
1581*1f5207b7SJohn Levon static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1582*1f5207b7SJohn Levon {
1583*1f5207b7SJohn Levon 	struct token *next = token->next;
1584*1f5207b7SJohn Levon 	int arg;
1585*1f5207b7SJohn Levon 	if (token_type(next) == TOKEN_IDENT) {
1586*1f5207b7SJohn Levon 		if (!stream->dirty && !stream->ifndef) {
1587*1f5207b7SJohn Levon 			if (!stream->protect) {
1588*1f5207b7SJohn Levon 				stream->ifndef = token;
1589*1f5207b7SJohn Levon 				stream->protect = next->ident;
1590*1f5207b7SJohn Levon 			} else if (stream->protect == next->ident) {
1591*1f5207b7SJohn Levon 				stream->ifndef = token;
1592*1f5207b7SJohn Levon 				stream->dirty = 1;
1593*1f5207b7SJohn Levon 			}
1594*1f5207b7SJohn Levon 		}
1595*1f5207b7SJohn Levon 		arg = !token_defined(next);
1596*1f5207b7SJohn Levon 	} else {
1597*1f5207b7SJohn Levon 		dirty_stream(stream);
1598*1f5207b7SJohn Levon 		if (!false_nesting)
1599*1f5207b7SJohn Levon 			sparse_error(token->pos, "expected preprocessor identifier");
1600*1f5207b7SJohn Levon 		arg = -1;
1601*1f5207b7SJohn Levon 	}
1602*1f5207b7SJohn Levon 
1603*1f5207b7SJohn Levon 	return preprocessor_if(stream, token, arg);
1604*1f5207b7SJohn Levon }
1605*1f5207b7SJohn Levon 
1606*1f5207b7SJohn Levon static const char *show_token_sequence(struct token *token, int quote);
1607*1f5207b7SJohn Levon 
1608*1f5207b7SJohn Levon /*
1609*1f5207b7SJohn Levon  * Expression handling for #if and #elif; it differs from normal expansion
1610*1f5207b7SJohn Levon  * due to special treatment of "defined".
1611*1f5207b7SJohn Levon  */
1612*1f5207b7SJohn Levon static int expression_value(struct token **where)
1613*1f5207b7SJohn Levon {
1614*1f5207b7SJohn Levon 	struct expression *expr;
1615*1f5207b7SJohn Levon 	struct token *p;
1616*1f5207b7SJohn Levon 	struct token **list = where, **beginning = NULL;
1617*1f5207b7SJohn Levon 	long long value;
1618*1f5207b7SJohn Levon 	int state = 0;
1619*1f5207b7SJohn Levon 
1620*1f5207b7SJohn Levon 	while (!eof_token(p = scan_next(list))) {
1621*1f5207b7SJohn Levon 		switch (state) {
1622*1f5207b7SJohn Levon 		case 0:
1623*1f5207b7SJohn Levon 			if (token_type(p) != TOKEN_IDENT)
1624*1f5207b7SJohn Levon 				break;
1625*1f5207b7SJohn Levon 			if (p->ident == &defined_ident) {
1626*1f5207b7SJohn Levon 				state = 1;
1627*1f5207b7SJohn Levon 				beginning = list;
1628*1f5207b7SJohn Levon 				break;
1629*1f5207b7SJohn Levon 			}
1630*1f5207b7SJohn Levon 			if (!expand_one_symbol(list))
1631*1f5207b7SJohn Levon 				continue;
1632*1f5207b7SJohn Levon 			if (token_type(p) != TOKEN_IDENT)
1633*1f5207b7SJohn Levon 				break;
1634*1f5207b7SJohn Levon 			token_type(p) = TOKEN_ZERO_IDENT;
1635*1f5207b7SJohn Levon 			break;
1636*1f5207b7SJohn Levon 		case 1:
1637*1f5207b7SJohn Levon 			if (match_op(p, '(')) {
1638*1f5207b7SJohn Levon 				state = 2;
1639*1f5207b7SJohn Levon 			} else {
1640*1f5207b7SJohn Levon 				state = 0;
1641*1f5207b7SJohn Levon 				replace_with_defined(p);
1642*1f5207b7SJohn Levon 				*beginning = p;
1643*1f5207b7SJohn Levon 			}
1644*1f5207b7SJohn Levon 			break;
1645*1f5207b7SJohn Levon 		case 2:
1646*1f5207b7SJohn Levon 			if (token_type(p) == TOKEN_IDENT)
1647*1f5207b7SJohn Levon 				state = 3;
1648*1f5207b7SJohn Levon 			else
1649*1f5207b7SJohn Levon 				state = 0;
1650*1f5207b7SJohn Levon 			replace_with_defined(p);
1651*1f5207b7SJohn Levon 			*beginning = p;
1652*1f5207b7SJohn Levon 			break;
1653*1f5207b7SJohn Levon 		case 3:
1654*1f5207b7SJohn Levon 			state = 0;
1655*1f5207b7SJohn Levon 			if (!match_op(p, ')'))
1656*1f5207b7SJohn Levon 				sparse_error(p->pos, "missing ')' after \"defined\"");
1657*1f5207b7SJohn Levon 			*list = p->next;
1658*1f5207b7SJohn Levon 			continue;
1659*1f5207b7SJohn Levon 		}
1660*1f5207b7SJohn Levon 		list = &p->next;
1661*1f5207b7SJohn Levon 	}
1662*1f5207b7SJohn Levon 
1663*1f5207b7SJohn Levon 	p = constant_expression(*where, &expr);
1664*1f5207b7SJohn Levon 	if (!eof_token(p))
1665*1f5207b7SJohn Levon 		sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1666*1f5207b7SJohn Levon 	value = get_expression_value(expr);
1667*1f5207b7SJohn Levon 	return value != 0;
1668*1f5207b7SJohn Levon }
1669*1f5207b7SJohn Levon 
1670*1f5207b7SJohn Levon static int handle_if(struct stream *stream, struct token **line, struct token *token)
1671*1f5207b7SJohn Levon {
1672*1f5207b7SJohn Levon 	int value = 0;
1673*1f5207b7SJohn Levon 	if (!false_nesting)
1674*1f5207b7SJohn Levon 		value = expression_value(&token->next);
1675*1f5207b7SJohn Levon 
1676*1f5207b7SJohn Levon 	dirty_stream(stream);
1677*1f5207b7SJohn Levon 	return preprocessor_if(stream, token, value);
1678*1f5207b7SJohn Levon }
1679*1f5207b7SJohn Levon 
1680*1f5207b7SJohn Levon static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1681*1f5207b7SJohn Levon {
1682*1f5207b7SJohn Levon 	struct token *top_if = stream->top_if;
1683*1f5207b7SJohn Levon 	end_group(stream);
1684*1f5207b7SJohn Levon 
1685*1f5207b7SJohn Levon 	if (!top_if) {
1686*1f5207b7SJohn Levon 		nesting_error(stream);
1687*1f5207b7SJohn Levon 		sparse_error(token->pos, "unmatched #elif within stream");
1688*1f5207b7SJohn Levon 		return 1;
1689*1f5207b7SJohn Levon 	}
1690*1f5207b7SJohn Levon 
1691*1f5207b7SJohn Levon 	if (token_type(top_if) == TOKEN_ELSE) {
1692*1f5207b7SJohn Levon 		nesting_error(stream);
1693*1f5207b7SJohn Levon 		sparse_error(token->pos, "#elif after #else");
1694*1f5207b7SJohn Levon 		if (!false_nesting)
1695*1f5207b7SJohn Levon 			false_nesting = 1;
1696*1f5207b7SJohn Levon 		return 1;
1697*1f5207b7SJohn Levon 	}
1698*1f5207b7SJohn Levon 
1699*1f5207b7SJohn Levon 	dirty_stream(stream);
1700*1f5207b7SJohn Levon 	if (token_type(top_if) != TOKEN_IF)
1701*1f5207b7SJohn Levon 		return 1;
1702*1f5207b7SJohn Levon 	if (false_nesting) {
1703*1f5207b7SJohn Levon 		false_nesting = 0;
1704*1f5207b7SJohn Levon 		if (!expression_value(&token->next))
1705*1f5207b7SJohn Levon 			false_nesting = 1;
1706*1f5207b7SJohn Levon 	} else {
1707*1f5207b7SJohn Levon 		false_nesting = 1;
1708*1f5207b7SJohn Levon 		token_type(top_if) = TOKEN_SKIP_GROUPS;
1709*1f5207b7SJohn Levon 	}
1710*1f5207b7SJohn Levon 	return 1;
1711*1f5207b7SJohn Levon }
1712*1f5207b7SJohn Levon 
1713*1f5207b7SJohn Levon static int handle_else(struct stream *stream, struct token **line, struct token *token)
1714*1f5207b7SJohn Levon {
1715*1f5207b7SJohn Levon 	struct token *top_if = stream->top_if;
1716*1f5207b7SJohn Levon 	end_group(stream);
1717*1f5207b7SJohn Levon 
1718*1f5207b7SJohn Levon 	if (!top_if) {
1719*1f5207b7SJohn Levon 		nesting_error(stream);
1720*1f5207b7SJohn Levon 		sparse_error(token->pos, "unmatched #else within stream");
1721*1f5207b7SJohn Levon 		return 1;
1722*1f5207b7SJohn Levon 	}
1723*1f5207b7SJohn Levon 
1724*1f5207b7SJohn Levon 	if (token_type(top_if) == TOKEN_ELSE) {
1725*1f5207b7SJohn Levon 		nesting_error(stream);
1726*1f5207b7SJohn Levon 		sparse_error(token->pos, "#else after #else");
1727*1f5207b7SJohn Levon 	}
1728*1f5207b7SJohn Levon 	if (false_nesting) {
1729*1f5207b7SJohn Levon 		if (token_type(top_if) == TOKEN_IF)
1730*1f5207b7SJohn Levon 			false_nesting = 0;
1731*1f5207b7SJohn Levon 	} else {
1732*1f5207b7SJohn Levon 		false_nesting = 1;
1733*1f5207b7SJohn Levon 	}
1734*1f5207b7SJohn Levon 	token_type(top_if) = TOKEN_ELSE;
1735*1f5207b7SJohn Levon 	return 1;
1736*1f5207b7SJohn Levon }
1737*1f5207b7SJohn Levon 
1738*1f5207b7SJohn Levon static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1739*1f5207b7SJohn Levon {
1740*1f5207b7SJohn Levon 	struct token *top_if = stream->top_if;
1741*1f5207b7SJohn Levon 	end_group(stream);
1742*1f5207b7SJohn Levon 	if (!top_if) {
1743*1f5207b7SJohn Levon 		nesting_error(stream);
1744*1f5207b7SJohn Levon 		sparse_error(token->pos, "unmatched #endif in stream");
1745*1f5207b7SJohn Levon 		return 1;
1746*1f5207b7SJohn Levon 	}
1747*1f5207b7SJohn Levon 	if (false_nesting)
1748*1f5207b7SJohn Levon 		false_nesting--;
1749*1f5207b7SJohn Levon 	stream->top_if = top_if->next;
1750*1f5207b7SJohn Levon 	__free_token(top_if);
1751*1f5207b7SJohn Levon 	return 1;
1752*1f5207b7SJohn Levon }
1753*1f5207b7SJohn Levon 
1754*1f5207b7SJohn Levon static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1755*1f5207b7SJohn Levon {
1756*1f5207b7SJohn Levon 	warning(token->pos, "%s", show_token_sequence(token->next, 0));
1757*1f5207b7SJohn Levon 	return 1;
1758*1f5207b7SJohn Levon }
1759*1f5207b7SJohn Levon 
1760*1f5207b7SJohn Levon static int handle_error(struct stream *stream, struct token **line, struct token *token)
1761*1f5207b7SJohn Levon {
1762*1f5207b7SJohn Levon 	sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1763*1f5207b7SJohn Levon 	return 1;
1764*1f5207b7SJohn Levon }
1765*1f5207b7SJohn Levon 
1766*1f5207b7SJohn Levon static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1767*1f5207b7SJohn Levon {
1768*1f5207b7SJohn Levon 	/*
1769*1f5207b7SJohn Levon 	 * Do we have any non-system includes?
1770*1f5207b7SJohn Levon 	 * Clear them out if so..
1771*1f5207b7SJohn Levon 	 */
1772*1f5207b7SJohn Levon 	*sys_includepath = NULL;
1773*1f5207b7SJohn Levon 	return 1;
1774*1f5207b7SJohn Levon }
1775*1f5207b7SJohn Levon 
1776*1f5207b7SJohn Levon static inline void update_inc_ptrs(const char ***where)
1777*1f5207b7SJohn Levon {
1778*1f5207b7SJohn Levon 
1779*1f5207b7SJohn Levon 	if (*where <= dirafter_includepath) {
1780*1f5207b7SJohn Levon 		dirafter_includepath++;
1781*1f5207b7SJohn Levon 		/* If this was the entry that we prepend, don't
1782*1f5207b7SJohn Levon 		 * rise the lower entries, even if they are at
1783*1f5207b7SJohn Levon 		 * the same level. */
1784*1f5207b7SJohn Levon 		if (where == &dirafter_includepath)
1785*1f5207b7SJohn Levon 			return;
1786*1f5207b7SJohn Levon 	}
1787*1f5207b7SJohn Levon 	if (*where <= sys_includepath) {
1788*1f5207b7SJohn Levon 		sys_includepath++;
1789*1f5207b7SJohn Levon 		if (where == &sys_includepath)
1790*1f5207b7SJohn Levon 			return;
1791*1f5207b7SJohn Levon 	}
1792*1f5207b7SJohn Levon 	if (*where <= isys_includepath) {
1793*1f5207b7SJohn Levon 		isys_includepath++;
1794*1f5207b7SJohn Levon 		if (where == &isys_includepath)
1795*1f5207b7SJohn Levon 			return;
1796*1f5207b7SJohn Levon 	}
1797*1f5207b7SJohn Levon 
1798*1f5207b7SJohn Levon 	/* angle_includepath is actually never updated, since we
1799*1f5207b7SJohn Levon 	 * don't suppport -iquote rught now. May change some day. */
1800*1f5207b7SJohn Levon 	if (*where <= angle_includepath) {
1801*1f5207b7SJohn Levon 		angle_includepath++;
1802*1f5207b7SJohn Levon 		if (where == &angle_includepath)
1803*1f5207b7SJohn Levon 			return;
1804*1f5207b7SJohn Levon 	}
1805*1f5207b7SJohn Levon }
1806*1f5207b7SJohn Levon 
1807*1f5207b7SJohn Levon /* Add a path before 'where' and update the pointers associated with the
1808*1f5207b7SJohn Levon  * includepath array */
1809*1f5207b7SJohn Levon static void add_path_entry(struct token *token, const char *path,
1810*1f5207b7SJohn Levon 	const char ***where)
1811*1f5207b7SJohn Levon {
1812*1f5207b7SJohn Levon 	const char **dst;
1813*1f5207b7SJohn Levon 	const char *next;
1814*1f5207b7SJohn Levon 
1815*1f5207b7SJohn Levon 	/* Need one free entry.. */
1816*1f5207b7SJohn Levon 	if (includepath[INCLUDEPATHS-2])
1817*1f5207b7SJohn Levon 		error_die(token->pos, "too many include path entries");
1818*1f5207b7SJohn Levon 
1819*1f5207b7SJohn Levon 	/* check that this is not a duplicate */
1820*1f5207b7SJohn Levon 	dst = includepath;
1821*1f5207b7SJohn Levon 	while (*dst) {
1822*1f5207b7SJohn Levon 		if (strcmp(*dst, path) == 0)
1823*1f5207b7SJohn Levon 			return;
1824*1f5207b7SJohn Levon 		dst++;
1825*1f5207b7SJohn Levon 	}
1826*1f5207b7SJohn Levon 	next = path;
1827*1f5207b7SJohn Levon 	dst = *where;
1828*1f5207b7SJohn Levon 
1829*1f5207b7SJohn Levon 	update_inc_ptrs(where);
1830*1f5207b7SJohn Levon 
1831*1f5207b7SJohn Levon 	/*
1832*1f5207b7SJohn Levon 	 * Move them all up starting at dst,
1833*1f5207b7SJohn Levon 	 * insert the new entry..
1834*1f5207b7SJohn Levon 	 */
1835*1f5207b7SJohn Levon 	do {
1836*1f5207b7SJohn Levon 		const char *tmp = *dst;
1837*1f5207b7SJohn Levon 		*dst = next;
1838*1f5207b7SJohn Levon 		next = tmp;
1839*1f5207b7SJohn Levon 		dst++;
1840*1f5207b7SJohn Levon 	} while (next);
1841*1f5207b7SJohn Levon }
1842*1f5207b7SJohn Levon 
1843*1f5207b7SJohn Levon static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1844*1f5207b7SJohn Levon {
1845*1f5207b7SJohn Levon 	for (;;) {
1846*1f5207b7SJohn Levon 		token = token->next;
1847*1f5207b7SJohn Levon 		if (eof_token(token))
1848*1f5207b7SJohn Levon 			return 1;
1849*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_STRING) {
1850*1f5207b7SJohn Levon 			warning(token->pos, "expected path string");
1851*1f5207b7SJohn Levon 			return 1;
1852*1f5207b7SJohn Levon 		}
1853*1f5207b7SJohn Levon 		add_path_entry(token, token->string->data, &isys_includepath);
1854*1f5207b7SJohn Levon 	}
1855*1f5207b7SJohn Levon }
1856*1f5207b7SJohn Levon 
1857*1f5207b7SJohn Levon static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1858*1f5207b7SJohn Levon {
1859*1f5207b7SJohn Levon 	for (;;) {
1860*1f5207b7SJohn Levon 		token = token->next;
1861*1f5207b7SJohn Levon 		if (eof_token(token))
1862*1f5207b7SJohn Levon 			return 1;
1863*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_STRING) {
1864*1f5207b7SJohn Levon 			sparse_error(token->pos, "expected path string");
1865*1f5207b7SJohn Levon 			return 1;
1866*1f5207b7SJohn Levon 		}
1867*1f5207b7SJohn Levon 		add_path_entry(token, token->string->data, &sys_includepath);
1868*1f5207b7SJohn Levon 	}
1869*1f5207b7SJohn Levon }
1870*1f5207b7SJohn Levon 
1871*1f5207b7SJohn Levon static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1872*1f5207b7SJohn Levon {
1873*1f5207b7SJohn Levon 	for (;;) {
1874*1f5207b7SJohn Levon 		token = token->next;
1875*1f5207b7SJohn Levon 		if (eof_token(token))
1876*1f5207b7SJohn Levon 			return 1;
1877*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_STRING) {
1878*1f5207b7SJohn Levon 			sparse_error(token->pos, "expected path string");
1879*1f5207b7SJohn Levon 			return 1;
1880*1f5207b7SJohn Levon 		}
1881*1f5207b7SJohn Levon 		add_path_entry(token, token->string->data, &dirafter_includepath);
1882*1f5207b7SJohn Levon 	}
1883*1f5207b7SJohn Levon }
1884*1f5207b7SJohn Levon 
1885*1f5207b7SJohn Levon /* Add to end on includepath list - no pointer updates */
1886*1f5207b7SJohn Levon static void add_dirafter_entry(struct token *token, const char *path)
1887*1f5207b7SJohn Levon {
1888*1f5207b7SJohn Levon 	const char **dst = includepath;
1889*1f5207b7SJohn Levon 
1890*1f5207b7SJohn Levon 	/* Need one free entry.. */
1891*1f5207b7SJohn Levon 	if (includepath[INCLUDEPATHS-2])
1892*1f5207b7SJohn Levon 		error_die(token->pos, "too many include path entries");
1893*1f5207b7SJohn Levon 
1894*1f5207b7SJohn Levon 	/* Add to the end */
1895*1f5207b7SJohn Levon 	while (*dst)
1896*1f5207b7SJohn Levon 		dst++;
1897*1f5207b7SJohn Levon 	*dst = path;
1898*1f5207b7SJohn Levon 	dst++;
1899*1f5207b7SJohn Levon 	*dst = NULL;
1900*1f5207b7SJohn Levon }
1901*1f5207b7SJohn Levon 
1902*1f5207b7SJohn Levon static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1903*1f5207b7SJohn Levon {
1904*1f5207b7SJohn Levon 	for (;;) {
1905*1f5207b7SJohn Levon 		token = token->next;
1906*1f5207b7SJohn Levon 		if (eof_token(token))
1907*1f5207b7SJohn Levon 			return 1;
1908*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_STRING) {
1909*1f5207b7SJohn Levon 			sparse_error(token->pos, "expected path string");
1910*1f5207b7SJohn Levon 			return 1;
1911*1f5207b7SJohn Levon 		}
1912*1f5207b7SJohn Levon 		add_dirafter_entry(token, token->string->data);
1913*1f5207b7SJohn Levon 	}
1914*1f5207b7SJohn Levon }
1915*1f5207b7SJohn Levon 
1916*1f5207b7SJohn Levon static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1917*1f5207b7SJohn Levon {
1918*1f5207b7SJohn Levon 	/*
1919*1f5207b7SJohn Levon 	 * -I-
1920*1f5207b7SJohn Levon 	 *  From info gcc:
1921*1f5207b7SJohn Levon 	 *  Split the include path.  Any directories specified with `-I'
1922*1f5207b7SJohn Levon 	 *  options before `-I-' are searched only for headers requested with
1923*1f5207b7SJohn Levon 	 *  `#include "FILE"'; they are not searched for `#include <FILE>'.
1924*1f5207b7SJohn Levon 	 *  If additional directories are specified with `-I' options after
1925*1f5207b7SJohn Levon 	 *  the `-I-', those directories are searched for all `#include'
1926*1f5207b7SJohn Levon 	 *  directives.
1927*1f5207b7SJohn Levon 	 *  In addition, `-I-' inhibits the use of the directory of the current
1928*1f5207b7SJohn Levon 	 *  file directory as the first search directory for `#include "FILE"'.
1929*1f5207b7SJohn Levon 	 */
1930*1f5207b7SJohn Levon 	quote_includepath = includepath+1;
1931*1f5207b7SJohn Levon 	angle_includepath = sys_includepath;
1932*1f5207b7SJohn Levon 	return 1;
1933*1f5207b7SJohn Levon }
1934*1f5207b7SJohn Levon 
1935*1f5207b7SJohn Levon /*
1936*1f5207b7SJohn Levon  * We replace "#pragma xxx" with "__pragma__" in the token
1937*1f5207b7SJohn Levon  * stream. Just as an example.
1938*1f5207b7SJohn Levon  *
1939*1f5207b7SJohn Levon  * We'll just #define that away for now, but the theory here
1940*1f5207b7SJohn Levon  * is that we can use this to insert arbitrary token sequences
1941*1f5207b7SJohn Levon  * to turn the pragmas into internal front-end sequences for
1942*1f5207b7SJohn Levon  * when we actually start caring about them.
1943*1f5207b7SJohn Levon  *
1944*1f5207b7SJohn Levon  * So eventually this will turn into some kind of extended
1945*1f5207b7SJohn Levon  * __attribute__() like thing, except called __pragma__(xxx).
1946*1f5207b7SJohn Levon  */
1947*1f5207b7SJohn Levon static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1948*1f5207b7SJohn Levon {
1949*1f5207b7SJohn Levon 	struct token *next = *line;
1950*1f5207b7SJohn Levon 
1951*1f5207b7SJohn Levon 	if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1952*1f5207b7SJohn Levon 		stream->once = 1;
1953*1f5207b7SJohn Levon 		return 1;
1954*1f5207b7SJohn Levon 	}
1955*1f5207b7SJohn Levon 	token->ident = &pragma_ident;
1956*1f5207b7SJohn Levon 	token->pos.newline = 1;
1957*1f5207b7SJohn Levon 	token->pos.whitespace = 1;
1958*1f5207b7SJohn Levon 	token->pos.pos = 1;
1959*1f5207b7SJohn Levon 	*line = token;
1960*1f5207b7SJohn Levon 	token->next = next;
1961*1f5207b7SJohn Levon 	return 0;
1962*1f5207b7SJohn Levon }
1963*1f5207b7SJohn Levon 
1964*1f5207b7SJohn Levon /*
1965*1f5207b7SJohn Levon  * We ignore #line for now.
1966*1f5207b7SJohn Levon  */
1967*1f5207b7SJohn Levon static int handle_line(struct stream *stream, struct token **line, struct token *token)
1968*1f5207b7SJohn Levon {
1969*1f5207b7SJohn Levon 	return 1;
1970*1f5207b7SJohn Levon }
1971*1f5207b7SJohn Levon 
1972*1f5207b7SJohn Levon /*
1973*1f5207b7SJohn Levon  * Ignore "#ident".
1974*1f5207b7SJohn Levon  */
1975*1f5207b7SJohn Levon static int handle_ident(struct stream *stream, struct token **line, struct token *token)
1976*1f5207b7SJohn Levon {
1977*1f5207b7SJohn Levon 	return 1;
1978*1f5207b7SJohn Levon }
1979*1f5207b7SJohn Levon 
1980*1f5207b7SJohn Levon static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1981*1f5207b7SJohn Levon {
1982*1f5207b7SJohn Levon 	sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1983*1f5207b7SJohn Levon 	return 1;
1984*1f5207b7SJohn Levon }
1985*1f5207b7SJohn Levon 
1986*1f5207b7SJohn Levon 
1987*1f5207b7SJohn Levon static void init_preprocessor(void)
1988*1f5207b7SJohn Levon {
1989*1f5207b7SJohn Levon 	int i;
1990*1f5207b7SJohn Levon 	int stream = init_stream("preprocessor", -1, includepath);
1991*1f5207b7SJohn Levon 	static struct {
1992*1f5207b7SJohn Levon 		const char *name;
1993*1f5207b7SJohn Levon 		int (*handler)(struct stream *, struct token **, struct token *);
1994*1f5207b7SJohn Levon 	} normal[] = {
1995*1f5207b7SJohn Levon 		{ "define",		handle_define },
1996*1f5207b7SJohn Levon 		{ "weak_define",	handle_weak_define },
1997*1f5207b7SJohn Levon 		{ "strong_define",	handle_strong_define },
1998*1f5207b7SJohn Levon 		{ "undef",		handle_undef },
1999*1f5207b7SJohn Levon 		{ "strong_undef",	handle_strong_undef },
2000*1f5207b7SJohn Levon 		{ "warning",		handle_warning },
2001*1f5207b7SJohn Levon 		{ "error",		handle_error },
2002*1f5207b7SJohn Levon 		{ "include",		handle_include },
2003*1f5207b7SJohn Levon 		{ "include_next",	handle_include_next },
2004*1f5207b7SJohn Levon 		{ "pragma",		handle_pragma },
2005*1f5207b7SJohn Levon 		{ "line",		handle_line },
2006*1f5207b7SJohn Levon 		{ "ident",		handle_ident },
2007*1f5207b7SJohn Levon 
2008*1f5207b7SJohn Levon 		// our internal preprocessor tokens
2009*1f5207b7SJohn Levon 		{ "nostdinc",	   handle_nostdinc },
2010*1f5207b7SJohn Levon 		{ "add_include",   handle_add_include },
2011*1f5207b7SJohn Levon 		{ "add_isystem",   handle_add_isystem },
2012*1f5207b7SJohn Levon 		{ "add_system",    handle_add_system },
2013*1f5207b7SJohn Levon 		{ "add_dirafter",  handle_add_dirafter },
2014*1f5207b7SJohn Levon 		{ "split_include", handle_split_include },
2015*1f5207b7SJohn Levon 		{ "argv_include",  handle_argv_include },
2016*1f5207b7SJohn Levon 	}, special[] = {
2017*1f5207b7SJohn Levon 		{ "ifdef",	handle_ifdef },
2018*1f5207b7SJohn Levon 		{ "ifndef",	handle_ifndef },
2019*1f5207b7SJohn Levon 		{ "else",	handle_else },
2020*1f5207b7SJohn Levon 		{ "endif",	handle_endif },
2021*1f5207b7SJohn Levon 		{ "if",		handle_if },
2022*1f5207b7SJohn Levon 		{ "elif",	handle_elif },
2023*1f5207b7SJohn Levon 	};
2024*1f5207b7SJohn Levon 
2025*1f5207b7SJohn Levon 	for (i = 0; i < ARRAY_SIZE(normal); i++) {
2026*1f5207b7SJohn Levon 		struct symbol *sym;
2027*1f5207b7SJohn Levon 		sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2028*1f5207b7SJohn Levon 		sym->handler = normal[i].handler;
2029*1f5207b7SJohn Levon 		sym->normal = 1;
2030*1f5207b7SJohn Levon 	}
2031*1f5207b7SJohn Levon 	for (i = 0; i < ARRAY_SIZE(special); i++) {
2032*1f5207b7SJohn Levon 		struct symbol *sym;
2033*1f5207b7SJohn Levon 		sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2034*1f5207b7SJohn Levon 		sym->handler = special[i].handler;
2035*1f5207b7SJohn Levon 		sym->normal = 0;
2036*1f5207b7SJohn Levon 	}
2037*1f5207b7SJohn Levon 
2038*1f5207b7SJohn Levon 	counter_macro = 0;
2039*1f5207b7SJohn Levon }
2040*1f5207b7SJohn Levon 
2041*1f5207b7SJohn Levon static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
2042*1f5207b7SJohn Levon {
2043*1f5207b7SJohn Levon 	int (*handler)(struct stream *, struct token **, struct token *);
2044*1f5207b7SJohn Levon 	struct token *token = start->next;
2045*1f5207b7SJohn Levon 	int is_normal = 1;
2046*1f5207b7SJohn Levon 
2047*1f5207b7SJohn Levon 	if (eof_token(token))
2048*1f5207b7SJohn Levon 		return;
2049*1f5207b7SJohn Levon 
2050*1f5207b7SJohn Levon 	if (token_type(token) == TOKEN_IDENT) {
2051*1f5207b7SJohn Levon 		struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
2052*1f5207b7SJohn Levon 		if (sym) {
2053*1f5207b7SJohn Levon 			handler = sym->handler;
2054*1f5207b7SJohn Levon 			is_normal = sym->normal;
2055*1f5207b7SJohn Levon 		} else {
2056*1f5207b7SJohn Levon 			handler = handle_nondirective;
2057*1f5207b7SJohn Levon 		}
2058*1f5207b7SJohn Levon 	} else if (token_type(token) == TOKEN_NUMBER) {
2059*1f5207b7SJohn Levon 		handler = handle_line;
2060*1f5207b7SJohn Levon 	} else {
2061*1f5207b7SJohn Levon 		handler = handle_nondirective;
2062*1f5207b7SJohn Levon 	}
2063*1f5207b7SJohn Levon 
2064*1f5207b7SJohn Levon 	if (is_normal) {
2065*1f5207b7SJohn Levon 		dirty_stream(stream);
2066*1f5207b7SJohn Levon 		if (false_nesting)
2067*1f5207b7SJohn Levon 			goto out;
2068*1f5207b7SJohn Levon 	}
2069*1f5207b7SJohn Levon 	if (!handler(stream, line, token))	/* all set */
2070*1f5207b7SJohn Levon 		return;
2071*1f5207b7SJohn Levon 
2072*1f5207b7SJohn Levon out:
2073*1f5207b7SJohn Levon 	free_preprocessor_line(token);
2074*1f5207b7SJohn Levon }
2075*1f5207b7SJohn Levon 
2076*1f5207b7SJohn Levon static void preprocessor_line(struct stream *stream, struct token **line)
2077*1f5207b7SJohn Levon {
2078*1f5207b7SJohn Levon 	struct token *start = *line, *next;
2079*1f5207b7SJohn Levon 	struct token **tp = &start->next;
2080*1f5207b7SJohn Levon 
2081*1f5207b7SJohn Levon 	for (;;) {
2082*1f5207b7SJohn Levon 		next = *tp;
2083*1f5207b7SJohn Levon 		if (next->pos.newline)
2084*1f5207b7SJohn Levon 			break;
2085*1f5207b7SJohn Levon 		tp = &next->next;
2086*1f5207b7SJohn Levon 	}
2087*1f5207b7SJohn Levon 	*line = next;
2088*1f5207b7SJohn Levon 	*tp = &eof_token_entry;
2089*1f5207b7SJohn Levon 	handle_preprocessor_line(stream, line, start);
2090*1f5207b7SJohn Levon }
2091*1f5207b7SJohn Levon 
2092*1f5207b7SJohn Levon static void do_preprocess(struct token **list)
2093*1f5207b7SJohn Levon {
2094*1f5207b7SJohn Levon 	struct token *next;
2095*1f5207b7SJohn Levon 
2096*1f5207b7SJohn Levon 	while (!eof_token(next = scan_next(list))) {
2097*1f5207b7SJohn Levon 		struct stream *stream = input_streams + next->pos.stream;
2098*1f5207b7SJohn Levon 
2099*1f5207b7SJohn Levon 		if (next->pos.newline && match_op(next, '#')) {
2100*1f5207b7SJohn Levon 			if (!next->pos.noexpand) {
2101*1f5207b7SJohn Levon 				preprocessor_line(stream, list);
2102*1f5207b7SJohn Levon 				__free_token(next);	/* Free the '#' token */
2103*1f5207b7SJohn Levon 				continue;
2104*1f5207b7SJohn Levon 			}
2105*1f5207b7SJohn Levon 		}
2106*1f5207b7SJohn Levon 
2107*1f5207b7SJohn Levon 		switch (token_type(next)) {
2108*1f5207b7SJohn Levon 		case TOKEN_STREAMEND:
2109*1f5207b7SJohn Levon 			if (stream->top_if) {
2110*1f5207b7SJohn Levon 				nesting_error(stream);
2111*1f5207b7SJohn Levon 				sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
2112*1f5207b7SJohn Levon 				stream->top_if = NULL;
2113*1f5207b7SJohn Levon 				false_nesting = 0;
2114*1f5207b7SJohn Levon 			}
2115*1f5207b7SJohn Levon 			if (!stream->dirty)
2116*1f5207b7SJohn Levon 				stream->constant = CONSTANT_FILE_YES;
2117*1f5207b7SJohn Levon 			*list = next->next;
2118*1f5207b7SJohn Levon 			continue;
2119*1f5207b7SJohn Levon 		case TOKEN_STREAMBEGIN:
2120*1f5207b7SJohn Levon 			*list = next->next;
2121*1f5207b7SJohn Levon 			continue;
2122*1f5207b7SJohn Levon 
2123*1f5207b7SJohn Levon 		default:
2124*1f5207b7SJohn Levon 			dirty_stream(stream);
2125*1f5207b7SJohn Levon 			if (false_nesting) {
2126*1f5207b7SJohn Levon 				*list = next->next;
2127*1f5207b7SJohn Levon 				__free_token(next);
2128*1f5207b7SJohn Levon 				continue;
2129*1f5207b7SJohn Levon 			}
2130*1f5207b7SJohn Levon 
2131*1f5207b7SJohn Levon 			if (token_type(next) != TOKEN_IDENT ||
2132*1f5207b7SJohn Levon 			    expand_one_symbol(list))
2133*1f5207b7SJohn Levon 				list = &next->next;
2134*1f5207b7SJohn Levon 		}
2135*1f5207b7SJohn Levon 	}
2136*1f5207b7SJohn Levon }
2137*1f5207b7SJohn Levon 
2138*1f5207b7SJohn Levon void init_include_path(void)
2139*1f5207b7SJohn Levon {
2140*1f5207b7SJohn Levon 	FILE *fp;
2141*1f5207b7SJohn Levon 	char path[256];
2142*1f5207b7SJohn Levon 	char arch[32];
2143*1f5207b7SJohn Levon 	char os[32];
2144*1f5207b7SJohn Levon 
2145*1f5207b7SJohn Levon 	fp = popen("/bin/uname -m", "r");
2146*1f5207b7SJohn Levon 	if (!fp)
2147*1f5207b7SJohn Levon 		return;
2148*1f5207b7SJohn Levon 	if (!fgets(arch, sizeof(arch) - 1, fp))
2149*1f5207b7SJohn Levon 		return;
2150*1f5207b7SJohn Levon 	pclose(fp);
2151*1f5207b7SJohn Levon 	if (arch[strlen(arch) - 1] == '\n')
2152*1f5207b7SJohn Levon 		arch[strlen(arch) - 1] = '\0';
2153*1f5207b7SJohn Levon 
2154*1f5207b7SJohn Levon 	fp = popen("/bin/uname -o", "r");
2155*1f5207b7SJohn Levon 	if (!fp)
2156*1f5207b7SJohn Levon 		return;
2157*1f5207b7SJohn Levon 	fgets(os, sizeof(os) - 1, fp);
2158*1f5207b7SJohn Levon 	pclose(fp);
2159*1f5207b7SJohn Levon 
2160*1f5207b7SJohn Levon 	if (strcmp(os, "GNU/Linux\n") != 0)
2161*1f5207b7SJohn Levon 		return;
2162*1f5207b7SJohn Levon 	strcpy(os, "linux-gnu");
2163*1f5207b7SJohn Levon 
2164*1f5207b7SJohn Levon 	snprintf(path, sizeof(path), "/usr/include/%s-%s/", arch, os);
2165*1f5207b7SJohn Levon 	add_pre_buffer("#add_system \"%s/\"\n", path);
2166*1f5207b7SJohn Levon }
2167*1f5207b7SJohn Levon 
2168*1f5207b7SJohn Levon struct token * preprocess(struct token *token)
2169*1f5207b7SJohn Levon {
2170*1f5207b7SJohn Levon 	preprocessing = 1;
2171*1f5207b7SJohn Levon 	init_preprocessor();
2172*1f5207b7SJohn Levon 	do_preprocess(&token);
2173*1f5207b7SJohn Levon 
2174*1f5207b7SJohn Levon 	// Drop all expressions from preprocessing, they're not used any more.
2175*1f5207b7SJohn Levon 	// This is not true when we have multiple files, though ;/
2176*1f5207b7SJohn Levon 	// clear_expression_alloc();
2177*1f5207b7SJohn Levon 	preprocessing = 0;
2178*1f5207b7SJohn Levon 
2179*1f5207b7SJohn Levon 	return token;
2180*1f5207b7SJohn Levon }
2181*1f5207b7SJohn Levon 
2182*1f5207b7SJohn Levon static void dump_macro(struct symbol *sym)
2183*1f5207b7SJohn Levon {
2184*1f5207b7SJohn Levon 	int nargs = sym->arglist ? sym->arglist->count.normal : 0;
2185*1f5207b7SJohn Levon 	struct token *args[nargs];
2186*1f5207b7SJohn Levon 	struct token *token;
2187*1f5207b7SJohn Levon 
2188*1f5207b7SJohn Levon 	printf("#define %s", show_ident(sym->ident));
2189*1f5207b7SJohn Levon 	token = sym->arglist;
2190*1f5207b7SJohn Levon 	if (token) {
2191*1f5207b7SJohn Levon 		const char *sep = "";
2192*1f5207b7SJohn Levon 		int narg = 0;
2193*1f5207b7SJohn Levon 		putchar('(');
2194*1f5207b7SJohn Levon 		for (; !eof_token(token); token = token->next) {
2195*1f5207b7SJohn Levon 			if (token_type(token) == TOKEN_ARG_COUNT)
2196*1f5207b7SJohn Levon 				continue;
2197*1f5207b7SJohn Levon 			printf("%s%s", sep, show_token(token));
2198*1f5207b7SJohn Levon 			args[narg++] = token;
2199*1f5207b7SJohn Levon 			sep = ", ";
2200*1f5207b7SJohn Levon 		}
2201*1f5207b7SJohn Levon 		putchar(')');
2202*1f5207b7SJohn Levon 	}
2203*1f5207b7SJohn Levon 	putchar(' ');
2204*1f5207b7SJohn Levon 
2205*1f5207b7SJohn Levon 	token = sym->expansion;
2206*1f5207b7SJohn Levon 	while (!eof_token(token)) {
2207*1f5207b7SJohn Levon 		struct token *next = token->next;
2208*1f5207b7SJohn Levon 		switch (token_type(token)) {
2209*1f5207b7SJohn Levon 		case TOKEN_UNTAINT:
2210*1f5207b7SJohn Levon 			break;
2211*1f5207b7SJohn Levon 		case TOKEN_MACRO_ARGUMENT:
2212*1f5207b7SJohn Levon 			token = args[token->argnum];
2213*1f5207b7SJohn Levon 			/* fall-through */
2214*1f5207b7SJohn Levon 		default:
2215*1f5207b7SJohn Levon 			printf("%s", show_token(token));
2216*1f5207b7SJohn Levon 			if (next->pos.whitespace)
2217*1f5207b7SJohn Levon 				putchar(' ');
2218*1f5207b7SJohn Levon 		}
2219*1f5207b7SJohn Levon 		token = next;
2220*1f5207b7SJohn Levon 	}
2221*1f5207b7SJohn Levon 	putchar('\n');
2222*1f5207b7SJohn Levon }
2223*1f5207b7SJohn Levon 
2224*1f5207b7SJohn Levon void dump_macro_definitions(void)
2225*1f5207b7SJohn Levon {
2226*1f5207b7SJohn Levon 	struct ident *name;
2227*1f5207b7SJohn Levon 
2228*1f5207b7SJohn Levon 	FOR_EACH_PTR(macros, name) {
2229*1f5207b7SJohn Levon 		struct symbol *sym = lookup_macro(name);
2230*1f5207b7SJohn Levon 		if (sym)
2231*1f5207b7SJohn Levon 			dump_macro(sym);
2232*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(name);
2233*1f5207b7SJohn Levon }
2234