diff options
author | Leandro Melo <[email protected]> | 2012-06-12 15:59:07 +0200 |
---|---|---|
committer | hjk <[email protected]> | 2012-06-19 12:47:44 +0200 |
commit | f978400ae5c9a7802c110ccd77db9a4dc8a95262 (patch) | |
tree | d83343a0704904581bffe21ae1aa16a862b900e8 /src/libs/cplusplus/pp-engine.cpp | |
parent | 7eaaab6e73c005d74ad3d4de53ca04bf1cb517c0 (diff) |
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 <[email protected]>
Diffstat (limited to 'src/libs/cplusplus/pp-engine.cpp')
-rw-r--r-- | src/libs/cplusplus/pp-engine.cpp | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp index 718f3dd0dfb..fa780f77fea 100644 --- a/src/libs/cplusplus/pp-engine.cpp +++ b/src/libs/cplusplus/pp-engine.cpp @@ -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; |