Auto variables: Minor cleanup

This commit is contained in:
Daniel Marjamäki 2019-03-14 06:41:11 +01:00
parent e9f25bed3f
commit 6eeee743d2
2 changed files with 12 additions and 4 deletions

View File

@ -173,13 +173,15 @@ static bool isAddressOfLocalVariableRecursive(const Token *expr)
return false;
if (Token::Match(expr, "+|-"))
return isAddressOfLocalVariableRecursive(expr->astOperand1()) || isAddressOfLocalVariableRecursive(expr->astOperand2());
if (expr->str() == "(" && !expr->astOperand2())
return isAddressOfLocalVariableRecursive(expr->astOperand1());
if (expr->str() == "&" && !expr->astOperand2()) {
if (expr->isCast())
return isAddressOfLocalVariableRecursive(expr->astOperand2() ? expr->astOperand2() : expr->astOperand1());
if (expr->isUnaryOp("&")) {
const Token *op = expr->astOperand1();
bool deref = false;
while (Token::Match(op, ".|[")) {
if (op->str() == "[" || op->originalName() == "->")
if (op->originalName() == "->")
return false;
if (op->str() == "[")
deref = true;
op = op->astOperand1();
}

View File

@ -388,6 +388,12 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
check("void f(char **out) {\n"
" struct S *p = glob;\n"
" *out = &p->data;\n"
"}");
ASSERT_EQUALS("", errout.str());
// #4998
check("void f(s8**out) {\n"
" s8 *p;\n" // <- p is pointer => no error