Implemented generic reportError functions in tokenizer (similar to Check::reportError) to reduce code duplication

This commit is contained in:
PKEuS 2012-04-22 11:28:46 +02:00
parent 8c8c9d5e5b
commit 3d2b5a30fe
2 changed files with 66 additions and 185 deletions

View File

@ -22,7 +22,6 @@
#include "token.h" #include "token.h"
#include "mathlib.h" #include "mathlib.h"
#include "settings.h" #include "settings.h"
#include "errorlogger.h"
#include "check.h" #include "check.h"
#include "path.h" #include "path.h"
#include "symboldatabase.h" #include "symboldatabase.h"
@ -33,7 +32,6 @@
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include <list>
#include <cassert> #include <cassert>
#include <cctype> #include <cctype>
#include <stack> #include <stack>
@ -440,31 +438,13 @@ void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, cons
if (tok1 && !(_settings->isEnabled("style") && _settings->inconclusive)) if (tok1 && !(_settings->isEnabled("style") && _settings->inconclusive))
return; return;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<const Token*> locationList;
std::string tok2_str; locationList.push_back(tok1);
if (tok1 && tok2) { locationList.push_back(tok2);
ErrorLogger::ErrorMessage::FileLocation loc; const std::string tok2_str = tok2 ? tok2->str():std::string("name");
loc.line = tok1->linenr();
loc.setfile(file(tok1));
locationList.push_back(loc);
loc.line = tok2->linenr();
loc.setfile(file(tok2));
locationList.push_back(loc);
tok2_str = tok2->str();
} else
tok2_str = "name";
const ErrorLogger::ErrorMessage errmsg(locationList, reportError(locationList, Severity::style, "variableHidingTypedef",
Severity::style, std::string(type + " '" + tok2_str + "' hides typedef with same name"), true);
std::string(type + " '" + tok2_str +
"' hides typedef with same name"),
"variableHidingTypedef",
true);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type) void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type)
@ -472,31 +452,13 @@ void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2,
if (tok1 && !(_settings->isEnabled("style"))) if (tok1 && !(_settings->isEnabled("style")))
return; return;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<const Token*> locationList;
std::string tok2_str; locationList.push_back(tok1);
if (tok1 && tok2) { locationList.push_back(tok2);
ErrorLogger::ErrorMessage::FileLocation loc; const std::string tok2_str = tok2 ? tok2->str():std::string("name");
loc.line = tok1->linenr();
loc.setfile(file(tok1));
locationList.push_back(loc);
loc.line = tok2->linenr();
loc.setfile(file(tok2));
locationList.push_back(loc);
tok2_str = tok2->str();
} else
tok2_str = "name";
const ErrorLogger::ErrorMessage errmsg(locationList, reportError(locationList, Severity::style, "unnecessaryForwardDeclaration",
Severity::style, std::string(type + " '" + tok2_str + "' forward declaration unnecessary, already declared"));
std::string(type + " '" + tok2_str +
"' forward declaration unnecessary, already declared"),
"unnecessaryForwardDeclaration",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
// check if this statement is a duplicate definition // check if this statement is a duplicate definition
@ -678,22 +640,9 @@ void Tokenizer::unsupportedTypedef(const Token *tok) const
} }
if (tok) if (tok)
str << " ;"; str << " ;";
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok1->linenr();
loc.setfile(file(tok1));
locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList, reportError(tok1, Severity::debug, "debug",
Severity::debug, "Failed to parse \'" + str.str() + "\'. The checking continues anyway.");
"Failed to parse \'" + str.str() + "\'. The checking continues anyway.",
"debug",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
Token * Tokenizer::deleteInvalidTypedef(Token *typeDef) Token * Tokenizer::deleteInvalidTypedef(Token *typeDef)
@ -6822,22 +6771,8 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
Token::Match(tok3->tokAt(-2), ") { %var% ++|--")) Token::Match(tok3->tokAt(-2), ") { %var% ++|--"))
break; break;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; reportError(tok3, Severity::debug, "debug",
ErrorLogger::ErrorMessage::FileLocation loc; "simplifyKnownVariables: bailing out (variable="+tok3->str()+", value="+value+")");
loc.line = tok3->linenr();
loc.setfile(file(tok3));
locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::debug,
"simplifyKnownVariables: bailing out (variable="+tok3->str()+", value="+value+")",
"debug",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
break; break;
@ -7457,31 +7392,13 @@ void Tokenizer::duplicateEnumError(const Token * tok1, const Token * tok2, const
if (tok1 && !(_settings->isEnabled("style"))) if (tok1 && !(_settings->isEnabled("style")))
return; return;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<const Token*> locationList;
std::string tok2_str; locationList.push_back(tok1);
if (tok1 && tok2) { locationList.push_back(tok2);
ErrorLogger::ErrorMessage::FileLocation loc; const std::string tok2_str = tok2 ? tok2->str():std::string("name");
loc.line = tok1->linenr();
loc.setfile(file(tok1));
locationList.push_back(loc);
loc.line = tok2->linenr();
loc.setfile(file(tok2));
locationList.push_back(loc);
tok2_str = tok2->str();
} else
tok2_str = "name";
const ErrorLogger::ErrorMessage errmsg(locationList, reportError(locationList, Severity::style, "variableHidingEnum",
Severity::style, std::string(type + " '" + tok2_str + "' hides enumerator with same name"));
std::string(type + " '" + tok2_str +
"' hides enumerator with same name"),
"variableHidingEnum",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
// Check if this statement is a duplicate definition. A duplicate // Check if this statement is a duplicate definition. A duplicate
@ -8244,73 +8161,20 @@ const std::string& Tokenizer::file(const Token *tok) const
void Tokenizer::syntaxError(const Token *tok) void Tokenizer::syntaxError(const Token *tok)
{ {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; reportError(tok, Severity::error, "syntaxError", "syntax error");
if (tok) {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
}
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::error,
"syntax error",
"syntaxError",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
void Tokenizer::syntaxError(const Token *tok, char c) void Tokenizer::syntaxError(const Token *tok, char c)
{ {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; reportError(tok, Severity::error, "syntaxError",
if (tok) { std::string("Invalid number of character (") + c + ") " +
ErrorLogger::ErrorMessage::FileLocation loc; "when these macros are defined: '" + _configuration + "'.");
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
}
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::error,
std::string("Invalid number of character (") +
c +
") " +
"when these macros are defined: '" +
_configuration +
"'.",
"syntaxError",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
void Tokenizer::cppcheckError(const Token *tok) const void Tokenizer::cppcheckError(const Token *tok) const
{ {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; reportError(tok, Severity::error, "cppcheckError",
if (tok) { "Analysis failed. If the code is valid then please report this failure.");
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
}
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::error,
"Analysis failed. If the code is valid then please report this failure.",
"cppcheckError",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
@ -9576,24 +9440,8 @@ void Tokenizer::removeUnnecessaryQualification()
void Tokenizer::unnecessaryQualificationError(const Token *tok, const std::string &qualification) void Tokenizer::unnecessaryQualificationError(const Token *tok, const std::string &qualification)
{ {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; reportError(tok, Severity::portability, "unnecessaryQualification",
if (tok) { "Extra qualification \'" + qualification + "\' unnecessary and considered an error by many compilers.");
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
}
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::portability,
"Extra qualification \'" + qualification + "\' unnecessary and considered an error by many compilers.",
"unnecessaryQualification",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
} }
void Tokenizer::simplifyReturnStrncat() void Tokenizer::simplifyReturnStrncat()
@ -9741,3 +9589,32 @@ bool Tokenizer::isCPP() const
{ {
return Path::isCPP(getSourceFilePath()); return Path::isCPP(getSourceFilePath());
} }
void Tokenizer::reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const
{
const std::list<const Token*> callstack(1, tok);
reportError(callstack, severity, id, msg, inconclusive);
}
void Tokenizer::reportError(const std::list<const Token*>& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = (*it)->linenr();
loc.setfile(file(*it));
locationList.push_back(loc);
}
ErrorLogger::ErrorMessage errmsg(locationList, severity, msg, id, inconclusive);
if (!_files.empty())
errmsg.file0 = _files[0];
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
}

View File

@ -22,16 +22,14 @@
#define tokenizeH #define tokenizeH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "path.h" #include "errorlogger.h"
#include <string> #include <string>
#include <map> #include <map>
#include <list> #include <list>
#include <vector> #include <vector>
#include <set>
class Token; class Token;
class ErrorLogger;
class Settings; class Settings;
class SymbolDatabase; class SymbolDatabase;
class TimerResults; class TimerResults;
@ -705,6 +703,12 @@ public:
*/ */
bool duplicateDefinition(Token **tokPtr, const Token *name); bool duplicateDefinition(Token **tokPtr, const Token *name);
/**
* report error message
*/
void reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
void reportError(const std::list<const Token*>& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
/** /**
* duplicate enum definition error * duplicate enum definition error
*/ */