Skip to content

Commit 7ae7ad6

Browse files
authored
Added cfg information about std::span (#4762)
* Added cfg information about std::span * Add tests for span handling * Add details about functions and tests fo std::span * Add tests in dangingLifetimeContainerView for span * Reduce c++ version from 20 to 2a * Add checking if span is supported in std lib cfg checks
1 parent 5818520 commit 7ae7ad6

4 files changed

Lines changed: 168 additions & 1 deletion

File tree

cfg/std.cfg

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8486,6 +8486,37 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
84868486
<not-uninit/>
84878487
</arg>
84888488
</function>
8489+
<function name="std::span::first">
8490+
<use-retval/>
8491+
<returnValue type="std::span"/>
8492+
<noreturn>false</noreturn>
8493+
<arg nr="1" direction="in">
8494+
<not-uninit/>
8495+
<valid>0:</valid>
8496+
</arg>
8497+
</function>
8498+
<function name="std::span::last">
8499+
<use-retval/>
8500+
<returnValue type="std::span"/>
8501+
<noreturn>false</noreturn>
8502+
<arg nr="1" direction="in">
8503+
<not-uninit/>
8504+
<valid>0:</valid>
8505+
</arg>
8506+
</function>
8507+
<function name="std::span::subspan">
8508+
<use-retval/>
8509+
<returnValue type="std::span"/>
8510+
<noreturn>false</noreturn>
8511+
<arg nr="1" direction="in">
8512+
<not-uninit/>
8513+
<valid>0:</valid>
8514+
</arg>
8515+
<arg nr="2" direction="in">
8516+
<not-uninit/>
8517+
<valid>0:</valid>
8518+
</arg>
8519+
</function>
84898520
<memory>
84908521
<alloc init="false" buffer-size="malloc">malloc</alloc>
84918522
<alloc init="true" buffer-size="calloc">calloc</alloc>
@@ -8700,6 +8731,13 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
87008731
<container id="stdStringView" startPattern="std :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" inherits="stdAllStringView"/>
87018732
<container id="stdExperimentalStringView" startPattern="std :: experimental :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" inherits="stdAllStringView"/>
87028733
<container id="stdExperimentalBasicStringView" startPattern="std :: experimental :: basic_string_view &lt;" inherits="stdBasicStringView" />
8734+
<container id="stdSpan" startPattern="std :: span" endPattern="" inherits="stdContainer" view="true">
8735+
<access indexOperator="array-like">
8736+
<function name="front" yields="item"/>
8737+
<function name="back" yields="item"/>
8738+
<function name="data" yields="buffer"/>
8739+
</access>
8740+
</container>
87038741
<smart-pointer class-name="std::auto_ptr">
87048742
<unique/>
87058743
</smart-pointer>
@@ -8747,6 +8785,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
87478785
<check>std::ios_base::failure</check>
87488786
<check>std::filesystem::filesystem_error</check>
87498787
<check>std::bad_variant_access</check>
8788+
<check>std::span</check>
87508789
</unusedvar>
87518790
<operatorEqVarError>
87528791
<suppress>std::mutex</suppress>

test/cfg/runtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ CPPCHECK_OPT='--check-library --platform=unix64 --enable=style --error-exitcode=
2727

2828
# Compiler settings
2929
CXX=g++
30-
CXX_OPT='-fsyntax-only -std=c++17 -Wno-format -Wno-format-security -Wno-deprecated-declarations'
30+
CXX_OPT='-fsyntax-only -std=c++2a -Wno-format -Wno-format-security -Wno-deprecated-declarations'
3131
CC=gcc
3232
CC_OPT='-Wno-format -Wno-stringop-overread -Wno-nonnull -Wno-implicit-function-declaration -Wno-deprecated-declarations -Wno-format-security -Wno-nonnull -fsyntax-only'
3333

test/cfg/std.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include <string_view>
4040
#include <unordered_set>
4141
#include <vector>
42+
#include <version>
43+
#ifdef __cpp_lib_span
44+
#include <span>
45+
#endif
4246

4347
int zerodiv_ldexp()
4448
{
@@ -4562,4 +4566,43 @@ void string_view_unused(std::string_view v)
45624566
{
45634567
// cppcheck-suppress ignoredReturnValue
45644568
v.substr(1, 3);
4569+
}
4570+
4571+
void stdspan()
4572+
{
4573+
#ifndef __cpp_lib_span
4574+
#warning "This compiler does not support std::span"
4575+
#else
4576+
std::vector<int> vec{1,2,3,4};
4577+
std::span spn{vec};
4578+
// cppcheck-suppress unreadVariable
4579+
std::span spn2 = spn;
4580+
4581+
spn.begin();
4582+
spn.end();
4583+
spn.rbegin();
4584+
spn.end();
4585+
4586+
spn.front();
4587+
spn.back();
4588+
//cppcheck-suppress constStatement
4589+
spn[0];
4590+
spn.data();
4591+
spn.size();
4592+
spn.size_bytes();
4593+
spn.empty();
4594+
//cppcheck-suppress ignoredReturnValue
4595+
spn.first(2);
4596+
//cppcheck-suppress ignoredReturnValue
4597+
spn.last(2);
4598+
//cppcheck-suppress ignoredReturnValue
4599+
spn.subspan(1, 2);
4600+
spn.subspan<1>();
4601+
4602+
static constexpr std::array<int, 2> arr{1, 2};
4603+
constexpr std::span spn3{arr};
4604+
spn3.first<1>();
4605+
spn3.last<1>();
4606+
spn3.subspan<1, 1>();
4607+
#endif
45654608
}

test/testautovariables.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,91 @@ class TestAutoVariables : public TestFixture {
27992799
"}\n");
28002800
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2] -> [test.cpp:3]: (error) Using object that is a temporary.\n",
28012801
errout.str());
2802+
2803+
check("std::span<int> f() {\n"
2804+
" std::vector<int> v{};\n"
2805+
" return v;\n"
2806+
"}\n");
2807+
ASSERT_EQUALS(
2808+
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning object that points to local variable 'v' that will be invalid when returning.\n",
2809+
errout.str());
2810+
2811+
check("std::span<int> f() {\n"
2812+
" std::vector<int> v;\n"
2813+
" std::span sp = v;\n"
2814+
" return sp;\n"
2815+
"}\n");
2816+
ASSERT_EQUALS(
2817+
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:4]: (error) Returning object that points to local variable 'v' that will be invalid when returning.\n",
2818+
errout.str());
2819+
2820+
check("std::span<int> f() {\n"
2821+
" std::vector<int> v;\n"
2822+
" return std::span{v};\n"
2823+
"}\n");
2824+
ASSERT_EQUALS(
2825+
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning object that points to local variable 'v' that will be invalid when returning.\n",
2826+
errout.str());
2827+
2828+
check("int f() {\n"
2829+
" std::span<int> s;\n"
2830+
" {\n"
2831+
" std::vector<int> v(1);"
2832+
" s = v;\n"
2833+
" }\n"
2834+
"return s.back()\n"
2835+
"}\n");
2836+
ASSERT_EQUALS(
2837+
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
2838+
errout.str());
2839+
2840+
check("int f() {\n"
2841+
" std::span<int> s;\n"
2842+
" {\n"
2843+
" std::vector<int> v(1);"
2844+
" s = v;\n"
2845+
" }\n"
2846+
"return s.back()\n"
2847+
"}\n");
2848+
ASSERT_EQUALS(
2849+
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
2850+
errout.str());
2851+
2852+
check("int f() {\n"
2853+
" std::span<int> s;\n"
2854+
" {\n"
2855+
" std::vector<int> v(1);"
2856+
" s = v;\n"
2857+
" }\n"
2858+
"return s.front()\n"
2859+
"}\n");
2860+
ASSERT_EQUALS(
2861+
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
2862+
errout.str());
2863+
2864+
check("int f() {\n"
2865+
" std::span<int> s;\n"
2866+
" {\n"
2867+
" std::vector<int> v(1);"
2868+
" s = v;\n"
2869+
" }\n"
2870+
"return s.last(1)\n"
2871+
"}\n");
2872+
ASSERT_EQUALS(
2873+
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
2874+
errout.str());
2875+
2876+
check("int f() {\n"
2877+
" std::span<int> s;\n"
2878+
" {\n"
2879+
" std::vector<int> v(1);"
2880+
" s = v;\n"
2881+
" }\n"
2882+
"return s.first(1)\n"
2883+
"}\n");
2884+
ASSERT_EQUALS(
2885+
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
2886+
errout.str());
28022887
}
28032888

28042889
void danglingLifetimeUniquePtr()

0 commit comments

Comments
 (0)