Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tbaederr wants to merge1 commit intollvm:main
base:main
Choose a base branch
Loading
fromtbaederr:diagnose-if

Conversation

tbaederr
Copy link
Contributor

…nevaluated

Fake a function call to the given function and evaluate the given expression as if it was part of that function call.

Fixes#149383

@tbaederrtbaederr changed the title[clang][bytecode] Use bytecode interprete in isPotentialConstantExprU…[clang][bytecode] Use bytecode interpreter in isPotentialConstantExprU…Jul 18, 2025
@llvmbotllvmbot added clangClang issues not falling into any other category clang:frontendLanguage frontend issues, e.g. anything involving "Sema" clang:bytecodeIssues for the clang bytecode constexpr interpreter labelsJul 18, 2025
@llvmbot
Copy link
Member

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

…nevaluated

Fake a function call to the given function and evaluate the given expression as if it was part of that function call.

Fixes #149383


Full diff:https://github.com/llvm/llvm-project/pull/149462.diff

8 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Context.cpp (+14)
  • (modified) clang/lib/AST/ByteCode/Context.h (+3-1)
  • (modified) clang/lib/AST/ByteCode/EvalEmitter.cpp (+13)
  • (modified) clang/lib/AST/ByteCode/EvalEmitter.h (+3)
  • (modified) clang/lib/AST/ExprConstant.cpp (+5)
  • (modified) clang/test/Sema/diagnose_if.c (+1)
  • (modified) clang/test/SemaCXX/diagnose_if-ext.cpp (+1)
  • (modified) clang/test/SemaCXX/diagnose_if.cpp (+1)
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cppindex a629ff9569428..d8a3368c9d885 100644--- a/clang/lib/AST/ByteCode/Context.cpp+++ b/clang/lib/AST/ByteCode/Context.cpp@@ -52,6 +52,20 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {   return Func->isValid(); }+bool 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);+  }+  return true;+}+ bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {   ++EvalID;   bool Recursing = !Stk.empty();diff --git a/clang/lib/AST/ByteCode/Context.h b/clang/lib/AST/ByteCode/Context.hindex 5898ab5e54599..6ab526cec0144 100644--- a/clang/lib/AST/ByteCode/Context.h+++ b/clang/lib/AST/ByteCode/Context.h@@ -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);+  bool 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);diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cppindex 5498065657e0a..6e511bc7d2fab 100644--- a/clang/lib/AST/ByteCode/EvalEmitter.cpp+++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp@@ -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++; }diff --git a/clang/lib/AST/ByteCode/EvalEmitter.h b/clang/lib/AST/ByteCode/EvalEmitter.hindex 7303adba22af7..2fe7da608c739 100644--- a/clang/lib/AST/ByteCode/EvalEmitter.h+++ b/clang/lib/AST/ByteCode/EvalEmitter.h@@ -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();diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cppindex 767cc4c3b19eb..6a5dc7a81f4a8 100644--- a/clang/lib/AST/ExprConstant.cpp+++ b/clang/lib/AST/ExprConstant.cpp@@ -18015,6 +18015,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());diff --git a/clang/test/Sema/diagnose_if.c b/clang/test/Sema/diagnose_if.cindex e9b8497d5ca4e..a4cf43e9c869f 100644--- a/clang/test/Sema/diagnose_if.c+++ b/clang/test/Sema/diagnose_if.c@@ -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__)))diff --git a/clang/test/SemaCXX/diagnose_if-ext.cpp b/clang/test/SemaCXX/diagnose_if-ext.cppindex d5625b501322e..e0f73976eea3a 100644--- a/clang/test/SemaCXX/diagnose_if-ext.cpp+++ b/clang/test/SemaCXX/diagnose_if-ext.cpp@@ -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}}diff --git a/clang/test/SemaCXX/diagnose_if.cpp b/clang/test/SemaCXX/diagnose_if.cppindex 21897c5184b73..1b9e660c4e224 100644--- a/clang/test/SemaCXX/diagnose_if.cpp+++ b/clang/test/SemaCXX/diagnose_if.cpp@@ -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__)))

@tbaederrtbaederrforce-pushed thediagnose-if branch 3 times, most recently from375d69e tob944301CompareJuly 18, 2025 10:50
…nevaluatedFake a function call to the given function and evaluate the givenexpression as if it was part of that function call.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@cor3ntincor3ntincor3ntin approved these changes

Assignees
No one assigned
Labels
clang:bytecodeIssues for the clang bytecode constexpr interpreterclang:frontendLanguage frontend issues, e.g. anything involving "Sema"clangClang issues not falling into any other category
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Bytecode Interpreter: Use inExpr::isPotentialConstantExpressionUnevaluated
3 participants
@tbaederr@llvmbot@cor3ntin

[8]ページ先頭

©2009-2025 Movatter.jp