AST: Refactored CheckOther::clarifyCalculation
This commit is contained in:
parent
753559fff3
commit
4c44e62159
|
@ -179,50 +179,21 @@ void CheckOther::clarifyCalculation()
|
|||
for (std::size_t i = 0; i < functions; ++i) {
|
||||
const Scope * scope = symbolDatabase->functionScopes[i];
|
||||
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
||||
if (tok->str() == "?") {
|
||||
// condition
|
||||
const Token *cond = tok->previous();
|
||||
if (cond->isName() || cond->isNumber())
|
||||
cond = cond->previous();
|
||||
else if (cond->str() == ")")
|
||||
cond = cond->link()->previous();
|
||||
else
|
||||
continue;
|
||||
// ? operator where lhs is arithmetical expression
|
||||
if (tok->str() != "?" || !tok->astOperand1() || !tok->astOperand1()->isArithmeticalOp())
|
||||
continue;
|
||||
|
||||
if (cond && cond->str() == "!")
|
||||
cond = cond->previous();
|
||||
|
||||
if (!cond)
|
||||
continue;
|
||||
|
||||
// calculation
|
||||
if (!cond->isArithmeticalOp())
|
||||
continue;
|
||||
|
||||
const std::string &op = cond->str();
|
||||
cond = cond->previous();
|
||||
|
||||
// skip previous multiplications..
|
||||
while (cond && cond->previous()) {
|
||||
if ((cond->isName() || cond->isNumber()) && cond->previous()->str() == "*")
|
||||
cond = cond->tokAt(-2);
|
||||
else if (cond->str() == ")")
|
||||
cond = cond->link()->previous();
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if (!cond)
|
||||
continue;
|
||||
|
||||
// first multiplication operand
|
||||
if (cond->str() == ")") {
|
||||
clarifyCalculationError(cond, op);
|
||||
} else if (cond->isName() || cond->isNumber()) {
|
||||
if (Token::Match(cond->previous(),"return|=|+|-|,|(") || cond->strAt(-1) == op)
|
||||
clarifyCalculationError(cond, op);
|
||||
}
|
||||
// Is code clarified by parentheses already?
|
||||
const Token *tok2 = tok->astOperand1();
|
||||
for (; tok2; tok2 = tok2->next()) {
|
||||
if (tok2->str() == "(")
|
||||
tok2 = tok2->link();
|
||||
else if (tok2->str() == ")" || tok2->str() == "?")
|
||||
break;
|
||||
}
|
||||
|
||||
if (tok2 && tok2->str() == "?")
|
||||
clarifyCalculationError(tok, tok->astOperand1()->str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -667,7 +667,7 @@ static void compileExpression(Token *&tok, std::stack<Token*> &op)
|
|||
void TokenList::createAst()
|
||||
{
|
||||
for (Token *tok = _front; tok; tok = tok ? tok->next() : NULL) {
|
||||
if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% (|[|.|=") || Token::Match(tok->previous(), "[;{}] %cop%")) {
|
||||
if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% (|[|.|=|::") || Token::Match(tok->previous(), "[;{}] %cop%")) {
|
||||
std::stack<Token *> operands;
|
||||
compileExpression(tok, operands);
|
||||
}
|
||||
|
|
|
@ -4138,6 +4138,11 @@ private:
|
|||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for '*' and '?'.\n", errout.str());
|
||||
|
||||
check("void f() {\n"
|
||||
" return (2*a)?b:c;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// Ticket #2585 - segmentation fault for invalid code
|
||||
check("abcdef?""?<"
|
||||
"123456?""?>"
|
||||
|
|
Loading…
Reference in New Issue