From 6e03e7dca2a96c95cff10e7caff40718ffa5fcd5 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Wed, 1 Jul 2015 07:50:13 +0200 Subject: [PATCH] Remove some code checking for invalid class hierarchy which got obsolete since 480a5672b046ef0017af8bf59b6284895c22123d. Run astyle --- lib/checkclass.cpp | 6 +++--- lib/checknonreentrantfunctions.cpp | 23 +++++++++++--------- lib/checknonreentrantfunctions.h | 2 +- lib/checkunusedvar.cpp | 2 +- lib/symboldatabase.cpp | 34 +++++++++++++++--------------- lib/symboldatabase.h | 4 ++-- lib/token.cpp | 7 +++--- test/testcondition.cpp | 2 +- test/testgarbage.cpp | 27 ++++++++++++------------ test/testother.cpp | 2 +- test/testsymboldatabase.cpp | 32 ++++++++++++++-------------- 11 files changed, 71 insertions(+), 70 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index c92c55501..d32a91b98 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1764,7 +1764,7 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const } // not found in this class - if (!scope->definedType->derivedFrom.empty() && !scope->definedType->hasCircularDependencies()) { + if (!scope->definedType->derivedFrom.empty()) { // check each base class for (std::size_t i = 0; i < scope->definedType->derivedFrom.size(); ++i) { // find the base class @@ -1794,7 +1794,7 @@ bool CheckClass::isMemberFunc(const Scope *scope, const Token *tok) const const Type *derivedFrom = scope->definedType->derivedFrom[i].type; // find the function in the base class - if (derivedFrom && derivedFrom->classScope && !derivedFrom->hasCircularDependencies()) { + if (derivedFrom && derivedFrom->classScope) { if (isMemberFunc(derivedFrom->classScope, tok)) return true; } @@ -1817,7 +1817,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const const Type *derivedFrom = scope->definedType->derivedFrom[i].type; // find the function in the base class - if (derivedFrom && derivedFrom->classScope && !derivedFrom->hasCircularDependencies()) { + if (derivedFrom && derivedFrom->classScope) { if (isConstMemberFunc(derivedFrom->classScope, tok)) return true; } diff --git a/lib/checknonreentrantfunctions.cpp b/lib/checknonreentrantfunctions.cpp index e6fd874eb..879241a06 100644 --- a/lib/checknonreentrantfunctions.cpp +++ b/lib/checknonreentrantfunctions.cpp @@ -33,17 +33,18 @@ namespace { namespace { const std::set _nonReentrantFunctions = make_container< std::set > () - << "localtime" << "gmtime" << "strtok" << "gethostbyname" << "gethostbyaddr" << "getservbyname" - << "getservbyport" << "crypt" << "ttyname" << "gethostbyname2" - << "getprotobyname" << "getnetbyname" << "getnetbyaddr" << "getrpcbyname" << "getrpcbynumber" << "getrpcent" - << "ctermid" << "readdir" << "getlogin" << "getpwent" << "getpwnam" << "getpwuid" << "getspent" - << "fgetspent" << "getspnam" << "getgrnam" << "getgrgid" << "getnetgrent" << "tempnam" << "fgetpwent" - << "fgetgrent" << "ecvt" << "gcvt" << "getservent" << "gethostent" << "getgrent" << "fcvt" ; + << "localtime" << "gmtime" << "strtok" << "gethostbyname" << "gethostbyaddr" << "getservbyname" + << "getservbyport" << "crypt" << "ttyname" << "gethostbyname2" + << "getprotobyname" << "getnetbyname" << "getnetbyaddr" << "getrpcbyname" << "getrpcbynumber" << "getrpcent" + << "ctermid" << "readdir" << "getlogin" << "getpwent" << "getpwnam" << "getpwuid" << "getspent" + << "fgetspent" << "getspnam" << "getgrnam" << "getgrgid" << "getnetgrent" << "tempnam" << "fgetpwent" + << "fgetgrent" << "ecvt" << "gcvt" << "getservent" << "gethostent" << "getgrent" << "fcvt" ; } -std::string CheckNonReentrantFunctions::generateErrorMessage(const std::string& function) { +std::string CheckNonReentrantFunctions::generateErrorMessage(const std::string& function) +{ return std::string("Non reentrant function '") + function + "' called. " + - "For threadsafe applications it is recommended to use the reentrant replacement function '" + function + "_r'."; + "For threadsafe applications it is recommended to use the reentrant replacement function '" + function + "_r'."; } void CheckNonReentrantFunctions::nonReentrantFunctions() @@ -83,7 +84,8 @@ void CheckNonReentrantFunctions::nonReentrantFunctions() } //--------------------------------------------------------------------------- -void CheckNonReentrantFunctions::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { +void CheckNonReentrantFunctions::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const +{ CheckNonReentrantFunctions c(0, settings, errorLogger); std::set::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end()); @@ -92,7 +94,8 @@ void CheckNonReentrantFunctions::getErrorMessages(ErrorLogger *errorLogger, cons } } -std::string CheckNonReentrantFunctions::classInfo() const { +std::string CheckNonReentrantFunctions::classInfo() const +{ std::string info = "Warn if any of these non reentrant functions are used:\n"; std::set::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end()); for (; it!=itend; ++it) { diff --git a/lib/checknonreentrantfunctions.h b/lib/checknonreentrantfunctions.h index b9c04a95c..bfb8ffeab 100644 --- a/lib/checknonreentrantfunctions.h +++ b/lib/checknonreentrantfunctions.h @@ -55,7 +55,7 @@ public: private: - static std::string generateErrorMessage(const std::string& function); + static std::string generateErrorMessage(const std::string& function); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const; diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 4608a7b5f..68ca52dfb 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -918,7 +918,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // skip nothrow if (_tokenizer->isCPP() && (Token::simpleMatch(type, "( nothrow )") || - Token::simpleMatch(type, "( std :: nothrow )"))) + Token::simpleMatch(type, "( std :: nothrow )"))) type = type->link()->next(); // is it a user defined type? diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index bd5b17a57..dd5c5aa3c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -845,15 +845,14 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti for (std::list::iterator it = typeList.begin(); it != typeList.end(); ++it) { // finish filling in base class info for (unsigned int i = 0; i < it->derivedFrom.size(); ++i) { - const Type* found = findType(it->derivedFrom[i].nameTok, it->enclosingScope); - if (found && found->findDependency(&(*it))) { - // circular dependency - //_tokenizer->syntaxError(nullptr); - } - else { - it->derivedFrom[i].type = found; - } - } + const Type* found = findType(it->derivedFrom[i].nameTok, it->enclosingScope); + if (found && found->findDependency(&(*it))) { + // circular dependency + //_tokenizer->syntaxError(nullptr); + } else { + it->derivedFrom[i].type = found; + } + } } // fill in friend info @@ -2062,14 +2061,15 @@ bool Type::hasCircularDependencies(std::set* anchestors) const return false; } -bool Type::findDependency(const Type* anchestor) const { - if (this==anchestor) - return true; - for (std::vector::const_iterator parent=derivedFrom.begin(); parent!=derivedFrom.end(); ++parent) { - if (parent->type && parent->type->findDependency(anchestor)) - return true; - } - return false; +bool Type::findDependency(const Type* anchestor) const +{ + if (this==anchestor) + return true; + for (std::vector::const_iterator parent=derivedFrom.begin(); parent!=derivedFrom.end(); ++parent) { + if (parent->type && parent->type->findDependency(anchestor)) + return true; + } + return false; } bool Variable::arrayDimensions(const Library* lib) diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 4152e953d..6b1ab7836 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -124,12 +124,12 @@ public: */ bool hasCircularDependencies(std::set* anchestors = nullptr) const; - /** + /** * Check for dependency * @param anchestor potential anchestor * @return true if there is a dependency */ - bool findDependency(const Type* anchestor) const; + bool findDependency(const Type* anchestor) const; }; /** @brief Information about a member variable. */ diff --git a/lib/token.cpp b/lib/token.cpp index e24dcd6ff..9cbc51f4e 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -824,8 +824,7 @@ const Token * Token::findClosingBracket() const closing = closing->link(); if (!closing) return nullptr; // #6803 - } - else if (Token::Match(closing, "}|]|)|;")) + } else if (Token::Match(closing, "}|]|)|;")) break; else if (closing->str() == "<") ++depth; @@ -1436,8 +1435,8 @@ const Token *Token::getValueTokenDeadPointer() const const Variable * const var = vartok->variable(); if (var->isStatic() || var->isReference()) continue; - if (!var->scope()) - return nullptr; // #6804 + if (!var->scope()) + return nullptr; // #6804 if (var->scope()->type == Scope::eUnion && var->scope()->nestedIn == this->scope()) continue; // variable must be in same function (not in subfunction) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 2fada72b2..8c4b88fb9 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1455,7 +1455,7 @@ private: " ;\n" "}"); #ifdef _MSC_VER - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("", errout.str()); #else TODO_ASSERT_EQUALS("", "[test.cpp:2]: (style) Redundant condition: If init == 9894494448401390090, the comparison init == 9965707617509186058 is always true.\n", errout.str()); #endif diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 86ca43be6..09dda9587 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -134,7 +134,7 @@ private: TEST_CASE(garbageCode92); TEST_CASE(garbageCode93); TEST_CASE(garbageCode94); - TEST_CASE(garbageCode95); + TEST_CASE(garbageCode95); TEST_CASE(garbageValueFlow); TEST_CASE(garbageSymbolDatabase); @@ -142,18 +142,17 @@ private: TEST_CASE(templateSimplifierCrashes); } - std::string checkCode(const char code[], const std::string& filename = "test.cpp") { - // double the tests - run each example as C as well as C++ - const std::string alternatefilename = (filename=="test.c") ? "test.cpp" : "test.c"; - // run alternate check first. It should only ensure stability - try { - checkCodeInternal(code, alternatefilename); - } - catch(InternalError& ) { - } + std::string checkCode(const char code[], const std::string& filename = "test.cpp") { + // double the tests - run each example as C as well as C++ + const std::string alternatefilename = (filename=="test.c") ? "test.cpp" : "test.c"; + // run alternate check first. It should only ensure stability + try { + checkCodeInternal(code, alternatefilename); + } catch (InternalError&) { + } - return checkCodeInternal(code, filename); - } + return checkCodeInternal(code, filename); + } std::string checkCodeInternal(const char code[], const std::string& filename) { errout.str(""); @@ -170,7 +169,7 @@ private: // tokenize.. Tokenizer tokenizer(&settings, this); std::istringstream istr(code); - tokenizer.tokenize(istr, filename.c_str()); + tokenizer.tokenize(istr, filename.c_str()); // call all "runChecks" in all registered Check classes for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { @@ -749,7 +748,7 @@ private: } void garbageCode95() { // #6804 - checkCode("{ } x x ; { } h h [ ] ( ) ( ) { struct x ( x ) ; int __attribute__ ( ) f ( ) { h - > first = & x ; struct x * n = h - > first ; ( ) n > } }"); // do not crash + checkCode("{ } x x ; { } h h [ ] ( ) ( ) { struct x ( x ) ; int __attribute__ ( ) f ( ) { h - > first = & x ; struct x * n = h - > first ; ( ) n > } }"); // do not crash } void garbageValueFlow() { diff --git a/test/testother.cpp b/test/testother.cpp index aae771d56..ad0cd167f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6034,7 +6034,7 @@ private: " if (ull == 0x89504e470d0a1a0a || ull == 0x8a4d4e470d0a1a0a) ;\n" "}\n"); #ifdef _MSC_VER - TODO_ASSERT_EQUALS("", "[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'.\n", errout.str()); + TODO_ASSERT_EQUALS("", "[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'.\n", errout.str()); #else ASSERT_EQUALS("", errout.str()); #endif diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 9662261df..b18120661 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -260,7 +260,7 @@ private: TEST_CASE(functionPrototype); // ticket #5867 TEST_CASE(lambda); // ticket #5867 - TEST_CASE(circularDependencies); // 6298 + TEST_CASE(circularDependencies); // 6298 } void array() const { @@ -2915,21 +2915,21 @@ private: ASSERT_EQUALS(Scope::eLambda, scope->type); } } - // #6298 "stack overflow in Scope::findFunctionInBase (endless recursion)" - void circularDependencies() { - check("template class E,class D> class C : E {\n" - " public:\n" - " int f();\n" - "};\n" - "class E : C {\n" - " public:\n" - " int f() { return C< ::D,int>::f(); }\n" - "};\n" - "int main() {\n" - " E c;\n" - " c.f();\n" - "}"); - } + // #6298 "stack overflow in Scope::findFunctionInBase (endless recursion)" + void circularDependencies() { + check("template class E,class D> class C : E {\n" + " public:\n" + " int f();\n" + "};\n" + "class E : C {\n" + " public:\n" + " int f() { return C< ::D,int>::f(); }\n" + "};\n" + "int main() {\n" + " E c;\n" + " c.f();\n" + "}"); + } }; REGISTER_TEST(TestSymbolDatabase)