From 7e3e5d628d78712922927f5b29f1ef3c88e137ff Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 23 Mar 2011 20:19:32 -0400 Subject: [PATCH] better fix for #2672 (False positive: function can be const, nested classes declared in one line) --- lib/tokenize.cpp | 33 +++++++++++---------------------- test/testclass.cpp | 24 ++++++++++++++++++++++++ test/testnullpointer.cpp | 2 +- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 01ee0c1ac..5c978a85f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5673,6 +5673,16 @@ void Tokenizer::simplifyVarDecl() if (Token::Match(tok2, "%type% *| %var% , %type% *| %var%")) continue; + // check for qualification.. + if (Token::Match(tok2, "%type% :: %type%")) + { + while (tok2 && Token::Match(tok2, "%type% ::")) + { + typelen += 2; + tok2 = tok2->tokAt(2); + } + } + if (Token::Match(tok2, "%type% *| %var% ,|=")) { const bool isPointer = (tok2->next()->str() == "*"); @@ -5715,17 +5725,6 @@ void Tokenizer::simplifyVarDecl() } } - else if (Token::Match(tok2, "%type% :: %type% %var% ,|=")) - { - if (tok2->tokAt(3)->str() != "operator") - { - tok2 = tok2->tokAt(4); // The ',' token - typelen = 3; - } - else - tok2 = NULL; - } - else if (Token::Match(tok2, "%type% %var% [ %num% ] ,|=|[") || Token::Match(tok2, "%type% %var% [ %var% ] ,|=|[")) { @@ -5758,17 +5757,8 @@ void Tokenizer::simplifyVarDecl() tok2 = tok2->tokAt(6); // The ',' token } - else if (Token::Match(tok2, "std :: %type% <") || Token::Match(tok2, "%type% <")) + else if (Token::Match(tok2, "%type% <")) { - // - // Deal with templates and standart types - // - if (Token::simpleMatch(tok2, "std ::")) - { - typelen += 2; - tok2 = tok2->tokAt(2); - } - typelen += 2; tok2 = tok2->tokAt(2); size_t indentlevel = 1; @@ -5827,7 +5817,6 @@ void Tokenizer::simplifyVarDecl() typelen = 0; } - if (tok2) { if (tok2->str() == ",") diff --git a/test/testclass.cpp b/test/testclass.cpp index 86822b058..255a4e69e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -168,6 +168,7 @@ private: TEST_CASE(const45); // ticket #2664 TEST_CASE(const46); // ticket #2636 TEST_CASE(const47); // ticket #2670 + TEST_CASE(const48); // ticket #2672 TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningArrayElementIsNotAConstOperation); TEST_CASE(constoperator1); // operator< can often be const @@ -5283,6 +5284,29 @@ private: ASSERT_EQUALS("[test.cpp:5]: (information) Technically the member function 'Altren::bar' can be const.\n", errout.str()); } + void const48() // ticket 2672 + { + checkConst("class S0 {\n" + " class S1 {\n" + " class S2 {\n" + " class S3 {\n" + " class S4 { };\n" + " };\n" + " };\n" + " };\n" + "};\n" + "class TextIterator {\n" + " S0::S1::S2::S3::S4 mCurrent, mSave;\n" + "public:\n" + " bool setTagColour();\n" + "};\n" + "bool TextIterator::setTagColour() {\n" + " mSave = mCurrent;\n" + "}\n"); + + ASSERT_EQUALS("", errout.str()); + } + void assigningPointerToPointerIsNotAConstOperation() { checkConst("struct s\n" diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 2a1cd7a17..811726fcb 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -831,7 +831,7 @@ private: " std::string * x = 0;\n" " *x = \"test\";\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: x\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); } // Check if pointer is null and the dereference it