]> gcc.gnu.org Git - gcc.git/commitdiff
cppinit.c (cpp_create_reader): Initialize discard_comments_in_macro_exp.
authorJason Thorpe <thorpej@wasabisystems.com>
Sun, 7 Apr 2002 03:12:23 +0000 (03:12 +0000)
committerJason Thorpe <thorpej@gcc.gnu.org>
Sun, 7 Apr 2002 03:12:23 +0000 (03:12 +0000)
* 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

15 files changed:
gcc/ChangeLog
gcc/cppinit.c
gcc/cpplex.c
gcc/cpplib.c
gcc/cpplib.h
gcc/cppmacro.c
gcc/doc/cppopts.texi
gcc/f/lang-specs.h
gcc/gcc.c
gcc/testsuite/gcc.dg/cpp/maccom1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/maccom2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/maccom3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/maccom4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/maccom5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/maccom6.c [new file with mode: 0644]

index ec6ed8025cf5ac3ef24bb11d0a9b6fbf11a87802..62631007bb7144ee4fb3031a62e6a8c7861d45a8 100644 (file)
@@ -1,3 +1,35 @@
+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>
 
        PR middle-end/6180
index 8d4e0a706280d09178711ce66573d7c34a08931a..ce950c0eaf5c81bd7b80c38e1bc262cc53d0d34a 100644 (file)
@@ -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;
index a765967facec2046decfba6542996770b4a20694..a6180314511d5fc7327751eaadef680e1e07c84b 100644 (file)
@@ -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 '<':
index 9a6b077f24b40e1747986b87d4ed8dcd1ed537a2..47dd977f3051c32a20b09c02cf2086eff207274e 100644 (file)
@@ -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)
     {
index e8298be1fae75d2bb23377155a2672d6b5ce26d4..8b87b1b41a02780022ecc58dbaaec5bfddcfd316 100644 (file)
@@ -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;
 
index a022581f84ff0a61bcea3b831f6e50ea2e884c38..6ba7ec4780e624bfe22b0bdbd36ccfc63fb340df 100644 (file)
@@ -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;
index 5958abde263b4236b6bdb4da51d4c2d4344c71d9..68bd43902ad30307948303b0b11b812db45e62d1 100644 (file)
@@ -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
index d6c3a1453d9226dcd7b797e770c7217d78f5842c..e39a3e9b5b224b520bece751b1ad553cc43ebaf8 100644 (file)
@@ -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},
index d0d0e3f079a2632cb67d106b4fe2ef12004b82da..c160006f88dedd157811807d54f140edc39c6c7a 100644 (file)
--- 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 (file)
index 0000000..7660dff
--- /dev/null
@@ -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 (file)
index 0000000..dcaf37a
--- /dev/null
@@ -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 (file)
index 0000000..1984535
--- /dev/null
@@ -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 (file)
index 0000000..b8a136f
--- /dev/null
@@ -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 (file)
index 0000000..f92f8bf
--- /dev/null
@@ -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 (file)
index 0000000..0b86c05
--- /dev/null
@@ -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"  } }
+*/
This page took 0.084331 seconds and 5 git commands to generate.