comparison libcpp/macro.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Part of CPP library. (Macro and #define handling.) 1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2017 Free Software Foundation, Inc. 2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994. 3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986 4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987 5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 6
7 This program is free software; you can redistribute it and/or modify it 7 This program is free software; you can redistribute it and/or modify it
49 /* The kind of macro tokens which the instance of 49 /* The kind of macro tokens which the instance of
50 macro_arg_token_iter is supposed to iterate over. */ 50 macro_arg_token_iter is supposed to iterate over. */
51 enum macro_arg_token_kind { 51 enum macro_arg_token_kind {
52 MACRO_ARG_TOKEN_NORMAL, 52 MACRO_ARG_TOKEN_NORMAL,
53 /* This is a macro argument token that got transformed into a string 53 /* This is a macro argument token that got transformed into a string
54 litteral, e.g. #foo. */ 54 literal, e.g. #foo. */
55 MACRO_ARG_TOKEN_STRINGIFIED, 55 MACRO_ARG_TOKEN_STRINGIFIED,
56 /* This is a token resulting from the expansion of a macro 56 /* This is a token resulting from the expansion of a macro
57 argument that was itself a macro. */ 57 argument that was itself a macro. */
58 MACRO_ARG_TOKEN_EXPANDED 58 MACRO_ARG_TOKEN_EXPANDED
59 }; 59 };
83 /* Saved data about an identifier being used as a macro argument 83 /* Saved data about an identifier being used as a macro argument
84 name. */ 84 name. */
85 struct macro_arg_saved_data { 85 struct macro_arg_saved_data {
86 /* The canonical (UTF-8) spelling of this identifier. */ 86 /* The canonical (UTF-8) spelling of this identifier. */
87 cpp_hashnode *canonical_node; 87 cpp_hashnode *canonical_node;
88 /* The previous value of this identifier. */ 88 /* The previous value & type of this identifier. */
89 union _cpp_hashnode_value value; 89 union _cpp_hashnode_value value;
90 node_type type;
91 };
92
93 static const char *vaopt_paste_error =
94 N_("'##' cannot appear at either end of __VA_OPT__");
95
96 /* A class for tracking __VA_OPT__ state while iterating over a
97 sequence of tokens. This is used during both macro definition and
98 expansion. */
99 class vaopt_state {
100
101 public:
102
103 /* Initialize the state tracker. ANY_ARGS is true if variable
104 arguments were provided to the macro invocation. */
105 vaopt_state (cpp_reader *pfile, bool is_variadic, bool any_args)
106 : m_pfile (pfile),
107 m_allowed (any_args),
108 m_variadic (is_variadic),
109 m_last_was_paste (false),
110 m_state (0),
111 m_paste_location (0),
112 m_location (0)
113 {
114 }
115
116 enum update_type
117 {
118 ERROR,
119 DROP,
120 INCLUDE,
121 BEGIN,
122 END
123 };
124
125 /* Given a token, update the state of this tracker and return a
126 boolean indicating whether the token should be be included in the
127 expansion. */
128 update_type update (const cpp_token *token)
129 {
130 /* If the macro isn't variadic, just don't bother. */
131 if (!m_variadic)
132 return INCLUDE;
133
134 if (token->type == CPP_NAME
135 && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__)
136 {
137 if (m_state > 0)
138 {
139 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
140 "__VA_OPT__ may not appear in a __VA_OPT__");
141 return ERROR;
142 }
143 ++m_state;
144 m_location = token->src_loc;
145 return BEGIN;
146 }
147 else if (m_state == 1)
148 {
149 if (token->type != CPP_OPEN_PAREN)
150 {
151 cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
152 "__VA_OPT__ must be followed by an "
153 "open parenthesis");
154 return ERROR;
155 }
156 ++m_state;
157 return DROP;
158 }
159 else if (m_state >= 2)
160 {
161 if (m_state == 2 && token->type == CPP_PASTE)
162 {
163 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
164 vaopt_paste_error);
165 return ERROR;
166 }
167 /* Advance states before further considering this token, in
168 case we see a close paren immediately after the open
169 paren. */
170 if (m_state == 2)
171 ++m_state;
172
173 bool was_paste = m_last_was_paste;
174 m_last_was_paste = false;
175 if (token->type == CPP_PASTE)
176 {
177 m_last_was_paste = true;
178 m_paste_location = token->src_loc;
179 }
180 else if (token->type == CPP_OPEN_PAREN)
181 ++m_state;
182 else if (token->type == CPP_CLOSE_PAREN)
183 {
184 --m_state;
185 if (m_state == 2)
186 {
187 /* Saw the final paren. */
188 m_state = 0;
189
190 if (was_paste)
191 {
192 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
193 vaopt_paste_error);
194 return ERROR;
195 }
196
197 return END;
198 }
199 }
200 return m_allowed ? INCLUDE : DROP;
201 }
202
203 /* Nothing to do with __VA_OPT__. */
204 return INCLUDE;
205 }
206
207 /* Ensure that any __VA_OPT__ was completed. If ok, return true.
208 Otherwise, issue an error and return false. */
209 bool completed ()
210 {
211 if (m_variadic && m_state != 0)
212 cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
213 "unterminated __VA_OPT__");
214 return m_state == 0;
215 }
216
217 private:
218
219 /* The cpp_reader. */
220 cpp_reader *m_pfile;
221
222 /* True if there were varargs. */
223 bool m_allowed;
224 /* True if the macro is variadic. */
225 bool m_variadic;
226 /* If true, the previous token was ##. This is used to detect when
227 a paste occurs at the end of the sequence. */
228 bool m_last_was_paste;
229
230 /* The state variable:
231 0 means not parsing
232 1 means __VA_OPT__ seen, looking for "("
233 2 means "(" seen (so the next token can't be "##")
234 >= 3 means looking for ")", the number encodes the paren depth. */
235 int m_state;
236
237 /* The location of the paste token. */
238 source_location m_paste_location;
239
240 /* Location of the __VA_OPT__ token. */
241 source_location m_location;
90 }; 242 };
91 243
92 /* Macro expansion. */ 244 /* Macro expansion. */
93 245
94 static int enter_macro_context (cpp_reader *, cpp_hashnode *, 246 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
155 static inline void tokens_buff_remove_last_token (_cpp_buff *); 307 static inline void tokens_buff_remove_last_token (_cpp_buff *);
156 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, 308 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
157 macro_arg *, source_location); 309 macro_arg *, source_location);
158 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, 310 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
159 _cpp_buff **, unsigned *); 311 _cpp_buff **, unsigned *);
160 static bool create_iso_definition (cpp_reader *, cpp_macro *); 312 static cpp_macro *create_iso_definition (cpp_reader *);
161 313
162 /* #define directive parsing and handling. */ 314 /* #define directive parsing and handling. */
163 315
164 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); 316 static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *);
165 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
166 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, 317 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
167 const cpp_macro *); 318 const cpp_macro *);
168 static bool parse_params (cpp_reader *, cpp_macro *); 319 static bool parse_params (cpp_reader *, unsigned *, bool *);
169 static void check_trad_stringification (cpp_reader *, const cpp_macro *, 320 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
170 const cpp_string *); 321 const cpp_string *);
171 static bool reached_end_of_context (cpp_context *); 322 static bool reached_end_of_context (cpp_context *);
172 static void consume_next_token_from_context (cpp_reader *pfile, 323 static void consume_next_token_from_context (cpp_reader *pfile,
173 const cpp_token **, 324 const cpp_token **,
189 has not been used. */ 340 has not been used. */
190 int 341 int
191 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, 342 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
192 void *v ATTRIBUTE_UNUSED) 343 void *v ATTRIBUTE_UNUSED)
193 { 344 {
194 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 345 if (cpp_user_macro_p (node))
195 { 346 {
196 cpp_macro *macro = node->value.macro; 347 cpp_macro *macro = node->value.macro;
197 348
198 if (!macro->used 349 if (!macro->used
199 && MAIN_FILE_P (linemap_check_ordinary 350 && MAIN_FILE_P (linemap_check_ordinary
299 { 450 {
300 name = _cpp_get_file_name (pfile->main_file); 451 name = _cpp_get_file_name (pfile->main_file);
301 if (!name) 452 if (!name)
302 abort (); 453 abort ();
303 } 454 }
455 if (pfile->cb.remap_filename)
456 name = pfile->cb.remap_filename (name);
304 len = strlen (name); 457 len = strlen (name);
305 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); 458 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
306 result = buf; 459 result = buf;
307 *buf = '"'; 460 *buf = '"';
308 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); 461 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
774 if (argc == macro->paramc) 927 if (argc == macro->paramc)
775 return true; 928 return true;
776 929
777 if (argc < macro->paramc) 930 if (argc < macro->paramc)
778 { 931 {
779 /* As an extension, variadic arguments are allowed to not appear in 932 /* In C++2a (here the va_opt flag is used), and also as a GNU
933 extension, variadic arguments are allowed to not appear in
780 the invocation at all. 934 the invocation at all.
781 e.g. #define debug(format, args...) something 935 e.g. #define debug(format, args...) something
782 debug("string"); 936 debug("string");
783 937
784 This is exactly the same as if an empty variadic list had been 938 This is exactly the same as if an empty variadic list had been
785 supplied - debug("string", ). */ 939 supplied - debug("string", ). */
786 940
787 if (argc + 1 == macro->paramc && macro->variadic) 941 if (argc + 1 == macro->paramc && macro->variadic)
788 { 942 {
789 if (CPP_PEDANTIC (pfile) && ! macro->syshdr) 943 if (CPP_PEDANTIC (pfile) && ! macro->syshdr
944 && ! CPP_OPTION (pfile, va_opt))
790 { 945 {
791 if (CPP_OPTION (pfile, cplusplus)) 946 if (CPP_OPTION (pfile, cplusplus))
792 cpp_error (pfile, CPP_DL_PEDWARN, 947 cpp_error (pfile, CPP_DL_PEDWARN,
793 "ISO C++11 requires at least one argument " 948 "ISO C++11 requires at least one argument "
794 "for the \"...\" in a variadic macro"); 949 "for the \"...\" in a variadic macro");
806 } 961 }
807 else 962 else
808 cpp_error (pfile, CPP_DL_ERROR, 963 cpp_error (pfile, CPP_DL_ERROR,
809 "macro \"%s\" passed %u arguments, but takes just %u", 964 "macro \"%s\" passed %u arguments, but takes just %u",
810 NODE_NAME (node), argc, macro->paramc); 965 NODE_NAME (node), argc, macro->paramc);
966
967 if (macro->line > RESERVED_LOCATION_COUNT)
968 cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here",
969 NODE_NAME (node));
811 970
812 return false; 971 return false;
813 } 972 }
814 973
815 /* Reads and returns the arguments to a function-like macro 974 /* Reads and returns the arguments to a function-like macro
1078 1237
1079 /* Return the real number of tokens in the expansion of MACRO. */ 1238 /* Return the real number of tokens in the expansion of MACRO. */
1080 static inline unsigned int 1239 static inline unsigned int
1081 macro_real_token_count (const cpp_macro *macro) 1240 macro_real_token_count (const cpp_macro *macro)
1082 { 1241 {
1083 unsigned int i;
1084 if (__builtin_expect (!macro->extra_tokens, true)) 1242 if (__builtin_expect (!macro->extra_tokens, true))
1085 return macro->count; 1243 return macro->count;
1086 for (i = 0; i < macro->count; i++) 1244
1087 if (macro->exp.tokens[i].type == CPP_PASTE) 1245 for (unsigned i = macro->count; i--;)
1088 return i; 1246 if (macro->exp.tokens[i].type != CPP_PASTE)
1089 abort (); 1247 return i + 1;
1248
1249 return 0;
1090 } 1250 }
1091 1251
1092 /* Push the context of a macro with hash entry NODE onto the context 1252 /* Push the context of a macro with hash entry NODE onto the context
1093 stack. If we can successfully expand the macro, we push a context 1253 stack. If we can successfully expand the macro, we push a context
1094 containing its yet-to-be-rescanned replacement list and return one. 1254 containing its yet-to-be-rescanned replacement list and return one.
1116 location once the process of expanding the macro starts; that is, 1276 location once the process of expanding the macro starts; that is,
1117 we must not do that recording between now and later down this 1277 we must not do that recording between now and later down this
1118 function where set this flag to FALSE. */ 1278 function where set this flag to FALSE. */
1119 pfile->about_to_expand_macro_p = true; 1279 pfile->about_to_expand_macro_p = true;
1120 1280
1121 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED)) 1281 if (cpp_user_macro_p (node))
1122 {
1123 node->flags |= NODE_USED;
1124 if ((!pfile->cb.user_builtin_macro
1125 || !pfile->cb.user_builtin_macro (pfile, node))
1126 && pfile->cb.used_define)
1127 pfile->cb.used_define (pfile, pfile->directive_line, node);
1128 }
1129
1130 /* Handle standard macros. */
1131 if (! (node->flags & NODE_BUILTIN))
1132 { 1282 {
1133 cpp_macro *macro = node->value.macro; 1283 cpp_macro *macro = node->value.macro;
1134 _cpp_buff *pragma_buff = NULL; 1284 _cpp_buff *pragma_buff = NULL;
1135 1285
1136 if (macro->fun_like) 1286 if (macro->fun_like)
1172 } 1322 }
1173 1323
1174 /* Disable the macro within its expansion. */ 1324 /* Disable the macro within its expansion. */
1175 node->flags |= NODE_DISABLED; 1325 node->flags |= NODE_DISABLED;
1176 1326
1177 if (!(node->flags & NODE_USED)) 1327 /* Laziness can only affect the expansion tokens of the macro,
1178 { 1328 not its fun-likeness or parameters. */
1179 node->flags |= NODE_USED; 1329 _cpp_maybe_notify_macro_use (pfile, node);
1180 if (pfile->cb.used_define)
1181 pfile->cb.used_define (pfile, pfile->directive_line, node);
1182 }
1183
1184 if (pfile->cb.used) 1330 if (pfile->cb.used)
1185 pfile->cb.used (pfile, location, node); 1331 pfile->cb.used (pfile, location, node);
1186 1332
1187 macro->used = 1; 1333 macro->used = 1;
1188 1334
1253 } 1399 }
1254 1400
1255 pfile->about_to_expand_macro_p = false; 1401 pfile->about_to_expand_macro_p = false;
1256 /* Handle built-in macros and the _Pragma operator. */ 1402 /* Handle built-in macros and the _Pragma operator. */
1257 { 1403 {
1258 source_location loc, expand_loc; 1404 source_location expand_loc;
1259 1405
1260 if (/* The top-level macro invocation that triggered the expansion 1406 if (/* The top-level macro invocation that triggered the expansion
1261 we are looking at is with a standard macro ...*/ 1407 we are looking at is with a function-like user macro ... */
1262 !(pfile->top_most_macro_node->flags & NODE_BUILTIN) 1408 cpp_fun_like_macro_p (pfile->top_most_macro_node)
1263 /* ... and it's a function-like macro invocation. */ 1409 /* ... and we are tracking the macro expansion. */
1264 && pfile->top_most_macro_node->value.macro->fun_like) 1410 && CPP_OPTION (pfile, track_macro_expansion))
1265 { 1411 /* Then the location of the end of the macro invocation is the
1266 /* Then the location of the end of the macro invocation is the 1412 location of the expansion point of this macro. */
1267 location of the closing parenthesis. */ 1413 expand_loc = location;
1268 loc = pfile->cur_token[-1].src_loc;
1269 expand_loc = loc;
1270 }
1271 else 1414 else
1272 { 1415 /* Otherwise, the location of the end of the macro invocation is
1273 /* Otherwise, the location of the end of the macro invocation is 1416 the location of the expansion point of that top-level macro
1274 the location of the expansion point of that top-level macro 1417 invocation. */
1275 invocation. */ 1418 expand_loc = pfile->invocation_location;
1276 loc = location; 1419
1277 expand_loc = pfile->invocation_location; 1420 return builtin_macro (pfile, node, location, expand_loc);
1278 }
1279
1280 return builtin_macro (pfile, node, loc, expand_loc);
1281 } 1421 }
1282 } 1422 }
1283 1423
1284 /* De-allocate the memory used by BUFF which is an array of instances 1424 /* De-allocate the memory used by BUFF which is an array of instances
1285 of macro_arg. NUM_ARGS is the number of instances of macro_arg 1425 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1546 if (CPP_OPTION (pfile, track_macro_expansion) > 1) 1686 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1547 return absolute_token_index; 1687 return absolute_token_index;
1548 return cur_replacement_token - macro->exp.tokens; 1688 return cur_replacement_token - macro->exp.tokens;
1549 } 1689 }
1550 1690
1691 /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1692
1693 static void
1694 copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag,
1695 const cpp_token *src)
1696 {
1697 cpp_token *token = _cpp_temp_token (pfile);
1698 token->type = (*paste_flag)->type;
1699 token->val = (*paste_flag)->val;
1700 if (src->flags & PASTE_LEFT)
1701 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1702 else
1703 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1704 *paste_flag = token;
1705 }
1706
1707 /* True IFF the last token emitted into BUFF (if any) is PTR. */
1708
1709 static bool
1710 last_token_is (_cpp_buff *buff, const cpp_token **ptr)
1711 {
1712 return (ptr && tokens_buff_last_token_ptr (buff) == ptr);
1713 }
1714
1551 /* Replace the parameters in a function-like macro of NODE with the 1715 /* Replace the parameters in a function-like macro of NODE with the
1552 actual ARGS, and place the result in a newly pushed token context. 1716 actual ARGS, and place the result in a newly pushed token context.
1553 Expand each argument before replacing, unless it is operated upon 1717 Expand each argument before replacing, unless it is operated upon
1554 by the # or ## operators. EXPANSION_POINT_LOC is the location of 1718 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1555 the expansion point of the macro. E.g, the location of the 1719 the expansion point of the macro. E.g, the location of the
1599 { 1763 {
1600 if (!arg->stringified) 1764 if (!arg->stringified)
1601 arg->stringified = stringify_arg (pfile, arg); 1765 arg->stringified = stringify_arg (pfile, arg);
1602 } 1766 }
1603 else if ((src->flags & PASTE_LEFT) 1767 else if ((src->flags & PASTE_LEFT)
1604 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) 1768 || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1605 total += arg->count - 1; 1769 total += arg->count - 1;
1606 else 1770 else
1607 { 1771 {
1608 if (!arg->expanded) 1772 if (!arg->expanded)
1609 expand_arg (pfile, arg); 1773 expand_arg (pfile, arg);
1664 /* Then the number of macro tokens won't take in account the 1828 /* Then the number of macro tokens won't take in account the
1665 fact that function-like macro arguments can expand to 1829 fact that function-like macro arguments can expand to
1666 multiple tokens. This is to save memory at the expense of 1830 multiple tokens. This is to save memory at the expense of
1667 accuracy. 1831 accuracy.
1668 1832
1669 Suppose we have #define SQARE(A) A * A 1833 Suppose we have #define SQUARE(A) A * A
1670 1834
1671 And then we do SQARE(2+3) 1835 And then we do SQUARE(2+3)
1672 1836
1673 Then the tokens 2, +, 3, will have the same location, 1837 Then the tokens 2, +, 3, will have the same location,
1674 saying they come from the expansion of the argument A. */ 1838 saying they come from the expansion of the argument A. */
1675 num_macro_tokens = exp_count; 1839 num_macro_tokens = exp_count;
1676 map = linemap_enter_macro (pfile->line_table, node, 1840 map = linemap_enter_macro (pfile->line_table, node,
1677 expansion_point_loc, 1841 expansion_point_loc,
1678 num_macro_tokens); 1842 num_macro_tokens);
1679 } 1843 }
1680 i = 0; 1844 i = 0;
1845 vaopt_state vaopt_tracker (pfile, macro->variadic,
1846 args[macro->paramc - 1].count > 0);
1847 const cpp_token **vaopt_start = NULL;
1681 for (src = macro->exp.tokens; src < limit; src++) 1848 for (src = macro->exp.tokens; src < limit; src++)
1682 { 1849 {
1683 unsigned int arg_tokens_count; 1850 unsigned int arg_tokens_count;
1684 macro_arg_token_iter from; 1851 macro_arg_token_iter from;
1685 const cpp_token **paste_flag = NULL; 1852 const cpp_token **paste_flag = NULL;
1686 const cpp_token **tmp_token_ptr; 1853 const cpp_token **tmp_token_ptr;
1854
1855 /* __VA_OPT__ handling. */
1856 vaopt_state::update_type vostate = vaopt_tracker.update (src);
1857 if (vostate != vaopt_state::INCLUDE)
1858 {
1859 if (vostate == vaopt_state::BEGIN)
1860 {
1861 /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
1862 if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1863 {
1864 const cpp_token *t = padding_token (pfile, src);
1865 unsigned index = expanded_token_index (pfile, macro, src, i);
1866 /* Allocate a virtual location for the padding token and
1867 append the token and its location to BUFF and
1868 VIRT_LOCS. */
1869 tokens_buff_add_token (buff, virt_locs, t,
1870 t->src_loc, t->src_loc,
1871 map, index);
1872 }
1873 vaopt_start = tokens_buff_last_token_ptr (buff);
1874 }
1875 else if (vostate == vaopt_state::END)
1876 {
1877 const cpp_token **start = vaopt_start;
1878 vaopt_start = NULL;
1879
1880 /* Remove any tail padding from inside the __VA_OPT__. */
1881 paste_flag = tokens_buff_last_token_ptr (buff);
1882 while (paste_flag && paste_flag != start
1883 && (*paste_flag)->type == CPP_PADDING)
1884 {
1885 tokens_buff_remove_last_token (buff);
1886 paste_flag = tokens_buff_last_token_ptr (buff);
1887 }
1888
1889 if (src->flags & PASTE_LEFT)
1890 {
1891 /* With a non-empty __VA_OPT__ on the LHS of ##, the last
1892 token should be flagged PASTE_LEFT. */
1893 if (paste_flag && (*paste_flag)->type != CPP_PADDING)
1894 copy_paste_flag (pfile, paste_flag, src);
1895 }
1896 else
1897 {
1898 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
1899 __VA_OPT__(c)__VA_OPT__(d). */
1900 const cpp_token *t = &pfile->avoid_paste;
1901 tokens_buff_add_token (buff, virt_locs,
1902 t, t->src_loc, t->src_loc,
1903 NULL, 0);
1904 }
1905 }
1906 continue;
1907 }
1687 1908
1688 if (src->type != CPP_MACRO_ARG) 1909 if (src->type != CPP_MACRO_ARG)
1689 { 1910 {
1690 /* Allocate a virtual location for token SRC, and add that 1911 /* Allocate a virtual location for token SRC, and add that
1691 token and its virtual location into the buffers BUFF and 1912 token and its virtual location into the buffers BUFF and
1760 if (macro_arg_token_iter_get_token (&from) == NULL) 1981 if (macro_arg_token_iter_get_token (&from) == NULL)
1761 tokens_buff_remove_last_token (buff); 1982 tokens_buff_remove_last_token (buff);
1762 else 1983 else
1763 paste_flag = tmp_token_ptr; 1984 paste_flag = tmp_token_ptr;
1764 } 1985 }
1765 /* Remove the paste flag if the RHS is a placemarker. */ 1986 /* Remove the paste flag if the RHS is a placemarker, unless the
1766 else if (arg_tokens_count == 0) 1987 previous emitted token is at the beginning of __VA_OPT__;
1988 placemarkers within __VA_OPT__ are ignored in that case. */
1989 else if (arg_tokens_count == 0
1990 && tmp_token_ptr != vaopt_start)
1767 paste_flag = tmp_token_ptr; 1991 paste_flag = tmp_token_ptr;
1768 } 1992 }
1769 } 1993 }
1770 else 1994 else
1771 { 1995 {
1773 macro_arg_token_iter_init (&from, 1997 macro_arg_token_iter_init (&from,
1774 CPP_OPTION (pfile, 1998 CPP_OPTION (pfile,
1775 track_macro_expansion), 1999 track_macro_expansion),
1776 MACRO_ARG_TOKEN_EXPANDED, 2000 MACRO_ARG_TOKEN_EXPANDED,
1777 arg, arg->expanded); 2001 arg, arg->expanded);
2002
2003 if (last_token_is (buff, vaopt_start))
2004 {
2005 /* We're expanding an arg at the beginning of __VA_OPT__.
2006 Skip padding. */
2007 while (arg_tokens_count)
2008 {
2009 const cpp_token *t = macro_arg_token_iter_get_token (&from);
2010 if (t->type != CPP_PADDING)
2011 break;
2012 macro_arg_token_iter_forward (&from);
2013 --arg_tokens_count;
2014 }
2015 }
1778 } 2016 }
1779 2017
1780 /* Padding on the left of an argument (unless RHS of ##). */ 2018 /* Padding on the left of an argument (unless RHS of ##). */
1781 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) 2019 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1782 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) 2020 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)
2021 && !last_token_is (buff, vaopt_start))
1783 { 2022 {
1784 const cpp_token *t = padding_token (pfile, src); 2023 const cpp_token *t = padding_token (pfile, src);
1785 unsigned index = expanded_token_index (pfile, macro, src, i); 2024 unsigned index = expanded_token_index (pfile, macro, src, i);
1786 /* Allocate a virtual location for the padding token and 2025 /* Allocate a virtual location for the padding token and
1787 append the token and its location to BUFF and 2026 append the token and its location to BUFF and
1801 { 2040 {
1802 /* So if track_macro_exp is < 2, the user wants to 2041 /* So if track_macro_exp is < 2, the user wants to
1803 save extra memory while tracking macro expansion 2042 save extra memory while tracking macro expansion
1804 locations. So in that case here is what we do: 2043 locations. So in that case here is what we do:
1805 2044
1806 Suppose we have #define SQARE(A) A * A 2045 Suppose we have #define SQUARE(A) A * A
1807 2046
1808 And then we do SQARE(2+3) 2047 And then we do SQUARE(2+3)
1809 2048
1810 Then the tokens 2, +, 3, will have the same location, 2049 Then the tokens 2, +, 3, will have the same location,
1811 saying they come from the expansion of the argument 2050 saying they come from the expansion of the argument
1812 A. 2051 A.
1813 2052
1814 So that means we are going to ignore the COUNT tokens 2053 So that means we are going to ignore the COUNT tokens
1815 resulting from the expansion of the current macro 2054 resulting from the expansion of the current macro
1816 arugment. In other words all the ARG_TOKENS_COUNT tokens 2055 argument. In other words all the ARG_TOKENS_COUNT tokens
1817 resulting from the expansion of the macro argument will 2056 resulting from the expansion of the macro argument will
1818 have the index I. Normally, each of those token should 2057 have the index I. Normally, each of those tokens should
1819 have index I+J. */ 2058 have index I+J. */
1820 unsigned token_index = i; 2059 unsigned token_index = i;
1821 unsigned index; 2060 unsigned index;
1822 if (track_macro_exp > 1) 2061 if (track_macro_exp > 1)
1823 token_index += j; 2062 token_index += j;
1831 } 2070 }
1832 2071
1833 /* With a non-empty argument on the LHS of ##, the last 2072 /* With a non-empty argument on the LHS of ##, the last
1834 token should be flagged PASTE_LEFT. */ 2073 token should be flagged PASTE_LEFT. */
1835 if (src->flags & PASTE_LEFT) 2074 if (src->flags & PASTE_LEFT)
1836 paste_flag = 2075 paste_flag
1837 (const cpp_token **) tokens_buff_last_token_ptr (buff); 2076 = (const cpp_token **) tokens_buff_last_token_ptr (buff);
1838 } 2077 }
1839 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99) 2078 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
1840 && ! macro->syshdr && ! cpp_in_system_header (pfile)) 2079 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1841 { 2080 {
1842 if (CPP_OPTION (pfile, cplusplus)) 2081 if (CPP_OPTION (pfile, cplusplus))
1862 "empty macro arguments are undefined" 2101 "empty macro arguments are undefined"
1863 " in ISO C90", 2102 " in ISO C90",
1864 NODE_NAME (node), src->val.macro_arg.arg_no); 2103 NODE_NAME (node), src->val.macro_arg.arg_no);
1865 2104
1866 /* Avoid paste on RHS (even case count == 0). */ 2105 /* Avoid paste on RHS (even case count == 0). */
1867 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) 2106 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)
2107 && !last_token_is (buff, vaopt_start))
1868 { 2108 {
1869 const cpp_token *t = &pfile->avoid_paste; 2109 const cpp_token *t = &pfile->avoid_paste;
1870 tokens_buff_add_token (buff, virt_locs, 2110 tokens_buff_add_token (buff, virt_locs,
1871 t, t->src_loc, t->src_loc, 2111 t, t->src_loc, t->src_loc,
1872 NULL, 0); 2112 NULL, 0);
1873 } 2113 }
1874 2114
1875 /* Add a new paste flag, or remove an unwanted one. */ 2115 /* Add a new paste flag, or remove an unwanted one. */
1876 if (paste_flag) 2116 if (paste_flag)
1877 { 2117 copy_paste_flag (pfile, paste_flag, src);
1878 cpp_token *token = _cpp_temp_token (pfile);
1879 token->type = (*paste_flag)->type;
1880 token->val = (*paste_flag)->val;
1881 if (src->flags & PASTE_LEFT)
1882 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1883 else
1884 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1885 *paste_flag = token;
1886 }
1887 2118
1888 i += arg_tokens_count; 2119 i += arg_tokens_count;
1889 } 2120 }
1890 2121
1891 if (track_macro_exp) 2122 if (track_macro_exp)
2052 /* Return a pointer to the last token contained in the token buffer 2283 /* Return a pointer to the last token contained in the token buffer
2053 BUFF. */ 2284 BUFF. */
2054 static const cpp_token ** 2285 static const cpp_token **
2055 tokens_buff_last_token_ptr (_cpp_buff *buff) 2286 tokens_buff_last_token_ptr (_cpp_buff *buff)
2056 { 2287 {
2288 if (BUFF_FRONT (buff) == buff->base)
2289 return NULL;
2057 return &((const cpp_token **) BUFF_FRONT (buff))[-1]; 2290 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2058 } 2291 }
2059 2292
2060 /* Remove the last token contained in the token buffer TOKENS_BUFF. 2293 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2061 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer 2294 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2500 if (result->type != CPP_NAME) 2733 if (result->type != CPP_NAME)
2501 break; 2734 break;
2502 2735
2503 node = result->val.node.node; 2736 node = result->val.node.node;
2504 2737
2505 if (node->type != NT_MACRO || (result->flags & NO_EXPAND)) 2738 if (node->type == NT_VOID || (result->flags & NO_EXPAND))
2506 break; 2739 break;
2507 2740
2508 if (!(node->flags & NODE_DISABLED)) 2741 if (!(node->flags & NODE_DISABLED))
2509 { 2742 {
2510 int ret = 0; 2743 int ret = 0;
2750 /* Returns nonzero if a macro redefinition warning is required. */ 2983 /* Returns nonzero if a macro redefinition warning is required. */
2751 static bool 2984 static bool
2752 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, 2985 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2753 const cpp_macro *macro2) 2986 const cpp_macro *macro2)
2754 { 2987 {
2755 const cpp_macro *macro1;
2756 unsigned int i; 2988 unsigned int i;
2757 2989
2758 /* Some redefinitions need to be warned about regardless. */ 2990 /* Some redefinitions need to be warned about regardless. */
2759 if (node->flags & NODE_WARN) 2991 if (node->flags & NODE_WARN)
2760 return true; 2992 return true;
2761 2993
2762 /* Suppress warnings for builtins that lack the NODE_WARN flag, 2994 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2763 unless Wbuiltin-macro-redefined. */ 2995 unless Wbuiltin-macro-redefined. */
2764 if (node->flags & NODE_BUILTIN 2996 if (cpp_builtin_macro_p (node))
2765 && (!pfile->cb.user_builtin_macro
2766 || !pfile->cb.user_builtin_macro (pfile, node)))
2767 return CPP_OPTION (pfile, warn_builtin_macro_redefined); 2997 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
2768 2998
2769 /* Redefinitions of conditional (context-sensitive) macros, on 2999 /* Redefinitions of conditional (context-sensitive) macros, on
2770 the other hand, must be allowed silently. */ 3000 the other hand, must be allowed silently. */
2771 if (node->flags & NODE_CONDITIONAL) 3001 if (node->flags & NODE_CONDITIONAL)
2772 return false; 3002 return false;
2773 3003
3004 cpp_macro *macro1 = node->value.macro;
3005 if (macro1->lazy)
3006 {
3007 /* We don't want to mark MACRO as used, but do need to finalize
3008 its laziness. */
3009 pfile->cb.user_lazy_macro (pfile, macro1, macro1->lazy - 1);
3010 macro1->lazy = 0;
3011 }
3012
2774 /* Redefinition of a macro is allowed if and only if the old and new 3013 /* Redefinition of a macro is allowed if and only if the old and new
2775 definitions are the same. (6.10.3 paragraph 2). */ 3014 definitions are the same. (6.10.3 paragraph 2). */
2776 macro1 = node->value.macro;
2777 3015
2778 /* Don't check count here as it can be different in valid 3016 /* Don't check count here as it can be different in valid
2779 traditional redefinitions with just whitespace differences. */ 3017 traditional redefinitions with just whitespace differences. */
2780 if (macro1->paramc != macro2->paramc 3018 if (macro1->paramc != macro2->paramc
2781 || macro1->fun_like != macro2->fun_like 3019 || macro1->fun_like != macro2->fun_like
2782 || macro1->variadic != macro2->variadic) 3020 || macro1->variadic != macro2->variadic)
2783 return true; 3021 return true;
2784 3022
2785 /* Check parameter spellings. */ 3023 /* Check parameter spellings. */
2786 for (i = 0; i < macro1->paramc; i++) 3024 for (i = 0; i < macro1->paramc; i++)
2787 if (macro1->params[i] != macro2->params[i]) 3025 if (macro1->parm.params[i] != macro2->parm.params[i])
2788 return true; 3026 return true;
2789 3027
2790 /* Check the replacement text or tokens. */ 3028 /* Check the replacement text or tokens. */
2791 if (CPP_OPTION (pfile, traditional)) 3029 if (CPP_OPTION (pfile, traditional))
2792 return _cpp_expansions_different_trad (macro1, macro2); 3030 return _cpp_expansions_different_trad (macro1, macro2);
2805 void 3043 void
2806 _cpp_free_definition (cpp_hashnode *h) 3044 _cpp_free_definition (cpp_hashnode *h)
2807 { 3045 {
2808 /* Macros and assertions no longer have anything to free. */ 3046 /* Macros and assertions no longer have anything to free. */
2809 h->type = NT_VOID; 3047 h->type = NT_VOID;
2810 /* Clear builtin flag in case of redefinition. */ 3048 h->value.answers = NULL;
2811 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED); 3049 h->flags &= ~(NODE_DISABLED | NODE_USED);
2812 } 3050 }
2813 3051
2814 /* Save parameter NODE (spelling SPELLING) to the parameter list of 3052 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2815 macro MACRO. Returns zero on success, nonzero if the parameter is 3053 macro MACRO. Returns true on success, false on failure. */
2816 a duplicate. */
2817 bool 3054 bool
2818 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node, 3055 _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node,
2819 cpp_hashnode *spelling) 3056 cpp_hashnode *spelling)
2820 { 3057 {
2821 unsigned int len;
2822 /* Constraint 6.10.3.6 - duplicate parameter names. */ 3058 /* Constraint 6.10.3.6 - duplicate parameter names. */
2823 if (node->flags & NODE_MACRO_ARG) 3059 if (node->type == NT_MACRO_ARG)
2824 { 3060 {
2825 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"", 3061 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2826 NODE_NAME (node)); 3062 NODE_NAME (node));
2827 return true; 3063 return false;
2828 } 3064 }
2829 3065
2830 if (BUFF_ROOM (pfile->a_buff) 3066 unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data);
2831 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2832 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2833
2834 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
2835 node->flags |= NODE_MACRO_ARG;
2836 len = macro->paramc * sizeof (struct macro_arg_saved_data);
2837 if (len > pfile->macro_buffer_len) 3067 if (len > pfile->macro_buffer_len)
2838 { 3068 {
2839 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer, 3069 pfile->macro_buffer
2840 len); 3070 = XRESIZEVEC (unsigned char, pfile->macro_buffer, len);
2841 pfile->macro_buffer_len = len; 3071 pfile->macro_buffer_len = len;
2842 } 3072 }
2843 struct macro_arg_saved_data save;
2844 save.value = node->value;
2845 save.canonical_node = node;
2846 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
2847 = save;
2848 3073
2849 node->value.arg_index = macro->paramc; 3074 macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer;
2850 return false; 3075 saved[n].canonical_node = node;
2851 } 3076 saved[n].value = node->value;
2852 3077 saved[n].type = node->type;
2853 /* Check the syntax of the parameters in a MACRO definition. Returns 3078
2854 false if an error occurs. */ 3079 void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *),
3080 sizeof (cpp_hashnode *));
3081 ((cpp_hashnode **)base)[n] = spelling;
3082
3083 /* Morph into a macro arg. */
3084 node->type = NT_MACRO_ARG;
3085 /* Index is 1 based. */
3086 node->value.arg_index = n + 1;
3087
3088 return true;
3089 }
3090
3091 /* Restore the parameters to their previous state. */
3092 void
3093 _cpp_unsave_parameters (cpp_reader *pfile, unsigned n)
3094 {
3095 /* Clear the fast argument lookup indices. */
3096 while (n--)
3097 {
3098 struct macro_arg_saved_data *save =
3099 &((struct macro_arg_saved_data *) pfile->macro_buffer)[n];
3100
3101 struct cpp_hashnode *node = save->canonical_node;
3102 node->type = save->type;
3103 node->value = save->value;
3104 }
3105 }
3106
3107 /* Check the syntax of the parameters in a MACRO definition. Return
3108 false on failure. Set *N_PTR and *VARADIC_PTR as appropriate.
3109 '(' ')'
3110 '(' parm-list ',' last-parm ')'
3111 '(' last-parm ')'
3112 parm-list: name
3113 | parm-list, name
3114 last-parm: name
3115 | name '...'
3116 | '...'
3117 */
3118
2855 static bool 3119 static bool
2856 parse_params (cpp_reader *pfile, cpp_macro *macro) 3120 parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr)
2857 { 3121 {
2858 unsigned int prev_ident = 0; 3122 unsigned nparms = 0;
2859 3123 bool ok = false;
2860 for (;;) 3124
3125 for (bool prev_ident = false;;)
2861 { 3126 {
2862 const cpp_token *token = _cpp_lex_token (pfile); 3127 const cpp_token *token = _cpp_lex_token (pfile);
2863 3128
2864 switch (token->type) 3129 switch (token->type)
2865 { 3130 {
2866 default: 3131 case CPP_COMMENT:
2867 /* Allow/ignore comments in parameter lists if we are 3132 /* Allow/ignore comments in parameter lists if we are
2868 preserving comments in macro expansions. */ 3133 preserving comments in macro expansions. */
2869 if (token->type == CPP_COMMENT 3134 if (!CPP_OPTION (pfile, discard_comments_in_macro_exp))
2870 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)) 3135 break;
2871 continue; 3136
2872 3137 /* FALLTHRU */
2873 cpp_error (pfile, CPP_DL_ERROR, 3138 default:
2874 "\"%s\" may not appear in macro parameter list", 3139 bad:
2875 cpp_token_as_text (pfile, token)); 3140 {
2876 return false; 3141 const char *const msgs[5] =
3142 {
3143 N_("expected parameter name, found \"%s\""),
3144 N_("expected ',' or ')', found \"%s\""),
3145 N_("expected parameter name before end of line"),
3146 N_("expected ')' before end of line"),
3147 N_("expected ')' after \"...\"")
3148 };
3149 unsigned ix = prev_ident;
3150 const unsigned char *as_text = NULL;
3151 if (*varadic_ptr)
3152 ix = 4;
3153 else if (token->type == CPP_EOF)
3154 ix += 2;
3155 else
3156 as_text = cpp_token_as_text (pfile, token);
3157 cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text);
3158 }
3159 goto out;
2877 3160
2878 case CPP_NAME: 3161 case CPP_NAME:
2879 if (prev_ident) 3162 if (prev_ident || *varadic_ptr)
3163 goto bad;
3164 prev_ident = true;
3165
3166 if (!_cpp_save_parameter (pfile, nparms, token->val.node.node,
3167 token->val.node.spelling))
3168 goto out;
3169 nparms++;
3170 break;
3171
3172 case CPP_CLOSE_PAREN:
3173 if (prev_ident || !nparms || *varadic_ptr)
2880 { 3174 {
2881 cpp_error (pfile, CPP_DL_ERROR, 3175 ok = true;
2882 "macro parameters must be comma-separated"); 3176 goto out;
2883 return false;
2884 } 3177 }
2885 prev_ident = 1; 3178
2886
2887 if (_cpp_save_parameter (pfile, macro, token->val.node.node,
2888 token->val.node.spelling))
2889 return false;
2890 continue;
2891
2892 case CPP_CLOSE_PAREN:
2893 if (prev_ident || macro->paramc == 0)
2894 return true;
2895
2896 /* Fall through to pick up the error. */
2897 /* FALLTHRU */ 3179 /* FALLTHRU */
2898 case CPP_COMMA: 3180 case CPP_COMMA:
3181 if (!prev_ident || *varadic_ptr)
3182 goto bad;
3183 prev_ident = false;
3184 break;
3185
3186 case CPP_ELLIPSIS:
3187 if (*varadic_ptr)
3188 goto bad;
3189 *varadic_ptr = true;
2899 if (!prev_ident) 3190 if (!prev_ident)
2900 { 3191 {
2901 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing"); 3192 /* An ISO bare ellipsis. */
2902 return false; 3193 _cpp_save_parameter (pfile, nparms,
2903 }
2904 prev_ident = 0;
2905 continue;
2906
2907 case CPP_ELLIPSIS:
2908 macro->variadic = 1;
2909 if (!prev_ident)
2910 {
2911 _cpp_save_parameter (pfile, macro,
2912 pfile->spec_nodes.n__VA_ARGS__, 3194 pfile->spec_nodes.n__VA_ARGS__,
2913 pfile->spec_nodes.n__VA_ARGS__); 3195 pfile->spec_nodes.n__VA_ARGS__);
3196 nparms++;
2914 pfile->state.va_args_ok = 1; 3197 pfile->state.va_args_ok = 1;
2915 if (! CPP_OPTION (pfile, c99) 3198 if (! CPP_OPTION (pfile, c99)
2916 && CPP_OPTION (pfile, cpp_pedantic) 3199 && CPP_OPTION (pfile, cpp_pedantic)
2917 && CPP_OPTION (pfile, warn_variadic_macros)) 3200 && CPP_OPTION (pfile, warn_variadic_macros))
2918 { 3201 cpp_pedwarning
2919 if (CPP_OPTION (pfile, cplusplus)) 3202 (pfile, CPP_W_VARIADIC_MACROS,
2920 cpp_pedwarning 3203 CPP_OPTION (pfile, cplusplus)
2921 (pfile, CPP_W_VARIADIC_MACROS, 3204 ? N_("anonymous variadic macros were introduced in C++11")
2922 "anonymous variadic macros were introduced in C++11"); 3205 : N_("anonymous variadic macros were introduced in C99"));
2923 else
2924 cpp_pedwarning
2925 (pfile, CPP_W_VARIADIC_MACROS,
2926 "anonymous variadic macros were introduced in C99");
2927 }
2928 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 3206 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2929 && ! CPP_OPTION (pfile, cplusplus)) 3207 && ! CPP_OPTION (pfile, cplusplus))
2930 cpp_error (pfile, CPP_DL_WARNING, 3208 cpp_error (pfile, CPP_DL_WARNING,
2931 "anonymous variadic macros were introduced in C99"); 3209 "anonymous variadic macros were introduced in C99");
2932 } 3210 }
2933 else if (CPP_OPTION (pfile, cpp_pedantic) 3211 else if (CPP_OPTION (pfile, cpp_pedantic)
2934 && CPP_OPTION (pfile, warn_variadic_macros)) 3212 && CPP_OPTION (pfile, warn_variadic_macros))
2935 { 3213 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2936 if (CPP_OPTION (pfile, cplusplus)) 3214 CPP_OPTION (pfile, cplusplus)
2937 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, 3215 ? N_("ISO C++ does not permit named variadic macros")
2938 "ISO C++ does not permit named variadic macros"); 3216 : N_("ISO C does not permit named variadic macros"));
2939 else 3217 break;
2940 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, 3218 }
2941 "ISO C does not permit named variadic macros"); 3219 }
2942 } 3220
2943 3221 out:
2944 /* We're at the end, and just expect a closing parenthesis. */ 3222 *n_ptr = nparms;
2945 token = _cpp_lex_token (pfile); 3223
2946 if (token->type == CPP_CLOSE_PAREN) 3224 return ok;
2947 return true;
2948 /* Fall through. */
2949
2950 case CPP_EOF:
2951 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2952 return false;
2953 }
2954 }
2955 }
2956
2957 /* Allocate room for a token from a macro's replacement list. */
2958 static cpp_token *
2959 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2960 {
2961 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2962 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2963
2964 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2965 } 3225 }
2966 3226
2967 /* Lex a token from the expansion of MACRO, but mark parameters as we 3227 /* Lex a token from the expansion of MACRO, but mark parameters as we
2968 find them and warn of traditional stringification. */ 3228 find them and warn of traditional stringification. */
2969 static cpp_token * 3229 static cpp_macro *
2970 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) 3230 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2971 { 3231 {
2972 cpp_token *token, *saved_cur_token; 3232 macro = (cpp_macro *)_cpp_reserve_room (pfile,
2973 3233 sizeof (cpp_macro) - sizeof (cpp_token)
2974 saved_cur_token = pfile->cur_token; 3234 + macro->count * sizeof (cpp_token),
2975 pfile->cur_token = alloc_expansion_token (pfile, macro); 3235 sizeof (cpp_token));
2976 token = _cpp_lex_direct (pfile); 3236 cpp_token *saved_cur_token = pfile->cur_token;
3237 pfile->cur_token = &macro->exp.tokens[macro->count];
3238 cpp_token *token = _cpp_lex_direct (pfile);
2977 pfile->cur_token = saved_cur_token; 3239 pfile->cur_token = saved_cur_token;
2978 3240
2979 /* Is this a parameter? */ 3241 /* Is this a parameter? */
2980 if (token->type == CPP_NAME 3242 if (token->type == CPP_NAME && token->val.node.node->type == NT_MACRO_ARG)
2981 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0) 3243 {
2982 { 3244 /* Morph into a parameter reference. */
2983 cpp_hashnode *spelling = token->val.node.spelling; 3245 cpp_hashnode *spelling = token->val.node.spelling;
2984 token->type = CPP_MACRO_ARG; 3246 token->type = CPP_MACRO_ARG;
2985 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; 3247 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2986 token->val.macro_arg.spelling = spelling; 3248 token->val.macro_arg.spelling = spelling;
2987 } 3249 }
2988 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 3250 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2989 && (token->type == CPP_STRING || token->type == CPP_CHAR)) 3251 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2990 check_trad_stringification (pfile, macro, &token->val.str); 3252 check_trad_stringification (pfile, macro, &token->val.str);
2991 3253
2992 return token; 3254 return macro;
2993 } 3255 }
2994 3256
2995 static bool 3257 static cpp_macro *
2996 create_iso_definition (cpp_reader *pfile, cpp_macro *macro) 3258 create_iso_definition (cpp_reader *pfile)
2997 { 3259 {
2998 cpp_token *token;
2999 const cpp_token *ctoken;
3000 bool following_paste_op = false; 3260 bool following_paste_op = false;
3001 const char *paste_op_error_msg = 3261 const char *paste_op_error_msg =
3002 N_("'##' cannot appear at either end of a macro expansion"); 3262 N_("'##' cannot appear at either end of a macro expansion");
3003 unsigned int num_extra_tokens = 0; 3263 unsigned int num_extra_tokens = 0;
3004 3264 unsigned nparms = 0;
3005 /* Get the first token of the expansion (or the '(' of a 3265 cpp_hashnode **params = NULL;
3006 function-like macro). */ 3266 bool varadic = false;
3007 ctoken = _cpp_lex_token (pfile); 3267 bool ok = false;
3008 3268 cpp_macro *macro = NULL;
3009 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE)) 3269
3010 { 3270 /* Look at the first token, to see if this is a function-like
3011 bool ok = parse_params (pfile, macro); 3271 macro. */
3012 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); 3272 cpp_token first;
3013 if (!ok) 3273 cpp_token *saved_cur_token = pfile->cur_token;
3014 return false; 3274 pfile->cur_token = &first;
3015 3275 cpp_token *token = _cpp_lex_direct (pfile);
3016 /* Success. Commit or allocate the parameter array. */ 3276 pfile->cur_token = saved_cur_token;
3017 if (pfile->hash_table->alloc_subobject) 3277
3018 { 3278 if (token->flags & PREV_WHITE)
3019 cpp_hashnode **params = 3279 /* Preceeded by space, must be part of expansion. */;
3020 (cpp_hashnode **) pfile->hash_table->alloc_subobject 3280 else if (token->type == CPP_OPEN_PAREN)
3021 (sizeof (cpp_hashnode *) * macro->paramc); 3281 {
3022 memcpy (params, macro->params, 3282 /* An open-paren, get a parameter list. */
3023 sizeof (cpp_hashnode *) * macro->paramc); 3283 if (!parse_params (pfile, &nparms, &varadic))
3024 macro->params = params; 3284 goto out;
3025 } 3285
3026 else 3286 params = (cpp_hashnode **)_cpp_commit_buff
3027 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc]; 3287 (pfile, sizeof (cpp_hashnode *) * nparms);
3028 macro->fun_like = 1; 3288 token = NULL;
3029 } 3289 }
3030 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) 3290 else if (token->type != CPP_EOF
3291 && !(token->type == CPP_COMMENT
3292 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)))
3031 { 3293 {
3032 /* While ISO C99 requires whitespace before replacement text 3294 /* While ISO C99 requires whitespace before replacement text
3033 in a macro definition, ISO C90 with TC1 allows characters 3295 in a macro definition, ISO C90 with TC1 allows characters
3034 from the basic source character set there. */ 3296 from the basic source character set there. */
3035 if (CPP_OPTION (pfile, c99)) 3297 if (CPP_OPTION (pfile, c99))
3036 { 3298 cpp_error (pfile, CPP_DL_PEDWARN,
3037 if (CPP_OPTION (pfile, cplusplus)) 3299 CPP_OPTION (pfile, cplusplus)
3038 cpp_error (pfile, CPP_DL_PEDWARN, 3300 ? N_("ISO C++11 requires whitespace after the macro name")
3039 "ISO C++11 requires whitespace after the macro name"); 3301 : N_("ISO C99 requires whitespace after the macro name"));
3040 else
3041 cpp_error (pfile, CPP_DL_PEDWARN,
3042 "ISO C99 requires whitespace after the macro name");
3043 }
3044 else 3302 else
3045 { 3303 {
3046 int warntype = CPP_DL_WARNING; 3304 enum cpp_diagnostic_level warntype = CPP_DL_WARNING;
3047 switch (ctoken->type) 3305 switch (token->type)
3048 { 3306 {
3049 case CPP_ATSIGN: 3307 case CPP_ATSIGN:
3050 case CPP_AT_NAME: 3308 case CPP_AT_NAME:
3051 case CPP_OBJC_STRING: 3309 case CPP_OBJC_STRING:
3052 /* '@' is not in basic character set. */ 3310 /* '@' is not in basic character set. */
3053 warntype = CPP_DL_PEDWARN; 3311 warntype = CPP_DL_PEDWARN;
3054 break; 3312 break;
3055 case CPP_OTHER: 3313 case CPP_OTHER:
3056 /* Basic character set sans letters, digits and _. */ 3314 /* Basic character set sans letters, digits and _. */
3057 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~", 3315 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3058 ctoken->val.str.text[0]) == NULL) 3316 token->val.str.text[0]) == NULL)
3059 warntype = CPP_DL_PEDWARN; 3317 warntype = CPP_DL_PEDWARN;
3060 break; 3318 break;
3061 default: 3319 default:
3062 /* All other tokens start with a character from basic 3320 /* All other tokens start with a character from basic
3063 character set. */ 3321 character set. */
3066 cpp_error (pfile, warntype, 3324 cpp_error (pfile, warntype,
3067 "missing whitespace after the macro name"); 3325 "missing whitespace after the macro name");
3068 } 3326 }
3069 } 3327 }
3070 3328
3071 if (macro->fun_like) 3329 macro = _cpp_new_macro (pfile, cmk_macro,
3072 token = lex_expansion_token (pfile, macro); 3330 _cpp_reserve_room (pfile, 0, sizeof (cpp_macro)));
3331
3332 if (!token)
3333 {
3334 macro->variadic = varadic;
3335 macro->paramc = nparms;
3336 macro->parm.params = params;
3337 macro->fun_like = true;
3338 }
3073 else 3339 else
3074 { 3340 {
3075 token = alloc_expansion_token (pfile, macro); 3341 /* Preserve the token we peeked, there is already a single slot for it. */
3076 *token = *ctoken; 3342 macro->exp.tokens[0] = *token;
3077 } 3343 token = &macro->exp.tokens[0];
3078 3344 macro->count = 1;
3079 for (;;) 3345 }
3080 { 3346
3347 for (vaopt_state vaopt_tracker (pfile, macro->variadic, true);; token = NULL)
3348 {
3349 if (!token)
3350 {
3351 macro = lex_expansion_token (pfile, macro);
3352 token = &macro->exp.tokens[macro->count++];
3353 }
3354
3081 /* Check the stringifying # constraint 6.10.3.2.1 of 3355 /* Check the stringifying # constraint 6.10.3.2.1 of
3082 function-like macros when lexing the subsequent token. */ 3356 function-like macros when lexing the subsequent token. */
3083 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) 3357 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3084 { 3358 {
3085 if (token->type == CPP_MACRO_ARG) 3359 if (token->type == CPP_MACRO_ARG)
3097 /* Let assembler get away with murder. */ 3371 /* Let assembler get away with murder. */
3098 else if (CPP_OPTION (pfile, lang) != CLK_ASM) 3372 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3099 { 3373 {
3100 cpp_error (pfile, CPP_DL_ERROR, 3374 cpp_error (pfile, CPP_DL_ERROR,
3101 "'#' is not followed by a macro parameter"); 3375 "'#' is not followed by a macro parameter");
3102 return false; 3376 goto out;
3103 } 3377 }
3104 } 3378 }
3105 3379
3106 if (token->type == CPP_EOF) 3380 if (token->type == CPP_EOF)
3107 { 3381 {
3109 Token-paste ##, can appear in both object-like and 3383 Token-paste ##, can appear in both object-like and
3110 function-like macros, but not at the end. */ 3384 function-like macros, but not at the end. */
3111 if (following_paste_op) 3385 if (following_paste_op)
3112 { 3386 {
3113 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); 3387 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3114 return false; 3388 goto out;
3115 } 3389 }
3390 if (!vaopt_tracker.completed ())
3391 goto out;
3116 break; 3392 break;
3117 } 3393 }
3118 3394
3119 /* Paste operator constraint 6.10.3.3.1. */ 3395 /* Paste operator constraint 6.10.3.3.1. */
3120 if (token->type == CPP_PASTE) 3396 if (token->type == CPP_PASTE)
3122 /* Token-paste ##, can appear in both object-like and 3398 /* Token-paste ##, can appear in both object-like and
3123 function-like macros, but not at the beginning. */ 3399 function-like macros, but not at the beginning. */
3124 if (macro->count == 1) 3400 if (macro->count == 1)
3125 { 3401 {
3126 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); 3402 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3127 return false; 3403 goto out;
3128 } 3404 }
3129 3405
3130 if (token[-1].flags & PASTE_LEFT) 3406 if (following_paste_op)
3131 { 3407 {
3132 macro->extra_tokens = 1; 3408 /* Consecutive paste operators. This one will be moved
3409 to the end. */
3133 num_extra_tokens++; 3410 num_extra_tokens++;
3134 token->val.token_no = macro->count - 1; 3411 token->val.token_no = macro->count - 1;
3135 } 3412 }
3136 else 3413 else
3137 { 3414 {
3415 /* Drop the paste operator. */
3138 --macro->count; 3416 --macro->count;
3139 token[-1].flags |= PASTE_LEFT; 3417 token[-1].flags |= PASTE_LEFT;
3140 if (token->flags & DIGRAPH) 3418 if (token->flags & DIGRAPH)
3141 token[-1].flags |= SP_DIGRAPH; 3419 token[-1].flags |= SP_DIGRAPH;
3142 if (token->flags & PREV_WHITE) 3420 if (token->flags & PREV_WHITE)
3143 token[-1].flags |= SP_PREV_WHITE; 3421 token[-1].flags |= SP_PREV_WHITE;
3144 } 3422 }
3145 } 3423 following_paste_op = true;
3146 3424 }
3147 following_paste_op = (token->type == CPP_PASTE); 3425 else
3148 token = lex_expansion_token (pfile, macro); 3426 following_paste_op = false;
3149 } 3427
3150 3428 if (vaopt_tracker.update (token) == vaopt_state::ERROR)
3151 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff); 3429 goto out;
3152 macro->traditional = 0; 3430 }
3431
3432 /* We're committed to winning now. */
3433 ok = true;
3153 3434
3154 /* Don't count the CPP_EOF. */ 3435 /* Don't count the CPP_EOF. */
3155 macro->count--; 3436 macro->count--;
3437
3438 macro = (cpp_macro *)_cpp_commit_buff
3439 (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
3440 + sizeof (cpp_token) * macro->count);
3156 3441
3157 /* Clear whitespace on first token for warn_of_redefinition(). */ 3442 /* Clear whitespace on first token for warn_of_redefinition(). */
3158 if (macro->count) 3443 if (macro->count)
3159 macro->exp.tokens[0].flags &= ~PREV_WHITE; 3444 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3160 3445
3161 /* Commit or allocate the memory. */ 3446 if (num_extra_tokens)
3162 if (pfile->hash_table->alloc_subobject) 3447 {
3163 { 3448 /* Place second and subsequent ## or %:%: tokens in sequences of
3164 cpp_token *tokns = 3449 consecutive such tokens at the end of the list to preserve
3165 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token) 3450 information about where they appear, how they are spelt and
3166 * macro->count); 3451 whether they are preceded by whitespace without otherwise
3167 if (num_extra_tokens) 3452 interfering with macro expansion. Remember, this is
3168 { 3453 extremely rare, so efficiency is not a priority. */
3169 /* Place second and subsequent ## or %:%: tokens in 3454 cpp_token *temp = (cpp_token *)_cpp_reserve_room
3170 sequences of consecutive such tokens at the end of the 3455 (pfile, 0, num_extra_tokens * sizeof (cpp_token));
3171 list to preserve information about where they appear, how 3456 unsigned extra_ix = 0, norm_ix = 0;
3172 they are spelt and whether they are preceded by 3457 cpp_token *exp = macro->exp.tokens;
3173 whitespace without otherwise interfering with macro 3458 for (unsigned ix = 0; ix != macro->count; ix++)
3174 expansion. */ 3459 if (exp[ix].type == CPP_PASTE)
3175 cpp_token *normal_dest = tokns; 3460 temp[extra_ix++] = exp[ix];
3176 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens; 3461 else
3177 unsigned int i; 3462 exp[norm_ix++] = exp[ix];
3178 for (i = 0; i < macro->count; i++) 3463 memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token));
3179 { 3464
3180 if (macro->exp.tokens[i].type == CPP_PASTE) 3465 /* Record there are extra tokens. */
3181 *extra_dest++ = macro->exp.tokens[i]; 3466 macro->extra_tokens = 1;
3182 else 3467 }
3183 *normal_dest++ = macro->exp.tokens[i]; 3468
3184 } 3469 out:
3185 } 3470 pfile->state.va_args_ok = 0;
3186 else 3471 _cpp_unsave_parameters (pfile, nparms);
3187 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count); 3472
3188 macro->exp.tokens = tokns; 3473 return ok ? macro : NULL;
3189 } 3474 }
3190 else 3475
3191 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count]; 3476 cpp_macro *
3192 3477 _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
3193 return true; 3478 {
3194 } 3479 cpp_macro *macro = (cpp_macro *) placement;
3195 3480
3196 /* Parse a macro and save its expansion. Returns nonzero on success. */
3197 bool
3198 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3199 {
3200 cpp_macro *macro;
3201 unsigned int i;
3202 bool ok;
3203
3204 if (pfile->hash_table->alloc_subobject)
3205 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3206 (sizeof (cpp_macro));
3207 else
3208 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3209 macro->line = pfile->directive_line; 3481 macro->line = pfile->directive_line;
3210 macro->params = 0; 3482 macro->parm.params = 0;
3483 macro->lazy = 0;
3211 macro->paramc = 0; 3484 macro->paramc = 0;
3212 macro->variadic = 0; 3485 macro->variadic = 0;
3213 macro->used = !CPP_OPTION (pfile, warn_unused_macros); 3486 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3214 macro->count = 0; 3487 macro->count = 0;
3215 macro->fun_like = 0; 3488 macro->fun_like = 0;
3216 macro->extra_tokens = 0; 3489 macro->extra_tokens = 0;
3217 /* To suppress some diagnostics. */ 3490 /* To suppress some diagnostics. */
3218 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; 3491 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3219 3492
3493 macro->kind = kind;
3494
3495 return macro;
3496 }
3497
3498 /* Parse a macro and save its expansion. Returns nonzero on success. */
3499 bool
3500 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3501 {
3502 cpp_macro *macro;
3503
3220 if (CPP_OPTION (pfile, traditional)) 3504 if (CPP_OPTION (pfile, traditional))
3221 ok = _cpp_create_trad_definition (pfile, macro); 3505 macro = _cpp_create_trad_definition (pfile);
3222 else 3506 else
3223 { 3507 macro = create_iso_definition (pfile);
3224 ok = create_iso_definition (pfile, macro); 3508
3225 3509 if (!macro)
3226 /* We set the type for SEEN_EOL() in directives.c. 3510 return false;
3227 3511
3228 Longer term we should lex the whole line before coming here, 3512 if (cpp_macro_p (node))
3229 and just copy the expansion. */
3230
3231 /* Stop the lexer accepting __VA_ARGS__. */
3232 pfile->state.va_args_ok = 0;
3233 }
3234
3235 /* Clear the fast argument lookup indices. */
3236 for (i = macro->paramc; i-- > 0; )
3237 {
3238 struct macro_arg_saved_data *save =
3239 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3240 struct cpp_hashnode *node = save->canonical_node;
3241 node->flags &= ~ NODE_MACRO_ARG;
3242 node->value = save->value;
3243 }
3244
3245 if (!ok)
3246 return ok;
3247
3248 if (node->type == NT_MACRO)
3249 { 3513 {
3250 if (CPP_OPTION (pfile, warn_unused_macros)) 3514 if (CPP_OPTION (pfile, warn_unused_macros))
3251 _cpp_warn_if_unused_macro (pfile, node, NULL); 3515 _cpp_warn_if_unused_macro (pfile, node, NULL);
3252 3516
3253 if (warn_of_redefinition (pfile, node, macro)) 3517 if (warn_of_redefinition (pfile, node, macro))
3254 { 3518 {
3255 const int reason = ((node->flags & NODE_BUILTIN) 3519 const enum cpp_warning_reason reason
3256 && !(node->flags & NODE_WARN)) 3520 = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN))
3257 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; 3521 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3258 3522
3259 bool warned = 3523 bool warned =
3260 cpp_pedwarning_with_line (pfile, reason, 3524 cpp_pedwarning_with_line (pfile, reason,
3261 pfile->directive_line, 0, 3525 pfile->directive_line, 0,
3262 "\"%s\" redefined", NODE_NAME (node)); 3526 "\"%s\" redefined", NODE_NAME (node));
3263 3527
3264 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 3528 if (warned && cpp_user_macro_p (node))
3265 cpp_error_with_line (pfile, CPP_DL_NOTE, 3529 cpp_error_with_line (pfile, CPP_DL_NOTE,
3266 node->value.macro->line, 0, 3530 node->value.macro->line, 0,
3267 "this is the location of the previous definition"); 3531 "this is the location of the previous definition");
3268 } 3532 }
3269 } 3533 _cpp_free_definition (node);
3270 3534 }
3271 if (node->type != NT_VOID)
3272 _cpp_free_definition (node);
3273 3535
3274 /* Enter definition in hash table. */ 3536 /* Enter definition in hash table. */
3275 node->type = NT_MACRO; 3537 node->type = NT_USER_MACRO;
3276 node->value.macro = macro; 3538 node->value.macro = macro;
3277 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) 3539 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3278 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") 3540 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3279 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned 3541 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3280 in the C standard, as something that one must use in C++. 3542 in the C standard, as something that one must use in C++.
3286 3548
3287 /* If user defines one of the conditional macros, remove the 3549 /* If user defines one of the conditional macros, remove the
3288 conditional flag */ 3550 conditional flag */
3289 node->flags &= ~NODE_CONDITIONAL; 3551 node->flags &= ~NODE_CONDITIONAL;
3290 3552
3291 return ok; 3553 return true;
3554 }
3555
3556 extern void
3557 cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
3558 {
3559 cpp_macro *macro = node->value.macro;
3560
3561 gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < UCHAR_MAX);
3562
3563 macro->lazy = num + 1;
3564 }
3565
3566 /* Notify the use of NODE in a macro-aware context (i.e. expanding it,
3567 or testing its existance). Also applies any lazy definition. */
3568
3569 extern void
3570 _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node)
3571 {
3572 node->flags |= NODE_USED;
3573 switch (node->type)
3574 {
3575 case NT_USER_MACRO:
3576 {
3577 cpp_macro *macro = node->value.macro;
3578 if (macro->lazy)
3579 {
3580 pfile->cb.user_lazy_macro (pfile, macro, macro->lazy - 1);
3581 macro->lazy = 0;
3582 }
3583 }
3584 /* FALLTHROUGH. */
3585
3586 case NT_BUILTIN_MACRO:
3587 if (pfile->cb.used_define)
3588 pfile->cb.used_define (pfile, pfile->directive_line, node);
3589 break;
3590
3591 case NT_VOID:
3592 if (pfile->cb.used_undef)
3593 pfile->cb.used_undef (pfile, pfile->directive_line, node);
3594 break;
3595
3596 default:
3597 abort ();
3598 }
3292 } 3599 }
3293 3600
3294 /* Warn if a token in STRING matches one of a function-like MACRO's 3601 /* Warn if a token in STRING matches one of a function-like MACRO's
3295 parameters. */ 3602 parameters. */
3296 static void 3603 static void
3317 3624
3318 /* Loop over the function macro arguments to see if the 3625 /* Loop over the function macro arguments to see if the
3319 identifier inside the string matches one of them. */ 3626 identifier inside the string matches one of them. */
3320 for (i = 0; i < macro->paramc; i++) 3627 for (i = 0; i < macro->paramc; i++)
3321 { 3628 {
3322 const cpp_hashnode *node = macro->params[i]; 3629 const cpp_hashnode *node = macro->parm.params[i];
3323 3630
3324 if (NODE_LEN (node) == len 3631 if (NODE_LEN (node) == len
3325 && !memcmp (p, NODE_NAME (node), len)) 3632 && !memcmp (p, NODE_NAME (node), len))
3326 { 3633 {
3327 cpp_error (pfile, CPP_DL_WARNING, 3634 cpp_warning (pfile, CPP_W_TRADITIONAL,
3328 "macro argument \"%s\" would be stringified in traditional C", 3635 "macro argument \"%s\" would be stringified in traditional C",
3329 NODE_NAME (node)); 3636 NODE_NAME (node));
3330 break; 3637 break;
3331 } 3638 }
3332 } 3639 }
3333 } 3640 }
3334 }
3335
3336 /* Returns true of NODE is a function-like macro. */
3337 bool
3338 cpp_fun_like_macro_p (cpp_hashnode *node)
3339 {
3340 return (node->type == NT_MACRO
3341 && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
3342 && node->value.macro->fun_like);
3343 } 3641 }
3344 3642
3345 /* Returns the name, arguments and expansion of a macro, in a format 3643 /* Returns the name, arguments and expansion of a macro, in a format
3346 suitable to be read back in again, and therefore also for DWARF 2 3644 suitable to be read back in again, and therefore also for DWARF 2
3347 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". 3645 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3349 returned text is temporary, and automatically freed later. */ 3647 returned text is temporary, and automatically freed later. */
3350 const unsigned char * 3648 const unsigned char *
3351 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) 3649 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3352 { 3650 {
3353 unsigned int i, len; 3651 unsigned int i, len;
3354 const cpp_macro *macro;
3355 unsigned char *buffer; 3652 unsigned char *buffer;
3356 3653
3357 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) 3654 gcc_checking_assert (cpp_user_macro_p (node));
3358 { 3655
3359 if (node->type != NT_MACRO 3656 const cpp_macro *macro = node->value.macro;
3360 || !pfile->cb.user_builtin_macro 3657
3361 || !pfile->cb.user_builtin_macro (pfile, node))
3362 {
3363 cpp_error (pfile, CPP_DL_ICE,
3364 "invalid hash type %d in cpp_macro_definition",
3365 node->type);
3366 return 0;
3367 }
3368 }
3369
3370 macro = node->value.macro;
3371 /* Calculate length. */ 3658 /* Calculate length. */
3372 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */ 3659 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3373 if (macro->fun_like) 3660 if (macro->fun_like)
3374 { 3661 {
3375 len += 4; /* "()" plus possible final ".." of named 3662 len += 4; /* "()" plus possible final ".." of named
3376 varargs (we have + 1 below). */ 3663 varargs (we have + 1 below). */
3377 for (i = 0; i < macro->paramc; i++) 3664 for (i = 0; i < macro->paramc; i++)
3378 len += NODE_LEN (macro->params[i]) + 1; /* "," */ 3665 len += NODE_LEN (macro->parm.params[i]) + 1; /* "," */
3379 } 3666 }
3380 3667
3381 /* This should match below where we fill in the buffer. */ 3668 /* This should match below where we fill in the buffer. */
3382 if (CPP_OPTION (pfile, traditional)) 3669 if (CPP_OPTION (pfile, traditional))
3383 len += _cpp_replacement_text_len (macro); 3670 len += _cpp_replacement_text_len (macro);
3384 else 3671 else
3385 { 3672 {
3386 unsigned int count = macro_real_token_count (macro); 3673 unsigned int count = macro_real_token_count (macro);
3387 for (i = 0; i < count; i++) 3674 for (i = 0; i < count; i++)
3388 { 3675 {
3389 cpp_token *token = &macro->exp.tokens[i]; 3676 const cpp_token *token = &macro->exp.tokens[i];
3390 3677
3391 if (token->type == CPP_MACRO_ARG) 3678 if (token->type == CPP_MACRO_ARG)
3392 len += NODE_LEN (token->val.macro_arg.spelling); 3679 len += NODE_LEN (token->val.macro_arg.spelling);
3393 else 3680 else
3394 len += cpp_token_len (token); 3681 len += cpp_token_len (token);
3417 if (macro->fun_like) 3704 if (macro->fun_like)
3418 { 3705 {
3419 *buffer++ = '('; 3706 *buffer++ = '(';
3420 for (i = 0; i < macro->paramc; i++) 3707 for (i = 0; i < macro->paramc; i++)
3421 { 3708 {
3422 cpp_hashnode *param = macro->params[i]; 3709 cpp_hashnode *param = macro->parm.params[i];
3423 3710
3424 if (param != pfile->spec_nodes.n__VA_ARGS__) 3711 if (param != pfile->spec_nodes.n__VA_ARGS__)
3425 { 3712 {
3426 memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); 3713 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3427 buffer += NODE_LEN (param); 3714 buffer += NODE_LEN (param);
3448 /* Expansion tokens. */ 3735 /* Expansion tokens. */
3449 { 3736 {
3450 unsigned int count = macro_real_token_count (macro); 3737 unsigned int count = macro_real_token_count (macro);
3451 for (i = 0; i < count; i++) 3738 for (i = 0; i < count; i++)
3452 { 3739 {
3453 cpp_token *token = &macro->exp.tokens[i]; 3740 const cpp_token *token = &macro->exp.tokens[i];
3454 3741
3455 if (token->flags & PREV_WHITE) 3742 if (token->flags & PREV_WHITE)
3456 *buffer++ = ' '; 3743 *buffer++ = ' ';
3457 if (token->flags & STRINGIFY_ARG) 3744 if (token->flags & STRINGIFY_ARG)
3458 *buffer++ = '#'; 3745 *buffer++ = '#';
3478 } 3765 }
3479 3766
3480 *buffer = '\0'; 3767 *buffer = '\0';
3481 return pfile->macro_buffer; 3768 return pfile->macro_buffer;
3482 } 3769 }
3770
3771 /* Get the line at which the macro was defined. */
3772
3773 source_location
3774 cpp_macro_definition_location (cpp_hashnode *node)
3775 {
3776 return node->value.macro->line;
3777 }