This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
-fpreprocessed, directives, #line and other fun
Zack Weinberg wrote:-
> Just about everything should be shut off by -fpreprocessed: -include,
> -imacros, -D, -I, ...
Done with the patch below. We silently ignore -I, -D, -U and -A with
-fpreprocessed, since gcc.c will still pass these. In fact, we don't
set up an include file path list at all, and we done define builtins
like __LINE__.
We emit a hard error for -include and -imacros with -fpreprocessed.
> I'd support this, and also removing the ability to specify trailing
> flags on #line. As you say, it can probably cause segfaults and the
> like. It used to be useful to write #line "foobar" 3, but now we have
> #pragma GCC system_header for that.
I've done this too. Now # NUMBER is -fpreprocessed only, and we allow
flags after the file name for -freprocessed only.
Jason Merrill wrote:-
> There are some oddball semi-preprocessed testcases in the g++
> testsuite, such as g++.jason/2371.C, which would break with this
> change. But that's probably OK.
OK, I'll fix these.
I'm bootstrapping this patch, and will commit it if it succeeds a make
check.
Questions:-
o Should we allow the #line form in -fpreprocessed?
o More generally, why should we allow *any* directive other than
#number with -fpreprocessed? Things like -dD and -dI are just for
debugging, right? It seems inconsistent to allow #define and expand
macros therefrom, if we ignore -D.
Jason, could you clarify what you want to see for -include? For
example, consider
gcc -c -include ~/test.h ~/test.c
Currently we get
# 1 "/home/neil/test.c"
# 1 "/home/neil/test.h" 1
[test.h stuff]
# 2 "/home/neil/test.c" 2 [This is a bug - the line number is wrong]
[test.c stuff]
Do we want
# 1 "/home/neil/test.h" 1
[test.h stuff]
# 1 "/home/neil/test.c"
[test.c stuff]
instead? What do you want "main_input_filename" and "input_filename"
to be at each stage in this example?
More questions to come later...
Neil.
* cppinit.c (initialize): Give prototype.
(initialize_builtins): C++ operators only if -fpreprocessed.
(do_includes): Emit an error for each -include or -imacros
with -fpreprocessed.
(cpp_start_read): If -fpreprocessed, don't set up the include
chains, ignore -A, -U, -D.
* cpplib.c (_cpp_handle_directve): # NUMBER with -fpreprocessed
only. Fix skipping slip.
(do_line): Accept #line flags only with -fpreprocessed.
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.122
diff -u -p -r1.122 cppinit.c
--- cppinit.c 2000/11/28 21:34:29 1.122
+++ cppinit.c 2000/11/30 18:50:24
@@ -94,6 +94,7 @@ struct cpp_pending
static void print_help PARAMS ((void));
static void path_include PARAMS ((cpp_reader *,
char *, int));
+static void initialize PARAMS ((void));
static void initialize_builtins PARAMS ((cpp_reader *));
static void append_include_chain PARAMS ((cpp_reader *,
char *, int, int));
@@ -719,6 +720,10 @@ initialize_builtins (pfile)
if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
continue;
+ /* No builtins with -fpreprocessed. */
+ if (CPP_OPTION (pfile, preprocessed) && ! (b->flags & OPERATOR))
+ continue;
+
if (b->flags & (OPERATOR | BUILTIN))
{
cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
@@ -928,10 +933,16 @@ do_includes (pfile, p, scan)
{
struct pending_option *q;
- /* Later: maybe update this to use the #include "" search path
- if cpp_read_file fails. */
- if (cpp_read_file (pfile, p->arg) && scan)
- cpp_scan_buffer_nooutput (pfile, 0);
+ if (! CPP_OPTION (pfile, preprocessed))
+ {
+ /* Later: maybe update this to use the #include "" search
+ path if cpp_read_file fails. */
+ if (cpp_read_file (pfile, p->arg) && scan)
+ cpp_scan_buffer_nooutput (pfile, 0);
+ }
+ else
+ cpp_error (pfile, "%s %s cannot be used with -fpreprocessed",
+ scan ? "-imacros": "-include", p->arg);
q = p->next;
free (p);
p = q;
@@ -969,11 +980,14 @@ cpp_start_read (pfile, fname)
if (CPP_OPTION (pfile, user_label_prefix) == NULL)
CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
- /* Set up the include search path now. */
- if (! CPP_OPTION (pfile, no_standard_includes))
- initialize_standard_includes (pfile);
+ if (! CPP_OPTION (pfile, preprocessed))
+ {
+ /* Set up the include search path now. */
+ if (! CPP_OPTION (pfile, no_standard_includes))
+ initialize_standard_includes (pfile);
- merge_include_chains (pfile);
+ merge_include_chains (pfile);
+ }
/* With -v, print the list of dirs to search. */
if (CPP_OPTION (pfile, verbose))
@@ -1005,19 +1019,19 @@ cpp_start_read (pfile, fname)
return 0;
initialize_dependency_output (pfile);
-
- /* Install __LINE__, etc. */
initialize_builtins (pfile);
- /* Do -U's, -D's and -A's in the order they were seen. */
+ /* If not preprocessed, do -U's, -D's and -A's in command-line order. */
p = CPP_OPTION (pfile, pending)->directive_head;
while (p)
{
- (*p->handler) (pfile, p->arg);
+ if (! CPP_OPTION (pfile, preprocessed))
+ (*p->handler) (pfile, p->arg);
q = p->next;
free (p);
p = q;
}
+
pfile->done_initializing = 1;
/* The -imacros files can be scanned now, but the -include files
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
retrieving revision 1.223
diff -u -p -r1.223 cpplib.c
--- cpplib.c 2000/11/27 08:00:03 1.223
+++ cpplib.c 2000/11/30 18:50:28
@@ -288,18 +288,14 @@ _cpp_handle_directive (pfile, indented)
}
else if (dname.type == CPP_NUMBER)
{
- /* # followed by a number is equivalent to #line. Do not
- recognize this form in assembly language source files or
- skipped conditional groups. Complain about this form if
- we're being pedantic, but not if this is regurgitated input
- (preprocessed or fed back in by the C++ frontend). */
- if (! buffer->was_skipping && CPP_OPTION (pfile, lang) != CLK_ASM)
+ /* # followed by a number is equivalent to #line in preprocessed
+ source. Do not recognize this form in assembly language
+ source files. */
+ if (CPP_OPTION (pfile, preprocessed)
+ && CPP_OPTION (pfile, lang) != CLK_ASM)
{
dir = &dtable[T_LINE];
_cpp_push_token (pfile, &dname, &pfile->directive_pos);
- if (CPP_PEDANTIC (pfile) && buffer->inc
- && ! CPP_OPTION (pfile, preprocessed))
- cpp_pedwarn (pfile, "# followed by integer");
}
}
@@ -348,7 +344,7 @@ _cpp_handle_directive (pfile, indented)
}
}
}
- else if (dname.type != CPP_EOF && ! pfile->skipping)
+ else if (dname.type != CPP_EOF && ! buffer->was_skipping)
{
/* An unknown directive. Don't complain about it in assembly
source: we don't know where the comments are, and # may
@@ -763,32 +759,33 @@ do_line (pfile)
buffer->nominal_fname = _cpp_fake_include (pfile, fname);
}
- if (read_line_number (pfile, &action_number) != 0)
+ /* Only accept flags if already preprocessed. */
+ if (CPP_OPTION (pfile, preprocessed))
{
- if (! CPP_OPTION (pfile, preprocessed) && CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "extra tokens at end of #line directive");
-
- if (action_number == 1)
- {
- reason = FC_ENTER;
- cpp_make_system_header (pfile, buffer, 0);
- read_line_number (pfile, &action_number);
- }
- else if (action_number == 2)
- {
- reason = FC_LEAVE;
- cpp_make_system_header (pfile, buffer, 0);
- read_line_number (pfile, &action_number);
- }
- if (action_number == 3)
- {
- cpp_make_system_header (pfile, buffer, 1);
- read_line_number (pfile, &action_number);
- }
- if (action_number == 4)
+ if (read_line_number (pfile, &action_number) != 0)
{
- cpp_make_system_header (pfile, buffer, 2);
- read_line_number (pfile, &action_number);
+ if (action_number == 1)
+ {
+ reason = FC_ENTER;
+ cpp_make_system_header (pfile, buffer, 0);
+ read_line_number (pfile, &action_number);
+ }
+ else if (action_number == 2)
+ {
+ reason = FC_LEAVE;
+ cpp_make_system_header (pfile, buffer, 0);
+ read_line_number (pfile, &action_number);
+ }
+ if (action_number == 3)
+ {
+ cpp_make_system_header (pfile, buffer, 1);
+ read_line_number (pfile, &action_number);
+ }
+ if (action_number == 4)
+ {
+ cpp_make_system_header (pfile, buffer, 2);
+ read_line_number (pfile, &action_number);
+ }
}
}