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
|
/**
* @file
* @author pavel.iqx
* @date 2014
* @copyright GNU General Public License v2
*/
#include "file_logger.h"
#include "memcheckerror.h"
bool MemCheckErrorLocation::operator==(const MemCheckErrorLocation & other) const
{
return func == other.func && file == other.file && line == other.line;
}
bool MemCheckErrorLocation::operator!=(const MemCheckErrorLocation & other) const
{
return !(*this == other);
}
const wxString MemCheckErrorLocation::toString() const
{
return wxString::Format(wxT("%s\t%s\t%i\t%s"), func, file, line, obj);
}
const wxString MemCheckErrorLocation::toText(const wxString & workspacePath) const
{
return wxString::Format(wxT("%s ( %s: %i )"), func, getFile(workspacePath), line);
}
const wxString MemCheckErrorLocation::getFile(const wxString & workspacePath) const
{
wxString localPath;
if (workspacePath.IsEmpty() || !file.StartsWith(workspacePath, &localPath)) {
return file;
} else {
return localPath;
}
}
const wxString MemCheckErrorLocation::getObj(const wxString & workspacePath) const
{
wxString localPath;
if (workspacePath.IsEmpty() || !obj.StartsWith(workspacePath, &localPath)) {
return obj;
} else {
return localPath;
}
}
const bool MemCheckErrorLocation::isOutOfWorkspace(const wxString & workspacePath) const
{
return !file.StartsWith(workspacePath);
}
MemCheckError::MemCheckError(): suppressed(false) {}
const wxString MemCheckError::toString() const
{
wxString string = wxString::Format(wxT("%s"), label);
for (ErrorList::const_iterator it = nestedErrors.begin(); it != nestedErrors.end(); ++it)
string.Append(wxString::Format("\n%s", it->toString()));
for (LocationList::const_iterator it = locations.begin(); it != locations.end(); ++it)
string.Append(wxString::Format("\n%s", it->toString()));
return string;
}
const wxString MemCheckError::toText(unsigned int indent) const
{
wxString text = label;
for (ErrorList::const_iterator it = nestedErrors.begin(); it != nestedErrors.end(); ++it)
text.Append(wxString::Format("\n%s%s", wxString(' ', 2 * indent), it->toText(indent + 1)));
for (LocationList::const_iterator it = locations.begin(); it != locations.end(); ++it)
text.Append(wxString::Format("\n%s%s", wxString(' ', 4 * indent), it->toText()));
return text;
}
const wxString MemCheckError::getSuppression()
{
wxString ruleName;
if (suppression.Contains(wxT(SUPPRESSION_NAME_PLACEHOLDER))) {
suppression.Trim(true);
suppression.Trim(false);
wxStringTokenizer tokenizer(suppression, "\n");
int kind = 1;
while ( tokenizer.HasMoreTokens() ) {
wxString line = tokenizer.GetNextToken();
wxString token = line.AfterFirst(':');
if (!token.IsEmpty())
switch (kind) {
case 1:
ruleName.Append(wxString::Format("(%s =", token));
++kind;
break;
case 2:
ruleName.Append(wxString::Format(" %s", token));
++kind;
break;
default:
ruleName.Append(wxString::Format(" | %s", token));
}
}
ruleName.Append(wxT(")"));
suppression.Replace(wxT(SUPPRESSION_NAME_PLACEHOLDER), ruleName, false);
}
return suppression;
}
const bool MemCheckError::hasPath(const wxString & path) const
{
for (LocationList::const_iterator it = locations.begin(); it != locations.end(); ++it)
if (it->file.StartsWith(path)) return true;
for (ErrorList::const_iterator it = nestedErrors.begin(); it != nestedErrors.end(); ++it)
if (it->hasPath(path)) return true;
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////
bool MemCheckIterTools::IterTool::isEqual(MemCheckError & lhs, MemCheckError & rhs) const
{
if (!(lhs.type == rhs.type && lhs.label == rhs.label))
return false;
// locations == other.locations
LocationListIterator lhsItL = LocationListIterator(lhs.locations, *this);
LocationListIterator rhsItL = LocationListIterator(rhs.locations, *this);
LocationList::iterator lhsItEndL = lhs.locations.end();
LocationList::iterator rhsItEndL = rhs.locations.end();
while (!(lhsItL == lhsItEndL && rhsItL == rhsItEndL)) { // both on end => ok
if ( lhsItL == lhsItEndL || rhsItL == rhsItEndL || *lhsItL != *rhsItL)
return false; // only one on end or not equal => fail
++lhsItL;
++rhsItL;
}
// nestedErrors == other.nestedErrors
ErrorListIterator lhsItE = ErrorListIterator(lhs.nestedErrors, *this);
ErrorListIterator rhsItE = ErrorListIterator(rhs.nestedErrors, *this);
ErrorList::iterator lhsItEndE = lhs.nestedErrors.end();
ErrorList::iterator rhsItEndE = rhs.nestedErrors.end();
while (!(lhsItE == lhsItEndE && rhsItE == rhsItEndE)) { // both on end => ok
if ( lhsItE == lhsItEndE || rhsItE == rhsItEndE || ! isEqual(*lhsItE, *rhsItE))
return false; // only one on end or not equal => fail
++lhsItE;
++rhsItE;
}
return true;
}
MemCheckIterTools::LocationListIterator::LocationListIterator(LocationList & l,
const IterTool &iterTool) : p(l.begin()), m_end(l.end()), m_iterTool(iterTool)
{
while (p != m_end && m_iterTool.omitNonWorkspace && p->isOutOfWorkspace(m_iterTool.workspacePath))
++p;
}
MemCheckIterTools::LocationListIterator::~LocationListIterator() {}
LocationList::iterator& MemCheckIterTools::LocationListIterator::operator++()
{
++p;
while (p != m_end && m_iterTool.omitNonWorkspace && p->isOutOfWorkspace(m_iterTool.workspacePath))
++p;
return p;
}
LocationList::iterator MemCheckIterTools::LocationListIterator::operator++(int)
{
LocationList::iterator tmp(p);
operator++();
return tmp;
}
bool MemCheckIterTools::LocationListIterator::operator==(const LocationList::iterator& rhs)
{
return p == rhs;
}
bool MemCheckIterTools::LocationListIterator::operator!=(const LocationList::iterator& rhs)
{
return p != rhs;
}
MemCheckErrorLocation & MemCheckIterTools::LocationListIterator::operator*()
{
return *p;
}
MemCheckIterTools::ErrorListIterator::ErrorListIterator(ErrorList & l, const IterTool & iterTool)
: p(l.begin()), m_end(l.end()), m_iterTool(iterTool)
{
while (p != m_end && m_iterTool.omitSuppressed && p->suppressed)
++p;
}
MemCheckIterTools::ErrorListIterator::~ErrorListIterator() {}
ErrorList::iterator& MemCheckIterTools::ErrorListIterator::operator++()
{
ErrorList::iterator prev(p);
++p;
while (p != m_end && ( (m_iterTool.omitDuplications && m_iterTool.isEqual(*prev, *p))
|| (m_iterTool.omitSuppressed && p->suppressed) ))
++p;
return p;
}
ErrorList::iterator MemCheckIterTools::ErrorListIterator::operator++(int)
{
ErrorList::iterator tmp(p);
operator++();
return tmp;
}
bool MemCheckIterTools::ErrorListIterator::operator==(const ErrorList::iterator& rhs)
{
return p == rhs;
}
bool MemCheckIterTools::ErrorListIterator::operator!=(const ErrorList::iterator& rhs)
{
return p != rhs;
}
MemCheckError & MemCheckIterTools::ErrorListIterator::operator*()
{
return *p;
}
MemCheckIterTools::MemCheckIterTools(const wxString & workspacePath, unsigned int flags)
{
m_iterTool.omitNonWorkspace = flags & MC_IT_OMIT_NONWORKSPACE;
m_iterTool.omitDuplications = flags & MC_IT_OMIT_DUPLICATIONS;
m_iterTool.omitSuppressed = flags & MC_IT_OMIT_SUPPRESSED;
m_iterTool.workspacePath = workspacePath;
}
MemCheckIterTools::ErrorListIterator MemCheckIterTools::GetIterator(ErrorList & l)
{
return ErrorListIterator(l, m_iterTool);
}
MemCheckIterTools::LocationListIterator MemCheckIterTools::GetIterator(LocationList & l)
{
return LocationListIterator(l, m_iterTool);
}
MemCheckIterTools::ErrorListIterator MemCheckIterTools::Factory(ErrorList & l,
const wxString & workspacePath, unsigned int flags)
{
return MemCheckIterTools(workspacePath, flags).GetIterator(l);
}
MemCheckIterTools::LocationListIterator MemCheckIterTools::Factory(LocationList & l,
const wxString & workspacePath, unsigned int flags)
{
return MemCheckIterTools(workspacePath, flags).GetIterator(l);
}
|