From 863e44a42bb91d9e2ba8024dfa594ecebe9d3b58 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 22 Nov 2012 09:13:14 +0100 Subject: Remove additional whitespaces from the macro tokens According to the spec, we should ignore whitespace tokens at the beginning and end of the macro definition. In addition, whitespaces after a # and around ## should be ignored Change-Id: I830d0f4aaed3bcfac345d7da6df65693ec3315b8 Reviewed-by: Olivier Goffart --- src/tools/moc/preprocessor.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/tools/moc/preprocessor.cpp') diff --git a/src/tools/moc/preprocessor.cpp b/src/tools/moc/preprocessor.cpp index cb53e665d0a..b49ab9c2107 100644 --- a/src/tools/moc/preprocessor.cpp +++ b/src/tools/moc/preprocessor.cpp @@ -1067,8 +1067,30 @@ void Preprocessor::preprocess(const QByteArray &filename, Symbols &preprocessed) int start = index; until(PP_NEWLINE); macro.symbols.reserve(index - start - 1); - for (int i = start; i < index - 1; ++i) - macro.symbols += symbols.at(i); + + // remove whitespace where there shouldn't be any: + // Before and after the macro, after a # and around ## + Token lastToken = HASH; // skip shitespace at the beginning + for (int i = start; i < index - 1; ++i) { + Token token = symbols.at(i).token; + if (token == PP_WHITESPACE || token == WHITESPACE) { + if (lastToken == PP_HASH || lastToken == HASH || + lastToken == PP_HASHHASH || + lastToken == PP_WHITESPACE || lastToken == WHITESPACE) + continue; + } else if (token == PP_HASHHASH) { + if (!macro.symbols.isEmpty() && + (lastToken == PP_WHITESPACE || lastToken == WHITESPACE)) + macro.symbols.pop_back(); + } + macro.symbols.append(symbols.at(i)); + lastToken = token; + } + // remove trailing whitespace + while (!macro.symbols.isEmpty() && + (macro.symbols.last().token == PP_WHITESPACE || macro.symbols.last().token == WHITESPACE)) + macro.symbols.pop_back(); + macros.insert(name, macro); continue; } -- cgit v1.2.3