Commit 477cdac7 by Jason Thorpe Committed by Jason Thorpe

cppinit.c (cpp_create_reader): Initialize discard_comments_in_macro_exp.

* 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.

From-SVN: r51975
parent 03da5286
2002-04-06 Jason Thorpe <thorpej@wasabisystems.com>
* 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 <dave@hiauly1.hia.nrc.ca> 2002-04-06 John David Anglin <dave@hiauly1.hia.nrc.ca>
PR middle-end/6180 PR middle-end/6180
......
...@@ -488,6 +488,7 @@ cpp_create_reader (lang) ...@@ -488,6 +488,7 @@ cpp_create_reader (lang)
set_lang (pfile, lang); set_lang (pfile, lang);
CPP_OPTION (pfile, warn_import) = 1; CPP_OPTION (pfile, warn_import) = 1;
CPP_OPTION (pfile, discard_comments) = 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, show_column) = 1;
CPP_OPTION (pfile, tabstop) = 8; CPP_OPTION (pfile, tabstop) = 8;
CPP_OPTION (pfile, operator_names) = 1; CPP_OPTION (pfile, operator_names) = 1;
...@@ -1172,6 +1173,7 @@ new_pending_directive (pend, text, handler) ...@@ -1172,6 +1173,7 @@ new_pending_directive (pend, text, handler)
DEF_OPT("-version", 0, OPT__version) \ DEF_OPT("-version", 0, OPT__version) \
DEF_OPT("A", no_ass, OPT_A) \ DEF_OPT("A", no_ass, OPT_A) \
DEF_OPT("C", 0, OPT_C) \ DEF_OPT("C", 0, OPT_C) \
DEF_OPT("CC", 0, OPT_CC) \
DEF_OPT("D", no_mac, OPT_D) \ DEF_OPT("D", no_mac, OPT_D) \
DEF_OPT("H", 0, OPT_H) \ DEF_OPT("H", 0, OPT_H) \
DEF_OPT("I", no_dir, OPT_I) \ DEF_OPT("I", no_dir, OPT_I) \
...@@ -1453,6 +1455,10 @@ cpp_handle_option (pfile, argc, argv, ignore) ...@@ -1453,6 +1455,10 @@ cpp_handle_option (pfile, argc, argv, ignore)
case OPT_C: case OPT_C:
CPP_OPTION (pfile, discard_comments) = 0; CPP_OPTION (pfile, discard_comments) = 0;
break; break;
case OPT_CC:
CPP_OPTION (pfile, discard_comments) = 0;
CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
break;
case OPT_P: case OPT_P:
CPP_OPTION (pfile, no_line_commands) = 1; CPP_OPTION (pfile, no_line_commands) = 1;
break; break;
......
...@@ -83,7 +83,8 @@ static void parse_number PARAMS ((cpp_reader *, cpp_string *, int)); ...@@ -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 int unescaped_terminator_p PARAMS ((cpp_reader *, const U_CHAR *));
static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t)); static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
static bool trigraph_p PARAMS ((cpp_reader *)); 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 name_p PARAMS ((cpp_reader *, const cpp_string *));
static int maybe_read_ucs PARAMS ((cpp_reader *, const unsigned char **, static int maybe_read_ucs PARAMS ((cpp_reader *, const unsigned char **,
const unsigned char *, unsigned int *)); const unsigned char *, unsigned int *));
...@@ -673,13 +674,14 @@ parse_string (pfile, token, terminator) ...@@ -673,13 +674,14 @@ parse_string (pfile, token, terminator)
/* The stored comment includes the comment start and any terminator. */ /* The stored comment includes the comment start and any terminator. */
static void static void
save_comment (pfile, token, from) save_comment (pfile, token, from, type)
cpp_reader *pfile; cpp_reader *pfile;
cpp_token *token; cpp_token *token;
const unsigned char *from; const unsigned char *from;
cppchar_t type;
{ {
unsigned char *buffer; unsigned char *buffer;
unsigned int len; unsigned int len, clen;
len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */ len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
...@@ -687,14 +689,31 @@ save_comment (pfile, token, from) ...@@ -687,14 +689,31 @@ save_comment (pfile, token, from)
line, which we don't want to save in the comment. */ line, which we don't want to save in the comment. */
if (is_vspace (pfile->buffer->cur[-1])) if (is_vspace (pfile->buffer->cur[-1]))
len--; 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->type = CPP_COMMENT;
token->val.str.len = len; token->val.str.len = clen;
token->val.str.text = buffer; token->val.str.text = buffer;
buffer[0] = '/'; buffer[0] = '/';
memcpy (buffer + 1, from, len - 1); 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. */ /* Allocate COUNT tokens for RUN. */
...@@ -1021,7 +1040,7 @@ _cpp_lex_direct (pfile) ...@@ -1021,7 +1040,7 @@ _cpp_lex_direct (pfile)
} }
/* Save the comment as a token in its own right. */ /* Save the comment as a token in its own right. */
save_comment (pfile, result, comment_start); save_comment (pfile, result, comment_start, c);
break; break;
case '<': case '<':
......
...@@ -394,6 +394,13 @@ _cpp_handle_directive (pfile, indented) ...@@ -394,6 +394,13 @@ _cpp_handle_directive (pfile, indented)
if (dir) 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 = dir;
(*pfile->directive->handler) (pfile); (*pfile->directive->handler) (pfile);
} }
...@@ -445,7 +452,16 @@ lex_macro_node (pfile) ...@@ -445,7 +452,16 @@ lex_macro_node (pfile)
In C++, it may not be any of the "named operators" either, In C++, it may not be any of the "named operators" either,
per C++98 [lex.digraph], [lex.key]. per C++98 [lex.digraph], [lex.key].
Finally, the identifier may not have been poisoned. (In that case 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) if (token->type != CPP_NAME)
{ {
......
...@@ -255,6 +255,10 @@ struct cpp_options ...@@ -255,6 +255,10 @@ struct cpp_options
/* Nonzero means don't copy comments into the output file. */ /* Nonzero means don't copy comments into the output file. */
unsigned char discard_comments; 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. */ /* Nonzero means process the ISO trigraph sequences. */
unsigned char trigraphs; unsigned char trigraphs;
......
...@@ -1015,6 +1015,9 @@ cpp_get_token (pfile) ...@@ -1015,6 +1015,9 @@ cpp_get_token (pfile)
return &pfile->avoid_paste; return &pfile->avoid_paste;
} }
if (pfile->state.in_directive && result->type == CPP_COMMENT)
continue;
if (result->type != CPP_NAME) if (result->type != CPP_NAME)
break; break;
...@@ -1194,6 +1197,12 @@ parse_params (pfile, macro) ...@@ -1194,6 +1197,12 @@ parse_params (pfile, macro)
switch (token->type) switch (token->type)
{ {
default: 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_error (pfile, "\"%s\" may not appear in macro parameter list",
cpp_token_as_text (pfile, token)); cpp_token_as_text (pfile, token));
return 0; return 0;
......
...@@ -527,6 +527,19 @@ For example, comments appearing at the start of what would be a ...@@ -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 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{#}. 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 @item -gcc
@opindex gcc @opindex gcc
Define the macros @sc{__gnuc__}, @sc{__gnuc_minor__} and Define the macros @sc{__gnuc__}, @sc{__gnuc_minor__} and
......
...@@ -33,7 +33,8 @@ the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA ...@@ -33,7 +33,8 @@ the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
{".r", "@ratfor", 0}, {".r", "@ratfor", 0},
{"@ratfor", {"@ratfor",
"%{C:%{!E:%eGNU C does not support -C without using -E}}\ "%{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}, f771 %{!pipe:%g.f} %(cc1_options) %{I*} %{!fsyntax-only:%(invoke_as)}}", 0},
{".f", "@f77", 0}, {".f", "@f77", 0},
{".for", "@f77", 0}, {".for", "@f77", 0},
......
...@@ -671,7 +671,8 @@ static const char *trad_capable_cpp = ...@@ -671,7 +671,8 @@ static const char *trad_capable_cpp =
static const char *cpp_unique_options = static const char *cpp_unique_options =
"%{C:%{!E:%eGNU C does not support -C without using -E}}\ "%{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%*}}\ %{MD:-MD %W{!o: %b.d}%W{o*:%.d%*}}\
%{MMD:-MMD %W{!o: %b.d}%W{o*:%.d%*}}\ %{MMD:-MMD %W{!o: %b.d}%W{o*:%.d%*}}\
%{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*}\ %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
...@@ -917,6 +918,7 @@ static const struct option_map option_map[] = ...@@ -917,6 +918,7 @@ static const struct option_map option_map[] =
{"--bootclasspath", "-fbootclasspath=", "aj"}, {"--bootclasspath", "-fbootclasspath=", "aj"},
{"--CLASSPATH", "-fclasspath=", "aj"}, {"--CLASSPATH", "-fclasspath=", "aj"},
{"--comments", "-C", 0}, {"--comments", "-C", 0},
{"--comments-in-macros", "-CC", 0},
{"--compile", "-c", 0}, {"--compile", "-c", 0},
{"--debug", "-g", "oj"}, {"--debug", "-g", "oj"},
{"--define-macro", "-D", "aj"}, {"--define-macro", "-D", "aj"},
......
/* { 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" } }
*/
/* { 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" } }
*/
/* { 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" } }
*/
/* { 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" } }
*/
/* { 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" } }
*/
/* { 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" } }
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment