]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Improve list assumption after constructor [PR118865]
authorAndrew Pinski <quic_apinski@quicinc.com>
Thu, 13 Feb 2025 22:10:06 +0000 (14:10 -0800)
committerAndrew Pinski <quic_apinski@quicinc.com>
Fri, 14 Feb 2025 23:10:43 +0000 (15:10 -0800)
commit0c3cc57f0e71a7a945fb10c817260dd8a7894e7f
tree78693e035f81091fc2b81e3db14870e5a18f96ca
parent8e44f7ecb7b9ad6893f7784b1a303a5463b46cd5
libstdc++: Improve list assumption after constructor [PR118865]

The code example here does:
```
if (begin == end) __builtin_unreachable();
std::list nl(begin, end);

for (auto it = nl.begin(); it != nl.end(); it++)
{
...
}
/* Remove the first element of the list. */
nl.erase(nl.begin());
```

And we get a warning because because we jump threaded the case were we
think the list was empty from the for loop BUT we populated it without
an empty array. So can help the compiler here by adding that after initializing
the list with non empty array, that the list will not be empty either.

This is able to remove the -Wfree-nonheap-object warning in the first reduced
testcase (with the fix for `begin == end` case added) in the PR 118865; the second
reduced testcase has been filed off as PR 118867.

Bootstrapped and tested on x86_64-linux-gnu.

libstdc++-v3/ChangeLog:

PR libstdc++/118865
* include/bits/stl_list.h (_M_initialize_dispatch): Add an
unreachable if the iterator was not empty that the list will
now be not empty.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
libstdc++-v3/include/bits/stl_list.h