Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/checkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,7 @@ void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, nonneg int numFo

Severity CheckIO::getSeverity(const CheckIO::ArgumentInfo *argInfo)
{
//TODO: It causes issues if we use fixed range types like int32_t etc so we should rewrite this to handle those types properly
return (argInfo && argInfo->typeToken && !argInfo->typeToken->originalName().empty()) ? Severity::portability : Severity::warning;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8237,11 +8237,15 @@ bool ValueType::fromLibraryType(const std::string &typestr, const Settings &sett
else
type = ValueType::Type::UNKNOWN_INT;
sign = (podtype->sign == 'u') ? ValueType::UNSIGNED : ValueType::SIGNED;
if (originalTypeName.empty())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the exact context but can't typestr be something like "int". can it contain qualifiers and stars?

originalTypeName = typestr;
return true;
}
if (podtype && podtype->stdtype == Library::PodType::Type::NO) {
type = ValueType::Type::POD;
sign = ValueType::UNKNOWN_SIGN;
if (originalTypeName.empty())
originalTypeName = typestr;
return true;
}

Expand All @@ -8267,6 +8271,8 @@ bool ValueType::fromLibraryType(const std::string &typestr, const Settings &sett
pointer = 2;
if (platformType->mConstPtr)
constness = 1;
if (originalTypeName.empty())
originalTypeName = typestr;
return true;
}
if (!podtype && (typestr == "size_t" || typestr == "std::size_t")) {
Expand Down
14 changes: 8 additions & 6 deletions test/testio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2915,14 +2915,16 @@ class TestIO : public TestFixture {
"[test.cpp:3]: (warning) %Lf in format string (no. 5) requires 'long double' but the argument type is 'signed int'.\n"
"[test.cpp:3]: (warning) %p in format string (no. 6) requires an address but the argument type is 'signed int'.\n", errout_str());

// We should revisit the logic behind portability checks here.
check("struct Fred { int32_t i; } f;\n"
"struct Fred & bar() { };\n"
"void foo() { printf(\"%d %ld %u %lu %f %Lf\", bar().i, bar().i, bar().i, bar().i, bar().i, bar().i); }");
ASSERT_EQUALS("[test.cpp:3]: (warning) %ld in format string (no. 2) requires 'long' but the argument type is 'signed int'.\n"
"[test.cpp:3]: (warning) %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.\n"
"[test.cpp:3]: (warning) %lu in format string (no. 4) requires 'unsigned long' but the argument type is 'signed int'.\n"
"[test.cpp:3]: (warning) %f in format string (no. 5) requires 'double' but the argument type is 'signed int'.\n"
"[test.cpp:3]: (warning) %Lf in format string (no. 6) requires 'long double' but the argument type is 'signed int'.\n",
"void foo() { printf(\"%d %ld %u %lu %f %Lf\", bar().i, bar().i, bar().i, bar().i, bar().i, bar().i); }",
dinit(CheckOptions, $.portability = true));
ASSERT_EQUALS("[test.cpp:3]: (portability) %ld in format string (no. 2) requires 'long' but the argument type is 'int32_t {aka signed int}'.\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. this looks very unfortunate. If the type is wrong i.e. long/int then I would prefer "warning". Or if we see that there is UB I would even prefer "error".
How difficult is it to keep writing warnings?

"[test.cpp:3]: (portability) %u in format string (no. 3) requires 'unsigned int' but the argument type is 'int32_t {aka signed int}'.\n"
"[test.cpp:3]: (portability) %lu in format string (no. 4) requires 'unsigned long' but the argument type is 'int32_t {aka signed int}'.\n"
"[test.cpp:3]: (portability) %f in format string (no. 5) requires 'double' but the argument type is 'int32_t {aka signed int}'.\n"
"[test.cpp:3]: (portability) %Lf in format string (no. 6) requires 'long double' but the argument type is 'int32_t {aka signed int}'.\n",
errout_str());

// #4984
Expand Down
9 changes: 9 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11354,6 +11354,15 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(tok->valueType()->pointer == 1);
ASSERT(tok->valueType()->constness == 0);
}

{
GET_SYMBOL_DB("int8_t value = 0;");
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "value");
ASSERT(tok);
ASSERT(tok->valueType());
ASSERT(tok->valueType()->originalTypeName == "int8_t");
ASSERT(tok->valueType()->type == ValueType::Type::CHAR);
}
}

void dumpFriend() {
Expand Down
Loading