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
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn)
parent = parent->astParent();
while (parent && parent->isCast())
parent = parent->astParent();
if (Token::Match(parent, "[+-]") && parent->valueType() && parent->valueType()->pointer)
if (Token::Match(parent, "[+-]") && (astIsPointer(parent) || astIsPointer(tok)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

astIsPointer(tok) is true for each operand of a pointer difference. The result of p - q is a known non-pointer type (ptrdiff_t), yet the - is now skipped, and getTokenArgumentFunction(p) returns g. If g is unknown, p and q are assumed to be changed.

Reproduced. Base reports these and the PR does not:

void f1(int* p, int* q) { g(p - q); }               // constParameterPointer p, q lost
void f(int* p, int* q) { int* a = p; int* b = q; g(a - b); }   // constVariablePointer a, b lost
void k(int* q) { int* a = nullptr; g(a - q); if (a == nullptr) {} }  // knownConditionTrueFalse lost

Suggestion: fall back to astIsPointer(tok) only when parent has no valueType, for example (astIsPointer(parent) || (!parent->valueType() && astIsPointer(tok))).

parent = parent->astParent();

// passing variable to subfunction?
Expand Down
2 changes: 1 addition & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ void CheckOtherImpl::checkConstPointer()
int argn = -1;
if (Token::simpleMatch(gparent, "return")) {
const Function* function = getEnclosingFunction(var);
if (function && (!Function::returnsReference(function) || Function::returnsConst(function)))
if (function && (!Function::returnsReference(function, /*unknown*/ true) || Function::returnsConst(function)))
Comment thread
chrchr-github marked this conversation as resolved.
Resolved
continue;
}
else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) {
Expand Down
25 changes: 17 additions & 8 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3352,15 +3352,20 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl
if (function->type != FunctionType::eFunction && function->type != FunctionType::eOperatorEqual && function->type != FunctionType::eLambda)
return false;
const Token* defStart = function->retDef;
if (!defStart)
if (!defStart && !function->isLambda())
return unknown;
const Token* defEnd = function->returnDefEnd();
if (!defEnd)
return unknown;
if (defEnd == defStart)
return unknown;
if (pred(defStart, defEnd))
bool conclusive = false;
if (pred(defStart, defEnd, conclusive))
return true;
if (conclusive)
return false;
if (!defStart)
return unknown;
if (Token::Match(defEnd->tokAt(-1), "*|&|&&"))
return false;
// void STDCALL foo()
Expand All @@ -3383,7 +3388,7 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl
}
}
assert(defEnd != defStart);
if (pred(defStart, defEnd))
if (pred(defStart, defEnd, conclusive))
return true;
if (isUnknownType(defStart, defEnd)) {
const Token* tok = function->token ? function->token->next() : function->tokenDef->next();
Expand All @@ -3396,35 +3401,39 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl

bool Function::returnsConst(const Function* function, bool unknown)
{
return checkReturns(function, unknown, false, [](const Token* defStart, const Token* defEnd) {
return checkReturns(function, unknown, false, [](const Token* defStart, const Token* defEnd, bool& /*conclusive*/) {
return Token::findsimplematch(defStart, "const", defEnd);
});
}

bool Function::returnsReference(const Function* function, bool unknown, bool includeRValueRef)
{
return checkReturns(function, unknown, false, [includeRValueRef](const Token* /*defStart*/, const Token* defEnd) {
return checkReturns(function, unknown, false, [includeRValueRef](const Token* /*defStart*/, const Token* defEnd, bool& conclusive) {
if (Token::simpleMatch(defEnd->previous(), "auto") || Token::simpleMatch(defEnd, "[")) {
conclusive = true;
return false;
}
return includeRValueRef ? Token::Match(defEnd->previous(), "&|&&") : Token::simpleMatch(defEnd->previous(), "&");
});
}

bool Function::returnsPointer(const Function* function, bool unknown)
{
return checkReturns(function, unknown, false, [](const Token* /*defStart*/, const Token* defEnd) {
return checkReturns(function, unknown, false, [](const Token* /*defStart*/, const Token* defEnd, bool& /*conclusive*/) {
return Token::simpleMatch(defEnd->previous(), "*");
});
}

bool Function::returnsStandardType(const Function* function, bool unknown)
{
return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd) {
return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd, bool& /*conclusive*/) {
return defEnd->previous() && defEnd->previous()->isStandardType();
});
}

bool Function::returnsVoid(const Function* function, bool unknown)
{
return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd) {
return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd, bool& /*conclusive*/) {
return Token::simpleMatch(defEnd->previous(), "void");
});
}
Expand Down
24 changes: 23 additions & 1 deletion test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4966,7 +4966,8 @@ class TestOther : public TestFixture {
" int i = 0;\n"
" return [](int* p) { return *p; }(&i);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());
ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
errout_str());

check("struct S {\n" // #14571
" char* c;\n"
Expand Down Expand Up @@ -5096,6 +5097,27 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:8]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
"[test.cpp:3:8]: (style) Parameter 'q' can be declared as pointer to const [constParameterPointer]\n",
errout_str());

check("void f(int* p, U n) {\n" // #15226
" g(*(p + n));\n"
"}\n"
"U g(int* p, int n) {\n"
" return *(p + n);\n"
"}\n"
"auto h1(int* p) {\n"
" return *p;\n"
"}\n"
"void h2(int* p) {\n"
" g(h() + *p);\n"
"}\n"
"void h3(int* p) {\n"
" int* q = p;\n"
" g(h() - *q);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7:14]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
"[test.cpp:10:14]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
"[test.cpp:14:10]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n",
errout_str());
}

void constArray() {
Expand Down
Loading