From bf4e850e119d0da7c121086ea4d46293816e4eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 Dec 2018 15:40:15 +0100 Subject: [PATCH] Fixed #4475 (New check: struct member is assigned a value that is not read) --- lib/astutils.cpp | 12 ++++++++++-- test/testunusedvar.cpp | 14 +++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index d0a7e7b75..ae0802358 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -47,7 +47,7 @@ void visitAstNodes(const Token *ast, std::functionastOperand1()); - if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) + if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) tokens.push(tok->astOperand2()); } } @@ -1260,7 +1260,11 @@ bool FwdAnalysis::isGlobalData(const Token *expr) const globalData = true; return ChildrenToVisit::none; } - if ((tok->previous()->str() != "." && (!tok->variable()->isLocal() && !tok->variable()->isArgument())) || tok->variable()->isExtern()) { + if (tok->variable()->isExtern()) { + globalData = true; + return ChildrenToVisit::none; + } + if (tok->previous()->str() != "." && !tok->variable()->isLocal() && !tok->variable()->isArgument()) { globalData = true; return ChildrenToVisit::none; } @@ -1268,6 +1272,10 @@ bool FwdAnalysis::isGlobalData(const Token *expr) const globalData = true; return ChildrenToVisit::none; } + if (tok->variable()->isPointerArray()) { + globalData = true; + return ChildrenToVisit::none; + } } if (Token::Match(tok, ".|[")) return ChildrenToVisit::op1; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index a1589f55f..07c24acc8 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1971,9 +1971,8 @@ private: void localvar44() { // #4020 - FP functionVariableUsage("void func() {\n" - " int *sp_mem[2] = { 0x00, 0x00 };\n" - " int src = 1, dst = 2;\n" - " sp_mem[(dst + i)][3] = src;\n" + " int *sp_mem[2] = { global1, global2 };\n" + " sp_mem[0][3] = 123;\n" "}"); ASSERT_EQUALS("", errout.str()); } @@ -4420,6 +4419,15 @@ private: "}" ); ASSERT_EQUALS("", errout.str()); + + // Unknown struct type + functionVariableUsage( + "void fun() {" + " struct FOO foo;\n" + " foo.x = 123;\n" + "}" + ); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'foo.x' is assigned a value that is never used.\n", errout.str()); } };