(root)/
gcc-13.2.0/
gcc/
testsuite/
g++.dg/
plugin/
comment_plugin.c
       1  /* Test of cpp_callbacks::comments.  */
       2  
       3  #include "gcc-plugin.h"
       4  #include "config.h"
       5  #include "system.h"
       6  #include "coretypes.h"
       7  #include "cpplib.h"
       8  #include "diagnostic.h"
       9  #include "c-family/c-pragma.h"
      10  
      11  int plugin_is_GPL_compatible;
      12  
      13  /* Test callback for cpp_callbacks::comments.  */
      14  
      15  void
      16  my_comment_cb (cpp_reader *, location_t loc,
      17  	       const unsigned char *content, size_t len)
      18  {
      19    if (in_system_header_at (loc))
      20      return;
      21  
      22    /* CONTENT contains the opening slash-star (or slash-slash),
      23       and for C-style comments contains the closing star-slash.  */
      24    gcc_assert (len >= 2);
      25    gcc_assert (content[0] == '/');
      26    gcc_assert (content[1] == '*' || content[1] == '/');
      27    bool c_style = (content[1] == '*');
      28    if (c_style)
      29      {
      30        gcc_assert (content[len - 2] == '*');
      31        gcc_assert (content[len - 1] == '/');
      32      }
      33  
      34    if (c_style)
      35      inform (loc, "got C-style comment; length=%i", len);
      36    else
      37      inform (loc, "got C++-style comment; length=%i", len);
      38  
      39    /* Print the content of the comment.
      40       For a C-style comment, the buffer CONTENT contains the opening
      41       slash-star and closing star-slash, so we can't directly verify
      42       it in the DejaGnu test without adding another comment, which
      43       would trigger this callback again.
      44       Hence we skip the syntactically-significant parts of the comment
      45       when printing it.  */
      46    fprintf (stderr, "stripped content of comment: >");
      47    /* Avoid printing trailing star-slash.  */
      48    if (c_style)
      49      len -= 2;
      50    for (size_t i = 2; i < len; i++)
      51      fputc (content[i], stderr);
      52    fprintf (stderr, "<\n");
      53  }
      54  
      55  int
      56  plugin_init (struct plugin_name_args *plugin_info,
      57  	     struct plugin_gcc_version *version)
      58  {
      59    cpp_callbacks *cb = cpp_get_callbacks (parse_in);
      60    cb->comment = my_comment_cb;
      61  
      62    return 0;
      63  }