Skip to content

Commit

Permalink
Initial commit to add clang-18 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbarton authored Mar 15, 2024
1 parent d2df900 commit e51d29b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 114 deletions.
25 changes: 20 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ jobs:
compiler: clang
clang-runtime: '17'

- name: osx14-arm-clang-runtime18
os: macos-14
compiler: clang
clang-runtime: '18'

- name: osx13-x86-clang-runtime12
os: macos-13
compiler: clang
Expand Down Expand Up @@ -79,6 +84,11 @@ jobs:
compiler: clang
clang-runtime: '17'

- name: osx13-x86-clang-runtime18
os: macos-13
compiler: clang
clang-runtime: '18'

- name: win2022-msvc-runtime14
os: windows-2022
compiler: msvc
Expand All @@ -99,6 +109,11 @@ jobs:
compiler: msvc
clang-runtime: '17'

- name: win2022-msvc-runtime18
os: windows-2022
compiler: msvc
clang-runtime: '18'

- name: ubu22-clang15-runtime16-debug
os: ubuntu-22.04
compiler: clang-15
Expand Down Expand Up @@ -442,12 +457,12 @@ jobs:
extra_packages: 'libtrilinos-kokkos-dev ninja-build'
extra_cmake_options: '-G Ninja'

# Оld, still supported versions
- name: ubu22-clang16-runtime18
os: ubuntu-22.04
compiler: clang-16
clang-runtime: '18'

- name: ubu20-gcc9-runtime7
os: ubuntu-20.04
compiler: gcc-9
clang-runtime: '7'
# Оld, still supported versions

- name: ubu20-gcc9-runtime8
os: ubuntu-20.04
Expand Down
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )

## Define clad supported version of clang and llvm

set(CLANG_MIN_SUPPORTED 7.0)
set(CLANG_MAX_SUPPORTED "17.0.x")
set(CLANG_VERSION_UPPER_BOUND 17.1.0)
set(LLVM_MIN_SUPPORTED 7.0)
set(LLVM_MAX_SUPPORTED "17.0.x")
set(LLVM_VERSION_UPPER_BOUND 17.1.0)
set(CLANG_MIN_SUPPORTED 8.0)
set(CLANG_MAX_SUPPORTED "19.0.0")
set(CLANG_VERSION_UPPER_BOUND 19.0.0)
set(LLVM_MIN_SUPPORTED 8.0)
set(LLVM_MAX_SUPPORTED "19.0.0")
set(LLVM_VERSION_UPPER_BOUND 19.0.0)

if (NOT DEFINED Clang_DIR)
set(Clang_DIR ${LLVM_DIR})
Expand Down
110 changes: 17 additions & 93 deletions include/clad/Differentiator/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,20 @@ NamespaceDecl_Create(ASTContext& C, DeclContext* DC, bool Inline,
}
#endif

// Clang 7 add one extra param in UnaryOperator constructor.

#if CLANG_VERSION_MAJOR >= 7
#define CLAD_COMPAT_CLANG7_UnaryOperator_ExtraParams , Node->canOverflow()
#endif


// Clang 8 change E->EvaluateAsInt(APSInt int, context) ===> E->EvaluateAsInt(Expr::EvalResult res, context)

static inline bool Expr_EvaluateAsInt(const Expr *E,
APSInt &IntValue, const ASTContext &Ctx,
Expr::SideEffectsKind AllowSideEffects = Expr::SideEffectsKind::SE_NoSideEffects)
{
#if CLANG_VERSION_MAJOR < 8
return E->EvaluateAsInt(IntValue, Ctx, AllowSideEffects);
#elif CLANG_VERSION_MAJOR >= 8
Expr::EvalResult res;
if (E->EvaluateAsInt(res, Ctx, AllowSideEffects)) {
IntValue = res.Val.getInt();
return true;
}
return false;
#endif

}

// Clang 12: bool Expr::EvaluateAsConstantExpr(EvalResult &Result,
Expand All @@ -111,7 +102,6 @@ static inline bool Expr_EvaluateAsConstantExpr(const Expr* E,
}

// Compatibility helper function for creation IfStmt.
// Clang 8 and above use Create.
// Clang 12 and above use two extra params.

static inline IfStmt* IfStmt_Create(const ASTContext &Ctx,
Expand All @@ -120,9 +110,8 @@ static inline IfStmt* IfStmt_Create(const ASTContext &Ctx,
SourceLocation LPL, SourceLocation RPL,
Stmt *Then, SourceLocation EL=SourceLocation(), Stmt *Else=nullptr)
{
#if CLANG_VERSION_MAJOR < 8
return new (Ctx) IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
#elif CLANG_VERSION_MAJOR < 12

#if CLANG_VERSION_MAJORR < 12
return IfStmt::Create(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
#elif CLANG_VERSION_MAJOR < 14
return IfStmt::Create(Ctx, IL, IsConstexpr, Init, Var, Cond, LPL, RPL, Then, EL, Else);
Expand All @@ -134,34 +123,14 @@ static inline IfStmt* IfStmt_Create(const ASTContext &Ctx,
#endif
}


// Clang 8 change Node->getIdentType() ===> Node->getIdentKind()

#if CLANG_VERSION_MAJOR < 8
#define getIdentKind() getIdentType()
#endif


// Clang 8 change <NAME>Stmt(...) constructor to private ===> Use <NAME>Stmt::Create(...)

#if CLANG_VERSION_MAJOR < 8
#define CLAD_COMPAT_CREATE(CLASS, CTORARGS) (new (Ctx) CLASS CTORARGS)
#elif CLANG_VERSION_MAJOR >= 8
#define CLAD_COMPAT_CREATE(CLASS, CTORARGS) (CLASS::Create CTORARGS)
#endif

#define CLAD_COMPAT_CREATE(CLASS, CTORARGS) (CLASS::Create CTORARGS)

// Compatibility helper function for creation CallExpr.
// Clang 8 and above use Create.
// Clang 12 and above use one extra param.

#if CLANG_VERSION_MAJOR < 8
static inline CallExpr* CallExpr_Create(const ASTContext &Ctx, Expr *Fn, ArrayRef< Expr *> Args,
QualType Ty, ExprValueKind VK, SourceLocation RParenLoc)
{
return new (Ctx) CallExpr(Ctx, Fn, Args, Ty, VK, RParenLoc);
}
#elif CLANG_VERSION_MAJOR < 12
#if CLANG_VERSION_MAJOR < 12
static inline CallExpr* CallExpr_Create(const ASTContext &Ctx, Expr *Fn, ArrayRef< Expr *> Args,
QualType Ty, ExprValueKind VK, SourceLocation RParenLoc,
unsigned MinNumArgs = 0, CallExpr::ADLCallKind UsesADL = CallExpr::NotADL)
Expand All @@ -178,12 +147,9 @@ static inline CallExpr* CallExpr_Create(const ASTContext &Ctx, Expr *Fn, ArrayRe
#endif


// Clang 8 add one extra param (Ctx) in some constructors.
// Clang 12 and above use one extra param.

#if CLANG_VERSION_MAJOR < 8
#define CLAD_COMPAT_CLANG8_CallExpr_ExtraParams /**/
#elif CLANG_VERSION_MAJOR < 12
#if CLANG_VERSION_MAJOR < 12
#define CLAD_COMPAT_CLANG8_CallExpr_ExtraParams ,Node->getNumArgs(),Node->getADLCallKind()
#elif CLANG_VERSION_MAJOR >= 12
#define CLAD_COMPAT_CLANG8_CallExpr_ExtraParams ,Node->getFPFeatures(),Node->getNumArgs(),Node->getADLCallKind()
Expand Down Expand Up @@ -239,35 +205,25 @@ static inline void ExprSetDeps(Expr* result, Expr* Node) {
#define CLAD_COMPAT_CLANG11_WhileStmt_ExtraParams ,Node->getLParenLoc(),Node->getRParenLoc()
#endif

// Compatibility helper function for creation CXXOperatorCallExpr. Clang 8 and above use Create.
// Compatibility helper function for creation CXXOperatorCallExpr.

static inline CXXOperatorCallExpr* CXXOperatorCallExpr_Create(ASTContext &Ctx,
OverloadedOperatorKind OpKind, Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
ExprValueKind VK, SourceLocation OperatorLoc, CLAD_COMPAT_CLANG11_CXXOperatorCallExpr_Create_ExtraParamsOverride FPFeatures
CLAD_COMPAT_CLANG11_CXXOperatorCallExpr_Create_ExtraParamsPar
)
{
#if CLANG_VERSION_MAJOR < 8
return new (Ctx) CXXOperatorCallExpr(Ctx, OpKind, Fn, Args, Ty, VK, OperatorLoc, FPFeatures);
#elif CLANG_VERSION_MAJOR >= 8
return CXXOperatorCallExpr::Create(Ctx, OpKind, Fn, Args, Ty, VK, OperatorLoc, FPFeatures
CLAD_COMPAT_CLANG11_CXXOperatorCallExpr_Create_ExtraParamsUse
);
#endif

}


// Compatibility helper function for creation CXXMemberCallExpr.
// Clang 8 and above use Create.
// Clang 12 and above use two extra param.

#if CLANG_VERSION_MAJOR < 8
static inline CXXMemberCallExpr* CXXMemberCallExpr_Create(ASTContext &Ctx,
Expr *Fn, ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, SourceLocation RP)
{
return new (Ctx) CXXMemberCallExpr(Ctx, Fn, Args, Ty, VK, RP);
}
#elif CLANG_VERSION_MAJOR < 12
#if CLANG_VERSION_MAJOR < 12
static inline CXXMemberCallExpr* CXXMemberCallExpr_Create(ASTContext &Ctx,
Expr *Fn, ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, SourceLocation RP)
{
Expand All @@ -284,62 +240,36 @@ static inline CXXMemberCallExpr* CXXMemberCallExpr_Create(ASTContext &Ctx,
#endif


// Compatibility helper function for creation CaseStmt. Clang 8 and above use Create.
// Compatibility helper function for creation CaseStmt.

static inline CaseStmt* CaseStmt_Create(ASTContext &Ctx,
Expr *lhs, Expr *rhs, SourceLocation caseLoc, SourceLocation ellipsisLoc, SourceLocation colonLoc)
{
#if CLANG_VERSION_MAJOR < 8
return new (Ctx) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
#elif CLANG_VERSION_MAJOR >= 8

return CaseStmt::Create(const_cast<ASTContext&>(Ctx), lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
#endif

}


// Compatibility helper function for creation SwitchStmt.
// Clang 8 and above use Create.
// Clang 12 and above use two extra params.

static inline SwitchStmt* SwitchStmt_Create(const ASTContext &Ctx,
Stmt *Init, VarDecl *Var, Expr *Cond,
SourceLocation LParenLoc, SourceLocation RParenLoc)
{
#if CLANG_VERSION_MAJOR < 8
return new (Ctx) SwitchStmt(Ctx, Init, Var, Cond);
#elif CLANG_VERSION_MAJOR < 12

#if CLANG_VERSION_MAJOR < 12
return SwitchStmt::Create(Ctx, Init, Var, Cond);
#elif CLANG_VERSION_MAJOR >= 12
return SwitchStmt::Create(Ctx, Init, Var, Cond, LParenLoc, RParenLoc);
#endif
}


// Clang 8 change E->getLocStart() ===> E->getBeginLoc()
// E->getLocEnd() ===> E->getEndLoc()
// Clang 7 define both for compatibility

#if CLANG_VERSION_MAJOR < 7
#define getBeginLoc() getLocStart()
#define getEndLoc() getLocEnd()
#endif


// Clang 8 add one extra param (Ctx) in some constructors.

#if CLANG_VERSION_MAJOR < 8
#define CLAD_COMPAT_CLANG8_Ctx_ExtraParams /**/
#elif CLANG_VERSION_MAJOR >= 8
#define CLAD_COMPAT_CLANG8_Ctx_ExtraParams Ctx,
#endif


// Clang 8 change result->setNumArgs(Ctx, Num) ===> result->setNumArgsUnsafe(Num)

#if CLANG_VERSION_MAJOR < 8
#define setNumArgsUnsafe(NUM) setNumArgs(Ctx, NUM)
#endif

#define CLAD_COMPAT_CLANG8_Ctx_ExtraParams Ctx,

// Compatibility helper function for getConstexprKind(). Clang 9

Expand Down Expand Up @@ -478,11 +408,9 @@ static inline QualType getConstantArrayType(const ASTContext &Ctx,
/// `ASTContext` to be passed as an argument.
static inline QualType CXXMethodDecl_getThisType(Sema& SemaRef,
const CXXMethodDecl* method) {
#if CLANG_VERSION_MAJOR >= 8

auto thisType = method->getThisType();
#elif CLANG_VERSION_MAJOR < 8
auto thisType = method->getThisType(SemaRef.getASTContext());
#endif

return thisType;
}

Expand Down Expand Up @@ -563,10 +491,6 @@ static inline Qualifiers CXXMethodDecl_getMethodQualifiers(const CXXMethodDecl*
static inline Qualifiers CXXMethodDecl_getMethodQualifiers(const CXXMethodDecl* MD) {
return MD->getTypeQualifiers();
}
#elif CLANG_VERSION_MAJOR < 8
static inline Qualifiers CXXMethodDecl_getMethodQualifiers(const CXXMethodDecl* MD) {
return Qualifiers::fromFastMask(MD->getTypeQualifiers());
}
#endif

#if CLANG_VERSION_MAJOR <= 13
Expand Down
3 changes: 0 additions & 3 deletions include/clad/Differentiator/StmtClone.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ namespace utils {
DECLARE_CLONE_FN(PseudoObjectExpr)
DECLARE_CLONE_FN(SubstNonTypeTemplateParmExpr)
DECLARE_CLONE_FN(CXXScalarValueInitExpr)
// `ConstantExpr` node is only available after clang 7.
#if CLANG_VERSION_MAJOR > 7
DECLARE_CLONE_FN(ConstantExpr)
#endif
#if CLANG_VERSION_MAJOR > 8
// `ValueStmt` node is only available after clang 8.
DECLARE_CLONE_FN(ValueStmt)
Expand Down
4 changes: 0 additions & 4 deletions lib/Differentiator/BaseForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1619,11 +1619,7 @@ static SwitchCase* getContainedSwitchCaseStmt(const CompoundStmt* CS) {
/// Returns top switch statement in the `SwitchStack` of the given
/// Function Scope.
static SwitchStmt* getTopSwitchStmtOfSwitchStack(sema::FunctionScopeInfo* FSI) {
#if CLANG_VERSION_MAJOR < 7
return FSI->SwitchStack.back();
#elif CLANG_VERSION_MAJOR >= 7
return FSI->SwitchStack.back().getPointer();
#endif
}

StmtDiff BaseForwardModeVisitor::VisitSwitchStmt(const SwitchStmt* SS) {
Expand Down
5 changes: 2 additions & 3 deletions lib/Differentiator/StmtClone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ DEFINE_CREATE_EXPR(CXXFunctionalCastExpr,
Node->getLParenLoc(), Node->getRParenLoc()))
DEFINE_CREATE_EXPR(ExprWithCleanups, (Ctx, Node->getSubExpr(),
Node->cleanupsHaveSideEffects(), {}))
// clang <= 7 do not have `ConstantExpr` node.
#if CLANG_VERSION_MAJOR > 7

DEFINE_CREATE_EXPR(ConstantExpr, (Ctx, Clone(Node->getSubExpr()) CLAD_COMPAT_ConstantExpr_Create_ExtraParams))
#endif


DEFINE_CLONE_EXPR_CO(
CXXTemporaryObjectExpr,
Expand Down

0 comments on commit e51d29b

Please sign in to comment.