mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
[C++] Ensure consistent non-specialization template argument representation
Previously, in certain cases the template arguments of non-specializations were retained, leading to incorrect merging of symbols.
This commit is contained in:
parent
3d6d501a43
commit
5b97ec522a
@ -2604,6 +2604,10 @@ class ASTDeclaratorPtr(ASTDeclarator):
|
|||||||
def name(self, name: ASTNestedName) -> None:
|
def name(self, name: ASTNestedName) -> None:
|
||||||
self.next.name = name
|
self.next.name = name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def isPack(self) -> bool:
|
||||||
|
return self.next.isPack
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def function_params(self) -> List[ASTFunctionParameter]:
|
def function_params(self) -> List[ASTFunctionParameter]:
|
||||||
return self.next.function_params
|
return self.next.function_params
|
||||||
@ -2707,7 +2711,7 @@ class ASTDeclaratorRef(ASTDeclarator):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def isPack(self) -> bool:
|
def isPack(self) -> bool:
|
||||||
return True
|
return self.next.isPack
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def function_params(self) -> List[ASTFunctionParameter]:
|
def function_params(self) -> List[ASTFunctionParameter]:
|
||||||
@ -2779,6 +2783,10 @@ class ASTDeclaratorParamPack(ASTDeclarator):
|
|||||||
def trailingReturn(self) -> "ASTType":
|
def trailingReturn(self) -> "ASTType":
|
||||||
return self.next.trailingReturn
|
return self.next.trailingReturn
|
||||||
|
|
||||||
|
@property
|
||||||
|
def isPack(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
def require_space_after_declSpecs(self) -> bool:
|
def require_space_after_declSpecs(self) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -2835,6 +2843,10 @@ class ASTDeclaratorMemPtr(ASTDeclarator):
|
|||||||
def name(self, name: ASTNestedName) -> None:
|
def name(self, name: ASTNestedName) -> None:
|
||||||
self.next.name = name
|
self.next.name = name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def isPack(self):
|
||||||
|
return self.next.isPack
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def function_params(self) -> List[ASTFunctionParameter]:
|
def function_params(self) -> List[ASTFunctionParameter]:
|
||||||
return self.next.function_params
|
return self.next.function_params
|
||||||
@ -2932,6 +2944,10 @@ class ASTDeclaratorParen(ASTDeclarator):
|
|||||||
def name(self, name: ASTNestedName) -> None:
|
def name(self, name: ASTNestedName) -> None:
|
||||||
self.inner.name = name
|
self.inner.name = name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def isPack(self):
|
||||||
|
return self.inner.isPack or self.next.isPack
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def function_params(self) -> List[ASTFunctionParameter]:
|
def function_params(self) -> List[ASTFunctionParameter]:
|
||||||
return self.inner.function_params
|
return self.inner.function_params
|
||||||
@ -3510,6 +3526,14 @@ class ASTTemplateParam(ASTBase):
|
|||||||
env: "BuildEnvironment", symbol: "Symbol") -> None:
|
env: "BuildEnvironment", symbol: "Symbol") -> None:
|
||||||
raise NotImplementedError(repr(self))
|
raise NotImplementedError(repr(self))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def isPack(self) -> bool:
|
||||||
|
raise NotImplementedError(repr(self))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> ASTNestedName:
|
||||||
|
raise NotImplementedError(repr(self))
|
||||||
|
|
||||||
|
|
||||||
class ASTTemplateKeyParamPackIdDefault(ASTTemplateParam):
|
class ASTTemplateKeyParamPackIdDefault(ASTTemplateParam):
|
||||||
def __init__(self, key: str, identifier: ASTIdentifier,
|
def __init__(self, key: str, identifier: ASTIdentifier,
|
||||||
@ -4129,6 +4153,31 @@ class LookupKey:
|
|||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
|
|
||||||
|
def _is_specialization(templateParams: Union[ASTTemplateParams, ASTTemplateIntroduction],
|
||||||
|
templateArgs: ASTTemplateArgs) -> bool:
|
||||||
|
"""Checks if `templateArgs` does not exactly match `templateParams`."""
|
||||||
|
# the names of the template parameters must be given exactly as args
|
||||||
|
# and params that are packs must in the args be the name expanded
|
||||||
|
if len(templateParams.params) != len(templateArgs.args):
|
||||||
|
return True
|
||||||
|
# having no template params and no arguments is also a specialization
|
||||||
|
if len(templateParams.params) == 0:
|
||||||
|
return True
|
||||||
|
for i in range(len(templateParams.params)):
|
||||||
|
param = templateParams.params[i]
|
||||||
|
arg = templateArgs.args[i]
|
||||||
|
# TODO: doing this by string manipulation is probably not the most efficient
|
||||||
|
paramName = str(param.name)
|
||||||
|
argTxt = str(arg)
|
||||||
|
isArgPackExpansion = argTxt.endswith('...')
|
||||||
|
if param.isPack != isArgPackExpansion:
|
||||||
|
return True
|
||||||
|
argName = argTxt[:-3] if isArgPackExpansion else argTxt
|
||||||
|
if paramName != argName:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Symbol:
|
class Symbol:
|
||||||
debug_indent = 0
|
debug_indent = 0
|
||||||
debug_indent_string = " "
|
debug_indent_string = " "
|
||||||
@ -4177,6 +4226,16 @@ class Symbol:
|
|||||||
self.siblingAbove: Symbol = None
|
self.siblingAbove: Symbol = None
|
||||||
self.siblingBelow: Symbol = None
|
self.siblingBelow: Symbol = None
|
||||||
self.identOrOp = identOrOp
|
self.identOrOp = identOrOp
|
||||||
|
# Ensure the same symbol for `A` is created for:
|
||||||
|
#
|
||||||
|
# .. cpp:class:: template <typename T> class A
|
||||||
|
#
|
||||||
|
# and
|
||||||
|
#
|
||||||
|
# .. cpp:function:: template <typename T> int A<T>::foo()
|
||||||
|
if (templateArgs is not None and
|
||||||
|
not _is_specialization(templateParams, templateArgs)):
|
||||||
|
templateArgs = None
|
||||||
self.templateParams = templateParams # template<templateParams>
|
self.templateParams = templateParams # template<templateParams>
|
||||||
self.templateArgs = templateArgs # identifier<templateArgs>
|
self.templateArgs = templateArgs # identifier<templateArgs>
|
||||||
self.declaration = declaration
|
self.declaration = declaration
|
||||||
@ -4357,33 +4416,12 @@ class Symbol:
|
|||||||
Symbol.debug_print("correctPrimaryTemplateAargs:", correctPrimaryTemplateArgs)
|
Symbol.debug_print("correctPrimaryTemplateAargs:", correctPrimaryTemplateArgs)
|
||||||
Symbol.debug_print("searchInSiblings: ", searchInSiblings)
|
Symbol.debug_print("searchInSiblings: ", searchInSiblings)
|
||||||
|
|
||||||
def isSpecialization() -> bool:
|
|
||||||
# the names of the template parameters must be given exactly as args
|
|
||||||
# and params that are packs must in the args be the name expanded
|
|
||||||
if len(templateParams.params) != len(templateArgs.args):
|
|
||||||
return True
|
|
||||||
# having no template params and no arguments is also a specialization
|
|
||||||
if len(templateParams.params) == 0:
|
|
||||||
return True
|
|
||||||
for i in range(len(templateParams.params)):
|
|
||||||
param = templateParams.params[i]
|
|
||||||
arg = templateArgs.args[i]
|
|
||||||
# TODO: doing this by string manipulation is probably not the most efficient
|
|
||||||
paramName = str(param.name)
|
|
||||||
argTxt = str(arg)
|
|
||||||
isArgPackExpansion = argTxt.endswith('...')
|
|
||||||
if param.isPack != isArgPackExpansion:
|
|
||||||
return True
|
|
||||||
argName = argTxt[:-3] if isArgPackExpansion else argTxt
|
|
||||||
if paramName != argName:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
if correctPrimaryTemplateArgs:
|
if correctPrimaryTemplateArgs:
|
||||||
if templateParams is not None and templateArgs is not None:
|
if templateParams is not None and templateArgs is not None:
|
||||||
# If both are given, but it's not a specialization, then do lookup as if
|
# If both are given, but it's not a specialization, then do lookup as if
|
||||||
# there is no argument list.
|
# there is no argument list.
|
||||||
# For example: template<typename T> int A<T>::var;
|
# For example: template<typename T> int A<T>::var;
|
||||||
if not isSpecialization():
|
if not _is_specialization(templateParams, templateArgs):
|
||||||
templateArgs = None
|
templateArgs = None
|
||||||
|
|
||||||
def matches(s: "Symbol") -> bool:
|
def matches(s: "Symbol") -> bool:
|
||||||
@ -4839,14 +4877,23 @@ class Symbol:
|
|||||||
ourChild.declaration.directiveType, name)
|
ourChild.declaration.directiveType, name)
|
||||||
logger.warning(msg, location=(otherChild.docname, otherChild.line))
|
logger.warning(msg, location=(otherChild.docname, otherChild.line))
|
||||||
else:
|
else:
|
||||||
# Both have declarations, and in the same docname.
|
if (otherChild.declaration.objectType ==
|
||||||
# This can apparently happen, it should be safe to
|
ourChild.declaration.objectType and
|
||||||
# just ignore it, right?
|
otherChild.declaration.objectType in
|
||||||
# Hmm, only on duplicate declarations, right?
|
('templateParam', 'functionParam')):
|
||||||
msg = "Internal C++ domain error during symbol merging.\n"
|
# `ourChild` was presumably just created during mergging
|
||||||
msg += "ourChild:\n" + ourChild.to_string(1)
|
# by the call to `_fill_empty` on the parent and can be
|
||||||
msg += "\notherChild:\n" + otherChild.to_string(1)
|
# ignored.
|
||||||
logger.warning(msg, location=otherChild.docname)
|
pass
|
||||||
|
else:
|
||||||
|
# Both have declarations, and in the same docname.
|
||||||
|
# This can apparently happen, it should be safe to
|
||||||
|
# just ignore it, right?
|
||||||
|
# Hmm, only on duplicate declarations, right?
|
||||||
|
msg = "Internal C++ domain error during symbol merging.\n"
|
||||||
|
msg += "ourChild:\n" + ourChild.to_string(1)
|
||||||
|
msg += "\notherChild:\n" + otherChild.to_string(1)
|
||||||
|
logger.warning(msg, location=otherChild.docname)
|
||||||
ourChild.merge_with(otherChild, docnames, env)
|
ourChild.merge_with(otherChild, docnames, env)
|
||||||
if Symbol.debug_lookup:
|
if Symbol.debug_lookup:
|
||||||
Symbol.debug_indent -= 2
|
Symbol.debug_indent -= 2
|
||||||
|
@ -841,9 +841,9 @@ def test_domain_cpp_ast_templates():
|
|||||||
check('class', 'abc::ns::foo{{id_0, id_1, ...id_2}} {key}xyz::bar',
|
check('class', 'abc::ns::foo{{id_0, id_1, ...id_2}} {key}xyz::bar',
|
||||||
{2: 'I00DpEXN3abc2ns3fooEI4id_04id_1sp4id_2EEN3xyz3barE'})
|
{2: 'I00DpEXN3abc2ns3fooEI4id_04id_1sp4id_2EEN3xyz3barE'})
|
||||||
check('class', 'abc::ns::foo{{id_0, id_1, id_2}} {key}xyz::bar<id_0, id_1, id_2>',
|
check('class', 'abc::ns::foo{{id_0, id_1, id_2}} {key}xyz::bar<id_0, id_1, id_2>',
|
||||||
{2: 'I000EXN3abc2ns3fooEI4id_04id_14id_2EEN3xyz3barI4id_04id_14id_2EE'})
|
{2: 'I000EXN3abc2ns3fooEI4id_04id_14id_2EEN3xyz3barE'})
|
||||||
check('class', 'abc::ns::foo{{id_0, id_1, ...id_2}} {key}xyz::bar<id_0, id_1, id_2...>',
|
check('class', 'abc::ns::foo{{id_0, id_1, ...id_2}} {key}xyz::bar<id_0, id_1, id_2...>',
|
||||||
{2: 'I00DpEXN3abc2ns3fooEI4id_04id_1sp4id_2EEN3xyz3barI4id_04id_1Dp4id_2EE'})
|
{2: 'I00DpEXN3abc2ns3fooEI4id_04id_1sp4id_2EEN3xyz3barE'})
|
||||||
|
|
||||||
check('class', 'template<> Concept{{U}} {key}A<int>::B', {2: 'IEI0EX7ConceptI1UEEN1AIiE1BE'})
|
check('class', 'template<> Concept{{U}} {key}A<int>::B', {2: 'IEI0EX7ConceptI1UEEN1AIiE1BE'})
|
||||||
|
|
||||||
@ -901,7 +901,7 @@ def test_domain_cpp_ast_requires_clauses():
|
|||||||
'template<typename T> requires R<T> ' +
|
'template<typename T> requires R<T> ' +
|
||||||
'template<typename U> requires S<T> ' +
|
'template<typename U> requires S<T> ' +
|
||||||
'void A<T>::f() requires B',
|
'void A<T>::f() requires B',
|
||||||
{4: 'I0EIQ1RI1TEEI0EIQaa1SI1TE1BEN1AI1TE1fEvv'})
|
{4: 'I0EIQ1RI1TEEI0EIQaa1SI1TE1BEN1A1fEvv'})
|
||||||
check('function',
|
check('function',
|
||||||
'template<template<typename T> requires R<T> typename X> ' +
|
'template<template<typename T> requires R<T> typename X> ' +
|
||||||
'void f()',
|
'void f()',
|
||||||
@ -1395,3 +1395,89 @@ def test_domain_cpp_parse_mix_decl_duplicate(app, warning):
|
|||||||
assert "index.rst:3: WARNING: Duplicate C++ declaration, also defined at index:1." in ws[2]
|
assert "index.rst:3: WARNING: Duplicate C++ declaration, also defined at index:1." in ws[2]
|
||||||
assert "Declaration is '.. cpp:struct:: A'." in ws[3]
|
assert "Declaration is '.. cpp:struct:: A'." in ws[3]
|
||||||
assert ws[4] == ""
|
assert ws[4] == ""
|
||||||
|
|
||||||
|
|
||||||
|
# For some reason, using the default testroot of "root" leads to the contents of
|
||||||
|
# `test-root/objects.txt` polluting the symbol table depending on the test
|
||||||
|
# execution order. Using a testroot of "config" seems to avoid that problem.
|
||||||
|
@pytest.mark.sphinx(testroot='config')
|
||||||
|
def test_domain_cpp_normalize_unspecialized_template_args(make_app, app_params):
|
||||||
|
args, kwargs = app_params
|
||||||
|
|
||||||
|
text1 = (".. cpp:class:: template <typename T> A\n")
|
||||||
|
text2 = (".. cpp:class:: template <typename T> template <typename U> A<T>::B\n")
|
||||||
|
|
||||||
|
app1 = make_app(*args, **kwargs)
|
||||||
|
restructuredtext.parse(app=app1, text=text1, docname='text1')
|
||||||
|
root1 = app1.env.domaindata['cpp']['root_symbol']
|
||||||
|
|
||||||
|
assert root1.dump(1) == (
|
||||||
|
' ::\n'
|
||||||
|
' template<typename T> \n'
|
||||||
|
' A: template<typename T> A\t(text1)\n'
|
||||||
|
' T: typename T\t(text1)\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
app2 = make_app(*args, **kwargs)
|
||||||
|
restructuredtext.parse(app=app2, text=text2, docname='text2')
|
||||||
|
root2 = app2.env.domaindata['cpp']['root_symbol']
|
||||||
|
|
||||||
|
assert root2.dump(1) == (
|
||||||
|
' ::\n'
|
||||||
|
' template<typename T> \n'
|
||||||
|
' A\n'
|
||||||
|
' T\n'
|
||||||
|
' template<typename U> \n'
|
||||||
|
' B: template<typename T> template<typename U> A<T>::B\t(text2)\n'
|
||||||
|
' U: typename U\t(text2)\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
root2.merge_with(root1, ['text1'], app2.env)
|
||||||
|
|
||||||
|
assert root2.dump(1) == (
|
||||||
|
' ::\n'
|
||||||
|
' template<typename T> \n'
|
||||||
|
' A: template<typename T> A\t(text1)\n'
|
||||||
|
' T: typename T\t(text1)\n'
|
||||||
|
' template<typename U> \n'
|
||||||
|
' B: template<typename T> template<typename U> A<T>::B\t(text2)\n'
|
||||||
|
' U: typename U\t(text2)\n'
|
||||||
|
)
|
||||||
|
warning = app2._warning.getvalue()
|
||||||
|
assert 'Internal C++ domain error during symbol merging' not in warning
|
||||||
|
|
||||||
|
|
||||||
|
def parse_template_parameter(param: str):
|
||||||
|
ast = parse('type', 'template<' + param + '> X')
|
||||||
|
return ast.templatePrefix.templates[0].params[0]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'param,is_pack',
|
||||||
|
[('typename', False),
|
||||||
|
('typename T', False),
|
||||||
|
('typename...', True),
|
||||||
|
('typename... T', True),
|
||||||
|
('int', False),
|
||||||
|
('int N', False),
|
||||||
|
('int* N', False),
|
||||||
|
('int& N', False),
|
||||||
|
('int&... N', True),
|
||||||
|
('int*... N', True),
|
||||||
|
('int...', True),
|
||||||
|
('int... N', True),
|
||||||
|
('auto', False),
|
||||||
|
('auto...', True),
|
||||||
|
('int X::*', False),
|
||||||
|
('int X::*...', True),
|
||||||
|
('int (X::*)(bool)', False),
|
||||||
|
('int (X::*x)(bool)', False),
|
||||||
|
# TODO: the following two declarations cannot currently be parsed
|
||||||
|
# ('int (X::*)(bool)...', True),
|
||||||
|
# ('int (X::*x...)(bool)', True),
|
||||||
|
('template<typename> class', False),
|
||||||
|
('template<typename> class...', True),
|
||||||
|
])
|
||||||
|
def test_domain_cpp_template_parameters_is_pack(param: str, is_pack: bool):
|
||||||
|
ast = parse_template_parameter(param)
|
||||||
|
assert ast.isPack == is_pack
|
||||||
|
Loading…
Reference in New Issue
Block a user