Beispiel #1
0
 def test_read_onPredefinedMacroDict_doesNotModifyPredefinedMacroDict(
         self, tmpdir):
     c_file = tmpdir.join('predef_macro.c')
     c_file.write_binary(b'#define B')
     predef_macros = dict(A=1)
     parser = CParser(predef_macros)
     parser.read(Path(c_file))
     assert predef_macros == dict(A=1)
Beispiel #2
0
 def test_read_onAdditionalIncludeDirs_searchIncludeDirs(self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include "test.h"',
             'sub dir': {
                 'test.h': b'int func(void);'
             }
         })
     parser = CParser(include_dirs=[basedir / 'sub dir'])
     parser.read(basedir / 'test.c')
     assert 'func' in parser.funcs
Beispiel #3
0
 def test_read_onIncludes_addsIncludeFileNamesToSourceFiles(self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'source.c': b'#include "include_1.h"',
             'include_1.h': b'#include "include_2.h"',
             'include_2.h': b''
         })
     parser = CParser()
     parser.read(basedir / 'source.c')
     assert parser.source_files \
            == { basedir / 'source.c',
                 basedir / 'include_1.h',
                 basedir / 'include_2.h'}
Beispiel #4
0
 def test_read_onSystemIncludeDirs_searchPassedSysIncludeDirsAndIgnoreFuncsAndVarsAndSourceFiles(
         self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include <test.h>',
             'sys-incl': {
                 'test.h': b'int func(void);\n'
                 b'extern int var;'
             }
         })
     parser = CParser(sys_include_dirs=[basedir / 'sys-incl'])
     parser.read(basedir / 'test.c')
     assert 'func' not in parser.funcs
     assert 'var' not in parser.vars
     assert parser.source_files == {basedir / 'test.c'}
Beispiel #5
0
 def test_read_onWhitelistedSystemHeaderEntries_doNotIgnore(self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include <test.h>\n',
             'sys-incl': {
                 'test.h':
                 b'struct strct { };\n'
                 b'#define MACRO\n'
                 b'void func(void);\n'
             }
         })
     parser = CParser(sys_include_dirs=[basedir / 'sys-incl'],
                      sys_whitelist=['strct', 'MACRO', 'func'])
     parser.read(basedir / 'test.c')
     assert 'strct' in parser.structs
     assert 'MACRO' in parser.macros
     assert 'func' in parser.funcs
Beispiel #6
0
 def parse(cls,
           content,
           patches=None,
           sys_include_dirs=None,
           target_compiler=None,
           **predef_macros):
     parser = CParser(predef_macros, [],
                      sys_include_dirs,
                      target_compiler=target_compiler)
     fileobj = NamedTemporaryFile(suffix='.c', delete=False, mode='w+t')
     try:
         fileobj.write(content)
         fileobj.close()
         parser.read(fileobj.name, patches)
     finally:
         os.remove(fileobj.name)
     return parser
Beispiel #7
0
 def test_read_onSystemHeaderFile_ignoresAllEntriesExceptRequiredTypes(
         self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include <test.h>\n'
             b'void func(req_type * param);\n',
             'sys-incl': {
                 'test.h':
                 b'typedef struct req_struct { } req_type;\n'
                 b'typedef struct not_req_struct { } not_req_type;\n'
                 b'void sys_func(not_req_type * param);\n'
             }
         })
     parser = CParser(sys_include_dirs=[basedir / 'sys-incl'])
     parser.read(basedir / 'test.c')
     assert 'req_type' in parser.typedefs, \
         'a type required by module under test was not parsed'
     assert 'not_req_type' not in parser.typedefs, \
         'a not required by module under test was parsed'
     assert 'sys_func' not in parser.funcs, \
         'a system function was parsed'
Beispiel #8
0
 def assert_parses(self,
                   srccode,
                   exp_typedefs=None,
                   exp_structs=None,
                   exp_macro_locs=None,
                   exp_funcs=None,
                   exp_vars=None,
                   exp_impls=None):
     parser = CParser()
     exp_typedefs = exp_typedefs or {}
     exp_typedefs.update(parser.typedefs.copy())  # add builtin typedefs
     tu = TranslationUnit.from_source(
         'test.c',
         unsaved_files=[('test.c', srccode)],
         options=TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)
     if tu.diagnostics:
         raise Exception(tu.diagnostics[0].spelling)
     parser.read_from_cursor(tu.cursor)
     assert parser.typedefs == exp_typedefs
     assert parser.structs == (exp_structs or {})
     assert parser.macro_locs == (exp_macro_locs or {})
     assert parser.funcs == (exp_funcs or {})
     assert parser.vars == (exp_vars or {})
     assert parser.implementations == (exp_impls or set())
Beispiel #9
0
 def test_read_onNotExistingFile_raisesFileNotFoundError(self):
     parser = CParser()
     with pytest.raises(FileNotFoundError):
         parser.read('invalid-file-name')
Beispiel #10
0
 def test_read_onNotExistinFile_raisesFileNotFoundError(self):
     with pytest.raises(FileNotFoundError):
         parser = CParser()
         parser.read('not_existing_file.c')