public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [libcpp] RFA: Add support for comments retrieval
@ 2008-09-26 15:23 Arnaud Charlet
  2008-09-26 15:44 ` Basile STARYNKEVITCH
  2008-09-26 16:07 ` Tom Tromey
  0 siblings, 2 replies; 10+ messages in thread
From: Arnaud Charlet @ 2008-09-26 15:23 UTC (permalink / raw)
  To: gcc-patches; +Cc: Matthew Gingell

This is a patch that adds support for easy retrieval of C/C++ comments
parsed by libcpp.

We have another patch depending on this one that would then take advantage
of this new "cpp_get_comments" function. This other patch will add the
capability to dump GCC GENERIC trees using Ada syntax, thus providing
a way to generate Ada specs automatically from C/C++ header files.
In this context, being able to also dump the original comments from the
header files is very useful.

I'm sure this function could then be used in other contexts (e.g. other
kind of source code analyzer/plug-ins based on GCC).

Tested on i686-pc-linux-gnu, OK for trunk ?

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.

	* lex.c (store_comment, cpp_get_comments): New function.s 
	(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,23 @@ 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);
 
+typedef struct
+{
+  const char *comment;
+  source_location sloc;
+} cpp_comment;
+
+typedef struct
+{
+  cpp_comment *entries;
+  int count;
+  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 (void);
+
 /* In hash.c */
 
 /* Lookup an identifier in the hashtable.  Puts the identifier in the
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_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,44 @@ lex_string (cpp_reader *pfile, cpp_token
   create_literal (pfile, token, base, cur - base, type);
 }
 
+/* Table for storing comments in the pfile->state.save_comments case.
+   In other cases there is no overhead. */
+
+static cpp_comment_table comments = {NULL, 0, 0};
+
+/* Return the comment table. The client may not make any assumption
+   about the ordering of the table.  */
+extern cpp_comment_table
+cpp_get_comments (void) 
+{
+  return comments;
+}
+
+/* Append a comment to the end of the comment table. */
+static void 
+store_comment (cpp_token *token) 
+{
+  if (comments.allocated == 0)
+    {
+      comments.allocated = 256; 
+      comments.entries = 
+	(cpp_comment *) xmalloc (comments.allocated * sizeof (cpp_comment));
+    }
+
+  if (comments.count == comments.allocated)
+    {
+      comments.allocated *= 2;
+      comments.entries = 
+	(cpp_comment *) xrealloc (comments.entries, 
+				  comments.allocated * sizeof (cpp_comment));
+    }
+
+  comments.entries [comments.count].comment = 
+    xstrdup ((char *) (token->val.str.text));
+  comments.entries [comments.count].sloc = token->src_loc;
+  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 +748,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 (token);
 }
 
 /* Allocate COUNT tokens for RUN.  */

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2008-10-05 12:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-26 15:23 [libcpp] RFA: Add support for comments retrieval Arnaud Charlet
2008-09-26 15:44 ` Basile STARYNKEVITCH
2008-09-26 16:07   ` Arnaud Charlet
2008-09-26 16:07 ` Tom Tromey
2008-09-26 16:54   ` Arnaud Charlet
2008-09-26 18:21     ` Paolo Bonzini
2008-09-26 21:14       ` Arnaud Charlet
2008-09-26 21:17         ` Arnaud Charlet
2008-10-03 22:06           ` Tom Tromey
2008-10-05 13:19             ` Arnaud Charlet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).