Skip to content

Commit

Permalink
Add support for null (empty) statements
Browse files Browse the repository at this point in the history
This commit removes the "unsupported" warning when differentiating code with ";" in both forward and reverse modes. It also prevents additional semicolons from getting pulled into the derivative code.

Fixes: #899
  • Loading branch information
gojakuch committed May 20, 2024
1 parent 19c6573 commit 0dd5adf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/clad/Differentiator/BaseForwardModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class BaseForwardModeVisitor
const clang::SubstNonTypeTemplateParmExpr* NTTP);
StmtDiff VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr* IVIE);
StmtDiff VisitCStyleCastExpr(const clang::CStyleCastExpr* CSCE);
StmtDiff VisitNullStmt(const clang::NullStmt* NS) {
return StmtDiff{};
};
static DeclDiff<clang::StaticAssertDecl>
DifferentiateStaticAssertDecl(const clang::StaticAssertDecl* SAD);

Expand Down
3 changes: 3 additions & 0 deletions include/clad/Differentiator/ReverseModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ namespace clad {
const clang::SubstNonTypeTemplateParmExpr* NTTP);
StmtDiff
VisitCXXNullPtrLiteralExpr(const clang::CXXNullPtrLiteralExpr* NPE);
StmtDiff VisitNullStmt(const clang::NullStmt* NS) {
return StmtDiff{};
};
static DeclDiff<clang::StaticAssertDecl>
DifferentiateStaticAssertDecl(const clang::StaticAssertDecl* SAD);

Expand Down
14 changes: 14 additions & 0 deletions test/Gradient/Gradients.C
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,18 @@ double fn_cond_init(double x) {
//CHECK-NEXT: }
//CHECK-NEXT: }

double fn_empty_lines(double x) {
;;;;;;;;;;;;;;;;;
;;;;;return x;;;;
;;;;;;;;;;;;;;;;;
} // = x

//CHECK: void fn_empty_lines_grad(double x, double *_d_x) {
//CHECK-NEXT: goto _label0;
//CHECK-NEXT: _label0:
//CHECK-NEXT: *_d_x += 1;
//CHECK-NEXT: }

#define TEST(F, x, y) \
{ \
result[0] = 0; \
Expand Down Expand Up @@ -971,4 +983,6 @@ int main() {
INIT_GRADIENT(fn_cond_init);
TEST_GRADIENT(fn_cond_init, /*numOfDerivativeArgs=*/1, 0, &dx); // CHECK-EXEC: 1.00
TEST_GRADIENT(fn_cond_init, /*numOfDerivativeArgs=*/1, -1, &dx); // CHECK-EXEC: 1.00

INIT_GRADIENT(fn_empty_lines);
}

0 comments on commit 0dd5adf

Please sign in to comment.