[rtems-central commit] interface: Improve compound substitution

Sebastian Huber sebh at rtems.org
Thu Jul 7 08:02:37 UTC 2022


Module:    rtems-central
Branch:    master
Commit:    43580822b7fd0261cd447985a763fcc693b0604e
Changeset: http://git.rtems.org/rtems-central/commit/?id=43580822b7fd0261cd447985a763fcc693b0604e

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed Jul  6 11:08:55 2022 +0200

interface: Improve compound substitution

---

 rtemsspec/content.py                     | 18 ++++++++++++++++++
 rtemsspec/interface.py                   | 27 ++++++++++-----------------
 rtemsspec/interfacedoc.py                | 16 +++++-----------
 rtemsspec/tests/spec-interface/func2.yml |  4 ++--
 rtemsspec/tests/spec-interface/u.yml     | 32 ++++++++++++++++++++++++++++++++
 rtemsspec/tests/test_interface.py        | 29 +++++++++++++++++++++++------
 rtemsspec/tests/test_interfacedoc.py     |  2 +-
 7 files changed, 91 insertions(+), 37 deletions(-)

diff --git a/rtemsspec/content.py b/rtemsspec/content.py
index 1893cfe8..24dafc64 100644
--- a/rtemsspec/content.py
+++ b/rtemsspec/content.py
@@ -1052,6 +1052,24 @@ def get_value_plural(ctx: ItemGetValueContext) -> Any:
         return f"{term}s"
 
 
+def forward_declaration(item: Item) -> str:
+    """ Gets the forward declare for the item. """
+    target = item.parent("interface-target")
+    return f"{target['interface-type']} {target['name']}"
+
+
+def get_value_forward_declaration(ctx: ItemGetValueContext) -> Any:
+    """ Gets a value as a forward declaration. """
+    return forward_declaration(ctx.item)
+
+
+def get_value_compound(ctx: ItemGetValueContext) -> Any:
+    """ Gets a value as a compound (struct or union). """
+    if ctx.item["definition-kind"] in ["struct-only", "union-only"]:
+        return f"{ctx.item['interface-type']} {ctx.item['name']}"
+    return ctx.item['name']
+
+
 class ExpressionMapper:
     """ Maps symbols and operations to form a C expression. """
 
diff --git a/rtemsspec/interface.py b/rtemsspec/interface.py
index 9df5576d..0e49fba3 100644
--- a/rtemsspec/interface.py
+++ b/rtemsspec/interface.py
@@ -33,11 +33,11 @@ from typing import Any, Callable, Dict, Iterator, List, NamedTuple, Optional, \
     Union, Set, Tuple
 
 from rtemsspec.content import CContent, CInclude, enabled_by_to_exp, \
-    ExpressionMapper, get_value_double_colon, get_value_doxygen_function, \
-    get_value_doxygen_group, get_value_hash, get_value_params, \
-    get_value_plural, to_camel_case
-from rtemsspec.items import Item, ItemCache, ItemGetValueContext, \
-    ItemGetValueMap, ItemMapper
+    ExpressionMapper, forward_declaration, get_value_compound, \
+    get_value_double_colon, get_value_doxygen_function, \
+    get_value_doxygen_group, get_value_forward_declaration, get_value_hash, \
+    get_value_params, get_value_plural, to_camel_case
+from rtemsspec.items import Item, ItemCache, ItemGetValueMap, ItemMapper
 
 ItemMap = Dict[str, Item]
 Lines = Union[str, List[str]]
@@ -55,25 +55,18 @@ def _get_group_identifiers(groups: ItemMap) -> List[str]:
     return [item["identifier"] for item in groups.values()]
 
 
-def _forward_declaration(item: Item) -> str:
-    target = item.parent("interface-target")
-    return f"{target['interface-type']} {target['name']}"
-
-
-def _get_value_forward_declaration(ctx: ItemGetValueContext) -> Any:
-    return _forward_declaration(ctx.item)
-
-
 class _InterfaceMapper(ItemMapper):
     def __init__(self, node: "Node"):
         super().__init__(node.item)
         self._node = node
         self._code_or_doc = "doc"
+        self.add_get_value("interface/struct/code:/name", get_value_compound)
+        self.add_get_value("interface/union/code:/name", get_value_compound)
         self.add_get_value("glossary/term/doc:/plural", get_value_plural)
         self.add_get_value("interface/forward-declaration/code:/name",
-                           _get_value_forward_declaration)
+                           get_value_forward_declaration)
         self.add_get_value("interface/forward-declaration/doc:/name",
-                           _get_value_forward_declaration)
+                           get_value_forward_declaration)
         self.add_get_value("interface/function/doc:/name",
                            get_value_doxygen_function)
         self.add_get_value("interface/function/doc:/params/name",
@@ -320,7 +313,7 @@ class Node:
         """ Generates a forward declaration. """
         self.content.append([
             "", "/* Forward declaration */",
-            _forward_declaration(self.item) + ";"
+            forward_declaration(self.item) + ";"
         ])
 
     def generate_function(self) -> None:
diff --git a/rtemsspec/interfacedoc.py b/rtemsspec/interfacedoc.py
index f902a075..65bc5443 100644
--- a/rtemsspec/interfacedoc.py
+++ b/rtemsspec/interfacedoc.py
@@ -30,7 +30,8 @@ import functools
 import os
 from typing import Any, Dict, List, Tuple
 
-from rtemsspec.content import CContent
+from rtemsspec.content import CContent, get_value_compound, \
+     get_value_forward_declaration
 from rtemsspec.sphinxcontent import get_label, get_reference, SphinxContent, \
      SphinxInterfaceMapper
 from rtemsspec.items import Item, ItemCache, ItemGetValueContext, ItemMapper
@@ -44,24 +45,17 @@ def _sanitize_name(name: str) -> str:
     return name.lstrip("_")
 
 
-def _forward_declaration(item: Item) -> str:
-    target = item.parent("interface-target")
-    return f"{target['interface-type']} {target['name']}"
-
-
 def _get_reference(name: str) -> str:
     return get_reference(get_label(f"{INTERFACE} {name}"))
 
 
-def _get_value_forward_declaration(ctx: ItemGetValueContext) -> Any:
-    return _forward_declaration(ctx.item)
-
-
 class _CodeMapper(ItemMapper):
     def __init__(self, item: Item):
         super().__init__(item)
         self.add_get_value("interface/forward-declaration:/name",
-                           _get_value_forward_declaration)
+                           get_value_forward_declaration)
+        self.add_get_value("interface/struct:/name", get_value_compound)
+        self.add_get_value("interface/union:/name", get_value_compound)
 
 
 def _get_param(ctx: ItemGetValueContext) -> Any:
