[hawkmoth] [PATCH 05/11] parser: fix placement of array brackets

  • From: Jani Nikula <jani@xxxxxxxxxx>
  • To: hawkmoth@xxxxxxxxxxxxx
  • Date: Tue, 15 Dec 2020 21:00:02 +0200

From: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>

Clang understandably splits `char test[10]` in a type `char [10]` and a
variable name `test`. However Sphinx expects the C notation in the
`c:var::` directive so we need to fix that. This was not an issue in
Sphinx 2.

Fixes #17.

[Jani: Move test to example-20-variable.{c,rst}]
---
 hawkmoth/parser.py           | 6 ++++++
 test/example-20-variable.c   | 5 +++++
 test/example-20-variable.rst | 5 +++++
 3 files changed, 16 insertions(+)

diff --git a/hawkmoth/parser.py b/hawkmoth/parser.py
index d06321731c8e..83b67c00d4ec 100644
--- a/hawkmoth/parser.py
+++ b/hawkmoth/parser.py
@@ -169,6 +169,12 @@ def _recursive_parse(comments, cursor, nest, compat):
     elif cursor.kind == CursorKind.VAR_DECL:
         fmt = docstr.Type.VAR
 
+        # The dimensions should be applied to the name, not the type.
+        dims = ttype.rsplit(' ', 1)[-1]
+        if dims.startswith('[') and dims.endswith(']'):
+            ttype = ttype.rsplit(' ', 1)[0]
+            name = name + dims
+
         return _result(comment, cursor=cursor, fmt=fmt,
                        nest=nest, name=name, ttype=ttype, compat=compat)
 
diff --git a/test/example-20-variable.c b/test/example-20-variable.c
index b6a7e26f91d1..2aae096e365a 100644
--- a/test/example-20-variable.c
+++ b/test/example-20-variable.c
@@ -9,3 +9,8 @@ const int meaning_of_life = 42;
  * Use :c:func:`frob` to frobnicate, always in :c:macro:`MODE_PRIMARY` mode.
  */
 static struct list *entries;
+
+/**
+ * This is a sized array.
+ */
+const char array[10];
diff --git a/test/example-20-variable.rst b/test/example-20-variable.rst
index 17b7c7bf0a9c..16d1450777cf 100644
--- a/test/example-20-variable.rst
+++ b/test/example-20-variable.rst
@@ -10,3 +10,8 @@
 
    Use :c:func:`frob` to frobnicate, always in :c:macro:`MODE_PRIMARY` mode.
 
+
+.. c:var:: const char array[10]
+
+   This is a sized array.
+
-- 
2.20.1


Other related posts:

  • » [hawkmoth] [PATCH 05/11] parser: fix placement of array brackets - Jani Nikula