From 9d111ee090f94cbe8bed7afaae882da17a9c7967 Mon Sep 17 00:00:00 2001 From: Andreas Fertig Date: Wed, 28 Sep 2022 22:11:35 +0200 Subject: [PATCH] Clang-15 support Linking against a new lib (libClangSupport) is required. --- CMakeLists.txt | 9 ++++++- ClangCompat.h | 2 +- CodeGenerator.cpp | 47 ++++++++++++++++++++++++++++++------- CoroutinesCodeGenerator.cpp | 28 ++++++++++++++++------ 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f7ca02bc..8a6931d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -250,7 +250,7 @@ if (BUILD_INSIGHTS_OUTSIDE_LLVM) elseif(APPLE) string(REPLACE ".dylib" "" LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS2}") string(REPLACE "/usr/lib/lib" "-l" LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS}") - set(LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS}") + set(LLVM_SYSTEM_LIBS ${LLVM_SYSTEM_LIBS}) else() set(LLVM_SYSTEM_LIBS ${LLVM_SYSTEM_LIBS2}) endif() @@ -379,6 +379,13 @@ if (BUILD_INSIGHTS_OUTSIDE_LLVM) ${LLVM_SYSTEM_LIBS} ) + if(${LLVM_PACKAGE_VERSION_PLAIN} VERSION_GREATER_EQUAL "15.0.0") + set(ADDITIONAL_LIBS + ${ADDITIONAL_LIBS} + clangSupport + ) + endif() + elseif(NOT DEFINED LLVM_VERSION_MAJOR) # used when build inside the clang tool/extra folder message(FATAL_ERROR "Neither in LLVM directory nor BUILD_INSIGHTS_OUTSIDE_LLVM is set") diff --git a/ClangCompat.h b/ClangCompat.h index d8286603..779c03f0 100644 --- a/ClangCompat.h +++ b/ClangCompat.h @@ -13,7 +13,7 @@ #include "version.h" //----------------------------------------------------------------------------- -#define IS_CLANG_NEWER_THAN(major) CLANG_VERSION_MAJOR > (major) +#define IS_CLANG_NEWER_THAN(major) (CLANG_VERSION_MAJOR > (major)) //----------------------------------------------------------------------------- #include "clang/AST/ExprCXX.h" diff --git a/CodeGenerator.cpp b/CodeGenerator.cpp index 87abab65..c2bfdcdd 100644 --- a/CodeGenerator.cpp +++ b/CodeGenerator.cpp @@ -271,6 +271,10 @@ static void AddBodyStmts(std::vector& v, Stmt* body) } //----------------------------------------------------------------------------- +namespace asthelpers { +CompoundStmt* mkCompoundStmt(ArrayRef bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {}); +} + void CodeGenerator::InsertArg(const CXXForRangeStmt* rangeForStmt) { auto& langOpts{GetLangOpts(*rangeForStmt->getLoopVariable())}; @@ -317,7 +321,7 @@ void CodeGenerator::InsertArg(const CXXForRangeStmt* rangeForStmt) ArrayRef innerScopeStmtsRef{bodyStmts}; auto* innerScope = - CompoundStmt::Create(ctx, innerScopeStmtsRef, rangeForStmt->getBeginLoc(), rangeForStmt->getEndLoc()); + asthelpers::mkCompoundStmt(innerScopeStmtsRef, rangeForStmt->getBeginLoc(), rangeForStmt->getEndLoc()); auto* forStmt = new(ctx) ForStmt(ctx, declStmt, @@ -333,7 +337,7 @@ void CodeGenerator::InsertArg(const CXXForRangeStmt* rangeForStmt) ArrayRef outerScopeStmtsRef{outerScopeStmts}; auto* outerScope = - CompoundStmt::Create(ctx, outerScopeStmtsRef, rangeForStmt->getBeginLoc(), rangeForStmt->getEndLoc()); + asthelpers::mkCompoundStmt(outerScopeStmtsRef, rangeForStmt->getBeginLoc(), rangeForStmt->getEndLoc()); InsertArg(outerScope); @@ -1916,7 +1920,7 @@ void CodeGenerator::InsertArg(const ForStmt* stmt) }(); ArrayRef bodyStmtsRef{bodyStmts}; - auto* outerBody = CompoundStmt::Create(ctx, bodyStmtsRef, stmt->getBeginLoc(), stmt->getEndLoc()); + auto* outerBody = asthelpers::mkCompoundStmt(bodyStmtsRef, stmt->getBeginLoc(), stmt->getEndLoc()); auto* whileStmt = WhileStmt::Create( ctx, nullptr, condition, outerBody, stmt->getBeginLoc(), stmt->getLParenLoc(), stmt->getRParenLoc()); @@ -1925,7 +1929,7 @@ void CodeGenerator::InsertArg(const ForStmt* stmt) AddStmt(outerScopeStmts, whileStmt); ArrayRef outerScopeStmtsRef{outerScopeStmts}; - auto* outerScopeBody = CompoundStmt::Create(ctx, outerScopeStmtsRef, stmt->getBeginLoc(), stmt->getEndLoc()); + auto* outerScopeBody = asthelpers::mkCompoundStmt(outerScopeStmtsRef, stmt->getBeginLoc(), stmt->getEndLoc()); InsertArg(outerScopeBody); mOutputFormatHelper.AppendNewLine(); @@ -3269,7 +3273,14 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt) CodeGenerator codeGenerator{ofm, LambdaInInitCapture::Yes}; codeGenerator.InsertArg(expr); - mOutputFormatHelper.InsertAt(insertPosBeforeCtor.getValueOr(-1), ofm); + mOutputFormatHelper.InsertAt(insertPosBeforeCtor. +#if IS_CLANG_NEWER_THAN(14) + value_or +#else + getValueOr +#endif + (-1), + ofm); } } else { if(isThis and not fieldDeclType->isPointerType()) { @@ -3496,13 +3507,31 @@ void CodeGenerator::InsertArg(const CXXStdInitializerListExpr* stmt) RETURN_IF(not mCurrentPos.hasValue() and not mCurrentFieldPos.hasValue() and not mCurrentReturnPos.hasValue()); std::string modifiers{}; - size_t variableInsertPos = mCurrentReturnPos.getValueOr(mCurrentPos.getValueOr(0)); + size_t variableInsertPos = mCurrentReturnPos. +#if IS_CLANG_NEWER_THAN(14) + value_or +#else + getValueOr +#endif + (mCurrentPos. +#if IS_CLANG_NEWER_THAN(14) + value_or +#else + getValueOr +#endif + (0)); auto& ofmToInsert = [&]() -> decltype(auto) { if(not mCurrentPos.hasValue() and not mCurrentReturnPos.hasValue()) { - variableInsertPos = mCurrentFieldPos.getValueOr(0); - mCurrentPos = variableInsertPos; - modifiers = StrCat(kwStaticSpace, kwInlineSpace); + variableInsertPos = mCurrentFieldPos. +#if IS_CLANG_NEWER_THAN(14) + value_or +#else + getValueOr +#endif + (0); + mCurrentPos = variableInsertPos; + modifiers = StrCat(kwStaticSpace, kwInlineSpace); return (*mOutputFormatHelperOutside); } diff --git a/CoroutinesCodeGenerator.cpp b/CoroutinesCodeGenerator.cpp index e267a867..c5c8746e 100644 --- a/CoroutinesCodeGenerator.cpp +++ b/CoroutinesCodeGenerator.cpp @@ -50,9 +50,15 @@ auto* mkLabelStmt(std::string_view name) return new(GetGlobalAST()) LabelStmt({}, mkLabelDecl(name), nullptr); } -auto* mkCompoundStmt(ArrayRef bodyStmts) +CompoundStmt* mkCompoundStmt(ArrayRef bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {}) { - return CompoundStmt::Create(GetGlobalAST(), bodyStmts, {}, {}); + return CompoundStmt::Create(GetGlobalAST(), + bodyStmts, +#if IS_CLANG_NEWER_THAN(14) + FPOptionsOverride{}, +#endif + beginLoc, + endLoc); } auto* mkIfStmt(Expr* condition, ArrayRef bodyStmts) @@ -649,8 +655,10 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro } // P0057R8: [dcl.fct.def.coroutine] p5: before initial_suspend and at tops 1 +#if not IS_CLANG_NEWER_THAN(14) mOutputFormatHelper.AppendNewLine(); InsertArg(stmt->getResultDecl()); +#endif // Make a call the the made up state machine function for the initial suspend mOutputFormatHelper.AppendNewLine(); @@ -688,15 +696,21 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro mOutputFormatHelper.AppendNewLine(); mOutputFormatHelper.AppendNewLine(); - // XXX: The getReturnStmt may contain more as just __coro_gro + // XXX: The getReturnStmt may contain more as just __coro_gro pre Clang 15 SKIP_NAME_PREFIX_FOR_SCOPE { +#if IS_CLANG_NEWER_THAN(14) + InsertArg(stmt->getReturnStmt()); +#else DeclsHolder tmpDeclsHolder{}; tmpDeclsHolder.FindAllVarDecls(stmt->getResultDecl()); - const auto* retVd = tmpDeclsHolder.mMoveDecls.at(0); - auto* coroReturnDref = asthelpers::mkDeclRefExpr(const_cast(retVd)); - auto* returnStmt = asthelpers::mkReturnStmt(coroReturnDref); - InsertArg(returnStmt); + if(tmpDeclsHolder.mMoveDecls.size() > 0) { + const auto* retVd = tmpDeclsHolder.mMoveDecls.at(0); + auto* coroReturnDref = asthelpers::mkDeclRefExpr(const_cast(retVd)); + auto* returnStmt = asthelpers::mkReturnStmt(coroReturnDref); + InsertArg(returnStmt); + } +#endif } mOutputFormatHelper.AppendSemiNewLine();