From cfc60d2966a627283084253f6504dd618312ba12 Mon Sep 17 00:00:00 2001 From: Vaibhav Thakkar Date: Sat, 28 Oct 2023 09:53:25 +0530 Subject: [PATCH] Add test for functions in inline namespace --- lib/Differentiator/CladUtils.cpp | 4 ++++ test/Gradient/FunctionCalls.C | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/Differentiator/CladUtils.cpp b/lib/Differentiator/CladUtils.cpp index 08aa5ea56..d1a435848 100644 --- a/lib/Differentiator/CladUtils.cpp +++ b/lib/Differentiator/CladUtils.cpp @@ -187,6 +187,10 @@ namespace clad { continue; } if (DC2->isInlineNamespace()) { + // Inline namespace can be skipped from context, because its members + // are automatically searched from the parent namespace. + // This will also help us to deal with intermediate inline namespaces + // like std::__1::, as present in std functions for libc++. DC2 = DC2->getParent(); continue; } diff --git a/test/Gradient/FunctionCalls.C b/test/Gradient/FunctionCalls.C index 60cfa5fb4..f3b6f6a2e 100644 --- a/test/Gradient/FunctionCalls.C +++ b/test/Gradient/FunctionCalls.C @@ -594,6 +594,45 @@ double fn10(double x, double y) { // CHECK-NEXT: * _d_x += _d_out; // CHECK-NEXT: } +namespace n1{ + inline namespace n2{ + double sum(const double& x, const double& y) { + return x + y; + } + } +} + +namespace clad{ +namespace custom_derivatives{ + namespace n1{ + inline namespace n2{ + void sum_pullback(const double& x, const double& y, double _d_y0, clad::array_ref _d_x, clad::array_ref _d_y) { + * _d_x += _d_y0; + * _d_y += _d_y0; + } + } + } +} +} + +double fn11(double x, double y) { + return n1::n2::sum(x, y); +} + +// CHECK: void fn11_grad(double x, double y, clad::array_ref _d_x, clad::array_ref _d_y) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: double _t1; +// CHECK-NEXT: _t0 = x; +// CHECK-NEXT: _t1 = y; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: clad::custom_derivatives::n1::sum_pullback(_t0, _t1, 1, &* _d_x, &* _d_y); +// CHECK-NEXT: double _r0 = * _d_x; +// CHECK-NEXT: double _r1 = * _d_y; +// CHECK-NEXT: } +// CHECK-NEXT: } + template void reset(T* arr, int n) { for (int i=0; i