Fix ticket #85 (False positive (style) Redundant code, begins with string)

This commit is contained in:
Reijo Tomperi 2009-02-08 08:52:03 +00:00
parent 7ee193490f
commit 200a159c67
3 changed files with 51 additions and 0 deletions

View File

@ -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())

View File

@ -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)

View File

@ -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)