Commit 360f5f70 by Abseil Team Committed by Mark Barolak

Googletest export

Fix gmock_gen to use MOCK_METHOD instead of old style macros. PiperOrigin-RevId: 294332975
parent 139fa202
......@@ -53,10 +53,8 @@ def _RenderType(ast_type):
ast_type: The AST of the type.
Returns:
Rendered string and a boolean to indicate whether we have multiple args
(which is not handled correctly).
Rendered string of the type
"""
has_multiarg_error = False
# Add modifiers like 'const'.
modifiers = ''
if ast_type.modifiers:
......@@ -66,59 +64,37 @@ def _RenderType(ast_type):
# Collect template args.
template_args = []
for arg in ast_type.templated_types:
rendered_arg, e = _RenderType(arg)
if e: has_multiarg_error = True
rendered_arg = _RenderType(arg)
template_args.append(rendered_arg)
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:
return_type += '*'
if ast_type.reference:
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
return return_type
def _GenerateMethods(output_lines, source, class_node):
function_type = (ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL |
ast.FUNCTION_OVERRIDE)
function_type = (
ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL | ast.FUNCTION_OVERRIDE)
ctor_or_dtor = ast.FUNCTION_CTOR | ast.FUNCTION_DTOR
indent = ' ' * _INDENT
for node in class_node.body:
# 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):
# Pick out all the elements we need from the original function.
const = ''
modifiers = 'override'
if node.modifiers & ast.FUNCTION_CONST:
const = 'CONST_'
num_parameters = _GetNumParameters(node.parameters, source)
modifiers = 'const, ' + modifiers
return_type = 'void'
if node.return_type:
return_type, has_multiarg_error = _RenderType(node.return_type)
if has_multiarg_error:
for line in [
'// 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)
return_type = _RenderType(node.return_type)
# commas mess with macros, so nest it in parens if it has one
if ',' in return_type:
return_type = '(' + return_type + ')'
args = ''
if node.parameters:
......@@ -137,11 +113,15 @@ def _GenerateMethods(output_lines, source, class_node):
# parameters together on a single line. Ensure there is a
# space in an argument which is split by a newline without
# intervening whitespace, e.g.: int\nBar
args = re.sub(' +', ' ', args_strings.replace('\n', ' '))
args_strings = 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.
output_lines.extend(['%s%s(%s,' % (indent, mock_method_macro, node.name),
'%s%s(%s));' % (indent * 3, return_type, args)])
output_lines.extend([
'%sMOCK_METHOD(%s, %s, (%s), (%s));' %
(indent, return_type, node.name, args, modifiers)
])
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