Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C and C++: support parameterized GNU style attributes #7854

Merged
merged 1 commit into from Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -13,6 +13,8 @@ Deprecated
Features added
--------------

* #7853: C and C++, support parameterized GNU style attributes.

Bugs fixed
----------

Expand Down
5 changes: 3 additions & 2 deletions sphinx/domains/c.py
Expand Up @@ -28,7 +28,8 @@
from sphinx.roles import SphinxRole, XRefRole
from sphinx.util import logging
from sphinx.util.cfamily import (
NoOldIdError, ASTBaseBase, verify_description_mode, StringifyTransform,
NoOldIdError, ASTBaseBase, ASTBaseParenExprList,
verify_description_mode, StringifyTransform,
BaseParser, DefinitionError, UnsupportedMultiCharacterCharLiteral,
identifier_re, anon_identifier_re, integer_literal_re, octal_literal_re,
hex_literal_re, binary_literal_re, integers_literal_suffix_re,
Expand Down Expand Up @@ -1053,7 +1054,7 @@ def describe_signature(self, signode: TextElement, mode: str,
# Initializer
################################################################################

class ASTParenExprList(ASTBase):
class ASTParenExprList(ASTBaseParenExprList):
def __init__(self, exprs: List[ASTExpression]) -> None:
self.exprs = exprs

Expand Down
5 changes: 3 additions & 2 deletions sphinx/domains/cpp.py
Expand Up @@ -31,7 +31,8 @@
from sphinx.transforms.post_transforms import ReferencesResolver
from sphinx.util import logging
from sphinx.util.cfamily import (
NoOldIdError, ASTBaseBase, ASTAttribute, verify_description_mode, StringifyTransform,
NoOldIdError, ASTBaseBase, ASTAttribute, ASTBaseParenExprList,
verify_description_mode, StringifyTransform,
BaseParser, DefinitionError, UnsupportedMultiCharacterCharLiteral,
identifier_re, anon_identifier_re, integer_literal_re, octal_literal_re,
hex_literal_re, binary_literal_re, integers_literal_suffix_re,
Expand Down Expand Up @@ -2742,7 +2743,7 @@ def describe_signature(self, signode: TextElement, mode: str,
signode += nodes.Text('...')


class ASTParenExprList(ASTBase):
class ASTParenExprList(ASTBaseParenExprList):
def __init__(self, exprs: List[Union[ASTExpression, ASTBracedInitList]]) -> None:
self.exprs = exprs

Expand Down
21 changes: 12 additions & 9 deletions sphinx/util/cfamily.py
Expand Up @@ -12,7 +12,7 @@
import warnings
from copy import deepcopy
from typing import (
Any, Callable, List, Match, Pattern, Tuple, Union
Any, Callable, List, Match, Optional, Pattern, Tuple, Union
)

from docutils import nodes
Expand Down Expand Up @@ -148,16 +148,14 @@ def describe_signature(self, signode: TextElement) -> None:


class ASTGnuAttribute(ASTBaseBase):
def __init__(self, name: str, args: Any) -> None:
def __init__(self, name: str, args: Optional["ASTBaseParenExprList"]) -> None:
self.name = name
self.args = args

def _stringify(self, transform: StringifyTransform) -> str:
res = [self.name]
if self.args:
res.append('(')
res.append(transform(self.args))
res.append(')')
return ''.join(res)


Expand Down Expand Up @@ -211,6 +209,11 @@ def describe_signature(self, signode: TextElement) -> None:

################################################################################

class ASTBaseParenExprList(ASTBaseBase):
pass


################################################################################

class UnsupportedMultiCharacterCharLiteral(Exception):
@property
Expand Down Expand Up @@ -415,11 +418,8 @@ def _parse_attribute(self) -> ASTAttribute:
while 1:
if self.match(identifier_re):
name = self.matched_text
self.skip_ws()
if self.skip_string_and_ws('('):
self.fail('Parameterized GNU style attribute not yet supported.')
attrs.append(ASTGnuAttribute(name, None))
# TODO: parse arguments for the attribute
exprs = self._parse_paren_expression_list()
attrs.append(ASTGnuAttribute(name, exprs))
if self.skip_string_and_ws(','):
continue
elif self.skip_string_and_ws(')'):
Expand Down Expand Up @@ -447,3 +447,6 @@ def _parse_attribute(self) -> ASTAttribute:
return ASTParenAttribute(id, arg)

return None

def _parse_paren_expression_list(self) -> ASTBaseParenExprList:
raise NotImplementedError
2 changes: 2 additions & 0 deletions tests/test_domain_c.py
Expand Up @@ -469,6 +469,8 @@ def test_attributes():
check('member', '__attribute__(()) int f', {1: 'f'})
check('member', '__attribute__((a)) int f', {1: 'f'})
check('member', '__attribute__((a, b)) int f', {1: 'f'})
check('member', '__attribute__((optimize(3))) int f', {1: 'f'})
check('member', '__attribute__((format(printf, 1, 2))) int f', {1: 'f'})
# style: user-defined id
check('member', 'id_attr int f', {1: 'f'})
# style: user-defined paren
Expand Down
2 changes: 2 additions & 0 deletions tests/test_domain_cpp.py
Expand Up @@ -897,6 +897,8 @@ def test_attributes():
check('member', '__attribute__(()) int f', {1: 'f__i', 2: '1f'})
check('member', '__attribute__((a)) int f', {1: 'f__i', 2: '1f'})
check('member', '__attribute__((a, b)) int f', {1: 'f__i', 2: '1f'})
check('member', '__attribute__((optimize(3))) int f', {1: 'f__i', 2: '1f'})
check('member', '__attribute__((format(printf, 1, 2))) int f', {1: 'f__i', 2: '1f'})
# style: user-defined id
check('member', 'id_attr int f', {1: 'f__i', 2: '1f'})
# style: user-defined paren
Expand Down