[clang] 3f98cdc - [Clang] Always constant-evaluate operands of comparisons to nullptr

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 24 07:11:16 PDT 2023


Author: Corentin Jabot
Date: 2023-08-24T16:11:10+02:00
New Revision: 3f98cdc815bddd2b69e3298fa104c0305503292e

URL: https://github.com/llvm/llvm-project/commit/3f98cdc815bddd2b69e3298fa104c0305503292e
DIFF: https://github.com/llvm/llvm-project/commit/3f98cdc815bddd2b69e3298fa104c0305503292e.diff

LOG: [Clang]  Always constant-evaluate operands of comparisons to nullptr

even if we know what the result is going to be.
There may be side effects we ought not to ignore,

Fixes #64923

Reviewed By: ChuanqiXu

Differential Revision: https://reviews.llvm.org/D158601

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/ExprConstant.cpp
    clang/test/SemaCXX/compare-cxx2a.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 42b4afa455e38a..0f6ad2e70c7d5f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -198,6 +198,11 @@ Bug Fixes to C++ Support
 - Update ``FunctionDeclBitfields.NumFunctionDeclBits``. This fixes:
   (`#64171 <https://github.com/llvm/llvm-project/issues/64171>`_).
 
+- Expressions producing ``nullptr`` are correctly evaluated
+  by the constant interpreter when appearing as the operand
+  of a binary comparision.
+  (`#64923 <https://github.com/llvm/llvm-project/issues/64923>_``)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.

diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 9f4c758b81303c..51fdef708dde5a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13310,6 +13310,10 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
     // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
     // are compared, the result is true of the operator is <=, >= or ==, and
     // false otherwise.
+    LValue Res;
+    if (!EvaluatePointer(E->getLHS(), Res, Info) ||
+        !EvaluatePointer(E->getRHS(), Res, Info))
+      return false;
     return Success(CmpResult::Equal, E);
   }
 

diff  --git a/clang/test/SemaCXX/compare-cxx2a.cpp b/clang/test/SemaCXX/compare-cxx2a.cpp
index 0cb48bcfcec27a..619e16aa745817 100644
--- a/clang/test/SemaCXX/compare-cxx2a.cpp
+++ b/clang/test/SemaCXX/compare-cxx2a.cpp
@@ -461,3 +461,21 @@ namespace PR52537 {
   template<typename T> bool f6() { return 0 < y6; } // expected-note {{instantiation of}}
   void g6() { f6<int>(); } // expected-note {{instantiation of}}
 }
+
+
+namespace GH64923 {
+using nullptr_t = decltype(nullptr);
+struct MyTask{};
+constexpr MyTask DoAnotherThing() {
+    return {};
+}
+
+constexpr nullptr_t operator++(MyTask &&T); // expected-note 2{{declared here}}
+void DoSomething() {
+  if constexpr (++DoAnotherThing() != nullptr) {} // expected-error {{constexpr if condition is not a constant expression}} \
+                                                  // expected-note  {{undefined function 'operator++' cannot be used in a constant expression}}
+
+  if constexpr (nullptr == ++DoAnotherThing()) {} // expected-error {{constexpr if condition is not a constant expression}} \
+                                                  // expected-note  {{undefined function 'operator++' cannot be used in a constant expression}}
+}
+}


        


More information about the cfe-commits mailing list