Repository navigation
Expand file tree
/
Copy pathnullable.py
More file actions
127 lines (110 loc) · 5.8 KB
/
Copy pathnullable.py
File metadata and controls
127 lines (110 loc) · 5.8 KB
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
"""Extract per-parameter nullability from the MEOS C Doxygen as the SoT.
A MEOS function parameter accepts NULL iff its Doxygen ``@param`` entry carries
the ``@nullable`` tag, e.g. ``@param[in] newjb New value @nullable``. The tag sits
inside the entry of the parameter it qualifies, so renaming the parameter edits the
line that carries both; on an output it states that a caller may pass NULL to skip
it. A description saying ``NULL`` in prose states nothing here: it describes a
value written or another argument. This is the single source of truth the codegens consume — grounded in
the C code, keyed by parameter name, and cross-checked in MobilityDB against the PG
layer (a SQL function declared without ``STRICT`` + the wrapper's ``PG_ARGISNULL``
guards).
The extractor walks the MEOS sources, pairs each Doxygen block with the function
it documents, and records the params whose description carries the tag. The result feeds ``shape.nullable`` in the IDL so every binding can guard the
param.
"""
from __future__ import annotations
import re
from pathlib import Path
# The Doxygen block of a function and the files that carry it are the ones every
# @param reader shares (#extract_outparams), so a block separated from its function
# by an `#if MEOS` guard in pgtypes/ is paired with it here too.
from parser.outparam import _FUNC, doxygen_sources
# One @param entry: capture the direction, the (possibly comma-separated) names and
# the description, which runs to the next line opening a Doxygen command.
_PARAM = re.compile(
r'@param\[(?P<dir>[^\]]*)\]\s+(?P<names>\w+(?:\s*,\s*\w+)*)\s+(?P<desc>.*?)'
r'(?=\n\s*\*\s*@|\*/|\Z)', re.S)
# The @nullable tag inside a @param description, of an input or of an output a
# caller may pass as NULL to skip it. A line opening with @nullable ends the
# description before it, so the tag is read only where it qualifies one entry.
_TAG = re.compile(r'(?<![\w@])@nullable\b')
# The @freed and @borrowed tags of a @param entry, read by #attach_ownership of
# parser/ownership.py and removed, as @nullable is, from the description a binding shows.
_FREED_TAG = re.compile(r'(?<![\w@])@(?:freed|borrowed)\b')
def extract_nullable(meos_root: str | Path) -> dict[str, list[str]]:
"""Return ``{function: [nullable params]}`` from the MEOS C sources
(#doxygen_sources)."""
out: dict[str, list[str]] = {}
for f in doxygen_sources(meos_root):
txt = Path(f).read_text(errors="ignore")
for m in _FUNC.finditer(txt):
name = m.group("name")
for pm in _PARAM.finditer(m.group("doc")):
if not _TAG.search(pm.group("desc")):
continue
for p in (n.strip() for n in pm.group("names").split(",")):
out.setdefault(name, [])
if p and p not in out[name]:
out[name].append(p)
return out
def extract_param_docs(meos_root: str | Path) -> dict[str, dict[str, str]]:
"""Return ``{function: {parameter: description}}`` from the MEOS C Doxygen, the
``@param`` text #extract_nullable reads, whitespace collapsed and the @nullable,
@freed and @borrowed tags removed, over the same files."""
out: dict[str, dict[str, str]] = {}
for f in doxygen_sources(meos_root):
txt = Path(f).read_text(errors="ignore")
for m in _FUNC.finditer(txt):
docs = out.setdefault(m.group("name"), {})
for pm in _PARAM.finditer(m.group("doc")):
desc = " ".join(_FREED_TAG.sub("", _TAG.sub("", pm.group("desc"))).split())
for p in (n.strip() for n in pm.group("names").split(",")):
if p:
docs.setdefault(p, desc)
return out
def merge_nullable(idl: dict, meos_root: str | Path) -> tuple[dict, int]:
"""Fold the extracted nullability into each function's ``shape.nullable``."""
nul = extract_nullable(meos_root)
n = 0
for func in idl["functions"]:
params = nul.get(func["name"])
if not params:
continue
present = {p["name"] for p in func.get("params", [])}
keep = [p for p in params if p in present]
if keep:
func.setdefault("shape", {})["nullable"] = keep
n += len(keep)
return idl, n
# The @brief of a Doxygen block: its text runs to the next line opening a command, to a
# blank comment line, or to the end of the block, as #_PARAM reads a @param entry.
_BRIEF = re.compile(r'@brief\s+(?P<text>.*?)(?=\n\s*\*\s*@|\n\s*\*\s*\n|\*/|\Z)', re.S)
def extract_briefs(meos_root: str | Path) -> dict[str, str]:
"""Return ``{function: brief}`` from the MEOS C Doxygen, the ``@brief`` of the block
#extract_param_docs reads, whitespace collapsed and the leading ``*`` of each line
removed, over the same files; a function documented twice keeps its first brief."""
out: dict[str, str] = {}
for f in doxygen_sources(meos_root):
txt = Path(f).read_text(errors="ignore")
for m in _FUNC.finditer(txt):
bm = _BRIEF.search(m.group("doc"))
if not bm:
continue
text = " ".join(ln.strip().lstrip("*").strip()
for ln in bm.group("text").splitlines())
text = " ".join(text.split())
if text:
out.setdefault(m.group("name"), text)
return out
def attach_function_docs(idl: dict, meos_root: str | Path) -> tuple[dict, int]:
"""Give each catalog function the ``doc`` its Doxygen ``@brief`` states
(#extract_briefs), as #merge_nullable folds its ``@nullable`` tags; a function the
meta already documents keeps that ``doc``."""
briefs = extract_briefs(meos_root)
n = 0
for func in idl["functions"]:
brief = briefs.get(func["name"])
if brief and not func.get("doc"):
func["doc"] = brief
n += 1
return idl, n