Skip to content

Commit 9147b89

Browse files
Merge pull request #568 from andreasfertig/clang16
Clang 16 support.
2 parents 317b43c + f2ee98e commit 9147b89

File tree

7 files changed

+103
-61
lines changed

7 files changed

+103
-61
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ if (BUILD_INSIGHTS_OUTSIDE_LLVM)
258258
llvm_config(LLVM_PACKAGE_VERSION "--version")
259259
string(REPLACE "git" "" LLVM_PACKAGE_VERSION_PLAIN "${LLVM_PACKAGE_VERSION}")
260260

261+
# With Clang 16 the location (path) of the include folder changed. Previously it was 15.0.0 now it is 16.
262+
if(${LLVM_PACKAGE_VERSION_PLAIN} VERSION_GREATER_EQUAL "15.0.0")
263+
string(REGEX REPLACE "([0-9]+).[0-9]+.[0-9]+" "\\1" LLVM_PACKAGE_VERSION_MAJOR_PLAIN "${LLVM_PACKAGE_VERSION_PLAIN}")
264+
else()
265+
set(LLVM_PACKAGE_VERSION_MAJOR_PLAIN "${LLVM_PACKAGE_VERSION_PLAIN}")
266+
endif()
267+
261268
# always generate the compile commands
262269
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
263270
if(WIN32)

CodeGenerator.cpp

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,21 @@ void CodeGenerator::InsertArg(const WhileStmt* stmt)
546546

547547
/// Get the name of a \c FieldDecl in case this \c FieldDecl is part of a lambda. The name has to be retrieved from the
548548
/// capture fields or can be \c __this.
549-
static Optional<std::string> GetFieldDeclNameForLambda(const FieldDecl& fieldDecl, const CXXRecordDecl& cxxRecordDecl)
549+
static std::optional<std::string> GetFieldDeclNameForLambda(const FieldDecl& fieldDecl,
550+
const CXXRecordDecl& cxxRecordDecl)
550551
{
551552
if(cxxRecordDecl.isLambda()) {
552-
llvm::DenseMap<const VarDecl*, FieldDecl*> captures{};
553-
FieldDecl* thisCapture{};
553+
llvm::DenseMap<const
554+
#if IS_CLANG_NEWER_THAN(15)
555+
ValueDecl*
556+
#else
557+
VarDecl*
558+
#endif
559+
,
560+
FieldDecl*>
561+
captures{};
562+
563+
FieldDecl* thisCapture{};
554564

555565
cxxRecordDecl.getCaptureFields(captures, thisCapture);
556566

@@ -608,7 +618,7 @@ void CodeGenerator::InsertArg(const MemberExpr* stmt)
608618
else if(const auto* fd = dyn_cast_or_null<FieldDecl>(meDecl)) {
609619
if(const auto* cxxRecordDecl = dyn_cast_or_null<CXXRecordDecl>(fd->getParent())) {
610620
if(const auto& fieldName = GetFieldDeclNameForLambda(*fd, *cxxRecordDecl)) {
611-
return fieldName.getValue();
621+
return fieldName.value();
612622
}
613623
}
614624
}
@@ -2105,7 +2115,7 @@ void CodeGenerator::InsertArg(const CXXNewExpr* stmt)
21052115
CodeGenerator codeGenerator{ofm};
21062116

21072117
ofm.Append("["sv);
2108-
codeGenerator.InsertArg(stmt->getArraySize().getValue());
2118+
codeGenerator.InsertArg(stmt->getArraySize().value());
21092119
ofm.Append(']');
21102120

21112121
// In case of multi dimension the first dimension is the getArraySize() while the others are part of the
@@ -2761,7 +2771,7 @@ void CodeGenerator::InsertArg(const FieldDecl* stmt)
27612771
std::string name{GetName(*stmt)};
27622772

27632773
if(const auto fieldName = GetFieldDeclNameForLambda(*stmt, *cxxRecordDecl)) {
2764-
name = std::move(fieldName.getValue());
2774+
name = std::move(fieldName.value());
27652775
}
27662776

27672777
mOutputFormatHelper.Append(GetTypeNameAsParameter(stmt->getType(), name));
@@ -3228,11 +3238,11 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
32283238

32293239
mCurrentFieldPos = mOutputFormatHelper.CurrentPos();
32303240

3231-
OnceTrue firstRecordDecl{};
3232-
OnceTrue firstDecl{};
3233-
Decl::Kind formerKind{};
3234-
llvm::Optional<size_t> insertPosBeforeCtor{};
3235-
AccessSpecifier lastAccess{stmt->isClass() ? AS_private : AS_public};
3241+
OnceTrue firstRecordDecl{};
3242+
OnceTrue firstDecl{};
3243+
Decl::Kind formerKind{};
3244+
std::optional<size_t> insertPosBeforeCtor{};
3245+
AccessSpecifier lastAccess{stmt->isClass() ? AS_private : AS_public};
32363246
for(const auto* d : stmt->decls()) {
32373247
if(isa<CXXRecordDecl>(d) and firstRecordDecl) {
32383248
continue;
@@ -3261,7 +3271,7 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
32613271

32623272
InsertArg(d);
32633273

3264-
if(lastAccess == AS_public and not insertPosBeforeCtor.hasValue()) {
3274+
if(lastAccess == AS_public and not insertPosBeforeCtor.has_value()) {
32653275
insertPosBeforeCtor = mOutputFormatHelper.CurrentPos();
32663276
}
32673277

@@ -3409,14 +3419,7 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
34093419
CodeGenerator codeGenerator{ofm, LambdaInInitCapture::Yes};
34103420
codeGenerator.InsertArg(expr);
34113421

3412-
mOutputFormatHelper.InsertAt(insertPosBeforeCtor.
3413-
#if IS_CLANG_NEWER_THAN(14)
3414-
value_or
3415-
#else
3416-
getValueOr
3417-
#endif
3418-
(-1),
3419-
ofm);
3422+
mOutputFormatHelper.InsertAt(insertPosBeforeCtor.value_or(-1), ofm);
34203423
}
34213424
} else {
34223425
if(isThis and not fieldDeclType->isPointerType()) {
@@ -3429,8 +3432,16 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
34293432
mOutputFormatHelper.Append(GetTypeNameAsParameter(fieldDeclType, StrCat("_"sv, name)));
34303433
};
34313434

3432-
llvm::DenseMap<const VarDecl*, FieldDecl*> captures{};
3433-
FieldDecl* thisCapture{};
3435+
llvm::DenseMap<const
3436+
#if IS_CLANG_NEWER_THAN(15)
3437+
ValueDecl*
3438+
#else
3439+
VarDecl*
3440+
#endif
3441+
,
3442+
FieldDecl*>
3443+
captures{};
3444+
FieldDecl* thisCapture{};
34343445

34353446
stmt->getCaptureFields(captures, thisCapture);
34363447

@@ -3451,8 +3462,15 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
34513462

34523463
const auto* capturedVar = c.getCapturedVar();
34533464
if(const auto* value = captures[capturedVar]) {
3454-
addToInits(
3455-
GetName(*capturedVar), value, false, cinit, VarDecl::ListInit == capturedVar->getInitStyle());
3465+
addToInits(GetName(*capturedVar),
3466+
value,
3467+
false,
3468+
cinit,
3469+
#if IS_CLANG_NEWER_THAN(15)
3470+
VarDecl::ListInit == dyn_cast_or_null<VarDecl>(capturedVar)->getInitStyle());
3471+
#else
3472+
VarDecl::ListInit == capturedVar->getInitStyle());
3473+
#endif
34563474
}
34573475
}
34583476

@@ -3630,7 +3648,11 @@ void CodeGenerator::InsertArg(const RequiresExpr* stmt)
36303648
} else if(const auto* nestedRequirement = dyn_cast_or_null<concepts::NestedRequirement>(requirement)) {
36313649
mOutputFormatHelper.Append(kwRequiresSpace);
36323650

3651+
#if IS_CLANG_NEWER_THAN(15)
3652+
if(nestedRequirement->hasInvalidConstraint()) {
3653+
#else
36333654
if(nestedRequirement->isSubstitutionFailure()) {
3655+
#endif
36343656
// The requirement failed. We need some way to express that. Using a nested
36353657
// requirement with false seems to be the simplest solution.
36363658
mOutputFormatHelper.Append("false");
@@ -3655,34 +3677,17 @@ void CodeGenerator::InsertArg(const CXXDefaultArgExpr* stmt)
36553677
void CodeGenerator::InsertArg(const CXXStdInitializerListExpr* stmt)
36563678
{
36573679
if(GetInsightsOptions().UseShowInitializerList) {
3658-
RETURN_IF(not mCurrentPos.hasValue() and not mCurrentFieldPos.hasValue() and not mCurrentReturnPos.hasValue());
3680+
RETURN_IF(not mCurrentPos.has_value() and not mCurrentFieldPos.has_value() and
3681+
not mCurrentReturnPos.has_value());
36593682

36603683
std::string modifiers{};
3661-
size_t variableInsertPos = mCurrentReturnPos.
3662-
#if IS_CLANG_NEWER_THAN(14)
3663-
value_or
3664-
#else
3665-
getValueOr
3666-
#endif
3667-
(mCurrentPos.
3668-
#if IS_CLANG_NEWER_THAN(14)
3669-
value_or
3670-
#else
3671-
getValueOr
3672-
#endif
3673-
(0));
3684+
size_t variableInsertPos = mCurrentReturnPos.value_or(mCurrentPos.value_or(0));
36743685

36753686
auto& ofmToInsert = [&]() -> decltype(auto) {
3676-
if(not mCurrentPos.hasValue() and not mCurrentReturnPos.hasValue()) {
3677-
variableInsertPos = mCurrentFieldPos.
3678-
#if IS_CLANG_NEWER_THAN(14)
3679-
value_or
3680-
#else
3681-
getValueOr
3682-
#endif
3683-
(0);
3684-
mCurrentPos = variableInsertPos;
3685-
modifiers = StrCat(kwStaticSpace, kwInlineSpace);
3687+
if(not mCurrentPos.has_value() and not mCurrentReturnPos.has_value()) {
3688+
variableInsertPos = mCurrentFieldPos.value_or(0);
3689+
mCurrentPos = variableInsertPos;
3690+
modifiers = StrCat(kwStaticSpace, kwInlineSpace);
36863691
return (*mOutputFormatHelperOutside);
36873692
}
36883693

@@ -3716,10 +3721,10 @@ void CodeGenerator::InsertArg(const CXXStdInitializerListExpr* stmt)
37163721
mOutputFormatHelper.Append(
37173722
GetName(stmt->getType(), Unqualified::Yes), "{"sv, internalListName, ", "sv, size, "}"sv);
37183723

3719-
if(mCurrentReturnPos.hasValue()) {
3720-
mCurrentReturnPos = mCurrentReturnPos.getValue() + ofm.size();
3724+
if(mCurrentReturnPos.has_value()) {
3725+
mCurrentReturnPos = mCurrentReturnPos.value() + ofm.size();
37213726
} else {
3722-
mCurrentPos = mCurrentPos.getValue() + ofm.size();
3727+
mCurrentPos = mCurrentPos.value() + ofm.size();
37233728
}
37243729

37253730
} else {

CodeGenerator.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "clang/Rewrite/Core/Rewriter.h"
1515
#include "llvm/ADT/APInt.h"
1616

17+
#include <optional>
18+
1719
#include "ClangCompat.h"
1820
#include "InsightsStaticStrings.h"
1921
#include "InsightsStrongTypes.h"
@@ -407,12 +409,12 @@ class CodeGenerator
407409
static constexpr auto MAX_FILL_VALUES_FOR_ARRAYS{
408410
uint64_t{100}}; //!< This is the upper limit of elements which will be shown for an array when filled by \c
409411
//!< FillConstantArray.
410-
llvm::Optional<size_t> mCurrentPos{}; //!< The position in mOutputFormatHelper where a potential
411-
//!< std::initializer_list expansion must be inserted.
412-
llvm::Optional<size_t> mCurrentReturnPos{}; //!< The position in mOutputFormatHelper from a return where a
413-
//!< potential std::initializer_list expansion must be inserted.
414-
llvm::Optional<size_t> mCurrentFieldPos{}; //!< The position in mOutputFormatHelper in a class where where a
415-
//!< potential std::initializer_list expansion must be inserted.
412+
std::optional<size_t> mCurrentPos{}; //!< The position in mOutputFormatHelper where a potential
413+
//!< std::initializer_list expansion must be inserted.
414+
std::optional<size_t> mCurrentReturnPos{}; //!< The position in mOutputFormatHelper from a return where a
415+
//!< potential std::initializer_list expansion must be inserted.
416+
std::optional<size_t> mCurrentFieldPos{}; //!< The position in mOutputFormatHelper in a class where where a
417+
//!< potential std::initializer_list expansion must be inserted.
416418
OutputFormatHelper* mOutputFormatHelperOutside{
417419
nullptr}; //!< Helper output buffer for std::initializer_list expansion.
418420
bool mRequiresImplicitReturnZero{}; //!< Track whether this is a function with an imlpicit return 0.

CoroutinesCodeGenerator.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,18 @@ auto* mkFunctionDecl(std::string_view name, QualType returnType, const params_ve
171171
auto* mkStdFunctionDecl(std::string_view name, QualType returnType, const params_vector& parameters)
172172
{
173173
auto& ctx = GetGlobalAST();
174-
NamespaceDecl* stdNs = NamespaceDecl::Create(
175-
const_cast<ASTContext&>(ctx), ctx.getTranslationUnitDecl(), false, {}, {}, &ctx.Idents.get("std"), nullptr);
174+
NamespaceDecl* stdNs = NamespaceDecl::Create(const_cast<ASTContext&>(ctx),
175+
ctx.getTranslationUnitDecl(),
176+
false,
177+
{},
178+
{},
179+
&ctx.Idents.get("std"),
180+
nullptr
181+
#if IS_CLANG_NEWER_THAN(15)
182+
,
183+
false
184+
#endif
185+
);
176186

177187
return mkFunctionDeclBase(name, returnType, parameters, stdNs);
178188
}
@@ -950,7 +960,11 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro
950960
false,
951961
{asRef},
952962
SourceRange{},
963+
#if IS_CLANG_NEWER_THAN(15)
964+
std::optional<Expr*>{},
965+
#else
953966
Optional<Expr*>{},
967+
#endif
954968
CXXNewExpr::ListInit,
955969
ctorArgs,
956970
ctx.getPointerType(mASTData.mPromiseField->getType()),

Insights.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ int main(int argc, const char** argv)
250250
if(gUseLibCpp) {
251251
prependArgument(INSIGHTS_LLVM_INCLUDE_DIR);
252252
prependArgument("-stdlib=libc++");
253+
254+
#if IS_CLANG_NEWER_THAN(15)
255+
prependArgument("-fexperimental-library");
256+
#endif
257+
253258
#ifdef __APPLE__
254259
prependArgument("-nostdinc++"); // macos Monterey
255260
#endif /* __APPLE__ */

TemplateHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ static SourceLocation FindLocationAfterRBrace(const SourceLocation
139139
if(not mustHaveSemi) {
140140
auto nToken = clang::Lexer::findNextToken(loc, GetSM(result), result.Context->getLangOpts());
141141

142-
if(nToken.hasValue()) {
143-
if(auto& rToken = nToken.getValue(); rToken.getKind() == tok::semi) {
142+
if(nToken.has_value()) {
143+
if(auto& rToken = nToken.value(); rToken.getKind() == tok::semi) {
144144
return rToken.getLocation();
145145
}
146146
}

version.h.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77

88
#define INSIGHTS_MIN_LLVM_MAJOR_VERSION @INSIGHTS_MIN_LLVM_MAJOR_VERSION@
99

10+
11+
#include <clang/Basic/Version.h>
12+
1013
// Build Clang's resource dir and include dir by hand. This is necessary, as we do not always have Clang installed.
1114
// See: https://github.com/MaskRay/ccls/wiki/Install#clang-resource-directory
15+
#if CLANG_VERSION_MAJOR > 15
16+
#define INSIGHTS_CLANG_RESOURCE_DIR R"(-resource-dir=@LLVM_LIBDIR@/clang/@LLVM_PACKAGE_VERSION_MAJOR_PLAIN@)"
17+
#define INSIGHTS_CLANG_RESOURCE_INCLUDE_DIR R"(-I @LLVM_LIBDIR@/clang/@LLVM_PACKAGE_VERSION_MAJOR_PLAIN@/include)"
18+
#else
1219
#define INSIGHTS_CLANG_RESOURCE_DIR R"(-resource-dir=@LLVM_LIBDIR@/clang/@LLVM_PACKAGE_VERSION_PLAIN@)"
1320
#define INSIGHTS_CLANG_RESOURCE_INCLUDE_DIR R"(-I @LLVM_LIBDIR@/clang/@LLVM_PACKAGE_VERSION_PLAIN@/include)"
21+
#endif
22+
1423
#define INSIGHTS_LLVM_INCLUDE_DIR R"(-isystem@LLVM_INCLUDE_DIR@/c++/v1)"
1524

1625
#endif /* INSIGHTS_VERSION_H */

0 commit comments

Comments
 (0)