Fix #8489 (Fix FN printf argument with parenthesis) (#2508)

This commit is contained in:
Rikard Falkeborn 2020-01-27 06:55:01 +01:00 committed by Daniel Marjamäki
parent 50216d53dd
commit 8819e19dae
2 changed files with 20 additions and 0 deletions

View File

@ -1323,6 +1323,8 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
// TODO: This is a bailout so that old code is used in simple cases. Remove the old code and always use the AST type.
if (!Token::Match(arg, "%str% ,|)") && !(Token::Match(arg,"%var%") && arg->variable() && arg->variable()->isArray())) {
const Token *top = arg;
while (top->str() == "(" && !top->isCast())
top = top->next();
while (top->astParent() && top->astParent()->str() != "," && top->astParent() != arg->previous())
top = top->astParent();
const ValueType *valuetype = top->argumentType();

View File

@ -73,6 +73,7 @@ private:
TEST_CASE(testPrintfTypeAlias1);
TEST_CASE(testPrintfAuto); // #8992
TEST_CASE(testPrintfParenthesis); // #8489
}
void check(const char* code, bool inconclusive = false, bool portability = false, Settings::PlatformType platform = Settings::Unspecified) {
@ -4804,6 +4805,23 @@ private:
"}\n", false, true);
ASSERT_EQUALS("[test.cpp:4]: (portability) %f in format string (no. 1) requires 'double' but the argument type is 'size_t {aka unsigned long}'.\n", errout.str());
}
void testPrintfParenthesis() { // #8489
check("void f(int a) {\n"
" printf(\"%f\", (a >> 24) & 0xff);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
check("void f(int a) {\n"
" printf(\"%f\", 0xff & (a >> 24));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
check("void f(int a) {\n"
" printf(\"%f\", ((a >> 24) + 1) & 0xff);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
}
};
REGISTER_TEST(TestIO)