def test_preprocess_with_if(self):
     some_content_2 = mock.MagicMock()
     if_ = pre_ast.If(
         conditional_blocks=[
             pre_ast.ConditionalBlock(
                 conditional_expression='some_expression_1',
                 content='some_content_1',
             ),
             pre_ast.ConditionalBlock(
                 conditional_expression='some_expression_2',
                 content=some_content_2,
             ),
             pre_ast.ConditionalBlock(
                 conditional_expression='some_expression_3',
                 content='some_content_3',
             ),
         ],
         else_content='else_content',
     )
     self.expression_evaluator.evaluate.side_effect = (
         c_ast.CNumber(0),
         c_ast.CNumber(1),
     )
     some_content_2.accept.return_value = 24
     actual = self.preprocessing_visitor.preprocess(if_)
     self.assertEqual(actual, 24)
Example #2
0
 def test_collect_types_with_if(self):
   content1 = [
       c_ast.CTypeDefinition('type_name_1', 'type_definition_1'),
   ]
   content2 = [
       c_ast.CTypeDefinition('type_name_2', 'type_definition_2'),
       c_ast.CTypeDefinition('type_name_3', 'type_definition_3'),
   ]
   content3 = [
       c_ast.CTypeDefinition('type_name_4', 'type_definition_4'),
   ]
   else_content = [
       c_ast.CTypeDefinition('type_name_5', 'type_definition_5'),
   ]
   if_ = pre_ast.If(
       conditional_blocks=[
           pre_ast.ConditionalBlock('expression1', content1),
           pre_ast.ConditionalBlock('expression2', content2),
           pre_ast.ConditionalBlock('expression3', content3),
       ],
       else_content=else_content,
   )
   self.expression_evaluator.evaluate.side_effect = (
       c_ast.CNumber(0),
       c_ast.CNumber(1),
   )
   actual = self.type_collector.visit_if(if_)
   expected = {
       'type_name_2': 'type_definition_2',
       'type_name_3': 'type_definition_3',
   }
   self.assertEqual(actual, expected)
 def test_parse_with_empty_if_and_elif_blocks_and_expressions(self):
     source = """
     #if CONFIG_SOMETHING == 32
     #elif CONFIG_SOMETHING == 64
     #endif
     """
     actual = self.parser.parse(source)
     expected = pre_ast.File(
         pre_ast.CompositeBlock([
             pre_ast.If(conditional_blocks=[
                 pre_ast.ConditionalBlock(
                     conditional_expression=c_ast.CFunctionCall(
                         function_name='==',
                         arguments=[
                             c_ast.CVariable('CONFIG_SOMETHING'),
                             c_ast.CNumber(32),
                         ],
                     ),
                     content=pre_ast.CompositeBlock([]),
                 ),
                 pre_ast.ConditionalBlock(
                     conditional_expression=c_ast.CFunctionCall(
                         function_name='==',
                         arguments=[
                             c_ast.CVariable('CONFIG_SOMETHING'),
                             c_ast.CNumber(64),
                         ],
                     ),
                     content=pre_ast.CompositeBlock([]),
                 ),
             ], ),
         ]), )
     self.assertEqual(actual, expected)
 def test_parse_with_empty_if_elif_and_else_blocks(self):
     source = """
     #if CONFIG_SOMETHING
     #elif defined(CONFIG_SOMETHING_ELSE)
     #else
     #endif
     """
     actual = self.parser.parse(source)
     expected = pre_ast.File(
         pre_ast.CompositeBlock([
             pre_ast.If(
                 conditional_blocks=[
                     pre_ast.ConditionalBlock(
                         conditional_expression=c_ast.CVariable(
                             name='CONFIG_SOMETHING', ),
                         content=pre_ast.CompositeBlock([]),
                     ),
                     pre_ast.ConditionalBlock(
                         conditional_expression=c_ast.CFunctionCall(
                             function_name='defined',
                             arguments=[
                                 c_ast.CVariable('CONFIG_SOMETHING_ELSE'),
                             ],
                         ),
                         content=pre_ast.CompositeBlock([]),
                     )
                 ],
                 else_content=pre_ast.CompositeBlock([]),
             ),
         ]), )
     self.assertEqual(actual, expected)
