diff --git a/pEpACIDgen/c_2_ast.py b/pEpACIDgen/c_2_ast.py index 3483bdb..ac969ad 100644 --- a/pEpACIDgen/c_2_ast.py +++ b/pEpACIDgen/c_2_ast.py @@ -18,7 +18,7 @@ class C2AST: def parse(self, filename, content=None, follow_includes=False): index = clang.cindex.Index.create() - arguments = ["-x", "c"] + arguments = ["-x", "c", "-I/Users/heck/local-default/include/"] options = clang.cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES if content: content = [(filename, content)] diff --git a/tests/test_pEpACIDgen.py b/tests/test_pEpACIDgen.py index 8c94ad0..d1fba21 100755 --- a/tests/test_pEpACIDgen.py +++ b/tests/test_pEpACIDgen.py @@ -2,164 +2,179 @@ # -*- coding: utf-8 -*- # This file is under GNU Affero General Public License 3.0 # see LICENSE.txt +import re import pytest - - import pEpACIDgen -types_with_alias = [] -types_no_alias = [] -types_no_alias.append({"var": ["_P"], - "type_names": [ - ]}) - -types_no_alias.append({"var": ["_E"], - "type_names": [ - "enum _E" - ]}) - -types_with_alias.append({"var": ["E"], - "type_names": [ - "enum E" - ]}) - -types_no_alias.append({"var": ["_PS"], - "type_names": [ - "struct _PS" - ]}) - -types_with_alias.append({"var": ["PS"], - "type_names": [ - "struct PS" - ]}) - -types_no_alias.append({"var": ["_CS"], - "type_names": [ - "struct _CS", - "struct PS", - "struct _PS" - ]}) - -types_with_alias.append({"var": ["CS"], - "type_names": [ - "struct CS", - "struct _PS", - "struct PS" - ]}) - -types_no_alias.append({"var": ["_CCS"], - "type_names": [ - "struct _CCS", - "struct CS", - "struct _PS", - "struct PS" - ]}) - -types_with_alias.append({"var": ["CCS"], - "type_names": [ - "struct CCS", - "struct CS", - "struct _PS", - "struct PS" - ]}) - -types_no_alias.append({"var": ["_HS"], - "type_names": [ - "struct _HS", - "struct _NCS", - "struct CS", - "struct _PS", - "struct PS" - ]}) - -types_with_alias.append({"var": ["HS"], - "type_names": [ - "struct HS", - "struct _NPS" - ]}) - -types_no_alias.append({"var": ["_NCS"], - "type_names": [ - "struct _NCS", - "struct CS", - "struct _PS", - "struct PS" - ]}) - -types_no_alias.append({"var": ["_NPS"], - "type_names": [ - "struct _NPS" - ]}) - -types_no_alias.append({"var": ["_HHS"], - "type_names": [ - "struct _HHS", - "struct _NHS", - "struct _NNPS" - ]}) - -types_with_alias.append({"var": ["HHS"], - "type_names": [ - "struct HHS", - "struct _NHS1", - "struct _NNCS", - "struct _CS", - "struct PS", - "struct _PS", - "enum _NENHS" - ]}) - -types_no_alias.append({"var": ["_NHS"], - "type_names": [ - "struct _NHS", - "struct _NNPS" - ]}) - -types_no_alias.append({"var": ["_NNPS"], - "type_names": [ - "struct _NNPS" - ]}) - -types_no_alias.append({"var": ["_NHS1"], - "type_names": [ - "struct _NHS1", - "struct _NNCS", - "struct _CS", - "struct PS", - "struct _PS", - "enum _NENHS" - ]}) - -types_no_alias.append({"var": ["_NNCS"], - "type_names": [ - "struct _NNCS", - "struct _CS", - "struct PS", - "struct _PS" - ]}) - -types_no_alias.append({"var": ["_NENHS"], - "type_names": [ - "enum _NENHS" - ]}) - -types_no_alias.append({"var": ["_HS1"], - "type_names": [ - "struct _HS1", - "enum _NEHS" - ]}) - -types_no_alias.append({"var": ["_NEHS"], - "type_names": [ - "enum _NEHS" - ]}) +# "type" - is the name of the type for which vars of all variations of this type exists +# (Variations being combinations of this type with qualifier alias and typedef, +# Which means that all variations resolve to the same underlying type) +# +# "requires" - is the list of types that this underlying type depends on +# (a type is not self sufficient, a struct for instance can have members of plenty of types, etc...) +# +# Subject of this test is to make sure: +# All variations for all the "type"s given extract the ACID of all the types, +# and only the types specified on "requires". + +all_types = [] + +all_types.append({"type": ["_P"], + "requires": [ + ]}) + +all_types.append({"type": ["_E"], + "requires": [ + "enum _E" + ]}) + +all_types.append({"type": ["E"], + "requires": [ + "enum E" + ]}) + +# all_types.append({"type": ["XE"], +# "requires": [ +# "AXE" +# ]}) + +all_types.append({"type": ["_PS"], + "requires": [ + "struct _PS" + ]}) + +all_types.append({"type": ["PS"], + "requires": [ + "struct PS" + ]}) + +all_types.append({"type": ["_CS"], + "requires": [ + "struct _CS", + "struct PS", + "struct _PS" + ]}) + +all_types.append({"type": ["CS"], + "requires": [ + "struct CS", + "struct _PS", + "struct PS" + ]}) + +all_types.append({"type": ["_CCS"], + "requires": [ + "struct _CCS", + "struct CS", + "struct _PS", + "struct PS" + ]}) + +all_types.append({"type": ["CCS"], + "requires": [ + "struct CCS", + "struct CS", + "struct _PS", + "struct PS" + ]}) + +all_types.append({"type": ["_HS"], + "requires": [ + "struct _HS", + "struct _NCS", + "struct CS", + "struct _PS", + "struct PS" + ]}) + +all_types.append({"type": ["HS"], + "requires": [ + "struct HS", + "struct _NPS" + ]}) + +all_types.append({"type": ["_NCS"], + "requires": [ + "struct _NCS", + "struct CS", + "struct _PS", + "struct PS" + ]}) + +all_types.append({"type": ["_NPS"], + "requires": [ + "struct _NPS" + ]}) + +all_types.append({"type": ["_HHS"], + "requires": [ + "struct _HHS", + "struct _NHS", + "struct _NNPS" + ]}) + +all_types.append({"type": ["HHS"], + "requires": [ + "struct HHS", + "struct _NHS1", + "struct _NNCS", + "struct _CS", + "struct PS", + "struct _PS", + "enum _NENHS" + ]}) + +all_types.append({"type": ["_NHS"], + "requires": [ + "struct _NHS", + "struct _NNPS" + ]}) + +all_types.append({"type": ["_NNPS"], + "requires": [ + "struct _NNPS" + ]}) + +all_types.append({"type": ["_NHS1"], + "requires": [ + "struct _NHS1", + "struct _NNCS", + "struct _CS", + "struct PS", + "struct _PS", + "enum _NENHS" + ]}) + +all_types.append({"type": ["_NNCS"], + "requires": [ + "struct _NNCS", + "struct _CS", + "struct PS", + "struct _PS" + ]}) + +all_types.append({"type": ["_NENHS"], + "requires": [ + "enum _NENHS" + ]}) + +all_types.append({"type": ["_HS1"], + "requires": [ + "struct _HS1", + "enum _NEHS" + ]}) + +all_types.append({"type": ["_NEHS"], + "requires": [ + "enum _NEHS" + ]}) def resolve_vars_and_funcs_to_acid(vars, funcs): header_filename = "examples/lib/lib_test/lib_test.h" libclang_path = "/opt/local/libexec/llvm-9.0/lib/libclang.dylib" acidgen = pEpACIDgen.pEpACIDGen(libclang_path, header_filename, "test_pEpACIDgen") - header = acidgen.extract(vars,funcs) + header = acidgen.extract(vars, funcs) return header["acid"] @@ -191,31 +206,46 @@ def check_expected_types_resolved(vars, funcs, type_names_expected): assert type_names == type_names_expected -@pytest.mark.skip -@pytest.mark.parametrize('vars', [["var_TP"]]) -@pytest.mark.parametrize('funcs', [""]) -def test_manual(vars, funcs): - resolve_vars_and_funcs_to_acid(vars, funcs) - - -# @pytest.mark.skip +# Variations +# a var var_X of a type X can generally exist in the variations: +# * X var_X +# * AX var_AX +# * TX var_TX +# * TAX var_TAX +# * TTX var_TTX +# * TTAX var_TTAX +# AND the type of ALL these vars resolves to the same underlying type X +# +# types with no alias +# type with no alias can only exists in variations NOT ending with A +# +# Anonymous types (enum only) +# an anonymous type only exists because of its alias typedef enum { a;b;c } XE; +# Therefore, only variations ending with A are valid @pytest.mark.parametrize('variation', ["var_", "var_A", "var_T", "var_TA", "var_TT", "var_TTA"]) -@pytest.mark.parametrize('data', types_with_alias) -def test_single_var_with_alias(data, variation): - var_list = [] - for var in data["var"]: - var_name = variation + var - var_list.append(var_name) - - check_expected_types_resolved(var_list, [], data["type_names"]) - - -@pytest.mark.parametrize('variation', ["var_", "var_T", "var_TT"]) -@pytest.mark.parametrize('data', types_no_alias) +@pytest.mark.parametrize('data', all_types) def test_single_var_no_alias(data, variation): - var_list = [] - for var in data["var"]: - var_name = variation + var - var_list.append(var_name) - - check_expected_types_resolved(var_list, [], data["type_names"]) + type = data["type"][0] + var_name = variation + type + + # it the type begins with _ it has no alias + re_type_no_alias = re.compile("^_") + # if the variation ends with a is an alias of something + re_vari_alias = re.compile("A$") + # if the type contains an X is is an anonymous type + re_type_anonynmous = re.compile("X") + + skipmsg = "variation not valid:" + variation + " for type:" + type + re_vari_invalid_for_anon = re.compile("") + if re_type_no_alias.search(type): + if re_vari_alias.search(variation): + print(skipmsg) + pytest.skip(skipmsg) + + # the anonymous type only exists because of its alias + if re_type_anonynmous.search(type): + if not re_vari_alias.search(variation): + print(skipmsg) + pytest.skip(skipmsg) + + check_expected_types_resolved([var_name], [], data["requires"])