C++: Better handling of arg count mismatch in macros

Do not expand function-like macros at all when there's a mismatch
in the parameter/argument count.

The report below raises the issue but its expected result is not
correct. This would be the more appropriate fix.

Task-number: QTCREATORBUG-7225
Change-Id: Ide8580faa7b724d3e8b396ec1f899cc5ca7f9e7e
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Leandro Melo
2012-06-12 15:59:07 +02:00
committed by hjk
parent 7eaaab6e73
commit f978400ae5
2 changed files with 32 additions and 53 deletions

View File

@@ -793,7 +793,25 @@ bool Preprocessor::handleIdentifier(PPToken *tk)
if (macro->isFunctionLike()) {
// Collect individual tokens that form the macro arguments.
QVector<QVector<PPToken> > allArgTks;
if (!collectActualArguments(tk, &allArgTks)) {
bool hasArgs = collectActualArguments(tk, &allArgTks);
// Check for matching parameter/argument count.
bool hasMatchingArgs = false;
if (hasArgs) {
const int expectedArgCount = macro->formals().size();
const int actualArgCount = allArgTks.size();
if (expectedArgCount == actualArgCount
|| (macro->isVariadic() && actualArgCount > expectedArgCount - 1)
// Handle '#define foo()' when invoked as 'foo()'
|| (expectedArgCount == 0
&& actualArgCount == 1
&& allArgTks.at(0).isEmpty())) {
hasMatchingArgs = true;
}
}
if (!hasArgs || !hasMatchingArgs) {
//### TODO: error message
pushToken(tk);
*tk = idTk;
return false;
@@ -872,12 +890,6 @@ bool Preprocessor::handleFunctionLikeMacro(PPToken *tk,
int j = 0;
for (; j < formals.size() && expanded.size() < MAX_TOKEN_EXPANSION_COUNT; ++j) {
if (formals[j] == id) {
if (actuals.size() <= j) {
// too few actual parameters
//### TODO: error message
goto exitNicely;
}
QVector<PPToken> actualsForThisParam = actuals.at(j);
if (id == "__VA_ARGS__" || (macro->isVariadic() && j + 1 == formals.size())) {
unsigned lineno = 0;
@@ -929,7 +941,6 @@ bool Preprocessor::handleFunctionLikeMacro(PPToken *tk,
}
}
exitNicely:
pushToken(tk);
if (addWhitespaceMarker) {
PPToken forceWhitespacingToken;