[clang] [clang][bytecode] Fix three-way unordered non-pointer comparisions (PR #127759)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 23:56:23 PST 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/127759

This _can_ happen with non-pointers, but we shouldn't diagnose it in that case.

>From 1b4f4b78741e297e8d257af427206f3dbc3501d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 19 Feb 2025 08:52:35 +0100
Subject: [PATCH] [clang][bytecode] Fix three-way unordered non-pointer
 comparisions

This _can_ happen with non-pointers, but we shouldn't diagnose it in
that case.
---
 clang/lib/AST/ByteCode/Interp.h   | 15 ++++++++-------
 clang/test/AST/ByteCode/cxx20.cpp |  2 ++
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index ca74046038072..fa113aa0bb157 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1132,13 +1132,14 @@ bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo) {
   const Pointer &P = S.Stk.peek<Pointer>();
 
   ComparisonCategoryResult CmpResult = LHS.compare(RHS);
-  if (CmpResult == ComparisonCategoryResult::Unordered) {
-    // This should only happen with pointers.
-    const SourceInfo &Loc = S.Current->getSource(OpPC);
-    S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
-        << LHS.toDiagnosticString(S.getASTContext())
-        << RHS.toDiagnosticString(S.getASTContext());
-    return false;
+  if constexpr (std::is_same_v<T, Pointer>) {
+    if (CmpResult == ComparisonCategoryResult::Unordered) {
+      const SourceInfo &Loc = S.Current->getSource(OpPC);
+      S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
+          << LHS.toDiagnosticString(S.getASTContext())
+          << RHS.toDiagnosticString(S.getASTContext());
+      return false;
+    }
   }
 
   assert(CmpInfo);
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index 6f65fa5c7cfd3..06501de64916a 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -626,6 +626,8 @@ namespace ThreeWayCmp {
   constexpr int k = (1 <=> 1, 0); // both-warning {{comparison result unused}}
   static_assert(k== 0, "");
 
+  static_assert(__builtin_nanf("") <=> __builtin_nanf("") == -127, "");
+
   /// Pointers.
   constexpr int a[] = {1,2,3};
   constexpr int b[] = {1,2,3};



More information about the cfe-commits mailing list