Example #5
0
 def test_get_active_content(self):
     conditional_blocks = [
         pre_ast.ConditionalBlock('expression1', 'content1'),
         pre_ast.ConditionalBlock('expression2', 'content2'),
         pre_ast.ConditionalBlock('expression3', 'content3'),
     ]
     if_ = pre_ast.If(conditional_blocks, 'else_content')
     expression_evaluator = mock.MagicMock()
     expression_evaluator.evaluate.side_effect = (
         c_ast.CNumber(0),
         c_ast.CNumber(1),
     )
     actual = if_.get_active_content(expression_evaluator)
     self.assertEqual(actual, 'content2')
 def test_parse_with_top_level_if_elif_and_else_blocks(self):
     source = '\n'.join((
         'int a;',
         '#if CONFIG_SOMETHING',
         'struct s {',
         '  int x;',
         '} y;',
         '#elif defined(CONFIG_SOMETHING_ELSE)',
         'struct s t, u;',
         '#else',
         'int z;',
         '#endif',
         'int b;',
     ))
     actual = self.parser.parse(source)
     expected = pre_ast.File(content=pre_ast.CompositeBlock([
         pre_ast.TextBlock('int a;'),
         pre_ast.If(
             conditional_blocks=[
                 pre_ast.ConditionalBlock(
                     conditional_expression=c_ast.CVariable(
                         name='CONFIG_SOMETHING', ),
                     content=pre_ast.CompositeBlock([
                         pre_ast.TextBlock(
                             '\n'.join((
                                 'struct s {',
                                 '  int x;',
                                 '} y;',
                             )), ),
                     ]),
                 ),
                 pre_ast.ConditionalBlock(
                     conditional_expression=c_ast.CFunctionCall(
                         function_name='defined',
                         arguments=[
                             c_ast.CVariable('CONFIG_SOMETHING_ELSE'),
                         ],
                     ),
                     content=pre_ast.CompositeBlock(
                         [pre_ast.TextBlock('struct s t, u;')]),
                 ),
             ],
             else_content=pre_ast.CompositeBlock(
                 [pre_ast.TextBlock('int z;')]),
         ),
         pre_ast.TextBlock('int b;'),
     ]), )
     self.assertEqual(actual, expected)
Example #7
0
 def construct_elif_block(expression_string, content):
   expression_parse_result = expression.parseString(
       expression_string,
       parseAll=True,
   )
   conditional_expression = expression_parse_result[0]
   return pre_ast.ConditionalBlock(conditional_expression, content)
 def test_parse_with_empty_ifndef_header_guard(self):
     source = """
     #ifndef _SOMETHING_H
     #define _SOMETHING_H
     #endif
     """
     actual = self.parser.parse(source)
     expected = pre_ast.File(
         pre_ast.CompositeBlock([
             pre_ast.If([
                 pre_ast.ConditionalBlock(
                     conditional_expression=c_ast.CFunctionCall(
                         function_name='!',
                         arguments=[
                             c_ast.CFunctionCall(
                                 function_name='defined',
                                 arguments=[
                                     c_ast.CVariable('_SOMETHING_H')
                                 ]),
                         ],
                     ),
                     content=pre_ast.CompositeBlock([
                         pre_ast.DefineObjectLike(
                             name='_SOMETHING_H',
                             replacement=None,
                             string_replacement='',
                         ),
                     ]),
                 ),
             ]),
         ]), )
     self.assertEqual(actual, expected)
 def test_parse_with_empty_ifndef_block_and_endif_with_comment(self):
     source = """
     #ifndef _SOMETHING_H
     #endif /* _SOMETHING_H */
     """
     actual = self.parser.parse(source)
     expected = pre_ast.File(
         pre_ast.CompositeBlock([
             pre_ast.If([
                 pre_ast.ConditionalBlock(
                     conditional_expression=c_ast.CFunctionCall(
                         function_name='!',
                         arguments=[
                             c_ast.CFunctionCall(
                                 function_name='defined',
                                 arguments=[
                                     c_ast.CVariable('_SOMETHING_H')
                                 ]),
                         ],
                     ),
                     content=pre_ast.CompositeBlock([]),
                 ),
             ]),
         ]), )
     self.assertEqual(actual, expected)
Example #10
0
 def test_collect_includes_with_conditional_block(self):
     mock_node = mock.MagicMock()
     node = pre_ast.ConditionalBlock(
         conditional_expression='some_expression',
         content=mock_node,
     )
     mock_node.accept.return_value = 33
     actual = self.include_collector.collect_includes(node)
     expected = 33
     self.assertEqual(actual, expected)
 def test_resolve_with_conditional_block(self):
     mock_node = mock.MagicMock()
     node = pre_ast.ConditionalBlock(
         conditional_expression='some_expression',
         content=mock_node,
     )
     self.include_linking_visitor.resolve(node, self.files)
     mock_node.accept.assert_called_once_with(
         self.include_linking_visitor,
         self.files,
     )
 def test_parse_if_with_comments(self):
     source = """
     #if 0 /* foo bar */
     /* 42 */
     #endif
     """
     actual = self.parser.parse(source)
     expected = pre_ast.File(content=pre_ast.CompositeBlock([
         pre_ast.If(conditional_blocks=[
             pre_ast.ConditionalBlock(
                 conditional_expression=c_ast.CNumber(0),
                 content=pre_ast.CompositeBlock([]),
             ),
         ], ),
     ]), )
     self.assertEqual(actual, expected)
 def test_parse_with_ifdef_with_comment(self):
     source = '\n'.join((
         '#ifdef CONFIG_SOMETHING /* foo 42 */',
         '#endif',
     ))
     actual = self.parser.parse(source)
     expected = pre_ast.File(content=pre_ast.CompositeBlock([
         pre_ast.If(conditional_blocks=[
             pre_ast.ConditionalBlock(
                 conditional_expression=c_ast.CFunctionCall(
                     function_name='defined',
                     arguments=[c_ast.CVariable('CONFIG_SOMETHING')],
                 ),
                 content=pre_ast.CompositeBlock([]),
             ),
         ], ),
     ]), )
     self.assertEqual(actual, expected)