99#include " ConstantFolder.h"
1010
1111#include " clad/Differentiator/CladUtils.h"
12+ #include " clad/Differentiator/DerivativeBuilder.h"
1213#include " clad/Differentiator/DiffMode.h"
1314#include " clad/Differentiator/DiffPlanner.h"
1415#include " clad/Differentiator/ErrorEstimator.h"
@@ -86,12 +87,11 @@ DerivativeAndOverload BaseForwardModeVisitor::Derive() {
8687 // or pointer type, only one of the indices have been requested
8788 if (DVI .size () > 1 || (isArrayOrPointerType (diffVarInfo.param ->getType ()) &&
8889 (diffVarInfo.paramIndexInterval .size () != 1 ))) {
89- diag (DiagnosticsEngine::Error,
90- m_DiffReq.Args ? m_DiffReq.Args ->getEndLoc () : noLoc,
91- " Forward mode differentiation w.r.t. several parameters at once is "
92- " not "
93- " supported, call 'clad::differentiate' for each parameter "
94- " separately" );
90+ SourceLocation L = m_DiffReq.Args ? m_DiffReq.Args ->getBeginLoc () : noLoc;
91+ diag (DiagnosticsEngine::Error, L,
92+ " forward mode differentiation w.r.t. several parameters at once is "
93+ " not supported; call 'clad::differentiate' for each parameter" )
94+ << L;
9595 return {};
9696 }
9797
@@ -113,10 +113,11 @@ DerivativeAndOverload BaseForwardModeVisitor::Derive() {
113113 if (!m_IndependentVar->getType ()
114114 ->getPointeeOrArrayElementType ()
115115 ->isRealType ()) {
116- diag (DiagnosticsEngine::Error, m_IndependentVar->getEndLoc (),
117- " attempted differentiation w.r.t. a parameter ('%0') which is not"
118- " an array or pointer of a real type" ,
119- {m_IndependentVar->getNameAsString ()});
116+ SourceLocation L = m_IndependentVar->getBeginLoc ();
117+ diag (DiagnosticsEngine::Error, L,
118+ " attempted differentiation w.r.t. parameter %0 which is not"
119+ " array or pointer of real type" )
120+ << m_IndependentVar << L;
120121 return {};
121122 }
122123 m_IndependentVarIndex = diffVarInfo.paramIndexInterval .Start ;
@@ -130,10 +131,11 @@ DerivativeAndOverload BaseForwardModeVisitor::Derive() {
130131 isField = true ;
131132 }
132133 if (!IsRealNonReferenceType (T)) {
133- diag (DiagnosticsEngine::Error, m_DiffReq.Args ->getEndLoc (),
134- " Attempted differentiation w.r.t. %0 '%1' which is not "
135- " of real type." ,
136- {(isField ? " member" : " parameter" ), diffVarInfo.source });
134+ SourceLocation L = m_DiffReq.Args ->getBeginLoc ();
135+ diag (DiagnosticsEngine::Error, L,
136+ " attempted differentiation w.r.t. %select{member|parameter}0 '%1' "
137+ " which is not of real type" )
138+ << isField << diffVarInfo.source << L;
137139 return {};
138140 }
139141 }
@@ -338,11 +340,11 @@ void BaseForwardModeVisitor::GenerateSeeds(const clang::FunctionDecl* dFD) {
338340 // Produce an error.
339341 if (param != m_IndependentVar &&
340342 !utils::GetValueType (dParamType).isConstQualified ()) {
341- // FIXME: Use diagnostics style as in #1596
342- diag (DiagnosticsEngine::Error, param-> getLocation () ,
343+ SourceLocation L = param-> getLocation ();
344+ diag (DiagnosticsEngine::Error, L ,
343345 " dependent non-const pointer and array parameters "
344- " are not supported; differentiate w.r.t. '%0' or mark it const" ,
345- { param-> getNameAsString ()}) ;
346+ " are not supported; differentiate w.r.t. %0 or mark it const" )
347+ << param << L ;
346348 }
347349 continue ;
348350 }
@@ -454,8 +456,7 @@ void BaseForwardModeVisitor::GenerateSeeds(const clang::FunctionDecl* dFD) {
454456}
455457
456458StmtDiff BaseForwardModeVisitor::VisitStmt (const Stmt* S) {
457- diag (DiagnosticsEngine::Warning, S->getBeginLoc (),
458- " attempted to differentiate unsupported statement, no changes applied" );
459+ diagUnsupported (S);
459460 // Unknown stmt, just clone it.
460461 return StmtDiff (Clone (S));
461462}
@@ -1012,8 +1013,7 @@ DiffMode BaseForwardModeVisitor::GetPushForwardMode() {
10121013StmtDiff BaseForwardModeVisitor::VisitCallExpr (const CallExpr* CE ) {
10131014 const FunctionDecl* FD = CE ->getDirectCallee ();
10141015 if (!FD ) {
1015- diag (DiagnosticsEngine::Warning, CE ->getBeginLoc (),
1016- " Differentiation of only direct calls is supported. Ignored" );
1016+ diagUnsupportedIndirectCalls (CE );
10171017 return StmtDiff (Clone (CE ));
10181018 }
10191019
@@ -1350,9 +1350,11 @@ BaseForwardModeVisitor::VisitBinaryOperator(const BinaryOperator* BinOp) {
13501350 (Ldiff.getExpr_dx ()->isModifiableLvalue (m_Context) !=
13511351 Expr::MLV_Valid) &&
13521352 !isCladArrayType (Ldiff.getExpr_dx ()->getType ())) {
1353- diag (DiagnosticsEngine::Warning, BinOp->getEndLoc (),
1353+ SourceLocation L = BinOp->getBeginLoc ();
1354+ diag (DiagnosticsEngine::Warning, L,
13541355 " derivative of an assignment attempts to assign to unassignable "
1355- " expr, assignment ignored" );
1356+ " expr, assignment ignored" )
1357+ << L;
13561358 opDiff = ConstantFolder::synthesizeLiteral (m_Context.IntTy , m_Context, 0 );
13571359 } else if (opCode == BO_Assign || opCode == BO_AddAssign ||
13581360 opCode == BO_SubAssign) {
@@ -1507,11 +1509,9 @@ StmtDiff BaseForwardModeVisitor::VisitDeclStmt(const DeclStmt* DS) {
15071509 if (typeDecl && (clad::utils::hasNonDifferentiableAttribute (typeDecl) ||
15081510 typeDecl->isLambda ())) {
15091511 for (auto * D : DS ->decls ()) {
1510- if (auto * VD = dyn_cast<VarDecl>(D))
1511- decls.push_back (VD );
1512- else
1513- diag (DiagnosticsEngine::Warning, D->getEndLoc (),
1514- " Unsupported declaration" );
1512+ assert (isa<VarDecl>(D) && " Mixed decl types in a single decl stmt is "
1513+ " not standard c++ syntax" );
1514+ decls.push_back (cast<VarDecl>(D));
15151515 }
15161516 Stmt* DSClone = BuildDeclStmt (decls);
15171517 return StmtDiff (DSClone, nullptr );
@@ -1556,8 +1556,7 @@ StmtDiff BaseForwardModeVisitor::VisitDeclStmt(const DeclStmt* DS) {
15561556 if (SADDiff.getDecl_dx ())
15571557 declsDiff.push_back (SADDiff.getDecl_dx ());
15581558 } else {
1559- diag (DiagnosticsEngine::Warning, D->getEndLoc (),
1560- " Unsupported declaration" );
1559+ diagUnsupported (D);
15611560 }
15621561 }
15631562
@@ -1984,10 +1983,12 @@ BaseForwardModeVisitor::DeriveSwitchStmtBodyHelper(const Stmt* stmt,
19841983 // We can also solve this issue by creating new scope and compound
19851984 // statement block wherever they are required instead of enclosing all
19861985 // the statements of a case label in a single compound statement.
1987- diag (DiagnosticsEngine::Error, containedSC->getBeginLoc (),
1988- " Differentiating switch case label contained in a compound "
1986+ SourceLocation L = containedSC->getBeginLoc ();
1987+ diag (DiagnosticsEngine::Error, L,
1988+ " differentiating switch case label contained in a compound "
19891989 " statement, other than the switch statement compound "
1990- " statement, is not supported." );
1990+ " statement, is not supported" )
1991+ << L;
19911992 return activeSC;
19921993 }
19931994 }
0 commit comments