Fixed #4668 (Tokenizer: improve handling of 'if (x) MACRO() { ..')

This commit is contained in:
Daniel Marjamki 2013-06-15 14:40:05 +02:00
parent 15c92384f4
commit 03f28e9c65
2 changed files with 22 additions and 2 deletions

View File

@ -1602,6 +1602,19 @@ bool Tokenizer::tokenize(std::istream &code,
return false; return false;
} }
// if (x) MACRO() ..
for (const Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "if (")) {
tok = tok->next()->link();
if (Token::Match(tok, ") %var% (") &&
tok->next()->isUpperCaseName() &&
Token::Match(tok->linkAt(2), ") {|else")) {
syntaxError(tok->next());
return false;
}
}
}
// replace 'NULL' and similar '0'-defined macros with '0' // replace 'NULL' and similar '0'-defined macros with '0'
simplifyNull(); simplifyNull();

View File

@ -799,8 +799,15 @@ private:
void wrong_syntax_if_macro() { void wrong_syntax_if_macro() {
// #2518 #4171 // #2518 #4171
const std::string code("void f() { if MACRO(); }"); tokenizeAndStringify("void f() { if MACRO(); }", false);
tokenizeAndStringify(code.c_str(), false); ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str());
// #4668 - note there is no semicolon after MACRO()
tokenizeAndStringify("void f() { if (x) MACRO() {} }", false);
ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str());
// #4810 - note there is no semicolon after MACRO()
tokenizeAndStringify("void f() { if (x) MACRO() else ; }", false);
ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str());
} }