2020#include " clang/Sema/Sema.h"
2121#include " llvm/ADT/ArrayRef.h"
2222#include " llvm/ADT/StringRef.h"
23+ #include " llvm/Support/PrettyStackTrace.h"
2324
2425#include < array>
2526#include < stack>
@@ -29,6 +30,10 @@ namespace clang {
2930class NestedNameSpecifier ;
3031} // namespace clang
3132
33+ namespace llvm {
34+ class raw_ostream ;
35+ } // namespace llvm
36+
3237namespace clad {
3338 class MultiplexExternalRMVSource ;
3439 // / A class that represents the result of Visit of ForwardModeVisitor.
@@ -136,6 +141,9 @@ namespace clad {
136141 // expression to be of object type in the reverse mode as well.
137142 clang::Expr* m_ThisExprDerivative = nullptr ;
138143
144+ // / The currently visited statement. Useful for crash pretty-printing.
145+ const clang::Stmt* m_CurVisitedStmt = nullptr ;
146+
139147 // / A function used to wrap result of visiting E in a lambda. Returns a call
140148 // / to the built lambda. Func is a functor that will be invoked inside
141149 // / lambda scope and block. Statements inside lambda are expected to be
@@ -671,6 +679,23 @@ namespace clad {
671679 clang::TemplateDecl* m_CladConstructorPushforwardTag = nullptr ;
672680 clang::TemplateDecl* m_CladConstructorReverseForwTag = nullptr ;
673681 };
682+
683+ // / A class that generates prettier stack traces when we crash on generating
684+ // / a derivative.
685+ class PrettyStackTraceDerivative : public llvm ::PrettyStackTraceEntry {
686+ const DiffRequest& m_DiffReq;
687+ using Blocks = std::vector<llvm::SmallVector<clang::Stmt*, 16 >>;
688+ const Blocks& m_Blocks;
689+ const clang::Sema& m_Sema;
690+ const clang::Stmt** m_Stmt = nullptr ;
691+
692+ public:
693+ PrettyStackTraceDerivative (const DiffRequest& DiffReq, const Blocks& B,
694+ const clang::Sema& Sema, const clang::Stmt** S)
695+ : m_DiffReq(DiffReq), m_Blocks(B), m_Sema(Sema), m_Stmt(S) {}
696+ void print (llvm::raw_ostream& OS ) const override ;
697+ };
698+
674699} // end namespace clad
675700
676701#endif // CLAD_VISITOR_BASE_H
0 commit comments