Merge branch 'master' of github.com:danmar/cppcheck

This commit is contained in:
Kimmo Varis 2009-08-01 18:28:45 +03:00
commit 365432e6ef
7 changed files with 56 additions and 5 deletions

View File

@ -1042,6 +1042,9 @@ void CheckOther::nullPointer()
if (varid == 0)
continue;
if (Token::Match(tok2->tokAt(-2), "%varid% ?", varid))
continue;
// Check usage of dereferenced variable in the loop..
unsigned int indentlevel3 = 0;
for (const Token *tok3 = tok1->next()->link(); tok3; tok3 = tok3->next())

View File

@ -64,7 +64,7 @@ void CheckStl::iterators()
if (Token::Match(tok2, "%varid% != %var% . end ( )", iteratorId) && tok2->tokAt(2)->varId() != containerId)
{
iteratorsError(tok2, tok->strAt(2), tok2->strAt(2));
tok2 = tok2->tokAt(7);
tok2 = tok2->tokAt(6);
}
else if (Token::Match(tok2, "%var% . insert|erase ( %varid%", iteratorId))
{
@ -73,17 +73,17 @@ void CheckStl::iterators()
else if (tok2->strAt(2) == std::string("erase"))
validIterator = false;
tok2 = tok2->tokAt(5);
tok2 = tok2->tokAt(4);
}
else if (!validIterator && Token::Match(tok2, "* %varid%", iteratorId))
{
dereferenceErasedError(tok2, tok2->strAt(1));
tok2 = tok2->tokAt(2);
tok2 = tok2->next();
}
else if (!validIterator && Token::Match(tok2, "%varid% . %var%", iteratorId))
{
dereferenceErasedError(tok2, tok2->strAt(0));
tok2 = tok2->tokAt(3);
tok2 = tok2->tokAt(2);
}
}
}

View File

@ -1516,7 +1516,11 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
}
else if (code[pos2] == ' ')
{
// Add space only if it is needed
if (par.size() && std::isalnum(par[par.length()-1]))
{
par += ' ';
}
}
else if (parlevel >= 1)
{

View File

@ -1400,6 +1400,12 @@ void Tokenizer::simplifyTokenList()
if (Token::Match(tok, "const %type% %var% = %num% ;"))
{
unsigned int varId = tok->tokAt(2)->varId();
if (varId == 0)
{
tok = tok->tokAt(5);
continue;
}
const char *num = tok->strAt(4);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())

View File

@ -446,6 +446,16 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
checkNullPointer("void foo()\n"
"{\n"
" for (const Token *tok = tokens; tok; tok = tok ? tok->next() : NULL)\n"
" {\n"
" while (tok && tok->str() != \";\")\n"
" tok = tok->next();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void nullpointer2()

View File

@ -112,6 +112,7 @@ private:
TEST_CASE(macro_simple7);
TEST_CASE(macro_simple8);
TEST_CASE(macro_simple9);
TEST_CASE(macro_simple10);
TEST_CASE(macro_mismatch);
TEST_CASE(macro_linenumbers);
TEST_CASE(macro_nopar);
@ -782,6 +783,13 @@ private:
ASSERT_EQUALS("\nf(\"\\\"\");\nf(\"g\");", OurPreprocessor::expandMacros(filedata));
}
void macro_simple10()
{
const char filedata[] = "#define ABC(t) t x\n"
"ABC(unsigned long);";
ASSERT_EQUALS("\nunsigned long x;", OurPreprocessor::expandMacros(filedata));
}
void macro_mismatch()
{
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"

View File

@ -98,6 +98,7 @@ private:
TEST_CASE(simplifyKnownVariables9);
TEST_CASE(simplifyKnownVariables10);
TEST_CASE(simplifyKnownVariables11);
TEST_CASE(simplifyKnownVariables12);
TEST_CASE(match1);
@ -864,6 +865,25 @@ private:
ASSERT_EQUALS(" const int foo = 0 ; int main ( ) { int foo ; foo = 0 ; }", ostr.str());
}
void simplifyKnownVariables12()
{
const char code[] = "ENTER_NAMESPACE(project_namespace)\n"
"const double pi = 3.14;\n"
"int main(){}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { }", ostr.str());
}
void match1()
{
// Match "%var% | %var%"