From c1c9b96bb68ddb49678d6f05d23077397c944b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 Feb 2011 21:11:56 +0100 Subject: [PATCH] Fixed #2031 (Tokenizer: simplify known value (string has known value after strcpy)) --- lib/tokenize.cpp | 16 ++++++++++++++++ test/testtokenize.cpp | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0a2900372..7460faf3b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6356,6 +6356,22 @@ bool Tokenizer::simplifyKnownVariables() ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel); } + + else if (Token::Match(tok2, "strcpy ( %var% , %str% ) ;")) + { + const unsigned int varid(tok2->tokAt(2)->varId()); + if (varid == 0) + continue; + const std::string structname(""); + const Token * const valueToken = tok2->tokAt(4); + std::string value(valueToken->str()); + const unsigned int valueVarId(0); + const bool valueIsPointer(false); + Token *tok3 = tok2; + for (int i = 0; i < 6; ++i) + tok3 = tok3->next(); + ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel); + } } if (tok2) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 02cf0cc71..c25552845 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -129,6 +129,7 @@ private: TEST_CASE(simplifyKnownVariables39); TEST_CASE(simplifyKnownVariables40); TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) .. + TEST_CASE(simplifyKnownVariables42); // ticket #2031 - known string value after strcpy TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor2); @@ -2040,6 +2041,21 @@ private: ASSERT_EQUALS("void f ( ) {\nint x ; x = 0 ;\nconst int * p ; p = & x ;\nif ( & x ) { return 0 ; }\n}", tokenizeAndStringify(code, true)); } + void simplifyKnownVariables42() + { + const char code[] = "void f() {\n" + " char str1[10], str2[10];\n" + " strcpy(str1, \"abc\");\n" + " strcpy(str2, str1);\n" + "}"; + const char expected[] = "void f ( ) {\n" + "char str1 [ 10 ] ; char str2 [ 10 ] ;\n" + "strcpy ( str1 , \"abc\" ) ;\n" + "strcpy ( str2 , \"abc\" ) ;\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutAssign() { const char code[] = "int foo() {\n"