Reverted 4f30468c80. There was false positives such as #1335 so please fix them and then re-commit.

This commit is contained in:
Daniel Marjamäki 2010-02-01 19:46:51 +01:00
parent 52be98492c
commit 1b9afc82a4
2 changed files with 14 additions and 123 deletions

View File

@ -811,70 +811,24 @@ void CheckOther::checkIncompleteStatement()
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// str plus char // str plus char
//
// What is checked:
// string = string + "xxx" + 'x'; OK
// string = const char * + %any%; ERR
// string = 'x' + %any%; ERR
// string = char + %any%; ERR
// const char * = "xxx" + %number%; OK
//
// where:
// string - variable of type string
// const char * - variable of type const char *
// 'x' - a character literal
// "xxx" - a string literal
// %any% - anything (variable or literal)
// %number% - a number (literal or variable)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckOther::strPlusChar() void CheckOther::strPlusChar()
{ {
char strVars[10000] = {0}; // 1 - string, 2 - const char *, 3 - number (char,int,short), 0 - other bool charVars[10000] = {0};
char assignToConstChar = 0; // 1 - assigning to const char *, 2 - 'const char*'-part assigned
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
if (assignToConstChar) // Declaring char variable..
{ if (Token::Match(tok, "char %var% [;=]"))
if (Token::Match(tok, "%var%"))
{
unsigned int varid = tok->varId();
if (strVars[varid] == 2)
{
assignToConstChar = 2;
}
else if (!(tok->isNumber() || (strVars[varid] == 3)))
{
strPlusChar(tok);
}
}
else if (Token::Match(tok, "%str%"))
{
assignToConstChar = 2;
}
else if (Token::Match(tok, ";"))
{
// 'const char *'-part was not found
if (assignToConstChar != 2)
{
strPlusChar(tok);
}
assignToConstChar = 0;
}
else if (!(Token::Match(tok, "[+=]") || tok->isNumber()))
{
strPlusChar(tok);
}
}
else if (Token::Match(tok, "string %var% [;=]"))
{ {
unsigned int varid = tok->next()->varId(); unsigned int varid = tok->next()->varId();
if (varid > 0 && varid < 10000) if (varid > 0 && varid < 10000)
strVars[varid] = 1; charVars[varid] = true;
} }
else if (Token::Match(tok, "const char * %var% [;=]"))
//
else if (Token::Match(tok, "[=(] %str% + %any%"))
{ {
// char constant.. // char constant..
const char *s = tok->strAt(3); const char *s = tok->strAt(3);
@ -883,29 +837,8 @@ void CheckOther::strPlusChar()
// char variable.. // char variable..
unsigned int varid = tok->tokAt(3)->varId(); unsigned int varid = tok->tokAt(3)->varId();
if (varid > 0 && varid < 10000) if (varid > 0 && varid < 10000 && charVars[varid])
strVars[varid] = 2; strPlusChar(tok->next());
}
else if (Token::Match(tok, "char|int|short %var% [;=]"))
{
unsigned int varid = tok->next()->varId();
if (varid > 0 && varid < 10000)
strVars[varid] = 3;
}
else if (Token::Match(tok, "%var% = %any% + %any%") &&
(strVars[tok->varId()] == 1))
{
// string =
unsigned int varidAny = tok->tokAt(2)->varId();
// first %any% has to be a variable of type string
if (!strVars[varidAny])
strPlusChar(tok->tokAt(3));
}
else if (Token::Match(tok, "%var% = %any%") &&
(strVars[tok->varId()] == 2))
{
// const char * =
assignToConstChar = 1;
} }
} }
} }

View File

@ -51,8 +51,7 @@ private:
TEST_CASE(strPlusChar1); // "/usr" + '/' TEST_CASE(strPlusChar1); // "/usr" + '/'
TEST_CASE(strPlusChar2); // "/usr" + ch TEST_CASE(strPlusChar2); // "/usr" + ch
TEST_CASE(strPlusChar3); // ok : path + "/sub" + '/'; err: '/' + path + "/sub" TEST_CASE(strPlusChar3); // ok: path + "/sub" + '/'
TEST_CASE(strPlusChar4); // ok : <const char *> + <number>
TEST_CASE(varScope1); TEST_CASE(varScope1);
TEST_CASE(varScope2); TEST_CASE(varScope2);
@ -424,36 +423,17 @@ private:
" const char *p = \"/usr\" + '/';\n" " const char *p = \"/usr\" + '/';\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Unusual pointer arithmetic\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Unusual pointer arithmetic\n", errout.str());
strPlusChar("void foo()\n"
"{\n"
" const char *p = '/' + \"/usr\";\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Unusual pointer arithmetic\n", errout.str());
} }
void strPlusChar2() void strPlusChar2()
{ {
// Strange looking pointer arithmetic.. // Strange looking pointer arithmetic..
strPlusChar("void foo()\n"
"{\n"
" int x = 1;\n"
" const char *p = \"/usr\" + x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
strPlusChar("void foo()\n"
"{\n"
" const char *p = \"/usr\" + 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
strPlusChar("void foo()\n" strPlusChar("void foo()\n"
"{\n" "{\n"
" char ch = '/';\n" " char ch = '/';\n"
" const char *p = ch + \"/usr\";\n" " const char *p = \"/usr\" + ch;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Unusual pointer arithmetic\n", errout.str());
} }
void strPlusChar3() void strPlusChar3()
@ -465,31 +445,9 @@ private:
" std::string path = temp + '/' + \"sub\" + '/';\n" " std::string path = temp + '/' + \"sub\" + '/';\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
strPlusChar("void foo()\n"
"{\n"
" std::string path = '/' + \"sub\" + '/';\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Unusual pointer arithmetic\n", errout.str());
} }
void strPlusChar4()
{
// Strange looking pointer arithmetic..
strPlusChar("void foo()\n"
"{\n"
" const char *p = \"abcd\" + 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
strPlusChar("void foo()\n"
"{\n"
" char ch = 1;\n"
" const char *p = \"abcd\";\n"
" const char *s = ch + p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void varScope(const char code[]) void varScope(const char code[])
{ {