From 40a14736b749a54bbd19e49543c22a283e822f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 23 Jun 2011 17:03:14 +0200 Subject: [PATCH] Added TODO test case for the simplifyKnownVariables to better simplify local variables --- lib/tokenize.cpp | 2 ++ test/testtokenize.cpp | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 05bb01573..7255d79ca 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6873,6 +6873,8 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign // Stop if unknown function call is seen // If the variable is a global or a member variable it might be // changed by the function call + // TODO: don't bail out if the variable is a local variable, + // then it can't be changed by the function call. if (tok3->str() == ")" && tok3->link() && Token::Match(tok3->link()->tokAt(-2), "[;{}] %var% (") && !Token::Match(tok3->link()->previous(), "if|for|while|switch")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 05228aae3..e50553e60 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2272,15 +2272,29 @@ private: void simplifyKnownVariablesClassMember() { // Ticket #2815 - const char code[] = "char *a;\n" - "void f(const char *s) {\n" - " a = NULL;\n" - " x();\n" - " memcpy(a, s, 10);\n" // <- don't simplify "a" here - "}\n"; + { + const char code[] = "char *a;\n" + "void f(const char *s) {\n" + " a = NULL;\n" + " x();\n" + " memcpy(a, s, 10);\n" // <- don't simplify "a" here + "}\n"; - const std::string s(tokenizeAndStringify(code, true)); - ASSERT_EQUALS(true, s.find("memcpy ( a , s , 10 ) ;") != std::string::npos); + const std::string s(tokenizeAndStringify(code, true)); + ASSERT_EQUALS(true, s.find("memcpy ( a , s , 10 ) ;") != std::string::npos); + } + + // If the variable is local then perform simplification.. + { + const char code[] = "void f(const char *s) {\n" + " char *a = NULL;\n" + " x();\n" + " memcpy(a, s, 10);\n" // <- simplify "a" + "}\n"; + + const std::string s(tokenizeAndStringify(code, true)); + TODO_ASSERT_EQUALS(true, false, s.find("memcpy ( 0 , s , 10 ) ;") != std::string::npos); + } }