Skip to content

Commit 5a3e119

Browse files
Merge pull request #9547 from jakobandersen/c_gnu_type_spec
C, update fundamental types, including GNU extensions
2 parents 8fd4373 + 4b62b6c commit 5a3e119

File tree

5 files changed

+162
-87
lines changed

5 files changed

+162
-87
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Features added
1919
template variable ``sphinx_version_tuple``
2020
* #9445: py domain: ``:py:property:`` directive supports ``:classmethod:``
2121
option to describe the class property
22+
* #9535: C and C++, support more fundamental types, including GNU extensions.
2223

2324
Bugs fixed
2425
----------

sphinx/domains/c.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,34 @@
9292
_string_re = re.compile(r"[LuU8]?('([^'\\]*(?:\\.[^'\\]*)*)'"
9393
r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S)
9494

95+
_simple_type_sepcifiers_re = re.compile(r"""(?x)
96+
\b(
97+
void|_Bool|bool
98+
# Integer
99+
# -------
100+
|((signed|unsigned)\s+)?(char|(
101+
((long\s+long|long|short)\s+)?int
102+
))
103+
|__uint128|__int128
104+
# extensions
105+
|((signed|unsigned)\s+)?__int(8|16|32|64|128)
106+
# Floating-point
107+
# --------------
108+
|(float|double|long\s+double)(\s+(_Complex|complex|_Imaginary|imaginary))?
109+
|(_Complex|complex|_Imaginary|imaginary)\s+(float|double|long\s+double)
110+
|_Decimal(32|64|128)
111+
# extensions
112+
|__float80|_Float64x|__float128|_Float128|__ibm128
113+
|__fp16
114+
# Fixed-point, extension
115+
|(_Sat\s+)?((signed|unsigned)\s+)?((short|long|long\s+long)\s+)?(_Fract|fract|_Accum|accum)
116+
# Integer types that could be prefixes of the previous ones
117+
# ---------------------------------------------------------
118+
|((signed|unsigned)\s+)?(long\s+long|long|short)
119+
|signed|unsigned
120+
)\b
121+
""")
122+
95123

96124
class _DuplicateSymbolError(Exception):
97125
def __init__(self, symbol: "Symbol", declaration: "ASTDeclaration") -> None:
@@ -609,14 +637,20 @@ class ASTTrailingTypeSpec(ASTBase):
609637

610638
class ASTTrailingTypeSpecFundamental(ASTTrailingTypeSpec):
611639
def __init__(self, name: str) -> None:
612-
self.name = name
640+
self.names = name.split()
613641

614642
def _stringify(self, transform: StringifyTransform) -> str:
615-
return self.name
643+
return ' '.join(self.names)
616644

617645
def describe_signature(self, signode: TextElement, mode: str,
618646
env: "BuildEnvironment", symbol: "Symbol") -> None:
619-
signode += addnodes.desc_sig_keyword_type(self.name, self.name)
647+
first = True
648+
for n in self.names:
649+
if not first:
650+
signode += addnodes.desc_sig_space()
651+
else:
652+
first = False
653+
signode += addnodes.desc_sig_keyword_type(n, n)
620654

621655

622656
class ASTTrailingTypeSpecName(ASTTrailingTypeSpec):
@@ -2123,15 +2157,6 @@ def dump(self, indent: int) -> str:
21232157

21242158

21252159
class DefinitionParser(BaseParser):
2126-
# those without signedness and size modifiers
2127-
# see https://en.cppreference.com/w/cpp/language/types
2128-
_simple_fundamental_types = (
2129-
'void', '_Bool', 'bool', 'char', 'int', 'float', 'double',
2130-
'__int64',
2131-
)
2132-
2133-
_prefix_keys = ('struct', 'enum', 'union')
2134-
21352160
@property
21362161
def language(self) -> str:
21372162
return 'C'
@@ -2556,40 +2581,16 @@ def _parse_nested_name(self) -> ASTNestedName:
25562581
return ASTNestedName(names, rooted)
25572582

