Fixed #5154 (Tokenizer: wrong handling of msvc 'for each')

This commit is contained in:
Daniel Marjamäki 2013-12-17 06:34:27 +01:00
parent 762806499f
commit e2bc99aa24
3 changed files with 14 additions and 5 deletions

View File

@ -1594,10 +1594,11 @@ bool Tokenizer::tokenize(std::istream &code,
// if MACRO // if MACRO
for (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% (")) { if (Token::Match(tok, "if|for|while|BOOST_FOREACH %var% (")) {
if (Token::simpleMatch(tok, "for each")) if (Token::simpleMatch(tok, "for each")) {
// 'for each ( )' -> 'for ( )' // 'for each ( )' -> 'asm ( )'
tok->str("asm");
tok->deleteNext(); tok->deleteNext();
else { } else {
syntaxError(tok); syntaxError(tok);
return false; return false;
} }

View File

@ -988,9 +988,9 @@ private:
} }
void foreach() { void foreach() {
// #3690 // #3690,#5154
const std::string code("void f() { for each ( char c in MyString ) { Console::Write(c); } }"); 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() { void concatenateNegativeNumber() {

View File

@ -3097,6 +3097,14 @@ private:
" std::for_each(ints.begin(), ints.end(), [&x](int i){ x += i; });\n" " 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()); 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<int,int> ints;\n"
" ints[0]= 1;\n"
" for each(std::pair<int,int> i in ints) { x += i.first; }\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void localvarShift1() { void localvarShift1() {