From c1638996f975fb3ada93ffa2f55dd0c4dc0dfcf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 10 Feb 2009 19:40:21 +0000 Subject: [PATCH] STL: added check for iterator usage --- Makefile | 11 +++++++- src/checkstl.cpp | 49 +++++++++++++++++++++++++++++++++ src/checkstl.h | 44 +++++++++++++++++++++++++++++ src/cppcheck.cpp | 5 ++++ src/errorlogger.h | 9 ++++++ test/teststl.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++ tools/errmsg.cpp | 4 +++ 7 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/checkstl.cpp create mode 100644 src/checkstl.h create mode 100644 test/teststl.cpp diff --git a/Makefile b/Makefile index a89737473..280d743da 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/checkstl.cpp b/src/checkstl.cpp new file mode 100644 index 000000000..5cc929488 --- /dev/null +++ b/src/checkstl.cpp @@ -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 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()); + } + } +} diff --git a/src/checkstl.h b/src/checkstl.h new file mode 100644 index 000000000..38b464930 --- /dev/null +++ b/src/checkstl.h @@ -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 @@ -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 diff --git a/src/errorlogger.h b/src/errorlogger.h index be91b4776..ef0f3ce91 100644 --- a/src/errorlogger.h +++ b/src/errorlogger.h @@ -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 &callStack); diff --git a/test/teststl.cpp b/test/teststl.cpp new file mode 100644 index 000000000..9fd5a2b50 --- /dev/null +++ b/test/teststl.cpp @@ -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 + +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) diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index b8c2f868f..998bef84c 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -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;