Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,11 @@ namespace {
};
}

static bool isCastToInteger(const Token* tok)
{
return tok && tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && tok->valueType()->pointer == 0;
}

void CheckOther::checkConstPointer()
{
if (!mSettings->severity.isEnabled(Severity::style) &&
Expand Down Expand Up @@ -1883,6 +1888,8 @@ void CheckOther::checkConstPointer()
deref = MEMBER;
else if (astIsRangeBasedForDecl(tok))
continue;
else if (isCastToInteger(parent))
continue;
if (deref != NONE) {
const Token* gparent = parent->astParent();
while (Token::simpleMatch(gparent, "[") && parent != gparent->astOperand2() && parent->str() == gparent->str())
Expand Down
2 changes: 1 addition & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7031,7 +7031,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
setValueType(parent, vt);
return;
}
if (Token::Match(parent->previous(), "%name% (") && parent->astOperand1() == tok && valuetype.pointer > 0U) {
if (Token::Match(parent->tokAt(-1), "%name% (") && !parent->tokAt(-1)->isKeyword() && parent->astOperand1() == tok && valuetype.pointer > 0U) {
ValueType vt(valuetype);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This was added in c12cb69 related to function pointers.

vt.pointer -= 1U;
setValueType(parent, vt);
Expand Down
13 changes: 13 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4598,6 +4598,19 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
errout_str());

check("uintptr_t f(int* p) {\n"
" return (uintptr_t)p;\n"
"}\n"
"uintptr_t g(int* p) {\n"
" return static_cast<uintptr_t>(p);\n"
"}\n"
"U h(int* p) {\n"
" return (U)p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
"[test.cpp:4:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
errout_str());
}

void constArray() {
Expand Down
Loading