From eaea4d15acd4cab92e6287d692d2652066c3368a Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 2 Sep 2024 17:15:44 +0800 Subject: [PATCH] [clang] The ms-extension __noop should return zero in a constexpr context. (#106849) Fixes #106713. --- clang/lib/AST/ExprConstant.cpp | 4 ++-- clang/test/SemaCXX/builtins.cpp | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index b5dfd4dd32b63c..3dc13c14c00343 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 and returns 0. + return Success(0, E); 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..f47ed3a1f7ebfc 100644 --- a/clang/test/SemaCXX/builtins.cpp +++ b/clang/test/SemaCXX/builtins.cpp @@ -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); #endif