Skip to content

Commit

Permalink
Clone properly the storage class.
Browse files Browse the repository at this point in the history
When cloning functions we should clone
the storage class of the canonical declaration
in order to retain all of the qualifiers.
Fixes #951
  • Loading branch information
PetroZarytskyi authored and vgvassilev committed Jun 20, 2024
1 parent 6168843 commit eb20491
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
37 changes: 15 additions & 22 deletions lib/Differentiator/DerivativeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,25 @@ static void registerDerivative(FunctionDecl* derivedFD, Sema& semaRef) {
NamespaceDecl* enclosingNS = nullptr;
if (isa<CXXMethodDecl>(FD)) {
CXXRecordDecl* CXXRD = cast<CXXRecordDecl>(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<FunctionDecl>(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())
Expand Down
26 changes: 26 additions & 0 deletions test/FirstDerivative/FunctionCalls.C
Original file line number Diff line number Diff line change
Expand Up @@ -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<double, double> 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<double, double> _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);
Expand All @@ -173,6 +194,7 @@ int main () {
clad::differentiate(test_8, "x");
clad::differentiate<clad::opts::enable_tbr>(test_8); // expected-error {{TBR analysis is not meant for forward mode AD.}}
clad::differentiate<clad::opts::enable_tbr, clad::opts::disable_tbr>(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) {
Expand All @@ -182,4 +204,8 @@ int main () {
// CHECK: clad::ValueAndPushforward<double, double> 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<double, double> static_method_pushforward(double x, double _d_x) {
// CHECK-NEXT: return {x, _d_x};
// CHECK-NEXT: }
}

0 comments on commit eb20491

Please sign in to comment.