Commit 56de7cc8 by Abseil Team Committed by Mark Barolak

Googletest export

Fix gmock_gen to use MOCK_METHOD instead of old style macros. PiperOrigin-RevId: 294360947
parent 360f5f70
...@@ -53,8 +53,10 @@ def _RenderType(ast_type): ...@@ -53,8 +53,10 @@ def _RenderType(ast_type):
ast_type: The AST of the type. ast_type: The AST of the type.
Returns: Returns:
Rendered string of the type Rendered string and a boolean to indicate whether we have multiple args
(which is not handled correctly).
""" """
has_multiarg_error = False
# Add modifiers like 'const'. # Add modifiers like 'const'.
modifiers = '' modifiers = ''
if ast_type.modifiers: if ast_type.modifiers:
...@@ -64,37 +66,59 @@ def _RenderType(ast_type): ...@@ -64,37 +66,59 @@ def _RenderType(ast_type):
# Collect template args. # Collect template args.
template_args = [] template_args = []
for arg in ast_type.templated_types: for arg in ast_type.templated_types:
rendered_arg = _RenderType(arg) rendered_arg, e = _RenderType(arg)
if e: has_multiarg_error = True
template_args.append(rendered_arg) template_args.append(rendered_arg)
return_type += '<' + ', '.join(template_args) + '>' return_type += '<' + ', '.join(template_args) + '>'
# We are actually not handling multi-template-args correctly. So mark it.
if len(template_args) > 1:
has_multiarg_error = True
if ast_type.pointer: if ast_type.pointer:
return_type += '*' return_type += '*'
if ast_type.reference: if ast_type.reference:
return_type += '&' return_type += '&'
return return_type return return_type, has_multiarg_error
def _GetNumParameters(parameters, source):
num_parameters = len(parameters)
if num_parameters == 1:
first_param = parameters[0]
if source[first_param.start:first_param.end].strip() == 'void':
# We must treat T(void) as a function with no parameters.
return 0
return num_parameters
def _GenerateMethods(output_lines, source, class_node): def _GenerateMethods(output_lines, source, class_node):
function_type = ( function_type = (ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL |
ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL | ast.FUNCTION_OVERRIDE) ast.FUNCTION_OVERRIDE)
ctor_or_dtor = ast.FUNCTION_CTOR | ast.FUNCTION_DTOR ctor_or_dtor = ast.FUNCTION_CTOR | ast.FUNCTION_DTOR
indent = ' ' * _INDENT indent = ' ' * _INDENT
for node in class_node.body: for node in class_node.body:
# We only care about virtual functions. # We only care about virtual functions.
if (isinstance(node, ast.Function) and node.modifiers & function_type and if (isinstance(node, ast.Function) and
node.modifiers & function_type and
not node.modifiers & ctor_or_dtor): not node.modifiers & ctor_or_dtor):
# Pick out all the elements we need from the original function. # Pick out all the elements we need from the original function.
modifiers = 'override' const = ''
if node.modifiers & ast.FUNCTION_CONST: if node.modifiers & ast.FUNCTION_CONST:
modifiers = 'const, ' + modifiers const = 'CONST_'
num_parameters = _GetNumParameters(node.parameters, source)
return_type = 'void' return_type = 'void'
if node.return_type: if node.return_type:
return_type = _RenderType(node.return_type) return_type, has_multiarg_error = _RenderType(node.return_type)
# commas mess with macros, so nest it in parens if it has one if has_multiarg_error:
if ',' in return_type: for line in [
return_type = '(' + return_type + ')' '// The following line won\'t really compile, as the return',
'// type has multiple template arguments. To fix it, use a',
'// typedef for the return type.']:
output_lines.append(indent + line)
tmpl = ''
if class_node.templated_types:
tmpl = '_T'
mock_method_macro = 'MOCK_%sMETHOD%d%s' % (const, num_parameters, tmpl)
args = '' args = ''
if node.parameters: if node.parameters:
...@@ -113,15 +137,11 @@ def _GenerateMethods(output_lines, source, class_node): ...@@ -113,15 +137,11 @@ def _GenerateMethods(output_lines, source, class_node):
# parameters together on a single line. Ensure there is a # parameters together on a single line. Ensure there is a
# space in an argument which is split by a newline without # space in an argument which is split by a newline without
# intervening whitespace, e.g.: int\nBar # intervening whitespace, e.g.: int\nBar
args_strings = re.sub(' +', ' ', args_strings.replace('\n', ' ')) args = re.sub(' +', ' ', args_strings.replace('\n', ' '))
# Remove spaces from the begining, end, and before commas
args = re.sub(' ,', ',', args_strings).strip()
# Create the mock method definition. # Create the mock method definition.
output_lines.extend([ output_lines.extend(['%s%s(%s,' % (indent, mock_method_macro, node.name),
'%sMOCK_METHOD(%s, %s, (%s), (%s));' % '%s%s(%s));' % (indent * 3, return_type, args)])
(indent, return_type, node.name, args, modifiers)
])
def _GenerateMocks(filename, source, ast_list, desired_class_names): def _GenerateMocks(filename, source, ast_list, desired_class_names):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment