|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +#include "tokenize.h" |
| 22 | +#include "checkboost.h" |
| 23 | +#include "testsuite.h" |
| 24 | +#include <sstream> |
| 25 | + |
| 26 | +extern std::ostringstream errout; |
| 27 | + |
| 28 | +class TestBoost : public TestFixture { |
| 29 | +public: |
| 30 | + TestBoost() : TestFixture("TestBoost") |
| 31 | + { } |
| 32 | + |
| 33 | +private: |
| 34 | + void run() { |
| 35 | + TEST_CASE(BoostForeachContainerModification) |
| 36 | + } |
| 37 | + |
| 38 | + void check(const std::string &code) { |
| 39 | + // Clear the error buffer.. |
| 40 | + errout.str(""); |
| 41 | + |
| 42 | + Settings settings; |
| 43 | + settings.addEnabled("style"); |
| 44 | + settings.addEnabled("performance"); |
| 45 | + |
| 46 | + // Tokenize.. |
| 47 | + Tokenizer tokenizer(&settings, this); |
| 48 | + std::istringstream istr(code.c_str()); |
| 49 | + tokenizer.tokenize(istr, "test.cpp"); |
| 50 | + tokenizer.simplifyTokenList(); |
| 51 | + |
| 52 | + // Check.. |
| 53 | + CheckBoost checkBoost; |
| 54 | + checkBoost.runSimplifiedChecks(&tokenizer, &settings, this); |
| 55 | + } |
| 56 | + |
| 57 | + void BoostForeachContainerModification() { |
| 58 | + check("void f() {\n" |
| 59 | + " vector<int> data;\n" |
| 60 | + " BOOST_FOREACH(int i, data) {\n" |
| 61 | + " data.push_back(123);\n" |
| 62 | + " }\n" |
| 63 | + "}"); |
| 64 | + ASSERT_EQUALS("[test.cpp:4]: (error) BOOST_FOREACH caches the end() iterator. It's undefined behavior if you modify the container.\n", errout.str()); |
| 65 | + |
| 66 | + check("void f() {\n" |
| 67 | + " set<int> data;\n" |
| 68 | + " BOOST_FOREACH(int i, data) {\n" |
| 69 | + " data.insert(123);\n" |
| 70 | + " }\n" |
| 71 | + "}"); |
| 72 | + ASSERT_EQUALS("[test.cpp:4]: (error) BOOST_FOREACH caches the end() iterator. It's undefined behavior if you modify the container.\n", errout.str()); |
| 73 | + |
| 74 | + check("void f() {\n" |
| 75 | + " set<int> data;\n" |
| 76 | + " BOOST_FOREACH(const int &i, data) {\n" |
| 77 | + " data.erase(123);\n" |
| 78 | + " }\n" |
| 79 | + "}"); |
| 80 | + ASSERT_EQUALS("[test.cpp:4]: (error) BOOST_FOREACH caches the end() iterator. It's undefined behavior if you modify the container.\n", errout.str()); |
| 81 | + |
| 82 | + // Check single line usage |
| 83 | + check("void f() {\n" |
| 84 | + " set<int> data;\n" |
| 85 | + " BOOST_FOREACH(const int &i, data)\n" |
| 86 | + " data.clear();\n" |
| 87 | + "}"); |
| 88 | + ASSERT_EQUALS("[test.cpp:4]: (error) BOOST_FOREACH caches the end() iterator. It's undefined behavior if you modify the container.\n", errout.str()); |
| 89 | + |
| 90 | + // Container returned as result of a function -> Be quiet |
| 91 | + check("void f() {\n" |
| 92 | + " BOOST_FOREACH(const int &i, get_data())\n" |
| 93 | + " data.insert(i);\n" |
| 94 | + "}"); |
| 95 | + ASSERT_EQUALS("", errout.str()); |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +REGISTER_TEST(TestBoost) |
0 commit comments