static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
struct search_path *));
static void merge_include_chains PARAMS ((cpp_reader *));
-static void do_includes PARAMS ((cpp_reader *,
- struct pending_option *,
- int));
+static bool push_include PARAMS ((cpp_reader *,
+ struct pending_option *));
+static void free_chain PARAMS ((struct pending_option *));
static void set_lang PARAMS ((cpp_reader *, enum c_lang));
static void init_dependency_output PARAMS ((cpp_reader *));
static void init_standard_includes PARAMS ((cpp_reader *));
}
}
-/* Handles -imacro and -include from the command line. */
-static void
-do_includes (pfile, p, scan)
+/* Pushes a -imacro and -include file given on the command line onto
+ the buffer stack. Returns non-zero if successful. */
+static bool
+push_include (pfile, p)
cpp_reader *pfile;
struct pending_option *p;
- int scan;
{
- while (p)
- {
- struct pending_option *q;
+ cpp_token header;
- /* Don't handle if -fpreprocessed. Later: maybe update this to
- use the #include "" search path if cpp_read_file fails. */
- if (CPP_OPTION (pfile, preprocessed))
- cpp_error (pfile, "-include and -imacros cannot be used with -fpreprocessed");
- else
- {
- cpp_token header;
- header.type = CPP_STRING;
- header.val.str.text = (const unsigned char *) p->arg;
- header.val.str.len = strlen (p->arg);
- if (_cpp_execute_include (pfile, &header, IT_CMDLINE) && scan)
- {
- pfile->buffer->return_at_eof = true;
- cpp_scan_nooutput (pfile);
- }
- }
- q = p->next;
- free (p);
- p = q;
+ /* Later: maybe update this to use the #include "" search path
+ if cpp_read_file fails. */
+ header.type = CPP_STRING;
+ header.val.str.text = (const unsigned char *) p->arg;
+ header.val.str.len = strlen (p->arg);
+ /* Make the command line directive take up a line. */
+ pfile->lexer_pos.line = pfile->lexer_pos.output_line = ++pfile->line;
+
+ return _cpp_execute_include (pfile, &header, IT_CMDLINE);
+}
+
+/* Frees a pending_option chain. */
+static void
+free_chain (head)
+ struct pending_option *head;
+{
+ struct pending_option *next;
+
+ while (head)
+ {
+ next = head->next;
+ free (head);
+ head = next;
}
}
cpp_reader *pfile;
const char *fname;
{
- struct pending_option *p, *q;
-
/* Set up the include search path now. */
if (! CPP_OPTION (pfile, no_standard_includes))
init_standard_includes (pfile);
if (!_cpp_read_file (pfile, fname))
return 0;
- /* FIXME: we want to set up linemaps with _("<builtin>") and
- _("<command line>") somewhere round here. Harder than it looks. */
-
- /* If already preprocessed, don't install __LINE__, etc., and ignore
- command line definitions and assertions. Handle -U's, -D's and
- -A's in the order they were seen. */
+ /* Install builtins and process command line macros etc. in the order
+ they appeared, but only if not already preprocessed. */
if (! CPP_OPTION (pfile, preprocessed))
- init_builtins (pfile);
-
- p = CPP_OPTION (pfile, pending)->directive_head;
- while (p)
{
- if (! CPP_OPTION (pfile, preprocessed))
+ struct pending_option *p;
+
+ _cpp_do_file_change (pfile, LC_RENAME, _("<builtin>"), 1, 0);
+ init_builtins (pfile);
+ _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
+ for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
(*p->handler) (pfile, p->arg);
- q = p->next;
- free (p);
- p = q;
+
+ /* Scan -imacros files after command line defines, but before
+ files given with -include. */
+ for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
+ {
+ if (push_include (pfile, p))
+ {
+ pfile->buffer->return_at_eof = true;
+ cpp_scan_nooutput (pfile);
+ }
+ }
}
- /* Hopefully a short-term kludge. We stacked the main file at line
- zero. The intervening macro definitions have messed up line
- numbering, so we need to restore it. */
- pfile->lexer_pos.output_line = pfile->line = 0;
+ free_chain (CPP_OPTION (pfile, pending)->directive_head);
+ free_chain (CPP_OPTION (pfile, pending)->imacros_head);
+ _cpp_push_next_buffer (pfile);
- /* The -imacros files can be scanned now, but the -include files
- have to be pushed onto the buffer stack and processed later,
- otherwise cppmain.c won't see the tokens. include_head was built
- up as a stack, and popping this stack onto the buffer stack means
- we preserve the order of the command line. */
- do_includes (pfile, CPP_OPTION (pfile, pending)->imacros_head, 1);
- do_includes (pfile, CPP_OPTION (pfile, pending)->include_head, 0);
+ return 1;
+}
- free (CPP_OPTION (pfile, pending));
- CPP_OPTION (pfile, pending) = NULL;
+/* Called to push the next buffer on the stack given by -include. If
+ there are none, free the pending structure and restore the line map
+ for the main file. */
+bool
+_cpp_push_next_buffer (pfile)
+ cpp_reader *pfile;
+{
+ bool pushed = false;
- return 1;
+ if (CPP_OPTION (pfile, pending))
+ {
+ while (!pushed)
+ {
+ struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
+
+ if (p == NULL)
+ break;
+ if (! CPP_OPTION (pfile, preprocessed))
+ pushed = push_include (pfile, p);
+ CPP_OPTION (pfile, pending)->include_head = p->next;
+ free (p);
+ }
+
+ if (!pushed)
+ {
+ free (CPP_OPTION (pfile, pending));
+ CPP_OPTION (pfile, pending) = NULL;
+
+ /* Restore the line map for the main file. */
+ if (! CPP_OPTION (pfile, preprocessed))
+ _cpp_do_file_change (pfile, LC_RENAME,
+ pfile->line_maps.maps[0].to_file, 1, 0);
+ }
+ }
+
+ return pushed;
}
/* Use mkdeps.c to output dependency information. */
append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
break;
case OPT_include:
- {
- struct pending_option *o = (struct pending_option *)
- xmalloc (sizeof (struct pending_option));
- o->arg = arg;
-
- /* This list has to be built in reverse order so that
- when cpp_start_read pushes all the -include files onto
- the buffer stack, they will be scanned in forward order. */
- o->next = pend->include_head;
- pend->include_head = o;
- }
- break;
case OPT_imacros:
{
struct pending_option *o = (struct pending_option *)
o->arg = arg;
o->next = NULL;
- APPEND (pend, imacros, o);
+ if (opt_code == OPT_include)
+ APPEND (pend, include, o);
+ else
+ APPEND (pend, imacros, o);
}
break;
case OPT_iwithprefix:
switch (c)
{
case EOF:
+ /* Non-empty files should end in a newline. Don't warn for
+ command line and _Pragma buffers. */
+ if (pfile->lexer_pos.col != 0)
+ {
+ /* Account for the missing \n, prevent multiple warnings. */
+ pfile->line++;
+ pfile->lexer_pos.col = 0;
+ if (!buffer->from_stage3)
+ cpp_pedwarn (pfile, "no newline at end of file");
+ }
+
/* To prevent bogus diagnostics, only pop the buffer when
in-progress directives and arguments have been taken care of.
Decrement the line to terminate an in-progress directive. */
pfile->lexer_pos.output_line = pfile->line--;
else if (! pfile->state.parsing_args)
{
- /* Non-empty files should end in a newline. Don't warn for
- command line and _Pragma buffers. */
- if (pfile->lexer_pos.col != 0)
- {
- /* Account for the missing \n, prevent multiple warnings. */
- pfile->line++;
- pfile->lexer_pos.col = 0;
- if (!buffer->from_stage3)
- cpp_pedwarn (pfile, "no newline at end of file");
- }
-
- /* Don't pop the last file. */
+ /* Don't pop the last buffer. */
if (buffer->prev)
{
unsigned char stop = buffer->return_at_eof;
_cpp_pop_buffer (pfile);
+ /* Push the next -included file, if any. */
+ if (!pfile->buffer->prev)
+ _cpp_push_next_buffer (pfile);
if (!stop)
goto next_token;
}