[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:40 PDT 2024


================
@@ -177,5 +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);
----------------
c8ef wrote:

> Could you also add tests that calling it returns zero and doesn't evaluate its arguments:
> 
> ```c++
> 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);
> ```
> 
> This passes on MSVC (https://godbolt.org/z/nvxr3GxT4) but currently crashes Clang.

Done.

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


More information about the cfe-commits mailing list