Merged PR 4649: Removed constructor_name and minor changes

Removed constructor_name from the .SimpleSchema, made ENUMS be unsupported, fixed the empty name that would be caused by a default constructor.
This commit is contained in:
Teo Magnino Chaban 2019-07-03 16:19:12 +00:00
Родитель 4b9812696a
Коммит 5a2026eba4
5 изменённых файлов: 25 добавлений и 130 удалений

Просмотреть файл

@ -22,7 +22,6 @@
<definition_line int min=1 ?>
<constructor_list *>:
<constructor_name string>
<var_names string *>
<raw_var_types string *>
<simple_var_types string *>

Просмотреть файл

@ -119,6 +119,7 @@ def ObtainFunctions(
cursor = translation_unit.cursor
# ----------------------------------------------------------------------
def GetAlias():
"""
This function will process all 'typedef' and 'using' and it will map the underlying type to
@ -130,6 +131,8 @@ def ObtainFunctions(
alias[child.spelling] = child.underlying_typedef_type.spelling
return alias
# ----------------------------------------------------------------------
alias = GetAlias()
alias_regex = re.compile(
@ -152,7 +155,6 @@ def ObtainFunctions(
)
# ----------------------------------------------------------------------
def FullVarType(types):
"""
This will undo all 'typedef' and 'using' by looking for the items in the 'alias' dict and substituting
@ -164,6 +166,7 @@ def ObtainFunctions(
types = struct_class_pattern.sub(r'', types)
return types
# ----------------------------------------------------------------------
object_type_list = []
@ -185,19 +188,11 @@ def ObtainFunctions(
on_unsupported_func(_FullName(node), filename if (not is_temp_file or filename != input_filename) else None, node.location.line)
# ----------------------------------------------------------------------
def EnumerateFuncs(node):
if node.kind == cindex.CursorKind.NAMESPACE:
for child in node.get_children():
EnumerateFuncs(child)
if node.kind == cindex.CursorKind.CONSTRUCTOR:
"""
TODO: This will support a constructor outside a struct/class. This functionality
will be implemented when multiple files are supported (next PR).
[EDIT]: This is proving to be much harder than expected. This will get a PR for itself later.
"""
if node.kind == cindex.CursorKind.FUNCTION_DECL and node.location.file.name == filename:
ret_type = FullVarType(node.result_type.spelling)
func = Function(_FullName(node), ret_type, SimpleVarType(ret_type))
@ -234,6 +229,7 @@ def ObtainFunctions(
for child in translation_unit.get_includes():
include_list.append((os.path.realpath(str(child.location.file.name)) if (not is_temp_file or child.location.file.name != input_filename) else None, os.path.realpath(os.path.join(filename, str(child.include)))))
return include_list
# ----------------------------------------------------------------------
include_list = GetIncludeList(filename)
@ -241,10 +237,9 @@ def ObtainFunctions(
return {"function_list": function_list, "object_type_list": object_type_list, "include_list": include_list}
# ----------------------------------------------------------------------
clean_results = OrderedDict()
# ----------------------------------------------------------------------
def InitializeCleanResults(filename, raw_includes):
clean_results[filename] = Results()
for include_tuple in raw_includes:
@ -290,12 +285,12 @@ def ObtainFunctions(
if obj_type in needed_obj_type_list:
return True
for var_type in obj_type.EnumerateSimpleVarTypes():
if not TestAndVerify(var_type) and GetObjType(var_type) != obj_type and not IsValidObjType(GetObjType(var_type)):
if GetObjType(var_type) != obj_type and not IsValidObjType(GetObjType(var_type)) and not TestAndVerify(var_type):
invalid_obj_type_list.append(obj_type)
return False
for constructor in obj_type.constructor_list:
for arg_type in constructor.EnumerateSimpleVarTypes():
if not TestAndVerify(arg_type) and GetObjType(arg_type) != obj_type and not IsValidObjType(GetObjType(arg_type)):
if GetObjType(arg_type) != obj_type and not IsValidObjType(GetObjType(arg_type)) and not TestAndVerify(arg_type):
invalid_obj_type_list.append(obj_type)
return False
@ -322,7 +317,7 @@ def ObtainFunctions(
return False
return True
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
for func in these_results["function_list"]:
if IsValidFunc(func):
@ -505,6 +500,10 @@ class Constructor(_FuncWithArguments):
# ----------------------------------------------------------------------
def ToDict(self):
new_dict = super(Constructor, self).ToDict()
# If the var name is "", it means that this is a default constructor.
if len(new_dict["var_names"]) == 1 and not new_dict["var_names"][0]:
new_dict["var_names"] = ["other"]
new_dict["definition_line"] = self._definition_line
@ -567,7 +566,7 @@ def _GetObjectType(node, SimpleVarType, FullVarType):
# The way to see if this is a definition or not, is to see if 'node' has any children.
is_def = True
# There are a few kinds that are supported, even though they are not directly exposed.
accepted_kinds = [cindex.CursorKind.CXX_ACCESS_SPEC_DECL, cindex.CursorKind.ENUM_DECL]
accepted_kinds = [cindex.CursorKind.CXX_ACCESS_SPEC_DECL]
# ----------------------------------------------------------------------
def DeleteDefault(node, speficier):
@ -600,9 +599,8 @@ def _GetObjectType(node, SimpleVarType, FullVarType):
has_move_constructor = True
if DeleteDefault(child, "default"):
# If this is a default move constructor, there wont be a variable name for the
# argument in the function, so I create one.
# argument in the function, so I create one when I return the dictionary representation.
assert constructor.VariableLen() == 1
# TODO: constructor['arg_names'] = ['other']
elif child.is_copy_constructor() and child.access_specifier == cindex.AccessSpecifier.PUBLIC:
# No public copy constructors are allowed.

Просмотреть файл

@ -5,7 +5,7 @@
# | future invocations of the generator!
# |
# | Generated by: <SimpleSchemaGenerator>/Plugins/Impl/PythonSerializationImpl.py
# | Generated on: 2019-07-02 08:44:54.732331
# | Generated on: 2019-07-02 14:47:04.572330
# |
# --------------------------------------------------------------------------------
import copy
@ -150,12 +150,12 @@ def Deserialize_FileInfo(
# | Type Infos
# |
# ----------------------------------------------------------------------
FileInfo_TypeInfo = ClassTypeInfo(OrderedDict([ ( "function_list", ClassTypeInfo(OrderedDict([ ( "func_name", StringTypeInfo(min_length=1) ), ( "raw_return_type", StringTypeInfo(min_length=1) ), ( "simple_return_type", StringTypeInfo(min_length=1) ), ( "var_names", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "raw_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "simple_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "declaration_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ), ( "definition_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ) ]), require_exact_match=True, arity=Arity.FromString('*')) ), ( "struct_list", ClassTypeInfo(OrderedDict([ ( "struct_name", StringTypeInfo(min_length=1) ), ( "var_names", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "raw_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "simple_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "definition_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ), ( "constructor_list", ClassTypeInfo(OrderedDict([ ( "constructor_name", StringTypeInfo(min_length=1) ), ( "var_names", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "raw_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "simple_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "definition_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ) ]), require_exact_match=True, arity=Arity.FromString('*')) ) ]), require_exact_match=True, arity=Arity.FromString('*')) ), ( "include_list", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ) ]), require_exact_match=True, arity=Arity.FromString('*'))
FileInfo_TypeInfo = ClassTypeInfo(OrderedDict([ ( "function_list", ClassTypeInfo(OrderedDict([ ( "func_name", StringTypeInfo(min_length=1) ), ( "raw_return_type", StringTypeInfo(min_length=1) ), ( "simple_return_type", StringTypeInfo(min_length=1) ), ( "var_names", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "raw_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "simple_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "declaration_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ), ( "definition_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ) ]), require_exact_match=True, arity=Arity.FromString('*')) ), ( "struct_list", ClassTypeInfo(OrderedDict([ ( "struct_name", StringTypeInfo(min_length=1) ), ( "var_names", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "raw_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "simple_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "definition_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ), ( "constructor_list", ClassTypeInfo(OrderedDict([ ( "var_names", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "raw_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "simple_var_types", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ), ( "definition_line", IntTypeInfo(min=1, arity=Arity.FromString('?')) ) ]), require_exact_match=True, arity=Arity.FromString('*')) ) ]), require_exact_match=True, arity=Arity.FromString('*')) ), ( "include_list", StringTypeInfo(min_length=1, arity=Arity.FromString('*')) ) ]), require_exact_match=True, arity=Arity.FromString('*'))
_FileInfo_TypeInfo_Contents = OrderedDict([("function_list", GenericTypeInfo(arity=Arity.FromString('*'))), ("struct_list", GenericTypeInfo(arity=Arity.FromString('*'))), ("include_list", GenericTypeInfo(arity=Arity.FromString('*')))])
_FileInfo_function_list_TypeInfo_Contents = OrderedDict([("func_name", GenericTypeInfo()), ("raw_return_type", GenericTypeInfo()), ("simple_return_type", GenericTypeInfo()), ("var_names", GenericTypeInfo(arity=Arity.FromString('*'))), ("raw_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("simple_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("declaration_line", GenericTypeInfo(arity=Arity.FromString('?'))), ("definition_line", GenericTypeInfo(arity=Arity.FromString('?')))])
_FileInfo_struct_list_TypeInfo_Contents = OrderedDict([("struct_name", GenericTypeInfo()), ("var_names", GenericTypeInfo(arity=Arity.FromString('*'))), ("raw_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("simple_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("definition_line", GenericTypeInfo(arity=Arity.FromString('?'))), ("constructor_list", GenericTypeInfo(arity=Arity.FromString('*')))])
_FileInfo_struct_list_constructor_list_TypeInfo_Contents = OrderedDict([("constructor_name", GenericTypeInfo()), ("var_names", GenericTypeInfo(arity=Arity.FromString('*'))), ("raw_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("simple_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("definition_line", GenericTypeInfo(arity=Arity.FromString('?')))])
_FileInfo_struct_list_constructor_list_TypeInfo_Contents = OrderedDict([("var_names", GenericTypeInfo(arity=Arity.FromString('*'))), ("raw_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("simple_var_types", GenericTypeInfo(arity=Arity.FromString('*'))), ("definition_line", GenericTypeInfo(arity=Arity.FromString('?')))])
_FileInfo_TypeInfo = AnyOfTypeInfo([ClassTypeInfo(_FileInfo_TypeInfo_Contents, require_exact_match=False), DictTypeInfo(_FileInfo_TypeInfo_Contents, require_exact_match=False)], arity=Arity.FromString('*'))
_FileInfo_function_list_TypeInfo = AnyOfTypeInfo([ClassTypeInfo(_FileInfo_function_list_TypeInfo_Contents, require_exact_match=False), DictTypeInfo(_FileInfo_function_list_TypeInfo_Contents, require_exact_match=False)], arity=Arity.FromString('*'))
@ -174,7 +174,6 @@ _FileInfo_struct_list_raw_var_types_TypeInfo = St
_FileInfo_struct_list_simple_var_types_TypeInfo = StringTypeInfo(min_length=1, arity=Arity.FromString('*'))
_FileInfo_struct_list_definition_line_TypeInfo = IntTypeInfo(min=1, arity=Arity.FromString('?'))
_FileInfo_struct_list_constructor_list_TypeInfo = AnyOfTypeInfo([ClassTypeInfo(_FileInfo_struct_list_constructor_list_TypeInfo_Contents, require_exact_match=False), DictTypeInfo(_FileInfo_struct_list_constructor_list_TypeInfo_Contents, require_exact_match=False)], arity=Arity.FromString('*'))
_FileInfo_struct_list_constructor_list_constructor_name_TypeInfo = StringTypeInfo(min_length=1)
_FileInfo_struct_list_constructor_list_var_names_TypeInfo = StringTypeInfo(min_length=1, arity=Arity.FromString('*'))
_FileInfo_struct_list_constructor_list_raw_var_types_TypeInfo = StringTypeInfo(min_length=1, arity=Arity.FromString('*'))
_FileInfo_struct_list_constructor_list_simple_var_types_TypeInfo = StringTypeInfo(min_length=1, arity=Arity.FromString('*'))
@ -469,19 +468,6 @@ class Deserializer(object):
return results
# ----------------------------------------------------------------------
@classmethod
def FileInfo_struct_list_constructor_list_constructor_name(cls, item):
if item in [DoesNotExist, None]:
_FileInfo_struct_list_constructor_list_constructor_name_TypeInfo.ValidateArity(None)
return DoesNotExist
result = cls._FileInfo_struct_list_constructor_list_constructor_name_Item(item)
_FileInfo_struct_list_constructor_list_constructor_name_TypeInfo.ValidateArity(result)
return result
# ----------------------------------------------------------------------
@classmethod
def FileInfo_struct_list_constructor_list_var_names(cls, items):
@ -844,19 +830,6 @@ class Deserializer(object):
attributes=None,
)
# constructor_name
try:
setattr(result, "constructor_name", cls.FileInfo_struct_list_constructor_list_constructor_name(
cls._GetPythonAttribute(
item,
"constructor_name",
is_optional=False,
),
)
)
except:
_DecorateActiveException("constructor_name")
# var_names
try:
cls._ApplyOptionalChildren(item, "var_names", result, cls.FileInfo_struct_list_constructor_list_var_names, always_include_optional)
@ -886,7 +859,7 @@ class Deserializer(object):
cls._ApplyAdditionalData(
item,
result,
exclude_names={"constructor_name", "var_names", "raw_var_types", "simple_var_types", "definition_line"},
exclude_names={"var_names", "raw_var_types", "simple_var_types", "definition_line"},
)
_FileInfo_struct_list_constructor_list_TypeInfo.ValidateItem(
@ -897,11 +870,6 @@ class Deserializer(object):
return result
# ----------------------------------------------------------------------
@classmethod
def _FileInfo_struct_list_constructor_list_constructor_name_Item(cls, item):
return JsonSerialization.DeserializeItem(_FileInfo_struct_list_constructor_list_constructor_name_TypeInfo, item, **{})
# ----------------------------------------------------------------------
@classmethod
def _FileInfo_struct_list_constructor_list_var_names_Item(cls, item):

Просмотреть файл

@ -174,7 +174,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "CName",
"var_names": ['arg1', 'arg2'],
"raw_var_types": ['a', 'b'],
"simple_var_types": ['c', 'd'],
@ -194,7 +193,6 @@ class TestSuite(unittest.TestCase):
self.assertEqual(result[0].struct_list[0].simple_var_types, ["e", "f",])
self.assertEqual(result[0].struct_list[0].definition_line, 7)
self.assertEqual(result[0].struct_list[0].constructor_list[0].constructor_name, "CName")
self.assertEqual(result[0].struct_list[0].constructor_list[0].var_names, ['arg1', 'arg2'])
self.assertEqual(result[0].struct_list[0].constructor_list[0].raw_var_types, ['a', 'b'])
self.assertEqual(result[0].struct_list[0].constructor_list[0].simple_var_types, ['c', 'd'])
@ -217,14 +215,12 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "CName",
"var_names": ['arg1', 'arg2'],
"raw_var_types": ['a', 'b'],
"simple_var_types": ['c', 'd'],
"definition_line": 13,
},
{
"constructor_name": "CName2",
"var_names": ['arg12', 'arg22'],
"raw_var_types": ['a2', 'b2'],
"simple_var_types": ['c2', 'd2'],
@ -244,13 +240,11 @@ class TestSuite(unittest.TestCase):
self.assertEqual(result[0].struct_list[0].simple_var_types, ["e", "f",])
self.assertEqual(result[0].struct_list[0].definition_line, 7)
self.assertEqual(result[0].struct_list[0].constructor_list[0].constructor_name, "CName")
self.assertEqual(result[0].struct_list[0].constructor_list[0].var_names, ['arg1', 'arg2'])
self.assertEqual(result[0].struct_list[0].constructor_list[0].raw_var_types, ['a', 'b'])
self.assertEqual(result[0].struct_list[0].constructor_list[0].simple_var_types, ['c', 'd'])
self.assertEqual(result[0].struct_list[0].constructor_list[0].definition_line, 13)
self.assertEqual(result[0].struct_list[0].constructor_list[1].constructor_name, "CName2")
self.assertEqual(result[0].struct_list[0].constructor_list[1].var_names, ['arg12', 'arg22'])
self.assertEqual(result[0].struct_list[0].constructor_list[1].raw_var_types, ['a2', 'b2'])
self.assertEqual(result[0].struct_list[0].constructor_list[1].simple_var_types, ['c2', 'd2'])
@ -834,67 +828,6 @@ class TestSuite(unittest.TestCase):
# ----------------------------------------------------------------------
def test_InvalidNameConstructor(self):
self.assertRaisesRegex(
Exception,
"An item was expected",
lambda: Deserialize(
[
{
"struct_list": [
{
"struct_name": "name",
"var_names": ["a", "b",],
"raw_var_types": ["c", "d",],
"simple_var_types": ["e", "f",],
"definition_line": 7,
"constructor_list": [
{
"constructor_name": None,
"var_names": ["ca", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["ce", "cf"],
"definition_line": 14
}
]
}
]
},
],
)
)
self.assertRaisesRegex(
Exception,
"'' is not a valid 'String' string - Value must have at least 1 character",
lambda: Deserialize(
[
{
"struct_list": [
{
"struct_name": "name",
"var_names": ["a", "b",],
"raw_var_types": ["c", "d",],
"simple_var_types": ["e", "f",],
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "",
"var_names": ["ca", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["ce", "cf"],
"definition_line": 14
}
]
}
]
},
],
)
)
# ----------------------------------------------------------------------
def test_InvalidArgNamesConstructor(self):
self.assertRaisesRegex(
Exception,
@ -911,7 +844,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": [None, "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["ce", "cf"],
@ -940,7 +872,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["ce", "cf"],
@ -972,7 +903,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["ca", "cb"],
"raw_var_types": [None, "cd"],
"simple_var_types": ["ce", "cf"],
@ -1001,7 +931,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["ca", "cb"],
"raw_var_types": ["", "cd"],
"simple_var_types": ["ce", "cf"],
@ -1033,7 +962,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["ca", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": [None, "cf"],
@ -1062,7 +990,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 7,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["ca", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["", "cf"],
@ -1094,7 +1021,6 @@ class TestSuite(unittest.TestCase):
"definition_line": 0,
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["ca", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["ce", "cf"],
@ -1123,7 +1049,6 @@ class TestSuite(unittest.TestCase):
"definition_line": "String",
"constructor_list": [
{
"constructor_name": "Cname",
"var_names": ["ca", "cb"],
"raw_var_types": ["cc", "cd"],
"simple_var_types": ["ce", "cf"],

Просмотреть файл

@ -403,6 +403,11 @@ class FuncTest(unittest.TestCase):
int main(){
return 0;
}
struct y{
int a, b;
y(struct y &&other): a(std::move(other.a)), b(std::move(other.b)){}
};
''')
func_list = self._GetFuncList(CppToJson.ObtainFunctions(s, None, lambda type: True))