From cbc7d7e2858bb1cf63d0f51f447501ec8c048dd3 Mon Sep 17 00:00:00 2001 From: Max Andriychuk Date: Mon, 18 Nov 2024 01:19:58 +0100 Subject: [PATCH] Mark variables passed by pointer/reference as varied --- lib/Differentiator/ActivityAnalyzer.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Differentiator/ActivityAnalyzer.cpp b/lib/Differentiator/ActivityAnalyzer.cpp index eb4f2d2a7..f7810baf2 100644 --- a/lib/Differentiator/ActivityAnalyzer.cpp +++ b/lib/Differentiator/ActivityAnalyzer.cpp @@ -124,8 +124,22 @@ bool VariedAnalyzer::VisitCallExpr(CallExpr* CE) { MutableArrayRef FDparam = FD->parameters(); for (std::size_t i = 0, e = CE->getNumArgs(); i != e; ++i) { clang::Expr* par = CE->getArg(i); + + QualType parType = FDparam[i]->getType(); + while (parType->isPointerType()) + parType = parType->getPointeeType(); + if((parType->isReferenceType() || utils::isArrayOrPointerType(parType)) && !parType.isConstQualified()){ + m_Marking = true; + m_Varied = true; + } + TraverseStmt(par); - m_VariedDecls.insert(FDparam[i]); + + m_Marking = false; + m_Varied = false; + + if(!parType.isConstQualified()) + m_VariedDecls.insert(FDparam[i]); } } return true; @@ -133,12 +147,16 @@ bool VariedAnalyzer::VisitCallExpr(CallExpr* CE) { bool VariedAnalyzer::VisitDeclStmt(DeclStmt* DS) { for (Decl* D : DS->decls()) { + QualType VDTy = cast(D)->getType(); + if(utils::isArrayOrPointerType(VDTy)){ + copyVarToCurBlock(cast(D)); + continue; + } if (Expr* init = cast(D)->getInit()) { m_Varied = false; TraverseStmt(init); m_Marking = true; - QualType VDTy = cast(D)->getType(); - if (m_Varied || utils::isArrayOrPointerType(VDTy)) + if (m_Varied ) copyVarToCurBlock(cast(D)); m_Marking = false; }