parent
a22e476162
commit
c0731703af
|
@ -1522,10 +1522,9 @@ void CheckClass::operatorEqToSelf()
|
||||||
if (!hasAssignSelf(&func, rhs, &out_ifStatementScopeStart)) {
|
if (!hasAssignSelf(&func, rhs, &out_ifStatementScopeStart)) {
|
||||||
if (hasAllocation(&func, scope))
|
if (hasAllocation(&func, scope))
|
||||||
operatorEqToSelfError(func.token);
|
operatorEqToSelfError(func.token);
|
||||||
}
|
} else if (out_ifStatementScopeStart != nullptr) {
|
||||||
else if (out_ifStatementScopeStart != nullptr) {
|
if (hasAllocationInIfScope(&func, scope, out_ifStatementScopeStart))
|
||||||
if(hasAllocationInIfScope(&func, scope, out_ifStatementScopeStart))
|
operatorEqToSelfError(func.token);
|
||||||
operatorEqToSelfError(func.token);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1535,23 +1534,23 @@ void CheckClass::operatorEqToSelf()
|
||||||
|
|
||||||
bool CheckClass::hasAllocationInIfScope(const Function *func, const Scope* scope, const Token *ifStatementScopeStart) const
|
bool CheckClass::hasAllocationInIfScope(const Function *func, const Scope* scope, const Token *ifStatementScopeStart) const
|
||||||
{
|
{
|
||||||
const Token *end;
|
const Token *end;
|
||||||
if (ifStatementScopeStart->str() == "{")
|
if (ifStatementScopeStart->str() == "{")
|
||||||
end = ifStatementScopeStart->link();
|
end = ifStatementScopeStart->link();
|
||||||
else
|
else
|
||||||
end = func->functionScope->bodyEnd;
|
end = func->functionScope->bodyEnd;
|
||||||
return hasAllocation(func, scope, ifStatementScopeStart, end);
|
return hasAllocation(func, scope, ifStatementScopeStart, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckClass::hasAllocation(const Function *func, const Scope* scope) const
|
bool CheckClass::hasAllocation(const Function *func, const Scope* scope) const
|
||||||
{
|
{
|
||||||
return hasAllocation(func, scope, func->functionScope->bodyStart, func->functionScope->bodyEnd);
|
return hasAllocation(func, scope, func->functionScope->bodyStart, func->functionScope->bodyEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const Token *start, const Token *end) const
|
bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const Token *start, const Token *end) const
|
||||||
{
|
{
|
||||||
if (!end)
|
if (!end)
|
||||||
end = func->functionScope->bodyEnd;
|
end = func->functionScope->bodyEnd;
|
||||||
for (const Token *tok = start; tok && (tok != end); tok = tok->next()) {
|
for (const Token *tok = start; tok && (tok != end); tok = tok->next()) {
|
||||||
if (Token::Match(tok, "%var% = malloc|realloc|calloc|new") && isMemberVar(scope, tok))
|
if (Token::Match(tok, "%var% = malloc|realloc|calloc|new") && isMemberVar(scope, tok))
|
||||||
return true;
|
return true;
|
||||||
|
@ -1580,12 +1579,12 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
|
||||||
|
|
||||||
static bool isTrueKeyword(const Token* tok)
|
static bool isTrueKeyword(const Token* tok)
|
||||||
{
|
{
|
||||||
return tok->hasKnownIntValue() && tok->getKnownIntValue() == 1;
|
return tok->hasKnownIntValue() && tok->getKnownIntValue() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isFalseKeyword(const Token* tok)
|
static bool isFalseKeyword(const Token* tok)
|
||||||
{
|
{
|
||||||
return tok->hasKnownIntValue() && tok->getKnownIntValue() == 0;
|
return tok->hasKnownIntValue() && tok->getKnownIntValue() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1594,51 +1593,47 @@ static bool isFalseKeyword(const Token* tok)
|
||||||
*/
|
*/
|
||||||
CheckClass::Bool CheckClass::isInverted(const Token *tok, const Token *rhs)
|
CheckClass::Bool CheckClass::isInverted(const Token *tok, const Token *rhs)
|
||||||
{
|
{
|
||||||
bool res = true;
|
bool res = true;
|
||||||
for (const Token *itr = tok; itr && itr->str()!="("; itr=itr->astParent()) {
|
for (const Token *itr = tok; itr && itr->str()!="("; itr=itr->astParent()) {
|
||||||
if (Token::simpleMatch(itr, "!=") && (isTrueKeyword(itr->astOperand1()) || isTrueKeyword(itr->astOperand2()))) {
|
if (Token::simpleMatch(itr, "!=") && (isTrueKeyword(itr->astOperand1()) || isTrueKeyword(itr->astOperand2()))) {
|
||||||
res = !res;
|
res = !res;
|
||||||
}
|
} else if (Token::simpleMatch(itr, "!=") && ((Token::simpleMatch(itr->astOperand1(), "this") && Token::simpleMatch(itr->astOperand2(), "&") && Token::simpleMatch(itr->astOperand2()->next(), rhs->str().c_str(), rhs->str().size()))
|
||||||
else if (Token::simpleMatch(itr, "!=") && ( (Token::simpleMatch(itr->astOperand1(), "this") && Token::simpleMatch(itr->astOperand2(), "&") && Token::simpleMatch(itr->astOperand2()->next(), rhs->str().c_str(), rhs->str().size()))
|
|| (Token::simpleMatch(itr->astOperand2(), "this") && Token::simpleMatch(itr->astOperand1(), "&") && Token::simpleMatch(itr->astOperand1()->next(), rhs->str().c_str(), rhs->str().size())))) {
|
||||||
|| (Token::simpleMatch(itr->astOperand2(), "this") && Token::simpleMatch(itr->astOperand1(), "&") && Token::simpleMatch(itr->astOperand1()->next(), rhs->str().c_str(), rhs->str().size())))) {
|
res = !res;
|
||||||
res = !res;
|
} else if (Token::simpleMatch(itr, "!=") && (isFalseKeyword(itr->astOperand1()) || isFalseKeyword(itr->astOperand2()))) {
|
||||||
}
|
//Do nothing
|
||||||
else if (Token::simpleMatch(itr, "!=") && (isFalseKeyword(itr->astOperand1()) || isFalseKeyword(itr->astOperand2()))) {
|
} else if (Token::simpleMatch(itr, "!")) {
|
||||||
//Do nothing
|
res = !res;
|
||||||
}
|
} else if (Token::simpleMatch(itr, "==") && (isFalseKeyword(itr->astOperand1()) || isFalseKeyword(itr->astOperand2()))) {
|
||||||
else if (Token::simpleMatch(itr, "!")) {
|
res = !res;
|
||||||
res = !res;
|
} else if (Token::simpleMatch(itr, "==") && (isTrueKeyword(itr->astOperand1()) || isTrueKeyword(itr->astOperand2()))) {
|
||||||
}
|
//Do nothing
|
||||||
else if (Token::simpleMatch(itr, "==") && (isFalseKeyword(itr->astOperand1()) || isFalseKeyword(itr->astOperand2()))) {
|
} else if (Token::simpleMatch(itr, "==") && ((Token::simpleMatch(itr->astOperand1(), "this") && Token::simpleMatch(itr->astOperand2(), "&") && Token::simpleMatch(itr->astOperand2()->next(), rhs->str().c_str(), rhs->str().size()))
|
||||||
res = !res;
|
|| (Token::simpleMatch(itr->astOperand2(), "this") && Token::simpleMatch(itr->astOperand1(), "&") && Token::simpleMatch(itr->astOperand1()->next(), rhs->str().c_str(), rhs->str().size())))) {
|
||||||
}
|
//Do nothing
|
||||||
else if (Token::simpleMatch(itr, "==") && (isTrueKeyword(itr->astOperand1()) || isTrueKeyword(itr->astOperand2()))) {
|
} else {
|
||||||
//Do nothing
|
return Bool::BAILOUT;
|
||||||
}
|
}
|
||||||
else if (Token::simpleMatch(itr, "==") && ( (Token::simpleMatch(itr->astOperand1(), "this") && Token::simpleMatch(itr->astOperand2(), "&") && Token::simpleMatch(itr->astOperand2()->next(), rhs->str().c_str(), rhs->str().size()))
|
}
|
||||||
|| (Token::simpleMatch(itr->astOperand2(), "this") && Token::simpleMatch(itr->astOperand1(), "&") && Token::simpleMatch(itr->astOperand1()->next(), rhs->str().c_str(), rhs->str().size())))) {
|
if (res)
|
||||||
//Do nothing
|
return Bool::TRUE;
|
||||||
}
|
return Bool::FALSE;
|
||||||
else {
|
|
||||||
return Bool::BAILOUT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (res)
|
|
||||||
return Bool::TRUE;
|
|
||||||
return Bool::FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Token * CheckClass::getIfStmtBodyStart(const Token *tok, const Token *rhs)
|
const Token * CheckClass::getIfStmtBodyStart(const Token *tok, const Token *rhs)
|
||||||
{
|
{
|
||||||
const Token *top = tok->astTop();
|
const Token *top = tok->astTop();
|
||||||
if (Token::simpleMatch(top->link(), ") {")) {
|
if (Token::simpleMatch(top->link(), ") {")) {
|
||||||
switch (isInverted(tok->astParent(), rhs)) {
|
switch (isInverted(tok->astParent(), rhs)) {
|
||||||
case Bool::BAILOUT: return nullptr;
|
case Bool::BAILOUT:
|
||||||
case Bool::TRUE: return top->link()->next();
|
return nullptr;
|
||||||
case Bool::FALSE: return top->link()->next()->link();
|
case Bool::TRUE:
|
||||||
}
|
return top->link()->next();
|
||||||
}
|
case Bool::FALSE:
|
||||||
return nullptr;
|
return top->link()->next()->link();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs, const Token **out_ifStatementScopeStart)
|
bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs, const Token **out_ifStatementScopeStart)
|
||||||
|
@ -1664,7 +1659,7 @@ bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs, const Tok
|
||||||
if (tok2 && tok2->isUnaryOp("&") && tok2->astOperand1()->str() == rhs->str())
|
if (tok2 && tok2->isUnaryOp("&") && tok2->astOperand1()->str() == rhs->str())
|
||||||
ret = true;
|
ret = true;
|
||||||
if (ret) {
|
if (ret) {
|
||||||
*out_ifStatementScopeStart = getIfStmtBodyStart(tok2, rhs);
|
*out_ifStatementScopeStart = getIfStmtBodyStart(tok2, rhs);
|
||||||
}
|
}
|
||||||
return ret ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2;
|
return ret ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1655,9 +1655,9 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (&a != this)\n"
|
" if (&a != this)\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
" else\n"
|
" else\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" free(s);\n"
|
" free(s);\n"
|
||||||
" s = strdup(a.s);\n"
|
" s = strdup(a.s);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
|
@ -1676,9 +1676,9 @@ private:
|
||||||
"A & A::operator=(const A &a)\n"
|
"A & A::operator=(const A &a)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (&a != this)\n"
|
" if (&a != this)\n"
|
||||||
" free(s);\n"
|
" free(s);\n"
|
||||||
" else\n"
|
" else\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" free(s);\n"
|
" free(s);\n"
|
||||||
" s = strdup(a.s);\n"
|
" s = strdup(a.s);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
|
|
Loading…
Reference in New Issue