From fefe670be067cd698a71ab8dcd6a92803e2192ef Mon Sep 17 00:00:00 2001 From: Timm Baeder Date: Fri, 12 Sep 2025 07:12:09 +0200 Subject: [PATCH] [clang][bytecode] Compile the definition, not the most recent decl (#158093) --- clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 6 ++---- clang/lib/AST/ByteCode/Function.h | 7 ++++--- clang/lib/AST/ByteCode/Interp.cpp | 7 +++++-- clang/test/Modules/lambda-merge.cpp | 1 + 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp index 1d7170879951..274efccac79d 100644 --- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp +++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp @@ -24,15 +24,13 @@ void ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl, Function *Func) { assert(FuncDecl); assert(Func); + assert(FuncDecl->isThisDeclarationADefinition()); // Manually created functions that haven't been assigned proper // parameters yet. if (!FuncDecl->param_empty() && !FuncDecl->param_begin()) return; - if (!FuncDecl->isDefined()) - return; - // Set up lambda captures. if (const auto *MD = dyn_cast(FuncDecl); MD && isLambdaCallOperator(MD)) { @@ -87,7 +85,7 @@ void ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl, } // Set the function's code. - Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap), + Func->setCode(FuncDecl, NextLocalOffset, std::move(Code), std::move(SrcMap), std::move(Scopes), FuncDecl->hasBody()); Func->setIsFullyCompiled(true); } diff --git a/clang/lib/AST/ByteCode/Function.h b/clang/lib/AST/ByteCode/Function.h index af429b7849e8..95add5809afc 100644 --- a/clang/lib/AST/ByteCode/Function.h +++ b/clang/lib/AST/ByteCode/Function.h @@ -236,9 +236,10 @@ private: bool HasRVO, bool IsLambdaStaticInvoker); /// Sets the code of a function. - void setCode(unsigned NewFrameSize, llvm::SmallVector &&NewCode, - SourceMap &&NewSrcMap, llvm::SmallVector &&NewScopes, - bool NewHasBody) { + void setCode(FunctionDeclTy Source, unsigned NewFrameSize, + llvm::SmallVector &&NewCode, SourceMap &&NewSrcMap, + llvm::SmallVector &&NewScopes, bool NewHasBody) { + this->Source = Source; FrameSize = NewFrameSize; Code = std::move(NewCode); SrcMap = std::move(NewSrcMap); diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index b961a413fbe7..d5e75a0c9046 100644 --- a/clang/lib/AST/ByteCode/Interp.cpp +++ b/clang/lib/AST/ByteCode/Interp.cpp @@ -1493,9 +1493,12 @@ bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { } static void compileFunction(InterpState &S, const Function *Func) { + const FunctionDecl *Definition = Func->getDecl()->getDefinition(); + if (!Definition) + return; + Compiler(S.getContext(), S.P) - .compileFunc(Func->getDecl()->getMostRecentDecl(), - const_cast(Func)); + .compileFunc(Definition, const_cast(Func)); } bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func, diff --git a/clang/test/Modules/lambda-merge.cpp b/clang/test/Modules/lambda-merge.cpp index e996c9c0d5d1..6b61d356ec58 100644 --- a/clang/test/Modules/lambda-merge.cpp +++ b/clang/test/Modules/lambda-merge.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -fmodules -std=c++17 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s +// RUN: %clang_cc1 -fmodules -std=c++17 -emit-llvm %s -o - -triple x86_64-linux-gnu -fexperimental-new-constant-interpreter | FileCheck %s #pragma clang module build A module A {}