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

This commit is contained in:
Kimmo Varis 2010-09-06 22:47:26 +03:00
commit ce45053131
5 changed files with 30 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Cppcheck - A tool for static C/C++ code analysis * Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* Cppcheck - A tool for static C/C++ code analysis * Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* Cppcheck - A tool for static C/C++ code analysis * Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View File

@ -3541,8 +3541,13 @@ void Tokenizer::simplifySizeof()
else if (tok->next()->str() != "(") else if (tok->next()->str() != "(")
{ {
// Add parenthesis around the sizeof // Add parenthesis around the sizeof
int parlevel = 0;
for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next()) for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next())
{ {
if (tempToken->str() == "(")
++parlevel;
else if (tempToken->str() == ")")
--parlevel;
if (Token::Match(tempToken, "%var%")) if (Token::Match(tempToken, "%var%"))
{ {
while (tempToken->next()->str() == "[") while (tempToken->next()->str() == "[")
@ -3568,8 +3573,9 @@ void Tokenizer::simplifySizeof()
// nothing after this // nothing after this
tempToken = tempToken->tokAt(2); tempToken = tempToken->tokAt(2);
} }
else if (Token::simpleMatch(tempToken->next(), ") .")) else if (parlevel > 0 && Token::simpleMatch(tempToken->next(), ") ."))
{ {
--parlevel;
tempToken = tempToken->tokAt(2); tempToken = tempToken->tokAt(2);
continue; continue;
} }

View File

@ -82,6 +82,7 @@ private:
TEST_CASE(sizeof17); TEST_CASE(sizeof17);
TEST_CASE(sizeof18); TEST_CASE(sizeof18);
TEST_CASE(sizeof19); // #1891 - sizeof 'x' TEST_CASE(sizeof19); // #1891 - sizeof 'x'
TEST_CASE(sizeof20); // #2024 - sizeof a)
TEST_CASE(sizeofsizeof); TEST_CASE(sizeofsizeof);
TEST_CASE(casting); TEST_CASE(casting);
@ -1322,6 +1323,25 @@ private:
} }
} }
void sizeof20()
{
// ticket #2024 - sizeof a)
const char code[] = "struct struct_a {\n"
" char a[20];\n"
"};\n"
"\n"
"void foo() {\n"
" struct_a a;\n"
" append(sizeof a).append();\n"
"}\n";
ASSERT_EQUALS("struct struct_a { char a [ 20 ] ; } ; "
"void foo ( ) {"
" struct_a a ;"
" append ( 100 ) . append ( ) ; "
"}", tok(code));
}
void sizeofsizeof() void sizeofsizeof()
{ {
// ticket #1682 // ticket #1682