From 2248cdfea0f660eb70e1e18058b43025a2b1ae80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 27 Mar 2014 05:39:48 +0100 Subject: [PATCH] CheckBool::checkAssignBoolToPointer: Refactor to use AST. --- lib/checkbool.cpp | 24 ++++++++++++------------ test/testbool.cpp | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index eccfd014b..740d72143 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -28,6 +28,12 @@ namespace { CheckBool instance; } + +static bool astIsBool(const Token *expr) +{ + return Token::Match(expr, "%comp%|%bool%|%oror%|&&"); +} + //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void CheckBool::checkIncrementBoolean() @@ -332,20 +338,14 @@ void CheckBool::checkAssignBoolToPointer() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "%var% = %bool% ;")) { - // check if there is a deref - // *x.p = true; // <- don't warn - // x.p = true; // <- warn - const Token *prev = tok; - while (Token::Match(prev->tokAt(-2), "%var% .")) - prev = prev->tokAt(-2); - if (Token::Match(prev->previous(), "[*.)]")) + if (tok->str() == "=" && astIsBool(tok->astOperand2())) { + const Token *lhs = tok->astOperand1(); + while (lhs && lhs->str() == ".") + lhs = lhs->astOperand2(); + if (!lhs || !lhs->variable() || !lhs->variable()->isPointer()) continue; - // Is variable a pointer? - const Variable *var1(tok->variable()); - if (var1 && var1->isPointer()) - assignBoolToPointerError(tok); + assignBoolToPointerError(tok); } } } diff --git a/test/testbool.cpp b/test/testbool.cpp index b22f08828..970a0a5c6 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -92,6 +92,21 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2]: (error) Boolean value assigned to pointer.\n", errout.str()); + check("void foo(bool *p) {\n" + " p = (x