[clang] [clang] The ms-extension __noop should return zero in a constexpr context. (PR #106849)

via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 31 17:14:04 PDT 2024


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/106849

>From 6ce4604725d36afaea17cf533d422a978c4389ff Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 30 Aug 2024 15:34:34 +0000
Subject: [PATCH 1/5] fix cast

---
 clang/lib/AST/ExprConstant.cpp |  2 +-
 clang/test/AST/atomic-expr.c   | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/AST/atomic-expr.c

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e8a4d1d3c74102..0571771e2a7e7d 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14458,7 +14458,6 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_IntegralComplexToFloatingComplex:
   case CK_BuiltinFnToFnPtr:
   case CK_ZeroToOCLOpaqueType:
-  case CK_NonAtomicToAtomic:
   case CK_AddressSpaceConversion:
   case CK_IntToOCLSampler:
   case CK_FloatingToFixedPoint:
@@ -14482,6 +14481,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_UserDefinedConversion:
   case CK_LValueToRValue:
   case CK_AtomicToNonAtomic:
+  case CK_NonAtomicToAtomic:
   case CK_NoOp:
   case CK_LValueToRValueBitCast:
   case CK_HLSLArrayRValue:
diff --git a/clang/test/AST/atomic-expr.c b/clang/test/AST/atomic-expr.c
new file mode 100644
index 00000000000000..0826a6491e8a6a
--- /dev/null
+++ b/clang/test/AST/atomic-expr.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+// expected-no-diagnostics
+
+typedef _Atomic char atomic_char;
+
+atomic_char counter;
+
+char load_plus_one() {
+  return ({ counter; }) + 1;
+}
\ No newline at end of file

>From f0957df83597a2c43a8510a83619ef67c918c7d6 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 31 Aug 2024 21:19:46 +0800
Subject: [PATCH 2/5] __noop should return 0

---
 clang/lib/AST/ExprConstant.cpp  | 4 ++--
 clang/test/SemaCXX/builtins.cpp | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 0571771e2a7e7d..69d2707aed9171 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12720,8 +12720,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
   }
 
   case Builtin::BI__noop:
-  // __noop always evaluates successfully
-    return true;
+    // __noop always evaluates successfully
+    return false;
 
   case Builtin::BI__builtin_is_constant_evaluated: {
     const auto *Callee = Info.CurrentCall->getCallee();
diff --git a/clang/test/SemaCXX/builtins.cpp b/clang/test/SemaCXX/builtins.cpp
index c6fbb8b514d671..78344c45092a79 100644
--- a/clang/test/SemaCXX/builtins.cpp
+++ b/clang/test/SemaCXX/builtins.cpp
@@ -178,4 +178,5 @@ static void __builtin_cpu_init(); // expected-error {{static declaration of '__b
 
 #ifdef _MSC_VER
 constexpr int x = []{ __noop; return 0; }(); // expected-no-diagnostics
+static_assert([] { return __noop; }() == 0);
 #endif

>From a53cbbb4f3aaf65d0ebb49602f76d7f648e80410 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 31 Aug 2024 21:22:47 +0800
Subject: [PATCH 3/5] Revert "fix cast"

This reverts commit 6ce4604725d36afaea17cf533d422a978c4389ff.
---
 clang/lib/AST/ExprConstant.cpp |  2 +-
 clang/test/AST/atomic-expr.c   | 10 ----------
 2 files changed, 1 insertion(+), 11 deletions(-)
 delete mode 100644 clang/test/AST/atomic-expr.c

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 69d2707aed9171..57040043f74193 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14458,6 +14458,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_IntegralComplexToFloatingComplex:
   case CK_BuiltinFnToFnPtr:
   case CK_ZeroToOCLOpaqueType:
+  case CK_NonAtomicToAtomic:
   case CK_AddressSpaceConversion:
   case CK_IntToOCLSampler:
   case CK_FloatingToFixedPoint:
@@ -14481,7 +14482,6 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_UserDefinedConversion:
   case CK_LValueToRValue:
   case CK_AtomicToNonAtomic:
-  case CK_NonAtomicToAtomic:
   case CK_NoOp:
   case CK_LValueToRValueBitCast:
   case CK_HLSLArrayRValue:
diff --git a/clang/test/AST/atomic-expr.c b/clang/test/AST/atomic-expr.c
deleted file mode 100644
index 0826a6491e8a6a..00000000000000
--- a/clang/test/AST/atomic-expr.c
+++ /dev/null
@@ -1,10 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only %s -verify
-// expected-no-diagnostics
-
-typedef _Atomic char atomic_char;
-
-atomic_char counter;
-
-char load_plus_one() {
-  return ({ counter; }) + 1;
-}
\ No newline at end of file

>From a3727d3ab517a78b85792378b473f3d531e8532c Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 31 Aug 2024 22:11:24 +0800
Subject: [PATCH 4/5] __noop should return 0

---
 clang/lib/AST/ExprConstant.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 57040043f74193..55b51c2d25f7f6 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12721,7 +12721,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
 
   case Builtin::BI__noop:
     // __noop always evaluates successfully
-    return false;
+    return ZeroInitialization(E);
 
   case Builtin::BI__builtin_is_constant_evaluated: {
     const auto *Callee = Info.CurrentCall->getCallee();

>From cc6f2087a4d4f3e556d6411cf839648571571b18 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 1 Sep 2024 08:13:47 +0800
Subject: [PATCH 5/5] add more test

---
 clang/test/SemaCXX/builtins.cpp | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/builtins.cpp b/clang/test/SemaCXX/builtins.cpp
index 78344c45092a79..f47ed3a1f7ebfc 100644
--- a/clang/test/SemaCXX/builtins.cpp
+++ b/clang/test/SemaCXX/builtins.cpp
@@ -177,6 +177,21 @@ static void __builtin_cpu_init(); // expected-error {{static declaration of '__b
 #endif
 
 #ifdef _MSC_VER
-constexpr int x = []{ __noop; return 0; }(); // expected-no-diagnostics
+constexpr int x = [] {
+  __noop;
+  return 0;
+}(); // expected-no-diagnostics
 static_assert([] { return __noop; }() == 0);
+static_assert([] { return __noop(4); }() == 0);
+extern int not_accessed;
+void not_called();
+static_assert([] { return __noop(not_accessed *= 6); }() == 0);
+static_assert([] { return __noop(not_called()); }() == 0);
+static_assert([] { return __noop(throw ""); }() == 0);
+static_assert([] { return __noop(throw "", throw ""); }() == 0);
+static_assert([] {
+  int a = 5;
+  __noop(++a);
+  return a;
+}() == 5);
 #endif



More information about the cfe-commits mailing list