From 6fd60eebb0e63aeb937b7947e9cbfe2b41206261 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Mon, 3 Sep 2012 20:23:53 +0200 Subject: [PATCH] Tokenizer: added a workaround for #3690 (Support MSVC's 'for each'). --- lib/tokenize.cpp | 11 ++++++++--- test/testtokenize.cpp | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 83a85c203..1525f2d81 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1593,10 +1593,15 @@ bool Tokenizer::tokenize(std::istream &code, } // if MACRO - for (const Token *tok = list.front(); tok; tok = tok->next()) { + for (Token *tok = list.front(); tok; tok = tok->next()) { if (Token::Match(tok, "if|for|while|BOOST_FOREACH %var% (")) { - syntaxError(tok); - return false; + if (Token::simpleMatch(tok, "for each")) + // 'for each ( )' -> 'for ( )' + tok->deleteNext(); + else { + syntaxError(tok); + return false; + } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 90ebf1212..df3085edb 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -66,6 +66,8 @@ private: TEST_CASE(wrong_syntax4); // #3618 TEST_CASE(wrong_syntax_if_macro); // #2518 - if MACRO() + TEST_CASE(foreach); // #3690 + TEST_CASE(minus); TEST_CASE(longtok); @@ -706,6 +708,12 @@ private: ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); } + void foreach() { + // #3690 + const std::string code("void f() { for each ( char c in MyString ) { Console::Write(c); } }"); + ASSERT_EQUALS("void f ( ) { for ( char c in MyString ) { Console :: Write ( c ) ; } }" ,tokenizeAndStringify(code.c_str())); + } + void minus() { ASSERT_EQUALS("i = -12", tokenizeAndStringify("i = -12")); ASSERT_EQUALS("1 - 2", tokenizeAndStringify("1-2"));