Skip to content

Commit 739aa3e

Browse files
author
Advenam Tacet
committed
Conditionalize ASan variables
Variables used only by ASan helper functions are conditionalized (put behind `#ifndef _LIBCPP_HAS_NO_ASAN`) after that commit. That should remove any impact on performance.
1 parent 2060199 commit 739aa3e

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

libcxx/include/string

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,9 @@ public:
13681368
_LIBCPP_HIDE_FROM_ABI constexpr void __move_assign(basic_string&& __str, size_type __pos, size_type __len) {
13691369
// Pilfer the allocation from __str.
13701370
_LIBCPP_ASSERT_INTERNAL(__alloc() == __str.__alloc(), "__move_assign called with wrong allocator");
1371+
#ifndef __LIBCPP_HAS_NO_ASAN
13711372
size_type __old_sz = __str.size();
1373+
#endif
13721374
_LIBCPP_IF_ASAN() {
13731375
if (!__str.__is_long())
13741376
__str.__annotate_delete();
@@ -1382,11 +1384,13 @@ public:
13821384
_Traits::assign(data()[__len], value_type());
13831385

13841386
_LIBCPP_IF_ASAN() {
1387+
#ifndef __LIBCPP_HAS_NO_ASAN
13851388
if (!__is_long()) {
13861389
__annotate_new(__len);
13871390
} else if (__old_sz > __len) {
13881391
__annotate_shrink(__old_sz);
13891392
}
1393+
#endif
13901394
}
13911395
}
13921396
#endif
@@ -2091,35 +2095,43 @@ private:
20912095

20922096
// Assigns the value in __s, guaranteed to be __n < __min_cap in length.
20932097
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_short(const value_type* __s, size_type __n) {
2098+
#ifndef __LIBCPP_HAS_NO_ASAN
20942099
size_type __old_size = size();
20952100
_LIBCPP_IF_ASAN() {
20962101
if (__n > __old_size)
20972102
__annotate_increase(__n - __old_size);
20982103
}
2104+
#endif
20992105
pointer __p =
21002106
__is_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_pointer());
21012107
traits_type::move(std::__to_address(__p), __s, __n);
21022108
traits_type::assign(__p[__n], value_type());
2109+
#ifndef __LIBCPP_HAS_NO_ASAN
21032110
_LIBCPP_IF_ASAN() {
21042111
if (__old_size > __n)
21052112
__annotate_shrink(__old_size);
21062113
}
2114+
#endif
21072115
return *this;
21082116
}
21092117

21102118
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
21112119
__null_terminate_at(value_type* __p, size_type __newsz) {
2120+
#ifndef __LIBCPP_HAS_NO_ASAN
21122121
size_type __old_size = size();
21132122
_LIBCPP_IF_ASAN() {
21142123
if (__newsz > __old_size)
21152124
__annotate_increase(__newsz - __old_size);
21162125
}
2126+
#endif
21172127
__set_size(__newsz);
21182128
traits_type::assign(__p[__newsz], value_type());
2129+
#ifndef __LIBCPP_HAS_NO_ASAN
21192130
_LIBCPP_IF_ASAN() {
21202131
if (__old_size > __newsz)
21212132
__annotate_shrink(__old_size);
21222133
}
2134+
#endif
21232135
return *this;
21242136
}
21252137

