From 200a159c6711c09980299181fcb95082c1183c22 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 8 Feb 2009 08:52:03 +0000 Subject: [PATCH] Fix ticket #85 (False positive (style) Redundant code, begins with string) --- src/tokenize.cpp | 14 ++++++++++++++ test/testincompletestatement.cpp | 15 +++++++++++++++ test/testsimplifytokens.cpp | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index ed9049a82..09b2880af 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -546,6 +546,20 @@ void Tokenizer::setVarId() void Tokenizer::simplifyTokenList() { + // Combine strings + for (Token *tok = _tokens; tok; tok = tok->next()) + { + if (tok->str()[0] == '"' && tok->next() && tok->next()->str()[0] == '"') + { + // Two strings after each other, combine them + std::string temp = tok->str(); + temp.erase(temp.length() - 1); + temp.append(tok->next()->str().substr(1)); + tok->str(temp.c_str()); + tok->deleteNext(); + } + } + // Remove unwanted keywords static const char* unwantedWords[] = { "unsigned", "unlikely" }; for (Token *tok = _tokens; tok; tok = tok->next()) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 337aca015..4acf605d8 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -61,6 +61,7 @@ private: TEST_CASE(test1); TEST_CASE(test2); TEST_CASE(test3); + TEST_CASE(test4); } void test1() @@ -97,6 +98,20 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + + void test4() + { + check("void foo()\n" + "{\n" + "const char *a =\n" + "{\n" + "\"hello \"\n" + "\"world\"\n" + "};\n" + "}\n"); + + ASSERT_EQUALS(std::string(""), errout.str()); + } }; REGISTER_TEST(TestIncompleteStatement) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 45c87450e..ed4a4dc53 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -39,6 +39,7 @@ private: TEST_CASE(cast0); TEST_CASE(sizeof1); TEST_CASE(iftruefalse); + TEST_CASE(combine_strings); } std::string tok(const char code[]) @@ -141,6 +142,27 @@ private: ASSERT_EQUALS(tok(code2), tok(code1)); } } + + void combine_strings() + { + const char code1[] = "void foo()\n" + "{\n" + "const char *a =\n" + "{\n" + "\"hello \"\n" + "\"world\"\n" + "};\n" + "}\n"; + + const char code2[] = "void foo()\n" + "{\n" + "const char *a =\n" + "{\n" + "\"hello world\"\n" + "};\n" + "}\n"; + ASSERT_EQUALS(tok(code2), tok(code1)); + } }; REGISTER_TEST(TestSimplifyTokens)