Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 1 | /* CPP Library. (Directive handling.) |
Jeff Law | 5e7b4e2 | 2000-02-25 22:59:31 -0700 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 4 | 2007, 2008, 2009 Free Software Foundation, Inc. |
Richard Kenner | 4c8cc61 | 1997-02-16 08:08:25 -0500 | [diff] [blame] | 5 | Contributed by Per Bothner, 1994-95. |
Richard Kenner | d8bfa78 | 1997-01-03 08:19:34 -0500 | [diff] [blame] | 6 | Based on CCCP program by Paul Rubin, June 1986 |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 7 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it |
| 10 | under the terms of the GNU General Public License as published by the |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 11 | Free Software Foundation; either version 3, or (at your option) any |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 20 | along with this program; see the file COPYING3. If not see |
| 21 | <http://www.gnu.org/licenses/>. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 22 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 23 | #include "config.h" |
Kaveh R. Ghazi | b04cd507 | 1998-03-30 12:05:54 +0000 | [diff] [blame] | 24 | #include "system.h" |
Jeff Law | 956d695 | 1997-12-06 17:31:01 -0700 | [diff] [blame] | 25 | #include "cpplib.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 26 | #include "internal.h" |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 27 | #include "mkdeps.h" |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 28 | #include "obstack.h" |
Jeff Law | 956d695 | 1997-12-06 17:31:01 -0700 | [diff] [blame] | 29 | |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 30 | /* Stack of conditionals currently in progress |
| 31 | (including both successful and failing conditionals). */ |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 32 | struct if_stack |
| 33 | { |
| 34 | struct if_stack *next; |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 35 | linenum_type line; /* Line where condition started. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 36 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 37 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
| 38 | bool was_skipping; /* If were skipping on entry. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 39 | int type; /* Most recent conditional for diagnostics. */ |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 40 | }; |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 41 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 42 | /* Contains a registered pragma or pragma namespace. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 43 | typedef void (*pragma_cb) (cpp_reader *); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 44 | struct pragma_entry |
| 45 | { |
| 46 | struct pragma_entry *next; |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 47 | const cpp_hashnode *pragma; /* Name and length. */ |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 48 | bool is_nspace; |
| 49 | bool is_internal; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 50 | bool is_deferred; |
| 51 | bool allow_expansion; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 52 | union { |
| 53 | pragma_cb handler; |
| 54 | struct pragma_entry *space; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 55 | unsigned int ident; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 56 | } u; |
| 57 | }; |
| 58 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 59 | /* Values for the origin field of struct directive. KANDR directives |
| 60 | come from traditional (K&R) C. STDC89 directives come from the |
| 61 | 1989 C standard. EXTENSION directives are extensions. */ |
| 62 | #define KANDR 0 |
| 63 | #define STDC89 1 |
| 64 | #define EXTENSION 2 |
| 65 | |
| 66 | /* Values for the flags field of struct directive. COND indicates a |
| 67 | conditional; IF_COND an opening conditional. INCL means to treat |
| 68 | "..." and <...> as q-char and h-char sequences respectively. IN_I |
| 69 | means this directive should be handled even if -fpreprocessed is in |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 70 | effect (these are the directives with callback hooks). |
| 71 | |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 72 | EXPAND is set on directives that are always macro-expanded. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 73 | #define COND (1 << 0) |
| 74 | #define IF_COND (1 << 1) |
| 75 | #define INCL (1 << 2) |
| 76 | #define IN_I (1 << 3) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 77 | #define EXPAND (1 << 4) |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 78 | #define DEPRECATED (1 << 5) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 79 | |
| 80 | /* Defines one #-directive, including how to handle it. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 81 | typedef void (*directive_handler) (cpp_reader *); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 82 | typedef struct directive directive; |
| 83 | struct directive |
| 84 | { |
| 85 | directive_handler handler; /* Function to handle directive. */ |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 86 | const uchar *name; /* Name of directive. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 87 | unsigned short length; /* Length of name. */ |
| 88 | unsigned char origin; /* Origin of directive. */ |
| 89 | unsigned char flags; /* Flags describing this directive. */ |
| 90 | }; |
| 91 | |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 92 | /* Forward declarations. */ |
| 93 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 94 | static void skip_rest_of_line (cpp_reader *); |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 95 | static void check_eol (cpp_reader *, bool); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 96 | static void start_directive (cpp_reader *); |
| 97 | static void prepare_directive_trad (cpp_reader *); |
| 98 | static void end_directive (cpp_reader *, int); |
| 99 | static void directive_diagnostics (cpp_reader *, const directive *, int); |
| 100 | static void run_directive (cpp_reader *, int, const char *, size_t); |
| 101 | static char *glue_header_name (cpp_reader *); |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 102 | static const char *parse_include (cpp_reader *, int *, const cpp_token ***, |
| 103 | source_location *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 104 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); |
| 105 | static unsigned int read_flag (cpp_reader *, unsigned int); |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 106 | static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 107 | static void do_diagnostic (cpp_reader *, int, int); |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 108 | static cpp_hashnode *lex_macro_node (cpp_reader *, bool); |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 109 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 110 | static void do_include_common (cpp_reader *, enum include_type); |
| 111 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, |
| 112 | const cpp_hashnode *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 113 | static int count_registered_pragmas (struct pragma_entry *); |
| 114 | static char ** save_registered_pragmas (struct pragma_entry *, char **); |
| 115 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, |
| 116 | char **); |
| 117 | static void do_pragma_once (cpp_reader *); |
| 118 | static void do_pragma_poison (cpp_reader *); |
| 119 | static void do_pragma_system_header (cpp_reader *); |
| 120 | static void do_pragma_dependency (cpp_reader *); |
| 121 | static void do_linemarker (cpp_reader *); |
| 122 | static const cpp_token *get_token_no_padding (cpp_reader *); |
| 123 | static const cpp_token *get__Pragma_string (cpp_reader *); |
| 124 | static void destringize_and_run (cpp_reader *, const cpp_string *); |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 125 | static int parse_answer (cpp_reader *, struct answer **, int, source_location); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 126 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); |
| 127 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); |
| 128 | static void handle_assertion (cpp_reader *, const char *, int); |
Kaveh R. Ghazi | 487a6e0 | 1998-05-19 08:42:48 +0000 | [diff] [blame] | 129 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 130 | /* This is the table of directive handlers. It is ordered by |
| 131 | frequency of occurrence; the numbers at the end are directive |
| 132 | counts from all the source code I have lying around (egcs and libc |
| 133 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 134 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 135 | is now O(1). All extensions other than #warning, #include_next, |
| 136 | and #import are deprecated. The name is where the extension |
| 137 | appears to have come from. */ |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 138 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 139 | #define DIRECTIVE_TABLE \ |
| 140 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 141 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 142 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
| 143 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 144 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 145 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
| 146 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ |
| 147 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 148 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
| 149 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 150 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
| 151 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ |
| 152 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 153 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
Neil Vachharajani | 2214382 | 2009-10-10 00:34:21 +0000 | [diff] [blame] | 154 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 155 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 156 | D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \ |
| 157 | D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \ |
Neil Vachharajani | 2214382 | 2009-10-10 00:34:21 +0000 | [diff] [blame] | 158 | D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ |
Zack Weinberg | 1ed17cd | 2005-05-12 18:31:38 +0000 | [diff] [blame] | 159 | |
| 160 | /* #sccs is synonymous with #ident. */ |
| 161 | #define do_sccs do_ident |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 162 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 163 | /* Use the table to generate a series of prototypes, an enum for the |
| 164 | directive names, and an array of directive handlers. */ |
| 165 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 166 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 167 | DIRECTIVE_TABLE |
| 168 | #undef D |
| 169 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 170 | #define D(n, tag, o, f) tag, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 171 | enum |
| 172 | { |
| 173 | DIRECTIVE_TABLE |
| 174 | N_DIRECTIVES |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 175 | }; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 176 | #undef D |
| 177 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 178 | #define D(name, t, origin, flags) \ |
Kaveh R. Ghazi | 9a23858 | 2003-06-16 19:14:22 +0000 | [diff] [blame] | 179 | { do_##name, (const uchar *) #name, \ |
| 180 | sizeof #name - 1, origin, flags }, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 181 | static const directive dtable[] = |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 182 | { |
| 183 | DIRECTIVE_TABLE |
| 184 | }; |
| 185 | #undef D |
| 186 | #undef DIRECTIVE_TABLE |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 187 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 188 | /* Wrapper struct directive for linemarkers. |
| 189 | The origin is more or less true - the original K+R cpp |
| 190 | did use this notation in its preprocessed output. */ |
| 191 | static const directive linemarker_dir = |
| 192 | { |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 193 | do_linemarker, UC"#", 1, KANDR, IN_I |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 196 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 197 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 198 | /* Skip any remaining tokens in a directive. */ |
| 199 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 200 | skip_rest_of_line (cpp_reader *pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 201 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 202 | /* Discard all stacked contexts. */ |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 203 | while (pfile->context->prev) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 204 | _cpp_pop_context (pfile); |
| 205 | |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 206 | /* Sweep up all tokens remaining on the line. */ |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 207 | if (! SEEN_EOL ()) |
| 208 | while (_cpp_lex_token (pfile)->type != CPP_EOF) |
| 209 | ; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 212 | /* Ensure there are no stray tokens at the end of a directive. If |
| 213 | EXPAND is true, tokens macro-expanding to nothing are allowed. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 214 | static void |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 215 | check_eol (cpp_reader *pfile, bool expand) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 216 | { |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 217 | if (! SEEN_EOL () && (expand |
| 218 | ? cpp_get_token (pfile) |
| 219 | : _cpp_lex_token (pfile))->type != CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 220 | cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 221 | pfile->directive->name); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 224 | /* Ensure there are no stray tokens other than comments at the end of |
| 225 | a directive, and gather the comments. */ |
| 226 | static const cpp_token ** |
| 227 | check_eol_return_comments (cpp_reader *pfile) |
| 228 | { |
| 229 | size_t c; |
| 230 | size_t capacity = 8; |
| 231 | const cpp_token **buf; |
| 232 | |
| 233 | buf = XNEWVEC (const cpp_token *, capacity); |
| 234 | c = 0; |
| 235 | if (! SEEN_EOL ()) |
| 236 | { |
| 237 | while (1) |
| 238 | { |
| 239 | const cpp_token *tok; |
| 240 | |
| 241 | tok = _cpp_lex_token (pfile); |
| 242 | if (tok->type == CPP_EOF) |
| 243 | break; |
| 244 | if (tok->type != CPP_COMMENT) |
| 245 | cpp_error (pfile, CPP_DL_PEDWARN, |
| 246 | "extra tokens at end of #%s directive", |
| 247 | pfile->directive->name); |
| 248 | else |
| 249 | { |
| 250 | if (c + 1 >= capacity) |
| 251 | { |
| 252 | capacity *= 2; |
| 253 | buf = XRESIZEVEC (const cpp_token *, buf, capacity); |
| 254 | } |
| 255 | buf[c] = tok; |
| 256 | ++c; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | buf[c] = NULL; |
| 261 | return buf; |
| 262 | } |
| 263 | |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 264 | /* Called when entering a directive, _Pragma or command-line directive. */ |
| 265 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 266 | start_directive (cpp_reader *pfile) |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 267 | { |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 268 | /* Setup in-directive state. */ |
| 269 | pfile->state.in_directive = 1; |
| 270 | pfile->state.save_comments = 0; |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 271 | pfile->directive_result.type = CPP_PADDING; |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 272 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 273 | /* Some handlers need the position of the # for diagnostics. */ |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 274 | pfile->directive_line = pfile->line_table->highest_line; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* Called when leaving a directive, _Pragma or command-line directive. */ |
| 278 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 279 | end_directive (cpp_reader *pfile, int skip_line) |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 280 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 281 | if (pfile->state.in_deferred_pragma) |
| 282 | ; |
| 283 | else if (CPP_OPTION (pfile, traditional)) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 284 | { |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 285 | /* Revert change of prepare_directive_trad. */ |
| 286 | pfile->state.prevent_expansion--; |
| 287 | |
Neil Booth | b66377c | 2002-06-13 21:16:00 +0000 | [diff] [blame] | 288 | if (pfile->directive != &dtable[T_DEFINE]) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 289 | _cpp_remove_overlay (pfile); |
| 290 | } |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 291 | /* We don't skip for an assembler #. */ |
Neil Booth | b66377c | 2002-06-13 21:16:00 +0000 | [diff] [blame] | 292 | else if (skip_line) |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 293 | { |
| 294 | skip_rest_of_line (pfile); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 295 | if (!pfile->keep_tokens) |
| 296 | { |
| 297 | pfile->cur_run = &pfile->base_run; |
| 298 | pfile->cur_token = pfile->base_run.base; |
| 299 | } |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 300 | } |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 301 | |
| 302 | /* Restore state. */ |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 303 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
| 304 | pfile->state.in_directive = 0; |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 305 | pfile->state.in_expression = 0; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 306 | pfile->state.angled_headers = 0; |
| 307 | pfile->directive = 0; |
| 308 | } |
| 309 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 310 | /* Prepare to handle the directive in pfile->directive. */ |
| 311 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 312 | prepare_directive_trad (cpp_reader *pfile) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 313 | { |
Neil Booth | 951a076 | 2002-06-27 06:01:58 +0000 | [diff] [blame] | 314 | if (pfile->directive != &dtable[T_DEFINE]) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 315 | { |
Neil Booth | b66377c | 2002-06-13 21:16:00 +0000 | [diff] [blame] | 316 | bool no_expand = (pfile->directive |
| 317 | && ! (pfile->directive->flags & EXPAND)); |
Neil Booth | 974c43f | 2002-06-13 06:25:28 +0000 | [diff] [blame] | 318 | bool was_skipping = pfile->state.skipping; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 319 | |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 320 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
| 321 | || pfile->directive == &dtable[T_ELIF]); |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 322 | if (pfile->state.in_expression) |
| 323 | pfile->state.skipping = false; |
| 324 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 325 | if (no_expand) |
| 326 | pfile->state.prevent_expansion++; |
Zack Weinberg | 4383964 | 2003-07-13 17:34:18 +0000 | [diff] [blame] | 327 | _cpp_scan_out_logical_line (pfile, NULL); |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 328 | if (no_expand) |
| 329 | pfile->state.prevent_expansion--; |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 330 | |
Neil Booth | 974c43f | 2002-06-13 06:25:28 +0000 | [diff] [blame] | 331 | pfile->state.skipping = was_skipping; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 332 | _cpp_overlay_buffer (pfile, pfile->out.base, |
| 333 | pfile->out.cur - pfile->out.base); |
| 334 | } |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 335 | |
| 336 | /* Stop ISO C from expanding anything. */ |
| 337 | pfile->state.prevent_expansion++; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 340 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 341 | the '#' was indented. */ |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 342 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 343 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 344 | { |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 345 | /* Issue -pedantic or deprecated warnings for extensions. We let |
| 346 | -pedantic take precedence if both are applicable. */ |
| 347 | if (! pfile->state.skipping) |
| 348 | { |
| 349 | if (dir->origin == EXTENSION |
| 350 | && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc)) |
| 351 | && CPP_PEDANTIC (pfile)) |
| 352 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); |
| 353 | else if (((dir->flags & DEPRECATED) != 0 |
| 354 | || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc))) |
| 355 | && CPP_OPTION (pfile, warn_deprecated)) |
| 356 | cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension", |
| 357 | dir->name); |
| 358 | } |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 359 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 360 | /* Traditionally, a directive is ignored unless its # is in |
| 361 | column 1. Therefore in code intended to work with K+R |
| 362 | compilers, directives added by C89 must have their # |
| 363 | indented, and directives present in traditional C must not. |
| 364 | This is true even of directives in skipped conditional |
| 365 | blocks. #elif cannot be used at all. */ |
| 366 | if (CPP_WTRADITIONAL (pfile)) |
| 367 | { |
| 368 | if (dir == &dtable[T_ELIF]) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 369 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 370 | "suggest not using #elif in traditional C"); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 371 | else if (indented && dir->origin == KANDR) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 372 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 373 | "traditional C ignores #%s with the # indented", |
| 374 | dir->name); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 375 | else if (!indented && dir->origin != KANDR) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 376 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 377 | "suggest hiding #%s from traditional C with an indented #", |
| 378 | dir->name); |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 382 | /* Check if we have a known directive. INDENTED is nonzero if the |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 383 | '#' of the directive was indented. This function is in this file |
Gabriel Dos Reis | a2566ae | 2005-01-02 01:32:21 +0000 | [diff] [blame] | 384 | to save unnecessarily exporting dtable etc. to lex.c. Returns |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 385 | nonzero if the line of tokens has been handled, zero if we should |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 386 | continue processing the line. */ |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 387 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 388 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 389 | { |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 390 | const directive *dir = 0; |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 391 | const cpp_token *dname; |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 392 | bool was_parsing_args = pfile->state.parsing_args; |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 393 | bool was_discarding_output = pfile->state.discarding_output; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 394 | int skip = 1; |
| 395 | |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 396 | if (was_discarding_output) |
| 397 | pfile->state.prevent_expansion = 0; |
| 398 | |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 399 | if (was_parsing_args) |
| 400 | { |
| 401 | if (CPP_OPTION (pfile, pedantic)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 402 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 403 | "embedding a directive within macro arguments is not portable"); |
| 404 | pfile->state.parsing_args = 0; |
| 405 | pfile->state.prevent_expansion = 0; |
| 406 | } |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 407 | start_directive (pfile); |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 408 | dname = _cpp_lex_token (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 409 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 410 | if (dname->type == CPP_NAME) |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 411 | { |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 412 | if (dname->val.node.node->is_directive) |
| 413 | dir = &dtable[dname->val.node.node->directive_index]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 414 | } |
Kazu Hirata | 05713b8 | 2002-09-15 18:24:08 +0000 | [diff] [blame] | 415 | /* We do not recognize the # followed by a number extension in |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 416 | assembler code. */ |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 417 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 418 | { |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 419 | dir = &linemarker_dir; |
| 420 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) |
| 421 | && ! pfile->state.skipping) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 422 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 423 | "style of line directive is a GCC extension"); |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 426 | if (dir) |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 427 | { |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 428 | /* If we have a directive that is not an opening conditional, |
| 429 | invalidate any control macro. */ |
| 430 | if (! (dir->flags & IF_COND)) |
| 431 | pfile->mi_valid = false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 432 | |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 433 | /* Kluge alert. In order to be sure that code like this |
| 434 | |
| 435 | #define HASH # |
| 436 | HASH define foo bar |
| 437 | |
| 438 | does not cause '#define foo bar' to get executed when |
| 439 | compiled with -save-temps, we recognize directives in |
Gabriel Dos Reis | a2566ae | 2005-01-02 01:32:21 +0000 | [diff] [blame] | 440 | -fpreprocessed mode only if the # is in column 1. macro.c |
Ollie Wild | ccfc4c9 | 2007-07-30 18:29:20 +0000 | [diff] [blame] | 441 | puts a space in front of any '#' at the start of a macro. |
| 442 | |
| 443 | We exclude the -fdirectives-only case because macro expansion |
| 444 | has not been performed yet, and block comments can cause spaces |
| 445 | to preceed the directive. */ |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 446 | if (CPP_OPTION (pfile, preprocessed) |
Ollie Wild | ccfc4c9 | 2007-07-30 18:29:20 +0000 | [diff] [blame] | 447 | && !CPP_OPTION (pfile, directives_only) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 448 | && (indented || !(dir->flags & IN_I))) |
Zack Weinberg | 6d4587f | 2001-05-10 00:07:23 +0000 | [diff] [blame] | 449 | { |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 450 | skip = 0; |
| 451 | dir = 0; |
Zack Weinberg | 6d4587f | 2001-05-10 00:07:23 +0000 | [diff] [blame] | 452 | } |
| 453 | else |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 454 | { |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 455 | /* In failed conditional groups, all non-conditional |
| 456 | directives are ignored. Before doing that, whether |
| 457 | skipping or not, we should lex angle-bracketed headers |
| 458 | correctly, and maybe output some diagnostics. */ |
| 459 | pfile->state.angled_headers = dir->flags & INCL; |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 460 | pfile->state.directive_wants_padding = dir->flags & INCL; |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 461 | if (! CPP_OPTION (pfile, preprocessed)) |
| 462 | directive_diagnostics (pfile, dir, indented); |
| 463 | if (pfile->state.skipping && !(dir->flags & COND)) |
| 464 | dir = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 467 | else if (dname->type == CPP_EOF) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 468 | ; /* CPP_EOF is the "null directive". */ |
| 469 | else |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 470 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 471 | /* An unknown directive. Don't complain about it in assembly |
| 472 | source: we don't know where the comments are, and # may |
| 473 | introduce assembler pseudo-ops. Don't complain about invalid |
| 474 | directives in skipped conditional groups (6.10 p4). */ |
Neil Booth | bdb05a7 | 2000-11-26 17:31:13 +0000 | [diff] [blame] | 475 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 476 | skip = 0; |
| 477 | else if (!pfile->state.skipping) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 478 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 479 | cpp_token_as_text (pfile, dname)); |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Neil Booth | d1a5868 | 2002-06-28 06:26:54 +0000 | [diff] [blame] | 482 | pfile->directive = dir; |
| 483 | if (CPP_OPTION (pfile, traditional)) |
| 484 | prepare_directive_trad (pfile); |
| 485 | |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 486 | if (dir) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 487 | pfile->directive->handler (pfile); |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 488 | else if (skip == 0) |
| 489 | _cpp_backup_tokens (pfile, 1); |
| 490 | |
| 491 | end_directive (pfile, skip); |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 492 | if (was_parsing_args && !pfile->state.in_deferred_pragma) |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 493 | { |
| 494 | /* Restore state when within macro args. */ |
| 495 | pfile->state.parsing_args = 2; |
| 496 | pfile->state.prevent_expansion = 1; |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 497 | } |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 498 | if (was_discarding_output) |
| 499 | pfile->state.prevent_expansion = 1; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 500 | return skip; |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 503 | /* Directive handler wrapper used by the command line option |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 504 | processor. BUF is \n terminated. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 505 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 506 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 507 | { |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 508 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 509 | /* from_stage3 */ true); |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 510 | start_directive (pfile); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 511 | |
| 512 | /* This is a short-term fix to prevent a leading '#' being |
| 513 | interpreted as a directive. */ |
| 514 | _cpp_clean_line (pfile); |
| 515 | |
Neil Booth | f71aebb | 2001-05-27 18:06:00 +0000 | [diff] [blame] | 516 | pfile->directive = &dtable[dir_no]; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 517 | if (CPP_OPTION (pfile, traditional)) |
| 518 | prepare_directive_trad (pfile); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 519 | pfile->directive->handler (pfile); |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 520 | end_directive (pfile, 1); |
Neil Booth | ef6e958 | 2001-08-04 12:01:59 +0000 | [diff] [blame] | 521 | _cpp_pop_buffer (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 522 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 523 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 524 | /* Checks for validity the macro name in #define, #undef, #ifdef and |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 525 | #ifndef directives. IS_DEF_OR_UNDEF is true if this call is |
| 526 | processing a #define or #undefine directive, and false |
| 527 | otherwise. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 528 | static cpp_hashnode * |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 529 | lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 530 | { |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 531 | const cpp_token *token = _cpp_lex_token (pfile); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 532 | |
Zack Weinberg | 92936ec | 2000-07-19 20:18:08 +0000 | [diff] [blame] | 533 | /* The token immediately after #define must be an identifier. That |
Zack Weinberg | b8363a2 | 2001-07-01 18:48:13 +0000 | [diff] [blame] | 534 | identifier may not be "defined", per C99 6.10.8p4. |
| 535 | In C++, it may not be any of the "named operators" either, |
| 536 | per C++98 [lex.digraph], [lex.key]. |
| 537 | Finally, the identifier may not have been poisoned. (In that case |
Neil Booth | 1d63a28 | 2002-06-28 20:27:14 +0000 | [diff] [blame] | 538 | the lexer has issued the error message for us.) */ |
Zack Weinberg | b8363a2 | 2001-07-01 18:48:13 +0000 | [diff] [blame] | 539 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 540 | if (token->type == CPP_NAME) |
| 541 | { |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 542 | cpp_hashnode *node = token->val.node.node; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 543 | |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 544 | if (is_def_or_undef && node == pfile->spec_nodes.n_defined) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 545 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 546 | "\"defined\" cannot be used as a macro name"); |
| 547 | else if (! (node->flags & NODE_POISONED)) |
| 548 | return node; |
| 549 | } |
| 550 | else if (token->flags & NAMED_OP) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 551 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 552 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 553 | NODE_NAME (token->val.node.node)); |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 554 | else if (token->type == CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 555 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 556 | pfile->directive->name); |
| 557 | else |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 558 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
Zack Weinberg | b8363a2 | 2001-07-01 18:48:13 +0000 | [diff] [blame] | 559 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 560 | return NULL; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 561 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 562 | |
Gabriel Dos Reis | a2566ae | 2005-01-02 01:32:21 +0000 | [diff] [blame] | 563 | /* Process a #define directive. Most work is done in macro.c. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 564 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 565 | do_define (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 566 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 567 | cpp_hashnode *node = lex_macro_node (pfile, true); |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 568 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 569 | if (node) |
| 570 | { |
Neil Booth | 1d63a28 | 2002-06-28 20:27:14 +0000 | [diff] [blame] | 571 | /* If we have been requested to expand comments into macros, |
| 572 | then re-enable saving of comments. */ |
| 573 | pfile->state.save_comments = |
| 574 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); |
| 575 | |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 576 | if (pfile->cb.before_define) |
| 577 | pfile->cb.before_define (pfile); |
| 578 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 579 | if (_cpp_create_definition (pfile, node)) |
| 580 | if (pfile->cb.define) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 581 | pfile->cb.define (pfile, pfile->directive_line, node); |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 582 | |
| 583 | node->flags &= ~NODE_USED; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 584 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 587 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 588 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 589 | do_undef (cpp_reader *pfile) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 590 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 591 | cpp_hashnode *node = lex_macro_node (pfile, true); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 592 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 593 | if (node) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 594 | { |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 595 | if (pfile->cb.before_define) |
| 596 | pfile->cb.before_define (pfile); |
| 597 | |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 598 | if (pfile->cb.undef) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 599 | pfile->cb.undef (pfile, pfile->directive_line, node); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 600 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 601 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
| 602 | identifier is not currently defined as a macro name. */ |
| 603 | if (node->type == NT_MACRO) |
| 604 | { |
| 605 | if (node->flags & NODE_WARN) |
| 606 | cpp_error (pfile, CPP_DL_WARNING, |
| 607 | "undefining \"%s\"", NODE_NAME (node)); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 608 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 609 | if (CPP_OPTION (pfile, warn_unused_macros)) |
| 610 | _cpp_warn_if_unused_macro (pfile, node, NULL); |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 611 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 612 | _cpp_free_definition (node); |
| 613 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 614 | } |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 615 | |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 616 | check_eol (pfile, false); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 619 | /* Undefine a single macro/assertion/whatever. */ |
| 620 | |
| 621 | static int |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 622 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 623 | void *data_p ATTRIBUTE_UNUSED) |
| 624 | { |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 625 | /* Body of _cpp_free_definition inlined here for speed. |
| 626 | Macros and assertions no longer have anything to free. */ |
| 627 | h->type = NT_VOID; |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 628 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED); |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 629 | return 1; |
| 630 | } |
| 631 | |
| 632 | /* Undefine all macros and assertions. */ |
| 633 | |
| 634 | void |
| 635 | cpp_undef_all (cpp_reader *pfile) |
| 636 | { |
| 637 | cpp_forall_identifiers (pfile, undefine_macros, NULL); |
| 638 | } |
| 639 | |
| 640 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 641 | /* Helper routine used by parse_include. Reinterpret the current line |
| 642 | as an h-char-sequence (< ... >); we are looking at the first token |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 643 | after the <. Returns a malloced filename. */ |
| 644 | static char * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 645 | glue_header_name (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 646 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 647 | const cpp_token *token; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 648 | char *buffer; |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 649 | size_t len, total_len = 0, capacity = 1024; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 650 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 651 | /* To avoid lexed tokens overwriting our glued name, we can only |
| 652 | allocate from the string pool once we've lexed everything. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 653 | buffer = XNEWVEC (char, capacity); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 654 | for (;;) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 655 | { |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 656 | token = get_token_no_padding (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 657 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 658 | if (token->type == CPP_GREATER) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 659 | break; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 660 | if (token->type == CPP_EOF) |
| 661 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 662 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 663 | break; |
| 664 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 665 | |
Neil Booth | 5932565 | 2003-04-24 20:03:57 +0000 | [diff] [blame] | 666 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 667 | if (total_len + len > capacity) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 668 | { |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 669 | capacity = (capacity + len) * 2; |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 670 | buffer = XRESIZEVEC (char, buffer, capacity); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 673 | if (token->flags & PREV_WHITE) |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 674 | buffer[total_len++] = ' '; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 675 | |
Geoffrey Keating | 47e2049 | 2005-03-12 10:44:06 +0000 | [diff] [blame] | 676 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
| 677 | true) |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 678 | - (uchar *) buffer); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 681 | buffer[total_len] = '\0'; |
| 682 | return buffer; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 685 | /* Returns the file name of #include, #include_next, #import and |
| 686 | #pragma dependency. The string is malloced and the caller should |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 687 | free it. Returns NULL on error. LOCATION is the source location |
| 688 | of the file name. */ |
| 689 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 690 | static const char * |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 691 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 692 | const cpp_token ***buf, source_location *location) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 693 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 694 | char *fname; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 695 | const cpp_token *header; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 696 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 697 | /* Allow macro expansion. */ |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 698 | header = get_token_no_padding (pfile); |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 699 | *location = header->src_loc; |
Jakub Jelinek | 2c6e3f5 | 2009-10-19 23:41:15 +0200 | [diff] [blame^] | 700 | if ((header->type == CPP_STRING && header->val.str.text[0] != 'R') |
| 701 | || header->type == CPP_HEADER_NAME) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 702 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 703 | fname = XNEWVEC (char, header->val.str.len - 1); |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 704 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
| 705 | fname[header->val.str.len - 2] = '\0'; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 706 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 707 | } |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 708 | else if (header->type == CPP_LESS) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 709 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 710 | fname = glue_header_name (pfile); |
| 711 | *pangle_brackets = 1; |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | const unsigned char *dir; |
| 716 | |
| 717 | if (pfile->directive == &dtable[T_PRAGMA]) |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 718 | dir = UC"pragma dependency"; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 719 | else |
| 720 | dir = pfile->directive->name; |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 721 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 722 | dir); |
| 723 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 724 | return NULL; |
Richard Kenner | cfb3ee1 | 1997-04-13 14:30:13 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Tom Tromey | cda5e67 | 2007-08-18 17:54:11 +0000 | [diff] [blame] | 727 | if (pfile->directive == &dtable[T_PRAGMA]) |
| 728 | { |
| 729 | /* This pragma allows extra tokens after the file name. */ |
| 730 | } |
| 731 | else if (buf == NULL || CPP_OPTION (pfile, discard_comments)) |
Joseph Myers | 61cc822 | 2009-04-18 21:25:07 +0100 | [diff] [blame] | 732 | check_eol (pfile, true); |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 733 | else |
| 734 | { |
| 735 | /* If we are not discarding comments, then gather them while |
| 736 | doing the eol check. */ |
| 737 | *buf = check_eol_return_comments (pfile); |
| 738 | } |
| 739 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 740 | return fname; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 743 | /* Handle #include, #include_next and #import. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 744 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 745 | do_include_common (cpp_reader *pfile, enum include_type type) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 746 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 747 | const char *fname; |
| 748 | int angle_brackets; |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 749 | const cpp_token **buf = NULL; |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 750 | source_location location; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 751 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 752 | /* Re-enable saving of comments if requested, so that the include |
| 753 | callback can dump comments which follow #include. */ |
| 754 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
| 755 | |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 756 | fname = parse_include (pfile, &angle_brackets, &buf, &location); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 757 | if (!fname) |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 758 | { |
| 759 | if (buf) |
| 760 | XDELETEVEC (buf); |
| 761 | return; |
| 762 | } |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 763 | |
Nathanael Nerode | 2830382 | 2004-11-28 22:28:13 +0000 | [diff] [blame] | 764 | if (!*fname) |
| 765 | { |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 766 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
| 767 | "empty filename in #%s", |
| 768 | pfile->directive->name); |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 769 | XDELETEVEC (fname); |
| 770 | if (buf) |
| 771 | XDELETEVEC (buf); |
Nathanael Nerode | 2830382 | 2004-11-28 22:28:13 +0000 | [diff] [blame] | 772 | return; |
| 773 | } |
| 774 | |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 775 | /* Prevent #include recursion. */ |
Per Bothner | 50f59cd | 2004-01-19 21:30:18 -0800 | [diff] [blame] | 776 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 777 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 778 | else |
Neil Booth | 09b8225 | 2001-07-29 22:27:20 +0000 | [diff] [blame] | 779 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 780 | /* Get out of macro context, if we are. */ |
| 781 | skip_rest_of_line (pfile); |
| 782 | |
| 783 | if (pfile->cb.include) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 784 | pfile->cb.include (pfile, pfile->directive_line, |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 785 | pfile->directive->name, fname, angle_brackets, |
| 786 | buf); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 787 | |
Neil Booth | 8f9b400 | 2003-07-29 22:26:13 +0000 | [diff] [blame] | 788 | _cpp_stack_include (pfile, fname, angle_brackets, type); |
Neil Booth | 09b8225 | 2001-07-29 22:27:20 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 791 | XDELETEVEC (fname); |
| 792 | if (buf) |
| 793 | XDELETEVEC (buf); |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 797 | do_include (cpp_reader *pfile) |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 798 | { |
| 799 | do_include_common (pfile, IT_INCLUDE); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 802 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 803 | do_import (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 804 | { |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 805 | do_include_common (pfile, IT_IMPORT); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 806 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 807 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 808 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 809 | do_include_next (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 810 | { |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 811 | enum include_type type = IT_INCLUDE_NEXT; |
| 812 | |
| 813 | /* If this is the primary source file, warn and use the normal |
| 814 | search logic. */ |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 815 | if (cpp_in_primary_file (pfile)) |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 816 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 817 | cpp_error (pfile, CPP_DL_WARNING, |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 818 | "#include_next in primary source file"); |
| 819 | type = IT_INCLUDE; |
| 820 | } |
| 821 | do_include_common (pfile, type); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 824 | /* Subroutine of do_linemarker. Read possible flags after file name. |
| 825 | LAST is the last flag seen; 0 if this is the first flag. Return the |
| 826 | flag if it is valid, 0 at the end of the directive. Otherwise |
| 827 | complain. */ |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 828 | static unsigned int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 829 | read_flag (cpp_reader *pfile, unsigned int last) |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 830 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 831 | const cpp_token *token = _cpp_lex_token (pfile); |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 832 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 833 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 834 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 835 | unsigned int flag = token->val.str.text[0] - '0'; |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 836 | |
| 837 | if (flag > last && flag <= 4 |
| 838 | && (flag != 4 || last == 3) |
| 839 | && (flag != 2 || last == 0)) |
| 840 | return flag; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 841 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 842 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 843 | if (token->type != CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 844 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 845 | cpp_token_as_text (pfile, token)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 846 | return 0; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 849 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 850 | of length LEN, to binary; store it in NUMP, and return false if the |
| 851 | number was well-formed, true if not. WRAPPED is set to true if the |
| 852 | number did not fit into 'unsigned long'. */ |
| 853 | static bool |
| 854 | strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 855 | { |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 856 | linenum_type reg = 0; |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 857 | linenum_type reg_prev = 0; |
| 858 | |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 859 | uchar c; |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 860 | *wrapped = false; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 861 | while (len--) |
| 862 | { |
| 863 | c = *str++; |
| 864 | if (!ISDIGIT (c)) |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 865 | return true; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 866 | reg *= 10; |
| 867 | reg += c - '0'; |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 868 | if (reg < reg_prev) |
| 869 | *wrapped = true; |
| 870 | reg_prev = reg; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 871 | } |
| 872 | *nump = reg; |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 873 | return false; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 876 | /* Interpret #line command. |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 877 | Note that the filename string (if any) is a true string constant |
| 878 | (escapes are interpreted), unlike in #line. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 879 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 880 | do_line (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 881 | { |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 882 | const struct line_maps *line_table = pfile->line_table; |
| 883 | const struct line_map *map = &line_table->maps[line_table->used - 1]; |
Devang Patel | 2203a88 | 2005-02-28 11:04:19 -0800 | [diff] [blame] | 884 | |
| 885 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down |
| 886 | sysp right now. */ |
| 887 | |
| 888 | unsigned char map_sysp = map->sysp; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 889 | const cpp_token *token; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 890 | const char *new_file = map->to_file; |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 891 | linenum_type new_lineno; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 892 | |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 893 | /* C99 raised the minimum limit on #line numbers. */ |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 894 | linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 895 | bool wrapped; |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 896 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 897 | /* #line commands expand macros. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 898 | token = cpp_get_token (pfile); |
| 899 | if (token->type != CPP_NUMBER |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 900 | || strtolinenum (token->val.str.text, token->val.str.len, |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 901 | &new_lineno, &wrapped)) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 902 | { |
Tom Tromey | 33ae483 | 2008-01-03 17:58:26 +0000 | [diff] [blame] | 903 | if (token->type == CPP_EOF) |
| 904 | cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line"); |
| 905 | else |
| 906 | cpp_error (pfile, CPP_DL_ERROR, |
| 907 | "\"%s\" after #line is not a positive integer", |
| 908 | cpp_token_as_text (pfile, token)); |
Zack Weinberg | 9ec7291 | 2000-08-09 19:41:12 +0000 | [diff] [blame] | 909 | return; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 910 | } |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 911 | |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 912 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 913 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 914 | else if (wrapped) |
| 915 | cpp_error (pfile, CPP_DL_WARNING, "line number out of range"); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 916 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 917 | token = cpp_get_token (pfile); |
| 918 | if (token->type == CPP_STRING) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 919 | { |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 920 | cpp_string s = { 0, 0 }; |
Eric Christopher | 423e95e | 2004-02-12 02:25:03 +0000 | [diff] [blame] | 921 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
Jerry Quinn | f1bf410 | 2009-07-18 03:22:16 +0000 | [diff] [blame] | 922 | &s, CPP_STRING)) |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 923 | new_file = (const char *)s.text; |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 924 | check_eol (pfile, true); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 925 | } |
| 926 | else if (token->type != CPP_EOF) |
| 927 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 928 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 929 | cpp_token_as_text (pfile, token)); |
| 930 | return; |
| 931 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 932 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 933 | skip_rest_of_line (pfile); |
Joseph Myers | c7f9c0b | 2009-04-18 18:36:28 +0100 | [diff] [blame] | 934 | _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno, |
Devang Patel | 2203a88 | 2005-02-28 11:04:19 -0800 | [diff] [blame] | 935 | map_sysp); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* Interpret the # 44 "file" [flags] notation, which has slightly |
| 939 | different syntax and semantics from #line: Flags are allowed, |
| 940 | and we never complain about the line number being too big. */ |
| 941 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 942 | do_linemarker (cpp_reader *pfile) |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 943 | { |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 944 | const struct line_maps *line_table = pfile->line_table; |
| 945 | const struct line_map *map = &line_table->maps[line_table->used - 1]; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 946 | const cpp_token *token; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 947 | const char *new_file = map->to_file; |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 948 | linenum_type new_lineno; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 949 | unsigned int new_sysp = map->sysp; |
Joseph Myers | c7f9c0b | 2009-04-18 18:36:28 +0100 | [diff] [blame] | 950 | enum lc_reason reason = LC_RENAME_VERBATIM; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 951 | int flag; |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 952 | bool wrapped; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 953 | |
| 954 | /* Back up so we can get the number again. Putting this in |
| 955 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in |
| 956 | some circumstances, which can segfault. */ |
| 957 | _cpp_backup_tokens (pfile, 1); |
| 958 | |
| 959 | /* #line commands expand macros. */ |
| 960 | token = cpp_get_token (pfile); |
| 961 | if (token->type != CPP_NUMBER |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 962 | || strtolinenum (token->val.str.text, token->val.str.len, |
Manuel López-Ibáñez | 3b8f20a | 2008-07-22 09:45:58 +0000 | [diff] [blame] | 963 | &new_lineno, &wrapped)) |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 964 | { |
Tom Tromey | 33ae483 | 2008-01-03 17:58:26 +0000 | [diff] [blame] | 965 | /* Unlike #line, there does not seem to be a way to get an EOF |
| 966 | here. So, it should be safe to always spell the token. */ |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 967 | cpp_error (pfile, CPP_DL_ERROR, |
| 968 | "\"%s\" after # is not a positive integer", |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 969 | cpp_token_as_text (pfile, token)); |
| 970 | return; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 971 | } |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 972 | |
| 973 | token = cpp_get_token (pfile); |
| 974 | if (token->type == CPP_STRING) |
| 975 | { |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 976 | cpp_string s = { 0, 0 }; |
Eric Christopher | 423e95e | 2004-02-12 02:25:03 +0000 | [diff] [blame] | 977 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
Jerry Quinn | f1bf410 | 2009-07-18 03:22:16 +0000 | [diff] [blame] | 978 | 1, &s, CPP_STRING)) |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 979 | new_file = (const char *)s.text; |
Eric Christopher | cf551fb | 2004-01-16 22:37:49 +0000 | [diff] [blame] | 980 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 981 | new_sysp = 0; |
| 982 | flag = read_flag (pfile, 0); |
| 983 | if (flag == 1) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 984 | { |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 985 | reason = LC_ENTER; |
| 986 | /* Fake an include for cpp_included (). */ |
| 987 | _cpp_fake_include (pfile, new_file); |
| 988 | flag = read_flag (pfile, flag); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 989 | } |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 990 | else if (flag == 2) |
| 991 | { |
| 992 | reason = LC_LEAVE; |
| 993 | flag = read_flag (pfile, flag); |
| 994 | } |
| 995 | if (flag == 3) |
| 996 | { |
| 997 | new_sysp = 1; |
| 998 | flag = read_flag (pfile, flag); |
| 999 | if (flag == 4) |
| 1000 | new_sysp = 2; |
| 1001 | } |
Jakub Jelinek | 9d30f27 | 2006-12-29 09:15:08 +0100 | [diff] [blame] | 1002 | pfile->buffer->sysp = new_sysp; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 1003 | |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1004 | check_eol (pfile, false); |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 1005 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1006 | else if (token->type != CPP_EOF) |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 1007 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1008 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1009 | cpp_token_as_text (pfile, token)); |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 1012 | |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1013 | skip_rest_of_line (pfile); |
Manuel López-Ibáñez | 00b0c19 | 2009-05-13 23:17:55 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* Compensate for the increment in linemap_add that occurs in |
| 1016 | _cpp_do_file_change. We're currently at the start of the line |
| 1017 | *following* the #line directive. A separate source_location for this |
| 1018 | location makes no sense (until we do the LC_LEAVE), and |
| 1019 | complicates LAST_SOURCE_LINE_LOCATION. */ |
| 1020 | pfile->line_table->highest_location--; |
| 1021 | |
Neil Booth | bb74c96 | 2001-08-17 22:23:49 +0000 | [diff] [blame] | 1022 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 1025 | /* Arrange the file_change callback. pfile->line has changed to |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 1026 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
Joseph Myers | a1f300c | 2001-11-23 02:05:19 +0000 | [diff] [blame] | 1027 | header, 2 for a system header that needs to be extern "C" protected, |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 1028 | and zero otherwise. */ |
Neil Booth | eb1f4d9 | 2000-12-18 19:00:26 +0000 | [diff] [blame] | 1029 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1030 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 1031 | const char *to_file, linenum_type file_line, |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1032 | unsigned int sysp) |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 1033 | { |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 1034 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
| 1035 | to_file, file_line); |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 1036 | if (map != NULL) |
| 1037 | linemap_line_start (pfile->line_table, map->to_line, 127); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1038 | |
Neil Booth | eb1f4d9 | 2000-12-18 19:00:26 +0000 | [diff] [blame] | 1039 | if (pfile->cb.file_change) |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 1040 | pfile->cb.file_change (pfile, map); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1041 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 1042 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1043 | /* Report a warning or error detected by the program we are |
| 1044 | processing. Use the directive's tokens in the error message. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1045 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1046 | do_diagnostic (cpp_reader *pfile, int code, int print_dir) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1047 | { |
Tom Tromey | 5d6342e | 2008-05-21 21:52:57 +0000 | [diff] [blame] | 1048 | const unsigned char *dir_name; |
| 1049 | unsigned char *line; |
| 1050 | source_location src_loc = pfile->cur_token[-1].src_loc; |
| 1051 | |
| 1052 | if (print_dir) |
| 1053 | dir_name = pfile->directive->name; |
| 1054 | else |
| 1055 | dir_name = NULL; |
| 1056 | pfile->state.prevent_expansion++; |
| 1057 | line = cpp_output_line_to_string (pfile, dir_name); |
| 1058 | pfile->state.prevent_expansion--; |
| 1059 | |
| 1060 | cpp_error_with_line (pfile, code, src_loc, 0, "%s", line); |
| 1061 | free (line); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Neil Booth | 838f313 | 2000-09-24 10:42:09 +0000 | [diff] [blame] | 1064 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1065 | do_error (cpp_reader *pfile) |
Neil Booth | 838f313 | 2000-09-24 10:42:09 +0000 | [diff] [blame] | 1066 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1067 | do_diagnostic (pfile, CPP_DL_ERROR, 1); |
Neil Booth | 838f313 | 2000-09-24 10:42:09 +0000 | [diff] [blame] | 1068 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1069 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1070 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1071 | do_warning (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1072 | { |
Neil Booth | 2f87897 | 2001-04-08 10:01:18 +0000 | [diff] [blame] | 1073 | /* We want #warning diagnostics to be emitted in system headers too. */ |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1074 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1077 | /* Report program identification. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1078 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1079 | do_ident (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1080 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1081 | const cpp_token *str = cpp_get_token (pfile); |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1082 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1083 | if (str->type != CPP_STRING) |
Zack Weinberg | 1ed17cd | 2005-05-12 18:31:38 +0000 | [diff] [blame] | 1084 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
| 1085 | pfile->directive->name); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1086 | else if (pfile->cb.ident) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1087 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1088 | |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1089 | check_eol (pfile, false); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1092 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
| 1093 | matching entry, or NULL if none is found. The returned entry could |
| 1094 | be the start of a namespace chain, or a pragma. */ |
| 1095 | static struct pragma_entry * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1096 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 1097 | { |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1098 | while (chain && chain->pragma != pragma) |
| 1099 | chain = chain->next; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1100 | |
| 1101 | return chain; |
| 1102 | } |
| 1103 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1104 | /* Create and insert a blank pragma entry at the beginning of a |
| 1105 | singly-linked CHAIN. */ |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1106 | static struct pragma_entry * |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1107 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1108 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1109 | struct pragma_entry *new_entry; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1110 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1111 | new_entry = (struct pragma_entry *) |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1112 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1113 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1114 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1115 | new_entry->next = *chain; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1116 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1117 | *chain = new_entry; |
| 1118 | return new_entry; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1122 | goes in the global namespace. */ |
| 1123 | static struct pragma_entry * |
| 1124 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, |
| 1125 | bool allow_name_expansion) |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 1126 | { |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1127 | struct pragma_entry **chain = &pfile->pragmas; |
| 1128 | struct pragma_entry *entry; |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1129 | const cpp_hashnode *node; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1130 | |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1131 | if (space) |
| 1132 | { |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 1133 | node = cpp_lookup (pfile, UC space, strlen (space)); |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1134 | entry = lookup_pragma_entry (*chain, node); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1135 | if (!entry) |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1136 | { |
| 1137 | entry = new_pragma_entry (pfile, chain); |
| 1138 | entry->pragma = node; |
| 1139 | entry->is_nspace = true; |
| 1140 | entry->allow_expansion = allow_name_expansion; |
| 1141 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1142 | else if (!entry->is_nspace) |
| 1143 | goto clash; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1144 | else if (entry->allow_expansion != allow_name_expansion) |
| 1145 | { |
| 1146 | cpp_error (pfile, CPP_DL_ICE, |
| 1147 | "registering pragmas in namespace \"%s\" with mismatched " |
| 1148 | "name expansion", space); |
| 1149 | return NULL; |
| 1150 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1151 | chain = &entry->u.space; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1152 | } |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1153 | else if (allow_name_expansion) |
| 1154 | { |
| 1155 | cpp_error (pfile, CPP_DL_ICE, |
| 1156 | "registering pragma \"%s\" with name expansion " |
| 1157 | "and no namespace", name); |
| 1158 | return NULL; |
| 1159 | } |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1160 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1161 | /* Check for duplicates. */ |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 1162 | node = cpp_lookup (pfile, UC name, strlen (name)); |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1163 | entry = lookup_pragma_entry (*chain, node); |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1164 | if (entry == NULL) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1165 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1166 | entry = new_pragma_entry (pfile, chain); |
| 1167 | entry->pragma = node; |
| 1168 | return entry; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1169 | } |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1170 | |
| 1171 | if (entry->is_nspace) |
| 1172 | clash: |
| 1173 | cpp_error (pfile, CPP_DL_ICE, |
| 1174 | "registering \"%s\" as both a pragma and a pragma namespace", |
| 1175 | NODE_NAME (node)); |
| 1176 | else if (space) |
| 1177 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", |
| 1178 | space, name); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1179 | else |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1180 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
| 1181 | |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | |
| 1185 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ |
| 1186 | static void |
| 1187 | register_pragma_internal (cpp_reader *pfile, const char *space, |
| 1188 | const char *name, pragma_cb handler) |
| 1189 | { |
| 1190 | struct pragma_entry *entry; |
| 1191 | |
| 1192 | entry = register_pragma_1 (pfile, space, name, false); |
| 1193 | entry->is_internal = true; |
| 1194 | entry->u.handler = handler; |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
| 1198 | goes in the global namespace. HANDLER is the handler it will call, |
Daniel Jacobowitz | b5b3e36 | 2004-11-23 23:25:40 +0000 | [diff] [blame] | 1199 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
| 1200 | expansion while parsing pragma NAME. This function is exported |
| 1201 | from libcpp. */ |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1202 | void |
| 1203 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, |
Daniel Jacobowitz | b5b3e36 | 2004-11-23 23:25:40 +0000 | [diff] [blame] | 1204 | pragma_cb handler, bool allow_expansion) |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1205 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1206 | struct pragma_entry *entry; |
| 1207 | |
| 1208 | if (!handler) |
| 1209 | { |
| 1210 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | entry = register_pragma_1 (pfile, space, name, false); |
| 1215 | if (entry) |
| 1216 | { |
| 1217 | entry->allow_expansion = allow_expansion; |
| 1218 | entry->u.handler = handler; |
| 1219 | } |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1220 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1221 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1222 | /* Similarly, but create mark the pragma for deferred processing. |
| 1223 | When found, a CPP_PRAGMA token will be insertted into the stream |
| 1224 | with IDENT in the token->u.pragma slot. */ |
| 1225 | void |
| 1226 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, |
| 1227 | const char *name, unsigned int ident, |
| 1228 | bool allow_expansion, bool allow_name_expansion) |
| 1229 | { |
| 1230 | struct pragma_entry *entry; |
| 1231 | |
| 1232 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); |
| 1233 | if (entry) |
| 1234 | { |
| 1235 | entry->is_deferred = true; |
| 1236 | entry->allow_expansion = allow_expansion; |
| 1237 | entry->u.ident = ident; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1241 | /* Register the pragmas the preprocessor itself handles. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1242 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1243 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1244 | { |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1245 | /* Pragmas in the global namespace. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1246 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1247 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1248 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1249 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
| 1250 | register_pragma_internal (pfile, "GCC", "system_header", |
| 1251 | do_pragma_system_header); |
| 1252 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 1253 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1254 | |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1255 | /* Return the number of registered pragmas in PE. */ |
| 1256 | |
| 1257 | static int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1258 | count_registered_pragmas (struct pragma_entry *pe) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1259 | { |
| 1260 | int ct = 0; |
| 1261 | for (; pe != NULL; pe = pe->next) |
| 1262 | { |
| 1263 | if (pe->is_nspace) |
| 1264 | ct += count_registered_pragmas (pe->u.space); |
| 1265 | ct++; |
| 1266 | } |
| 1267 | return ct; |
| 1268 | } |
| 1269 | |
| 1270 | /* Save into SD the names of the registered pragmas referenced by PE, |
| 1271 | and return a pointer to the next free space in SD. */ |
| 1272 | |
| 1273 | static char ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1274 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1275 | { |
| 1276 | for (; pe != NULL; pe = pe->next) |
| 1277 | { |
| 1278 | if (pe->is_nspace) |
| 1279 | sd = save_registered_pragmas (pe->u.space, sd); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1280 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
| 1281 | HT_LEN (&pe->pragma->ident), |
| 1282 | HT_LEN (&pe->pragma->ident) + 1); |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1283 | } |
| 1284 | return sd; |
| 1285 | } |
| 1286 | |
| 1287 | /* Return a newly-allocated array which saves the names of the |
| 1288 | registered pragmas. */ |
| 1289 | |
| 1290 | char ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1291 | _cpp_save_pragma_names (cpp_reader *pfile) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1292 | { |
| 1293 | int ct = count_registered_pragmas (pfile->pragmas); |
Bernardo Innocenti | 72bb2c3 | 2004-07-24 20:04:42 +0200 | [diff] [blame] | 1294 | char **result = XNEWVEC (char *, ct); |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1295 | (void) save_registered_pragmas (pfile->pragmas, result); |
| 1296 | return result; |
| 1297 | } |
| 1298 | |
| 1299 | /* Restore from SD the names of the registered pragmas referenced by PE, |
| 1300 | and return a pointer to the next unused name in SD. */ |
| 1301 | |
| 1302 | static char ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1303 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
| 1304 | char **sd) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1305 | { |
| 1306 | for (; pe != NULL; pe = pe->next) |
| 1307 | { |
| 1308 | if (pe->is_nspace) |
| 1309 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 1310 | pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd)); |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1311 | free (*sd); |
| 1312 | sd++; |
| 1313 | } |
| 1314 | return sd; |
| 1315 | } |
| 1316 | |
| 1317 | /* Restore the names of the registered pragmas from SAVED. */ |
| 1318 | |
| 1319 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1320 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1321 | { |
| 1322 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); |
| 1323 | free (saved); |
| 1324 | } |
| 1325 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1326 | /* Pragmata handling. We handle some, and pass the rest on to the |
| 1327 | front end. C99 defines three pragmas and says that no macro |
| 1328 | expansion is to be performed on them; whether or not macro |
| 1329 | expansion happens for other pragmas is implementation defined. |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1330 | This implementation allows for a mix of both, since GCC did not |
| 1331 | traditionally macro expand its (few) pragmas, whereas OpenMP |
| 1332 | specifies that macro expansion should happen. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1333 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1334 | do_pragma (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1335 | { |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1336 | const struct pragma_entry *p = NULL; |
Alexandre Oliva | e2e1fa5 | 2003-09-24 23:53:07 +0000 | [diff] [blame] | 1337 | const cpp_token *token, *pragma_token = pfile->cur_token; |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1338 | cpp_token ns_token; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1339 | unsigned int count = 1; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 1340 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1341 | pfile->state.prevent_expansion++; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1342 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1343 | token = cpp_get_token (pfile); |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1344 | ns_token = *token; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1345 | if (token->type == CPP_NAME) |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 1346 | { |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1347 | p = lookup_pragma_entry (pfile->pragmas, token->val.node.node); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1348 | if (p && p->is_nspace) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1349 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1350 | bool allow_name_expansion = p->allow_expansion; |
| 1351 | if (allow_name_expansion) |
| 1352 | pfile->state.prevent_expansion--; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1353 | token = cpp_get_token (pfile); |
| 1354 | if (token->type == CPP_NAME) |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1355 | p = lookup_pragma_entry (p->u.space, token->val.node.node); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1356 | else |
| 1357 | p = NULL; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1358 | if (allow_name_expansion) |
| 1359 | pfile->state.prevent_expansion++; |
| 1360 | count = 2; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1361 | } |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1364 | if (p) |
Alexandre Oliva | e2e1fa5 | 2003-09-24 23:53:07 +0000 | [diff] [blame] | 1365 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1366 | if (p->is_deferred) |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1367 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1368 | pfile->directive_result.src_loc = pragma_token->src_loc; |
| 1369 | pfile->directive_result.type = CPP_PRAGMA; |
| 1370 | pfile->directive_result.flags = pragma_token->flags; |
| 1371 | pfile->directive_result.val.pragma = p->u.ident; |
| 1372 | pfile->state.in_deferred_pragma = true; |
| 1373 | pfile->state.pragma_allow_expansion = p->allow_expansion; |
| 1374 | if (!p->allow_expansion) |
Daniel Jacobowitz | b5b3e36 | 2004-11-23 23:25:40 +0000 | [diff] [blame] | 1375 | pfile->state.prevent_expansion++; |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1376 | } |
| 1377 | else |
| 1378 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1379 | /* Since the handler below doesn't get the line number, that |
| 1380 | it might need for diagnostics, make sure it has the right |
| 1381 | numbers in place. */ |
| 1382 | if (pfile->cb.line_change) |
| 1383 | (*pfile->cb.line_change) (pfile, pragma_token, false); |
| 1384 | if (p->allow_expansion) |
| 1385 | pfile->state.prevent_expansion--; |
| 1386 | (*p->u.handler) (pfile); |
| 1387 | if (p->allow_expansion) |
| 1388 | pfile->state.prevent_expansion++; |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1389 | } |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1390 | } |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1391 | else if (pfile->cb.def_pragma) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1392 | { |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1393 | if (count == 1 || pfile->context->prev == NULL) |
| 1394 | _cpp_backup_tokens (pfile, count); |
| 1395 | else |
| 1396 | { |
| 1397 | /* Invalid name comes from macro expansion, _cpp_backup_tokens |
| 1398 | won't allow backing 2 tokens. */ |
| 1399 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook |
| 1400 | reads both tokens, we could perhaps free it, but if it doesn't, |
| 1401 | we don't know the exact lifespan. */ |
| 1402 | cpp_token *toks = XNEWVEC (cpp_token, 2); |
| 1403 | toks[0] = ns_token; |
| 1404 | toks[0].flags |= NO_EXPAND; |
| 1405 | toks[1] = *token; |
| 1406 | toks[1].flags |= NO_EXPAND; |
| 1407 | _cpp_push_token_context (pfile, NULL, toks, 2); |
| 1408 | } |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1409 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1410 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1411 | |
Neil Booth | 9729389 | 2001-09-14 22:04:46 +0000 | [diff] [blame] | 1412 | pfile->state.prevent_expansion--; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1415 | /* Handle #pragma once. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1416 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1417 | do_pragma_once (cpp_reader *pfile) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1418 | { |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 1419 | if (cpp_in_primary_file (pfile)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1420 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1421 | |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1422 | check_eol (pfile, false); |
Neil Booth | 49634b3 | 2003-08-02 16:29:46 +0000 | [diff] [blame] | 1423 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Neil Booth | c3bf3e6 | 2002-05-09 17:14:22 +0000 | [diff] [blame] | 1426 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
| 1427 | that the lexer produces a hard error for each subsequent usage. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1428 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1429 | do_pragma_poison (cpp_reader *pfile) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1430 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1431 | const cpp_token *tok; |
Zack Weinberg | f8f769e | 2000-05-28 05:56:38 +0000 | [diff] [blame] | 1432 | cpp_hashnode *hp; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1433 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1434 | pfile->state.poisoned_ok = 1; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1435 | for (;;) |
| 1436 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1437 | tok = _cpp_lex_token (pfile); |
| 1438 | if (tok->type == CPP_EOF) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1439 | break; |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1440 | if (tok->type != CPP_NAME) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1441 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1442 | cpp_error (pfile, CPP_DL_ERROR, |
| 1443 | "invalid #pragma GCC poison directive"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1444 | break; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1447 | hp = tok->val.node.node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1448 | if (hp->flags & NODE_POISONED) |
| 1449 | continue; |
| 1450 | |
| 1451 | if (hp->type == NT_MACRO) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1452 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1453 | NODE_NAME (hp)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1454 | _cpp_free_definition (hp); |
| 1455 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1456 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1457 | pfile->state.poisoned_ok = 0; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1458 | } |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1459 | |
| 1460 | /* Mark the current header as a system header. This will suppress |
| 1461 | some categories of warnings (notably those from -pedantic). It is |
| 1462 | intended for use in system libraries that cannot be implemented in |
| 1463 | conforming C, but cannot be certain that their headers appear in a |
| 1464 | system include directory. To prevent abuse, it is rejected in the |
| 1465 | primary source file. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1466 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1467 | do_pragma_system_header (cpp_reader *pfile) |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1468 | { |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 1469 | if (cpp_in_primary_file (pfile)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1470 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1471 | "#pragma system_header ignored outside include file"); |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1472 | else |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1473 | { |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1474 | check_eol (pfile, false); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1475 | skip_rest_of_line (pfile); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1476 | cpp_make_system_header (pfile, 1, 0); |
| 1477 | } |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1478 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1479 | |
| 1480 | /* Check the modified date of the current include file against a specified |
| 1481 | file. Issue a diagnostic, if the specified file is newer. We use this to |
| 1482 | determine if a fixed header should be refixed. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1483 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1484 | do_pragma_dependency (cpp_reader *pfile) |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1485 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1486 | const char *fname; |
| 1487 | int angle_brackets, ordering; |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 1488 | source_location location; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 1489 | |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 1490 | fname = parse_include (pfile, &angle_brackets, NULL, &location); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1491 | if (!fname) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1492 | return; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1493 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1494 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1495 | if (ordering < 0) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1496 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1497 | else if (ordering > 0) |
| 1498 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1499 | cpp_error (pfile, CPP_DL_WARNING, |
| 1500 | "current file is older than %s", fname); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1501 | if (cpp_get_token (pfile)->type != CPP_EOF) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1502 | { |
| 1503 | _cpp_backup_tokens (pfile, 1); |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1504 | do_diagnostic (pfile, CPP_DL_WARNING, 0); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1505 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1506 | } |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1507 | |
Kaveh R. Ghazi | fad205f | 2003-06-16 21:41:10 +0000 | [diff] [blame] | 1508 | free ((void *) fname); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1511 | /* Get a token but skip padding. */ |
| 1512 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1513 | get_token_no_padding (cpp_reader *pfile) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1514 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1515 | for (;;) |
| 1516 | { |
| 1517 | const cpp_token *result = cpp_get_token (pfile); |
| 1518 | if (result->type != CPP_PADDING) |
| 1519 | return result; |
| 1520 | } |
| 1521 | } |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1522 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1523 | /* Check syntax is "(string-literal)". Returns the string on success, |
| 1524 | or NULL on failure. */ |
| 1525 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1526 | get__Pragma_string (cpp_reader *pfile) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1527 | { |
| 1528 | const cpp_token *string; |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 1529 | const cpp_token *paren; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1530 | |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 1531 | paren = get_token_no_padding (pfile); |
| 1532 | if (paren->type == CPP_EOF) |
| 1533 | _cpp_backup_tokens (pfile, 1); |
| 1534 | if (paren->type != CPP_OPEN_PAREN) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1535 | return NULL; |
| 1536 | |
| 1537 | string = get_token_no_padding (pfile); |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 1538 | if (string->type == CPP_EOF) |
| 1539 | _cpp_backup_tokens (pfile, 1); |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 1540 | if (string->type != CPP_STRING && string->type != CPP_WSTRING |
Jakub Jelinek | 2c6e3f5 | 2009-10-19 23:41:15 +0200 | [diff] [blame^] | 1541 | && string->type != CPP_STRING32 && string->type != CPP_STRING16 |
| 1542 | && string->type != CPP_UTF8STRING) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1543 | return NULL; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1544 | |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 1545 | paren = get_token_no_padding (pfile); |
| 1546 | if (paren->type == CPP_EOF) |
| 1547 | _cpp_backup_tokens (pfile, 1); |
| 1548 | if (paren->type != CPP_CLOSE_PAREN) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1549 | return NULL; |
| 1550 | |
| 1551 | return string; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Neil Booth | 8706281 | 2001-10-20 09:00:53 +0000 | [diff] [blame] | 1554 | /* Destringize IN into a temporary buffer, by removing the first \ of |
| 1555 | \" and \\ sequences, and process the result as a #pragma directive. */ |
| 1556 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1557 | destringize_and_run (cpp_reader *pfile, const cpp_string *in) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1558 | { |
| 1559 | const unsigned char *src, *limit; |
Neil Booth | 8706281 | 2001-10-20 09:00:53 +0000 | [diff] [blame] | 1560 | char *dest, *result; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1561 | cpp_context *saved_context; |
| 1562 | cpp_token *saved_cur_token; |
| 1563 | tokenrun *saved_cur_run; |
| 1564 | cpp_token *toks; |
| 1565 | int count; |
Tom Tromey | 14ccf80 | 2008-03-13 21:10:07 +0000 | [diff] [blame] | 1566 | const struct directive *save_directive; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1567 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1568 | dest = result = (char *) alloca (in->len - 1); |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 1569 | src = in->text + 1 + (in->text[0] == 'L'); |
| 1570 | limit = in->text + in->len - 1; |
| 1571 | while (src < limit) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1572 | { |
| 1573 | /* We know there is a character following the backslash. */ |
| 1574 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) |
| 1575 | src++; |
| 1576 | *dest++ = *src++; |
| 1577 | } |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 1578 | *dest = '\n'; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1579 | |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 1580 | /* Ugh; an awful kludge. We are really not set up to be lexing |
| 1581 | tokens when in the middle of a macro expansion. Use a new |
| 1582 | context to force cpp_get_token to lex, and so skip_rest_of_line |
| 1583 | doesn't go beyond the end of the text. Also, remember the |
| 1584 | current lexing position so we can return to it later. |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1585 | |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 1586 | Something like line-at-a-time lexing should remove the need for |
| 1587 | this. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1588 | saved_context = pfile->context; |
| 1589 | saved_cur_token = pfile->cur_token; |
| 1590 | saved_cur_run = pfile->cur_run; |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 1591 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1592 | pfile->context = XNEW (cpp_context); |
| 1593 | pfile->context->macro = 0; |
| 1594 | pfile->context->prev = 0; |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1595 | pfile->context->next = 0; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1596 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1597 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
| 1598 | until we've read all of the tokens that we want. */ |
| 1599 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, |
| 1600 | /* from_stage3 */ true); |
| 1601 | /* ??? Antique Disgusting Hack. What does this do? */ |
| 1602 | if (pfile->buffer->prev) |
| 1603 | pfile->buffer->file = pfile->buffer->prev->file; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1604 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1605 | start_directive (pfile); |
| 1606 | _cpp_clean_line (pfile); |
Tom Tromey | 14ccf80 | 2008-03-13 21:10:07 +0000 | [diff] [blame] | 1607 | save_directive = pfile->directive; |
| 1608 | pfile->directive = &dtable[T_PRAGMA]; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1609 | do_pragma (pfile); |
| 1610 | end_directive (pfile, 1); |
Tom Tromey | 14ccf80 | 2008-03-13 21:10:07 +0000 | [diff] [blame] | 1611 | pfile->directive = save_directive; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1612 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1613 | /* We always insert at least one token, the directive result. It'll |
| 1614 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we |
| 1615 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1616 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1617 | /* If we're not handling the pragma internally, read all of the tokens from |
| 1618 | the string buffer now, while the string buffer is still installed. */ |
| 1619 | /* ??? Note that the token buffer allocated here is leaked. It's not clear |
| 1620 | to me what the true lifespan of the tokens are. It would appear that |
| 1621 | the lifespan is the entire parse of the main input stream, in which case |
| 1622 | this may not be wrong. */ |
| 1623 | if (pfile->directive_result.type == CPP_PRAGMA) |
| 1624 | { |
| 1625 | int maxcount; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1626 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1627 | count = 1; |
| 1628 | maxcount = 50; |
| 1629 | toks = XNEWVEC (cpp_token, maxcount); |
| 1630 | toks[0] = pfile->directive_result; |
| 1631 | |
| 1632 | do |
| 1633 | { |
| 1634 | if (count == maxcount) |
| 1635 | { |
| 1636 | maxcount = maxcount * 3 / 2; |
| 1637 | toks = XRESIZEVEC (cpp_token, toks, maxcount); |
| 1638 | } |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1639 | toks[count] = *cpp_get_token (pfile); |
| 1640 | /* Macros have been already expanded by cpp_get_token |
| 1641 | if the pragma allowed expansion. */ |
| 1642 | toks[count++].flags |= NO_EXPAND; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1643 | } |
| 1644 | while (toks[count-1].type != CPP_PRAGMA_EOL); |
| 1645 | } |
| 1646 | else |
| 1647 | { |
| 1648 | count = 1; |
| 1649 | toks = XNEW (cpp_token); |
| 1650 | toks[0] = pfile->directive_result; |
| 1651 | |
| 1652 | /* If we handled the entire pragma internally, make sure we get the |
| 1653 | line number correct for the next token. */ |
| 1654 | if (pfile->cb.line_change) |
| 1655 | pfile->cb.line_change (pfile, pfile->cur_token, false); |
| 1656 | } |
| 1657 | |
| 1658 | /* Finish inlining run_directive. */ |
| 1659 | pfile->buffer->file = NULL; |
| 1660 | _cpp_pop_buffer (pfile); |
| 1661 | |
| 1662 | /* Reset the old macro state before ... */ |
| 1663 | XDELETE (pfile->context); |
| 1664 | pfile->context = saved_context; |
| 1665 | pfile->cur_token = saved_cur_token; |
| 1666 | pfile->cur_run = saved_cur_run; |
| 1667 | |
| 1668 | /* ... inserting the new tokens we collected. */ |
| 1669 | _cpp_push_token_context (pfile, NULL, toks, count); |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 1672 | /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ |
| 1673 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1674 | _cpp_do__Pragma (cpp_reader *pfile) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1675 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1676 | const cpp_token *string = get__Pragma_string (pfile); |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1677 | pfile->directive_result.type = CPP_PADDING; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1678 | |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1679 | if (string) |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 1680 | { |
| 1681 | destringize_and_run (pfile, &string->val.str); |
| 1682 | return 1; |
| 1683 | } |
| 1684 | cpp_error (pfile, CPP_DL_ERROR, |
| 1685 | "_Pragma takes a parenthesized string literal"); |
| 1686 | return 0; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1689 | /* Handle #ifdef. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1690 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1691 | do_ifdef (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1692 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1693 | int skip = 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1694 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1695 | if (! pfile->state.skipping) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1696 | { |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 1697 | cpp_hashnode *node = lex_macro_node (pfile, false); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1698 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1699 | if (node) |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1700 | { |
| 1701 | skip = node->type != NT_MACRO; |
| 1702 | _cpp_mark_macro_used (node); |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 1703 | if (!(node->flags & NODE_USED)) |
| 1704 | { |
| 1705 | node->flags |= NODE_USED; |
| 1706 | if (node->type == NT_MACRO) |
| 1707 | { |
| 1708 | if (pfile->cb.used_define) |
| 1709 | pfile->cb.used_define (pfile, pfile->directive_line, node); |
| 1710 | } |
| 1711 | else |
| 1712 | { |
| 1713 | if (pfile->cb.used_undef) |
| 1714 | pfile->cb.used_undef (pfile, pfile->directive_line, node); |
| 1715 | } |
| 1716 | } |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1717 | check_eol (pfile, false); |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1718 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | push_conditional (pfile, skip, T_IFDEF, 0); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1724 | /* Handle #ifndef. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1725 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1726 | do_ifndef (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1727 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1728 | int skip = 1; |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 1729 | cpp_hashnode *node = 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1730 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1731 | if (! pfile->state.skipping) |
Jakub Jelinek | 5af7e2c | 2000-06-08 00:27:57 +0200 | [diff] [blame] | 1732 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 1733 | node = lex_macro_node (pfile, false); |
Geoffrey Keating | b43db0b | 2000-12-02 22:28:44 +0000 | [diff] [blame] | 1734 | |
| 1735 | if (node) |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1736 | { |
| 1737 | skip = node->type == NT_MACRO; |
| 1738 | _cpp_mark_macro_used (node); |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 1739 | if (!(node->flags & NODE_USED)) |
| 1740 | { |
| 1741 | node->flags |= NODE_USED; |
| 1742 | if (node->type == NT_MACRO) |
| 1743 | { |
| 1744 | if (pfile->cb.used_define) |
| 1745 | pfile->cb.used_define (pfile, pfile->directive_line, node); |
| 1746 | } |
| 1747 | else |
| 1748 | { |
| 1749 | if (pfile->cb.used_undef) |
| 1750 | pfile->cb.used_undef (pfile, pfile->directive_line, node); |
| 1751 | } |
| 1752 | } |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1753 | check_eol (pfile, false); |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1754 | } |
Jakub Jelinek | 5af7e2c | 2000-06-08 00:27:57 +0200 | [diff] [blame] | 1755 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1756 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1757 | push_conditional (pfile, skip, T_IFNDEF, node); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1758 | } |
| 1759 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1760 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
| 1761 | pfile->mi_ind_cmacro so we can handle multiple-include |
Kazu Hirata | 6356f89 | 2003-06-12 19:01:08 +0000 | [diff] [blame] | 1762 | optimizations. If macro expansion occurs in the expression, we |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1763 | cannot treat it as a controlling conditional, since the expansion |
| 1764 | could change in the future. That is handled by cpp_get_token. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1765 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1766 | do_if (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1767 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1768 | int skip = 1; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1769 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1770 | if (! pfile->state.skipping) |
Tom Tromey | d750887 | 2008-05-30 14:25:09 +0000 | [diff] [blame] | 1771 | skip = _cpp_parse_expr (pfile, true) == false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1772 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1773 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1776 | /* Flip skipping state if appropriate and continue without changing |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1777 | if_stack; this is so that the error message for missing #endif's |
| 1778 | etc. will point to the original #if. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1779 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1780 | do_else (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1781 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1782 | cpp_buffer *buffer = pfile->buffer; |
| 1783 | struct if_stack *ifs = buffer->if_stack; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1784 | |
| 1785 | if (ifs == NULL) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1786 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1787 | else |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1788 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1789 | if (ifs->type == T_ELSE) |
| 1790 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1791 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
| 1792 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1793 | "the conditional began here"); |
| 1794 | } |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1795 | ifs->type = T_ELSE; |
| 1796 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1797 | /* Skip any future (erroneous) #elses or #elifs. */ |
| 1798 | pfile->state.skipping = ifs->skip_elses; |
| 1799 | ifs->skip_elses = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1800 | |
| 1801 | /* Invalidate any controlling macro. */ |
| 1802 | ifs->mi_cmacro = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1803 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1804 | /* Only check EOL if was not originally skipping. */ |
Phil Edwards | 909de5d | 2002-03-22 21:59:04 +0000 | [diff] [blame] | 1805 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1806 | check_eol (pfile, false); |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1807 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1808 | } |
| 1809 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1810 | /* Handle a #elif directive by not changing if_stack either. See the |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1811 | comment above do_else. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1812 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1813 | do_elif (cpp_reader *pfile) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1814 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1815 | cpp_buffer *buffer = pfile->buffer; |
| 1816 | struct if_stack *ifs = buffer->if_stack; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1817 | |
| 1818 | if (ifs == NULL) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1819 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1820 | else |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1821 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1822 | if (ifs->type == T_ELSE) |
| 1823 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1824 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
| 1825 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1826 | "the conditional began here"); |
| 1827 | } |
| 1828 | ifs->type = T_ELIF; |
| 1829 | |
Tom Tromey | d750887 | 2008-05-30 14:25:09 +0000 | [diff] [blame] | 1830 | if (! ifs->was_skipping) |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1831 | { |
Tom Tromey | d750887 | 2008-05-30 14:25:09 +0000 | [diff] [blame] | 1832 | bool value; |
| 1833 | /* The standard mandates that the expression be parsed even |
| 1834 | if we are skipping elses at this point -- the lexical |
| 1835 | restrictions on #elif only apply to skipped groups, but |
| 1836 | this group is not being skipped. Temporarily set |
| 1837 | skipping to false to get lexer warnings. */ |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1838 | pfile->state.skipping = 0; |
Tom Tromey | d750887 | 2008-05-30 14:25:09 +0000 | [diff] [blame] | 1839 | value = _cpp_parse_expr (pfile, false); |
| 1840 | if (ifs->skip_elses) |
| 1841 | pfile->state.skipping = 1; |
| 1842 | else |
| 1843 | { |
| 1844 | pfile->state.skipping = ! value; |
| 1845 | ifs->skip_elses = value; |
| 1846 | } |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1847 | } |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1848 | |
| 1849 | /* Invalidate any controlling macro. */ |
| 1850 | ifs->mi_cmacro = 0; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1851 | } |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1854 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1855 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1856 | do_endif (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1857 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1858 | cpp_buffer *buffer = pfile->buffer; |
| 1859 | struct if_stack *ifs = buffer->if_stack; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1860 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1861 | if (ifs == NULL) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1862 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1863 | else |
| 1864 | { |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1865 | /* Only check EOL if was not originally skipping. */ |
Phil Edwards | 909de5d | 2002-03-22 21:59:04 +0000 | [diff] [blame] | 1866 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 1867 | check_eol (pfile, false); |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1868 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1869 | /* If potential control macro, we go back outside again. */ |
| 1870 | if (ifs->next == 0 && ifs->mi_cmacro) |
| 1871 | { |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1872 | pfile->mi_valid = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1873 | pfile->mi_cmacro = ifs->mi_cmacro; |
| 1874 | } |
| 1875 | |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1876 | buffer->if_stack = ifs->next; |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1877 | pfile->state.skipping = ifs->was_skipping; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 1878 | obstack_free (&pfile->buffer_ob, ifs); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1879 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1880 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1881 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1882 | /* Push an if_stack entry for a preprocessor conditional, and set |
| 1883 | pfile->state.skipping to SKIP. If TYPE indicates the conditional |
| 1884 | is #if or #ifndef, CMACRO is a potentially controlling macro, and |
| 1885 | we need to check here that we are at the top of the file. */ |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1886 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1887 | push_conditional (cpp_reader *pfile, int skip, int type, |
| 1888 | const cpp_hashnode *cmacro) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1889 | { |
| 1890 | struct if_stack *ifs; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1891 | cpp_buffer *buffer = pfile->buffer; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1892 | |
Bernardo Innocenti | 72bb2c3 | 2004-07-24 20:04:42 +0200 | [diff] [blame] | 1893 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
Neil Booth | 5041042 | 2001-09-15 10:18:03 +0000 | [diff] [blame] | 1894 | ifs->line = pfile->directive_line; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1895 | ifs->next = buffer->if_stack; |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1896 | ifs->skip_elses = pfile->state.skipping || !skip; |
| 1897 | ifs->was_skipping = pfile->state.skipping; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1898 | ifs->type = type; |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1899 | /* This condition is effectively a test for top-of-file. */ |
| 1900 | if (pfile->mi_valid && pfile->mi_cmacro == 0) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1901 | ifs->mi_cmacro = cmacro; |
| 1902 | else |
| 1903 | ifs->mi_cmacro = 0; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1904 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1905 | pfile->state.skipping = skip; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1906 | buffer->if_stack = ifs; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1909 | /* Read the tokens of the answer into the macro pool, in a directive |
| 1910 | of type TYPE. Only commit the memory if we intend it as permanent |
| 1911 | storage, i.e. the #assert case. Returns 0 on success, and sets |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 1912 | ANSWERP to point to the answer. PRED_LOC is the location of the |
| 1913 | predicate. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1914 | static int |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 1915 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type, |
| 1916 | source_location pred_loc) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1917 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1918 | const cpp_token *paren; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1919 | struct answer *answer; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1920 | unsigned int acount; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1921 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1922 | /* In a conditional, it is legal to not have an open paren. We |
| 1923 | should save the following token in this case. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1924 | paren = cpp_get_token (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1925 | |
| 1926 | /* If not a paren, see if we're OK. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1927 | if (paren->type != CPP_OPEN_PAREN) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1928 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1929 | /* In a conditional no answer is a test for any answer. It |
| 1930 | could be followed by any token. */ |
| 1931 | if (type == T_IF) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1932 | { |
| 1933 | _cpp_backup_tokens (pfile, 1); |
| 1934 | return 0; |
| 1935 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1936 | |
| 1937 | /* #unassert with no answer is valid - it removes all answers. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1938 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1939 | return 0; |
| 1940 | |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 1941 | cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0, |
| 1942 | "missing '(' after predicate"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1943 | return 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1946 | for (acount = 0;; acount++) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1947 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1948 | size_t room_needed; |
| 1949 | const cpp_token *token = cpp_get_token (pfile); |
| 1950 | cpp_token *dest; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1951 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1952 | if (token->type == CPP_CLOSE_PAREN) |
| 1953 | break; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1954 | |
| 1955 | if (token->type == CPP_EOF) |
| 1956 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1957 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1958 | return 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1959 | } |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1960 | |
| 1961 | /* struct answer includes the space for one token. */ |
| 1962 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); |
| 1963 | |
| 1964 | if (BUFF_ROOM (pfile->a_buff) < room_needed) |
| 1965 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); |
| 1966 | |
| 1967 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; |
| 1968 | *dest = *token; |
| 1969 | |
| 1970 | /* Drop whitespace at start, for answer equivalence purposes. */ |
| 1971 | if (acount == 0) |
| 1972 | dest->flags &= ~PREV_WHITE; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1975 | if (acount == 0) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1976 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1977 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1978 | return 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1981 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
| 1982 | answer->count = acount; |
| 1983 | answer->next = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1984 | *answerp = answer; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1985 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1986 | return 0; |
| 1987 | } |
| 1988 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1989 | /* Parses an assertion directive of type TYPE, returning a pointer to |
| 1990 | the hash node of the predicate, or 0 on error. If an answer was |
| 1991 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1992 | static cpp_hashnode * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1993 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1994 | { |
| 1995 | cpp_hashnode *result = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1996 | const cpp_token *predicate; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1997 | |
| 1998 | /* We don't expand predicates or answers. */ |
| 1999 | pfile->state.prevent_expansion++; |
| 2000 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2001 | *answerp = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2002 | predicate = cpp_get_token (pfile); |
| 2003 | if (predicate->type == CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2004 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2005 | else if (predicate->type != CPP_NAME) |
Manuel López-Ibáñez | a28fbdb | 2009-06-23 16:30:58 +0000 | [diff] [blame] | 2006 | cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0, |
| 2007 | "predicate must be an identifier"); |
| 2008 | else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2009 | { |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2010 | unsigned int len = NODE_LEN (predicate->val.node.node); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2011 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2012 | |
| 2013 | /* Prefix '#' to get it out of macro namespace. */ |
| 2014 | sym[0] = '#'; |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2015 | memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2016 | result = cpp_lookup (pfile, sym, len + 1); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2019 | pfile->state.prevent_expansion--; |
| 2020 | return result; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 2023 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2024 | or a pointer to NULL if the answer is not in the chain. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2025 | static struct answer ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2026 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2027 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2028 | unsigned int i; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2029 | struct answer **result; |
| 2030 | |
| 2031 | for (result = &node->value.answers; *result; result = &(*result)->next) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2032 | { |
| 2033 | struct answer *answer = *result; |
| 2034 | |
| 2035 | if (answer->count == candidate->count) |
| 2036 | { |
| 2037 | for (i = 0; i < answer->count; i++) |
| 2038 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) |
| 2039 | break; |
| 2040 | |
| 2041 | if (i == answer->count) |
| 2042 | break; |
| 2043 | } |
| 2044 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2045 | |
| 2046 | return result; |
| 2047 | } |
| 2048 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2049 | /* Test an assertion within a preprocessor conditional. Returns |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 2050 | nonzero on failure, zero on success. On success, the result of |
Hans-Peter Nilsson | 2402645 | 2002-11-29 22:41:04 +0000 | [diff] [blame] | 2051 | the test is written into VALUE, otherwise the value 0. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2052 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2053 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2054 | { |
| 2055 | struct answer *answer; |
| 2056 | cpp_hashnode *node; |
| 2057 | |
| 2058 | node = parse_assertion (pfile, &answer, T_IF); |
Hans-Peter Nilsson | 2402645 | 2002-11-29 22:41:04 +0000 | [diff] [blame] | 2059 | |
| 2060 | /* For recovery, an erroneous assertion expression is handled as a |
| 2061 | failing assertion. */ |
| 2062 | *value = 0; |
| 2063 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2064 | if (node) |
| 2065 | *value = (node->type == NT_ASSERTION && |
| 2066 | (answer == 0 || *find_answer (node, answer) != 0)); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2067 | else if (pfile->cur_token[-1].type == CPP_EOF) |
| 2068 | _cpp_backup_tokens (pfile, 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2069 | |
| 2070 | /* We don't commit the memory for the answer - it's temporary only. */ |
| 2071 | return node == 0; |
| 2072 | } |
| 2073 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 2074 | /* Handle #assert. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2075 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2076 | do_assert (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2077 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2078 | struct answer *new_answer; |
| 2079 | cpp_hashnode *node; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2080 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2081 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2082 | if (node) |
| 2083 | { |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2084 | size_t answer_size; |
| 2085 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2086 | /* Place the new answer in the answer list. First check there |
| 2087 | is not a duplicate. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2088 | new_answer->next = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2089 | if (node->type == NT_ASSERTION) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2090 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2091 | if (*find_answer (node, new_answer)) |
| 2092 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2093 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 2094 | NODE_NAME (node) + 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2095 | return; |
| 2096 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2097 | new_answer->next = node->value.answers; |
| 2098 | } |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2099 | |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2100 | answer_size = sizeof (struct answer) + ((new_answer->count - 1) |
| 2101 | * sizeof (cpp_token)); |
| 2102 | /* Commit or allocate storage for the object. */ |
| 2103 | if (pfile->hash_table->alloc_subobject) |
| 2104 | { |
| 2105 | struct answer *temp_answer = new_answer; |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2106 | new_answer = (struct answer *) pfile->hash_table->alloc_subobject |
| 2107 | (answer_size); |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2108 | memcpy (new_answer, temp_answer, answer_size); |
| 2109 | } |
| 2110 | else |
| 2111 | BUFF_FRONT (pfile->a_buff) += answer_size; |
| 2112 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2113 | node->type = NT_ASSERTION; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2114 | node->value.answers = new_answer; |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 2115 | check_eol (pfile, false); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2116 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2117 | } |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 2118 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 2119 | /* Handle #unassert. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2120 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2121 | do_unassert (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2122 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2123 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2124 | struct answer *answer; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2125 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2126 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
| 2127 | /* It isn't an error to #unassert something that isn't asserted. */ |
| 2128 | if (node && node->type == NT_ASSERTION) |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 2129 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2130 | if (answer) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2131 | { |
| 2132 | struct answer **p = find_answer (node, answer), *temp; |
| 2133 | |
| 2134 | /* Remove the answer from the list. */ |
| 2135 | temp = *p; |
| 2136 | if (temp) |
| 2137 | *p = temp->next; |
| 2138 | |
| 2139 | /* Did we free the last answer? */ |
| 2140 | if (node->value.answers == 0) |
| 2141 | node->type = NT_VOID; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2142 | |
Joseph Myers | a5cb563 | 2009-04-18 16:28:40 +0100 | [diff] [blame] | 2143 | check_eol (pfile, false); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2144 | } |
| 2145 | else |
| 2146 | _cpp_free_definition (node); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 2147 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2148 | |
| 2149 | /* We don't commit the memory for the answer - it's temporary only. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2150 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2151 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2152 | /* These are for -D, -U, -A. */ |
| 2153 | |
| 2154 | /* Process the string STR as if it appeared as the body of a #define. |
| 2155 | If STR is just an identifier, define it with value 1. |
| 2156 | If STR has anything after the identifier, then it should |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 2157 | be identifier=definition. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2158 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2159 | cpp_define (cpp_reader *pfile, const char *str) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2160 | { |
Jason Merrill | 86373e7 | 2009-09-12 15:46:45 -0400 | [diff] [blame] | 2161 | char *buf; |
| 2162 | const char *p; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2163 | size_t count; |
| 2164 | |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2165 | /* Copy the entire option so we can modify it. |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2166 | Change the first "=" in the string to a space. If there is none, |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2167 | tack " 1" on the end. */ |
| 2168 | |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2169 | count = strlen (str); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2170 | buf = (char *) alloca (count + 3); |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2171 | memcpy (buf, str, count); |
| 2172 | |
| 2173 | p = strchr (str, '='); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2174 | if (p) |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2175 | buf[p - str] = ' '; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2176 | else |
| 2177 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2178 | buf[count++] = ' '; |
| 2179 | buf[count++] = '1'; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2180 | } |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2181 | buf[count] = '\n'; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2182 | |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2183 | run_directive (pfile, T_DEFINE, buf, count); |
Zack Weinberg | 2c8f051 | 2000-08-29 18:37:37 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
Daniel Franke | 28f6862 | 2008-04-22 14:04:32 -0400 | [diff] [blame] | 2186 | |
| 2187 | /* Use to build macros to be run through cpp_define() as |
| 2188 | described above. |
| 2189 | Example: cpp_define_formatted (pfile, "MACRO=%d", value); */ |
| 2190 | |
| 2191 | void |
| 2192 | cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...) |
| 2193 | { |
| 2194 | char *ptr = NULL; |
| 2195 | |
| 2196 | va_list ap; |
| 2197 | va_start (ap, fmt); |
| 2198 | vasprintf (&ptr, fmt, ap); |
| 2199 | va_end (ap); |
| 2200 | |
| 2201 | cpp_define (pfile, ptr); |
| 2202 | free (ptr); |
| 2203 | } |
| 2204 | |
| 2205 | |
Neil Booth | ad2a084 | 2000-12-17 00:13:54 +0000 | [diff] [blame] | 2206 | /* Slight variant of the above for use by initialize_builtins. */ |
Zack Weinberg | 2c8f051 | 2000-08-29 18:37:37 +0000 | [diff] [blame] | 2207 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2208 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
Zack Weinberg | 2c8f051 | 2000-08-29 18:37:37 +0000 | [diff] [blame] | 2209 | { |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2210 | size_t len = strlen (str); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2211 | char *buf = (char *) alloca (len + 1); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2212 | memcpy (buf, str, len); |
| 2213 | buf[len] = '\n'; |
| 2214 | run_directive (pfile, T_DEFINE, buf, len); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /* Process MACRO as if it appeared as the body of an #undef. */ |
| 2218 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2219 | cpp_undef (cpp_reader *pfile, const char *macro) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2220 | { |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2221 | size_t len = strlen (macro); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2222 | char *buf = (char *) alloca (len + 1); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2223 | memcpy (buf, macro, len); |
| 2224 | buf[len] = '\n'; |
| 2225 | run_directive (pfile, T_UNDEF, buf, len); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Richard Henderson | 121de39 | 2007-03-30 14:12:53 -0700 | [diff] [blame] | 2228 | /* Like lex_macro_node, but read the input from STR. */ |
| 2229 | static cpp_hashnode * |
| 2230 | lex_macro_node_from_str (cpp_reader *pfile, const char *str) |
| 2231 | { |
| 2232 | size_t len = strlen (str); |
Michael Meissner | 4cd9707 | 2007-03-30 22:40:19 +0000 | [diff] [blame] | 2233 | uchar *buf = (uchar *) alloca (len + 1); |
Richard Henderson | 121de39 | 2007-03-30 14:12:53 -0700 | [diff] [blame] | 2234 | cpp_hashnode *node; |
| 2235 | |
| 2236 | memcpy (buf, str, len); |
| 2237 | buf[len] = '\n'; |
| 2238 | cpp_push_buffer (pfile, buf, len, true); |
| 2239 | node = lex_macro_node (pfile, true); |
| 2240 | _cpp_pop_buffer (pfile); |
| 2241 | |
| 2242 | return node; |
| 2243 | } |
| 2244 | |
| 2245 | /* If STR is a defined macro, return its definition node, else return NULL. */ |
| 2246 | cpp_macro * |
| 2247 | cpp_push_definition (cpp_reader *pfile, const char *str) |
| 2248 | { |
| 2249 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); |
| 2250 | if (node && node->type == NT_MACRO) |
| 2251 | return node->value.macro; |
| 2252 | else |
| 2253 | return NULL; |
| 2254 | } |
| 2255 | |
| 2256 | /* Replace a previous definition DFN of the macro STR. If DFN is NULL, |
| 2257 | then the macro should be undefined. */ |
| 2258 | void |
| 2259 | cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn) |
| 2260 | { |
| 2261 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); |
| 2262 | if (node == NULL) |
| 2263 | return; |
| 2264 | |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 2265 | if (pfile->cb.before_define) |
| 2266 | pfile->cb.before_define (pfile); |
| 2267 | |
Richard Henderson | 121de39 | 2007-03-30 14:12:53 -0700 | [diff] [blame] | 2268 | if (node->type == NT_MACRO) |
| 2269 | { |
| 2270 | if (pfile->cb.undef) |
| 2271 | pfile->cb.undef (pfile, pfile->directive_line, node); |
| 2272 | if (CPP_OPTION (pfile, warn_unused_macros)) |
| 2273 | _cpp_warn_if_unused_macro (pfile, node, NULL); |
| 2274 | } |
| 2275 | if (node->type != NT_VOID) |
| 2276 | _cpp_free_definition (node); |
| 2277 | |
| 2278 | if (dfn) |
| 2279 | { |
| 2280 | node->type = NT_MACRO; |
| 2281 | node->value.macro = dfn; |
| 2282 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))) |
| 2283 | node->flags |= NODE_WARN; |
| 2284 | |
| 2285 | if (pfile->cb.define) |
| 2286 | pfile->cb.define (pfile, pfile->directive_line, node); |
| 2287 | } |
| 2288 | } |
| 2289 | |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 2290 | /* Process the string STR as if it appeared as the body of a #assert. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2291 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2292 | cpp_assert (cpp_reader *pfile, const char *str) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2293 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2294 | handle_assertion (pfile, str, T_ASSERT); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 2297 | /* Process STR as if it appeared as the body of an #unassert. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 2298 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2299 | cpp_unassert (cpp_reader *pfile, const char *str) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 2300 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2301 | handle_assertion (pfile, str, T_UNASSERT); |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2302 | } |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 2303 | |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2304 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
| 2305 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2306 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2307 | { |
| 2308 | size_t count = strlen (str); |
| 2309 | const char *p = strchr (str, '='); |
| 2310 | |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2311 | /* Copy the entire option so we can modify it. Change the first |
| 2312 | "=" in the string to a '(', and tack a ')' on the end. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2313 | char *buf = (char *) alloca (count + 2); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2314 | |
| 2315 | memcpy (buf, str, count); |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2316 | if (p) |
| 2317 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2318 | buf[p - str] = '('; |
| 2319 | buf[count++] = ')'; |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2320 | } |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2321 | buf[count] = '\n'; |
| 2322 | str = buf; |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2323 | |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2324 | run_directive (pfile, type, str, count); |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2327 | /* The options structure. */ |
| 2328 | cpp_options * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2329 | cpp_get_options (cpp_reader *pfile) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2330 | { |
| 2331 | return &pfile->opts; |
| 2332 | } |
| 2333 | |
| 2334 | /* The callbacks structure. */ |
| 2335 | cpp_callbacks * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2336 | cpp_get_callbacks (cpp_reader *pfile) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2337 | { |
| 2338 | return &pfile->cb; |
| 2339 | } |
| 2340 | |
| 2341 | /* Copy the given callbacks structure to our own. */ |
| 2342 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2343 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2344 | { |
| 2345 | pfile->cb = *cb; |
| 2346 | } |
| 2347 | |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 2348 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
| 2349 | struct deps * |
| 2350 | cpp_get_deps (cpp_reader *pfile) |
| 2351 | { |
| 2352 | if (!pfile->deps) |
| 2353 | pfile->deps = deps_init (); |
| 2354 | return pfile->deps; |
| 2355 | } |
| 2356 | |
Neil Booth | eb1f4d9 | 2000-12-18 19:00:26 +0000 | [diff] [blame] | 2357 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
| 2358 | doesn't fail. It does not generate a file change call back; that |
| 2359 | is the responsibility of the caller. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2360 | cpp_buffer * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2361 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 2362 | int from_stage3) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2363 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2364 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2365 | |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2366 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2367 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
Neil Booth | ad2a084 | 2000-12-17 00:13:54 +0000 | [diff] [blame] | 2368 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2369 | new_buffer->next_line = new_buffer->buf = buffer; |
| 2370 | new_buffer->rlimit = buffer + len; |
| 2371 | new_buffer->from_stage3 = from_stage3; |
| 2372 | new_buffer->prev = pfile->buffer; |
| 2373 | new_buffer->need_line = true; |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 2374 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2375 | pfile->buffer = new_buffer; |
Eric Christopher | cf551fb | 2004-01-16 22:37:49 +0000 | [diff] [blame] | 2376 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2377 | return new_buffer; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2380 | /* Pops a single buffer, with a file change call-back if appropriate. |
| 2381 | Then pushes the next -include file, if any remain. */ |
Neil Booth | ef6e958 | 2001-08-04 12:01:59 +0000 | [diff] [blame] | 2382 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2383 | _cpp_pop_buffer (cpp_reader *pfile) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2384 | { |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2385 | cpp_buffer *buffer = pfile->buffer; |
Neil Booth | 8f9b400 | 2003-07-29 22:26:13 +0000 | [diff] [blame] | 2386 | struct _cpp_file *inc = buffer->file; |
Neil Booth | ad2a084 | 2000-12-17 00:13:54 +0000 | [diff] [blame] | 2387 | struct if_stack *ifs; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2388 | |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2389 | /* Walk back up the conditional stack till we reach its level at |
| 2390 | entry to this file, issuing error messages. */ |
| 2391 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2392 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2393 | "unterminated #%s", dtable[ifs->type].name); |
| 2394 | |
Neil Booth | 9729389 | 2001-09-14 22:04:46 +0000 | [diff] [blame] | 2395 | /* In case of a missing #endif. */ |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 2396 | pfile->state.skipping = 0; |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2397 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2398 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2399 | pfile->buffer = buffer->prev; |
| 2400 | |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2401 | free (buffer->notes); |
| 2402 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2403 | /* Free the buffer object now; we may want to push a new buffer |
| 2404 | in _cpp_push_next_include_file. */ |
| 2405 | obstack_free (&pfile->buffer_ob, buffer); |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2406 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2407 | if (inc) |
| 2408 | { |
| 2409 | _cpp_pop_file_buffer (pfile, inc); |
| 2410 | |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 2411 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2412 | } |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Kazu Hirata | 05713b8 | 2002-09-15 18:24:08 +0000 | [diff] [blame] | 2415 | /* Enter all recognized directives in the hash table. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2416 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2417 | _cpp_init_directives (cpp_reader *pfile) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2418 | { |
Neil Booth | 766ee68 | 2001-01-29 19:20:12 +0000 | [diff] [blame] | 2419 | unsigned int i; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2420 | cpp_hashnode *node; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame] | 2421 | |
John David Anglin | 37b8524 | 2001-03-02 01:11:50 +0000 | [diff] [blame] | 2422 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2423 | { |
Neil Booth | 766ee68 | 2001-01-29 19:20:12 +0000 | [diff] [blame] | 2424 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2425 | node->is_directive = 1; |
| 2426 | node->directive_index = i; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2427 | } |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2428 | } |