From 8e5949c6ce61c4a01eb9af701b52c0174b2afac3 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 17 Apr 2012 12:54:01 +0200 Subject: [PATCH] Added several C++11 algorithms and containers to CheckStl Added pattern "> %varid%" to CheckStl::stlBoundries() Fixed message in checkOther (#1320) --- lib/checkother.cpp | 2 +- lib/checkstl.cpp | 20 +++++++++++++------- test/testother.cpp | 10 +++++----- test/teststl.cpp | 6 ++++++ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a41a20422..d18e2e37e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2470,7 +2470,7 @@ void CheckOther::checkMisusedScopedObject() void CheckOther::misusedScopeObjectError(const Token *tok, const std::string& varname) { reportError(tok, Severity::error, - "unusedScopedObject", "instance of \"" + varname + "\" object destroyed immediately"); + "unusedScopedObject", "Instance of \"" + varname + "\" object destroyed immediately."); } //--------------------------------------------------------------------------- diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index cac10dac0..eb9779dbe 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -190,10 +190,14 @@ void CheckStl::mismatchingContainersError(const Token *tok) void CheckStl::mismatchingContainers() { static const char* const algorithm2_strings[] = { // func(begin1, end1 - "adjacent_find", "binary_search", "count", "count_if", "equal", "equal_range", "find", "find_if", "for_each", "generate", "lower_bound", "make_heap", - "max_element", "min_element", "mismatch", "next_permutation", "partition", "pop_heap", "prev_permutation", "push_heap", "random_shuffle", "remove", - "remove_copy", "remove_copy_if", "remove_if", "replace", "replace_copy", "replace_copy_if", "replace_if", "reverse", "reverse_copy", "search_n", - "sort", "sort_heap", "stable_partition", "stable_sort", "swap_ranges", "transform", "unique", "unique_copy", "upper_bound" + "adjacent_find", "all_of", "any_of", "binary_search", "copy", "copy_if", "count", "count_if", "equal", "equal_range", + "find", "find_if", "find_if_not", "for_each", "generate", "is_heap", "is_heap_until", "is_partitioned", + "is_permutation", "is_sorted", "is_sorted_until", "lower_bound", "make_heap", "max_element", "minmax_element", + "min_element", "mismatch", "move", "move_backward", "next_permutation", "none_of", "partition", "partition_copy", + "partition_point", "pop_heap", "prev_permutation", "push_heap", "random_shuffle", "remove", "remove_copy", + "remove_copy_if", "remove_if", "replace", "replace_copy", "replace_copy_if", "replace_if", "reverse", "reverse_copy", + "search_n", "shuffle", "sort", "sort_heap", "stable_partition", "stable_sort", "swap_ranges", "transform", "unique", + "unique_copy", "upper_bound" }; static const char* const algorithm22_strings[] = { // func(begin1, end1, begin2, end2 "find_end", "find_first_of", "includes", "lexicographical_compare", "merge", "partial_sort_copy", @@ -664,7 +668,7 @@ void CheckStl::invalidPointerError(const Token *tok, const std::string &pointer_ void CheckStl::stlBoundries() { // containers (not the vector).. - static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset"; + static const char STL_CONTAINER_LIST[] = "bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset"; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Declaring iterator.. @@ -691,6 +695,8 @@ void CheckStl::stlBoundries() --indentlevel; } else if (Token::Match(tok2, "!!* %varid% <", iteratorid)) { stlBoundriesError(tok2, container_name); + } else if (Token::Match(tok2, "> %varid%", iteratorid)) { + stlBoundriesError(tok2, container_name); } } } @@ -861,7 +867,7 @@ bool CheckStl::isStlContainer(unsigned int varid) type = type->tokAt(2); // all possible stl containers as a token - static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; + static const char STL_CONTAINER_LIST[] = "array|bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; // check if it's an stl template if (Token::Match(type, STL_CONTAINER_LIST)) @@ -1216,7 +1222,7 @@ void CheckStl::checkAutoPointer() return; std::set autoPtrVarId; - static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; + static const char STL_CONTAINER_LIST[] = "array|bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "auto_ptr <")) { diff --git a/test/testother.cpp b/test/testother.cpp index 9c3fdf370..6f9466f7d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2385,7 +2385,7 @@ private: CheckOther checkOther(&tokenizer, &settings, this); checkOther.checkMisusedScopedObject(); - ASSERT_EQUALS("[trac1132.cpp:16]: (error) instance of \"Lock\" object destroyed immediately\n", errout.str()); + ASSERT_EQUALS("[trac1132.cpp:16]: (error) Instance of \"Lock\" object destroyed immediately.\n", errout.str()); } void trac3693() { @@ -2433,7 +2433,7 @@ private: " return 0 ;\n" "}\n" ); - ASSERT_EQUALS("[test.cpp:4]: (error) instance of \"NotAFunction\" object destroyed immediately\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Instance of \"NotAFunction\" object destroyed immediately.\n", errout.str()); } void testMisusedScopeObjectPicksStruct() { @@ -2444,7 +2444,7 @@ private: " return true ;\n" "}\n" ); - ASSERT_EQUALS("[test.cpp:4]: (error) instance of \"NotAClass\" object destroyed immediately\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Instance of \"NotAClass\" object destroyed immediately.\n", errout.str()); } void testMisusedScopeObjectDoesNotPickIf() { @@ -2501,7 +2501,7 @@ private: " Foo();\n" "}\n" ); - ASSERT_EQUALS("[test.cpp:7]: (error) instance of \"Foo\" object destroyed immediately\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (error) Instance of \"Foo\" object destroyed immediately.\n", errout.str()); } void testMisusedScopeObjectDoesNotPickUsedObject() { @@ -2529,7 +2529,7 @@ private: "}\n"; check(code, "test.cpp"); - ASSERT_EQUALS("[test.cpp:7]: (error) instance of \"cb_watch_bool\" object destroyed immediately\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (error) Instance of \"cb_watch_bool\" object destroyed immediately.\n", errout.str()); check(code, "test.c"); ASSERT_EQUALS("", errout.str()); diff --git a/test/teststl.cpp b/test/teststl.cpp index 7916b532d..10fd16409 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1105,6 +1105,12 @@ private: ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous container iterator compare using < operator for " + stlCont[i] + "\n", errout.str()); } + + check("void f() {\n" + " std::forward_list::iterator it;\n" + " for (it = ab.begin(); ab.end() > it; ++it) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous container iterator compare using < operator for forward_list\n", errout.str()); } void stlBoundries2() {