str plus char: added simple variable handling

This commit is contained in:
Daniel Marjamäki 2009-01-15 17:12:33 +00:00
parent ad9aba6e1b
commit 66e93b24e2
2 changed files with 29 additions and 2 deletions

View File

@ -971,15 +971,30 @@ void CheckOther::functionVariableUsage()
void CheckOther::strPlusChar() void CheckOther::strPlusChar()
{ {
bool charVars[10000] = {0};
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
if (Token::Match(tok, "%str% + %any%")) // Declaring char variable..
if (Token::Match(tok, "char %var% [;=]"))
{ {
const char *s = tok->strAt(2); unsigned int varid = tok->next()->varId();
if (varid>0 && varid<10000)
charVars[varid] = true;
}
//
else if (Token::Match(tok, "%str% + %any%"))
{
// char constant.. // char constant..
const char *s = tok->strAt(2);
if (*s == '\'') if (*s == '\'')
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok)); _errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
// char variable..
unsigned int varid = tok->tokAt(2)->varId();
if (varid>0 && varid<10000 && charVars[varid])
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
} }
} }
} }

View File

@ -43,6 +43,7 @@ private:
TEST_CASE(sprintf4); // struct member TEST_CASE(sprintf4); // struct member
TEST_CASE(strPlusChar1); // "/usr" + '/' TEST_CASE(strPlusChar1); // "/usr" + '/'
TEST_CASE(strPlusChar2); // "/usr" + ch
} }
void check(const char code[]) void check(const char code[])
@ -189,6 +190,17 @@ private:
ASSERT_EQUALS(std::string("[test.cpp:3]: Unusual pointer arithmetic\n"), errout.str()); ASSERT_EQUALS(std::string("[test.cpp:3]: Unusual pointer arithmetic\n"), errout.str());
} }
void strPlusChar2()
{
// Strange looking pointer arithmetic..
strPlusChar("void foo()\n"
"{\n"
" char ch = '/';\n"
" const char *p = \"/usr\" + ch;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: Unusual pointer arithmetic\n"), errout.str());
}
}; };
REGISTER_TEST(TestOther) REGISTER_TEST(TestOther)