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
20 changes: 18 additions & 2 deletions cfg/std.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8831,15 +8831,31 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<valid>0:</valid>
</arg>
</function>
<function name="std::begin,std::cbegin,std::rbegin,std::crbegin">
<function name="std::begin,std::rbegin">
<use-retval/>
<leak-ignore/>
<noreturn>false</noreturn>
<arg nr="1" direction="inout"/>

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

direction="inout" makes std::begin/std::rbegin (and the same change on std::end/std::rend) count their argument as modified every time, even when the iterator is only read. As a result, the PR loses warnings that base reports:

int g1(std::vector<int>& v) { return *std::begin(v); }
bool g2(std::vector<int>& v) { return std::begin(v) == std::end(v); }
int g3(std::vector<int>& v) { return *std::rbegin(v); }
int g5(std::vector<int>& v) { int s = 0; for (auto it = std::begin(v); it != std::end(v); ++it) s += *it; return s; }
int g7(int* p) { std::vector<int>& v = *reinterpret_cast<std::vector<int>*>(p); return *std::begin(v); }

Base gives constParameterReference for g1–g5 and constVariableReference for g7. The PR gives none of these warnings. For a non-const argument, std::begin(x) behaves like the member x.begin(). Whether it changes anything depends on how the returned iterator is used, as it does for .begin(). So the C9 false positive would be better fixed in isVariableChanged(): when the stripped dereference goes into a function with a start-iterator/end-iterator yield, look at how the result is used. That would keep the library entry as in. Please also add a test that checks read-only std::begin(v) still gives constParameterReference.

<container yields="start-iterator"/>
<returnValue type="iterator" container="1"/>
</function>
<function name="std::end,std::rend">
<use-retval/>
<leak-ignore/>
<noreturn>false</noreturn>
<arg nr="1" direction="inout"/>
<container yields="end-iterator"/>
<returnValue type="iterator" container="1"/>
</function>
<function name="std::cbegin,std::crbegin">
<use-retval/>
<leak-ignore/>
<noreturn>false</noreturn>
<arg nr="1" direction="in" indirect="0"/>
<container yields="start-iterator"/>
<returnValue type="iterator" container="1"/>
</function>
<function name="std::end,std::cend,std::rend,std::crend">
<function name="std::cend,std::crend">
<use-retval/>
<leak-ignore/>
<noreturn>false</noreturn>
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
const Token * ptok = tok2;
while (Token::Match(ptok->astParent(), ".|::"))
ptok = ptok->astParent();
int pindirect = indirect;
int pindirect = std::max(indirect - derefs, 0);

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

With pindirect now reduced to 0, std::begin(*p) / std::end(*p) is looked up as direction="in" indirect="0" in std.cfg, so the argument counts as unchanged. The yielded start iterator gives write access, though:

#include <vector>
#include <algorithm>
#include <iterator>
void f1(std::vector<int>* p) { std::fill(std::begin(*p), std::end(*p), 0); }
void f2(std::vector<int>* p) { *std::begin(*p) = 1; }
void f4(std::vector<int>* p) { std::sort(std::begin(*p), std::end(*p)); }

The PR reports constParameterPointer for each of these (base reports nothing). The local-variable form gives constVariablePointer. With const std::vector<int>* p, g++ fails with assignment of read-only location.

The reference version, std::fill(std::begin(v), std::end(v), 0), gives no warning, so the *p path seems to skip a check that the reference path does. The container yield (start-iterator/iterator/buffer) of the called library function should still be taken into account after the dereference is stripped.

if (indirect == 0 && astIsLHS(tok2) && Token::Match(ptok, ". %var%") && astIsPointer(ptok->next()))
pindirect = 1;
bool inconclusive = false;
Expand Down
5 changes: 5 additions & 0 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5120,6 +5120,11 @@ int f_constVariable_std_begin() {
return arr[0];
}

void f_constParameterPointer_std_begin(std::vector<int>* p) {
std::fill(std::begin(*p), std::end(*p), 0);
}


std::size_t uninitvar_std_size() { // #14976
int a[3];
return std::size(a);
Expand Down
13 changes: 13 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5096,6 +5096,11 @@ 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(std::vector<int>& v, int* p) {\n" // #15203
" v.push_back(*p);\n"
"};\n");
ASSERT_EQUALS("[test.cpp:1:34]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());
}

void constArray() {
Expand Down Expand Up @@ -10509,6 +10514,14 @@ class TestOther : public TestFixture {
" return m.trace();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("std::string f(const std::string& s) {\n"
" std::string u = s;\n"
" std::string::iterator i = u.begin();\n"
" *i = toupper(*i);\n"
" return u;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void checkNegativeShift() {
Expand Down
Loading