From a3f9b5c07dc08d06862315ea9addb5b99a1de1d8 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Sat, 30 Mar 2013 13:49:56 +0100 Subject: [PATCH] Fixed #4686 (Prefer prefix ++/-- operators for non-primitive types with template iterator) --- lib/symboldatabase.cpp | 9 ++++++--- test/testpostfixoperator.cpp | 9 +++++++++ test/testpreprocessor.cpp | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 8ebdb0550..2349c6440 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2302,9 +2302,12 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const if (found) { localVarTok = skipPointers(closeTok->next()); - if (Token::Match(localVarTok, ":: %type% %var% ;|=")) { - localTypeTok = localVarTok->next(); - localVarTok = localVarTok->tokAt(2); + if (Token::Match(localVarTok, ":: %type% %var% ;|=|(")) { + if (localVarTok->strAt(3) != "(" || + Token::simpleMatch(localVarTok->linkAt(3), ") ;")) { + localTypeTok = localVarTok->next(); + localVarTok = localVarTok->tokAt(2); + } } } } else if (Token::Match(localTypeTok, "%type%")) { diff --git a/test/testpostfixoperator.cpp b/test/testpostfixoperator.cpp index be1093b1d..1e226b856 100644 --- a/test/testpostfixoperator.cpp +++ b/test/testpostfixoperator.cpp @@ -62,6 +62,7 @@ private: TEST_CASE(test2168); TEST_CASE(pointer); // #2321 - postincrement of pointer is OK TEST_CASE(testHangWithInvalidCode); // #2847 - cppcheck hangs with 100% cpu load + TEST_CASE(testtemplate); // #4686 } void testHangWithInvalidCode() { @@ -345,6 +346,14 @@ private: "p++;\n"); ASSERT_EQUALS("", errout.str()); } + + void testtemplate() { + check("bool foo() {\n" + " std::vector::iterator aIter(aImport.begin());\n" + " aIter++;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str()); + } }; REGISTER_TEST(TestPostfixOperator) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index b5b546057..75afa16a2 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -322,6 +322,7 @@ private: void readCode4() { const char code[] = "char c = '\\ ';"; Settings settings; + errout.str(""); Preprocessor preprocessor(&settings, this); std::istringstream istr(code); ASSERT_EQUALS("char c = '\\ ';", preprocessor.read(istr,"test.c"));