25582583
def _parse_trailing_type_spec(self) -> ASTTrailingTypeSpec:
2559-
# fundamental types
2584+
# fundamental types, https://en.cppreference.com/w/c/language/type
2585+
# and extensions
25602586
self.skip_ws()
2561-
for t in self._simple_fundamental_types:
2562-
if self.skip_word(t):
2563-
return ASTTrailingTypeSpecFundamental(t)
2564-
2565-
# TODO: this could/should be more strict
2566-
elements = []
2567-
if self.skip_word_and_ws('signed'):
2568-
elements.append('signed')
2569-
elif self.skip_word_and_ws('unsigned'):
2570-
elements.append('unsigned')
2571-
while 1:
2572-
if self.skip_word_and_ws('short'):
2573-
elements.append('short')
2574-
elif self.skip_word_and_ws('long'):
2575-
elements.append('long')
2576-
else:
2577-
break
2578-
if self.skip_word_and_ws('char'):
2579-
elements.append('char')
2580-
elif self.skip_word_and_ws('int'):
2581-
elements.append('int')
2582-
elif self.skip_word_and_ws('double'):
2583-
elements.append('double')
2584-
elif self.skip_word_and_ws('__int64'):
2585-
elements.append('__int64')
2586-
if len(elements) > 0:
2587-
return ASTTrailingTypeSpecFundamental(' '.join(elements))
2587+
if self.match(_simple_type_sepcifiers_re):
2588+
return ASTTrailingTypeSpecFundamental(self.matched_text)
25882589

25892590
# prefixed
25902591
prefix = None
25912592
self.skip_ws()
2592-
for k in self._prefix_keys:
2593+
for k in ('struct', 'enum', 'union'):
25932594
if self.skip_word_and_ws(k):
25942595
prefix = k
25952596
break

sphinx/domains/cpp.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,31 @@
334334
'while', 'xor', 'xor_eq'
335335
]
336336

337+
338+
_simple_type_sepcifiers_re = re.compile(r"""(?x)
339+
\b(
340+
auto|void|bool
341+
# Integer
342+
# -------
343+
|((signed|unsigned)\s+)?(char|__int128|(
344+
((long\s+long|long|short)\s+)?int
345+
))
346+
|wchar_t|char(8|16|32)_t
347+
# extensions
348+
|((signed|unsigned)\s+)?__int(64|128)
349+
# Floating-point
350+
# --------------
351+
|(float|double|long\s+double)(\s+(_Complex|_Imaginary))?
352+
|(_Complex|_Imaginary)\s+(float|double|long\s+double)
353+
# extensions
354+
|__float80|_Float64x|__float128|_Float128
355+
# Integer types that could be prefixes of the previous ones
356+
# ---------------------------------------------------------
357+
|((signed|unsigned)\s+)?(long\s+long|long|short)
358+
|signed|unsigned
359+
)\b
360+
""")
361+
337362
_max_id = 4
338363
_id_prefix = [None, '', '_CPPv2', '_CPPv3', '_CPPv4']
339364
# Ids are used in lookup keys which are used across pickled files,
@@ -449,11 +474,23 @@
449474
'long long int': 'x',
450475
'signed long long': 'x',
451476
'signed long long int': 'x',
477+
'__int64': 'x',
452478
'unsigned long long': 'y',
453479
'unsigned long long int': 'y',
480+
'__int128': 'n',
481+
'signed __int128': 'n',
482+
'unsigned __int128': 'o',
454483
'float': 'f',
455484
'double': 'd',
456485
'long double': 'e',
486+
'__float80': 'e', '_Float64x': 'e',
487+
'__float128': 'g', '_Float128': 'g',
488+
'float _Complex': 'Cf', '_Complex float': 'Cf',
489+
'double _Complex': 'Cd', '_Complex double': 'Cd',
490+
'long double _Complex': 'Ce', '_Complex long double': 'Ce',
491+
'float _Imaginary': 'f', '_Imaginary float': 'f',
492+
'double _Imaginary': 'd', '_Imaginary double': 'd',
493+
'long double _Imaginary': 'e', '_Imaginary long double': 'e',
457494
'auto': 'Da',
458495
'decltype(auto)': 'Dc',
459496
'std::nullptr_t': 'Dn'
@@ -1817,31 +1854,38 @@ def describe_signature(self, signode: TextElement, mode: str,
18171854

18181855
class ASTTrailingTypeSpecFundamental(ASTTrailingTypeSpec):
18191856
def __init__(self, name: str) -> None:
1820-
self.name = name
1857+
self.names = name.split()
18211858

18221859
def _stringify(self, transform: StringifyTransform) -> str:
1823-
return self.name
1860+
return ' '.join(self.names)
18241861

18251862
def get_id(self, version: int) -> str:
18261863
if version == 1:
18271864
res = []
1828-
for a in self.name.split(' '):
1865+
for a in self.names:
18291866
if a in _id_fundamental_v1:
18301867
res.append(_id_fundamental_v1[a])
18311868
else:
18321869
res.append(a)
18331870
return '-'.join(res)
18341871

1835-
if self.name not in _id_fundamental_v2:
1872+
txt = str(self)
1873+
if txt not in _id_fundamental_v2:
18361874
raise Exception(
18371875
'Semi-internal error: Fundamental type "%s" can not be mapped '
1838-
'to an id. Is it a true fundamental type? If not so, the '
1839-
'parser should have rejected it.' % self.name)
1840-
return _id_fundamental_v2[self.name]
1876+
'to an ID. Is it a true fundamental type? If not so, the '
1877+
'parser should have rejected it.' % txt)
1878+
return _id_fundamental_v2[txt]
18411879

