From bfc95e01c115723dee104343984380a73061d621 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 7 Jan 2011 08:02:47 +0100 Subject: [PATCH] Fixed #2426 (### Internal error in Cppcheck. Please report it.) --- lib/tokenize.cpp | 21 +++++++++++++++++++-- test/testsimplifytokens.cpp | 9 +++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8cca03e7d..6c963dbec 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -665,8 +665,16 @@ void Tokenizer::unsupportedTypedef(const Token *tok) const std::ostringstream str; const Token *tok1 = tok; - while (tok && tok->str() != ";") + int level = 0; + while (tok) { + if (level == 0 && tok->str() == ";") + break; + else if (tok->str() == "{") + level++; + else if (tok->str() == "}") + level--; + if (tok != tok1) str << " "; str << tok->str(); @@ -694,10 +702,19 @@ void Tokenizer::unsupportedTypedef(const Token *tok) const Token * Tokenizer::deleteInvalidTypedef(Token *typeDef) { Token *tok = NULL; + int level = 0; // remove typedef but leave ; - while (typeDef->next() && typeDef->next()->str() != ";") + while (typeDef->next()) + { + if (level == 0 && typeDef->next()->str() == ";") + break; + else if (typeDef->next()->str() == "{") + level++; + else if (typeDef->next()->str() == "}") + level--; typeDef->deleteNext(); + } if (typeDef != _tokens) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0efd8f757..dc19bb1ab 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -232,6 +232,7 @@ private: TEST_CASE(simplifyTypedef72); // ticket #2375 TEST_CASE(simplifyTypedef73); // ticket #2412 TEST_CASE(simplifyTypedef74); // ticket #2414 + TEST_CASE(simplifyTypedef75); // ticket #2426 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -4786,6 +4787,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef75() // ticket #2426 + { + const char code[] = "typedef _Packed struct S { long l; }; \n"; + const std::string expected = ";"; + ASSERT_EQUALS(expected, sizeof_(code)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { {