llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Vinayak Dev (vinayakdsci)

<details>
<summary>Changes</summary>

Fixes #<!-- -->82512 

Adds diagnostics for lambda expressions being cast to boolean values, which 
results in the expression always evaluating to true.
Earlier, Clang allowed compilation of such erroneous programs, but now emits a 
warning through `-Wpointer-bool-conversion`.

---
Full diff: https://github.com/llvm/llvm-project/pull/83152.diff


1 Files Affected:

- (modified) clang/lib/Sema/SemaChecking.cpp (+21) 


``````````diff
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 0de76ee119cf81..3b03036587baeb 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -16538,6 +16538,27 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
     }
   }
 
+  // Complain if we are converting a lambda expression to a boolean value
+  if (auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
+    if (MCallExpr->getObjectType()->isRecordType()) {
+      if (auto *MRecordDecl = MCallExpr->getRecordDecl()) {
+        if (MRecordDecl->isLambda()) {
+          std::string Str;
+          llvm::raw_string_ostream S(Str);
+          const unsigned DiagID = diag::warn_impcast_pointer_to_bool;
+          // For lambdas, the pointer type is function, which corresponds to 1.
+          const unsigned FunctionPointerType = 1;
+          // Pretty print the diagnostic for the warning
+          E->printPretty(S, nullptr, getPrintingPolicy());
+          Diag(E->getExprLoc(), DiagID)
+              << FunctionPointerType << S.str() << E->getSourceRange() << Range
+              << IsEqual;
+          return;
+        }
+      }
+    }
+  }
+
   // Expect to find a single Decl.  Skip anything more complicated.
   ValueDecl *D = nullptr;
   if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/83152
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to