From 508c171945ac3c9d1c4d4498911387be54bba2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Nov 2010 17:06:07 +0100 Subject: [PATCH] Symbol database: add support for class inline functions with classes --- lib/checkclass.cpp | 56 +++++++++++++++++++-------------------- test/testconstructors.cpp | 45 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 65f2b2fc4..8b4456063 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -232,18 +232,21 @@ void CheckClass::addNewFunction(SpaceInfo **info, const Token **tok) SpaceInfo *new_info = new SpaceInfo(this, tok1, *info); // skip to start of function - while (tok1->str() != "{") + while (tok1 && tok1->str() != "{") tok1 = tok1->next(); - new_info->classStart = tok1; - new_info->classEnd = tok1->link(); + if (tok1) + { + new_info->classStart = tok1; + new_info->classEnd = tok1->link(); - *info = new_info; + *info = new_info; - // add space - spaceInfoList.push_back(new_info); + // add space + spaceInfoList.push_back(new_info); - *tok = tok1; + *tok = tok1; + } } void CheckClass::addIfFunction(SpaceInfo **info, const Token **tok) @@ -459,6 +462,8 @@ void CheckClass::createSymbolDatabase() { // find the function implementation later tok = end->next(); + + info->functionList.push_back(function); } // inline function @@ -468,16 +473,14 @@ void CheckClass::createSymbolDatabase() function.hasBody = true; function.arg = function.argDef; - // skip over function body - tok = end->next(); - while (tok && tok->str() != "{") - tok = tok->next(); - if (!tok) - return; - tok = tok->link(); - } + info->functionList.push_back(function); - info->functionList.push_back(function); + const Token *tok2 = funcStart; + + addNewFunction(&info, &tok2); + + tok = tok2; + } } // nested class function? @@ -502,19 +505,7 @@ void CheckClass::createSymbolDatabase() // not in SpaceInfo else - { - // found a "?" skip until the end of statement is found to avoid detecting - // false functions.. - if (tok->str() == "?") - { - while (tok && !Token::Match(tok, "[;{}]")) - tok = tok->next(); - if (!tok) - break; - } - addIfFunction(&info, &tok); - } } std::list::iterator it; @@ -1417,7 +1408,14 @@ void CheckClass::privateFunctions() std::list FuncList; /** @todo embedded class have access to private functions */ - if (info->nestedList.empty()) + int nested = 0; + std::list::const_iterator ni; + for (ni = info->nestedList.begin(); ni != info->nestedList.end(); ++ni) + { + if ((*ni)->type != SpaceInfo::Function) + nested++; + } + if (!nested) { for (func = info->functionList.begin(); func != info->functionList.end(); ++func) { diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index a9ec365c7..dd6663521 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -490,6 +490,51 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + + check("class Foo {\n" + " void func1()\n" + " {\n" + " struct Fred\n" + " {\n" + " int a;\n" + " Fred() { a = 0; }\n" + " };\n" + " }\n" + "\n" + " void func2()\n" + " {\n" + " struct Fred\n" + " {\n" + " int b;\n" + " Fred() { b = 0; }\n" + " };\n" + " }\n" + "};\n"); + + ASSERT_EQUALS("", errout.str()); + + check("class Foo {\n" + " void func1()\n" + " {\n" + " struct Fred\n" + " {\n" + " int a;\n" + " Fred() { }\n" + " };\n" + " }\n" + "\n" + " void func2()\n" + " {\n" + " struct Fred\n" + " {\n" + " int b;\n" + " Fred() { }\n" + " };\n" + " }\n" + "};\n"); + + ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable not initialized in the constructor 'Fred::a'\n" + "[test.cpp:16]: (warning) Member variable not initialized in the constructor 'Fred::b'\n", errout.str()); } void initvar_chained_assign()