Skip to content

Commit 64216ba

Browse files
authored
[Sema] -Wpointer-bool-conversion: suppress lambda function pointer conversion diagnostic during instantiation (#83497)
I have seen two internal pieces of code that uses a template type parameter to accept any callable type (function pointer, std::function, closure type, etc). The diagnostic added in #83152 would require adaptation to the template, which is difficult and also seems unnecessary. Example: ```cpp template <typename... Ts> static bool IsFalse(const Ts&...) { return false; } template <typename T, typename... Ts, typename = typename std::enable_if<std::is_constructible<bool, const T&>::value>::type> static bool IsFalse(const T& p, const Ts&...) { return p ? false : true; } template <typename... Args> void Init(Args&&... args) { if (IsFalse(absl::implicit_cast<const typename std::decay<Args>::type&>( args)...)) { // A callable object convertible to false is either a null pointer or a // null functor (e.g., a default-constructed std::function). empty_ = true; } else { empty_ = false; new (&factory_) Factory(std::forward<Args>(args)...); } } ```
1 parent 18d0d9b commit 64216ba

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16588,13 +16588,16 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
1658816588
}
1658916589

1659016590
// Complain if we are converting a lambda expression to a boolean value
16591-
if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
16592-
if (const auto *MRecordDecl = MCallExpr->getRecordDecl();
16593-
MRecordDecl && MRecordDecl->isLambda()) {
16594-
Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool)
16595-
<< /*LambdaPointerConversionOperatorType=*/3
16596-
<< MRecordDecl->getSourceRange() << Range << IsEqual;
16597-
return;
16591+
// outside of instantiation.
16592+
if (!inTemplateInstantiation()) {
16593+
if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
16594+
if (const auto *MRecordDecl = MCallExpr->getRecordDecl();
16595+
MRecordDecl && MRecordDecl->isLambda()) {
16596+
Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool)
16597+
<< /*LambdaPointerConversionOperatorType=*/3
16598+
<< MRecordDecl->getSourceRange() << Range << IsEqual;
16599+
return;
16600+
}
1659816601
}
1659916602
}
1660016603

clang/test/SemaCXX/warn-bool-conversion.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ void foo() {
9292
bool is_true = [](){ return true; };
9393
// expected-warning@-1{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
9494
}
95+
96+
template <typename... Ts>
97+
static bool IsFalse(const Ts&...) { return false; }
98+
template <typename T>
99+
static bool IsFalse(const T& p) {
100+
bool b;
101+
b = f7; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
102+
// Intentionally not warned on because p could be a lambda type in one
103+
// instantiation, but a pointer type in another.
104+
return p ? false : true;
105+
}
106+
107+
bool use_instantiation() {
108+
return IsFalse([]() { return 0; });
109+
}
95110
#endif
96111

97112
void bar() {

0 commit comments

Comments
 (0)