Skip to content

Commit

Permalink
Merge pull request #511 from andreasfertig/clang15
Browse files Browse the repository at this point in the history
Clang-15 support
  • Loading branch information
andreasfertig authored Jan 7, 2023
2 parents 81fe4e1 + 9d111ee commit 7dfa882
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion ClangCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
47 changes: 38 additions & 9 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ static void AddBodyStmts(std::vector<Stmt*>& v, Stmt* body)
}
//-----------------------------------------------------------------------------

namespace asthelpers {
CompoundStmt* mkCompoundStmt(ArrayRef<Stmt*> bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {});
}

void CodeGenerator::InsertArg(const CXXForRangeStmt* rangeForStmt)
{
auto& langOpts{GetLangOpts(*rangeForStmt->getLoopVariable())};
Expand Down Expand Up @@ -317,7 +321,7 @@ void CodeGenerator::InsertArg(const CXXForRangeStmt* rangeForStmt)

ArrayRef<Stmt*> 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,
Expand All @@ -333,7 +337,7 @@ void CodeGenerator::InsertArg(const CXXForRangeStmt* rangeForStmt)

ArrayRef<Stmt*> outerScopeStmtsRef{outerScopeStmts};
auto* outerScope =
CompoundStmt::Create(ctx, outerScopeStmtsRef, rangeForStmt->getBeginLoc(), rangeForStmt->getEndLoc());
asthelpers::mkCompoundStmt(outerScopeStmtsRef, rangeForStmt->getBeginLoc(), rangeForStmt->getEndLoc());

InsertArg(outerScope);

Expand Down Expand Up @@ -1916,7 +1920,7 @@ void CodeGenerator::InsertArg(const ForStmt* stmt)
}();

ArrayRef<Stmt*> 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());

Expand All @@ -1925,7 +1929,7 @@ void CodeGenerator::InsertArg(const ForStmt* stmt)
AddStmt(outerScopeStmts, whileStmt);

ArrayRef<Stmt*> 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();
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}

Expand Down
28 changes: 21 additions & 7 deletions CoroutinesCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ auto* mkLabelStmt(std::string_view name)
return new(GetGlobalAST()) LabelStmt({}, mkLabelDecl(name), nullptr);
}

auto* mkCompoundStmt(ArrayRef<Stmt*> bodyStmts)
CompoundStmt* mkCompoundStmt(ArrayRef<Stmt*> 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<Stmt*> bodyStmts)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<VarDecl*>(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<VarDecl*>(retVd));
auto* returnStmt = asthelpers::mkReturnStmt(coroReturnDref);
InsertArg(returnStmt);
}
#endif
}

mOutputFormatHelper.AppendSemiNewLine();
Expand Down

0 comments on commit 7dfa882

Please sign in to comment.