From b816968f28d79beb86373457857863ddb3ad48d7 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 2 Jan 2010 03:25:37 +0600 Subject: [PATCH] Fixed #1197 (Segmentation fault when we define function which was already defined by preprocessor) http://sourceforge.net/apps/trac/cppcheck/ticket/1197 --- lib/tokenize.cpp | 23 ++++++++++++----------- test/testsimplifytokens.cpp | 10 ++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5ad5497c7..2ffffeb56 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5147,17 +5147,6 @@ void Tokenizer::simplifyWhile0() if (!Token::simpleMatch(tok, "while ( 0 )")) continue; - // remove "while (0) { .. }" - if (Token::simpleMatch(tok->tokAt(4), "{")) - { - const Token *end = tok->tokAt(4)->link(); - if (!findmatch(tok, end, "continue|break")) - { - Token::eraseTokens(tok, end ? end->next() : 0); - tok->deleteThis(); // delete "while" - } - } - if (Token::simpleMatch(tok->previous(), "}")) { // find "do" @@ -5180,5 +5169,17 @@ void Tokenizer::simplifyWhile0() continue; } } + + // remove "while (0) { .. }" + if (Token::simpleMatch(tok->tokAt(4), "{")) + { + const Token *end = tok->tokAt(4)->link(); + if (!findmatch(tok, end, "continue|break")) + { + Token::eraseTokens(tok, end ? end->next() : 0); + tok->deleteThis(); // delete "while" + } + } + } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 05909251b..679058e32 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -153,6 +153,7 @@ private: // simplify "while (0)" TEST_CASE(while0); + TEST_CASE(while1); } std::string tok(const char code[], bool simplify = true) @@ -2551,6 +2552,15 @@ private: ASSERT_EQUALS("; do { continue ; } while ( false ) ;", tok("; do { continue ; } while (0);")); ASSERT_EQUALS("; do { break ; } while ( false ) ;", tok("; do { break; } while (0);")); } + + void while1() + { + // ticket #1197 + const char code[] = "void do {} while (0) { }"; + const char expected[] = "void { }"; + ASSERT_EQUALS(expected, tok(code)); + } + }; REGISTER_TEST(TestSimplifyTokens)