C++, fix minor space issues in declarators

This commit is contained in:
Jakob Lykke Andersen 2020-03-18 09:57:23 +01:00
parent f6cb7f1a43
commit dcbbc83ac7
2 changed files with 27 additions and 17 deletions

View File

@ -2288,8 +2288,7 @@ class ASTDeclaratorPtr(ASTDeclarator):
return self.next.function_params return self.next.function_params
def require_space_after_declSpecs(self) -> bool: def require_space_after_declSpecs(self) -> bool:
# TODO: if has paramPack, then False ? return self.next.require_space_after_declSpecs()
return True
def _stringify(self, transform: StringifyTransform) -> str: def _stringify(self, transform: StringifyTransform) -> str:
res = ['*'] res = ['*']
@ -2304,7 +2303,7 @@ class ASTDeclaratorPtr(ASTDeclarator):
res.append(' ') res.append(' ')
res.append('const') res.append('const')
if self.const or self.volatile or len(self.attrs) > 0: if self.const or self.volatile or len(self.attrs) > 0:
if self.next.require_space_after_declSpecs: if self.next.require_space_after_declSpecs():
res.append(' ') res.append(' ')
res.append(transform(self.next)) res.append(transform(self.next))
return ''.join(res) return ''.join(res)
@ -2364,7 +2363,7 @@ class ASTDeclaratorPtr(ASTDeclarator):
signode += nodes.Text(' ') signode += nodes.Text(' ')
_add_anno(signode, 'const') _add_anno(signode, 'const')
if self.const or self.volatile or len(self.attrs) > 0: if self.const or self.volatile or len(self.attrs) > 0:
if self.next.require_space_after_declSpecs: if self.next.require_space_after_declSpecs():
signode += nodes.Text(' ') signode += nodes.Text(' ')
self.next.describe_signature(signode, mode, env, symbol) self.next.describe_signature(signode, mode, env, symbol)
@ -2394,7 +2393,7 @@ class ASTDeclaratorRef(ASTDeclarator):
res = ['&'] res = ['&']
for a in self.attrs: for a in self.attrs:
res.append(transform(a)) res.append(transform(a))
if len(self.attrs) > 0 and self.next.require_space_after_declSpecs: if len(self.attrs) > 0 and self.next.require_space_after_declSpecs():
res.append(' ') res.append(' ')
res.append(transform(self.next)) res.append(transform(self.next))
return ''.join(res) return ''.join(res)
@ -2425,7 +2424,7 @@ class ASTDeclaratorRef(ASTDeclarator):
signode += nodes.Text("&") signode += nodes.Text("&")
for a in self.attrs: for a in self.attrs:
a.describe_signature(signode) a.describe_signature(signode)
if len(self.attrs) > 0 and self.next.require_space_after_declSpecs: if len(self.attrs) > 0 and self.next.require_space_after_declSpecs():
signode += nodes.Text(' ') signode += nodes.Text(' ')
self.next.describe_signature(signode, mode, env, symbol) self.next.describe_signature(signode, mode, env, symbol)
@ -2507,9 +2506,11 @@ class ASTDeclaratorMemPtr(ASTDeclarator):
res.append(transform(self.className)) res.append(transform(self.className))
res.append('::*') res.append('::*')
if self.volatile: if self.volatile:
res.append(' volatile') res.append('volatile')
if self.const: if self.const:
res.append(' const') if self.volatile:
res.append(' ')
res.append('const')
if self.next.require_space_after_declSpecs(): if self.next.require_space_after_declSpecs():
res.append(' ') res.append(' ')
res.append(transform(self.next)) res.append(transform(self.next))
@ -2565,8 +2566,7 @@ class ASTDeclaratorMemPtr(ASTDeclarator):
signode += nodes.Text(' ') signode += nodes.Text(' ')
_add_anno(signode, 'const') _add_anno(signode, 'const')
if self.next.require_space_after_declSpecs(): if self.next.require_space_after_declSpecs():
if self.volatile or self.const: signode += nodes.Text(' ')
signode += nodes.Text(' ')
self.next.describe_signature(signode, mode, env, symbol) self.next.describe_signature(signode, mode, env, symbol)

View File

