From eb2049162edc933bf24114eb2803b08712639cdb Mon Sep 17 00:00:00 2001 From: "petro.zarytskyi" Date: Wed, 19 Jun 2024 21:52:16 +0300 Subject: [PATCH] Clone properly the storage class. When cloning functions we should clone the storage class of the canonical declaration in order to retain all of the qualifiers. Fixes #951 --- lib/Differentiator/DerivativeBuilder.cpp | 37 ++++++++++-------------- test/FirstDerivative/FunctionCalls.C | 26 +++++++++++++++++ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/lib/Differentiator/DerivativeBuilder.cpp b/lib/Differentiator/DerivativeBuilder.cpp index 24cd53b0d..33ba9b2e0 100644 --- a/lib/Differentiator/DerivativeBuilder.cpp +++ b/lib/Differentiator/DerivativeBuilder.cpp @@ -101,32 +101,25 @@ static void registerDerivative(FunctionDecl* derivedFD, Sema& semaRef) { NamespaceDecl* enclosingNS = nullptr; if (isa(FD)) { CXXRecordDecl* CXXRD = cast(DC); - returnedFD = CXXMethodDecl::Create(m_Context, - CXXRD, - noLoc, - name, - functionType, - FD->getTypeSourceInfo(), - FD->getStorageClass() - CLAD_COMPAT_FunctionDecl_UsesFPIntrin_Param(FD), - FD->isInlineSpecified(), - clad_compat::Function_GetConstexprKind - (FD), noLoc); + returnedFD = CXXMethodDecl::Create( + m_Context, CXXRD, noLoc, name, functionType, FD->getTypeSourceInfo(), + FD->getCanonicalDecl()->getStorageClass() + CLAD_COMPAT_FunctionDecl_UsesFPIntrin_Param(FD), + FD->isInlineSpecified(), clad_compat::Function_GetConstexprKind(FD), + noLoc); returnedFD->setAccess(FD->getAccess()); } else { assert (isa(FD) && "Unexpected!"); enclosingNS = VB.RebuildEnclosingNamespaces(DC); - returnedFD = FunctionDecl::Create(m_Context, - m_Sema.CurContext, - noLoc, - name, - functionType, - FD->getTypeSourceInfo(), - FD->getStorageClass() - CLAD_COMPAT_FunctionDecl_UsesFPIntrin_Param(FD), - FD->isInlineSpecified(), - FD->hasWrittenPrototype(), - clad_compat::Function_GetConstexprKind(FD)CLAD_COMPAT_CLANG10_FunctionDecl_Create_ExtraParams(FD->getTrailingRequiresClause())); + returnedFD = FunctionDecl::Create( + m_Context, m_Sema.CurContext, noLoc, name, functionType, + FD->getTypeSourceInfo(), + FD->getCanonicalDecl()->getStorageClass() + CLAD_COMPAT_FunctionDecl_UsesFPIntrin_Param(FD), + FD->isInlineSpecified(), FD->hasWrittenPrototype(), + clad_compat::Function_GetConstexprKind(FD) + CLAD_COMPAT_CLANG10_FunctionDecl_Create_ExtraParams( + FD->getTrailingRequiresClause())); } for (const FunctionDecl* NFD : FD->redecls()) diff --git a/test/FirstDerivative/FunctionCalls.C b/test/FirstDerivative/FunctionCalls.C index 28e659c5d..8222d7333 100644 --- a/test/FirstDerivative/FunctionCalls.C +++ b/test/FirstDerivative/FunctionCalls.C @@ -162,6 +162,27 @@ double test_8(double x) { // CHECK-NEXT: return _t0.pushforward; // CHECK-NEXT: } +class A { + public: + static double static_method(double x); +}; + +double A::static_method(double x) { + return x; +} + +double test_9(double x) { + return A::static_method(x); +} + +// CHECK: static clad::ValueAndPushforward static_method_pushforward(double x, double _d_x); + +// CHECK: double test_9_darg0(double x) { +// CHECK-NEXT: double _d_x = 1; +// CHECK-NEXT: clad::ValueAndPushforward _t0 = static_method_pushforward(x, _d_x); +// CHECK-NEXT: return _t0.pushforward; +// CHECK-NEXT: } + int main () { clad::differentiate(test_1, 0); clad::differentiate(test_2, 0); @@ -173,6 +194,7 @@ int main () { clad::differentiate(test_8, "x"); clad::differentiate(test_8); // expected-error {{TBR analysis is not meant for forward mode AD.}} clad::differentiate(test_8); // expected-error {{Both enable and disable TBR options are specified.}} + clad::differentiate(test_9); return 0; // CHECK: void increment_pushforward(int &i, int &_d_i) { @@ -182,4 +204,8 @@ int main () { // CHECK: clad::ValueAndPushforward func_with_enum_pushforward(double x, E e, double _d_x) { // CHECK-NEXT: return {x * x, _d_x * x + x * _d_x}; // CHECK-NEXT: } + +// CHECK: static clad::ValueAndPushforward static_method_pushforward(double x, double _d_x) { +// CHECK-NEXT: return {x, _d_x}; +// CHECK-NEXT: } }