refactoring: first step - started with checkstl

This commit is contained in:
Daniel Marjamäki 2009-03-18 22:40:38 +01:00
parent f37dd4f143
commit a573c62cd5
6 changed files with 291 additions and 245 deletions

View File

@ -124,10 +124,10 @@ src/checkother.o: src/checkother.cpp src/checkother.h src/tokenize.h src/setting
src/checksecurity.o: src/checksecurity.cpp src/checksecurity.h src/errorlogger.h src/settings.h src/token.h src/tokenize.h
$(CXX) $(CXXFLAGS) -c -o src/checksecurity.o src/checksecurity.cpp
src/checkstl.o: src/checkstl.cpp src/checkstl.h src/errorlogger.h src/settings.h src/token.h src/tokenize.h
src/checkstl.o: src/checkstl.cpp src/checkstl.h src/check.h src/settings.h src/tokenize.h src/errorlogger.h src/token.h
$(CXX) $(CXXFLAGS) -c -o src/checkstl.o src/checkstl.cpp
src/cppcheck.o: src/cppcheck.cpp src/cppcheck.h src/settings.h src/errorlogger.h src/checkfunctionusage.h src/tokenize.h src/token.h src/preprocessor.h src/checkmemoryleak.h src/checkbufferoverrun.h src/checkdangerousfunctions.h src/checkclass.h src/checkheaders.h src/checkother.h src/checkstl.h src/filelister.h
src/cppcheck.o: src/cppcheck.cpp src/cppcheck.h src/settings.h src/errorlogger.h src/checkfunctionusage.h src/tokenize.h src/token.h src/preprocessor.h src/checkmemoryleak.h src/checkbufferoverrun.h src/checkdangerousfunctions.h src/checkclass.h src/checkheaders.h src/checkother.h src/filelister.h
$(CXX) $(CXXFLAGS) -c -o src/cppcheck.o src/cppcheck.cpp
src/cppcheckexecutor.o: src/cppcheckexecutor.cpp src/cppcheckexecutor.h src/errorlogger.h src/settings.h src/cppcheck.h src/checkfunctionusage.h src/tokenize.h src/token.h src/threadexecutor.h
@ -211,7 +211,7 @@ test/testsecurity.o: test/testsecurity.cpp src/tokenize.h src/settings.h src/err
test/testsimplifytokens.o: test/testsimplifytokens.cpp test/testsuite.h src/errorlogger.h src/settings.h src/tokenize.h src/token.h
$(CXX) $(CXXFLAGS) -c -o test/testsimplifytokens.o test/testsimplifytokens.cpp
test/teststl.o: test/teststl.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkstl.h test/testsuite.h
test/teststl.o: test/teststl.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkstl.h src/check.h test/testsuite.h
$(CXX) $(CXXFLAGS) -c -o test/teststl.o test/teststl.cpp
test/testsuite.o: test/testsuite.cpp test/testsuite.h src/errorlogger.h src/settings.h

49
src/check.h Normal file
View File

@ -0,0 +1,49 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
* Leandro Penz, Kimmo Varis, Vesa Pikki
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/
*/
#ifndef checkH
#define checkH
#include "settings.h"
class Tokenizer;
class ErrorLogger;
class Check
{
public:
Check(const Tokenizer * const tokenizer, const Settings &settings, ErrorLogger *errorLogger)
: _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger)
{ }
virtual ~Check()
{ }
/** run checks.. */
virtual void runChecks() = 0;
protected:
const Tokenizer * const _tokenizer;
const Settings &_settings;
ErrorLogger *_errorLogger;
};
#endif

View File

@ -18,21 +18,8 @@
*/
#include "checkstl.h"
#include "errorlogger.h"
#include "token.h"
#include "tokenize.h"
CheckStl::CheckStl(const Tokenizer *tokenizer, ErrorLogger *errorLogger)
: _tokenizer(tokenizer), _errorLogger(errorLogger)
{
}
CheckStl::~CheckStl()
{
}
#include "token.h"
void CheckStl::iterators()
@ -48,7 +35,7 @@ void CheckStl::iterators()
// Same container..
if (tok->tokAt(2)->str() == tok->tokAt(10)->str())
continue;
_errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(10)->str());
errorIteratorUsage(tok, tok->strAt(2), tok->strAt(10));
}
// it = foo.begin();
@ -61,7 +48,7 @@ void CheckStl::iterators()
// Same container..
if (tok->tokAt(2)->str() == tok->tokAt(12)->str())
continue;
_errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(12)->str());
errorIteratorUsage(tok, tok->strAt(2), tok->strAt(12));
}
}
}
@ -263,3 +250,12 @@ void CheckStl::pushback()
}
void CheckStl::errorIteratorUsage(const Token * const tok, const char container1[], const char container2[])
{
ErrorLogger::ErrorMessage errmsg;
errmsg._severity = "error";
errmsg._msg = std::string("Same iterator is used with both ") + container1 + " and " + container2;
errmsg._id = "iteratorUsage";
_errorLogger->reportErr(errmsg);
}

View File

