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): ...@@ -53,10 +53,8 @@ def _RenderType(ast_type):
ast_type: The AST of the type. ast_type: The AST of the type.
Returns: Returns:
Rendered string and a boolean to indicate whether we have multiple args Rendered string of the type
(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:
...@@ -66,59 +64,37 @@ def _RenderType(ast_type): ...@@ -66,59 +64,37 @@ 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, e = _RenderType(arg) rendered_arg = _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, has_multiarg_error return return_type
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 = (ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL | function_type = (
ast.FUNCTION_OVERRIDE) ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL | 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 if (isinstance(node, ast.Function) and node.modifiers & function_type 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.
const = '' modifiers = 'override'
if node.modifiers & ast.FUNCTION_CONST: if node.modifiers & ast.FUNCTION_CONST:
const = 'CONST_' modifiers = 'const, ' + modifiers
num_parameters = _GetNumParameters(node.parameters, source)
return_type = 'void' return_type = 'void'
if node.return_type: if node.return_type:
return_type, has_multiarg_error = _RenderType(node.return_type) return_type = _RenderType(node.return_type)
if has_multiarg_error: # commas mess with macros, so nest it in parens if it has one
for line in [ if ',' in return_type:
'// The following line won\'t really compile, as the return', return_type = '(' + return_type + ')'
'// 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:
...@@ -137,11 +113,15 @@ def _GenerateMethods(output_lines, source, class_node): ...@@ -137,11 +113,15 @@ 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 = 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. # Create the mock method definition.
output_lines.extend(['%s%s(%s,' % (indent, mock_method_macro, node.name), output_lines.extend([
'%s%s(%s));' % (indent * 3, return_type, args)]) '%sMOCK_METHOD(%s, %s, (%s), (%s));' %
(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