Lines Matching refs:token

95 static struct token *alloc_token(struct position *pos)  in alloc_token()
97 struct token *token = __alloc_token(0); in alloc_token() local
99 token->pos.stream = pos->stream; in alloc_token()
100 token->pos.line = pos->line; in alloc_token()
101 token->pos.pos = pos->pos; in alloc_token()
102 token->pos.whitespace = 1; in alloc_token()
103 return token; in alloc_token()
107 static int expand(struct token **, struct symbol *);
109 static void replace_with_string(struct token *token, const char *str) in replace_with_string() argument
116 token_type(token) = TOKEN_STRING; in replace_with_string()
117 token->string = s; in replace_with_string()
120 static void replace_with_integer(struct token *token, unsigned int val) in replace_with_integer() argument
124 token_type(token) = TOKEN_NUMBER; in replace_with_integer()
125 token->number = buf; in replace_with_integer()
136 static int token_defined(struct token *token) in token_defined() argument
138 if (token_type(token) == TOKEN_IDENT) { in token_defined()
139 struct symbol *sym = lookup_macro(token->ident); in token_defined()
147 sparse_error(token->pos, "expected preprocessor identifier"); in token_defined()
151 static void replace_with_bool(struct token *token, bool val) in replace_with_bool() argument
155 token_type(token) = TOKEN_NUMBER; in replace_with_bool()
156 token->number = string[val]; in replace_with_bool()
159 static void replace_with_defined(struct token *token) in replace_with_defined() argument
161 replace_with_bool(token, token_defined(token)); in replace_with_defined()
164 static void replace_with_has_builtin(struct token *token) in replace_with_has_builtin() argument
166 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL); in replace_with_has_builtin()
167 replace_with_bool(token, sym && sym->builtin); in replace_with_has_builtin()
170 static void replace_with_has_attribute(struct token *token) in replace_with_has_attribute() argument
172 struct symbol *sym = lookup_symbol(token->ident, NS_KEYWORD); in replace_with_has_attribute()
173 replace_with_bool(token, sym && sym->op && sym->op->attribute); in replace_with_has_attribute()
176 static void expand_line(struct token *token) in expand_line() argument
178 replace_with_integer(token, token->pos.line); in expand_line()
181 static void expand_file(struct token *token) in expand_file() argument
183 replace_with_string(token, stream_name(token->pos.stream)); in expand_file()
186 static void expand_basefile(struct token *token) in expand_basefile() argument
188 replace_with_string(token, base_filename); in expand_basefile()
192 static void expand_date(struct token *token) in expand_date() argument
199 replace_with_string(token, buffer); in expand_date()
202 static void expand_time(struct token *token) in expand_time() argument
209 replace_with_string(token, buffer); in expand_time()
212 static void expand_counter(struct token *token) in expand_counter() argument
214 replace_with_integer(token, counter_macro++); in expand_counter()
217 static void expand_include_level(struct token *token) in expand_include_level() argument
219 replace_with_integer(token, include_level - 1); in expand_include_level()
222 static int expand_one_symbol(struct token **list) in expand_one_symbol()
224 struct token *token = *list; in expand_one_symbol() local
227 if (token->pos.noexpand) in expand_one_symbol()
230 sym = lookup_macro(token->ident); in expand_one_symbol()
233 store_macro_pos(token); in expand_one_symbol()
235 sym->expander(token); in expand_one_symbol()
243 static inline struct token *scan_next(struct token **where) in scan_next()
245 struct token *token = *where; in scan_next() local
246 if (token_type(token) != TOKEN_UNTAINT) in scan_next()
247 return token; in scan_next()
249 token->ident->tainted = 0; in scan_next()
250 token = token->next; in scan_next()
251 } while (token_type(token) == TOKEN_UNTAINT); in scan_next()
252 *where = token; in scan_next()
253 return token; in scan_next()
256 static void expand_list(struct token **list) in expand_list()
258 struct token *next; in expand_list()
265 static void preprocessor_line(struct stream *stream, struct token **line);
267 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count) in collect_arg()
270 struct token **p = &prev->next; in collect_arg()
271 struct token *next; in collect_arg()
322 struct token *arg;
323 struct token *expanded;
324 struct token *str;
330 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct t… in collect_arguments()
333 struct token *next = NULL; in collect_arguments()
404 static struct token *dup_list(struct token *list) in dup_list()
406 struct token *res = NULL; in dup_list()
407 struct token **p = &res; in dup_list()
410 struct token *newtok = __alloc_token(0); in dup_list()
419 static const char *show_token_sequence(struct token *token, int quote) in show_token_sequence() argument
425 if (!token && !quote) in show_token_sequence()
427 while (!eof_token(token)) { in show_token_sequence()
428 const char *val = quote ? quote_token(token) : show_token(token); in show_token_sequence()
432 sparse_error(token->pos, "too long token expansion"); in show_token_sequence()
440 token = token->next; in show_token_sequence()
441 whitespace = token->pos.whitespace; in show_token_sequence()
447 static struct token *stringify(struct token *arg) in stringify()
451 struct token *token = __alloc_token(0); in stringify() local
456 token->pos = arg->pos; in stringify()
457 token_type(token) = TOKEN_STRING; in stringify()
458 token->string = string; in stringify()
459 token->next = &eof_token_entry; in stringify()
460 return token; in stringify()
467 struct token *arg = args[i].arg; in expand_arguments()
499 static enum token_type combine(struct token *left, struct token *right, char *p) in combine()
553 static int merge(struct token *left, struct token *right) in merge()
601 static struct token *dup_token(struct token *token, struct position *streampos) in dup_token() argument
603 struct token *alloc = alloc_token(streampos); in dup_token()
604 token_type(alloc) = token_type(token); in dup_token()
605 alloc->pos.newline = token->pos.newline; in dup_token()
606 alloc->pos.whitespace = token->pos.whitespace; in dup_token()
607 alloc->number = token->number; in dup_token()
608 alloc->pos.noexpand = token->pos.noexpand; in dup_token()
612 static struct token **copy(struct token **where, struct token *list, int *count) in copy()
616 struct token *token; in copy() local
618 token = dup_token(list, &list->pos); in copy()
620 token = list; in copy()
621 if (token_type(token) == TOKEN_IDENT && token->ident->tainted) in copy()
622 token->pos.noexpand = 1; in copy()
623 *where = token; in copy()
624 where = &token->next; in copy()
631 static int handle_kludge(struct token **p, struct arg *args) in handle_kludge()
633 struct token *t = (*p)->next->next; in handle_kludge()
652 static struct token **substitute(struct token **list, struct token *body, struct arg *args) in substitute()
659 struct token *added, *arg; in substitute()
660 struct token **tail; in substitute()
661 struct token *t; in substitute()
755 static int expand(struct token **list, struct symbol *sym) in expand()
757 struct token *last; in expand()
758 struct token *token = *list; in expand() local
759 struct ident *expanding = token->ident; in expand()
760 struct token **tail; in expand()
765 token->pos.noexpand = 1; in expand()
770 if (!match_op(scan_next(&token->next), '(')) in expand()
772 if (!collect_arguments(token->next, sym->arglist, args, token)) in expand()
779 last = token->next; in expand()
787 (*list)->pos.newline = token->pos.newline; in expand()
788 (*list)->pos.whitespace = token->pos.whitespace; in expand()
794 static const char *token_name_sequence(struct token *token, int endop, struct token *start) in token_name_sequence() argument
799 while (!eof_token(token) && !match_op(token, endop)) { in token_name_sequence()
801 const char *val = token->string->data; in token_name_sequence()
802 if (token_type(token) != TOKEN_STRING) in token_name_sequence()
803 val = show_token(token); in token_name_sequence()
807 token = token->next; in token_name_sequence()
810 if (endop && !match_op(token, endop)) in token_name_sequence()
903 static int try_include(const char *path, const char *filename, int flen, struct token **where, cons… in try_include()
927 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char … in do_include_path() argument
939 static int free_preprocessor_line(struct token *token) in free_preprocessor_line() argument
941 while (token_type(token) != TOKEN_EOF) { in free_preprocessor_line()
942 struct token *free = token; in free_preprocessor_line()
943 token = token->next; in free_preprocessor_line()
1031 static void use_best_guess_header_file(struct token *token, const char *filename, struct token **li… in use_best_guess_header_file() argument
1054 snprintf(dir_part, sizeof(dir_part), "%s", stream_name(token->pos.stream)); in use_best_guess_header_file()
1073 sparse_error(token->pos, "using '%s'", include_name); in use_best_guess_header_file()
1078 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int… in handle_include_path() argument
1081 struct token *next; in handle_include_path()
1086 next = token->next; in handle_include_path()
1089 expand_list(&token->next); in handle_include_path()
1091 next = token; in handle_include_path()
1092 if (match_op(token->next, '<')) { in handle_include_path()
1093 next = token->next; in handle_include_path()
1098 token = next->next; in handle_include_path()
1099 filename = token_name_sequence(token, expect, token); in handle_include_path()
1124 if (do_include_path(path, list, token, filename, flen)) in handle_include_path()
1127 sparse_error(token->pos, "unable to open '%s'", filename); in handle_include_path()
1128 use_best_guess_header_file(token, filename, list); in handle_include_path()
1132 static int handle_include(struct stream *stream, struct token **list, struct token *token) in handle_include() argument
1134 return handle_include_path(stream, list, token, 0); in handle_include()
1137 static int handle_include_next(struct stream *stream, struct token **list, struct token *token) in handle_include_next() argument
1139 return handle_include_path(stream, list, token, 1); in handle_include_next()
1142 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token) in handle_argv_include() argument
1144 return handle_include_path(stream, list, token, 2); in handle_argv_include()
1147 static int token_different(struct token *t1, struct token *t2) in token_different()
1200 static int token_list_different(struct token *list1, struct token *list2) in token_list_different()
1214 static inline void set_arg_count(struct token *token) in set_arg_count() argument
1216 token_type(token) = TOKEN_ARG_COUNT; in set_arg_count()
1217 token->count.normal = token->count.quoted = in set_arg_count()
1218 token->count.str = token->count.vararg = 0; in set_arg_count()
1221 static struct token *parse_arguments(struct token *list) in parse_arguments()
1223 struct token *arg = list->next, *next = list; in parse_arguments()
1320 static int try_arg(struct token *token, enum token_type type, struct token *arglist) in try_arg() argument
1322 struct ident *ident = token->ident; in try_arg()
1325 if (!arglist || token_type(token) != TOKEN_IDENT) in try_arg()
1335 token->argnum = nr; in try_arg()
1336 token_type(token) = type; in try_arg()
1353 token_type(token) = TOKEN_ERROR; in try_arg()
1360 static struct token *handle_hash(struct token **p, struct token *arglist) in handle_hash()
1362 struct token *token = *p; in handle_hash() local
1364 struct token *next = token->next; in handle_hash()
1367 next->pos.whitespace = token->pos.whitespace; in handle_hash()
1368 __free_token(token); in handle_hash()
1369 token = *p = next; in handle_hash()
1371 token->pos.noexpand = 1; in handle_hash()
1373 return token; in handle_hash()
1376 sparse_error(token->pos, "'#' is not followed by a macro parameter"); in handle_hash()
1381 static struct token *handle_hashhash(struct token *token, struct token *arglist) in handle_hashhash() argument
1383 struct token *last = token; in handle_hashhash()
1384 struct token *concat; in handle_hashhash()
1385 int state = match_op(token, ','); in handle_hashhash()
1387 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist); in handle_hashhash()
1390 struct token *t; in handle_hashhash()
1394 concat = token->next; in handle_hashhash()
1396 token->next = t; in handle_hashhash()
1420 token = t; in handle_hashhash()
1421 if (!match_op(token->next, SPECIAL_HASHHASH)) in handle_hashhash()
1427 return token; in handle_hashhash()
1434 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *… in parse_expansion()
1436 struct token *token = expansion; in parse_expansion() local
1437 struct token **p; in parse_expansion()
1439 if (match_op(token, SPECIAL_HASHHASH)) in parse_expansion()
1442 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) { in parse_expansion()
1443 if (match_op(token, '#')) { in parse_expansion()
1444 token = handle_hash(p, arglist); in parse_expansion()
1445 if (!token) in parse_expansion()
1448 if (match_op(token->next, SPECIAL_HASHHASH)) { in parse_expansion()
1449 token = handle_hashhash(token, arglist); in parse_expansion()
1450 if (!token) in parse_expansion()
1453 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist); in parse_expansion()
1455 switch (token_type(token)) { in parse_expansion()
1461 token->string->immutable = 1; in parse_expansion()
1465 token = alloc_token(&expansion->pos); in parse_expansion()
1466 token_type(token) = TOKEN_UNTAINT; in parse_expansion()
1467 token->ident = name; in parse_expansion()
1468 token->next = *p; in parse_expansion()
1469 *p = token; in parse_expansion()
1473 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion"); in parse_expansion()
1476 sparse_error(token->pos, "too many instances of argument in body"); in parse_expansion()
1480 static int do_define(struct position pos, struct token *token, struct ident *name, in do_define() argument
1481 struct token *arglist, struct token *expansion, int attr) in do_define()
1522 if (token) /* Free the "define" token, but not the rest of the line */ in do_define()
1523 __free_token(token); in do_define()
1545 struct token *value = &eof_token_entry; in predefine()
1579 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int at… in do_handle_define() argument
1581 struct token *arglist, *expansion; in do_handle_define()
1582 struct token *left = token->next; in do_handle_define()
1586 sparse_error(token->pos, "expected identifier to 'define'"); in do_handle_define()
1606 return do_define(left->pos, token, name, arglist, expansion, attr); in do_handle_define()
1609 static int handle_define(struct stream *stream, struct token **line, struct token *token) in handle_define() argument
1611 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL); in handle_define()
1614 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token) in handle_weak_define() argument
1616 return do_handle_define(stream, line, token, SYM_ATTR_WEAK); in handle_weak_define()
1619 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token) in handle_strong_define() argument
1621 return do_handle_define(stream, line, token, SYM_ATTR_STRONG); in handle_strong_define()
1624 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int att… in do_handle_undef() argument
1626 struct token *left = token->next; in do_handle_undef()
1630 sparse_error(token->pos, "expected identifier to 'undef'"); in do_handle_undef()
1655 static int handle_undef(struct stream *stream, struct token **line, struct token *token) in handle_undef() argument
1657 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL); in handle_undef()
1660 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token) in handle_strong_undef() argument
1662 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG); in handle_strong_undef()
1665 static int preprocessor_if(struct stream *stream, struct token *token, int cond) in preprocessor_if() argument
1667 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF; in preprocessor_if()
1668 free_preprocessor_line(token->next); in preprocessor_if()
1669 token->next = stream->top_if; in preprocessor_if()
1670 stream->top_if = token; in preprocessor_if()
1676 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token) in handle_ifdef() argument
1678 struct token *next = token->next; in handle_ifdef()
1685 sparse_error(token->pos, "expected preprocessor identifier"); in handle_ifdef()
1688 return preprocessor_if(stream, token, arg); in handle_ifdef()
1691 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token) in handle_ifndef() argument
1693 struct token *next = token->next; in handle_ifndef()
1698 stream->ifndef = token; in handle_ifndef()
1701 stream->ifndef = token; in handle_ifndef()
1709 sparse_error(token->pos, "expected preprocessor identifier"); in handle_ifndef()
1713 return preprocessor_if(stream, token, arg); in handle_ifndef()
1716 static const char *show_token_sequence(struct token *token, int quote);
1722 static int expression_value(struct token **where) in expression_value()
1725 struct token *p; in expression_value()
1726 struct token **list = where, **beginning = NULL; in expression_value()
1820 static int handle_if(struct stream *stream, struct token **line, struct token *token) in handle_if() argument
1824 value = expression_value(&token->next); in handle_if()
1827 return preprocessor_if(stream, token, value); in handle_if()
1830 static int handle_elif(struct stream * stream, struct token **line, struct token *token) in handle_elif() argument
1832 struct token *top_if = stream->top_if; in handle_elif()
1837 sparse_error(token->pos, "unmatched #elif within stream"); in handle_elif()
1843 sparse_error(token->pos, "#elif after #else"); in handle_elif()
1854 if (!expression_value(&token->next)) in handle_elif()
1863 static int handle_else(struct stream *stream, struct token **line, struct token *token) in handle_else() argument
1865 struct token *top_if = stream->top_if; in handle_else()
1870 sparse_error(token->pos, "unmatched #else within stream"); in handle_else()
1876 sparse_error(token->pos, "#else after #else"); in handle_else()
1888 static int handle_endif(struct stream *stream, struct token **line, struct token *token) in handle_endif() argument
1890 struct token *top_if = stream->top_if; in handle_endif()
1894 sparse_error(token->pos, "unmatched #endif in stream"); in handle_endif()
1904 static int handle_warning(struct stream *stream, struct token **line, struct token *token) in handle_warning() argument
1906 warning(token->pos, "%s", show_token_sequence(token->next, 0)); in handle_warning()
1910 static int handle_error(struct stream *stream, struct token **line, struct token *token) in handle_error() argument
1912 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0)); in handle_error()
1916 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token) in handle_nostdinc() argument
1959 static void add_path_entry(struct token *token, const char *path, in add_path_entry() argument
1967 error_die(token->pos, "too many include path entries"); in add_path_entry()
1993 static int handle_add_include(struct stream *stream, struct token **line, struct token *token) in handle_add_include() argument
1996 token = token->next; in handle_add_include()
1997 if (eof_token(token)) in handle_add_include()
1999 if (token_type(token) != TOKEN_STRING) { in handle_add_include()
2000 warning(token->pos, "expected path string"); in handle_add_include()
2003 add_path_entry(token, token->string->data, &isys_includepath); in handle_add_include()
2007 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token) in handle_add_isystem() argument
2010 token = token->next; in handle_add_isystem()
2011 if (eof_token(token)) in handle_add_isystem()
2013 if (token_type(token) != TOKEN_STRING) { in handle_add_isystem()
2014 sparse_error(token->pos, "expected path string"); in handle_add_isystem()
2017 add_path_entry(token, token->string->data, &sys_includepath); in handle_add_isystem()
2021 static int handle_add_system(struct stream *stream, struct token **line, struct token *token) in handle_add_system() argument
2024 token = token->next; in handle_add_system()
2025 if (eof_token(token)) in handle_add_system()
2027 if (token_type(token) != TOKEN_STRING) { in handle_add_system()
2028 sparse_error(token->pos, "expected path string"); in handle_add_system()
2031 add_path_entry(token, token->string->data, &dirafter_includepath); in handle_add_system()
2036 static void add_dirafter_entry(struct token *token, const char *path) in add_dirafter_entry() argument
2042 error_die(token->pos, "too many include path entries"); in add_dirafter_entry()
2052 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token) in handle_add_dirafter() argument
2055 token = token->next; in handle_add_dirafter()
2056 if (eof_token(token)) in handle_add_dirafter()
2058 if (token_type(token) != TOKEN_STRING) { in handle_add_dirafter()
2059 sparse_error(token->pos, "expected path string"); in handle_add_dirafter()
2062 add_dirafter_entry(token, token->string->data); in handle_add_dirafter()
2066 static int handle_split_include(struct stream *stream, struct token **line, struct token *token) in handle_split_include() argument
2097 static int handle_pragma(struct stream *stream, struct token **line, struct token *token) in handle_pragma() argument
2099 struct token *next = *line; in handle_pragma()
2101 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) { in handle_pragma()
2105 token->ident = &pragma_ident; in handle_pragma()
2106 token->pos.newline = 1; in handle_pragma()
2107 token->pos.whitespace = 1; in handle_pragma()
2108 token->pos.pos = 1; in handle_pragma()
2109 *line = token; in handle_pragma()
2110 token->next = next; in handle_pragma()
2117 static int handle_line(struct stream *stream, struct token **line, struct token *token) in handle_line() argument
2122 static int handle_ident(struct stream *stream, struct token **line, struct token *token) in handle_ident() argument
2127 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token) in handle_nondirective() argument
2129 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0)); in handle_nondirective()
2140 int (*handler)(struct stream *, struct token **, struct token *); in init_preprocessor()
2173 void (*expander)(struct token *); in init_preprocessor()
2205 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *star… in handle_preprocessor_line()
2207 int (*handler)(struct stream *, struct token **, struct token *); in handle_preprocessor_line()
2208 struct token *token = start->next; in handle_preprocessor_line() local
2211 if (eof_token(token)) in handle_preprocessor_line()
2214 if (token_type(token) == TOKEN_IDENT) { in handle_preprocessor_line()
2215 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR); in handle_preprocessor_line()
2222 } else if (token_type(token) == TOKEN_NUMBER) { in handle_preprocessor_line()
2233 if (!handler(stream, line, token)) /* all set */ in handle_preprocessor_line()
2237 free_preprocessor_line(token); in handle_preprocessor_line()
2240 static void preprocessor_line(struct stream *stream, struct token **line) in preprocessor_line()
2242 struct token *start = *line, *next; in preprocessor_line()
2243 struct token **tp = &start->next; in preprocessor_line()
2256 static void do_preprocess(struct token **list) in do_preprocess()
2258 struct token *next; in do_preprocess()
2334 struct token * preprocess(struct token *token) in preprocess() argument
2338 do_preprocess(&token); in preprocess()
2345 return token; in preprocess()
2348 static int is_VA_ARGS_token(struct token *token) in is_VA_ARGS_token() argument
2350 return (token_type(token) == TOKEN_IDENT) && in is_VA_ARGS_token()
2351 (token->ident == &__VA_ARGS___ident); in is_VA_ARGS_token()
2357 struct token *args[nargs]; in dump_macro()
2358 struct token *token; in dump_macro() local
2361 token = sym->arglist; in dump_macro()
2362 if (token) { in dump_macro()
2366 for (; !eof_token(token); token = token->next) { in dump_macro()
2367 if (token_type(token) == TOKEN_ARG_COUNT) in dump_macro()
2369 if (is_VA_ARGS_token(token)) in dump_macro()
2372 printf("%s%s", sep, show_token(token)); in dump_macro()
2373 args[narg++] = token; in dump_macro()
2379 token = sym->expansion; in dump_macro()
2380 while (token_type(token) != TOKEN_UNTAINT) { in dump_macro()
2381 struct token *next = token->next; in dump_macro()
2382 if (token->pos.whitespace) in dump_macro()
2384 switch (token_type(token)) { in dump_macro()
2393 token = args[token->argnum]; in dump_macro()
2396 printf("%s", show_token(token)); in dump_macro()
2398 token = next; in dump_macro()