Skip to content

Commit 1686516

Browse files
committedJan 10, 2023
Add patch to pull nested anonymous structs out of structs and unions
Newer versionf of NVML have introduced anonymous structs inside of versious top-level structs and unions, and godefs doesn't handle these unfortunately. To compensate for this, we use a coccinelle file to restructure nvml.h and create well-defined structs (with typedefs) instead of anonymous structs. These new typedefs are named such that they can be easily referred to in user-code as desired (e.g. to populate them and then cast the original union type to them). Signed-off-by: Kevin Klues <[email protected]>
1 parent 1079726 commit 1686516

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
 

‎Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ patch-nvml-h: $(PKG_BINDINGS_DIR)/nvml.h
5757
$(PKG_BINDINGS_DIR)/nvml.h: $(GEN_BINDINGS_DIR)/nvml.h | $(PKG_BINDINGS_DIR)
5858
cp $(<) $(@)
5959
$(SED) -i -E 's#(typedef\s+struct)\s+(nvml.*_st\*)\s+(nvml.*_t);#\1\n{\n struct \2 handle;\n} \3;#g' $(@)
60+
spatch --in-place --very-quiet --sp-file $(GEN_BINDINGS_DIR)/anonymous_structs.cocci $(@) > /dev/null
6061

6162
bindings: .create-bindings .strip-autogen-comment .strip-nvml-h-linenumber
6263

‎gen/nvml/anonymous_structs.cocci

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
@patch@
2+
type WRAPPER_TYPE;
3+
field list FIELDS;
4+
identifier V;
5+
expression E;
6+
fresh identifier ST = "nvmlGenerated_struct___";
7+
fresh identifier TEMP_VAR = "nvmlGenerated_variable___" ## V;
8+
@@
9+
10+
++ struct ST {
11+
++ WRAPPER_TYPE TEMP_VAR;
12+
++ FIELDS
13+
++ };
14+
+
15+
16+
WRAPPER_TYPE
17+
{
18+
...
19+
(
20+
- struct {
21+
- FIELDS
22+
- } V[E];
23+
+ struct ST V[E];
24+
25+
|
26+
27+
- struct {
28+
- FIELDS
29+
- } V;
30+
+ struct ST V;
31+
)
32+
...
33+
};
34+
35+
@capture@
36+
type WRAPPER_TYPE;
37+
identifier TEMP_VAR;
38+
identifier ST =~ "^nvmlGenerated_struct___";
39+
@@
40+
41+
struct ST {
42+
WRAPPER_TYPE TEMP_VAR;
43+
...
44+
};
45+
46+
@script:python concat@
47+
WRAPPER_TYPE << capture.WRAPPER_TYPE;
48+
TEMP_VAR << capture.TEMP_VAR;
49+
ST << capture.ST;
50+
T;
51+
@@
52+
53+
def removePrefix(string, prefix):
54+
if string.startswith(prefix):
55+
return string[len(prefix):]
56+
return string
57+
58+
def removeSuffix(string, suffix):
59+
if string.endswith(suffix):
60+
return string[:-len(suffix)]
61+
return string
62+
63+
WRAPPER_TYPE = removeSuffix(WRAPPER_TYPE, "_t")
64+
TEMP_VAR = removePrefix(TEMP_VAR, "nvmlGenerated_variable___")
65+
coccinelle.T = cocci.make_type(WRAPPER_TYPE + TEMP_VAR[0].upper() + TEMP_VAR[1:] + "_t")
66+
67+
@add_typedef@
68+
identifier capture.ST;
69+
type concat.T;
70+
type WRAPPER_TYPE;
71+
identifier TEMP_VAR;
72+
@@
73+
74+
- struct ST {
75+
+ typedef struct {
76+
- WRAPPER_TYPE TEMP_VAR;
77+
...
78+
- };
79+
+ } T;
80+
81+
@update@
82+
identifier capture.ST;
83+
type concat.T;
84+
identifier V;
85+
expression E;
86+
type WRAPPER_TYPE;
87+
@@
88+
89+
WRAPPER_TYPE
90+
{
91+
...
92+
(
93+
- struct ST V[E];
94+
+ T V[E];
95+
|
96+
- struct ST V;
97+
+ T V;
98+
)
99+
...
100+
};

0 commit comments

Comments
 (0)
Please sign in to comment.