Fixed #4171 (don't choke when parentheses are missing from macros)

This commit is contained in:
Daniel Marjamäki 2012-09-09 09:48:07 +02:00
parent 0c55d5cfa7
commit 107b3b4401
2 changed files with 20 additions and 6 deletions

View File

@ -1598,8 +1598,22 @@ bool Tokenizer::tokenize(std::istream &code,
// 'for each ( )' -> 'for ( )'
tok->deleteNext();
else {
syntaxError(tok);
return false;
// locate the ')' parenthesis (the Token::link is not set yet)
// Then replace 'if MACRO(X)' with 'if (MACRO(X))'
unsigned int parlevel = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")") {
if (parlevel == 1) {
tok->insertToken("(");
tok2->insertToken(")");
}
if (parlevel <= 1)
break;
--parlevel;
}
}
}
}
}

View File

@ -696,10 +696,10 @@ private:
}
void wrong_syntax_if_macro() {
// #2518
const std::string code("void f() { if MACRO(); }");
tokenizeAndStringify(code.c_str(), false);
ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str());
// #2518 and #4171
const char code[] = "void f() { if MACRO() { } }";
ASSERT_EQUALS("void f ( ) { if ( MACRO ( ) ) { } }", tokenizeAndStringify(code, false));
ASSERT_EQUALS("", errout.str());
}
void foreach() {