Skip to content

Commit 8f1ffeb

Browse files
committed
[clang] Disable implicit conversions from pointer to bool!
1 parent 6512bf0 commit 8f1ffeb

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6902,7 +6902,7 @@ void InitializationSequence::InitializeFrom(Sema &S,
69026902
ImplicitConversionSequence ICS
69036903
= S.TryImplicitConversion(Initializer, DestType,
69046904
/*SuppressUserConversions*/true,
6905-
Sema::AllowedExplicit::None,
6905+
(Kind.getKind() == InitializationKind::IK_Copy) ? Sema::AllowedExplicit::None : Sema::AllowedExplicit::All,
69066906
/*InOverloadResolution*/ false,
69076907
/*CStyle=*/Kind.isCStyleOrFunctionalCast(),
69086908
allowObjCWritebackConversion);

clang/lib/Sema/SemaOverload.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType,
9393
bool InOverloadResolution,
9494
StandardConversionSequence &SCS,
9595
bool CStyle,
96-
bool AllowObjCWritebackConversion);
96+
bool AllowObjCWritebackConversion,
97+
bool AllowPointerToBoolConversion);
9798

9899
static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From,
99100
QualType &ToType,
@@ -1785,11 +1786,13 @@ TryImplicitConversion(Sema &S, Expr *From, QualType ToType,
17851786
bool AllowObjCConversionOnExplicit) {
17861787
ImplicitConversionSequence ICS;
17871788
if (IsStandardConversion(S, From, ToType, InOverloadResolution,
1788-
ICS.Standard, CStyle, AllowObjCWritebackConversion)){
1789+
ICS.Standard, CStyle, AllowObjCWritebackConversion,
1790+
/*AllowPointerToBoolConversion=*/(AllowExplicit != AllowedExplicit::None))){
17891791
ICS.setStandard();
17901792
return ICS;
17911793
}
17921794

1795+
17931796
if (!S.getLangOpts().CPlusPlus) {
17941797
ICS.setBad(BadConversionSequence::no_conversion, From, ToType);
17951798
return ICS;
@@ -2249,7 +2252,8 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType,
22492252
bool InOverloadResolution,
22502253
StandardConversionSequence &SCS,
22512254
bool CStyle,
2252-
bool AllowObjCWritebackConversion) {
2255+
bool AllowObjCWritebackConversion,
2256+
bool AllowPointerToBoolConversion) {
22532257
QualType FromType = From->getType();
22542258

22552259
// Standard conversions (C++ [conv])
@@ -2428,8 +2432,13 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType,
24282432
SCS.Second = ICK_Complex_Promotion;
24292433
FromType = ToType.getUnqualifiedType();
24302434
} else if (ToType->isBooleanType() &&
2431-
(FromType->isArithmeticType() ||
2432-
FromType->isAnyPointerType() ||
2435+
FromType->isArithmeticType()) {
2436+
// Boolean conversions (C++ 4.12).
2437+
SCS.Second = ICK_Boolean_Conversion;
2438+
FromType = S.Context.BoolTy;
2439+
} else if (AllowPointerToBoolConversion &&
2440+
ToType->isBooleanType() &&
2441+
(FromType->isAnyPointerType() ||
24332442
FromType->isBlockPointerType() ||
24342443
FromType->isMemberPointerType())) {
24352444
// Boolean conversions (C++ 4.12).
@@ -2621,7 +2630,8 @@ IsTransparentUnionStandardConversion(Sema &S, Expr* From,
26212630
// It's compatible if the expression matches any of the fields.
26222631
for (const auto *it : UD->fields()) {
26232632
if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS,
2624-
CStyle, /*AllowObjCWritebackConversion=*/false)) {
2633+
CStyle, /*AllowObjCWritebackConversion=*/false,
2634+
/*AllowPointerToBoolConversion=*/true)) {
26252635
ToType = it->getType();
26262636
return true;
26272637
}
@@ -3865,7 +3875,8 @@ static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType,
38653875
StandardConversionSequence InnerSCS;
38663876
if (!IsStandardConversion(S, From, ToAtomic->getValueType(),
38673877
InOverloadResolution, InnerSCS,
3868-
CStyle, /*AllowObjCWritebackConversion=*/false))
3878+
CStyle, /*AllowObjCWritebackConversion=*/false,
3879+
/*AllowPointerToBoolConversion=*/true))
38693880
return false;
38703881

38713882
SCS.Second = InnerSCS.Second;

0 commit comments

Comments
 (0)