Skip to content

[clang][bytecode] Use bytecode interpreter in isPotentialConstantExprU… #149462

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 1 commit into from
Jul 19, 2025
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
5 changes: 5 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6670,6 +6670,11 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
}
// Function parameters.
if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
!D->getType()->isIntegralOrEnumerationType()) {
return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
/*InitializerFailed=*/false, E);
}
if (auto It = this->Params.find(PVD); It != this->Params.end()) {
if (IsReference || !It->second.IsPtr)
return this->emitGetParam(classifyPrim(E), It->second.Offset, E);
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/AST/ByteCode/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
return Func->isValid();
}

void Context::isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,
const FunctionDecl *FD) {
assert(Stk.empty());
++EvalID;
size_t StackSizeBefore = Stk.size();
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);

if (!C.interpretCall(FD, E)) {
C.cleanup();
Stk.clearTo(StackSizeBefore);
}
}

bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
++EvalID;
bool Recursing = !Stk.empty();
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/AST/ByteCode/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class Context final {
~Context();

/// Checks if a function is a potential constant expression.
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD);
void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,
const FunctionDecl *FD);

/// Evaluates a toplevel expression as an rvalue.
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/AST/ByteCode/EvalEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ EvaluationResult EvalEmitter::interpretAsPointer(const Expr *E,
return std::move(this->EvalResult);
}

bool EvalEmitter::interpretCall(const FunctionDecl *FD, const Expr *E) {
// Add parameters to the parameter map. The values in the ParamOffset don't
// matter in this case as reading from them can't ever work.
for (const ParmVarDecl *PD : FD->parameters()) {
this->Params.insert({PD, {0, false}});
}

if (!this->visit(E))
return false;
PrimType T = Ctx.classify(E).value_or(PT_Ptr);
return this->emitPop(T, E);
}

void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }

EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/EvalEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class EvalEmitter : public SourceMapper {
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);
/// Interpret the given Expr to a Pointer.
EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB);
/// Interpret the given expression as if it was in the body of the given
/// function, i.e. the parameters of the function are available for use.
bool interpretCall(const FunctionDecl *FD, const Expr *E);

/// Clean up all resources.
void cleanup();
Expand Down
9 changes: 8 additions & 1 deletion clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
return false;

if (isa<ParmVarDecl>(D)) {
if (D->getType()->isReferenceType())
if (D->getType()->isReferenceType()) {
if (S.inConstantContext() && S.getLangOpts().CPlusPlus &&
!S.getLangOpts().CPlusPlus11)
diagnoseNonConstVariable(S, OpPC, D);
return false;
}

const SourceInfo &Loc = S.Current->getSource(OpPC);
if (S.getLangOpts().CPlusPlus11) {
Expand Down Expand Up @@ -658,6 +662,9 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
if (Ptr.isInitialized())
return true;

if (Ptr.isExtern() && S.checkingPotentialConstantExpression())
return false;

if (const auto *VD = Ptr.getDeclDesc()->asVarDecl();
VD && (VD->isConstexpr() || VD->hasGlobalStorage())) {

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ bool Dup(InterpState &S, CodePtr OpPC) {

template <PrimType Name, class T = typename PrimConv<Name>::T>
bool Pop(InterpState &S, CodePtr OpPC) {
S.Stk.pop<T>();
S.Stk.discard<T>();
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18018,6 +18018,11 @@ bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
Info.InConstantContext = true;
Info.CheckingPotentialConstantExpression = true;

if (Info.EnableNewConstInterp) {
Info.Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Info, E, FD);
return Diags.empty();
}

// Fabricate a call stack frame to give the arguments a plausible cover story.
CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
/*CallExpr=*/nullptr, CallRef());
Expand Down
8 changes: 8 additions & 0 deletions clang/test/AST/ByteCode/builtin-constant-p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ void test17(void) {
F("string literal" + 1); // both-warning {{adding}} \
// both-note {{use array indexing}}
}

/// FIXME
static void foo(int i) __attribute__((__diagnose_if__(!__builtin_constant_p(i), "not constant", "error"))) // expected-note {{from}}
{
}
static void bar(int i) {
foo(15); // expected-error {{not constant}}
}
1 change: 1 addition & 0 deletions clang/test/Sema/diagnose_if.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 %s -verify -fno-builtin
// RUN: %clang_cc1 %s -verify -fno-builtin -fexperimental-new-constant-interpreter

#define _diagnose_if(...) __attribute__((diagnose_if(__VA_ARGS__)))

Expand Down
1 change: 1 addition & 0 deletions clang/test/SemaCXX/diagnose_if-ext.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -Wpedantic -fsyntax-only %s -verify
// RUN: %clang_cc1 -Wpedantic -fsyntax-only %s -verify -fexperimental-new-constant-interpreter

void foo() __attribute__((diagnose_if(1, "", "error"))); // expected-warning{{'diagnose_if' is a clang extension}}
void foo(int a) __attribute__((diagnose_if(a, "", "error"))); // expected-warning{{'diagnose_if' is a clang extension}}
Expand Down
1 change: 1 addition & 0 deletions clang/test/SemaCXX/diagnose_if.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 %s -verify -fno-builtin -std=c++14
// RUN: %clang_cc1 %s -verify -fno-builtin -std=c++14 -fexperimental-new-constant-interpreter

#define _diagnose_if(...) __attribute__((diagnose_if(__VA_ARGS__)))

Expand Down