From a24398324245a0de034832674fafac1b9121aed2 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 20 Aug 2012 08:57:28 -0700 Subject: [PATCH] Refactorized type handling in CheckIO and CheckOther: - Added several types (std::) to isComplexType - Types in namespace std:: are considered to have no side-effects (solved one TODO) - Scope of a pointer can be limited without side effects --- lib/checkio.cpp | 3 ++- lib/checkother.cpp | 12 ++++++------ test/testother.cpp | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 8f1cf536c..e4abea793 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -383,11 +383,12 @@ static bool isComplexType(const Variable* var, const Token* varTypeTok) if (knownTypes.empty()) { knownTypes.insert("struct"); // If a type starts with the struct keyword, its a complex type knownTypes.insert("string"); + knownTypes.insert("wstring"); } if (varTypeTok->str() == "std") varTypeTok = varTypeTok->tokAt(2); - return(knownTypes.find(varTypeTok->str()) != knownTypes.end() && !var->isPointer() && !var->isArray()); + return((knownTypes.find(varTypeTok->str()) != knownTypes.end() || (varTypeTok->strAt(1) == "<" && varTypeTok->linkAt(1) && varTypeTok->linkAt(1)->strAt(1) != "::")) && !var->isPointer() && !var->isArray()); } static bool isKnownType(const Variable* var, const Token* varTypeTok) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bcef68ff9..f414ea713 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -870,9 +870,9 @@ void CheckOther::switchCaseFallThrough(const Token *tok) // // int y = y; // <- redundant initialization to self //--------------------------------------------------------------------------- -static bool isPOD(const Tokenizer *tokenizer, const Variable* var) +static bool isTypeWithoutSideEffects(const Tokenizer *tokenizer, const Variable* var) { - return ((var && (!var->isClass() || var->isPointer())) || !tokenizer->isCPP()); + return ((var && (!var->isClass() || var->isPointer()) || Token::simpleMatch(var->typeStartToken(), "std ::")) || !tokenizer->isCPP()); } void CheckOther::checkSelfAssignment() @@ -887,7 +887,7 @@ void CheckOther::checkSelfAssignment() while (tok) { if (Token::Match(tok->previous(), "[;{}]") && tok->varId() && tok->varId() == tok->tokAt(2)->varId() && - isPOD(_tokenizer, symbolDatabase->getVariableFromVarId(tok->varId()))) { + isTypeWithoutSideEffects(_tokenizer, symbolDatabase->getVariableFromVarId(tok->varId()))) { bool err = true; // no false positive for 'x = x ? x : 1;' @@ -1567,7 +1567,7 @@ void CheckOther::checkVariableScope() for (unsigned int i = 1; i < symbolDatabase->getVariableListSize(); i++) { const Variable* var = symbolDatabase->getVariableFromVarId(i); - if (!var || !var->isLocal() || (!var->typeStartToken()->isStandardType() && !var->typeStartToken()->next()->isStandardType())) + if (!var || !var->isLocal() || (!var->isPointer() && !var->typeStartToken()->isStandardType() && !var->typeStartToken()->next()->isStandardType())) continue; bool forHead = false; // Don't check variables declared in header of a for loop @@ -1758,7 +1758,7 @@ static bool isChar(const Variable* var) static bool isSignedChar(const Variable* var) { - return(isChar(var) && !var->typeEndToken()->isUnsigned() && (!var->typeEndToken()->previous() || !var->typeEndToken()->previous()->isUnsigned())); + return(isChar(var) && !var->typeStartToken()->isUnsigned()); } void CheckOther::checkCharVariable() @@ -2176,7 +2176,7 @@ void CheckOther::checkDuplicateIf() expressionMap.insert(std::make_pair(expression, tok)); // find the next else if (...) statement - const Token *tok1 = tok->next()->link()->next()->link(); + const Token *tok1 = scope->classEnd; // check all the else if (...) statements while (Token::simpleMatch(tok1, "} else if (") && diff --git a/test/testother.cpp b/test/testother.cpp index cb7555ece..62ff12f84 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2306,7 +2306,7 @@ private: "{\n" " std::string var = var = \"test\";\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"var\" to itself\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"var\" to itself\n", errout.str()); check("void foo()\n" "{\n"