Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 17 additions & 1 deletion clang/test/SemaCXX/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add tests that calling it returns zero and doesn't evaluate its arguments:

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.

#endif
Loading