From 5be12a1d27d1bcdd40a7844bce0992f3a7aacaf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 6 Nov 2010 19:42:38 +0100 Subject: [PATCH] Tokenizer: improved simplifyKnownVariables --- lib/tokenize.cpp | 14 ++++++++++++++ test/testtokenize.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 927c22ef5..f62ee2c0b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5973,6 +5973,11 @@ bool Tokenizer::simplifyKnownVariables() // Variable is used somehow in a non-defined pattern => bail out if (tok3->varId() == varid) { + // calling member function.. bail out because this might + // have different effects to the variable. + if (Token::Match(tok3->next(), ". %var% (")) + break; + // This is a really generic bailout so let's try to avoid this. // There might be lots of false negatives. if (_settings && _settings->debugwarnings) @@ -6029,6 +6034,15 @@ bool Tokenizer::simplifyKnownVariables() } } + // array usage + if (Token::Match(tok3, "( %varid% [", varid)) + { + tok3 = tok3->next(); + tok3->str(value); + tok3->varId(valueVarId); + ret = true; + } + // Variable is used in calculation.. if (((tok3->previous()->varId() > 0) && Token::Match(tok3, "& %varid%", varid)) || Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;])]", varid) || diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 540d28be7..6b4dcfa82 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -121,6 +121,8 @@ private: TEST_CASE(simplifyKnownVariables28); TEST_CASE(simplifyKnownVariables29); // ticket #1811 TEST_CASE(simplifyKnownVariables30); + TEST_CASE(simplifyKnownVariables31); + TEST_CASE(simplifyKnownVariablesBailOut1); TEST_CASE(varid1); TEST_CASE(varid2); @@ -1838,6 +1840,34 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void simplifyKnownVariables31() + { + const char code[] = "void foo(const char str[]) {\n" + " const char *p = str;\n" + " if (p[0] == 0) {\n" + " }\n" + "}\n"; + const char expected[] = "void foo ( const char str [ ] ) {\n" + "const char * p ; p = str ;\n" + "if ( str [ 0 ] == 0 ) {\n" + "}\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + + void simplifyKnownVariablesBailOut1() + { + const char code[] = "void foo(obj a) {\n" + " obj b = a;\n" + " b.f();\n" + "}\n"; + const char expected[] = "void foo ( obj a ) {\n" + "obj b ; b = a ;\n" + "b . f ( ) ;\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + std::string tokenizeDebugListing(const std::string &code, bool simplify = false) { Tokenizer tokenizer;