-
Notifications
You must be signed in to change notification settings - Fork 37
/
idquery.cpp
311 lines (288 loc) · 7.56 KB
/
idquery.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* (C) Copyright 2001-2015 Diomidis Spinellis
*
* This file is part of CScout.
*
* CScout is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CScout is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CScout. If not, see <http://www.gnu.org/licenses/>.
*
*
* Encapsulates an (user interface) identifier query
* Can be used to evaluate against IdProp elements
*
*/
#include <map>
#include <string>
#include <deque>
#include <vector>
#include <stack>
#include <iterator>
#include <iostream>
#include <fstream>
#include <list>
#include <set>
#include <functional>
#include <algorithm> // set_difference
#include <cctype>
#include <sstream> // ostringstream
#include <cstdio> // perror, rename
#include <cstdlib> // atoi
#include <regex.h>
#include "swill.h"
#include "getopt.h"
#include "cpp.h"
#include "debug.h"
#include "error.h"
#include "parse.tab.h"
#include "attr.h"
#include "metrics.h"
#include "fileid.h"
#include "tokid.h"
#include "token.h"
#include "ptoken.h"
#include "fchar.h"
#include "pltoken.h"
#include "macro.h"
#include "pdtoken.h"
#include "eclass.h"
#include "ctoken.h"
#include "type.h"
#include "stab.h"
#include "fdep.h"
#include "version.h"
#include "call.h"
#include "fcall.h"
#include "mcall.h"
#include "compiledre.h"
#include "option.h"
#include "query.h"
#include "idquery.h"
IdProp Identifier::ids;
// Construct an object based on URL parameters
IdQuery::IdQuery(FILE *of, bool icase, Attributes::size_type cp, bool e, bool r) :
Query(!e, r, true),
match(attr_end),
current_project(cp)
{
if (lazy)
return;
// Query name
char *qname = swill_getvar("n");
if (qname && *qname)
name = qname;
// Identifier EC match
if (!swill_getargs("p(ec)", &ec)) {
ec = NULL;
// Type of boolean match
char *m;
if (!(m = swill_getvar("match"))) {
fprintf(of, "Missing value: match");
valid = return_val = false;
lazy = true;
return;
}
match_type = *m;
}
xfile = !!swill_getvar("xfile");
unused = !!swill_getvar("unused");
writable = !!swill_getvar("writable");
exclude_ire = !!swill_getvar("xire");
exclude_fre = !!swill_getvar("xfre");
// Compile regular expression specs
if (!compile_re(of, "Identifier", "ire", ire, match_ire, str_ire))
return;
if (!compile_re(of, "Filename", "fre", fre, match_fre, str_fre, (icase ? REG_ICASE : 0)))
return;
// Store match specifications in a vector
for (int i = attr_begin; i < attr_end; i++) {
ostringstream varname;
varname << "a" << i;
match[i] = !!swill_getvar(varname.str().c_str());
if (DP())
cout << "v=[" << varname.str() << "] m=" << match[i] << "\n";
}
}
// Report the string query specification usage
void
IdQuery::usage(void)
{
cerr << "The monitored identifier attributes must be specified using the syntax:\n"
"Y|L|E|T[:attr1][:attr2]...\n"
"\tY: Match any of the specified attributes\n"
"\tL: Match all of the specified attributes\n"
"\tE: Exclude the specified attributes matched\n"
"\tT: Exact match of the specified attributes\n\n"
"Allowable attribute names are:\n"
"\tunused: Unused\n"
"\twritable: Writable\n";
for (int i = attr_begin; i < attr_end; i++)
cerr << "\t" << Attributes::shortname(i) << ": " << Attributes::name(i) << "\n";
exit(1);
}
// Construct an object based on a string specification
// The syntax is Y|L|E|T[:attr1][:attr2]...
IdQuery::IdQuery(const string &s) :
Query(false, false, true),
match_fre(false),
match_ire(false),
match(attr_end),
xfile(false),
ec(NULL),
current_project(0)
{
// Type of boolean match
if (s.length() == 0)
usage();
switch (s[0]) {
case 'Y':
case 'L':
case 'E':
case 'T':
match_type = s[0];
break;
default:
usage();
}
unused = (s.find(":unused") != string::npos);
writable = (s.find(":writable") != string::npos);
// Store match specifications in a vector
for (int i = attr_begin; i < attr_end; i++)
match[i] = (s.find(":" + Attributes::shortname(i)) != string::npos);
}
// Return the URL for re-executing this query
string
IdQuery::base_url() const
{
return string("xiquery.html?") + param_url();
}
// Return the query's parameters as a URL
string
IdQuery::param_url() const
{
string r("qt=id");
if (ec) {
char buff[256];
snprintf(buff, sizeof(buff), "&ec=%p", ec);
r += buff;
} else {
r += "&match=";
r += Query::url(string(1, match_type));
}
if (xfile)
r += "&xfile=1";
if (unused)
r += "&unused=1";
if (writable)
r += "&writable=1";
if (match_ire)
r += "&ire=" + Query::url(str_ire);
if (exclude_ire)
r += "&xire=1";
if (exclude_fre)
r += "&xfre=1";
if (match_fre)
r += "&fre=" + Query::url(str_fre);
for (int i = attr_begin; i < attr_end; i++) {
if (match[i]) {
ostringstream varname;
varname << "&a" << i << "=1";
r += varname.str();
}
}
if (name.length())
r += "&n=" + Query::url(name);
return r;
}
// Evaluate the object's identifier query against i
// return true if it matches
bool
IdQuery::eval(const IdPropElem &i)
{
if (lazy)
return return_val;
if (ec)
return (i.first == ec);
if (current_project && !i.first->get_attribute(current_project))
return false;
int retval = exclude_ire ? 0 : REG_NOMATCH;
if (match_ire && ire.exec(i.second.get_id()) == retval)
return false;
bool add = false;
switch (match_type) {
case 'Y': // anY match
add = false;
for (int j = attr_begin; j < attr_end; j++)
if (match[j] && i.first->get_attribute(j)) {
add = true;
break;
}
add = (add || (xfile && i.second.get_xfile()));
add = (add || (unused && i.first->is_unused()));
add = (add || (writable && !i.first->get_attribute(is_readonly)));
break;
case 'L': // alL match
add = true;
for (int j = attr_begin; j < attr_end; j++)
if (match[j] && !i.first->get_attribute(j)) {
add = false;
break;
}
add = (add && (!xfile || i.second.get_xfile()));
add = (add && (!unused || i.first->is_unused()));
add = (add && (!writable || !i.first->get_attribute(is_readonly)));
break;
case 'E': // excludE match
add = true;
for (int j = attr_begin; j < attr_end; j++)
if (match[j] && i.first->get_attribute(j)) {
add = false;
break;
}
add = (add && (!xfile || !i.second.get_xfile()));
add = (add && (!unused || !(i.first->is_unused())));
add = (add && (!writable || i.first->get_attribute(is_readonly)));
break;
case 'T': // exactT match
add = true;
for (int j = attr_begin; j < attr_end; j++)
if (match[j] != i.first->get_attribute(j)) {
add = false;
break;
}
add = (add && (xfile == i.second.get_xfile()));
add = (add && (unused == (i.first->is_unused())));
add = (add && (writable == !i.first->get_attribute(is_readonly)));
break;
}
if (!add)
return false;
if (match_fre) {
// Before we add it check if its filename matches the RE
IFSet f = i.first->sorted_files();
IFSet::iterator j;
for (j = f.begin(); j != f.end(); j++)
if (fre.exec((*j).get_path()) == 0) {
if (DP())
cout << "Identifier " << i.second.get_id() <<
" occurs in file " << j->get_path() <<
", which matches RE " << str_fre << "\n";
if (exclude_fre)
return false; // Match; fail
else
break; // Match; stop search
}
if (!exclude_fre && j == f.end())
return false; // Asked to match and no match found
}
return true;
}