| 1 |
// PR tree-optimization/83239 - False positive from -Wstringop-overflow |
| 2 |
// on simple std::vector code |
| 3 |
// { dg-do compile } |
| 4 |
// { dg-options "-O3 -finline-limit=500 -Wall -fdump-tree-optimized" } |
| 5 |
// { dg-skip-if "requires hosted libstdc++ for vector" { ! hostedlib } } |
| 6 |
|
| 7 |
#include <vector> |
| 8 |
|
| 9 |
// Verify no warnings are issued. |
| 10 |
|
| 11 |
template <class T> |
| 12 |
void test_loop () |
| 13 |
{ |
| 14 |
std::vector<T> a; |
| 15 |
|
| 16 |
int num = 2; |
| 17 |
|
| 18 |
while (num > 0) |
| 19 |
{ |
| 20 |
const typename std::vector<T>::size_type sz = a.size (); |
| 21 |
|
| 22 |
if (sz < 3) |
| 23 |
a.assign (1, 0); |
| 24 |
else |
| 25 |
a.resize (sz - 2); |
| 26 |
|
| 27 |
--num; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// Verify no warnings are issued here either. |
| 32 |
|
| 33 |
template <class T> |
| 34 |
void test_if (std::vector<T> &a, int num) |
| 35 |
{ |
| 36 |
if (num > 0) |
| 37 |
{ |
| 38 |
const typename std::vector<T>::size_type sz = a.size (); |
| 39 |
|
| 40 |
if (sz < 3) |
| 41 |
a.assign (1, 0); |
| 42 |
else |
| 43 |
a.resize (sz - 2); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Instantiate each function on a different type to force both |
| 48 |
// to be fully inlined. Instantiating both on the same type |
| 49 |
// causes the inlining heuristics to outline _M_default_append |
| 50 |
// which, in turn, masks the warning. |
| 51 |
template void test_loop<int>(); |
| 52 |
template void test_if<long>(std::vector<long>&, int); |
| 53 |
|
| 54 |
// Verify that std::vector<T>::_M_default_append() has been inlined |
| 55 |
// (the absence of warnings depends on it). |
| 56 |
// { dg-final { scan-tree-dump-not "_ZNSt6vectorIiSaIiEE17_M_default_appendEm" optimized } } |
| 57 |
// { dg-final { scan-tree-dump-not "_ZNSt6vectorIPvSaIS0_EE17_M_default_appendEm" optimized } } |