CheckClass: Refactoring. Use Token::variable(). Ticket: #4535

This commit is contained in:
Robert Reif 2013-02-02 16:30:17 +01:00 committed by Daniel Marjamki
parent 9124847211
commit 8e35860fc2
1 changed files with 12 additions and 13 deletions

View File

@ -186,7 +186,7 @@ void CheckClass::copyconstructors()
if (func->type == Function::eConstructor && func->functionScope) { if (func->type == Function::eConstructor && func->functionScope) {
for (const Token* tok = func->functionScope->classStart; tok!=func->functionScope->classEnd; tok=tok->next()) { for (const Token* tok = func->functionScope->classStart; tok!=func->functionScope->classEnd; tok=tok->next()) {
if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) { if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) {
const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId()); const Variable* var = tok->variable();
if (var && var->isPointer() && var->scope() == &*scope) if (var && var->isPointer() && var->scope() == &*scope)
allocatedVars[tok->varId()] = tok; allocatedVars[tok->varId()] = tok;
} }
@ -674,12 +674,12 @@ void CheckClass::initializationListUsage()
if (Token::Match(tok, "try|do {")) if (Token::Match(tok, "try|do {"))
break; break;
if (tok->varId() && Token::Match(tok, "%var% = %any%")) { if (tok->varId() && Token::Match(tok, "%var% = %any%")) {
const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId()); const Variable* var = tok->variable();
if (var && var->scope() == owner && !var->isStatic()) { if (var && var->scope() == owner && !var->isStatic()) {
bool allowed = true; bool allowed = true;
for (const Token* tok2 = tok->tokAt(2); tok2->str() != ";"; tok2 = tok2->next()) { for (const Token* tok2 = tok->tokAt(2); tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId()) { if (tok2->varId()) {
const Variable* var2 = symbolDatabase->getVariableFromVarId(tok2->varId()); const Variable* var2 = tok2->variable();
if (var2 && var2->scope() == owner) { // Is there a dependency between two member variables? if (var2 && var2->scope() == owner) { // Is there a dependency between two member variables?
allowed = false; allowed = false;
break; break;
@ -845,8 +845,7 @@ void CheckClass::noMemset()
else if (Token::Match(arg3, "sizeof ( struct %type% ) )")) else if (Token::Match(arg3, "sizeof ( struct %type% ) )"))
typeTok = arg3->tokAt(3); typeTok = arg3->tokAt(3);
else if (Token::Match(arg1, "&| %var% ,")) { else if (Token::Match(arg1, "&| %var% ,")) {
unsigned int varid = arg1->str() == "&" ? arg1->next()->varId() : arg1->varId(); const Variable *var = arg1->str() == "&" ? arg1->next()->variable() : arg1->variable();
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var && (var->typeStartToken() == var->typeEndToken() || Token::Match(var->typeStartToken(), "%type% :: %type%")) if (var && (var->typeStartToken() == var->typeEndToken() || Token::Match(var->typeStartToken(), "%type% :: %type%"))
&& (arg1->str() == "&" || var->isPointer() || var->isArray())) && (arg1->str() == "&" || var->isPointer() || var->isArray()))
typeTok = var->typeEndToken(); typeTok = var->typeEndToken();
@ -1565,23 +1564,23 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
for (const Token *tok1 = func->functionScope->classStart; tok1 && tok1 != func->functionScope->classEnd; tok1 = tok1->next()) { for (const Token *tok1 = func->functionScope->classStart; tok1 && tok1 != func->functionScope->classEnd; tok1 = tok1->next()) {
if (tok1->isName() && isMemberVar(scope, tok1)) { if (tok1->isName() && isMemberVar(scope, tok1)) {
memberAccessed = true; memberAccessed = true;
const Variable* v = symbolDatabase->getVariableFromVarId(tok1->varId()); const Variable* v = tok1->variable();
if (v && v->isMutable()) if (v && v->isMutable())
continue; continue;
if (tok1->str() == "this" && tok1->previous()->isAssignmentOp()) if (tok1->str() == "this" && tok1->previous()->isAssignmentOp())
return(false); return(false);
unsigned int lastVarId = tok1->varId(); const Token *lastVarTok = tok1;
const Token *end = tok1; const Token *end = tok1;
for (;;) { for (;;) {
if (Token::Match(end->next(), ". %var%")) { if (Token::Match(end->next(), ". %var%")) {
end = end->tokAt(2); end = end->tokAt(2);
if (end->varId()) if (end->varId())
lastVarId = end->varId(); lastVarTok = end;
} else if (end->strAt(1) == "[") { } else if (end->strAt(1) == "[") {
if (end->varId()) { if (end->varId()) {
const Variable *var = symbolDatabase->getVariableFromVarId(end->varId()); const Variable *var = end->variable();
if (var && Token::simpleMatch(var->typeStartToken(), "std :: map")) // operator[] changes a map if (var && Token::simpleMatch(var->typeStartToken(), "std :: map")) // operator[] changes a map
return(false); return(false);
@ -1594,7 +1593,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
} }
if (end->strAt(1) == "(") { if (end->strAt(1) == "(") {
const Variable *var = symbolDatabase->getVariableFromVarId(lastVarId); const Variable *var = lastVarTok->variable();
if (!var) if (!var)
return(false); return(false);
if (Token::simpleMatch(var->typeStartToken(), "std ::") // assume all std::*::size() and std::*::empty() are const if (Token::simpleMatch(var->typeStartToken(), "std ::") // assume all std::*::size() and std::*::empty() are const
@ -1632,7 +1631,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
// streaming: << // streaming: <<
else if (Token::simpleMatch(tok1->previous(), ") <<") && else if (Token::simpleMatch(tok1->previous(), ") <<") &&
isMemberVar(scope, tok1->tokAt(-2))) { isMemberVar(scope, tok1->tokAt(-2))) {
const Variable* var = symbolDatabase->getVariableFromVarId(tok1->tokAt(-2)->varId()); const Variable* var = tok1->tokAt(-2)->variable();
if (!var || !var->isMutable()) if (!var || !var->isMutable())
return(false); return(false);
} }
@ -1651,7 +1650,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
if (tok2->str() == "(") if (tok2->str() == "(")
tok2 = tok2->link(); tok2 = tok2->link();
else if (tok2->isName() && isMemberVar(scope, tok2)) { else if (tok2->isName() && isMemberVar(scope, tok2)) {
const Variable* var = symbolDatabase->getVariableFromVarId(tok2->varId()); const Variable* var = tok2->variable();
if (!var || !var->isMutable()) if (!var || !var->isMutable())
return(false); // TODO: Only bailout if function takes argument as non-const reference return(false); // TODO: Only bailout if function takes argument as non-const reference
} }