diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d21484c52..bdbb6e44b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2041,6 +2041,16 @@ bool Tokenizer::tokenize(std::istream &code, } } + // if MACRO + for (const Token *tok = _tokens; tok; tok = tok->next()) + { + if (Token::Match(tok, "if|for|while %var% (")) + { + syntaxError(tok); + return false; + } + } + // Simplify JAVA/C# code if (isJavaOrCSharp() && _files[0].find(".java") != std::string::npos) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 561b91a95..d252696c3 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -52,6 +52,7 @@ private: // don't freak out when the syntax is wrong TEST_CASE(wrong_syntax); + TEST_CASE(wrong_syntax_if_macro); // #2518 - if MACRO() TEST_CASE(minus); @@ -522,6 +523,15 @@ private: } } + void wrong_syntax_if_macro() + { + // #2518 + errout.str(""); + const std::string code("void f() { if MACRO(); }"); + tokenizeAndStringify(code.c_str(), false); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + } + void minus() { ASSERT_EQUALS("i = -12", tokenizeAndStringify("i = -12"));