6262
6363
6464def _RenderType (ast_type ):
65- """Renders the potentially recursively templated type into a string.
65+ """Renders the potentially recursively templated type into a string.
6666
6767 Args:
6868 ast_type: The AST of the type.
@@ -71,97 +71,92 @@ def _RenderType(ast_type):
7171 Rendered string and a boolean to indicate whether we have multiple args
7272 (which is not handled correctly).
7373 """
74- has_multiarg_error = False
75- # Add modifiers like 'const'.
76- modifiers = ''
77- if ast_type .modifiers :
78- modifiers = ' ' .join (ast_type .modifiers ) + ' '
79- return_type = modifiers + ast_type .name
80- if ast_type .templated_types :
81- # Collect template args.
82- template_args = []
83- for arg in ast_type .templated_types :
84- rendered_arg , e = _RenderType (arg )
85- if e : has_multiarg_error = True
86- template_args .append (rendered_arg )
87- return_type += '<' + ', ' .join (template_args ) + '>'
88- # We are actually not handling multi-template-args correctly. So mark it.
89- if len (template_args ) > 1 :
90- has_multiarg_error = True
91- if ast_type .pointer :
92- return_type += '*'
93- if ast_type .reference :
94- return_type += '&'
95- return return_type , has_multiarg_error
74+ has_multiarg_error = False
75+ # Add modifiers like 'const'.
76+ modifiers = ''
77+ if ast_type .modifiers :
78+ modifiers = ' ' .join (ast_type .modifiers ) + ' '
79+ return_type = modifiers + ast_type .name
80+ if ast_type .templated_types :
81+ # Collect template args.
82+ template_args = []
83+ for arg in ast_type .templated_types :
84+ rendered_arg , e = _RenderType (arg )
85+ if e : has_multiarg_error = True
86+ template_args .append (rendered_arg )
87+ return_type += '<' + ', ' .join (template_args ) + '>'
88+ # We are actually not handling multi-template-args correctly. So mark it.
89+ if len (template_args ) > 1 :
90+ has_multiarg_error = True
91+ if ast_type .pointer :
92+ return_type += '*'
93+ if ast_type .reference :
94+ return_type += '&'
95+ return return_type , has_multiarg_error
9696
9797
9898def _GetNumParameters (parameters , source ):
99- num_parameters = len (parameters )
100- if num_parameters == 1 :
101- first_param = parameters [0 ]
102- if source [first_param .start :first_param .end ].strip () == 'void' :
103- # We must treat T(void) as a function with no parameters.
104- return 0
105- return num_parameters
99+ num_parameters = len (parameters )
100+ if num_parameters == 1 :
101+ first_param = parameters [0 ]
102+ if source [first_param .start :first_param .end ].strip () == 'void' :
103+ # We must treat T(void) as a function with no parameters.
104+ return 0
105+ return num_parameters
106106
107107
108108def _GenerateMethods (output_lines , source , class_node ):
109- function_type = (ast .FUNCTION_VIRTUAL | ast .FUNCTION_PURE_VIRTUAL |
110- ast .FUNCTION_OVERRIDE )
111- ctor_or_dtor = ast .FUNCTION_CTOR | ast .FUNCTION_DTOR
112- indent = ' ' * _INDENT
113-
114- for node in class_node .body :
115- # We only care about virtual functions.
116- if (isinstance (node , ast .Function ) and
117- node .modifiers & function_type and
118- not node .modifiers & ctor_or_dtor ):
119- # Pick out all the elements we need from the original function.
120- const = ''
121- if node .modifiers & ast .FUNCTION_CONST :
122- const = 'CONST_'
123- num_parameters = _GetNumParameters (node .parameters , source )
124- return_type = 'void'
125- if node .return_type :
126- return_type , has_multiarg_error = _RenderType (node .return_type )
127- if has_multiarg_error :
128- for line in [
129- '// The following line won\' t really compile, as the return' ,
130- '// type has multiple template arguments. To fix it, use a' ,
131- '// typedef for the return type.' ]:
132- output_lines .append (indent + line )
133- tmpl = ''
134- if class_node .templated_types :
135- tmpl = '_T'
136- mock_method_macro = 'MOCK_%sMETHOD%d%s' % (const , num_parameters , tmpl )
137-
138- args = ''
139- if node .parameters :
140- # Due to the parser limitations, it is impossible to keep comments
141- # while stripping the default parameters. When defaults are
142- # present, we choose to strip them and comments (and produce
143- # compilable code).
144- # TODO([email protected] ): Investigate whether it is possible to 145- # preserve parameter name when reconstructing parameter text from
146- # the AST.
147- if len ([param for param in node .parameters if param .default ]) > 0 :
148- args = ', ' .join (param .type .name for param in node .parameters )
149- else :
150- # Get the full text of the parameters from the start
151- # of the first parameter to the end of the last parameter.
152- start = node .parameters [0 ].start
153- end = node .parameters [- 1 ].end
154- # Remove // comments.
155- args_strings = re .sub (r'//.*' , '' , source [start :end ])
156- # Condense multiple spaces and eliminate newlines putting the
157- # parameters together on a single line. Ensure there is a
158- # space in an argument which is split by a newline without
159- # intervening whitespace, e.g.: int\nBar
160- args = re .sub (' +' , ' ' , args_strings .replace ('\n ' , ' ' ))
161-
162- # Create the mock method definition.
163- output_lines .extend (['%s%s(%s,' % (indent , mock_method_macro , node .name ),
164- '%s%s(%s));' % (indent * 3 , return_type , args )])
109+ function_type = (ast .FUNCTION_VIRTUAL | ast .FUNCTION_PURE_VIRTUAL |
110+ ast .FUNCTION_OVERRIDE )
111+ ctor_or_dtor = ast .FUNCTION_CTOR | ast .FUNCTION_DTOR
112+ indent = ' ' * _INDENT
113+
114+ for node in class_node .body :
115+ # We only care about virtual functions.
116+ if (isinstance (node , ast .Function ) and
117+ node .modifiers & function_type and
118+ not node .modifiers & ctor_or_dtor ):
119+ # Pick out all the elements we need from the original function.
120+ const = ''
121+ if node .modifiers & ast .FUNCTION_CONST :
122+ const = 'CONST_'
123+ num_parameters = _GetNumParameters (node .parameters , source )
124+ return_type = 'void'
125+ if node .return_type :
126+ return_type , has_multiarg_error = _RenderType (node .return_type )
127+ if has_multiarg_error :
128+ for line in [
129+ '// The following line won\' t really compile, as the return' ,
130+ '// type has multiple template arguments. To fix it, use a' ,
131+ '// typedef for the return type.' ]:
132+ output_lines .append (indent + line )
133+ tmpl = ''
134+ if class_node .templated_types :
135+ tmpl = '_T'
136+ mock_method_macro = 'MOCK_%sMETHOD%d%s' % (const , num_parameters , tmpl )
137+
138+ args = ''
139+ if node .parameters :
140+ # Get the full text of the parameters from the start
141+ # of the first parameter to the end of the last parameter.
142+ start = node .parameters [0 ].start
143+ end = node .parameters [- 1 ].end
144+ # Remove // comments.
145+ args_strings = re .sub (r'//.*' , '' , source [start :end ])
146+ # Remove /* comments */.
147+ args_strings = re .sub (r'/\*.*\*/' , '' , args_strings )
148+ # Remove default arguments.
149+ args_strings = re .sub (r'=.*,' , ',' , args_strings )
150+ args_strings = re .sub (r'=.*' , '' , args_strings )
151+ # Condense multiple spaces and eliminate newlines putting the
152+ # parameters together on a single line. Ensure there is a
153+ # space in an argument which is split by a newline without
154+ # intervening whitespace, e.g.: int\nBar
155+ args = re .sub (' +' , ' ' , args_strings .replace ('\n ' , ' ' ))
156+
157+ # Create the mock method definition.
158+ output_lines .extend (['%s%s(%s,' % (indent , mock_method_macro , node .name ),
159+ '%s%s(%s));' % (indent * 3 , return_type , args )])
165160
166161
167162def _GenerateMocks (filename , source , ast_list , desired_class_names ):
0 commit comments