18421880
def describe_signature(self, signode: TextElement, mode: str,
18431881
env: "BuildEnvironment", symbol: "Symbol") -> None:
1844-
signode += addnodes.desc_sig_keyword_type(self.name, self.name)
1882+
first = True
1883+
for n in self.names:
1884+
if not first:
1885+
signode += addnodes.desc_sig_space()
1886+
else:
1887+
first = False
1888+
signode += addnodes.desc_sig_keyword_type(n, n)
18451889

18461890

18471891
class ASTTrailingTypeSpecDecltypeAuto(ASTTrailingTypeSpec):
@@ -4996,15 +5040,6 @@ def dump(self, indent: int) -> str:
49965040

49975041

49985042
class DefinitionParser(BaseParser):
4999-
# those without signedness and size modifiers
5000-
# see https://en.cppreference.com/w/cpp/language/types
5001-
_simple_fundemental_types = (
5002-
'void', 'bool', 'char', 'wchar_t', 'char8_t', 'char16_t', 'char32_t',
5003-
'int', 'float', 'double', 'auto'
5004-
)
5005-
5006-
_prefix_keys = ('class', 'struct', 'enum', 'union', 'typename')
5007-
50085043
@property
50095044
def language(self) -> str:
50105045
return 'C++'
@@ -5821,33 +5856,11 @@ def _parse_nested_name(self, memberPointer: bool = False) -> ASTNestedName:
58215856
# ==========================================================================
58225857

58235858
def _parse_trailing_type_spec(self) -> ASTTrailingTypeSpec:
5824-
# fundemental types
5859+
# fundamental types, https://en.cppreference.com/w/cpp/language/type
5860+
# and extensions
58255861
self.skip_ws()
5826-
for t in self._simple_fundemental_types:
5827-
if self.skip_word(t):
5828-
return ASTTrailingTypeSpecFundamental(t)
5829-
5830-
# TODO: this could/should be more strict
5831-
elements = []
5832-
if self.skip_word_and_ws('signed'):
5833-
elements.append('signed')
5834-
elif self.skip_word_and_ws('unsigned'):
5835-
elements.append('unsigned')
5836-
while 1:
5837-
if self.skip_word_and_ws('short'):
5838-
elements.append('short')
5839-
elif self.skip_word_and_ws('long'):
5840-
elements.append('long')
5841-
else:
5842-
break
5843-
if self.skip_word_and_ws('char'):
5844-
elements.append('char')
5845-
elif self.skip_word_and_ws('int'):
5846-
elements.append('int')
5847-
elif self.skip_word_and_ws('double'):
5848-
elements.append('double')
5849-
if len(elements) > 0:
5850-
return ASTTrailingTypeSpecFundamental(' '.join(elements))
5862+
if self.match(_simple_type_sepcifiers_re):
5863+
return ASTTrailingTypeSpecFundamental(self.matched_text)
58515864

