From 324e5e581b5034b6f1bd8a944a71cfa57e926a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 24 Aug 2019 20:15:52 +0200 Subject: [PATCH] Redundant assignments: Fix false positive when reassignment expression contains assembler --- lib/astutils.cpp | 12 ++++++++++++ test/testother.cpp | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index c59d5531f..3541ecd9f 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1331,6 +1331,15 @@ static bool nonLocal(const Variable* var, bool deref) return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isArgument() && var->isPointer()) || var->isStatic() || var->isReference() || var->isExtern(); } +static bool hasGccCompoundStatement(const Token *tok) +{ + if (!tok) + return false; + if (tok->str() == "{" && Token::simpleMatch(tok->previous(), "( {")) + return true; + return hasGccCompoundStatement(tok->astOperand1()) || hasGccCompoundStatement(tok->astOperand2()); +} + static bool hasFunctionCall(const Token *tok) { if (!tok) @@ -1688,6 +1697,9 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const return Result(Result::Type::READ); continue; } + // ({ .. }) + if (hasGccCompoundStatement(parent->astParent()->astOperand2())) + return Result(Result::Type::BAILOUT); const bool reassign = isSameExpression(mCpp, false, expr, parent, mLibrary, false, false, nullptr); if (reassign) return Result(Result::Type::WRITE, parent->astParent()); diff --git a/test/testother.cpp b/test/testother.cpp index d55c99c37..eebe94da5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6172,6 +6172,14 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + // ({ }) + check("void f() {\n" + " int x;\n" + " x = 321;\n" + " x = ({ asm(123); })\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // from #3103 (avoid a false negative) check("int foo(){\n" " int x;\n"