From 83cb0289631c4987b50e80ae94a5e480e7de7bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 Dec 2015 06:54:44 +0100 Subject: [PATCH] Preprocessor: insert space between ++ or -- in macro expansion --- lib/preprocessor.cpp | 5 ++++- test/testpreprocessor.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 752e57b8c..129b1dcb4 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -2776,11 +2776,14 @@ public: continue; } optcomma = false; + // separate ++ and -- with space + if (!str.empty() && !macrocode.empty() && (str[0] == '+' || str[0] == '-') && str[0] == macrocode[macrocode.size()-1U]) + macrocode += ' '; macrocode += str; if (Token::Match(tok, "%name% %name%|%num%") || Token::Match(tok, "%num% %name%") || Token::simpleMatch(tok, "> >")) - macrocode += " "; + macrocode += ' '; } } } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 5ee9a6d88..2a976ecf4 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -184,6 +184,7 @@ private: TEST_CASE(macro_mismatch); TEST_CASE(macro_linenumbers); TEST_CASE(macro_nopar); + TEST_CASE(macro_incdec); // separate ++ and -- with space when expanding such macro: '#define M(X) A-X' TEST_CASE(macro_switchCase); TEST_CASE(macro_NULL); // skip #define NULL .. it is replaced in the tokenizer TEST_CASE(string1); @@ -1950,6 +1951,13 @@ private: ASSERT_EQUALS("\n${ $NULL }\n", OurPreprocessor::expandMacros(filedata)); } + void macro_incdec() const { + const char filedata[] = "#define M1(X) 1+X\n" + "#define M2(X) 2-X\n" + "M1(+1) M2(-1)\n"; + ASSERT_EQUALS("\n\n$1+ +$1 $2- -$1\n", OurPreprocessor::expandMacros(filedata)); + } + void macro_switchCase() const { { // Make sure "case 2" doesn't become "case2"