Skip to content

Commit

Permalink
Fix braceless if differentiation in reverse mode
Browse files Browse the repository at this point in the history
As in #1049, the statement inside the braceless if is not included causing an error.
Fixes:#1049
  • Loading branch information
Max Andriychuk authored and Max Andriychuk committed Aug 24, 2024
1 parent 4368c04 commit a4c1d25
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,21 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
elseDiff.getStmt());
addToCurrentBlock(Forward, direction::forward);

Stmt* Reverse = clad_compat::IfStmt_Create(
m_Context, noLoc, If->isConstexpr(), /*Init=*/nullptr, /*Var=*/nullptr,
condDiffStored, noLoc, noLoc, thenDiff.getStmt_dx(), noLoc,
elseDiff.getStmt_dx());
Stmt* Reverse = nullptr;
// thenDiff.getStmt_dx() might be empty if TBR is on leadinf to a crash in
// case of the braceless if.
if (thenDiff.getStmt_dx())
Reverse = clad_compat::IfStmt_Create(
m_Context, noLoc, If->isConstexpr(), /*Init=*/nullptr,
/*Var=*/nullptr, condDiffStored, noLoc, noLoc, thenDiff.getStmt_dx(),
noLoc, elseDiff.getStmt_dx());
else if (elseDiff.getStmt_dx())
Reverse = clad_compat::IfStmt_Create(
m_Context, noLoc, If->isConstexpr(), /*Init=*/nullptr,

Check warning on line 891 in lib/Differentiator/ReverseModeVisitor.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Differentiator/ReverseModeVisitor.cpp#L889-L891

Added lines #L889 - L891 were not covered by tests
/*Var=*/nullptr,
BuildOp(clang::UnaryOperatorKind::UO_LNot,
BuildParens(condDiffStored)),
noLoc, noLoc, elseDiff.getStmt_dx(), noLoc, {});
addToCurrentBlock(Reverse, direction::reverse);
CompoundStmt* ForwardBlock = endBlock(direction::forward);
CompoundStmt* ReverseBlock = endBlock(direction::reverse);
Expand Down

0 comments on commit a4c1d25

Please sign in to comment.