diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 89f1e50bd..090b812fc 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1,6 +1,6 @@ /* * 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 * it under the terms of the GNU General Public License as published by diff --git a/cli/cmdlineparser.h b/cli/cmdlineparser.h index 7a02a1475..fa726be61 100644 --- a/cli/cmdlineparser.h +++ b/cli/cmdlineparser.h @@ -1,6 +1,6 @@ /* * 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 * it under the terms of the GNU General Public License as published by diff --git a/lib/timer.cpp b/lib/timer.cpp index 626fdcb7b..598fa3952 100644 --- a/lib/timer.cpp +++ b/lib/timer.cpp @@ -1,6 +1,6 @@ /* * 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 * it under the terms of the GNU General Public License as published by diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e69ab4f0c..f8d141b6b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3541,8 +3541,13 @@ void Tokenizer::simplifySizeof() else if (tok->next()->str() != "(") { // Add parenthesis around the sizeof + int parlevel = 0; for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next()) { + if (tempToken->str() == "(") + ++parlevel; + else if (tempToken->str() == ")") + --parlevel; if (Token::Match(tempToken, "%var%")) { while (tempToken->next()->str() == "[") @@ -3568,8 +3573,9 @@ void Tokenizer::simplifySizeof() // nothing after this tempToken = tempToken->tokAt(2); } - else if (Token::simpleMatch(tempToken->next(), ") .")) + else if (parlevel > 0 && Token::simpleMatch(tempToken->next(), ") .")) { + --parlevel; tempToken = tempToken->tokAt(2); continue; } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 9a9cc6647..0322c1b39 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -82,6 +82,7 @@ private: TEST_CASE(sizeof17); TEST_CASE(sizeof18); TEST_CASE(sizeof19); // #1891 - sizeof 'x' + TEST_CASE(sizeof20); // #2024 - sizeof a) TEST_CASE(sizeofsizeof); 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() { // ticket #1682