From 2a640a59c22db86bae132ae007d052e4b612c849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 16 Nov 2013 17:30:04 +0100 Subject: [PATCH] Tokenizer: Improved handling of pointer aliases. This fixes some false negatives. --- lib/tokenize.cpp | 13 ++++++++++++- test/testtokenize.cpp | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bda1a3df4..6ce67b1ac 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6730,6 +6730,17 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign ret = true; } + // pointer alias used in condition.. + if (Token::Match(valueToken,"& %var% ;") && Token::Match(tok3, ("( * " + structname + " %varid% %cop%").c_str(), varid)) { + tok3->deleteNext(); + if (!structname.empty()) + tok3->deleteNext(2); + tok3 = tok3->next(); + tok3->str(value); + tok3->varId(valueVarId); + ret = true; + } + // Delete pointer alias if (pointeralias && tok3->str() == "delete" && (Token::Match(tok3, "delete %varid% ;", varid) || @@ -6818,7 +6829,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign ret = true; tok3->str(value); tok3->varId(valueVarId); - if (tok3->previous()->str() == "*" && valueIsPointer) { + if (tok3->previous()->str() == "*" && (valueIsPointer || Token::Match(valueToken, "& %var% ;"))) { tok3 = tok3->previous(); tok3->deleteThis(); ret = true; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index dcaf8b6e1..87f84ad2d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -181,6 +181,7 @@ private: TEST_CASE(simplifyKnownVariables52); // #4728 "= x %cop%" TEST_CASE(simplifyKnownVariables53); // references TEST_CASE(simplifyKnownVariables54); // #4913 'x' is not 0 after *--x=0; + TEST_CASE(simplifyKnownVariables55); // pointer alias TEST_CASE(simplifyKnownVariablesIfEq1); // if (a==5) => a is 5 in the block TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; } TEST_CASE(simplifyKnownVariablesIfEq3); // #4708 - if (a==5) { buf[--a] = 0; } @@ -2824,6 +2825,12 @@ private: ASSERT_EQUALS("void f ( int * p ) { * -- p = 0 ; * p = 0 ; }", tokenizeAndStringify("void f(int*p) { *--p=0; *p=0; }", true)); } + void simplifyKnownVariables55() { // pointer alias + ASSERT_EQUALS("void f ( ) { int a ; if ( a > 0 ) { } }", tokenizeAndStringify("void f() { int a; int *p=&a; if (*p>0) {} }", true)); + ASSERT_EQUALS("void f ( ) { int a ; struct AB ab ; ab . a = & a ; if ( a > 0 ) { } }", tokenizeAndStringify("void f() { int a; struct AB ab; ab.a = &a; if (*ab.a>0) {} }", true)); + ASSERT_EQUALS("void f ( ) { int a ; if ( x > a ) { } }", tokenizeAndStringify("void f() { int a; int *p=&a; if (x>*p) {} }", true)); + } + void simplifyKnownVariablesIfEq1() { const char code[] = "void f(int x) {\n" " if (x==5) {\n"