Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions cpp/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,11 @@ def add_parameter():
if default:
del default[0] # Remove flag.
end = type_modifiers[-1].end
needs_name_removed = \
not (len(type_modifiers) == 1 or (len(type_modifiers) == 2 and
type_modifiers[0].name == 'const'))
name, type_name, templated_types, modifiers, _, __ = \
self.declaration_to_parts(type_modifiers, True)
self.declaration_to_parts(type_modifiers, needs_name_removed)
parameter_type = Type(first_token.start, first_token.end,
type_name, templated_types, modifiers,
reference, pointer, array)
Expand Down Expand Up @@ -1029,31 +1032,37 @@ def _get_method(self, return_type_and_name, modifiers, templated_types,
parameters = list(self._get_parameters())
del parameters[-1] # Remove trailing ')'.

token = self._get_next_token()
while token.token_type == tokenize.NAME:
modifier_token = token
try:
token = self._get_next_token()
if modifier_token.name == 'const':
except:
token = tokenize.Token(tokenize.SYNTAX, ';', 0, 0)
while token.token_type == tokenize.NAME:
if token.name == 'const':
modifiers |= FUNCTION_CONST
elif modifier_token.name == '__attribute__':
token = self._get_next_token()
elif token.name == '__attribute__':
# TODO(nnorwitz): handle more __attribute__ details.
modifiers |= FUNCTION_ATTRIBUTE
token = self._get_next_token()
assert token.name == '(', token
# Consume everything between the (parens).
list(self._get_matching_char('(', ')'))
token = self._get_next_token()
elif modifier_token.name == 'throw':
elif token.name == 'throw':
modifiers |= FUNCTION_THROW
token = self._get_next_token()
assert token.name == '(', token
# Consume everything between the (parens).
list(self._get_matching_char('(', ')'))
token = self._get_next_token()
elif modifier_token.name == modifier_token.name.upper():
elif token.name == token.name.upper():
# HACK(nnorwitz): assume that all upper-case names
# are some macro we aren't expanding.
modifiers |= FUNCTION_UNKNOWN_ANNOTATION
token = self._get_next_token()
else:
self.handle_error('unexpected token', modifier_token)
self._add_back_token(token)
token = tokenize.Token(tokenize.SYNTAX, ';', 0, 0)

if token.token_type != tokenize.SYNTAX:
raise ParseError(token)
Expand Down
2 changes: 1 addition & 1 deletion test/baz.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#include "typedef.h"

void fct(Bar& bar);
void fct(Type& t);
void fct(Type&);
1 change: 1 addition & 0 deletions test/macro1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MyMacro(Get)
2 changes: 2 additions & 0 deletions test/macro2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MyMacro(Get)
MyMacro(Set)
5 changes: 5 additions & 0 deletions test/macro3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MyMacro(NS, Name)

namespace NS {

}
16 changes: 16 additions & 0 deletions test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ class AstBuilderIntegrationTest(unittest.TestCase):

"""

def test_function_one_argument_with_name(self):
for argument in ('Foo f', 'const Foo f', 'Foo& f', 'const Foo& f'):
code = 'void fct(%s);' % argument
nodes = list(MakeBuilder(code).generate())
self.assertEqual(1, len(nodes))
self.assertEqual(1, len(nodes[0].parameters))
self.assertEqual('f', nodes[0].parameters[0].name)

def test_function_one_argument_with_no_name(self):
for argument in ('Foo', 'const Foo', 'Foo&', 'const Foo&'):
code = 'void fct(%s);' % argument
nodes = list(MakeBuilder(code).generate())
self.assertEqual(1, len(nodes))
self.assertEqual(1, len(nodes[0].parameters))
self.assertEqual(None, nodes[0].parameters[0].name)

def test_no_argument(self):
nodes = list(MakeBuilder('FOO();').generate())
self.assertEqual(1, len(nodes))
Expand Down