-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][bytecode] Save Constexpr bit in Function #142793
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rename isConstexpr to isValid, the former was always a bad name. Save a constexpr bit in Function so we don't have to access the decl in CheckCallable.
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesRename isConstexpr to isValid, the former was always a bad name. Save a constexpr bit in Function so we don't have to access the decl in CheckCallable. Full diff: https://github.com/llvm/llvm-project/pull/142793.diff 4 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index d73705b6126fe..971eb7fd58876 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -49,7 +49,7 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
if (!Run(Parent, Func))
return false;
- return Func->isConstexpr();
+ return Func->isValid();
}
bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
diff --git a/clang/lib/AST/ByteCode/Function.cpp b/clang/lib/AST/ByteCode/Function.cpp
index e421dad062817..9eb6744ea47ad 100644
--- a/clang/lib/AST/ByteCode/Function.cpp
+++ b/clang/lib/AST/ByteCode/Function.cpp
@@ -27,6 +27,7 @@ Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
if (const auto *F = dyn_cast<const FunctionDecl *>(Source)) {
Variadic = F->isVariadic();
Immediate = F->isImmediateFunction();
+ Constexpr = F->isConstexpr() || F->hasAttr<MSConstexprAttr>();
if (const auto *CD = dyn_cast<CXXConstructorDecl>(F)) {
Virtual = CD->isVirtual();
Kind = FunctionKind::Ctor;
@@ -48,6 +49,7 @@ Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
Variadic = false;
Virtual = false;
Immediate = false;
+ Constexpr = false;
}
}
diff --git a/clang/lib/AST/ByteCode/Function.h b/clang/lib/AST/ByteCode/Function.h
index 8b577858474af..de88f3ded34dc 100644
--- a/clang/lib/AST/ByteCode/Function.h
+++ b/clang/lib/AST/ByteCode/Function.h
@@ -150,12 +150,13 @@ class Function final {
/// Returns the source information at a given PC.
SourceInfo getSource(CodePtr PC) const;
- /// Checks if the function is valid to call in constexpr.
- bool isConstexpr() const { return IsValid || isLambdaStaticInvoker(); }
+ /// Checks if the function is valid to call.
+ bool isValid() const { return IsValid || isLambdaStaticInvoker(); }
/// Checks if the function is virtual.
bool isVirtual() const { return Virtual; };
bool isImmediate() const { return Immediate; }
+ bool isConstexpr() const { return Constexpr; }
/// Checks if the function is a constructor.
bool isConstructor() const { return Kind == FunctionKind::Ctor; }
@@ -303,6 +304,8 @@ class Function final {
unsigned Virtual : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned Immediate : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned Constexpr : 1;
public:
/// Dumps the disassembled bytecode to \c llvm::errs().
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index db91e5e328485..5c8abffb3a99d 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -857,23 +857,22 @@ bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
return false;
}
- // Bail out if the function declaration itself is invalid. We will
- // have produced a relevant diagnostic while parsing it, so just
- // note the problematic sub-expression.
- if (F->getDecl()->isInvalidDecl())
- return Invalid(S, OpPC);
-
if (S.checkingPotentialConstantExpression() && S.Current->getDepth() != 0)
return false;
- if (F->isConstexpr() && F->hasBody() &&
- (F->getDecl()->isConstexpr() || F->getDecl()->hasAttr<MSConstexprAttr>()))
+ if (F->isValid() && F->hasBody() && F->isConstexpr())
return true;
// Implicitly constexpr.
if (F->isLambdaStaticInvoker())
return true;
+ // Bail out if the function declaration itself is invalid. We will
+ // have produced a relevant diagnostic while parsing it, so just
+ // note the problematic sub-expression.
+ if (F->getDecl()->isInvalidDecl())
+ return Invalid(S, OpPC);
+
// Diagnose failed assertions specially.
if (S.Current->getLocation(OpPC).isMacroID() &&
F->getDecl()->getIdentifier()) {
@@ -923,7 +922,8 @@ bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
// for a constant expression. It might be defined at the point we're
// actually calling it.
bool IsExtern = DiagDecl->getStorageClass() == SC_Extern;
- if (!DiagDecl->isDefined() && !IsExtern && DiagDecl->isConstexpr() &&
+ bool IsDefined = F->isDefined();
+ if (!IsDefined && !IsExtern && DiagDecl->isConstexpr() &&
S.checkingPotentialConstantExpression())
return false;
|
rorth
pushed a commit
to rorth/llvm-project
that referenced
this pull request
Jun 11, 2025
Rename isConstexpr to isValid, the former was always a bad name. Save a constexpr bit in Function so we don't have to access the decl in CheckCallable.
DhruvSrivastavaX
pushed a commit
to DhruvSrivastavaX/lldb-for-aix
that referenced
this pull request
Jun 12, 2025
Rename isConstexpr to isValid, the former was always a bad name. Save a constexpr bit in Function so we don't have to access the decl in CheckCallable.
mysterymath
added a commit
to mysterymath/llvm-project
that referenced
this pull request
Jun 20, 2025
This reverts commit 478bdd8.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:bytecode
Issues for the clang bytecode constexpr interpreter
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rename isConstexpr to isValid, the former was always a bad name. Save a constexpr bit in Function so we don't have to access the decl in CheckCallable.