[hawkmoth] Re: [PATCH v2 09/15] hawkmoth: filter non documentation comments early on

  • From: Jani Nikula <jani@xxxxxxxxxx>
  • To: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>, hawkmoth@xxxxxxxxxxxxx
  • Date: Mon, 21 Jan 2019 23:18:00 +0200

On Sat, 19 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.

LGTM.

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

diff --git a/hawkmoth/hawkmoth.py b/hawkmoth/hawkmoth.py
index 5b8e79a..779cee6 100755
--- a/hawkmoth/hawkmoth.py
+++ b/hawkmoth/hawkmoth.py
@@ -61,7 +61,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
@@ -80,12 +80,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
@@ -173,19 +173,15 @@ def _recursive_parse(comments, cursor, nest, compat):
                 # 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
 
@@ -199,18 +195,14 @@ def _recursive_parse(comments, cursor, nest, compat):
 
         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
 
@@ -273,18 +265,12 @@ def parse(filename, **options):
     compat = lambda x: doccompat.convert(x, 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, compat))
+        if cursor.hash in comments:
+            result.extend(_recursive_parse(comments, cursor, 0, compat))
 
     # Sort all elements by order of appearance.
     result.sort(key=lambda r: r[1]['line'])
-- 
2.20.1

Other related posts: