From e2835237282438b6f0caec3bb708879e0871a4af Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Tue, 19 Sep 2023 08:05:05 +0000 Subject: [PATCH] Add LLVM/Clang 17 support --- CMakeLists.txt | 8 ++++---- include/clad/Differentiator/Compatibility.h | 22 +++++++++++++++++++++ include/clad/Differentiator/VisitorBase.h | 3 ++- lib/Differentiator/StmtClone.cpp | 2 +- tools/ClangBackendPlugin.cpp | 3 +++ 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2163bdda..6ee4e06e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,11 +21,11 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) ## Define clad supported version of clang and llvm set(CLANG_MIN_SUPPORTED 5.0) - set(CLANG_MAX_SUPPORTED "16.0.x") - set(CLANG_VERSION_UPPER_BOUND 16.1.0) + set(CLANG_MAX_SUPPORTED "17.0.x") + set(CLANG_VERSION_UPPER_BOUND 17.1.0) set(LLVM_MIN_SUPPORTED 5.0) - set(LLVM_MAX_SUPPORTED "16.0.x") - set(LLVM_VERSION_UPPER_BOUND 16.1.0) + set(LLVM_MAX_SUPPORTED "17.0.x") + set(LLVM_VERSION_UPPER_BOUND 17.1.0) if (NOT DEFINED Clang_DIR) set(Clang_DIR ${LLVM_DIR}) diff --git a/include/clad/Differentiator/Compatibility.h b/include/clad/Differentiator/Compatibility.h index edf84134d..3c237606c 100644 --- a/include/clad/Differentiator/Compatibility.h +++ b/include/clad/Differentiator/Compatibility.h @@ -767,5 +767,27 @@ static inline bool IsPRValue(const Expr* E) { return E->isPRValue(); } ,Fn->isThisDeclarationADefinition() #endif + +// Clang 17 change type of last param of ActOnStartOfLambdaDefinition +// from Scope* to 'const DeclSpec&' +#if CLANG_VERSION_MAJOR < 17 +static inline Scope* Sema_ActOnStartOfLambdaDefinition_ScopeOrDeclSpec(Scope *CurScope, const DeclSpec &DS) { + return CurScope; +} +#elif CLANG_VERSION_MAJOR >= 17 +static inline const DeclSpec& Sema_ActOnStartOfLambdaDefinition_ScopeOrDeclSpec(Scope *CurScope, const DeclSpec &DS) { + return DS; +} +#endif + +// Clang 17 add one extra param to clang::PredefinedExpr::Create - isTransparent + +#if CLANG_VERSION_MAJOR < 17 +#define CLAD_COMPAT_CLANG17_IsTransparent(Node) /**/ +#elif CLANG_VERSION_MAJOR >= 17 +#define CLAD_COMPAT_CLANG17_IsTransparent(Node) \ + ,Node->isTransparent() +#endif + } // namespace clad_compat #endif //CLAD_COMPATIBILITY diff --git a/include/clad/Differentiator/VisitorBase.h b/include/clad/Differentiator/VisitorBase.h index ef609f395..81e4af9a2 100644 --- a/include/clad/Differentiator/VisitorBase.h +++ b/include/clad/Differentiator/VisitorBase.h @@ -155,7 +155,8 @@ namespace clad { S.PushLambdaScope(); V.beginScope(clang::Scope::BlockScope | clang::Scope::FnScope | clang::Scope::DeclScope); - S.ActOnStartOfLambdaDefinition(Intro, D, V.getCurrentScope()); + S.ActOnStartOfLambdaDefinition(Intro, D, + clad_compat::Sema_ActOnStartOfLambdaDefinition_ScopeOrDeclSpec(V.getCurrentScope(), DS)); V.beginBlock(); func(); clang::CompoundStmt* body = V.endBlock(); diff --git a/lib/Differentiator/StmtClone.cpp b/lib/Differentiator/StmtClone.cpp index 14c49c894..241fe79ff 100644 --- a/lib/Differentiator/StmtClone.cpp +++ b/lib/Differentiator/StmtClone.cpp @@ -71,7 +71,7 @@ Stmt* StmtClone::VisitDeclRefExpr(DeclRefExpr *Node) { return DeclRefExpr::Create(Ctx, Node->getQualifierLoc(), Node->getTemplateKeywordLoc(), Node->getDecl(), Node->refersToEnclosingVariableOrCapture(), Node->getNameInfo(), Node->getType(), Node->getValueKind(), Node->getFoundDecl(), &TAListInfo); } DEFINE_CREATE_EXPR(IntegerLiteral, (Ctx, Node->getValue(), Node->getType(), Node->getLocation())) -DEFINE_CLONE_EXPR_CO(PredefinedExpr, (CLAD_COMPAT_CLANG8_Ctx_ExtraParams Node->getLocation(), Node->getType(), Node->getIdentKind(), Node->getFunctionName())) +DEFINE_CLONE_EXPR_CO(PredefinedExpr, (CLAD_COMPAT_CLANG8_Ctx_ExtraParams Node->getLocation(), Node->getType(), Node->getIdentKind() CLAD_COMPAT_CLANG17_IsTransparent(Node), Node->getFunctionName())) DEFINE_CLONE_EXPR(CharacterLiteral, (Node->getValue(), Node->getKind(), Node->getType(), Node->getLocation())) DEFINE_CLONE_EXPR(ImaginaryLiteral, (Clone(Node->getSubExpr()), Node->getType())) DEFINE_CLONE_EXPR(ParenExpr, (Node->getLParen(), Node->getRParen(), Clone(Node->getSubExpr()))) diff --git a/tools/ClangBackendPlugin.cpp b/tools/ClangBackendPlugin.cpp index f703296d5..ac2e94389 100644 --- a/tools/ClangBackendPlugin.cpp +++ b/tools/ClangBackendPlugin.cpp @@ -15,7 +15,10 @@ #include "llvm/IR/LegacyPassManager.h" #include "llvm/Pass.h" #include "llvm/PassRegistry.h" + +#if LLVM_VERSION_MAJOR >= 10 && LLVM_VERSION_MAJOR < 16 #include "llvm/Transforms/IPO/PassManagerBuilder.h" +#endif // LLVM_VERSION_MAJOR >= 10 && LLVM_VERSION_MAJOR < 16 namespace clad { using namespace llvm;