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 <ogoffart@woboq.com>
This commit is contained in:
Lars Knoll 2012-11-22 09:13:14 +01:00 committed by The Qt Project
parent 43619db05d
commit 863e44a42b

View File

@ -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;
}