Refactorized CheckObsoleteFunctions:
- Mention all checked functions in getErrorMessages() and classInfo(), not only posix ones - Made members static and initialize them only once only once - Better support for global namespace
This commit is contained in:
parent
7368daa14a
commit
002b606a9c
|
@ -25,6 +25,10 @@
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
std::map<std::string, std::string> CheckObsoleteFunctions::_obsoleteStandardFunctions;
|
||||
std::map<std::string, std::string> CheckObsoleteFunctions::_obsoletePosixFunctions;
|
||||
std::map<std::string, std::string> CheckObsoleteFunctions::_obsoleteC99Functions;
|
||||
|
||||
|
||||
// Register this check class (by creating a static instance of it)
|
||||
namespace {
|
||||
|
@ -39,19 +43,11 @@ void CheckObsoleteFunctions::obsoleteFunctions()
|
|||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
||||
const bool cStandard = _settings->standards.c >= Standards::C99 ;
|
||||
|
||||
// Functions defined somewhere?
|
||||
for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
|
||||
const Scope* scope = symbolDatabase->functionScopes[i];
|
||||
_obsoleteStandardFunctions.erase(scope->className);
|
||||
_obsoletePosixFunctions.erase(scope->className);
|
||||
_obsoleteC99Functions.erase(scope->className);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
|
||||
const Scope* scope = symbolDatabase->functionScopes[i];
|
||||
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
|
||||
if (tok->isName() && tok->varId()==0 && (tok->next() && tok->next()->str() == "(") &&
|
||||
(!Token::Match(tok->previous(), ".|::") || Token::simpleMatch(tok->tokAt(-2), "std ::"))) {
|
||||
if (tok->isName() && tok->varId()==0 && (!tok->function() || !tok->function()->hasBody()) && tok->strAt(1) == "(" &&
|
||||
tok->strAt(-1) != "." && (!Token::Match(tok->tokAt(-2), "%name% ::") || Token::simpleMatch(tok->tokAt(-2), "std ::"))) {
|
||||
|
||||
std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
|
||||
if (it != _obsoleteStandardFunctions.end()) {
|
||||
|
|
|
@ -45,7 +45,6 @@ public:
|
|||
/** This constructor is used when running checks. */
|
||||
CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
: Check(myName(), tokenizer, settings, errorLogger) {
|
||||
initObsoleteFunctions();
|
||||
}
|
||||
|
||||
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
||||
|
@ -58,9 +57,9 @@ public:
|
|||
|
||||
private:
|
||||
/* function name / error message */
|
||||
std::map<std::string, std::string> _obsoleteStandardFunctions;
|
||||
std::map<std::string, std::string> _obsoletePosixFunctions;
|
||||
std::map<std::string, std::string> _obsoleteC99Functions;
|
||||
static std::map<std::string, std::string> _obsoleteStandardFunctions;
|
||||
static std::map<std::string, std::string> _obsoletePosixFunctions;
|
||||
static std::map<std::string, std::string> _obsoleteC99Functions;
|
||||
|
||||
/** init obsolete functions list ' */
|
||||
void initObsoleteFunctions() {
|
||||
|
@ -125,10 +124,12 @@ private:
|
|||
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
||||
CheckObsoleteFunctions c(0, settings, errorLogger);
|
||||
|
||||
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
|
||||
for (; it!=itend; ++it) {
|
||||
c.reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
|
||||
}
|
||||
for (std::map<std::string, std::string>::const_iterator it = _obsoleteStandardFunctions.begin(); it != _obsoleteStandardFunctions.end(); ++it)
|
||||
c.reportError(0, Severity::style, "obsoleteFunctions" + it->first, it->second);
|
||||
for (std::map<std::string, std::string>::const_iterator it = _obsoleteC99Functions.begin(); it != _obsoleteC99Functions.end(); ++it)
|
||||
c.reportError(0, Severity::style, "obsoleteFunctions" + it->first, it->second);
|
||||
for (std::map<std::string, std::string>::const_iterator it = _obsoletePosixFunctions.begin(); it != _obsoletePosixFunctions.end(); ++it)
|
||||
c.reportError(0, Severity::style, "obsoleteFunctions" + it->first, it->second);
|
||||
}
|
||||
|
||||
static std::string myName() {
|
||||
|
@ -137,10 +138,12 @@ private:
|
|||
|
||||
std::string classInfo() const {
|
||||
std::string info = "Warn if any of these obsolete functions are used:\n";
|
||||
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
|
||||
for (; it!=itend; ++it) {
|
||||
for (std::map<std::string, std::string>::const_iterator it = _obsoleteStandardFunctions.begin(); it != _obsoleteStandardFunctions.end(); ++it)
|
||||
info += "- " + it->first + "\n";
|
||||
for (std::map<std::string, std::string>::const_iterator it = _obsoleteC99Functions.begin(); it != _obsoleteC99Functions.end(); ++it)
|
||||
info += "- " + it->first + "\n";
|
||||
for (std::map<std::string, std::string>::const_iterator it = _obsoletePosixFunctions.begin(); it != _obsoletePosixFunctions.end(); ++it)
|
||||
info += "- " + it->first + "\n";
|
||||
}
|
||||
return info;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -251,8 +251,10 @@ private:
|
|||
check("void f(char * str)\n"
|
||||
"{\n"
|
||||
" char *x = std::gets(str);\n"
|
||||
" char *y = gets(str);\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n"
|
||||
"[test.cpp:4]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
|
||||
}
|
||||
|
||||
// multiple use
|
||||
|
|
Loading…
Reference in New Issue