Refactorization: Make use of Token::scope() replacing certain indentation counters

This commit is contained in:
PKEuS 2012-08-12 03:13:07 -07:00
parent 35d94c26d5
commit c8773b891d
5 changed files with 26 additions and 103 deletions

View File

@ -829,19 +829,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
// out of bounds then this flag will be set. // out of bounds then this flag will be set.
bool pointerIsOutOfBounds = false; bool pointerIsOutOfBounds = false;
// Count { and } for tok for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) {
int indentlevel = 0;
for (; tok; tok = tok->next()) {
if (tok->str() == "{") {
++indentlevel;
}
else if (tok->str() == "}") {
--indentlevel;
if (indentlevel < 0)
return;
}
if (varid != 0 && Token::Match(tok, "%varid% = new|malloc|realloc", varid)) { if (varid != 0 && Token::Match(tok, "%varid% = new|malloc|realloc", varid)) {
// Abort // Abort
break; break;
@ -1034,22 +1022,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
const Token *scope_begin = tok->previous(); const Token *scope_begin = tok->previous();
assert(scope_begin != 0); assert(scope_begin != 0);
// Count { and } for tok for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) {
unsigned int indentlevel = 0;
for (; tok; tok = tok->next()) {
if (tok->str() == "{") {
++indentlevel;
}
else if (tok->str() == "}") {
if (indentlevel == 0)
return;
--indentlevel;
}
// Skip array declarations // Skip array declarations
else if (Token::Match(tok, "[;{}] %type% *| %var% [") && if (Token::Match(tok, "[;{}] %type% *| %var% [") && tok->strAt(1) != "return") {
tok->strAt(1) != "return") {
tok = tok->tokAt(3); tok = tok->tokAt(3);
continue; continue;
} }
@ -1737,21 +1712,9 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen()
} else } else
continue; continue;
// count { and } for tok // To avoid false positives and added complexity, we will only look for
int indentlevel = 0; // improper usage of the buffer within the block that it was allocated
for (; tok && tok->next(); tok = tok->next()) { for (const Token* const end = tok->scope()->classEnd; tok && tok->next() && tok != end; tok = tok->next()) {
// To avoid false positives and added complexity, we will only look for
// improper usage of the buffer within the block that it was allocated
if (tok->str() == "{") {
++indentlevel;
}
else if (tok->str() == "}") {
--indentlevel;
if (indentlevel < 0)
return;
}
// If the buffers are modified, we can't be sure of their sizes // If the buffers are modified, we can't be sure of their sizes
if (tok->varId() == srcVarId || tok->varId() == dstVarId) if (tok->varId() == srcVarId || tok->varId() == dstVarId)
break; break;

View File

@ -92,24 +92,14 @@ void CheckExceptionSafety::deallocThrow()
if (!var || !(var->isGlobal() || var->isStatic())) if (!var || !(var->isGlobal() || var->isStatic()))
continue; continue;
// indentlevel..
unsigned int indentlevel = 0;
// Token where throw occurs // Token where throw occurs
const Token *ThrowToken = 0; const Token *ThrowToken = 0;
// is there a throw after the deallocation? // is there a throw after the deallocation?
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { const Token* const end2 = tok->scope()->classEnd;
if (tok2->str() == "{") for (const Token *tok2 = tok; tok2 != end2; tok2 = tok2->next()) {
++indentlevel;
else if (tok2->str() == "}") {
if (indentlevel == 0)
break;
--indentlevel;
}
// Throw after delete -> Dead pointer // Throw after delete -> Dead pointer
else if (tok2->str() == "throw") { if (tok2->str() == "throw") {
if (_settings->inconclusive) { // For inconclusive checking, throw directly. if (_settings->inconclusive) { // For inconclusive checking, throw directly.
deallocThrowError(tok2, tok->str()); deallocThrowError(tok2, tok->str());
break; break;

View File

@ -455,17 +455,10 @@ void CheckNullPointer::nullPointerLinkedList()
// Make sure there is a "break" or "return" inside the loop. // Make sure there is a "break" or "return" inside the loop.
// Without the "break" a null pointer could be dereferenced in the // Without the "break" a null pointer could be dereferenced in the
// for statement. // for statement.
// indentlevel4 is a counter for { and }. When scanning the code with tok4
unsigned int indentlevel4 = 1;
for (const Token *tok4 = scope->classStart; tok4; tok4 = tok4->next()) { for (const Token *tok4 = scope->classStart; tok4; tok4 = tok4->next()) {
if (tok4->str() == "{") if (tok4 == i->classEnd) {
++indentlevel4; nullPointerError(tok1, var->name(), scope->classDef);
else if (tok4->str() == "}") { break;
if (indentlevel4 <= 1) {
nullPointerError(tok1, var->name(), scope->classDef);
break;
}
--indentlevel4;
} }
// There is a "break" or "return" inside the loop. // There is a "break" or "return" inside the loop.
@ -609,19 +602,10 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
} }
// count { and } using tok2 // count { and } using tok2
unsigned int indentlevel2 = 0; const Token* const end2 = tok1->scope()->classEnd;
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok1->tokAt(3); tok2 != end2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel2;
else if (tok2->str() == "}") {
if (indentlevel2 == 0)
break;
--indentlevel2;
}
// label / ?: // label / ?:
else if (tok2->str() == ":") if (tok2->str() == ":")
break; break;
// function call.. // function call..
@ -650,7 +634,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
break; break;
// return/break at base level => stop checking // return/break at base level => stop checking
else if (indentlevel2 == 0 && (tok2->str() == "return" || tok2->str() == "break")) else if (tok2->scope()->classEnd == end2 && (tok2->str() == "return" || tok2->str() == "break"))
break; break;
// Function call: If the pointer is not a local variable it // Function call: If the pointer is not a local variable it

View File

@ -1435,19 +1435,8 @@ void CheckOther::checkUnreachableCode()
if (tok->str() == "break") // If the previous was a break, too: Issue warning if (tok->str() == "break") // If the previous was a break, too: Issue warning
duplicateBreakError(secondBreak, inconclusive); duplicateBreakError(secondBreak, inconclusive);
else { else {
unsigned int indent = 0; if (tok->scope()->type != Scope::eSwitch) // Check, if the enclosing scope is a switch
for (const Token* tok2 = tok; tok2; tok2 = tok2->previous()) { // Check, if the enclosing scope is a switch (TODO: Can we use SymbolDatabase here?) duplicateBreakError(secondBreak, inconclusive);
if (tok2->str() == "}")
indent++;
else if (indent == 0 && tok2->str() == "{" && tok2->strAt(-1) == ")") {
if (tok2->previous()->link()->strAt(-1) != "switch") {
duplicateBreakError(secondBreak, inconclusive);
break;
} else
break;
} else if (tok2->str() == "{")
indent--;
}
} }
tok = Token::findmatch(secondBreak, "[}:]"); tok = Token::findmatch(secondBreak, "[}:]");
} else if (!Token::Match(secondBreak, "return|}|case|default") && secondBreak->strAt(1) != ":") { // TODO: No bailout for unconditional scopes } else if (!Token::Match(secondBreak, "return|}|case|default") && secondBreak->strAt(1) != ":") { // TODO: No bailout for unconditional scopes

View File

@ -680,8 +680,11 @@ void CheckStl::stlBoundries()
// Declaring iterator.. // Declaring iterator..
if (tok->str() == "<" && Token::Match(tok->previous(), STL_CONTAINER_LIST)) { if (tok->str() == "<" && Token::Match(tok->previous(), STL_CONTAINER_LIST)) {
const std::string& container_name(tok->strAt(-1)); const std::string& container_name(tok->strAt(-1));
while (tok && tok->str() != ">") if (tok->link())
tok = tok->next(); tok = tok->link();
else
while (tok && tok->str() != ">")
tok = tok->next();
if (!tok) if (!tok)
break; break;
@ -691,15 +694,9 @@ void CheckStl::stlBoundries()
continue; continue;
// Using "iterator < ..." is not allowed // Using "iterator < ..." is not allowed
unsigned int indentlevel = 0; const Token* const end = tok->scope()->classEnd;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok; tok2 != end; tok2 = tok2->next()) {
if (tok2->str() == "{") if (Token::Match(tok2, "!!* %varid% <", iteratorid)) {
++indentlevel;
else if (tok2->str() == "}") {
if (indentlevel == 0)
break;
--indentlevel;
} else if (Token::Match(tok2, "!!* %varid% <", iteratorid)) {
stlBoundriesError(tok2, container_name); stlBoundriesError(tok2, container_name);
} else if (Token::Match(tok2, "> %varid% !!.", iteratorid)) { } else if (Token::Match(tok2, "> %varid% !!.", iteratorid)) {
stlBoundriesError(tok2, container_name); stlBoundriesError(tok2, container_name);