- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathc_annotations.py
291 lines (252 loc) · 9.39 KB
/
c_annotations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Support annotations for C API elements.
* Reference count annotations for C API functions.
* Stable ABI annotations
* Limited API annotations
Configuration:
* Set ``refcount_file`` to the path to the reference count data file.
* Set ``stable_abi_file`` to the path to stable ABI list.
"""
from __future__ importannotations
importcsv
importdataclasses
frompathlibimportPath
fromtypingimportTYPE_CHECKING
fromdocutilsimportnodes
fromdocutils.statemachineimportStringList
fromsphinximportaddnodes
fromsphinx.localeimport_assphinx_gettext
fromsphinx.util.docutilsimportSphinxDirective
ifTYPE_CHECKING:
fromsphinx.applicationimportSphinx
fromsphinx.util.typingimportExtensionMetadata
ROLE_TO_OBJECT_TYPE= {
"func": "function",
"macro": "macro",
"member": "member",
"type": "type",
"data": "var",
}
@dataclasses.dataclass(slots=True)
classRefCountEntry:
# Name of the function.
name: str
# List of (argument name, type, refcount effect) tuples.
# (Currently not used. If it was, a dataclass might work better.)
args: list=dataclasses.field(default_factory=list)
# Return type of the function.
result_type: str=""
# Reference count effect for the return value.
result_refs: int|None=None
@dataclasses.dataclass(frozen=True, slots=True)
classStableABIEntry:
# Role of the object.
# Source: Each [item_kind] in stable_abi.toml is mapped to a C Domain role.
role: str
# Name of the object.
# Source: [<item_kind>.*] in stable_abi.toml.
name: str
# Version when the object was added to the stable ABI.
# (Source: [<item_kind>.*.added] in stable_abi.toml.
added: str
# An explananatory blurb for the ifdef.
# Source: ``feature_macro.*.doc`` in stable_abi.toml.
ifdef_note: str
# Defines how much of the struct is exposed. Only relevant for structs.
# Source: [<item_kind>.*.struct_abi_kind] in stable_abi.toml.
struct_abi_kind: str
defread_refcount_data(refcount_filename: Path) ->dict[str, RefCountEntry]:
refcount_data= {}
refcounts=refcount_filename.read_text(encoding="utf8")
forlineinrefcounts.splitlines():
line=line.strip()
ifnotlineorline.startswith("#"):
# blank lines and comments
continue
# Each line is of the form
# function ':' type ':' [param name] ':' [refcount effect] ':' [comment]
parts=line.split(":", 4)
iflen(parts) !=5:
raiseValueError(f"Wrong field count in {line!r}")
function, type, arg, refcount, _comment=parts
# Get the entry, creating it if needed:
try:
entry=refcount_data[function]
exceptKeyError:
entry=refcount_data[function] =RefCountEntry(function)
ifnotrefcountorrefcount=="null":
refcount=None
else:
refcount=int(refcount)
# Update the entry with the new parameter
# or the result information.
ifarg:
entry.args.append((arg, type, refcount))
else:
entry.result_type=type
entry.result_refs=refcount
returnrefcount_data
defread_stable_abi_data(stable_abi_file: Path) ->dict[str, StableABIEntry]:
stable_abi_data= {}
withopen(stable_abi_file, encoding="utf8") asfp:
forrecordincsv.DictReader(fp):
name=record["name"]
stable_abi_data[name] =StableABIEntry(**record)
returnstable_abi_data
defadd_annotations(app: Sphinx, doctree: nodes.document) ->None:
state=app.env.domaindata["c_annotations"]
refcount_data=state["refcount_data"]
stable_abi_data=state["stable_abi_data"]
fornodeindoctree.findall(addnodes.desc_content):
par=node.parent
ifpar["domain"] !="c":
continue
ifnotpar[0].get("ids", None):
continue
name=par[0]["ids"][0].removeprefix("c.")
objtype=par["objtype"]
# Stable ABI annotation.
ifrecord:=stable_abi_data.get(name):
ifROLE_TO_OBJECT_TYPE[record.role] !=objtype:
msg= (
f"Object type mismatch in limited API annotation for {name}: "
f"{ROLE_TO_OBJECT_TYPE[record.role]!r} != {objtype!r}"
)
raiseValueError(msg)
annotation=_stable_abi_annotation(record)
node.insert(0, annotation)
# Unstable API annotation.
ifname.startswith("PyUnstable"):
annotation=_unstable_api_annotation()
node.insert(0, annotation)
# Return value annotation
ifobjtype!="function":
continue
ifnamenotinrefcount_data:
continue
entry=refcount_data[name]
ifnotentry.result_type.endswith("Object*"):
continue
annotation=_return_value_annotation(entry.result_refs)
node.insert(0, annotation)
def_stable_abi_annotation(record: StableABIEntry) ->nodes.emphasis:
"""Create the Stable ABI annotation.
These have two forms:
Part of the `Stable ABI <link>`_.
Part of the `Stable ABI <link>`_ since version X.Y.
For structs, there's some more info in the message:
Part of the `Limited API <link>`_ (as an opaque struct).
Part of the `Stable ABI <link>`_ (including all members).
Part of the `Limited API <link>`_ (Only some members are part
of the stable ABI.).
... all of which can have "since version X.Y" appended.
"""
stable_added=record.added
message=sphinx_gettext("Part of the")
message=message.center(len(message) +2)
emph_node=nodes.emphasis(message, message, classes=["stableabi"])
ref_node=addnodes.pending_xref(
"Stable ABI",
refdomain="std",
reftarget="stable",
reftype="ref",
refexplicit="False",
)
struct_abi_kind=record.struct_abi_kind
ifstruct_abi_kindin {"opaque", "members"}:
ref_node+=nodes.Text(sphinx_gettext("Limited API"))
else:
ref_node+=nodes.Text(sphinx_gettext("Stable ABI"))
emph_node+=ref_node
ifstruct_abi_kind=="opaque":
emph_node+=nodes.Text(" "+sphinx_gettext("(as an opaque struct)"))
elifstruct_abi_kind=="full-abi":
emph_node+=nodes.Text(
" "+sphinx_gettext("(including all members)")
)
ifrecord.ifdef_note:
emph_node+=nodes.Text(f" {record.ifdef_note}")
ifstable_added=="3.2":
# Stable ABI was introduced in 3.2.
pass
else:
emph_node+=nodes.Text(
" "+sphinx_gettext("since version %s") %stable_added
)
emph_node+=nodes.Text(".")
ifstruct_abi_kind=="members":
msg=" "+sphinx_gettext(
"(Only some members are part of the stable ABI.)"
)
emph_node+=nodes.Text(msg)
returnemph_node
def_unstable_api_annotation() ->nodes.admonition:
ref_node=addnodes.pending_xref(
"Unstable API",
nodes.Text(sphinx_gettext("Unstable API")),
refdomain="std",
reftarget="unstable-c-api",
reftype="ref",
refexplicit="False",
)
emph_node=nodes.emphasis(
"This is ",
sphinx_gettext("This is") +" ",
ref_node,
nodes.Text(
sphinx_gettext(
". It may change without warning in minor releases."
)
),
)
returnnodes.admonition(
"",
emph_node,
classes=["unstable-c-api", "warning"],
)
def_return_value_annotation(result_refs: int|None) ->nodes.emphasis:
classes= ["refcount"]
ifresult_refsisNone:
rc=sphinx_gettext("Return value: Always NULL.")
classes.append("return_null")
elifresult_refs:
rc=sphinx_gettext("Return value: New reference.")
classes.append("return_new_ref")
else:
rc=sphinx_gettext("Return value: Borrowed reference.")
classes.append("return_borrowed_ref")
returnnodes.emphasis(rc, rc, classes=classes)
classLimitedAPIList(SphinxDirective):
has_content=False
required_arguments=0
optional_arguments=0
final_argument_whitespace=True
defrun(self) ->list[nodes.Node]:
state=self.env.domaindata["c_annotations"]
content= [
f"* :c:{record.role}:`{record.name}`"
forrecordinstate["stable_abi_data"].values()
]
node=nodes.paragraph()
self.state.nested_parse(StringList(content), 0, node)
return [node]
definit_annotations(app: Sphinx) ->None:
# Using domaindata is a bit hack-ish,
# but allows storing state without a global variable or closure.
app.env.domaindata["c_annotations"] =state= {}
state["refcount_data"] =read_refcount_data(
Path(app.srcdir, app.config.refcount_file)
)
state["stable_abi_data"] =read_stable_abi_data(
Path(app.srcdir, app.config.stable_abi_file)
)
defsetup(app: Sphinx) ->ExtensionMetadata:
app.add_config_value("refcount_file", "", "env", types={str})
app.add_config_value("stable_abi_file", "", "env", types={str})
app.add_directive("limited-api-list", LimitedAPIList)
app.connect("builder-inited", init_annotations)
app.connect("doctree-read", add_annotations)
return {
"version": "1.0",
"parallel_read_safe": True,
"parallel_write_safe": True,
}