Skip to content

[clang][ExprConst] Fix second arg of __builtin_{clzg,ctzg} not always being evaluated #86742

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
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
30 changes: 20 additions & 10 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12361,12 +12361,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;

std::optional<APSInt> Fallback;
if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) {
APSInt FallbackTemp;
if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info))
return false;
Fallback = FallbackTemp;
}

if (!Val) {
if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) {
if (!EvaluateInteger(E->getArg(1), Val, Info))
return false;
return Success(Val, E);
}
if (Fallback)
return Success(*Fallback, E);

// When the argument is 0, the result of GCC builtins is undefined,
// whereas for Microsoft intrinsics, the result is the bit-width of the
Expand Down Expand Up @@ -12425,12 +12430,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;

std::optional<APSInt> Fallback;
if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) {
APSInt FallbackTemp;
if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info))
return false;
Fallback = FallbackTemp;
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess using a bool HasFallback instead of std::optional would be more efficient but defaulting to std::optional feels like better practice.

}

if (!Val) {
if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) {
if (!EvaluateInteger(E->getArg(1), Val, Info))
return false;
return Success(Val, E);
}
if (Fallback)
return Success(*Fallback, E);

return Error(E);
}
Expand Down
34 changes: 34 additions & 0 deletions clang/test/Sema/constant-builtins-all-args-evaluated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// expected-no-diagnostics

constexpr int test_clzg_0() {
int x = 0;
(void)__builtin_clzg(0U, ++x);
return x;
}

static_assert(test_clzg_0() == 1);

constexpr int test_clzg_1() {
int x = 0;
(void)__builtin_clzg(1U, ++x);
return x;
}

static_assert(test_clzg_1() == 1);

constexpr int test_ctzg_0() {
int x = 0;
(void)__builtin_ctzg(0U, ++x);
return x;
}

static_assert(test_ctzg_0() == 1);

constexpr int test_ctzg_1() {
int x = 0;
(void)__builtin_ctzg(1U, ++x);
return x;
}

static_assert(test_ctzg_1() == 1);