[hawkmoth] Re: [PATCH 08/13] hawkmoth: filter non documentation comments early on

  • From: Jani Nikula <jani@xxxxxxxxxx>
  • To: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>, hawkmoth@xxxxxxxxxxxxx
  • Date: Tue, 08 Jan 2019 23:30:26 +0200

On Tue, 08 Jan 2019, Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx> wrote:

Discarding comments that are not documentation early on saves a few
repeated lines of code later on in the recursive part of the comment
parser and a few dud iterations.

I didn't test... but does this change the behaviour for things like:

/**
 * top level documentation comment
 */

/* meh */
int some_undocumented_function(void);

where the /* meh */ comment prevents the top level comment from becoming
documentation for the next cursor.

BR,
Jani.

---
 hawkmoth/hawkmoth.py | 56 +++++++++++++++++---------------------------
 1 file changed, 21 insertions(+), 35 deletions(-)

diff --git a/hawkmoth/hawkmoth.py b/hawkmoth/hawkmoth.py
index 6b45aab..d6075f9 100755
--- a/hawkmoth/hawkmoth.py
+++ b/hawkmoth/hawkmoth.py
@@ -62,7 +62,7 @@ def comment_extract(tu):
         # handle all comments we come across
         if token.kind == TokenKind.COMMENT:
             # if we already have a comment, it wasn't related to a cursor
-            if current_comment:
+            if current_comment and docstr.is_doc(current_comment.spelling):
                 top_level_comments.append(current_comment)
             current_comment = token
             continue
@@ -81,12 +81,12 @@ def comment_extract(tu):
         cursor = token.cursor

         # Note: current_comment may be None
-        if current_comment != None:
+        if current_comment != None and 
docstr.is_doc(current_comment.spelling):
             comments[cursor.hash] = current_comment
         current_comment = None

     # comment at the end of file
-    if current_comment:
+    if current_comment and docstr.is_doc(current_comment.spelling):
         top_level_comments.append(current_comment)

     return top_level_comments, comments
@@ -175,19 +175,15 @@ def _recursive_parse(comments, cursor, nest, **options):
                 # FIXME: Recurse for new structure or union.
                 pass

-            if c.hash not in comments:
-                continue
+            if c.hash in comments:
+                comment = comments[c.hash]
+                text = comment.spelling
+                fmt = docstr.Type.MEMBER
+                name = c.spelling
+                ttype = c.type.spelling

-            comment = comments[c.hash]
-            if not docstr.is_doc(comment.spelling):
-                continue
-            text = comment.spelling
-            fmt = docstr.Type.MEMBER
-            name = c.spelling
-            ttype = c.type.spelling
-
-            result.extend(_result(comment, cursor=cursor, fmt=fmt, nest=nest,
-                                  name=name, ttype=ttype, compat=compat))
+                result.extend(_result(comment, cursor=cursor, fmt=fmt, 
nest=nest,
+                                      name=name, ttype=ttype, compat=compat))

         return result

@@ -202,18 +198,14 @@ def _recursive_parse(comments, cursor, nest, **options):

         nest += 1
         for c in cursor.get_children():
-            if c.hash not in comments:
-                continue
-            comment = comments[c.hash]
-            if not docstr.is_doc(comment.spelling):
-                continue
+            if c.hash in comments:
+                comment = comments[c.hash]
+                text = comment.spelling
+                fmt = docstr.Type.ENUM_VAL
+                name = c.spelling

-            text = comment.spelling
-            fmt = docstr.Type.ENUM_VAL
-            name = c.spelling
-
-            result.extend(_result(comment, cursor=cursor, fmt=fmt,
-                                  nest=nest, name=name, compat=compat))
+                result.extend(_result(comment, cursor=cursor, fmt=fmt,
+                                    nest=nest, name=name, compat=compat))

         return result

@@ -256,18 +248,12 @@ def parse(filename, **options):
     compat = options.get('compat')

     for comment in top_level_comments:
-        if docstr.is_doc(comment.spelling):
-            result.extend(_result(comment, compat=compat))
+        result.extend(_result(comment, compat=compat))

     # Bootstrap the individual parsers.
     for cursor in tu.cursor.get_children():
-        if cursor.hash not in comments:
-            continue
-        comment = comments[cursor.hash]
-        if not docstr.is_doc(comment.spelling):
-            continue
-
-        result.extend(_recursive_parse(comments, cursor, 0, **options))
+        if cursor.hash in comments:
+            result.extend(_recursive_parse(comments, cursor, 0, **options))

     # Sort all elements by order of appearance.
     result.sort(key=lambda r: r[1]['line'])
--
2.20.1

Other related posts: