]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
cpplib.h (cpp_comments, [...]): New structs.
authorMatthew Gingell <gingell@adacore.com>
Sun, 5 Oct 2008 12:35:36 +0000 (12:35 +0000)
committerArnaud Charlet <charlet@gcc.gnu.org>
Sun, 5 Oct 2008 12:35:36 +0000 (14:35 +0200)
2008-10-05  Matthew Gingell  <gingell@adacore.com>
    Arnaud Charlet  <charlet@adacore.com>

* include/cpplib.h (cpp_comments, cpp_comment_table): New structs.
(cpp_get_comments): New function.
* internal.h (struct cpp_reader): Add comments field.
* init.c (cpp_destroy): Free comments.
* lex.c (store_comment, cpp_get_comments): New functions.
(comments): New struct.
(save_comment): Store comments in comments struct.

Co-Authored-By: Arnaud Charlet <charlet@adacore.com>
From-SVN: r140883

libcpp/ChangeLog
libcpp/include/cpplib.h
libcpp/init.c
libcpp/internal.h
libcpp/lex.c

index 39be98972ae8ef174d3fe5a966787805a8a311b1..278bb06d6b85b2dd989d14bed66ed988137110fc 100644 (file)
@@ -1,3 +1,14 @@
+2008-10-05  Matthew Gingell  <gingell@adacore.com>
+           Arnaud Charlet  <charlet@adacore.com>
+
+       * include/cpplib.h (cpp_comments, cpp_comment_table): New structs.
+       (cpp_get_comments): New function.
+       * internal.h (struct cpp_reader): Add comments field.
+       * init.c (cpp_destroy): Free comments.
+       * lex.c (store_comment, cpp_get_comments): New functions.
+       (comments): New struct.
+       (save_comment): Store comments in comments struct.
+
 2008-09-18  Simon Baldwin  <simonb@google.com>
 
        * include/cpplib.h (struct cpp_options): Add new boolean flag
index 4f073f9943330262d3e6e666c1f9500d184f82d9..5720c6fc8ea9a4efd8a89270cf69f1ce2e0a4b28 100644 (file)
@@ -870,6 +870,36 @@ extern const char *cpp_type2name (enum cpp_ttype);
 extern cppchar_t cpp_parse_escape (cpp_reader *, const unsigned char ** pstr,
                                   const unsigned char *limit, int wide);
 
+/* Structure used to hold a comment block at a given location in the
+   source code.  */
+
+typedef struct
+{
+  /* Text of the comment including the terminators.  */
+  char *comment;
+
+  /* source location for the given comment.  */
+  source_location sloc;
+} cpp_comment;
+
+/* Structure holding all comments for a given cpp_reader.  */
+
+typedef struct
+{
+  /* table of comment entries.  */
+  cpp_comment *entries;
+
+  /* number of actual entries entered in the table.  */
+  int count;
+
+  /* number of entries allocated currently.  */
+  int allocated;
+} cpp_comment_table;
+
+/* Returns the table of comments encountered by the preprocessor. This
+   table is only populated when pfile->state.save_comments is true. */
+extern cpp_comment_table *cpp_get_comments (cpp_reader *);
+
 /* In hash.c */
 
 /* Lookup an identifier in the hashtable.  Puts the identifier in the
index 0db167c133c4ed287c741642a2d4e31fd9de94cb..cc7a09ed8c27fbc6db12c0cf9af8f326532374c8 100644 (file)
@@ -245,6 +245,7 @@ cpp_destroy (cpp_reader *pfile)
 {
   cpp_context *context, *contextn;
   tokenrun *run, *runn;
+  int i;
 
   free (pfile->op_stack);
 
@@ -287,6 +288,14 @@ cpp_destroy (cpp_reader *pfile)
       free (context);
     }
 
+  if (pfile->comments.entries)
+    {
+      for (i = 0; i < pfile->comments.count; i++)
+       free (pfile->comments.entries[i].comment);
+
+      free (pfile->comments.entries);
+    }
+
   free (pfile);
 }
 
index c5bf35eab8423484a3d2966a041353e4f41b9c73..af075b4b1c94cc06d8352854b1f8599e8591817f 100644 (file)
@@ -471,6 +471,9 @@ struct cpp_reader
 
   /* Next value of __COUNTER__ macro. */
   unsigned int counter;
+
+  /* Table of comments, when state.save_comments is true.  */
+  cpp_comment_table comments;
 };
 
 /* Character classes.  Based on the more primitive macros in safe-ctype.h.
index 2eb66bd63420f3338353bf5921cd53f410c5c076..57364f00bb98da1f3fa3085973450dc536d12699 100644 (file)
@@ -55,6 +55,7 @@ static int skip_line_comment (cpp_reader *);
 static void skip_whitespace (cpp_reader *, cppchar_t);
 static void lex_string (cpp_reader *, cpp_token *, const uchar *);
 static void save_comment (cpp_reader *, cpp_token *, const uchar *, cppchar_t);
+static void store_comment (cpp_reader *, cpp_token *);
 static void create_literal (cpp_reader *, cpp_token *, const uchar *,
                            unsigned int, enum cpp_ttype);
 static bool warn_in_comment (cpp_reader *, _cpp_line_note *);
@@ -670,6 +671,51 @@ lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
   create_literal (pfile, token, base, cur - base, type);
 }
 
+/* Return the comment table. The client may not make any assumption
+   about the ordering of the table.  */
+cpp_comment_table *
+cpp_get_comments (cpp_reader *pfile)
+{
+  return &pfile->comments;
+}
+
+/* Append a comment to the end of the comment table. */
+static void 
+store_comment (cpp_reader *pfile, cpp_token *token) 
+{
+  int len;
+
+  if (pfile->comments.allocated == 0)
+    {
+      pfile->comments.allocated = 256; 
+      pfile->comments.entries = (cpp_comment *) xmalloc
+       (pfile->comments.allocated * sizeof (cpp_comment));
+    }
+
+  if (pfile->comments.count == pfile->comments.allocated)
+    {
+      pfile->comments.allocated *= 2;
+      pfile->comments.entries = (cpp_comment *) xrealloc
+       (pfile->comments.entries,
+        pfile->comments.allocated * sizeof (cpp_comment));
+    }
+
+  len = token->val.str.len;
+
+  /* Copy comment. Note, token may not be NULL terminated. */
+  pfile->comments.entries[pfile->comments.count].comment = 
+    (char *) xmalloc (sizeof (char) * (len + 1));
+  memcpy (pfile->comments.entries[pfile->comments.count].comment,
+         token->val.str.text, len);
+  pfile->comments.entries[pfile->comments.count].comment[len] = '\0';
+
+  /* Set source location. */
+  pfile->comments.entries[pfile->comments.count].sloc = token->src_loc;
+
+  /* Increment the count of entries in the comment table. */
+  pfile->comments.count++;
+}
+
 /* The stored comment includes the comment start and any terminator.  */
 static void
 save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from,
@@ -709,6 +755,9 @@ save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from,
       buffer[clen - 2] = '*';
       buffer[clen - 1] = '/';
     }
+
+  /* Finally store this comment for use by clients of libcpp. */
+  store_comment (pfile, token);
 }
 
 /* Allocate COUNT tokens for RUN.  */