Skip to content
Merged
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
39 changes: 39 additions & 0 deletions cfg/std.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8486,6 +8486,37 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<not-uninit/>
</arg>
</function>
<function name="std::span::first">
<use-retval/>
<returnValue type="std::span"/>
<noreturn>false</noreturn>
<arg nr="1" direction="in">
<not-uninit/>
<valid>0:</valid>
</arg>
</function>
<function name="std::span::last">
<use-retval/>
<returnValue type="std::span"/>
<noreturn>false</noreturn>
<arg nr="1" direction="in">
<not-uninit/>
<valid>0:</valid>
</arg>
</function>
<function name="std::span::subspan">
<use-retval/>
<returnValue type="std::span"/>
<noreturn>false</noreturn>
<arg nr="1" direction="in">
<not-uninit/>
<valid>0:</valid>
</arg>
<arg nr="2" direction="in">
<not-uninit/>
<valid>0:</valid>
</arg>
</function>
<memory>
<alloc init="false" buffer-size="malloc">malloc</alloc>
<alloc init="true" buffer-size="calloc">calloc</alloc>
Expand Down Expand Up @@ -8700,6 +8731,13 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<container id="stdStringView" startPattern="std :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" inherits="stdAllStringView"/>
<container id="stdExperimentalStringView" startPattern="std :: experimental :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" inherits="stdAllStringView"/>
<container id="stdExperimentalBasicStringView" startPattern="std :: experimental :: basic_string_view &lt;" inherits="stdBasicStringView" />
<container id="stdSpan" startPattern="std :: span" endPattern="" inherits="stdContainer" view="true">
<access indexOperator="array-like">
<function name="front" yields="item"/>
<function name="back" yields="item"/>
<function name="data" yields="buffer"/>
</access>
</container>
<smart-pointer class-name="std::auto_ptr">
<unique/>
</smart-pointer>
Expand Down Expand Up @@ -8747,6 +8785,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<check>std::ios_base::failure</check>
<check>std::filesystem::filesystem_error</check>
<check>std::bad_variant_access</check>
<check>std::span</check>
</unusedvar>
<operatorEqVarError>
<suppress>std::mutex</suppress>
Expand Down
2 changes: 1 addition & 1 deletion test/cfg/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CPPCHECK_OPT='--check-library --platform=unix64 --enable=style --error-exitcode=

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

Expand Down
43 changes: 43 additions & 0 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <string_view>
#include <unordered_set>
#include <vector>
#include <version>
#ifdef __cpp_lib_span
#include <span>
#endif

int zerodiv_ldexp()
{
Expand Down Expand Up @@ -4562,4 +4566,43 @@ void string_view_unused(std::string_view v)
{
// cppcheck-suppress ignoredReturnValue
v.substr(1, 3);
}

void stdspan()
{
#ifndef __cpp_lib_span
#warning "This compiler does not support std::span"
#else
std::vector<int> vec{1,2,3,4};
std::span spn{vec};
// cppcheck-suppress unreadVariable
std::span spn2 = spn;

spn.begin();
spn.end();
spn.rbegin();
spn.end();

spn.front();
spn.back();
//cppcheck-suppress constStatement
spn[0];
spn.data();
spn.size();
spn.size_bytes();
spn.empty();
//cppcheck-suppress ignoredReturnValue
spn.first(2);
//cppcheck-suppress ignoredReturnValue
spn.last(2);
//cppcheck-suppress ignoredReturnValue
spn.subspan(1, 2);
spn.subspan<1>();

static constexpr std::array<int, 2> arr{1, 2};
constexpr std::span spn3{arr};
spn3.first<1>();
spn3.last<1>();
spn3.subspan<1, 1>();
#endif
}
85 changes: 85 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,91 @@ class TestAutoVariables : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2] -> [test.cpp:3]: (error) Using object that is a temporary.\n",
errout.str());

check("std::span<int> f() {\n"
" std::vector<int> v{};\n"
" return v;\n"
"}\n");
ASSERT_EQUALS(
"[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",
errout.str());

check("std::span<int> f() {\n"
" std::vector<int> v;\n"
" std::span sp = v;\n"
" return sp;\n"
"}\n");
ASSERT_EQUALS(
"[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",
errout.str());

check("std::span<int> f() {\n"
" std::vector<int> v;\n"
" return std::span{v};\n"
"}\n");
ASSERT_EQUALS(
"[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",
errout.str());

check("int f() {\n"
" std::span<int> s;\n"
" {\n"
" std::vector<int> v(1);"
" s = v;\n"
" }\n"
"return s.back()\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
errout.str());

check("int f() {\n"
" std::span<int> s;\n"
" {\n"
" std::vector<int> v(1);"
" s = v;\n"
" }\n"
"return s.back()\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
errout.str());

check("int f() {\n"
" std::span<int> s;\n"
" {\n"
" std::vector<int> v(1);"
" s = v;\n"
" }\n"
"return s.front()\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
errout.str());

check("int f() {\n"
" std::span<int> s;\n"
" {\n"
" std::vector<int> v(1);"
" s = v;\n"
" }\n"
"return s.last(1)\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
errout.str());

check("int f() {\n"
" std::span<int> s;\n"
" {\n"
" std::vector<int> v(1);"
" s = v;\n"
" }\n"
"return s.first(1)\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:4] -> [test.cpp:6]: (error) Using object that points to local variable 'v' that is out of scope.\n",
errout.str());
}

void danglingLifetimeUniquePtr()
Expand Down