Add macro name to debugfile messages.
[m4.git] / m4 / macro.c
blobffe55f718f6c34cb7e4ec05f2d940cf000a2b159
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2001, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* This file contains the functions that perform the basic argument
22 parsing and macro expansion. */
24 #include <config.h>
26 #include <stdarg.h>
28 #include "m4private.h"
30 #include "intprops.h"
32 static void collect_arguments (m4 *, const char *, m4_symbol *,
33 m4_obstack *, m4_obstack *);
34 static void expand_macro (m4 *, const char *, m4_symbol *);
35 static void expand_token (m4 *, m4_obstack *, m4__token_type,
36 m4_symbol_value *, int);
37 static bool expand_argument (m4 *, m4_obstack *, m4_symbol_value *,
38 const char *);
39 static void process_macro (m4 *, m4_symbol_value *, m4_obstack *, int,
40 m4_symbol_value **);
42 static void trace_prepre (m4 *, const char *, size_t,
43 m4_symbol_value *);
44 static void trace_pre (m4 *, const char *, size_t, int,
45 m4_symbol_value **);
46 static void trace_post (m4 *, const char *, size_t, int,
47 m4_symbol_value **, m4_input_block *, bool);
49 static void trace_format (m4 *, const char *, ...)
50 M4_GNUC_PRINTF (2, 3);
51 static void trace_header (m4 *, size_t);
52 static void trace_flush (m4 *);
55 /* Current recursion level in expand_macro (). */
56 static size_t expansion_level = 0;
58 /* The number of the current call of expand_macro (). */
59 static size_t macro_call_id = 0;
61 /* The shared stack of collected arguments for macro calls; as each
62 argument is collected, it is finished and its location stored in
63 argv_stack. This stack can be used simultaneously by multiple
64 macro calls, using obstack_regrow to handle partial objects
65 embedded in the stack. */
66 static struct obstack argc_stack;
68 /* The shared stack of pointers to collected arguments for macro
69 calls. This object is never finished; we exploit the fact that
70 obstack_blank is documented to take a negative size to reduce the
71 size again. */
72 static struct obstack argv_stack;
74 /* This function reads all input, and expands each token, one at a time. */
75 void
76 m4_macro_expand_input (m4 *context)
78 m4__token_type type;
79 m4_symbol_value token;
80 int line;
82 obstack_init (&argc_stack);
83 obstack_init (&argv_stack);
85 while ((type = m4__next_token (context, &token, &line, NULL))
86 != M4_TOKEN_EOF)
87 expand_token (context, (m4_obstack *) NULL, type, &token, line);
89 obstack_free (&argc_stack, NULL);
90 obstack_free (&argv_stack, NULL);
94 /* Expand one token, according to its type. Potential macro names
95 (M4_TOKEN_WORD) are looked up in the symbol table, to see if they have a
96 macro definition. If they have, they are expanded as macros, otherwise
97 the text are just copied to the output. */
98 static void
99 expand_token (m4 *context, m4_obstack *obs,
100 m4__token_type type, m4_symbol_value *token, int line)
102 m4_symbol *symbol;
103 const char *text = (m4_is_symbol_value_text (token)
104 ? m4_get_symbol_value_text (token) : NULL);
106 switch (type)
107 { /* TOKSW */
108 case M4_TOKEN_EOF:
109 case M4_TOKEN_MACDEF:
110 break;
112 case M4_TOKEN_OPEN:
113 case M4_TOKEN_COMMA:
114 case M4_TOKEN_CLOSE:
115 case M4_TOKEN_SIMPLE:
116 case M4_TOKEN_STRING:
117 case M4_TOKEN_SPACE:
118 m4_shipout_text (context, obs, text, strlen (text), line);
119 break;
121 case M4_TOKEN_WORD:
123 const char *textp = text;
125 if (m4_has_syntax (M4SYNTAX, to_uchar (*textp), M4_SYNTAX_ESCAPE))
126 ++textp;
128 symbol = m4_symbol_lookup (M4SYMTAB, textp);
129 assert (! symbol || ! m4_is_symbol_void (symbol));
130 if (symbol == NULL
131 || (symbol->value->type == M4_SYMBOL_FUNC
132 && BIT_TEST (SYMBOL_FLAGS (symbol), VALUE_BLIND_ARGS_BIT)
133 && ! m4__next_token_is_open (context)))
135 m4_shipout_text (context, obs, text, strlen (text), line);
137 else
138 expand_macro (context, textp, symbol);
140 break;
142 default:
143 assert (!"INTERNAL ERROR: bad token type in expand_token ()");
144 abort ();
149 /* This function parses one argument to a macro call. It expects the
150 first left parenthesis or the separating comma to have been read by
151 the caller. It skips leading whitespace, then reads and expands
152 tokens, until it finds a comma or a right parenthesis at the same
153 level of parentheses. It returns a flag indicating whether the
154 argument read is the last for the active macro call. The arguments
155 are built on the obstack OBS, indirectly through expand_token ().
156 Report errors on behalf of CALLER. */
157 static bool
158 expand_argument (m4 *context, m4_obstack *obs, m4_symbol_value *argp,
159 const char *caller)
161 m4__token_type type;
162 m4_symbol_value token;
163 int paren_level = 0;
164 const char *file = m4_get_current_file (context);
165 int line = m4_get_current_line (context);
167 argp->type = M4_SYMBOL_VOID;
169 /* Skip leading white space. */
172 type = m4__next_token (context, &token, NULL, caller);
174 while (type == M4_TOKEN_SPACE);
176 while (1)
178 switch (type)
179 { /* TOKSW */
180 case M4_TOKEN_COMMA:
181 case M4_TOKEN_CLOSE:
182 if (paren_level == 0)
184 /* FIXME - For now, we match the behavior of the branch,
185 except we don't issue warnings. But in the future,
186 we want to allow concatenation of builtins and
187 text. */
188 if (argp->type == M4_SYMBOL_FUNC
189 && obstack_object_size (obs) == 0)
190 return type == M4_TOKEN_COMMA;
191 obstack_1grow (obs, '\0');
192 VALUE_MODULE (argp) = NULL;
193 m4_set_symbol_value_text (argp, obstack_finish (obs));
194 return type == M4_TOKEN_COMMA;
196 /* fallthru */
197 case M4_TOKEN_OPEN:
198 case M4_TOKEN_SIMPLE:
199 if (type == M4_TOKEN_OPEN)
200 paren_level++;
201 else if (type == M4_TOKEN_CLOSE)
202 paren_level--;
203 expand_token (context, obs, type, &token, line);
204 break;
206 case M4_TOKEN_EOF:
207 m4_error_at_line (context, EXIT_FAILURE, 0, file, line, caller,
208 _("end of file in argument list"));
209 break;
211 case M4_TOKEN_WORD:
212 case M4_TOKEN_SPACE:
213 case M4_TOKEN_STRING:
214 expand_token (context, obs, type, &token, line);
215 break;
217 case M4_TOKEN_MACDEF:
218 if (argp->type == M4_SYMBOL_VOID && obstack_object_size (obs) == 0)
219 m4_symbol_value_copy (argp, &token);
220 else
221 argp->type = M4_SYMBOL_TEXT;
222 break;
224 default:
225 assert (!"INTERNAL ERROR: bad token type in expand_argument ()");
226 abort ();
229 type = m4__next_token (context, &token, NULL, caller);
234 /* The macro expansion is handled by expand_macro (). It parses the
235 arguments, using collect_arguments (), and builds a table of pointers to
236 the arguments. The arguments themselves are stored on a local obstack.
237 Expand_macro () uses call_macro () to do the call of the macro.
239 Expand_macro () is potentially recursive, since it calls expand_argument
240 (), which might call expand_token (), which might call expand_macro ().
242 NAME points to storage on the token stack, so it is only valid
243 until a call to collect_arguments parses more tokens. SYMBOL is
244 the result of the symbol table lookup on NAME. */
245 static void
246 expand_macro (m4 *context, const char *name, m4_symbol *symbol)
248 char *argc_base = NULL; /* Base of argc_stack on entry. */
249 unsigned int argc_size; /* Size of argc_stack on entry. */
250 unsigned int argv_size; /* Size of argv_stack on entry. */
251 m4_symbol_value **argv;
252 int argc;
253 m4_obstack *expansion;
254 m4_input_block *expanded;
255 bool traced;
256 bool trace_expansion = false;
257 size_t my_call_id;
258 m4_symbol_value *value;
260 /* Report errors at the location where the open parenthesis (if any)
261 was found, but after expansion, restore global state back to the
262 location of the close parenthesis. This is safe since we
263 guarantee that macro expansion does not alter the state of
264 current_file/current_line (dnl, include, and sinclude are special
265 cased in the input engine to ensure this fact). */
266 const char *loc_open_file = m4_get_current_file (context);
267 int loc_open_line = m4_get_current_line (context);
268 const char *loc_close_file;
269 int loc_close_line;
271 /* Grab the current value of this macro, because it may change while
272 collecting arguments. Likewise, grab any state needed during
273 tracing. */
274 value = m4_get_symbol_value (symbol);
275 traced = (m4_is_debug_bit (context, M4_DEBUG_TRACE_ALL)
276 || m4_get_symbol_traced (symbol));
277 if (traced)
278 trace_expansion = m4_is_debug_bit (context, M4_DEBUG_TRACE_EXPANSION);
280 /* Prepare for macro expansion. */
281 VALUE_PENDING (value)++;
282 expansion_level++;
283 if (m4_get_nesting_limit_opt (context) > 0
284 && expansion_level > m4_get_nesting_limit_opt (context))
285 m4_error (context, EXIT_FAILURE, 0, NULL, _("\
286 recursion limit of %zu exceeded, use -L<N> to change it"),
287 m4_get_nesting_limit_opt (context));
289 macro_call_id++;
290 my_call_id = macro_call_id;
292 argc_size = obstack_object_size (&argc_stack);
293 argv_size = obstack_object_size (&argv_stack);
294 if (0 < argc_size)
295 argc_base = obstack_finish (&argc_stack);
297 if (traced && m4_is_debug_bit (context, M4_DEBUG_TRACE_CALL))
298 trace_prepre (context, name, my_call_id, value);
300 collect_arguments (context, name, symbol, &argv_stack, &argc_stack);
302 argc = ((obstack_object_size (&argv_stack) - argv_size)
303 / sizeof (m4_symbol_value *));
304 argv = (m4_symbol_value **) ((char *) obstack_base (&argv_stack)
305 + argv_size);
306 /* Calling collect_arguments invalidated name, but we copied it as
307 argv[0]. */
308 name = m4_get_symbol_value_text (argv[0]);
310 loc_close_file = m4_get_current_file (context);
311 loc_close_line = m4_get_current_line (context);
312 m4_set_current_file (context, loc_open_file);
313 m4_set_current_line (context, loc_open_line);
315 if (traced)
316 trace_pre (context, name, my_call_id, argc, argv);
318 expansion = m4_push_string_init (context);
319 m4_macro_call (context, value, expansion, argc, argv);
320 expanded = m4_push_string_finish ();
322 if (traced)
323 trace_post (context, name, my_call_id, argc, argv, expanded,
324 trace_expansion);
326 m4_set_current_file (context, loc_close_file);
327 m4_set_current_line (context, loc_close_line);
329 --expansion_level;
330 --VALUE_PENDING (value);
331 if (BIT_TEST (VALUE_FLAGS (value), VALUE_DELETED_BIT))
332 m4_symbol_value_delete (value);
334 if (0 < argc_size)
335 obstack_regrow (&argc_stack, argc_base, argc_size);
336 else
337 obstack_free (&argc_stack, argv[0]);
338 obstack_blank (&argv_stack, -argc * sizeof (m4_symbol_value *));
341 /* Collect all the arguments to a call of the macro SYMBOL (called NAME).
342 The arguments are stored on the obstack ARGUMENTS and a table of pointers
343 to the arguments on the obstack ARGPTR. */
344 static void
345 collect_arguments (m4 *context, const char *name, m4_symbol *symbol,
346 m4_obstack *argptr, m4_obstack *arguments)
348 m4_symbol_value token;
349 m4_symbol_value *tokenp;
350 bool more_args;
351 bool groks_macro_args;
353 groks_macro_args = BIT_TEST (SYMBOL_FLAGS (symbol), VALUE_MACRO_ARGS_BIT);
355 tokenp = (m4_symbol_value *) obstack_alloc (arguments, sizeof *tokenp);
356 m4_set_symbol_value_text (tokenp, (char *) obstack_copy0 (arguments, name,
357 strlen (name)));
358 name = m4_get_symbol_value_text (tokenp);
359 obstack_ptr_grow (argptr, tokenp);
361 if (m4__next_token_is_open (context))
363 m4__next_token (context, &token, NULL, name); /* gobble parenthesis */
366 more_args = expand_argument (context, arguments, &token, name);
368 if (!groks_macro_args && m4_is_symbol_value_func (&token))
370 VALUE_MODULE (&token) = NULL;
371 m4_set_symbol_value_text (&token, "");
373 tokenp = (m4_symbol_value *) obstack_copy (arguments, &token,
374 sizeof token);
375 obstack_ptr_grow (argptr, tokenp);
377 while (more_args);
382 /* The actual call of a macro is handled by m4_macro_call ().
383 m4_macro_call () is passed a SYMBOL, whose type is used to
384 call either a builtin function, or the user macro expansion
385 function process_macro (). There are ARGC arguments to
386 the call, stored in the ARGV table. The expansion is left on
387 the obstack EXPANSION. Macro tracing is also handled here. */
388 void
389 m4_macro_call (m4 *context, m4_symbol_value *value, m4_obstack *expansion,
390 int argc, m4_symbol_value **argv)
392 if (m4_bad_argc (context, argc, argv,
393 VALUE_MIN_ARGS (value), VALUE_MAX_ARGS (value),
394 BIT_TEST (VALUE_FLAGS (value),
395 VALUE_SIDE_EFFECT_ARGS_BIT)))
396 return;
397 if (m4_is_symbol_value_text (value))
399 process_macro (context, value, expansion, argc, argv);
401 else if (m4_is_symbol_value_func (value))
403 (*m4_get_symbol_value_func (value)) (context, expansion, argc, argv);
405 else if (m4_is_symbol_value_placeholder (value))
407 m4_warn (context, 0, M4ARG (0),
408 _("builtin `%s' requested by frozen file not found"),
409 m4_get_symbol_value_placeholder (value));
411 else
413 assert (!"INTERNAL ERROR: bad symbol type in call_macro ()");
414 abort ();
418 /* This function handles all expansion of user defined and predefined
419 macros. It is called with an obstack OBS, where the macros expansion
420 will be placed, as an unfinished object. SYMBOL points to the macro
421 definition, giving the expansion text. ARGC and ARGV are the arguments,
422 as usual. */
423 static void
424 process_macro (m4 *context, m4_symbol_value *value, m4_obstack *obs,
425 int argc, m4_symbol_value **argv)
427 const char *text;
428 int i;
430 for (text = m4_get_symbol_value_text (value); *text != '\0';)
432 char ch;
434 if (!m4_has_syntax (M4SYNTAX, to_uchar (*text), M4_SYNTAX_DOLLAR))
436 obstack_1grow (obs, *text);
437 text++;
438 continue;
440 ch = *text++;
441 switch (*text)
443 case '0': case '1': case '2': case '3': case '4':
444 case '5': case '6': case '7': case '8': case '9':
445 /* FIXME - multidigit arguments should convert over to ${10}
446 syntax instead of $10; see
447 http://lists.gnu.org/archive/html/m4-discuss/2006-08/msg00028.html
448 for more discussion. */
449 if (m4_get_posixly_correct_opt (context) || !isdigit(text[1]))
451 i = *text++ - '0';
453 else
455 char *endp;
456 i = (int) strtol (text, &endp, 10);
457 text = endp;
459 if (i < argc)
460 m4_shipout_string (context, obs, M4ARG (i), 0, false);
461 break;
463 case '#': /* number of arguments */
464 m4_shipout_int (obs, argc - 1);
465 text++;
466 break;
468 case '*': /* all arguments */
469 case '@': /* ... same, but quoted */
470 m4_dump_args (context, obs, argc, argv, ",", *text == '@');
471 text++;
472 break;
474 default:
475 if (m4_get_posixly_correct_opt (context)
476 || !VALUE_ARG_SIGNATURE (value))
478 obstack_1grow (obs, ch);
480 else
482 size_t len = 0;
483 const char *endp;
484 const char *key;
486 for (endp = ++text;
487 *endp && m4_has_syntax (M4SYNTAX, to_uchar (*endp),
488 (M4_SYNTAX_OTHER | M4_SYNTAX_ALPHA
489 | M4_SYNTAX_NUM));
490 ++endp)
492 ++len;
494 key = xstrndup (text, len);
496 if (*endp)
498 struct m4_symbol_arg **arg
499 = (struct m4_symbol_arg **)
500 m4_hash_lookup (VALUE_ARG_SIGNATURE (value), key);
502 if (arg)
504 i = SYMBOL_ARG_INDEX (*arg);
506 if (i < argc)
507 m4_shipout_string (context, obs, M4ARG (i), 0, false);
508 else
510 assert (!"INTERNAL ERROR: out of range reference");
511 abort ();
515 else
517 m4_error (context, 0, 0, M4ARG (0),
518 _("unterminated parameter reference: %s"),
519 key);
522 text = *endp ? 1 + endp : endp;
524 free ((char *) key);
525 break;
527 break;
534 /* The rest of this file contains the functions for macro tracing output.
535 All tracing output for a macro call is collected on an obstack TRACE,
536 and printed whenever the line is complete. This prevents tracing
537 output from interfering with other debug messages generated by the
538 various builtins. */
540 /* Tracing output is formatted here, by a simplified printf-to-obstack
541 function trace_format (). Understands only %s, %d, %zu (size_t
542 value). */
543 static void
544 trace_format (m4 *context, const char *fmt, ...)
546 va_list args;
547 char ch;
548 const char *s;
550 va_start (args, fmt);
552 while (true)
554 while ((ch = *fmt++) != '\0' && ch != '%')
555 obstack_1grow (&context->trace_messages, ch);
557 if (ch == '\0')
558 break;
560 switch (*fmt++)
562 case 's':
563 s = va_arg (args, const char *);
564 break;
566 case 'd':
568 int d = va_arg (args, int);
569 char nbuf[INT_BUFSIZE_BOUND (int)];
571 sprintf (nbuf, "%d", d);
572 s = nbuf;
574 break;
576 case 'z':
577 ch = *fmt++;
578 assert (ch == 'u');
580 size_t z = va_arg (args, size_t);
581 char nbuf[INT_BUFSIZE_BOUND (size_t)];
583 sprintf (nbuf, "%zu", z);
584 s = nbuf;
586 break;
588 default:
589 abort ();
590 break;
593 obstack_grow (&context->trace_messages, s, strlen (s));
596 va_end (args);
599 /* Format the standard header attached to all tracing output lines. */
600 static void
601 trace_header (m4 *context, size_t id)
603 trace_format (context, "m4trace:");
604 if (m4_get_current_line (context))
606 if (m4_is_debug_bit (context, M4_DEBUG_TRACE_FILE))
607 trace_format (context, "%s:", m4_get_current_file (context));
608 if (m4_is_debug_bit (context, M4_DEBUG_TRACE_LINE))
609 trace_format (context, "%d:", m4_get_current_line (context));
611 trace_format (context, " -%zu- ", expansion_level);
612 if (m4_is_debug_bit (context, M4_DEBUG_TRACE_CALLID))
613 trace_format (context, "id %zu: ", id);
616 /* Print current tracing line, and clear the obstack. */
617 static void
618 trace_flush (m4 *context)
620 char *line;
622 obstack_1grow (&context->trace_messages, '\n');
623 obstack_1grow (&context->trace_messages, '\0');
624 line = obstack_finish (&context->trace_messages);
625 if (m4_get_debug_file (context))
626 fputs (line, m4_get_debug_file (context));
627 obstack_free (&context->trace_messages, line);
630 /* Do pre-argument-collction tracing for macro NAME. Used from
631 expand_macro (). */
632 static void
633 trace_prepre (m4 *context, const char *name, size_t id, m4_symbol_value *value)
635 bool quote = m4_is_debug_bit (context, M4_DEBUG_TRACE_QUOTE);
636 const char *lquote = m4_get_syntax_lquote (M4SYNTAX);
637 const char *rquote = m4_get_syntax_rquote (M4SYNTAX);
638 size_t arg_length = m4_get_max_debug_arg_length_opt (context);
639 bool module = m4_is_debug_bit (context, M4_DEBUG_TRACE_MODULE);
641 trace_header (context, id);
642 trace_format (context, "%s ... = ", name);
643 m4_symbol_value_print (value, &context->trace_messages,
644 quote, lquote, rquote, arg_length, module);
645 trace_flush (context);
648 /* Format the parts of a trace line, that can be made before the macro is
649 actually expanded. Used from expand_macro (). */
650 static void
651 trace_pre (m4 *context, const char *name, size_t id,
652 int argc, m4_symbol_value **argv)
654 int i;
656 trace_header (context, id);
657 trace_format (context, "%s", name);
659 if ((argc > 1) && m4_is_debug_bit (context, M4_DEBUG_TRACE_ARGS))
661 bool quote = m4_is_debug_bit (context, M4_DEBUG_TRACE_QUOTE);
662 const char *lquote = m4_get_syntax_lquote (M4SYNTAX);
663 const char *rquote = m4_get_syntax_rquote (M4SYNTAX);
664 size_t arg_length = m4_get_max_debug_arg_length_opt (context);
665 bool module = m4_is_debug_bit (context, M4_DEBUG_TRACE_MODULE);
667 trace_format (context, "(");
668 for (i = 1; i < argc; i++)
670 if (i != 1)
671 trace_format (context, ", ");
673 m4_symbol_value_print (argv[i], &context->trace_messages,
674 quote, lquote, rquote, arg_length, module);
676 trace_format (context, ")");
680 /* Format the final part of a trace line and print it all. Used from
681 expand_macro (). */
682 static void
683 trace_post (m4 *context, const char *name, size_t id,
684 int argc, m4_symbol_value **argv, m4_input_block *expanded,
685 bool trace_expansion)
687 if (trace_expansion)
689 trace_format (context, " -> ");
690 m4_input_print (context, &context->trace_messages, expanded);
693 trace_flush (context);