Preprocessor: insert space between ++ or -- in macro expansion

This commit is contained in:
Daniel Marjamäki 2015-12-29 06:54:44 +01:00
parent 997d7dc695
commit 83cb028963
2 changed files with 12 additions and 1 deletions

View File

@ -2776,11 +2776,14 @@ public:
continue; continue;
} }
optcomma = false; optcomma = false;
// separate ++ and -- with space
if (!str.empty() && !macrocode.empty() && (str[0] == '+' || str[0] == '-') && str[0] == macrocode[macrocode.size()-1U])
macrocode += ' ';
macrocode += str; macrocode += str;
if (Token::Match(tok, "%name% %name%|%num%") || if (Token::Match(tok, "%name% %name%|%num%") ||
Token::Match(tok, "%num% %name%") || Token::Match(tok, "%num% %name%") ||
Token::simpleMatch(tok, "> >")) Token::simpleMatch(tok, "> >"))
macrocode += " "; macrocode += ' ';
} }
} }
} }

View File

@ -184,6 +184,7 @@ private:
TEST_CASE(macro_mismatch); TEST_CASE(macro_mismatch);
TEST_CASE(macro_linenumbers); TEST_CASE(macro_linenumbers);
TEST_CASE(macro_nopar); 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_switchCase);
TEST_CASE(macro_NULL); // skip #define NULL .. it is replaced in the tokenizer TEST_CASE(macro_NULL); // skip #define NULL .. it is replaced in the tokenizer
TEST_CASE(string1); TEST_CASE(string1);
@ -1950,6 +1951,13 @@ private:
ASSERT_EQUALS("\n${ $NULL }\n", OurPreprocessor::expandMacros(filedata)); 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 { void macro_switchCase() const {
{ {
// Make sure "case 2" doesn't become "case2" // Make sure "case 2" doesn't become "case2"