CheckIO: improved handling of functions returning pointers. Ticket: #4964

This commit is contained in:
Robert Reif 2013-08-28 05:57:40 +02:00 committed by Daniel Marjamäki
parent e223e13747
commit 5c3315db7a
2 changed files with 21 additions and 3 deletions

View File

@ -661,7 +661,8 @@ void CheckIO::checkWrongPrintfScanfArguments()
case 'G':
specifier += *i;
if (functionInfo && varTypeTok && (((varTypeTok->isStandardType() || functionInfo->retType) && !Token::Match(varTypeTok, "float|double")) ||
Token::simpleMatch(varTypeTok->next(), "*") ||
(!element && Token::simpleMatch(varTypeTok->next(), "*")) ||
(element && !Token::simpleMatch(varTypeTok->next(), "*")) ||
(specifier[0] == 'l' && (!varTypeTok->isLong() || varTypeTok->str() != "double")) ||
(specifier[0] != 'l' && varTypeTok->isLong())))
invalidPrintfArgTypeError_float(tok, numFormat, specifier);
@ -781,9 +782,19 @@ bool CheckIO::getArgumentInfo(const Token * tok, const Variable **var, const Tok
const Token *tok1 = tok->next();
for (; tok1; tok1 = tok1->next()) {
if (tok1->str() == "," || tok1->str() == ")") {
if (tok1->previous()->str() == "]")
if (tok1->previous()->str() == "]") {
varTok = tok1->linkAt(-1)->previous();
else if (tok1->previous()->str() == ")" && tok1->linkAt(-1)->previous()->type() == Token::eFunction) {
if (varTok->str() == ")" && varTok->link()->previous()->type() == Token::eFunction) {
const Function * function = varTok->link()->previous()->function();
if (function) {
*var = 0;
*typeTok = function->retDef;
*func = function;
element = true;
return true;
}
}
} else if (tok1->previous()->str() == ")" && tok1->linkAt(-1)->previous()->type() == Token::eFunction) {
const Function * function = tok1->linkAt(-1)->previous()->function();
if (function) {
*var = 0;

View File

@ -1021,6 +1021,13 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("int array[10];\n"
"int * foo() { return array; }\n"
"void f() {\n"
" printf(\"%f\", foo()[0]);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) %f in format string (no. 1) requires a floating point number given in the argument list.\n", errout.str());
}
void testPosixPrintfScanfParameterPosition() { // #4900 - No support for parameters in format strings