@@ -2463,19 +2475,23 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Al
24632475
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
24642476
size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
24652477
if (__n < __cap) {
2478+
#ifndef __LIBCPP_HAS_NO_ASAN
24662479
size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
24672480
_LIBCPP_IF_ASAN() {
24682481
if (__n > __old_size)
24692482
__annotate_increase(__n - __old_size);
24702483
}
2484+
#endif
24712485
pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
24722486
__is_short ? __set_short_size(__n) : __set_long_size(__n);
24732487
traits_type::copy(std::__to_address(__p), __s, __n);
24742488
traits_type::assign(__p[__n], value_type());
2489+
#ifndef __LIBCPP_HAS_NO_ASAN
24752490
_LIBCPP_IF_ASAN() {
24762491
if (__old_size > __n)
24772492
__annotate_shrink(__old_size);
24782493
}
2494+
#endif
24792495
} else {
24802496
size_type __sz = __is_short ? __get_short_size() : __get_long_size();
24812497
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
@@ -2488,10 +2504,9 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Al
24882504
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
24892505
size_type __cap = capacity();
24902506
if (__cap >= __n) {
2491-
size_type __old_size = size();
24922507
_LIBCPP_IF_ASAN() {
2493-
if (__n > __old_size)
2494-
__annotate_increase(__n - __old_size);
2508+
if (__n > size())
2509+
__annotate_increase(__n - size());
24952510
}
24962511
value_type* __p = std::__to_address(__get_pointer());
24972512
traits_type::move(__p, __s, __n);
@@ -2514,14 +2529,13 @@ template <class _CharT, class _Traits, class _Allocator>
25142529
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
25152530
basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) {
25162531
size_type __cap = capacity();
2517-
size_type __old_size = size();
25182532
if (__cap < __n) {
25192533
size_type __sz = size();
25202534
__grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
25212535
_LIBCPP_IF_ASAN() { __annotate_increase(__n); }
25222536
} else _LIBCPP_IF_ASAN() {
2523-
if (__n > __old_size)
2524-
__annotate_increase(__n - __old_size);
2537+
if (__n > size())
2538+
__annotate_increase(__n - size());
25252539
}
25262540
value_type* __p = std::__to_address(__get_pointer());
25272541
traits_type::assign(__p, __n, __c);
@@ -2532,11 +2546,13 @@ template <class _CharT, class _Traits, class _Allocator>
25322546
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
25332547
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
25342548
pointer __p;
2549+
#ifndef _LIBCPP_HAS_NO_ASAN
25352550
size_type __old_size = size();
25362551
_LIBCPP_IF_ASAN() {
25372552
if (__old_size == 0)
25382553
__annotate_increase(1);
25392554
}
2555+
#endif
25402556
if (__is_long()) {
25412557
__p = __get_long_pointer();
25422558
__set_long_size(1);
@@ -2546,10 +2562,12 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
25462562
}
25472563
traits_type::assign(*__p, __c);
25482564
traits_type::assign(*++__p, value_type());
2565+
#ifndef _LIBCPP_HAS_NO_ASAN
25492566
_LIBCPP_IF_ASAN() {
25502567
if (__old_size > 1)
25512568
__annotate_shrink(__old_size);
25522569
}
2570+
#endif
25532571
return *this;
25542572
}
25552573

@@ -2560,16 +2578,20 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
25602578
__copy_assign_alloc(__str);
25612579
if (!__is_long()) {
25622580
if (!__str.__is_long()) {
2581+
#ifndef _LIBCPP_HAS_NO_ASAN
25632582
size_type __old_size = __get_short_size();
2583+
#endif
25642584
_LIBCPP_IF_ASAN() {
25652585
if (__get_short_size() < __str.__get_short_size())
25662586
__annotate_increase(__str.__get_short_size() - __get_short_size());
25672587
}
25682588
__r_.first() = __str.__r_.first();
2589+
#ifndef _LIBCPP_HAS_NO_ASAN
25692590
_LIBCPP_IF_ASAN() {
25702591
if (__old_size > __get_short_size())
25712592
__annotate_shrink(__old_size);
25722593
}
2594+
#endif
25732595
} else {
25742596
return __assign_no_alias<true>(__str.data(), __str.size());
25752597
}
@@ -2612,13 +2634,16 @@ basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, tr
26122634
}
26132635
# endif
26142636
}
2637+
#ifndef _LIBCPP_HAS_NO_ASAN
26152638
size_type __str_old_size = __str.size();
2639+
#endif
26162640
bool __str_was_short = !__str.__is_long();
26172641

26182642
__move_assign_alloc(__str);
26192643
__r_.first() = __str.__r_.first();
26202644
__str.__set_short_size(0);
26212645
traits_type::assign(__str.__get_short_pointer()[0], value_type());
2646+
#ifndef _LIBCPP_HAS_NO_ASAN
26222647
_LIBCPP_IF_ASAN() {
26232648
if (__str_was_short && this != &__str)
26242649
__str.__annotate_shrink(__str_old_size);
@@ -2638,6 +2663,7 @@ basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, tr
26382663
// If it is long string, delete was never called on original __str's buffer.
26392664
__annotate_new(__get_short_size());
26402665
}
2666+
#endif
26412667
}
26422668

26432669
#endif
@@ -2678,8 +2704,9 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
26782704
basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _Sentinel __last, size_type __n) {
26792705
_LIBCPP_ASSERT_INTERNAL(
26802706
__string_is_trivial_iterator<_Iterator>::value, "The iterator type given to `__assign_trivial` must be trivial");
2681-
2707+
#ifndef _LIBCPP_HAS_NO_ASAN
26822708
size_type __old_size = size();
2709+
#endif
26832710
size_type __cap = capacity();
26842711
if (__cap < __n) {
26852712
// Unlike `append` functions, if the input range points into the string itself, there is no case that the input
@@ -2692,18 +2719,20 @@ basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _
26922719
__grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
26932720
_LIBCPP_IF_ASAN() { __annotate_increase(__n); }
26942721
} else _LIBCPP_IF_ASAN() {
2695-
if (__n > __old_size)
2696-
__annotate_increase(__n - __old_size);
2722+
if (__n > size())
2723+
__annotate_increase(__n - size());
26972724
}
26982725
pointer __p = __get_pointer();
26992726
for (; __first != __last; ++__p, (void)++__first)
27002727
traits_type::assign(*__p, *__first);
27012728
traits_type::assign(*__p, value_type());
27022729
__set_size(__n);
2730+
#ifndef _LIBCPP_HAS_NO_ASAN
27032731
_LIBCPP_IF_ASAN() {
27042732
if (__n < __old_size)
27052733
__annotate_shrink(__old_size);
27062734
}
2735+
#endif
27072736
}
27082737

27092738
template <class _CharT, class _Traits, class _Allocator>
@@ -3208,15 +3237,19 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
32083237

32093238
template <class _CharT, class _Traits, class _Allocator>
32103239
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3240+
#ifndef _LIBCPP_HAS_NO_ASAN
32113241
size_type __old_size = size();
3242+
#endif
32123243
if (__is_long()) {
32133244
traits_type::assign(*__get_long_pointer(), value_type());
32143245
__set_long_size(0);
32153246
} else {
32163247
traits_type::assign(*__get_short_pointer(), value_type());
32173248
__set_short_size(0);
32183249
}
3250+
#ifndef _LIBCPP_HAS_NO_ASAN
32193251
_LIBCPP_IF_ASAN() { __annotate_shrink(__old_size); }
3252+
#endif
32203253
}
32213254

32223255
template <class _CharT, class _Traits, class _Allocator>

0 commit comments

Comments
 (0)