58525865
# decltype
58535866
self.skip_ws()
@@ -5867,7 +5880,7 @@ def _parse_trailing_type_spec(self) -> ASTTrailingTypeSpec:
58675880
# prefixed
58685881
prefix = None
58695882
self.skip_ws()
5870-
for k in self._prefix_keys:
5883+
for k in ('class', 'struct', 'enum', 'union', 'typename'):
58715884
if self.skip_word_and_ws(k):
58725885
prefix = k
58735886
break

tests/test_domain_c.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,62 @@ def exprCheck(expr, output=None):
275275
exprCheck('a or_eq 5')
276276

277277

278+
def test_domain_c_ast_fundamental_types():
279+
def types():
280+
def signed(t):
281+
yield t
282+
yield 'signed ' + t
283+
yield 'unsigned ' + t
284+
285+
# integer types
286+
# -------------
287+
yield 'void'
288+
yield from ('_Bool', 'bool')
289+
yield from signed('char')
290+
yield from signed('short')
291+
yield from signed('short int')
292+
yield from signed('int')
293+
yield from ('signed', 'unsigned')
294+
yield from signed('long')
295+
yield from signed('long int')
296+
yield from signed('long long')
297+
yield from signed('long long int')
298+
yield from ('__int128', '__uint128')
299+
# extensions
300+
for t in ('__int8', '__int16', '__int32', '__int64', '__int128'):
301+
yield from signed(t)
302+
303+
# floating point types
304+
# --------------------
305+
yield from ('_Decimal32', '_Decimal64', '_Decimal128')
306+
for f in ('float', 'double', 'long double'):
307+
yield f
308+
yield from (f + " _Complex", f + " complex")
309+
yield from ("_Complex " + f, "complex " + f)
310+
yield from ("_Imaginary " + f, "imaginary " + f)
311+
# extensions
312+
# https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html#Floating-Types
313+
yield from ('__float80', '_Float64x',
314+
'__float128', '_Float128',
315+
'__ibm128')
316+
# https://gcc.gnu.org/onlinedocs/gcc/Half-Precision.html#Half-Precision
317+
yield '__fp16'
318+
319+
# fixed-point types (extension)
320+
# -----------------------------
321+
# https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html#Fixed-Point
322+
for sat in ('', '_Sat '):
323+
for t in ('_Fract', 'fract', '_Accum', 'accum'):
324+
for size in ('short ', '', 'long ', 'long long '):
325+
for tt in signed(size + t):
326+
yield sat + tt
327+
328+
for t in types():
329+
input = "{key}%s foo" % t
330+
output = ' '.join(input.split())
331+
check('type', input, {1: 'foo'}, key='typedef', output=output)
332+
333+
278334
def test_domain_c_ast_type_definitions():
279335
check('type', "{key}T", {1: "T"})
280336

tests/test_domain_cpp.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def test_domain_cpp_ast_fundamental_types():
123123
def makeIdV1():
124124
if t == 'decltype(auto)':
125125
return None
126-
id = t.replace(" ", "-").replace("long", "l").replace("int", "i")
126+
id = t.replace(" ", "-").replace("long", "l")
127+
if "__int" not in t:
128+
id = id.replace("int", "i")
127129
id = id.replace("bool", "b").replace("char", "c")
128130
id = id.replace("wc_t", "wchar_t").replace("c16_t", "char16_t")
129131
id = id.replace("c8_t", "char8_t")
@@ -135,7 +137,9 @@ def makeIdV2():
135137
if t == "std::nullptr_t":
136138
id = "NSt9nullptr_tE"
137139
return "1f%s" % id
138-
check("function", "void f(%s arg)" % t, {1: makeIdV1(), 2: makeIdV2()})
140+
input = "void f(%s arg)" % t.replace(' ', ' ')
141+
output = "void f(%s arg)" % t
142+
check("function", input, {1: makeIdV1(), 2: makeIdV2()}, output=output)
139143

140144

141145
def test_domain_cpp_ast_expressions():

0 commit comments

Comments
 (0)