[libcpp] RFA: Add support for comments retrieval

Arnaud Charlet charlet@adacore.com
Fri Sep 26 16:54:00 GMT 2008


> I think the general idea is ok.

Thanks.

> There are a few changes I would like.  Some of these are nits :)

Sure, thanks for the quick review.
Here is a new version taking your comments into account:

2008-09-26  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.

Index: include/cpplib.h
===================================================================
--- include/cpplib.h	(revision 140660)
+++ include/cpplib.h	(working copy)
@@ -870,6 +870,28 @@ extern const char *cpp_type2name (enum c
 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
+{
+  const char *comment;   /* string containing comment at a given location.  */
+  source_location sloc;  /* source location for the given comment.  */
+} cpp_comment;
+
+/* Structure holding all comments for a given cpp_reader.  */
+
+typedef struct
+{
+  cpp_comment *entries;  /* table of comment entries.  */
+  int count;             /* number of actual entries entered in the table.  */
+  int allocated;         /* number of entries allocated currently.  */
+} 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: init.c
===================================================================
--- init.c	(revision 140660)
+++ init.c	(working copy)
@@ -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: internal.h
===================================================================
--- internal.h	(revision 140660)
+++ internal.h	(working copy)
@@ -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: lex.c
===================================================================
--- lex.c	(revision 140660)
+++ lex.c	(working copy)
@@ -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,39 @@ lex_string (cpp_reader *pfile, cpp_token
   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) 
+{
+  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));
+    }
+
+  pfile->comments.entries [pfile->comments.count].comment = 
+    xstrdup ((char *) (token->val.str.text));
+  pfile->comments.entries [pfile->comments.count].sloc = token->src_loc;
+  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 +743,9 @@ save_comment (cpp_reader *pfile, cpp_tok
       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.  */



More information about the Gcc-patches mailing list