From 107b3b44016f90adc4d9b566dbce7d95c5ac4cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 9 Sep 2012 09:48:07 +0200 Subject: [PATCH] Fixed #4171 (don't choke when parentheses are missing from macros) --- lib/tokenize.cpp | 18 ++++++++++++++++-- test/testtokenize.cpp | 8 ++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2639ec3aa..b0f2695b5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; + } + } } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 24f75888d..932dfddaa 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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() {