From d3f7919d2a630945daba61dc8cf51ed5cde46ed1 Mon Sep 17 00:00:00 2001 From: thorpej Date: Sun, 7 Apr 2002 03:12:23 +0000 Subject: [PATCH] * cppinit.c (cpp_create_reader): Initialize discard_comments_in_macro_exp. (COMMAND_LINE_OPTIONS): Add "-CC" option. (cpp_handle_option): Handle "-CC" option. * cpplex.c (save_comment): If saving a C++ comment in a directive, convert it to a C comment. (_cpp_lex_direct): Pass second comment start character to save_comment to indicate comment type. * cpplib.c (_cpp_handle_directive): If processing a "#define" directive and discard_comments_in_macro_exp is false, re-enable saving of comments. (lex_macro_node): If discard_comments_in_macro_exp is false, discard any comments before the macro identifier. * cpplib.h (struct cpp_options): Add discard_comments_in_macro_exp member. * cppmacro.c (cpp_get_token): If expanding a macro while processing a directive, discard any comments we might encounter. (parse_params): If discard_comments_in_macro_exp is false, ignore comments in the macro parameter list. * gcc.c (cpp_unique_options): Add "-CC" option. (option_map): Map "--comments-in-macros" to "-CC". * doc/cppopts.texi: Document "-CC" option. * f/lang-specs.h: Add "-CC" option. * testsuite/gcc.dg/cpp/maccom1.c: New test. * testsuite/gcc.dg/cpp/maccom2.c: New test. * testsuite/gcc.dg/cpp/maccom3.c: New test. * testsuite/gcc.dg/cpp/maccom4.c: New test. * testsuite/gcc.dg/cpp/maccom5.c: New test. * testsuite/gcc.dg/cpp/maccom6.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@51975 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 32 ++++++++++++++++++++++++++++++++ gcc/cppinit.c | 6 ++++++ gcc/cpplex.c | 31 +++++++++++++++++++++++++------ gcc/cpplib.c | 18 +++++++++++++++++- gcc/cpplib.h | 4 ++++ gcc/cppmacro.c | 9 +++++++++ gcc/doc/cppopts.texi | 13 +++++++++++++ gcc/f/lang-specs.h | 3 ++- gcc/gcc.c | 4 +++- gcc/testsuite/gcc.dg/cpp/maccom1.c | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/cpp/maccom2.c | 18 ++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/maccom3.c | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/cpp/maccom4.c | 19 +++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/maccom5.c | 21 +++++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/maccom6.c | 24 ++++++++++++++++++++++++ 15 files changed, 227 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/maccom1.c create mode 100644 gcc/testsuite/gcc.dg/cpp/maccom2.c create mode 100644 gcc/testsuite/gcc.dg/cpp/maccom3.c create mode 100644 gcc/testsuite/gcc.dg/cpp/maccom4.c create mode 100644 gcc/testsuite/gcc.dg/cpp/maccom5.c create mode 100644 gcc/testsuite/gcc.dg/cpp/maccom6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ec6ed80..6263100 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,35 @@ +2002-04-06 Jason Thorpe + + * cppinit.c (cpp_create_reader): Initialize + discard_comments_in_macro_exp. + (COMMAND_LINE_OPTIONS): Add "-CC" option. + (cpp_handle_option): Handle "-CC" option. + * cpplex.c (save_comment): If saving a C++ comment in + a directive, convert it to a C comment. + (_cpp_lex_direct): Pass second comment start character to + save_comment to indicate comment type. + * cpplib.c (_cpp_handle_directive): If processing + a "#define" directive and discard_comments_in_macro_exp + is false, re-enable saving of comments. + (lex_macro_node): If discard_comments_in_macro_exp is false, + discard any comments before the macro identifier. + * cpplib.h (struct cpp_options): Add discard_comments_in_macro_exp + member. + * cppmacro.c (cpp_get_token): If expanding a macro while + processing a directive, discard any comments we might encounter. + (parse_params): If discard_comments_in_macro_exp is false, + ignore comments in the macro parameter list. + * gcc.c (cpp_unique_options): Add "-CC" option. + (option_map): Map "--comments-in-macros" to "-CC". + * doc/cppopts.texi: Document "-CC" option. + * f/lang-specs.h: Add "-CC" option. + * testsuite/gcc.dg/cpp/maccom1.c: New test. + * testsuite/gcc.dg/cpp/maccom2.c: New test. + * testsuite/gcc.dg/cpp/maccom3.c: New test. + * testsuite/gcc.dg/cpp/maccom4.c: New test. + * testsuite/gcc.dg/cpp/maccom5.c: New test. + * testsuite/gcc.dg/cpp/maccom6.c: New test. + 2002-04-06 John David Anglin PR middle-end/6180 diff --git a/gcc/cppinit.c b/gcc/cppinit.c index 8d4e0a7..ce950c0 100644 --- a/gcc/cppinit.c +++ b/gcc/cppinit.c @@ -488,6 +488,7 @@ cpp_create_reader (lang) set_lang (pfile, lang); CPP_OPTION (pfile, warn_import) = 1; CPP_OPTION (pfile, discard_comments) = 1; + CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1; CPP_OPTION (pfile, show_column) = 1; CPP_OPTION (pfile, tabstop) = 8; CPP_OPTION (pfile, operator_names) = 1; @@ -1172,6 +1173,7 @@ new_pending_directive (pend, text, handler) DEF_OPT("-version", 0, OPT__version) \ DEF_OPT("A", no_ass, OPT_A) \ DEF_OPT("C", 0, OPT_C) \ + DEF_OPT("CC", 0, OPT_CC) \ DEF_OPT("D", no_mac, OPT_D) \ DEF_OPT("H", 0, OPT_H) \ DEF_OPT("I", no_dir, OPT_I) \ @@ -1453,6 +1455,10 @@ cpp_handle_option (pfile, argc, argv, ignore) case OPT_C: CPP_OPTION (pfile, discard_comments) = 0; break; + case OPT_CC: + CPP_OPTION (pfile, discard_comments) = 0; + CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0; + break; case OPT_P: CPP_OPTION (pfile, no_line_commands) = 1; break; diff --git a/gcc/cpplex.c b/gcc/cpplex.c index a765967..a618031 100644 --- a/gcc/cpplex.c +++ b/gcc/cpplex.c @@ -83,7 +83,8 @@ static void parse_number PARAMS ((cpp_reader *, cpp_string *, int)); static int unescaped_terminator_p PARAMS ((cpp_reader *, const U_CHAR *)); static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t)); static bool trigraph_p PARAMS ((cpp_reader *)); -static void save_comment PARAMS ((cpp_reader *, cpp_token *, const U_CHAR *)); +static void save_comment PARAMS ((cpp_reader *, cpp_token *, const U_CHAR *, + cppchar_t)); static int name_p PARAMS ((cpp_reader *, const cpp_string *)); static int maybe_read_ucs PARAMS ((cpp_reader *, const unsigned char **, const unsigned char *, unsigned int *)); @@ -673,13 +674,14 @@ parse_string (pfile, token, terminator) /* The stored comment includes the comment start and any terminator. */ static void -save_comment (pfile, token, from) +save_comment (pfile, token, from, type) cpp_reader *pfile; cpp_token *token; const unsigned char *from; + cppchar_t type; { unsigned char *buffer; - unsigned int len; + unsigned int len, clen; len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */ @@ -687,14 +689,31 @@ save_comment (pfile, token, from) line, which we don't want to save in the comment. */ if (is_vspace (pfile->buffer->cur[-1])) len--; - buffer = _cpp_unaligned_alloc (pfile, len); + + /* If we are currently in a directive, then we need to store all + C++ comments as C comments internally, and so we need to + allocate a little extra space in that case. + + Note that the only time we encounter a directive here is + when we are saving comments in a "#define". */ + clen = (pfile->state.in_directive && type == '/') ? len + 2 : len; + + buffer = _cpp_unaligned_alloc (pfile, clen); token->type = CPP_COMMENT; - token->val.str.len = len; + token->val.str.len = clen; token->val.str.text = buffer; buffer[0] = '/'; memcpy (buffer + 1, from, len - 1); + + /* Finish conversion to a C comment, if necessary. */ + if (pfile->state.in_directive && type == '/') + { + buffer[1] = '*'; + buffer[clen - 2] = '*'; + buffer[clen - 1] = '/'; + } } /* Allocate COUNT tokens for RUN. */ @@ -1021,7 +1040,7 @@ _cpp_lex_direct (pfile) } /* Save the comment as a token in its own right. */ - save_comment (pfile, result, comment_start); + save_comment (pfile, result, comment_start, c); break; case '<': diff --git a/gcc/cpplib.c b/gcc/cpplib.c index 9a6b077..47dd977f3 100644 --- a/gcc/cpplib.c +++ b/gcc/cpplib.c @@ -394,6 +394,13 @@ _cpp_handle_directive (pfile, indented) if (dir) { + /* If we are processing a `#define' directive and we have been + requested to expand comments into macros, then re-enable + saving of comments. */ + if (dir == &dtable[T_DEFINE]) + pfile->state.save_comments = + ! CPP_OPTION (pfile, discard_comments_in_macro_exp); + pfile->directive = dir; (*pfile->directive->handler) (pfile); } @@ -445,7 +452,16 @@ lex_macro_node (pfile) In C++, it may not be any of the "named operators" either, per C++98 [lex.digraph], [lex.key]. Finally, the identifier may not have been poisoned. (In that case - the lexer has issued the error message for us.) */ + the lexer has issued the error message for us.) + + Note that if we're copying comments into macro expansions, we + could encounter comment tokens here, so eat them all up first. */ + + if (! CPP_OPTION (pfile, discard_comments_in_macro_exp)) + { + while (token->type == CPP_COMMENT) + token = _cpp_lex_token (pfile); + } if (token->type != CPP_NAME) { diff --git a/gcc/cpplib.h b/gcc/cpplib.h index e8298be..8b87b1b 100644 --- a/gcc/cpplib.h +++ b/gcc/cpplib.h @@ -255,6 +255,10 @@ struct cpp_options /* Nonzero means don't copy comments into the output file. */ unsigned char discard_comments; + /* Nonzero means don't copy comments into the output file during + macro expansion. */ + unsigned char discard_comments_in_macro_exp; + /* Nonzero means process the ISO trigraph sequences. */ unsigned char trigraphs; diff --git a/gcc/cppmacro.c b/gcc/cppmacro.c index a022581..6ba7ec4 100644 --- a/gcc/cppmacro.c +++ b/gcc/cppmacro.c @@ -1015,6 +1015,9 @@ cpp_get_token (pfile) return &pfile->avoid_paste; } + if (pfile->state.in_directive && result->type == CPP_COMMENT) + continue; + if (result->type != CPP_NAME) break; @@ -1194,6 +1197,12 @@ parse_params (pfile, macro) switch (token->type) { default: + /* Allow/ignore comments in parameter lists if we are + preserving comments in macro expansions. */ + if (token->type == CPP_COMMENT + && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)) + continue; + cpp_error (pfile, "\"%s\" may not appear in macro parameter list", cpp_token_as_text (pfile, token)); return 0; diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi index 5958abd..68bd439 100644 --- a/gcc/doc/cppopts.texi +++ b/gcc/doc/cppopts.texi @@ -527,6 +527,19 @@ For example, comments appearing at the start of what would be a directive line have the effect of turning that line into an ordinary source line, since the first token on the line is no longer a @samp{#}. +@item -CC +Do not discard comments, including during macro expansion. This is +like @option{-C}, except that comments contained within macros are +also passed through to the output file where the macro is expanded. + +In addition to the side-effects of the @option{-C} option, the +@option{-CC} option causes all C++-style comments inside a macro +to be converted to C-style comments. This is to prevent later use +of that macro from inadvertently commenting out the remainer of +the source line. + +The @option{-CC} option is generally used to support lint comments. + @item -gcc @opindex gcc Define the macros @sc{__gnuc__}, @sc{__gnuc_minor__} and diff --git a/gcc/f/lang-specs.h b/gcc/f/lang-specs.h index d6c3a14..e39a3e9 100644 --- a/gcc/f/lang-specs.h +++ b/gcc/f/lang-specs.h @@ -33,7 +33,8 @@ the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA {".r", "@ratfor", 0}, {"@ratfor", "%{C:%{!E:%eGNU C does not support -C without using -E}}\ - ratfor %{C} %{v} %i %{E:%W{o*}} %{!E: %{!pipe:-o %g.f} |\n\ + %{CC:%{!E:%eGNU C does not support -CC without using -E}}\ + ratfor %{C} %{CC} %{v} %i %{E:%W{o*}} %{!E: %{!pipe:-o %g.f} |\n\ f771 %{!pipe:%g.f} %(cc1_options) %{I*} %{!fsyntax-only:%(invoke_as)}}", 0}, {".f", "@f77", 0}, {".for", "@f77", 0}, diff --git a/gcc/gcc.c b/gcc/gcc.c index d0d0e3f..c160006 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -671,7 +671,8 @@ static const char *trad_capable_cpp = static const char *cpp_unique_options = "%{C:%{!E:%eGNU C does not support -C without using -E}}\ - %{!Q:-quiet} %{nostdinc*} %{C} %{v} %{I*} %{P} %{$} %I\ + %{CC:%{!E:%eGNU C does not support -CC without using -E}}\ + %{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*} %{P} %{$} %I\ %{MD:-MD %W{!o: %b.d}%W{o*:%.d%*}}\ %{MMD:-MMD %W{!o: %b.d}%W{o*:%.d%*}}\ %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*}\ @@ -917,6 +918,7 @@ static const struct option_map option_map[] = {"--bootclasspath", "-fbootclasspath=", "aj"}, {"--CLASSPATH", "-fclasspath=", "aj"}, {"--comments", "-C", 0}, + {"--comments-in-macros", "-CC", 0}, {"--compile", "-c", 0}, {"--debug", "-g", "oj"}, {"--define-macro", "-D", "aj"}, diff --git a/gcc/testsuite/gcc.dg/cpp/maccom1.c b/gcc/testsuite/gcc.dg/cpp/maccom1.c new file mode 100644 index 0000000..7660dff --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/maccom1.c @@ -0,0 +1,17 @@ +/* { dg-do preprocess } */ +/* { dg-options "-CC" } */ + +/* This tests to make sure that comments are ignored between # and the + directive name when the -CC option is used. + + Jason R. Thorpe, 6 Apr 2002 */ + +#/**/define def passed + +def + +/* + { dg-final { if ![file exists maccom1.i] { return } } } + { dg-final { if { [grep maccom1.i "^passed"] != "" } { return } } } + { dg-final { fail "maccom1.c: comment between # and directive name with -CC" } } +*/ diff --git a/gcc/testsuite/gcc.dg/cpp/maccom2.c b/gcc/testsuite/gcc.dg/cpp/maccom2.c new file mode 100644 index 0000000..dcaf37a --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/maccom2.c @@ -0,0 +1,18 @@ +/* { dg-do preprocess } */ +/* { dg-options "-CC" } */ + +/* This tests to make sure that comments between the #define directive + and the macro identifier are ignored (i.e. treated like whitespace) + when the -CC option is used. + + Jason R. Thorpe, 6 Apr 2002 */ + +#define/**/def passed + +def + +/* + { dg-final { if ![file exists maccom2.i] { return } } } + { dg-final { if { [grep maccom2.i "^passed"] != "" } { return } } } + { dg-final { fail "maccom2.c: comment between #define and identifier with -CC" } } +*/ diff --git a/gcc/testsuite/gcc.dg/cpp/maccom3.c b/gcc/testsuite/gcc.dg/cpp/maccom3.c new file mode 100644 index 0000000..1984535 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/maccom3.c @@ -0,0 +1,17 @@ +/* { dg-do preprocess } */ +/* { dg-options "-CC" } */ + +/* This tests to make sure that comments in the definition of a macro + parameter list are ignored when the -CC option is used. + + Jason R. Thorpe, 6 Apr 2002 */ + +#define def(x /**/, y) passed + +def(x,y) + +/* + { dg-final { if ![file exists maccom3.i] { return } } } + { dg-final { if { [grep maccom3.i "^passed"] != "" } { return } } } + { dg-final { fail "maccom3.c: comment in macro parameter list with -CC" } } +*/ diff --git a/gcc/testsuite/gcc.dg/cpp/maccom4.c b/gcc/testsuite/gcc.dg/cpp/maccom4.c new file mode 100644 index 0000000..b8a136f --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/maccom4.c @@ -0,0 +1,19 @@ +/* { dg-do preprocess } */ +/* { dg-options "-CC" } */ + +/* This tests to make sure the comment is saved in the macro and copied + to the output file when the macro is expanded when the -CC option is + used. + + Jason R. Thorpe, 6 Apr 2002 */ + +#define def /* passed */ + +def + +/* + /* The + in the regexp prevents it from matching itself. */ + { dg-final { if ![file exists maccom4.i] { return } } } + { dg-final { if { [grep maccom4.i "p+assed"] != "" } { return } } } + { dg-final { fail "maccom4.c: comment in macro expansion with -CC" } } +*/ diff --git a/gcc/testsuite/gcc.dg/cpp/maccom5.c b/gcc/testsuite/gcc.dg/cpp/maccom5.c new file mode 100644 index 0000000..f92f8bf --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/maccom5.c @@ -0,0 +1,21 @@ +/* { dg-do preprocess } */ +/* { dg-options "-CC" } */ + +/* This tests to make sure that C++ comments are converted to C comments + when saved in the macro and copied to the output file when the macro + is expanded when the -CC option is used. + + Jason R. Thorpe, 6 Apr 2002 */ + +#define def // passed + +def: + +/* + /* The + in the regexp prevents it from matching itself. */ + { dg-final { if ![file exists maccom5.i] { return } } } + { dg-final { if \{ [grep maccom5.i "p+assed"] != "" \} \{ } } + { dg-final { if \{ [grep maccom5.i "p+assed:"] == "" \} \{ } } + { dg-final { return \} \} } } + { dg-final { fail "maccom5.c: C++ comment in macro expansion with -CC" } } +*/ diff --git a/gcc/testsuite/gcc.dg/cpp/maccom6.c b/gcc/testsuite/gcc.dg/cpp/maccom6.c new file mode 100644 index 0000000..0b86c05 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/maccom6.c @@ -0,0 +1,24 @@ +/* { dg-do preprocess } */ +/* { dg-options "-CC" } */ + +/* This tests to make sure that expressions function properly + when used with macros containing comments and the -CC option + is being used. + + Jason R. Thorpe, 6 Apr 2002 */ + +#define ONE 1 /* one */ +#define TWO 2 /* two */ +#define THREE 3 /* three */ + +#if (ONE + TWO) != THREE +failed +#else +passed +#endif + +/* + { dg-final { if ![file exists maccom6.i] { return } } } + { dg-final { if { [grep maccom6.i "^passed"] != "" } { return } } } + { dg-final { fail "maccom6.c: comments in macro expressions with -CC" } } +*/ -- 2.7.4