diff --git a/rtemsspec/tests/spec-interface/func2.yml b/rtemsspec/tests/spec-interface/func2.yml
index e66e7e9a..f77ec49f 100644
--- a/rtemsspec/tests/spec-interface/func2.yml
+++ b/rtemsspec/tests/spec-interface/func2.yml
@@ -13,8 +13,8 @@ definition:
     params:
     - int ${.:/params[0]/name}
     - const ${forward-decl:/name} *${.:/params[1]/name}
-    - ${forward-decl:/name} *( *${.:/params[2]/name} )( void )
-    - ${forward-decl:/name} *${.:/params[3]/name}
+    - ${u:/name} *( *${.:/params[2]/name} )( void )
+    - ${s:/name} *${.:/params[3]/name}
     return: int
   variants: []
 description: |
diff --git a/rtemsspec/tests/spec-interface/u.yml b/rtemsspec/tests/spec-interface/u.yml
new file mode 100644
index 00000000..d6e7d8ce
--- /dev/null
+++ b/rtemsspec/tests/spec-interface/u.yml
@@ -0,0 +1,32 @@
+SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
+brief: null
+copyrights:
+- Copyright (C) 2022 embedded brains GmbH (http://www.embedded-brains.de)
+definition:
+- default:
+    brief: Brief member 0 description.
+    definition: int ${.:name}
+    description: null
+    kind: member
+    name: m_0
+  variants: []
+- default:
+    brief: Brief member 1 description.
+    definition: long ${.:name}
+    description: null
+    kind: member
+    name: m_1
+  variants: []
+definition-kind: typedef-and-union
+description: null
+enabled-by: true
+index-entries: []
+interface-type: union
+links:
+- role: interface-placement
+  uid: h
+- role: interface-ingroup
+  uid: gc
+name: Union
+notes: null
+type: interface
diff --git a/rtemsspec/tests/test_interface.py b/rtemsspec/tests/test_interface.py
index f1d4d70d..037dbee9 100644
--- a/rtemsspec/tests/test_interface.py
+++ b/rtemsspec/tests/test_interface.py
@@ -155,11 +155,6 @@ extern "C" {
     ((float_t) 123)
 #endif
 
-/* Generated from spec:/forward-decl */
-
-/* Forward declaration */
-struct Struct;
-
 /* Generated from spec:/enum */
 
 /**
@@ -200,6 +195,11 @@ typedef enum EnumB {
   ENUMERATOR_B = ENUMERATOR_A
 } EnumB;
 
+/* Generated from spec:/forward-decl */
+
+/* Forward declaration */
+struct Struct;
+
 /* Generated from spec:/func */
 
 /**
@@ -594,6 +594,23 @@ typedef uint32_t Integer /* Some comment. */;
   typedef uint32_t Integer3;
 #endif
 
+/* Generated from spec:/u */
+
+/**
+ * @ingroup GroupC
+ */
+typedef union Union {
+  /**
+   * @brief Brief member 0 description.
+   */
+  int m_0;
+
+  /**
+   * @brief Brief member 1 description.
+   */
+  long m_1;
+} Union;
+
 #if !defined(ASM)
   /* Generated from spec:/var */
 
@@ -639,7 +656,7 @@ typedef uint32_t Integer /* Some comment. */;
 __attribute__((__const__)) static inline int VeryLongFunction(
   int                  VeryLongParam0,
   const struct Struct *VeryLongParam1,
-  struct Struct    *( *VeryLongParam2 )( void ),
+  Union            *( *VeryLongParam2 )( void ),
   struct Struct       *VeryLongParam3
 )
 {
diff --git a/rtemsspec/tests/test_interfacedoc.py b/rtemsspec/tests/test_interfacedoc.py
index b57cf310..c82b923d 100644
--- a/rtemsspec/tests/test_interfacedoc.py
+++ b/rtemsspec/tests/test_interfacedoc.py
@@ -184,7 +184,7 @@ Very long function brief description.
     int VeryLongFunction(
       int                  VeryLongParam0,
       const struct Struct *VeryLongParam1,
-      struct Struct    *( *VeryLongParam2 )( void ),
+      Union            *( *VeryLongParam2 )( void ),
       struct Struct       *VeryLongParam3
     );
 



More information about the vc mailing list