-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
152 lines (129 loc) · 5.1 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from pyswip import Prolog
import argparse
import json
import re
FUNCTION_TYPE_PATTERN = r"([^\(\)\s]+)\s*\(([^\(\)]*)\).*"
TYPE_CLASS = 'class'
TYPE_METHOD = 'method'
LIBRARIES = [
"use_module(library(clpfd))"
]
RULES = [
"all_diff(L) :- \+ (append(_,[X|R],L), memberchk(X,R))"
]
declared_methods = {}
parser = argparse.ArgumentParser(description='Tests OOP program structure.')
parser.add_argument('ast', type=str, help='Json file of CLang AST')
parser.add_argument('src_name', type=str, help='Name of source code file')
parser.add_argument('test', type=str, help='Prolog test query')
args = parser.parse_args()
def create_object(parent, obj_type, name):
return {
'parent': parent,
'type': obj_type,
'name': name
}
def collect_facts(prolog, parent, ast):
node_kind = ast['kind']
facts = []
if node_kind == 'CXXRecordDecl':
class_name = ast['name']
facts.append("class('{}')".format(class_name))
parent = create_object(parent, TYPE_CLASS, class_name)
if 'bases' in ast:
for base in ast['bases']:
base_class_name = base['type']['qualType']
facts.append("parent('{}','{}',{})".format(
base_class_name, class_name, base['access']
))
if node_kind == 'FieldDecl' \
and parent != None and parent['type'] == TYPE_CLASS:
class_name = parent['name']
property_name = ast['name']
facts.append("property('{}','{}')".format(class_name, property_name))
if node_kind == 'CXXConstructorDecl' \
and parent != None and parent['type'] == TYPE_CLASS \
and ('isImplicit' not in ast or not ast['isImplicit']):
# and 'type' in ast and 'qualType' in ast['type'] \
match = re.match(FUNCTION_TYPE_PATTERN, ast['type']['qualType'])
assert(match.lastindex == 2)
parameter_types = match.group(2)
if parameter_types.strip() == '':
parameter_types = []
else:
parameter_types = parameter_types.split(', ')
class_name = parent['name']
facts.append("constructor('{}',{})".format(class_name, str(parameter_types)))
if node_kind == 'CXXDestructorDecl' \
and parent != None and parent['type'] == TYPE_CLASS \
and ('isImplicit' not in ast or not ast['isImplicit']):
class_name = parent['name']
facts.append("destructor('{}')".format(class_name))
if node_kind == 'CXXMethodDecl':
method_name = ast['name']
match = re.match(FUNCTION_TYPE_PATTERN, ast['type']['qualType'])
assert(match.lastindex == 2)
return_type = match.group(1)
parameter_types = match.group(2)
if parameter_types.strip() == '':
parameter_types = []
else:
parameter_types = parameter_types.split(', ')
# parent = create_object(parent, TYPE_METHOD, method_name)
if parent is not None and parent['type'] == TYPE_CLASS:
class_name = parent['name']
facts.append("method_declaration('{}','{}','{}',{})".format(
class_name, method_name, return_type, str(parameter_types)
))
if 'inner' in ast:
facts.append("method_implementation('{}','{}',inside,'{}',{})".format(
class_name, method_name, return_type, str(parameter_types)
))
declared_methods[ast['id']] = parent
elif 'previousDecl' in ast and ast['previousDecl'] in declared_methods \
and 'inner' in ast:
class_obj = declared_methods[ast['previousDecl']]
facts.append("method_implementation('{}','{}',outside,'{}',{})".format(
class_obj['name'], method_name, return_type, str(parameter_types)
))
else:
print('Warning: Invalid CXXMethodDecl node: {}'.format(ast['id']))
if len(facts) > 0:
print('Collected facts: {}'.format(str(facts)))
for fact in facts:
prolog.assertz(fact)
if 'inner' in ast:
for children in ast['inner']:
collect_facts(prolog, parent, children)
def main():
prolog = Prolog()
with open(args.ast, 'r') as f:
try:
ast = json.loads(f.read())
except:
print("Can't read ast file!")
exit()
for library in LIBRARIES:
prolog.assertz(library)
for rule in RULES:
prolog.assertz(rule)
if ast['kind'] == 'TranslationUnitDecl':
items = ast['inner']
current_file = ''
for item in items:
if 'loc' in item and 'file' in item['loc'] and item['loc']['file'] != '':
current_file = item['loc']['file']
# print('Handling file: {}'.format(current_file))
if current_file == args.src_name:
collect_facts(prolog, None, item)
else:
print('Invalid root declaration')
exit()
solutions = prolog.query(args.test)
try:
specific_solution = next(solutions)
print('PASSED')
except:
print('FAILED')
solutions.close()
main()