From e2bc99aa24d42516ab0517060a023ce06f01b023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Dec 2013 06:34:27 +0100 Subject: [PATCH] Fixed #5154 (Tokenizer: wrong handling of msvc 'for each') --- lib/tokenize.cpp | 7 ++++--- test/testtokenize.cpp | 4 ++-- test/testunusedvar.cpp | 8 ++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0a6600cd8..306e4f6d3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1594,10 +1594,11 @@ bool Tokenizer::tokenize(std::istream &code, // if MACRO for (Token *tok = list.front(); tok; tok = tok->next()) { if (Token::Match(tok, "if|for|while|BOOST_FOREACH %var% (")) { - if (Token::simpleMatch(tok, "for each")) - // 'for each ( )' -> 'for ( )' + if (Token::simpleMatch(tok, "for each")) { + // 'for each ( )' -> 'asm ( )' + tok->str("asm"); tok->deleteNext(); - else { + } else { syntaxError(tok); return false; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index afdd1312e..32d288b1f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -988,9 +988,9 @@ private: } void foreach() { - // #3690 + // #3690,#5154 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())); + ASSERT_EQUALS("void f ( ) { asm ( \"char c in MyString\" ) { Console :: Write ( c ) ; } }" ,tokenizeAndStringify(code.c_str())); } void concatenateNegativeNumber() { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index f867ae640..dba8854a9 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -3097,6 +3097,14 @@ private: " std::for_each(ints.begin(), ints.end(), [&x](int i){ x += i; });\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str()); + + // #5154 - MSVC 'for each' + functionVariableUsage("void f() {\n" + " std::map ints;\n" + " ints[0]= 1;\n" + " for each(std::pair i in ints) { x += i.first; }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void localvarShift1() {