Skip to content

Commit

Permalink
Fix previous commit #824
Browse files Browse the repository at this point in the history
Co-Authored-By: Vassil Vassilev <[email protected]>
Co-Authored-By: Alexander Penev <[email protected]>
  • Loading branch information
3 people committed Mar 31, 2024
1 parent a37878b commit bffdcac
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Clad also provides certain flags to save and print the generated derivative code
- To print the Clad generated derivative: `-Xclang -plugin-arg-clad -Xclang -fdump-derived-fn`

## How to install
At the moment, LLVM/Clang 8.0.x - 18.x.x are supported.
At the moment, LLVM/Clang 8.0.x - 18.1.x are supported.

### Conda Installation

Expand Down
20 changes: 11 additions & 9 deletions include/clad/Differentiator/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ using namespace llvm;
//Clang 18 ArrayType::Normal -> ArraySizeModifier::Normal

#if LLVM_VERSION_MAJOR < 18
#define ArrayType_Normal ArrayType::Normal
const auto ArraySizeModifier_Normal = clang::ArrayType::Normal;
#else
#define ArrayType_Normal ArraySizeModifier::Normal
const auto ArraySizeModifier_Normal = clang::ArraySizeModifier::Normal;
#endif

// Compatibility helper function for creation UnresolvedLookupExpr.
Expand Down Expand Up @@ -98,9 +98,9 @@ static inline Stmt* UnresolvedLookupExpr_Create(const ASTContext &Ctx,
//Clang 18 ETK_None -> ElaboratedTypeKeyword::None

#if LLVM_VERSION_MAJOR < 18
#define ElaboratedTypeKeyword_None ETK_None
const auto ElaboratedTypeKeyword_None = ETK_None;
#else
#define ElaboratedTypeKeyword_None ElaboratedTypeKeyword::None
const auto ElaboratedTypeKeyword_None = ElaboratedTypeKeyword::None;
#endif

//Clang 18 endswith->ends_with
Expand Down Expand Up @@ -708,13 +708,15 @@ static inline bool IsPRValue(const Expr* E) { return E->isPRValue(); }
// Clang 18 renamed clang::StringLiteral::StringKind::Ordinary became clang::StringLiteralKind::Ordinary;

#if CLANG_VERSION_MAJOR < 15
const auto StringKind_Ordinary = clang::StringLiteral::StringKind::Ascii;
const auto StringLiteralKind_Ordinary = clang::StringLiteral::StringKind::Ascii;
#elif CLANG_VERSION_MAJOR >= 15
#if CLANG_VERSION_MAJOR < 18
const auto StringKind_Ordinary = clang::StringLiteral::StringKind::Ordinary;
#else
const auto StringKind_Ordinary = clang::StringLiteralKind::Ordinary;
#endif
const auto StringLiteralKind_Ordinary =
clang::StringLiteral::StringKind::Ordinary;
#else
const auto StringLiteralKind_Ordinary =
clang::StringLiteral::StringKind::Ordinary;
#endif
#endif

// Clang 15 add one extra param to Sema::CheckFunctionDeclaration
Expand Down
7 changes: 4 additions & 3 deletions lib/Differentiator/BaseForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1726,8 +1726,9 @@ BaseForwardModeVisitor::DeriveSwitchStmtBodyHelper(const Stmt* stmt,
(newCaseSC->getLHS() ? Clone(newCaseSC->getLHS()) : nullptr);
Expr* rhsClone =
(newCaseSC->getRHS() ? Clone(newCaseSC->getRHS()) : nullptr);
newActiveSC = CaseStmt::Create(const_cast<ASTContext&>(m_Sema.getASTContext()), lhsClone, rhsClone, noLoc, noLoc, noLoc);

newActiveSC = CaseStmt::Create(m_Sema.getASTContext(), lhsClone, rhsClone,
noLoc, noLoc, noLoc);

} else if (isa<DefaultStmt>(SC)) {
newActiveSC =
new (m_Sema.getASTContext()) DefaultStmt(noLoc, noLoc, nullptr);
Expand Down Expand Up @@ -1993,4 +1994,4 @@ StmtDiff BaseForwardModeVisitor::VisitSubstNonTypeTemplateParmExpr(
const clang::SubstNonTypeTemplateParmExpr* NTTP) {
return Visit(NTTP->getReplacement());
}
} // end namespace clad
} // end namespace clad
22 changes: 11 additions & 11 deletions lib/Differentiator/CladUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ namespace clad {
utils::BuildNNS(semaRef, const_cast<clang::DeclContext*>(declContext), CSS);
NestedNameSpecifier* NS = CSS.getScopeRep();
if (auto* Prefix = NS->getPrefix())
return C.getElaboratedType(ElaboratedTypeKeyword_None, Prefix, QT);
return C.getElaboratedType(clad_compat::ElaboratedTypeKeyword_None,
Prefix, QT);
}
return QT;
}
Expand Down Expand Up @@ -268,16 +269,15 @@ namespace clad {
StringLiteral* CreateStringLiteral(ASTContext& C, llvm::StringRef str) {
// Copied and adapted from clang::Sema::ActOnStringLiteral.
QualType CharTyConst = C.CharTy.withConst();
QualType
StrTy = clad_compat::getConstantArrayType(C, CharTyConst,
llvm::APInt(/*numBits=*/32,
str.size() + 1),
/*SizeExpr=*/nullptr,
/*ASM=*/ArrayType_Normal,
/*IndexTypeQuals*/ 0);
StringLiteral* SL = StringLiteral::Create(C, str,
/*Kind=*/clad_compat::StringKind_Ordinary,
/*Pascal=*/false, StrTy, noLoc);
QualType StrTy = clad_compat::getConstantArrayType(
C, CharTyConst, llvm::APInt(/*numBits=*/32, str.size() + 1),
/*SizeExpr=*/nullptr,
/*ASM=*/clad_compat::ArraySizeModifier_Normal,
/*IndexTypeQuals*/ 0);
StringLiteral* SL = StringLiteral::Create(
C, str,
/*Kind=*/clad_compat::StringLiteralKind_Ordinary,
/*Pascal=*/false, StrTy, noLoc);
return SL;
}

Expand Down
18 changes: 8 additions & 10 deletions lib/Differentiator/HessianModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ namespace clad {
static const StringLiteral* CreateStringLiteral(ASTContext& C,
std::string str) {
QualType CharTyConst = C.CharTy.withConst();
QualType StrTy =
clad_compat::getConstantArrayType(C, CharTyConst,
llvm::APInt(/*numBits=*/32,
str.size() + 1),
/*SizeExpr=*/nullptr,
/*ASM=*/ArrayType_Normal,
/*IndexTypeQuals*/ 0);
const StringLiteral* SL =
StringLiteral::Create(C, str, /*Kind=*/clad_compat::StringKind_Ordinary,
/*Pascal=*/false, StrTy, noLoc);
QualType StrTy = clad_compat::getConstantArrayType(
C, CharTyConst, llvm::APInt(/*numBits=*/32, str.size() + 1),
/*SizeExpr=*/nullptr,
/*ASM=*/clad_compat::ArraySizeModifier_Normal,
/*IndexTypeQuals*/ 0);
const StringLiteral* SL = StringLiteral::Create(
C, str, /*Kind=*/clad_compat::StringLiteralKind_Ordinary,
/*Pascal=*/false, StrTy, noLoc);
return SL;
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3443,7 +3443,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
Expr* lhsClone = (CS->getLHS() ? Clone(CS->getLHS()) : nullptr);
Expr* rhsClone = (CS->getRHS() ? Clone(CS->getRHS()) : nullptr);

auto* newSC = CaseStmt::Create(const_cast<ASTContext&>(m_Sema.getASTContext()), lhsClone, rhsClone, noLoc, noLoc, noLoc);
auto* newSC = CaseStmt::Create(m_Sema.getASTContext(), lhsClone, rhsClone,
noLoc, noLoc, noLoc);

Expr* ifCond = BuildOp(BinaryOperatorKind::BO_EQ, newSC->getLHS(),
SSData->switchStmtCond);
Expand Down Expand Up @@ -3625,7 +3626,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
CaseStmt* ReverseModeVisitor::BreakContStmtHandler::GetNextCFCaseStmt() {
++m_CaseCounter;
auto* counterLiteral = CreateSizeTLiteralExpr(m_CaseCounter);
CaseStmt* CS = CaseStmt::Create(const_cast<ASTContext&>(m_RMV.m_Context), counterLiteral, nullptr, noLoc, noLoc, noLoc);
CaseStmt* CS = CaseStmt::Create(m_RMV.m_Context, counterLiteral, nullptr,
noLoc, noLoc, noLoc);

// Initialise switch case statements with null statement because it is
// necessary for switch case statements to have a substatement but it
Expand Down
4 changes: 3 additions & 1 deletion lib/Differentiator/StmtClone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ Stmt* StmtClone::VisitShuffleVectorExpr(ShuffleVectorExpr* Node) {
}

Stmt* StmtClone::VisitCaseStmt(CaseStmt* Node) {
CaseStmt* result = CaseStmt::Create(const_cast<ASTContext&>(Ctx), Clone(Node->getLHS()), Clone(Node->getRHS()), Node->getCaseLoc(), Node->getEllipsisLoc(), Node->getColonLoc());
CaseStmt* result = CaseStmt::Create(
Ctx, Clone(Node->getLHS()), Clone(Node->getRHS()), Node->getCaseLoc(),
Node->getEllipsisLoc(), Node->getColonLoc());

Check warning on line 407 in lib/Differentiator/StmtClone.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Differentiator/StmtClone.cpp#L405-L407

Added lines #L405 - L407 were not covered by tests
result->setSubStmt(Clone(Node->getSubStmt()));
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Differentiator/VisitorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ namespace clad {

// Create elaborated type with namespace specifier,
// i.e. class<T> -> clad::class<T>
return m_Context.getElaboratedType(ElaboratedTypeKeyword_None, NS, TT);
return m_Context.getElaboratedType(clad_compat::ElaboratedTypeKeyword_None,
NS, TT);
}

QualType VisitorBase::InstantiateTemplate(TemplateDecl* CladClassDecl,
Expand Down
8 changes: 4 additions & 4 deletions tools/ClangPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ namespace clad {
// Find the path to clad.
llvm::StringRef CladSoPath;
for (llvm::StringRef P : Opts.Plugins)
if (llvm::sys::path::stem(P).ends_with("clad")) {
CladSoPath = P;
break;
}
if (llvm::sys::path::stem(P).ends_with("clad")) {
CladSoPath = P;
break;
}

// Register clad as a backend pass.
CodeGenOptions& CGOpts = CI.getCodeGenOpts();
Expand Down

0 comments on commit bffdcac

Please sign in to comment.