-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcsfilter.cc
219 lines (182 loc) · 6.57 KB
/
csfilter.cc
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
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff 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
* any later version.
*
* csdiff 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 csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "csfilter.hh"
#include <iostream>
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
// Setup verbosity for debugging string substitions while matching them.
// Verbosity levels are from 0 to 3 (0 is off)
#define DEBUG_SUBST 0
inline std::string regexReplaceWrap(
const std::string &input,
const boost::regex &re,
const std::string &fmt)
{
std::string output(boost::regex_replace(input, re, fmt));
#if DEBUG_SUBST > 1
if (input != output)
std::cerr << "regex_replace: " << input << " -> " << output << "\n";
#endif
return output;
}
MsgFilter* MsgFilter::self_;
struct MsgReplace {
const boost::regex regex;
const std::string replaceWith;
MsgReplace(const std::string ®ex_, const std::string &rpl) :
regex(regex_),
replaceWith(rpl)
{
}
};
typedef std::vector<MsgReplace *> TRegexList;
typedef std::map<const std::string, TRegexList> TMsgFilterMap;
struct MsgFilter::Private {
bool ignorePath;
const std::string strKrn;
const boost::regex reKrn;
const boost::regex reMsgConstExprRes;
const boost::regex reDir;
const boost::regex reFile;
const boost::regex rePath;
const boost::regex reTmpPath;
const boost::regex reTmpCleaner;
TMsgFilterMap msgFilterMap;
TSubstMap fileSubsts;
void addMsgFilter(
const std::string &checker,
const std::string ®exp,
const std::string &replacement = "")
{
struct MsgReplace *rpl = new MsgReplace(regexp, replacement);
msgFilterMap[checker].push_back(rpl);
}
Private():
ignorePath(false),
strKrn("^[a-zA-Z+]+"),
reKrn(strKrn),
reDir("^([^:]*/)"),
reFile("[^/]+$"),
rePath("^(?:/builddir/build/BUILD/)?([^/]+/)(.*)(\\.[ly])?$"),
reTmpPath("^(/var)?/tmp/(.*)$"),
reTmpCleaner("(.*)")
{
addMsgFilter("", "[0-9][0-9]* out of [0-9][0-9]* times");
addMsgFilter("UNUSED_VALUE",
"\\(instance [0-9]+\\)");
addMsgFilter("STRING_OVERFLOW",
"You might overrun the [0-9][0-9]* byte");
// ignore changes in parameters -> it is still the same UNUSED_VALUE
addMsgFilter("UNUSED_VALUE",
"returned by \"([^\\(]+)\\(.*\\)\"",
"returned by \"\\1\\(\\)\"");
// ignore embeded declaration location
addMsgFilter("COMPILER_WARNING", " \\(declared at [^)]*\\)", "");
// ignore suggestion for deprecation warnings
addMsgFilter("COMPILER_WARNING", ": Use '[^']*' instead", "");
// unify (per build random) names of temporary variables
addMsgFilter("COMPILER_WARNING", "_tmp[0-9]+_", "_tmp_");
// "__coverity_strcmp" -> "strcmp", etc.
addMsgFilter("", "__coverity_", "");
// artificial field names of anonymous unions that Coverity produces
addMsgFilter("", "__C[0-9]+");
// used by IDENTIFIER_TYPO (but applies generally)
addMsgFilter("", "at least [0-9][0-9]* times.$");
}
};
MsgFilter::MsgFilter():
d(new Private)
{
}
MsgFilter::~MsgFilter() {
BOOST_FOREACH(TMsgFilterMap::const_reference item, d->msgFilterMap)
BOOST_FOREACH(struct MsgReplace *rpl, item.second)
delete rpl;
delete d;
}
void MsgFilter::setIgnorePath(bool enable) {
d->ignorePath = enable;
}
void MsgFilter::setFileNameSubstitution(
const std::string &oldFile,
const std::string &newFile)
{
d->fileSubsts[oldFile] = newFile;
}
std::string MsgFilter::filterMsg(
const std::string &msg,
const std::string &checker)
{
std::string filtered = msg;
BOOST_FOREACH(const struct MsgReplace *rpl, d->msgFilterMap[checker]) {
filtered = regexReplaceWrap(filtered, rpl->regex, rpl->replaceWith);
}
// these substitutions are common for all checkers
BOOST_FOREACH(const struct MsgReplace *rpl, d->msgFilterMap[""]) {
filtered = regexReplaceWrap(filtered, rpl->regex, rpl->replaceWith);
}
#if DEBUG_SUBST > 1
std::cerr << "filterMsg: " << filtered << "\n";
#endif
return filtered;
}
std::string MsgFilter::filterPath(const std::string &origPath) {
std::string path = origPath;
TSubstMap &substMap = d->fileSubsts;
if (!substMap.empty()) {
std::string base = regexReplaceWrap(origPath, d->reDir, "");
std::string dir = regexReplaceWrap(origPath, d->reFile, "");
if (substMap.find(base) != substMap.end()) {
const std::string &substWith = substMap[base];
path = dir + substWith;
}
}
if (d->ignorePath)
return regexReplaceWrap(path, d->reDir, "");
if (boost::regex_match(path, d->reTmpPath)) {
// filter random numbers in names of temporary generated files
std::string tmpPath = boost::regex_replace(path, d->reTmpCleaner, "/tmp/tmp.c");
return tmpPath;
}
boost::smatch sm;
if (!boost::regex_match(path, sm, d->rePath))
// no match
return path;
std::string nvr (sm[/* NVR */ 1]);
std::string core(sm[/* core */ 2]);
// try to kill the multiple version strings in paths (kernel, OpenLDAP, ...)
nvr.resize(nvr.size() - 1);
std::string ver(boost::regex_replace(nvr, d->reKrn, ""));
const std::string krnPattern = d->strKrn + ver + "[^/]*/";
#if DEBUG_SUBST > 2
std::cerr << "nvr: " << nvr << "\n";
std::cerr << "ver: " << ver << "\n";
std::cerr << "krnPattern: " << krnPattern << "\n";
#endif
const boost::regex reKill(krnPattern);
core = boost::regex_replace(core, reKill, "");
// quirk for Coverity inconsistency in handling bison-generated file names
std::string suff(sm[/* Bison suffix */ 3]);
if (!suff.empty())
core += ".c";
#if DEBUG_SUBST
std::cerr << "filterPath: " << path << " -> " << core << "\n";
#endif
return core;
}