forked from usefulalgorithm/PAC-MAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
138 lines (129 loc) · 5.27 KB
/
utils.cpp
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
#include "utils.h"
using namespace std;
bool checkTrivial(const string& s) {
if (s.find_first_not_of("{") == string::npos)
return true;
else if (s[s.find_first_not_of("{")] == ' ')
return true;
else
return false;
}
bool checkFunctionEntrance(const string& s) {
if (s.find_first_of("=") == string::npos
&& s.find("main") == string::npos) {
string t = s.substr(s.find_first_of(")")+1);
if (t.find_first_not_of(" \t\r\n") == string::npos)
return true;
}
return false;
}
bool checkFunctionReturn(const string& s) {
if (s.find_first_not_of("}") == string::npos)
return true;
else return false;
}
void printMisc(string filename) { // TODO
cout << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<graphml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n";
cout << "<key attr.name=\"assumption\" attr.type=\"string\" for=\"edge\" id=\"assumption\"/>\n";
cout << "<key attr.name=\"sourcecode\" attr.type=\"string\" for=\"edge\" id=\"sourcecode\"/>\n";
cout << "<key attr.name=\"sourcecodeLanguage\" attr.type=\"string\" for=\"graph\" id=\"sourcecodelang\"/>\n";
cout << "<key attr.name=\"startline\" attr.type=\"int\" for=\"edge\" id=\"startline\"/>\n";
cout << "<key attr.name=\"originFileName\" attr.type=\"string\" for=\"edge\" id=\"originfile\">\n";
cout << "<default>" << filename << "</default>\n"; // TODO
cout << "</key>\n";
cout << "<key attr.name=\"nodeType\" attr.type=\"string\" for=\"node\" id=\"nodetype\">\n";
cout << "<default>path</default>\n";
cout << "</key>\n";
cout << "<key attr.name=\"isViolationNode\" attr.type=\"boolean\" for=\"node\" id=\"violation\">\n";
cout << "<default>false</default>\n";
cout << "</key>\n";
cout << "<key attr.name=\"isEntryNode\" attr.type=\"boolean\" for=\"node\" id=\"entry\">\n";
cout << "<default>false</default>\n";
cout << "</key>\n";
cout << "<key attr.name=\"isSinkNode\" attr.type=\"boolean\" for=\"node\" id=\"sink\">\n";
cout << "<default>false</default>\n";
cout << "</key>\n";
cout << "<key attr.name=\"enterFunction\" attr.type=\"string\" for=\"edge\" id=\"enterFunction\"/>\n";
cout << "<key attr.name=\"returnFromFunction\" attr.type=\"string\" for=\"edge\" id=\"returnFrom\"/>\n";
cout << "<graph edgedefault=\"directed\"><data key=\"sourcecodelang\">C</data>\n";
cout << "<data key=\"producer\">PACMAN</data>\n";
cout << "<data key=\"programfile\">" << filename << "</data>\n"; // TODO
cout << "<data key=\"memorymodel\">precise</data>\n";
cout << "<data key=\"architecture\">32bit</data>\n";
}
void printGraph(vector<Node>& nodes, map<string, pair<int, int>>& f, int b, int e) {
for (int i = b; i <= e; i++) {
if (!nodes[i].isExtern()) {
nodes[i].printNode();
if (nodes[i].isFuncCall()) {
string n = nodes[i].getFunc();
int bn = f[n].first, en = f[n].second;
printGraph(nodes, f, bn, en);
}
}
}
}
void printFuncSeq(vector<tuple<int, bool, string>>& funcSeq) {
vector<string> funcName;
cout << "<node id=\"ENTRY\">\n"
<< "<data key=\"entry\">true</data>\n"
<< "</node>\n";
cout << "<edge source=\"ENTRY\" target=\"" << get<0>(funcSeq[0]) << "\"/>\n";
for (size_t i = 0; i < funcSeq.size()-1; i++) {
cout << "<node id=\"" << get<0>(funcSeq[i]) << "\"/>\n";
cout << "<edge source=\"" << get<0>(funcSeq[i]) << "\" target=\"" << get<0>(funcSeq[i+1]) << "\">\n";
if (get<1>(funcSeq[i]) == 0) {
cout << "<data key=\"enterFunction\">" << get<2>(funcSeq[i]) << "</data>\n"
<< "</edge>\n";
funcName.push_back(get<2>(funcSeq[i]));
}
else {
cout << "<data key=\"returnFrom\">" << funcName.back() << "</data>\n"
<< "</edge>\n";
funcName.pop_back();
}
}
cout << "<node id=\"" << get<0>(funcSeq.back()) << "\"/>\n";
cout << "<edge source=\"" << get<0>(funcSeq.back()) << "\" target=\"ERROR\">\n";
cout << "<data key=\"returnFrom\">" << funcName.back() << "</data>\n"
<< "</edge>\n";
funcName.pop_back();
cout << "<node id=\"ERROR\">\n"
<< "<data key=\"violation\">true</data>\n"
<< "</node>\n";
}
void traverse(vector<Node>& nodes, vector<tuple<int, bool, string>>& funcSeq, map<string, pair<int, int>>& f, int b, int e, string s, int p) {
for (int i = b; i <= e; i++) {
if (!nodes[i].isExtern()) {
nodes[i].setScope(s);
if (nodes[i].isFuncCall()) {
string n = nodes[i].getFunc();
int bn = f[n].first, en = f[n].second;
if (n.find("VERIFIER") == string::npos) {
funcSeq.push_back(make_tuple(nodes[i].getId(), 0, n));
nodes[i].setNext(bn);
traverse(nodes, funcSeq, f, bn, en, n, i);
}
}
else if (nodes[i].isFuncReturn()) {
nodes[i].setNext(p+1);
string n = nodes[i].getFunc();
funcSeq.push_back(make_tuple(nodes[i].getId(),1, n));
}
}
}
}
void changeOperator(string& s) {
size_t gtp = s.find(" > ");
if (gtp != string::npos)
s.replace(gtp+1, 1, ">");
size_t ltp = s.find(" < ");
if (ltp != string::npos)
s.replace(ltp+1, 1, "<");
size_t gtoep = s.find(" >= ");
if (gtoep != string::npos)
s.replace(gtoep+1, 2, ">=");
size_t ltoep = s.find(" <= ");
if (ltoep != string::npos)
s.replace(ltoep+1, 2, "<=");
}