STL: added check for iterator usage

This commit is contained in:
Daniel Marjamäki 2009-02-10 19:40:21 +00:00
parent 5721e4469b
commit c1638996f9
7 changed files with 191 additions and 1 deletions

View File

@ -11,6 +11,7 @@ OBJECTS = src/checkbufferoverrun.o \
src/checkheaders.o \
src/checkmemoryleak.o \
src/checkother.o \
src/checkstl.o \
src/cppcheck.o \
src/cppcheckexecutor.o \
src/errorlogger.o \
@ -38,6 +39,7 @@ TESTOBJ = test/testbufferoverrun.o \
test/testredundantif.o \
test/testrunner.o \
test/testsimplifytokens.o \
test/teststl.o \
test/testsuite.o \
test/testtoken.o \
test/testtokenize.o \
@ -50,6 +52,7 @@ TESTOBJ = test/testbufferoverrun.o \
src/checkheaders.o \
src/checkmemoryleak.o \
src/checkother.o \
src/checkstl.o \
src/cppcheck.o \
src/cppcheckexecutor.o \
src/errorlogger.o \
@ -112,7 +115,10 @@ src/checkmemoryleak.o: src/checkmemoryleak.cpp src/checkmemoryleak.h src/tokeniz
src/checkother.o: src/checkother.cpp src/checkother.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h
g++ $(CXXFLAGS) -c -o src/checkother.o src/checkother.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/filelister.h
src/checkstl.o: src/checkstl.cpp src/checkstl.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h
g++ $(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
g++ $(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
@ -190,6 +196,9 @@ test/testrunner.o: test/testrunner.cpp test/testsuite.h src/errorlogger.h src/se
test/testsimplifytokens.o: test/testsimplifytokens.cpp test/testsuite.h src/errorlogger.h src/settings.h src/tokenize.h src/token.h
g++ $(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
g++ $(CXXFLAGS) -c -o test/teststl.o test/teststl.cpp
test/testsuite.o: test/testsuite.cpp test/testsuite.h src/errorlogger.h src/settings.h
g++ $(CXXFLAGS) -c -o test/testsuite.o test/testsuite.cpp

49
src/checkstl.cpp 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
*
* 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/
*/
#include "checkstl.h"
CheckStl::CheckStl(const Tokenizer *tokenizer, ErrorLogger *errorLogger)
: _tokenizer(tokenizer), _errorLogger(errorLogger)
{
}
CheckStl::~CheckStl()
{
}
void CheckStl::iterators()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "%var% = %var% . begin ( ) ; %var% != %var% . end ( ) ;"))
{
// Different iterators..
if (tok->str() != tok->tokAt(8)->str())
continue;
// Same container..
if (tok->tokAt(2)->str() == tok->tokAt(10)->str())
continue;
_errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(10)->str());
}
}
}

44
src/checkstl.h Normal file
View File

@ -0,0 +1,44 @@
/*
* 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
*
* 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 checkstlH
#define checkstlH
//---------------------------------------------------------------------------
#include "tokenize.h"
#include "errorlogger.h"
class CheckStl
{
public:
CheckStl(const Tokenizer *tokenizer, ErrorLogger *errorLogger);
~CheckStl();
void iterators();
private:
const Tokenizer *_tokenizer;
ErrorLogger *_errorLogger;
};
//---------------------------------------------------------------------------
#endif

View File

@ -28,6 +28,7 @@
#include "checkheaders.h"
#include "checkother.h"
#include "checkfunctionusage.h"
#include "checkstl.h"
#include "filelister.h"
#include <algorithm>
@ -407,6 +408,10 @@ 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();
}
Settings CppCheck::settings() const

View File

@ -419,6 +419,15 @@ public:
return s._checkCodingStyle;
}
void iteratorUsage(const Tokenizer *tokenizer, const Token *Location, const std::string &container1, const std::string &container2)
{
_writemsg(tokenizer, Location, "error", "Same iterator is used with both " + container1 + " and " + container2 + "", "iteratorUsage");
}
static bool iteratorUsage()
{
return true;
}
static std::string callStackToString(const std::list<FileLocation> &callStack);

70
test/teststl.cpp Normal file
View File

@ -0,0 +1,70 @@
/*
* 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
*
* 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/
*/
#include "../src/tokenize.h"
#include "../src/checkstl.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
class TestStl : public TestFixture
{
public:
TestStl() : TestFixture("TestStl")
{ }
private:
void run()
{
TEST_CASE(iterator1);
}
void check(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.iterators();
}
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());
}
};
REGISTER_TEST(TestStl)

View File

@ -111,6 +111,10 @@ int main()
err.push_back(Message("dangerousFunctiongets", Message::style, "Found 'gets'. You should use 'fgets' instead"));
err.push_back(Message("dangerousFunctionscanf", Message::style, "Found 'scanf'. You should use 'fgets' instead"));
// checkstl.cpp..
err.push_back(Message("iteratorUsage", Message::error, "Same iterator is used with both %1 and %2", "container1", "container2"));
// Generate code..
std::cout << "Generate code.." << std::endl;