Fixed ticket #88 (False positive, (style) Redundant code - begins with numeric constant)

This commit is contained in:
Reijo Tomperi 2009-02-08 09:51:45 +00:00
parent 200a159c67
commit 4305d749ff
3 changed files with 51 additions and 6 deletions

View File

@ -322,12 +322,19 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
if (strchr("#+-*/%&|^?!=<>[](){};:,.~", ch))
{
addtoken(CurrentToken.c_str(), lineno, FileIndex);
CurrentToken.clear();
CurrentToken += ch;
addtoken(CurrentToken.c_str(), lineno, FileIndex);
CurrentToken.clear();
continue;
if (ch == '.' && std::isdigit(CurrentToken[0]))
{
// Don't separate doubles
}
else
{
addtoken(CurrentToken.c_str(), lineno, FileIndex);
CurrentToken.clear();
CurrentToken += ch;
addtoken(CurrentToken.c_str(), lineno, FileIndex);
CurrentToken.clear();
continue;
}
}

View File

@ -62,6 +62,7 @@ private:
TEST_CASE(test2);
TEST_CASE(test3);
TEST_CASE(test4);
TEST_CASE(test_numeric);
}
void test1()
@ -112,6 +113,25 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void test_numeric()
{
check("struct P\n"
"{\n"
"double a;\n"
"double b;\n"
"};\n"
"void f()\n"
"{\n"
"const P values[2] =\n"
"{\n"
"{ 346.1,114.1 }, { 347.1,111.1 }\n"
"};\n"
"}\n"
"};\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
};
REGISTER_TEST(TestIncompleteStatement)

View File

@ -108,6 +108,7 @@ private:
TEST_CASE(sizeof3);
TEST_CASE(sizeof4);
TEST_CASE(simplify_numeric_condition);
TEST_CASE(tokenize_double);
}
@ -989,7 +990,24 @@ private:
ASSERT_EQUALS(std::string(" void f ( ) { int x ; x = 0 ; if ( ! x ) { } }"), ostr.str());
}
void tokenize_double()
{
const char code[] = "void f()\n"
"{\n"
" double a = 4.2;\n"
" float b = 4.2f;\n"
"}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void f ( ) { double a = 4.2 ; float b = 4.2f ; }"), ostr.str());
}
};
REGISTER_TEST(TestTokenizer)