Skip to content

Commit d4d959f

Browse files
Abseil Teamcopybara-github
authored andcommitted
Avoid relying on true and false tokens in the preprocessor macros used in absl/container
Some third-party libraries #define true and false to other tokens (such as 1 and 0), breaking the macros. This is a fix-forward for commit 45acc96 PiperOrigin-RevId: 737448596 Change-Id: Id5c0ce6295b4c29e6ccdc74fd251c0c0bd12f748
1 parent b3b568d commit d4d959f

File tree

3 files changed

+48
-55
lines changed

3 files changed

+48
-55
lines changed

absl/container/internal/btree_container.h

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -490,28 +490,28 @@ class btree_map_container : public btree_set_container<Tree> {
490490

491491
// TODO(b/402804213): Remove these macros whenever we have a better mechanism
492492
// available to handle lifetime analysis.
493-
#define ABSL_INTERNAL_X(Func, Callee, KQual, MQual, KValue, MValue, ...) \
494-
template < \
495-
typename K = key_type, class M, \
496-
ABSL_INTERNAL_IF_NOR( \
497-
KValue, MValue, \
498-
int = (EnableIf<LifetimeBoundKV<K, KValue, M, MValue, \
499-
IfRRef<int KQual>::AddPtr<K>, \
500-
IfRRef<int MQual>::AddPtr<M>>>()), \
501-
ABSL_INTERNAL_SINGLE_ARG( \
502-
int &..., \
503-
decltype(EnableIf<LifetimeBoundKV<K, KValue, M, MValue>>()) = \
504-
0))> \
505-
decltype(auto) Func(__VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF( \
506-
KValue, ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)), \
507-
M MQual obj ABSL_INTERNAL_IF( \
508-
MValue, ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))) \
509-
ABSL_ATTRIBUTE_LIFETIME_BOUND { \
510-
return ABSL_INTERNAL_IF_OR(KValue, MValue, (this->template Func<K, M, 0>), \
511-
Callee)( \
512-
__VA_ARGS__ std::forward<decltype(k)>(k), \
513-
std::forward<decltype(obj)>(obj)); \
514-
} \
493+
#define ABSL_INTERNAL_X(Func, Callee, KQual, MQual, KValue, MValue, ...) \
494+
template < \
495+
typename K = key_type, class M, \
496+
ABSL_INTERNAL_IF_##KValue##_NOR_##MValue( \
497+
int = (EnableIf<LifetimeBoundKV<K, KValue, M, MValue, \
498+
IfRRef<int KQual>::AddPtr<K>, \
499+
IfRRef<int MQual>::AddPtr<M>>>()), \
500+
ABSL_INTERNAL_SINGLE_ARG( \
501+
int &..., \
502+
decltype(EnableIf<LifetimeBoundKV<K, KValue, M, MValue>>()) = \
503+
0))> \
504+
decltype(auto) Func( \
505+
__VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF_##KValue( \
506+
ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)), \
507+
M MQual obj ABSL_INTERNAL_IF_##MValue( \
508+
ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))) \
509+
ABSL_ATTRIBUTE_LIFETIME_BOUND { \
510+
return ABSL_INTERNAL_IF_##KValue##_OR_##MValue( \
511+
(this->template Func<K, M, 0>), Callee)( \
512+
__VA_ARGS__ std::forward<decltype(k)>(k), \
513+
std::forward<decltype(obj)>(obj)); \
514+
} \
515515
friend struct std::enable_if<false> /* just to force a semicolon */
516516
// Insertion routines.
517517
// Note: the nullptr template arguments and extra `const M&` overloads allow
@@ -591,21 +591,23 @@ class btree_map_container : public btree_set_container<Tree> {
591591
#undef ABSL_INTERNAL_X
592592

593593
#define ABSL_INTERNAL_X(Func, Callee, KQual, KValue, ...) \
594-
template <class K = key_type, \
595-
ABSL_INTERNAL_IF(KValue, class... Args, \
596-
int = EnableIf<LifetimeBoundK< \
597-
K, KValue, IfRRef<int KQual>::AddPtr<K>>>()), \
598-
ABSL_INTERNAL_IF( \
599-
KValue, \
600-
decltype(EnableIf<LifetimeBoundK< \
601-
K, KValue, IfRRef<int KQual>::AddPtr<K>>>()) = 0, \
602-
class... Args), \
603-
std::enable_if_t<!std::is_convertible<K, const_iterator>::value, \
604-
int> = 0> \
605-
decltype(auto) Func(__VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF( \
606-
KValue, ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)), \
607-
Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND { \
608-
return ABSL_INTERNAL_IF(KValue, (this->template Func<K, 0>), Callee)( \
594+
template < \
595+
class K = key_type, \
596+
ABSL_INTERNAL_IF_##KValue( \
597+
class... Args, \
598+
int = (EnableIf< \
599+
LifetimeBoundK<K, KValue, IfRRef<int KQual>::AddPtr<K>>>())), \
600+
ABSL_INTERNAL_IF_##KValue( \
601+
decltype(EnableIf<LifetimeBoundK< \
602+
K, KValue, IfRRef<int KQual>::AddPtr<K>>>()) = 0, \
603+
class... Args), \
604+
std::enable_if_t<!std::is_convertible<K, const_iterator>::value, int> = \
605+
0> \
606+
decltype(auto) Func( \
607+
__VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF_##KValue( \
608+
ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)), \
609+
Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND { \
610+
return ABSL_INTERNAL_IF_##KValue((this->template Func<K, 0>), Callee)( \
609611
__VA_ARGS__ std::forward<decltype(k)>(k), \
610612
std::forward<decltype(args)>(args)...); \
611613
} \