@ -23,15 +23,27 @@
#define checkstlH
//---------------------------------------------------------------------------
class ErrorLogger;
class Token;
class Tokenizer;
#include "check.h"
class CheckStl
class Token;
class CheckStl : public Check
{
public:
CheckStl(const Tokenizer *tokenizer, ErrorLogger *errorLogger);
~CheckStl();
CheckStl(const Tokenizer * const tokenizer, const Settings &settings, ErrorLogger *errorLogger)
: Check(tokenizer, settings, errorLogger)
{
}
void runChecks()
{
stlOutOfBounds();
iterators();
erase();
pushback();
}
/**
@ -57,8 +69,6 @@ public:
void pushback();
private:
const Tokenizer *_tokenizer;
ErrorLogger *_errorLogger;
/**
* Helper function used by the 'erase' function
@ -66,6 +76,8 @@ private:
* @param it iterator token
*/
void eraseCheckLoop(const Token *it);
void errorIteratorUsage(const Token * const tok, const char container1[], const char container2[]);
};
//---------------------------------------------------------------------------

View File

@ -28,7 +28,6 @@
#include "checkheaders.h"
#include "checkother.h"
#include "checkfunctionusage.h"
#include "checkstl.h"
#include "filelister.h"
#include <algorithm>
@ -482,19 +481,6 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Unusual pointer arithmetic
if (ErrorLogger::strPlusChar())
checkOther.strPlusChar();
CheckStl checkStl(&_tokenizer, this);
if (ErrorLogger::iteratorUsage())
checkStl.iterators();
if (ErrorLogger::stlOutOfBounds())
checkStl.stlOutOfBounds();
if (ErrorLogger::erase())
checkStl.erase();
if (ErrorLogger::pushback())
checkStl.pushback();
}
Settings CppCheck::settings() const

View File

@ -33,9 +33,9 @@ public:
{ }
private:
void run()
{
/*
TEST_CASE(iterator1);
TEST_CASE(iterator2);
TEST_CASE(STLSize);
@ -48,232 +48,235 @@ private:
TEST_CASE(pushback1);
TEST_CASE(invalidcode);
*/
}
void check(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
/*
void check(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Clear the error buffer..
errout.str("");
// Clear the error buffer..
errout.str("");
// Check char variable usage..
CheckStl checkStl(&tokenizer, this);
checkStl.iterators();
checkStl.stlOutOfBounds();
}
// Check char variable usage..
CheckStl checkStl(&tokenizer, this);
checkStl.iterators();
checkStl.stlOutOfBounds();
}
void iterator1()
{
check("void foo()\n"
"{\n"
" for (it = foo.begin(); it != bar.end(); ++it)\n"
" { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
}
void iterator1()
{
check("void foo()\n"
"{\n"
" for (it = foo.begin(); it != bar.end(); ++it)\n"
" { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
}
void iterator2()
{
check("void foo()\n"
"{\n"
" it = foo.begin();\n"
" while (it != bar.end())\n"
" {\n"
" ++it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
}
void iterator2()
{
check("void foo()\n"
"{\n"
" it = foo.begin();\n"
" while (it != bar.end())\n"
" {\n"
" ++it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
}
void STLSize()
{
check("void foo()\n"
"{\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:6]: (error) When ii == size(), foo [ ii ] is out of bounds\n"), errout.str());
}
void STLSizeNoErr()
{
void STLSize()
{
check("void foo()\n"
"{\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii < foo.size(); ++ii)\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:6]: (error) When ii == size(), foo [ ii ] is out of bounds\n"), errout.str());
}
void STLSizeNoErr()
{
check("void foo()\n"
"{\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
{
check("void foo()\n"
"{\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" if (ii == foo.size())\n"
" {\n"
" }\n"
" else\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
" }\n"
"}\n");
// TODO ASSERT_EQUALS(std::string(""), errout.str());
}
}
void checkErase(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Clear the error buffer..
errout.str("");
// Check char variable usage..
CheckStl checkStl(&tokenizer, this);
checkStl.erase();
}
void erase()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str());
}
void eraseBreak()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" break;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseReturn()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseGoto()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" goto abc;\n"
" }\n"
"bar:\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseAssign()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" it = foo.begin();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkPushback(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Clear the error buffer..
errout.str("");
// Check char variable usage..
CheckStl checkStl(&tokenizer, this);
checkStl.pushback();
}
void pushback1()
{
checkPushback("void f()\n"
{
check("void foo()\n"
"{\n"
" std::vector<int>::const_iterator it = foo.begin();\n"
" foo.push_back(123);\n"
" *it;\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii < foo.size(); ++ii)\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str());
}
ASSERT_EQUALS(std::string(""), errout.str());
}
void invalidcode()
{
check("void f()\n"
"{\n"
" for ( \n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
{
check("void foo()\n"
"{\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
{
check("void foo()\n"
"{\n"
" std::vector<int> foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" if (ii == foo.size())\n"
" {\n"
" }\n"
" else\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
" }\n"
"}\n");
// TODO ASSERT_EQUALS(std::string(""), errout.str());
}
}
void checkErase(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Clear the error buffer..
errout.str("");
// Check char variable usage..
CheckStl checkStl(&tokenizer, this);
checkStl.erase();
}
void erase()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str());
}
void eraseBreak()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" break;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseReturn()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseGoto()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" goto abc;\n"
" }\n"
"bar:\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseAssign()
{
checkErase("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" it = foo.begin();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkPushback(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Clear the error buffer..
errout.str("");
// Check char variable usage..
CheckStl checkStl(&tokenizer, this);
checkStl.pushback();
}
void pushback1()
{
checkPushback("void f()\n"
"{\n"
" std::vector<int>::const_iterator it = foo.begin();\n"
" foo.push_back(123);\n"
" *it;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str());
}
void invalidcode()
{
check("void f()\n"
"{\n"
" for ( \n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
*/
};
REGISTER_TEST(TestStl)