Skip to content

Commit

Permalink
libstdc++: Tell GCC what basic_string::_M_is_local() means [PR109299]
Browse files Browse the repository at this point in the history
This avoids a bogus warning about overflowing a buffer, because GCC
can't tell that we don't copy into the buffer unless it fits. By adding
a __builtin_unreachable() hint we inform the compiler about the
invariant that the buffer is only used when it's big enough.

This can also improve codegen, by eliminating dead code that GCC
couldn't tell was unreachable.

libstdc++-v3/ChangeLog:

	PR libstdc++/109299
	* include/bits/basic_string.h (basic_string::_M_is_local()): Add
	hint for compiler that local strings fit in the local buffer.
  • Loading branch information
jwakely committed Mar 28, 2023
1 parent cf19ef9 commit bf78b43
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libstdc++-v3/include/bits/basic_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
_GLIBCXX20_CONSTEXPR
bool
_M_is_local() const
{ return _M_data() == _M_local_data(); }
{
if (_M_data() == _M_local_data())
{
if (_M_string_length > _S_local_capacity)
__builtin_unreachable();
return true;
}
return false;
}

// Create & Destroy
_GLIBCXX20_CONSTEXPR
Expand Down

0 comments on commit bf78b43

Please sign in to comment.