diff options
author | Ollie Wild <aaw@google.com> | 2007-07-30 18:29:20 +0000 |
---|---|---|
committer | Ollie Wild <aaw@gcc.gnu.org> | 2007-07-30 18:29:20 +0000 |
commit | ccfc4c91bb38ae91e4fe206fca657366e5f7caa7 (patch) | |
tree | bb584ffda6bfb46d8b03b533da0e2f408e0bbd23 /gcc | |
parent | 7bc980e1d9410f6182088fe4b60f125634e71941 (diff) | |
download | gcc-ccfc4c91bb38ae91e4fe206fca657366e5f7caa7.zip gcc-ccfc4c91bb38ae91e4fe206fca657366e5f7caa7.tar.gz gcc-ccfc4c91bb38ae91e4fe206fca657366e5f7caa7.tar.bz2 |
directives-only.c: New file.
libcpp/
* directives-only.c: New file.
* internal.h (struct _cpp_dir_only_callbacks): New.
(_cpp_preprocess_dir_only): New function.
* directives.c (_cpp_handle_directive): Check directives_only before
disabling execution of indented directives.
* files.c (_cpp_stack_file): Add directives_only check.
* include/cpplib.h (struct cpp_options): Add directives_only.
(cpp_init_special_builtins): New function.
* init.c (cpp_init_special_builtins): New function.
(cpp_init_builtins): Move builtin_array initialization to
cpp_init_special_builtins.
(post_options): Check directives_only before setting
pfile->state.prevent_expansion = 1.
* macro.c (_cpp_builtin_macro_text): Print an error if __COUNTER__
is expanded inside a directive while -fdirectives-only is enabled.
* Makefile.in (libcpp_a_OBJS): Add directives-only.o.
(libcpp_a_SOURCES): Add directives-only.c.
gcc/
* c-ppoutput.c (print_lines_directives_only): New function.
(scan_translation_unit_directives_only): New function.
(preprocess_file): Add call to scan_translation_unit_directives_only.
* c-opts.c (c_common_handle_option): Add OPT_fdirectives_only.
(sanitize_cpp_opts): Add default flag_dump_macros setting for
-fdirectives-only. Add errors for -fdirectives-only conflict with
-Wunused-macros and -traditional.
(finish_options): Add builtin macro initialization for
-fdirectives-only + -fpreprocessed.
* c.opt (fdirectives-only): New.
* doc/cppopts.texi (fdirectives-only): New.
gcc/testsuite/
* gcc.dg/cpp/counter-2.c: New test.
* gcc.dg/cpp/counter-3.c: New test.
* gcc.dg/cpp/dir-only-1.c: New test.
* gcc.dg/cpp/dir-only-1.h: New file.
* gcc.dg/cpp/dir-only-2.c: New test.
* gcc.dg/cpp/dir-only-3.c: New test.
* gcc.dg/cpp/dir-only-3a.h: New file.
* gcc.dg/cpp/dir-only-3b.h: New file.
* gcc.dg/cpp/dir-only-4.c: New test.
* gcc.dg/cpp/dir-only-5.c: New test.
* gcc.dg/cpp/dir-only-6.c: New test.
From-SVN: r127066
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 14 | ||||
-rw-r--r-- | gcc/c-opts.c | 19 | ||||
-rw-r--r-- | gcc/c-ppoutput.c | 25 | ||||
-rw-r--r-- | gcc/c.opt | 4 | ||||
-rw-r--r-- | gcc/doc/cppopts.texi | 22 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 14 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/counter-2.c | 14 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/counter-3.c | 10 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-1.c | 73 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-1.h | 3 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-2.c | 12 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-3.c | 13 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-3a.h | 8 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-3b.h | 9 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-4.c | 6 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-5.c | 6 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/cpp/dir-only-6.c | 7 |
17 files changed, 259 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 3e62b14..a2371b0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,17 @@ +2007-07-30 Ollie Wild <aaw@google.com> + + * c-ppoutput.c (print_lines_directives_only): New function. + (scan_translation_unit_directives_only): New function. + (preprocess_file): Add call to scan_translation_unit_directives_only. + * c-opts.c (c_common_handle_option): Add OPT_fdirectives_only. + (sanitize_cpp_opts): Add default flag_dump_macros setting for + -fdirectives-only. Add errors for -fdirectives-only conflict with + -Wunused-macros and -traditional. + (finish_options): Add builtin macro initialization for + -fdirectives-only + -fpreprocessed. + * c.opt (fdirectives-only): New. + * doc/cppopts.texi (fdirectives-only): New. + 2007-07-30 Kaveh R. Ghazi <ghazi@caip.rutgers.edu> * alias.c (record_set, memory_modified_1): Constify. diff --git a/gcc/c-opts.c b/gcc/c-opts.c index a78c692..a637ffc 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -618,6 +618,10 @@ c_common_handle_option (size_t scode, const char *arg, int value) disable_builtin_function (arg); break; + case OPT_fdirectives_only: + cpp_opts->directives_only = 1; + break; + case OPT_fdollars_in_identifiers: cpp_opts->dollars_in_ident = value; break; @@ -1406,6 +1410,11 @@ sanitize_cpp_opts (void) if (flag_dump_macros == 'M') flag_no_output = 1; + /* By default, -fdirectives-only implies -dD. This allows subsequent phases + to perform proper macro expansion. */ + if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros) + flag_dump_macros = 'D'; + /* Disable -dD, -dN and -dI if normal output is suppressed. Allow -dM since at least glibc relies on -M -dM to work. */ /* Also, flag_no_output implies flag_no_line_commands, always. */ @@ -1436,6 +1445,14 @@ sanitize_cpp_opts (void) actually output the current directory? */ if (flag_working_directory == -1) flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE); + + if (cpp_opts->directives_only) + { + if (warn_unused_macros) + error ("-fdirectives-only is incompatible with -Wunused_macros"); + if (cpp_opts->traditional) + error ("-fdirectives-only is incompatible with -traditional"); + } } /* Add include path with a prefix at the front of its name. */ @@ -1519,6 +1536,8 @@ finish_options (void) } } } + else if (cpp_opts->directives_only) + cpp_init_special_builtins (parse_in); include_cursor = 0; push_command_line_include (); diff --git a/gcc/c-ppoutput.c b/gcc/c-ppoutput.c index 6c44095..22ee0f6 100644 --- a/gcc/c-ppoutput.c +++ b/gcc/c-ppoutput.c @@ -41,6 +41,8 @@ static struct /* General output routines. */ static void scan_translation_unit (cpp_reader *); +static void print_lines_directives_only (int, const void *, size_t); +static void scan_translation_unit_directives_only (cpp_reader *); static void scan_translation_unit_trad (cpp_reader *); static void account_for_newlines (const unsigned char *, size_t); static int dump_macro (cpp_reader *, cpp_hashnode *, void *); @@ -75,6 +77,9 @@ preprocess_file (cpp_reader *pfile) } else if (cpp_get_options (pfile)->traditional) scan_translation_unit_trad (pfile); + else if (cpp_get_options (pfile)->directives_only + && !cpp_get_options (pfile)->preprocessed) + scan_translation_unit_directives_only (pfile); else scan_translation_unit (pfile); @@ -179,6 +184,26 @@ scan_translation_unit (cpp_reader *pfile) } } +static void +print_lines_directives_only (int lines, const void *buf, size_t size) +{ + print.src_line += lines; + fwrite (buf, 1, size, print.outf); +} + +/* Writes out the preprocessed file, handling spacing and paste + avoidance issues. */ +static void +scan_translation_unit_directives_only (cpp_reader *pfile) +{ + struct _cpp_dir_only_callbacks cb; + + cb.print_lines = print_lines_directives_only; + cb.maybe_print_line = maybe_print_line; + + _cpp_preprocess_dir_only (pfile, &cb); +} + /* Adjust print.src_line for newlines embedded in output. */ static void account_for_newlines (const unsigned char *str, size_t len) @@ -523,6 +523,10 @@ fdefault-inline C++ ObjC++ Inline member functions by default +fdirectives-only +C ObjC C++ ObjC++ +Preprocess directives only. + fdollars-in-identifiers C ObjC C++ ObjC++ Permit '$' as an identifier character diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi index 73db832..dbc8f0e 100644 --- a/gcc/doc/cppopts.texi +++ b/gcc/doc/cppopts.texi @@ -518,6 +518,28 @@ Search @var{dir} only for header files requested with If @var{dir} begins with @code{=}, then the @code{=} will be replaced by the sysroot prefix; see @option{--sysroot} and @option{-isysroot}. +@item -fdirectives-only +@opindex fdirectives-only +When preprocessing, handle directives, but do not expand macros. + +The option's behavior depends on the @option{-E} and @option{-fpreprocessed} +options. + +With @option{-E}, preprocessing is limited to the handling of directives +such as @code{#define}, @code{#ifdef}, and @code{#error}. Other +preprocessor operations, such as macro expansion and trigraph +conversion are not performed. In addition, the @option{-dD} option is +implicitly enabled. + +With @option{-fpreprocessed}, predefinition of command line and most +builtin macros is disabled. Macros such as @code{__LINE__}, which are +contextually dependent, are handled normally. This enables compilation of +files previously preprocessed with @code{-E -fdirectives-only}. + +With both @option{-E} and @option{-fpreprocessed}, the rules for +@option{-fpreprocessed} take precedence. This enables full preprocessing of +files previously preprocessed with @code{-E -fdirectives-only}. + @item -fdollars-in-identifiers @opindex fdollars-in-identifiers @anchor{fdollars-in-identifiers} diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b7681c3..7d6cd5e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,17 @@ +2007-07-30 Ollie Wild <aaw@google.com> + + * gcc.dg/cpp/counter-2.c: New test. + * gcc.dg/cpp/counter-3.c: New test. + * gcc.dg/cpp/dir-only-1.c: New test. + * gcc.dg/cpp/dir-only-1.h: New file. + * gcc.dg/cpp/dir-only-2.c: New test. + * gcc.dg/cpp/dir-only-3.c: New test. + * gcc.dg/cpp/dir-only-3a.h: New file. + * gcc.dg/cpp/dir-only-3b.h: New file. + * gcc.dg/cpp/dir-only-4.c: New test. + * gcc.dg/cpp/dir-only-5.c: New test. + * gcc.dg/cpp/dir-only-6.c: New test. + 2007-07-30 Julian Brown <julian@codesourcery.com> * gcc.target/arm/neon/v*.c: Regenerate. diff --git a/gcc/testsuite/gcc.dg/cpp/counter-2.c b/gcc/testsuite/gcc.dg/cpp/counter-2.c new file mode 100644 index 0000000..7d6578d --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/counter-2.c @@ -0,0 +1,14 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options -fdirectives-only } */ + +/* Tests __COUNTER__ macro expansion is disabled inside directives with + -fdirectives-only. */ + +#ifdef __COUNTER__ /* Macro not expanded. */ +#endif + +#if __COUNTER__ == 0 /* { dg-error "__COUNTER__ expanded inside directive with -fdirectives-only" } */ +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/counter-3.c b/gcc/testsuite/gcc.dg/cpp/counter-3.c new file mode 100644 index 0000000..3b1824f --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/counter-3.c @@ -0,0 +1,10 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options "-fdirectives-only -fpreprocessed" } */ + +/* Tests __COUNTER__ macro expansion is enabled outside directives with + -fdirectives-only. */ + +int zero = __COUNTER__; diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-1.c b/gcc/testsuite/gcc.dg/cpp/dir-only-1.c new file mode 100644 index 0000000..3c22616 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-1.c @@ -0,0 +1,73 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options -fdirectives-only } */ + +/* Tests scan_translation_unit_directives_only()'s handling of corner cases. */ + +/* Ignore directives inside block comments... +#error directive inside block comment +*/ + +// Escaped newline doesn't terminate line comment \ +#error directive inside line comment + +/* A comment canot start inside a string. */ +const char *c1 = "/*"; +#define NOT_IN_COMMENT +const char *c2 = "*/"; +#ifndef NOT_IN_COMMENT +#error Comment started inside a string literal +#endif + +/* Escaped newline handling. */ +int i; \ +#error ignored escaped newline + \ + \ +#define BOL +#ifndef BOL +#error escaped newline did not preserve beginning of line +#endif + +/* Handles \\ properly at the end of a string. */ +"string ends in \\"/* +#error Missed string terminator. +*/ + +/* Handles macro expansion in preprocessing directives. */ +#define HEADER "dir-only-1.h" +#include HEADER +#ifndef GOT_HEADER +#error Failed to include header. +#endif + +/\ +* +#define IN_COMMENT +*/ +#ifdef IN_COMMENT +#error Escaped newline breaks block comment initiator. +#endif + +/* +*\ +/ +#define NOT_IN_COMMENT2 +/**/ +#ifndef NOT_IN_COMMENT2 +#error Escaped newline breaks block comment terminator. +#endif + +/* Test escaped newline inside character escape sequence. */ +"\\ +\"/* +#error Missed string terminator +*/ + +/* Block comments don't mask trailing preprocessing + directive. */ #define NOT_MASKED +#ifndef NOT_MASKED +#error Comment masks trailing directive. +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-1.h b/gcc/testsuite/gcc.dg/cpp/dir-only-1.h new file mode 100644 index 0000000..96dbcc0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-1.h @@ -0,0 +1,3 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ +#define GOT_HEADER diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-2.c b/gcc/testsuite/gcc.dg/cpp/dir-only-2.c new file mode 100644 index 0000000..489b4d6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-2.c @@ -0,0 +1,12 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options "-fpreprocessed -fdirectives-only -DNOT_SET" } */ + +/* Tests -fdirectives-only + -fpreprocessed. */ + +/* Check this is not defined. */ +#ifdef NOT_SET +#error Command line macro not disabled. +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-3.c b/gcc/testsuite/gcc.dg/cpp/dir-only-3.c new file mode 100644 index 0000000..e6eaa01 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-3.c @@ -0,0 +1,13 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options "-fdirectives-only -H" } */ +/* { dg-message "dir-only-3a\.h\n\[^\n\]*dir-only-3b\.h\n\[^\n\]*dir-only-3a\.h\n" "include guard check" { target *-*-* } 0 } */ + +/* Tests include guards. */ + +#include "dir-only-3a.h" +#include "dir-only-3b.h" +#include "dir-only-3b.h" +#include "dir-only-3a.h" diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-3a.h b/gcc/testsuite/gcc.dg/cpp/dir-only-3a.h new file mode 100644 index 0000000..6644bbf --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-3a.h @@ -0,0 +1,8 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +extern int outside_guard + +#ifndef DIR_ONLY_3A_H +#define DIR_ONLY_3A_H +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-3b.h b/gcc/testsuite/gcc.dg/cpp/dir-only-3b.h new file mode 100644 index 0000000..4edaa7b --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-3b.h @@ -0,0 +1,9 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +#ifndef DIR_ONLY_3B_H +#define DIR_ONLY_3B_H + +extern int inside guard; + +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-4.c b/gcc/testsuite/gcc.dg/cpp/dir-only-4.c new file mode 100644 index 0000000..a7b5f04 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-4.c @@ -0,0 +1,6 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options "-fdirectives-only -Wunused-macros" } */ +/* { dg-error "-fdirectives-only is incompatible with -Wunused_macros\n" "-Wunused-macros check" { target *-*-* } 0 } */ diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-5.c b/gcc/testsuite/gcc.dg/cpp/dir-only-5.c new file mode 100644 index 0000000..643a4d7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-5.c @@ -0,0 +1,6 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options "-fdirectives-only -traditional" } */ +/* { dg-error "-fdirectives-only is incompatible with -traditional\n" "-traditional check" { target *-*-* } 0 } */ diff --git a/gcc/testsuite/gcc.dg/cpp/dir-only-6.c b/gcc/testsuite/gcc.dg/cpp/dir-only-6.c new file mode 100644 index 0000000..0023205 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/dir-only-6.c @@ -0,0 +1,7 @@ +/* Copyright 2007 Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. */ + +/* { dg-do preprocess } */ +/* { dg-options -fdirectives-only } */ + +/* { dg-error "unterminated comment" } |