diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 16dfc2725..7e5f9f667 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7924,6 +7924,14 @@ static Token* findStartToken(const Variable* var, Token* start, const Library* l Token* first = uses.front(); if (Token::findmatch(start, "goto|asm|setjmp|longjmp", first)) return start; + if (first != var->nameToken()) { + // if this is lhs in assignment then set first to the first token in LHS expression + Token* temp = first; + while (Token::Match(temp->astParent(), "[&*(]") && precedes(temp->astParent(), temp)) + temp = temp->astParent(); + if (Token::simpleMatch(temp->astParent(), "=") && precedes(temp, temp->astParent())) + first = temp; + } // If there is only one usage if (uses.size() == 1) return first->previous(); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 17e4278d9..1f5cb95a8 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -5292,6 +5292,18 @@ private: "}"; ASSERT_EQUALS(0U, tokenValues(code, "x )").size()); + // initialization + code = "int foo() {\n" + " int x;\n" + " *((int *)(&x)) = 12;" + " a = x + 1;\n" + "}"; + values = tokenValues(code, "x +"); + ASSERT_EQUALS(true, values.empty()); + // ASSERT_EQUALS(1U, values.size()); + // ASSERT(values.front().isIntValue()); + // ASSERT_EQUALS(12, values.front().intvalue); + // #8036 code = "void foo() {\n" " int x;\n"