@ -461,6 +461,13 @@ def test_function_definitions():
with pytest.raises(DefinitionError): with pytest.raises(DefinitionError):
parse('function', 'int foo(D d=x(a') parse('function', 'int foo(D d=x(a')
check('function', 'int foo(const A&... a)', {1: "foo__ACRDp", 2: "3fooDpRK1A"}) check('function', 'int foo(const A&... a)', {1: "foo__ACRDp", 2: "3fooDpRK1A"})
check('function', 'int foo(const A&...)', {1: "foo__ACRDp", 2: "3fooDpRK1A"})
check('function', 'int foo(const A*... a)', {1: "foo__ACPDp", 2: "3fooDpPK1A"})
check('function', 'int foo(const A*...)', {1: "foo__ACPDp", 2: "3fooDpPK1A"})
check('function', 'int foo(const int A::*... a)', {2: "3fooDpM1AKi"})
check('function', 'int foo(const int A::*...)', {2: "3fooDpM1AKi"})
#check('function', 'int foo(int (*a)(A)...)', {1: "foo__ACRDp", 2: "3fooDpPK1A"})
#check('function', 'int foo(int (*)(A)...)', {1: "foo__ACRDp", 2: "3fooDpPK1A"})
check('function', 'virtual void f()', {1: "f", 2: "1fv"}) check('function', 'virtual void f()', {1: "f", 2: "1fv"})
# test for ::nestedName, from issue 1738 # test for ::nestedName, from issue 1738
check("function", "result(int val, ::std::error_category const &cat)", check("function", "result(int val, ::std::error_category const &cat)",
@ -502,18 +509,21 @@ def test_function_definitions():
check('function', 'void f(int C::*)', {2: '1fM1Ci'}) check('function', 'void f(int C::*)', {2: '1fM1Ci'})
check('function', 'void f(int C::* p)', {2: '1fM1Ci'}) check('function', 'void f(int C::* p)', {2: '1fM1Ci'})
check('function', 'void f(int ::C::* p)', {2: '1fM1Ci'}) check('function', 'void f(int ::C::* p)', {2: '1fM1Ci'})
check('function', 'void f(int C::* const)', {2: '1fKM1Ci'}) check('function', 'void f(int C::*const)', {2: '1fKM1Ci'})
check('function', 'void f(int C::* const&)', {2: '1fRKM1Ci'}) check('function', 'void f(int C::*const&)', {2: '1fRKM1Ci'})
check('function', 'void f(int C::* volatile)', {2: '1fVM1Ci'}) check('function', 'void f(int C::*volatile)', {2: '1fVM1Ci'})
check('function', 'void f(int C::* const volatile)', {2: '1fVKM1Ci'}, check('function', 'void f(int C::*const volatile)', {2: '1fVKM1Ci'},
output='void f(int C::* volatile const)') output='void f(int C::*volatile const)')
check('function', 'void f(int C::* volatile const)', {2: '1fVKM1Ci'}) check('function', 'void f(int C::*volatile const)', {2: '1fVKM1Ci'})
check('function', 'void f(int (C::*)(float, double))', {2: '1fM1CFifdE'}) check('function', 'void f(int (C::*)(float, double))', {2: '1fM1CFifdE'})
check('function', 'void f(int (C::* p)(float, double))', {2: '1fM1CFifdE'}) check('function', 'void f(int (C::* p)(float, double))', {2: '1fM1CFifdE'})
check('function', 'void f(int (::C::* p)(float, double))', {2: '1fM1CFifdE'}) check('function', 'void f(int (::C::* p)(float, double))', {2: '1fM1CFifdE'})
check('function', 'void f(void (C::*)() const &)', {2: '1fM1CKRFvvE'}) check('function', 'void f(void (C::*)() const &)', {2: '1fM1CKRFvvE'})
check('function', 'int C::* f(int, double)', {2: '1fid'}) check('function', 'int C::* f(int, double)', {2: '1fid'})
check('function', 'void f(int C::* *)', {2: '1fPM1Ci'}) check('function', 'void f(int C::* *p)', {2: '1fPM1Ci'})
check('function', 'void f(int C::**)', {2: '1fPM1Ci'})
check('function', 'void f(int C::*const *p)', {2: '1fPKM1Ci'})
check('function', 'void f(int C::*const*)', {2: '1fPKM1Ci'})
# exceptions from return type mangling # exceptions from return type mangling
check('function', 'template<typename T> C()', {2: 'I0E1Cv'}) check('function', 'template<typename T> C()', {2: 'I0E1Cv'})