absl/container/internal/common.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,21 @@
2626

2727
#define ABSL_INTERNAL_IF_true(if_satisfied, ...) if_satisfied
2828
#define ABSL_INTERNAL_IF_false(if_satisfied, ...) __VA_ARGS__
29-
#define ABSL_INTERNAL_IF(cond1, if_satisfied, ...) \
30-
ABSL_INTERNAL_IF_##cond1(if_satisfied, __VA_ARGS__)
3129

3230
#define ABSL_INTERNAL_IF_true_AND_true ABSL_INTERNAL_IF_true
3331
#define ABSL_INTERNAL_IF_false_AND_false ABSL_INTERNAL_IF_false
3432
#define ABSL_INTERNAL_IF_true_AND_false ABSL_INTERNAL_IF_false_AND_false
3533
#define ABSL_INTERNAL_IF_false_AND_true ABSL_INTERNAL_IF_false_AND_false
36-
#define ABSL_INTERNAL_IF_AND(cond1, cond2, if_satisfied, ...) \
37-
ABSL_INTERNAL_IF_##cond1##_AND_##cond2(if_satisfied, __VA_ARGS__)
3834

3935
#define ABSL_INTERNAL_IF_true_OR_true ABSL_INTERNAL_IF_true
4036
#define ABSL_INTERNAL_IF_false_OR_false ABSL_INTERNAL_IF_false
4137
#define ABSL_INTERNAL_IF_true_OR_false ABSL_INTERNAL_IF_true_OR_true
4238
#define ABSL_INTERNAL_IF_false_OR_true ABSL_INTERNAL_IF_true_OR_true
43-
#define ABSL_INTERNAL_IF_OR(cond1, cond2, if_satisfied, ...) \
44-
ABSL_INTERNAL_IF_##cond1##_OR_##cond2(if_satisfied, __VA_ARGS__)
4539

4640
#define ABSL_INTERNAL_IF_true_NOR_true ABSL_INTERNAL_IF_false_AND_false
4741
#define ABSL_INTERNAL_IF_false_NOR_false ABSL_INTERNAL_IF_true_AND_true
4842
#define ABSL_INTERNAL_IF_true_NOR_false ABSL_INTERNAL_IF_false_AND_true
4943
#define ABSL_INTERNAL_IF_false_NOR_true ABSL_INTERNAL_IF_true_AND_false
50-
#define ABSL_INTERNAL_IF_NOR(cond1, cond2, if_satisfied, ...) \
51-
ABSL_INTERNAL_IF_##cond1##_NOR_##cond2(if_satisfied, __VA_ARGS__)
5244

5345
#define ABSL_INTERNAL_COMMA ,
5446

absl/container/internal/raw_hash_map.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,22 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
104104
#define ABSL_INTERNAL_X(Func, Callee, KQual, VQual, KValue, VValue, Tail, ...) \
105105
template < \
106106
typename K = key_type, class V = mapped_type, \
107-
ABSL_INTERNAL_IF_NOR( \
108-
KValue, VValue, \
107+
ABSL_INTERNAL_IF_##KValue##_NOR_##VValue( \
109108
int = (EnableIf<LifetimeBoundKV<K, KValue, V, VValue, \
110109
IfRRef<int KQual>::AddPtr<K>, \
111110
IfRRef<int VQual>::AddPtr<V>>>()), \
112111
ABSL_INTERNAL_SINGLE_ARG( \
113112
int &..., \
114113
decltype(EnableIf<LifetimeBoundKV<K, KValue, V, VValue>>()) = \
115114
0))> \
116-
decltype(auto) Func(__VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF( \
117-
KValue, ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)), \
118-
V VQual v ABSL_INTERNAL_IF( \
119-
VValue, ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))) \
120-
ABSL_ATTRIBUTE_LIFETIME_BOUND { \
121-
return ABSL_INTERNAL_IF_OR(KValue, VValue, (this->template Func<K, V, 0>), \
122-
Callee)(std::forward<decltype(k)>(k), \
123-
std::forward<decltype(v)>(v)) Tail; \
115+
decltype(auto) Func( \
116+
__VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF_##KValue( \
117+
ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)), \
118+
V VQual v ABSL_INTERNAL_IF_##VValue(ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY( \
119+
this))) ABSL_ATTRIBUTE_LIFETIME_BOUND { \
120+
return ABSL_INTERNAL_IF_##KValue##_OR_##VValue( \
121+
(this->template Func<K, V, 0>), Callee)( \
122+
std::forward<decltype(k)>(k), std::forward<decltype(v)>(v)) Tail; \
124123
} \
125124
static_assert(true, "This is to force a semicolon.")
126125

0 commit comments

Comments
 (0)