Skip to content

Commit 7396c64

Browse files
authored
std.cfg: Added support for std::clamp() (danmar#7154)
Reference: https://en.cppreference.com/w/cpp/algorithm/clamp
1 parent b251092 commit 7396c64

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

cfg/std.cfg

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<noreturn>true</noreturn>
7070
</function>
7171
<!-- void unexpected(); (until C++11) -->
72-
<!-- [[noreturn]] void unexpected(); (since C++11) -->
72+
<!-- [[noreturn]] void unexpected(); (since C++11) -->
7373
<!-- (deprecated)(removed in C++17) -->
7474
<function name="std::unexpected">
7575
<noreturn>true</noreturn>
@@ -6363,6 +6363,28 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
63636363
<arg nr="1" direction="in"/>
63646364
<leak-ignore/>
63656365
</function>
6366+
<!-- template< class T > constexpr const T& std::clamp( const T& v, const T& lo, const T& hi); (since C++17) -->
6367+
<!-- template< class T, class Compare > constexpr const T& std::clamp( const T& v, const T& lo, const T& hi, Compare comp ); (since C++17) -->
6368+
<!-- https://en.cppreference.com/w/cpp/algorithm/clamp -->
6369+
<function name="clamp,std::clamp">
6370+
<use-retval/>
6371+
<noreturn>false</noreturn>
6372+
<leak-ignore/>
6373+
<pure/>
6374+
<returnValue>arg1&lt;arg2?arg2:arg1&gt;arg3?arg3:arg1</returnValue>
6375+
<arg nr="1" direction="in">
6376+
<not-uninit/>
6377+
</arg>
6378+
<arg nr="2" direction="in">
6379+
<not-uninit/>
6380+
</arg>
6381+
<arg nr="3" direction="in">
6382+
<not-uninit/>
6383+
</arg>
6384+
<arg nr="4" direction="in" default="">
6385+
<not-uninit/>
6386+
</arg>
6387+
</function>
63666388
<!-- template <class T> const T& min(const T& a, const T& b); -->
63676389
<function name="min,std::min">
63686390
<use-retval/>

test/cfg/std.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ void unreachableCode_std_unexpected(int &x)
7373
}
7474
#endif
7575

76+
#ifdef __cpp_lib_clamp
77+
int ignoredReturnValue_std_clamp(const int x)
78+
{
79+
// cppcheck-suppress ignoredReturnValue
80+
std::clamp(x, 1, -1);
81+
return std::clamp(x, 1, -1);
82+
}
83+
void knownConditionTrueFalse_std_clamp(const int x)
84+
{
85+
// cppcheck-suppress knownConditionTrueFalse
86+
if(std::clamp(-2, -1, 1) == -1){}
87+
// cppcheck-suppress knownConditionTrueFalse
88+
if(std::clamp(2, -1, 1) == 1){}
89+
// cppcheck-suppress knownConditionTrueFalse
90+
if(std::clamp(0, -1, 1) == 0){}
91+
if(std::clamp(x, 0, 2)){}
92+
}
93+
#endif // __cpp_lib_clamp
94+
7695
void unreachableCode_std_terminate(int &x)
7796
{
7897
std::terminate();

0 commit comments

Comments
 (0)