diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 0f4dba5f3..7250f532f 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -111,7 +111,7 @@ void CheckClass::addFunction(SpaceInfo **info, const Token **tok) unsigned int path_length = 0; // back up to head of path - while (tok1->previous()->str() == "::") + while (tok1->previous() && tok1->previous()->str() == "::") { path = tok1->str() + " :: " + path; tok1 = tok1->tokAt(-2); @@ -227,6 +227,28 @@ void CheckClass::addNewFunction(SpaceInfo **info, const Token **tok) *tok = tok1; } +void CheckClass::addIfFunction(SpaceInfo **info, const Token **tok) +{ + const Token *funcStart = 0; + const Token *argStart = 0; + + // function? + if (isFunction(*tok, &funcStart, &argStart)) + { + // has body? + if (Token::Match(argStart->link(), ") const| {|:")) + { + // class function + if ((*tok)->previous() && (*tok)->previous()->str() == "::") + addFunction(info, tok); + + // regular function + else + addNewFunction(info, tok); + } + } +} + void CheckClass::createSymbolDatabase() { // Multiple calls => bail out @@ -441,50 +463,12 @@ void CheckClass::createSymbolDatabase() } } else if (info->type == SpaceInfo::Namespace) - { - const Token *funcStart = 0; - const Token *argStart = 0; - - // function? - if (isFunction(tok, &funcStart, &argStart)) - { - // has body? - if (Token::Match(argStart->link(), ") const| {|:")) - { - // class function - if (tok->previous()->str() == "::") - addFunction(&info, &tok); - - // regular function - else - addNewFunction(&info, &tok); - } - } - } + addIfFunction(&info, &tok); } // not in SpaceInfo else - { - const Token *funcStart = 0; - const Token *argStart = 0; - - // function? - if (isFunction(tok, &funcStart, &argStart)) - { - // has body? - if (Token::Match(argStart->link(), ") const| {|:")) - { - // class function - if (tok->previous() && tok->previous()->str() == "::") - addFunction(&info, &tok); - - // regular function - else - addNewFunction(&info, &tok); - } - } - } + addIfFunction(&info, &tok); } std::list::iterator it; diff --git a/lib/checkclass.h b/lib/checkclass.h index 717859ca4..34e637e80 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -318,6 +318,8 @@ private: void addFunction(SpaceInfo **info, const Token **tok); void addNewFunction(SpaceInfo **info, const Token **tok); + void addIfFunction(SpaceInfo **info, const Token **tok); + /** @brief Information about all namespaces/classes/structrues */ std::list spaceInfoList; diff --git a/test/testclass.cpp b/test/testclass.cpp index 86184b921..42b1a2718 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -4767,6 +4767,11 @@ private: " static void function_declaration_after(void) __attribute__((__used__));\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkConst("testing::testing()\n" + "{\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } };