Skip to content

Commit

Permalink
Merge pull request #695 from andreasfertig/cleanupClangCompat
Browse files Browse the repository at this point in the history
Clean up compatibility with Clang 18 and earlier.
  • Loading branch information
andreasfertig authored Jan 13, 2025
2 parents f243226 + 6a7eb68 commit e25f555
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 104 deletions.
22 changes: 3 additions & 19 deletions ASTHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,19 +514,8 @@ CXXRecordDecl* Struct(std::string_view name)
auto getRecord = [&] {
auto& ctx = GetGlobalAST();

return CXXRecordDecl::Create(ctx,
#if IS_CLANG_NEWER_THAN(17)
TagTypeKind::Struct
#else
TTK_Struct
#endif
,
ctx.getTranslationUnitDecl(),
{},
{},
&ctx.Idents.get(name),
nullptr,
false);
return CXXRecordDecl::Create(
ctx, TagTypeKind::Struct, ctx.getTranslationUnitDecl(), {}, {}, &ctx.Idents.get(name), nullptr, false);
};

auto* rd = getRecord();
Expand Down Expand Up @@ -710,12 +699,7 @@ CXXNewExpr* New(ArrayRef<Expr*> placementArgs, const Expr* expr, QualType t)
placementArgs,
SourceRange{},
std::optional<Expr*>{},
#if IS_CLANG_NEWER_THAN(17)
CXXNewInitializationStyle::Parens
#else
CXXNewExpr::CallInit
#endif
,
CXXNewInitializationStyle::Parens,
const_cast<Expr*>(expr),
Ptr(t),
ctx.getTrivialTypeSourceInfo(t),
Expand Down
72 changes: 4 additions & 68 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,7 @@ class TemporaryDeclFinder : public StmtVisitor<TemporaryDeclFinder>
{},
&ctx.Idents.get(mTempName),
expr->getType(),
#if IS_CLANG_NEWER_THAN(17)
ImplicitParamKind::Other
#else
ImplicitParamDecl::Other
#endif
);
ImplicitParamKind::Other);

#endif

Expand Down Expand Up @@ -1208,17 +1203,7 @@ static bool IsPrimaryTemplatePackExpansionExpr(const ParenListExpr* stmt)

void CodeGenerator::InsertArg(const LinkageSpecDecl* stmt)
{
mOutputFormatHelper.Append("extern \"",
(
#if IS_CLANG_NEWER_THAN(17)
LinkageSpecLanguageIDs::C
#else
LinkageSpecDecl::lang_c
#endif
== stmt->getLanguage())
? "C"sv
: "C++"sv,
"\"");
mOutputFormatHelper.Append("extern \"", (LinkageSpecLanguageIDs::C == stmt->getLanguage()) ? "C"sv : "C++"sv, "\"");
mOutputFormatHelper.OpenScope();

for(const auto* decl : stmt->decls()) {
Expand Down Expand Up @@ -1752,26 +1737,14 @@ void CodeGenerator::InsertTemplateParameters(const TemplateParameterList& list,
if(tt->hasDefaultArgument() and not tt->defaultArgumentWasInherited()) {
const auto& defaultArg = tt->getDefaultArgument();

if(const auto decltypeType = dyn_cast_or_null<DecltypeType>(defaultArg
.
#if IS_CLANG_NEWER_THAN(18)
getArgument()
.getAsType()
#else
getTypePtrOrNull()
#endif
)) {
if(const auto decltypeType = dyn_cast_or_null<DecltypeType>(defaultArg.getArgument().getAsType())) {
mOutputFormatHelper.Append(hlpAssing);

InsertArg(decltypeType->getUnderlyingExpr());

} else {
mOutputFormatHelper.Append(hlpAssing);
InsertTemplateArg(defaultArg
#if IS_CLANG_NEWER_THAN(18)
.getArgument()
#endif
);
InsertTemplateArg(defaultArg.getArgument());
}
}

Expand All @@ -1788,11 +1761,7 @@ void CodeGenerator::InsertTemplateParameters(const TemplateParameterList& list,

if(nonTmplParam->hasDefaultArgument()) {
mOutputFormatHelper.Append(hlpAssing);
#if IS_CLANG_NEWER_THAN(18)
InsertTemplateArg(nonTmplParam->getDefaultArgument().getArgument());
#else
InsertArg(nonTmplParam->getDefaultArgument());
#endif
}
} else {
mOutputFormatHelper.Append(typeName, EllipsisSpace(nonTmplParam->isParameterPack()));
Expand Down Expand Up @@ -3048,7 +3017,6 @@ void CodeGenerator::InsertArg(const TypeAliasDecl* stmt)
mOutputFormatHelper.Append(kwUsingSpace, GetName(*stmt), hlpAssing);

if(auto* templateSpecializationType = underlyingType->getAs<TemplateSpecializationType>()) {
#if IS_CLANG_NEWER_THAN(18)
const bool carriesNamespace{[&] {
if(const auto tn = templateSpecializationType->getTemplateName();
(TemplateName::QualifiedTemplate == tn.getKind()) or (TemplateName::DependentTemplate == tn.getKind())) {
Expand All @@ -3066,11 +3034,6 @@ void CodeGenerator::InsertArg(const TypeAliasDecl* stmt)
InsertNamespace(elaboratedType->getQualifier());
}
}
#else
if(const auto* elaboratedType = underlyingType->getAs<ElaboratedType>()) {
InsertNamespace(elaboratedType->getQualifier());
}
#endif

StringStream stream{};
stream.Print(*templateSpecializationType);
Expand Down Expand Up @@ -3699,10 +3662,8 @@ void CodeGenerator::InsertAttribute(const Attr& attr)
// skip this attribute. Clang seems to tag final methods or classes with final
RETURN_IF(attr::Final == attr.getKind());

#if IS_CLANG_NEWER_THAN(17)
// skip this custom clang attribute
RETURN_IF(attr::NoInline == attr.getKind());
#endif

// Clang's printPretty misses the parameter pack ellipsis. Hence treat this special case here.
if(const auto* alignedAttr = dyn_cast_or_null<AlignedAttr>(&attr)) {
Expand Down Expand Up @@ -3740,12 +3701,6 @@ void CodeGenerator::InsertAttribute(const Attr& attr)

// attributes start with a space, skip it as it is not required for the first attribute
std::string_view start{stream.str()};
#if IS_CLANG_NEWER_THAN(18)
#else
if(!start.empty()) {
start.remove_prefix(1);
}
#endif

mOutputFormatHelper.Append(start, " "sv);
}
Expand Down Expand Up @@ -4485,20 +4440,11 @@ void CodeGenerator::InsertSuffix(const QualType& type)

void CodeGenerator::InsertTemplateArgs(const ClassTemplateSpecializationDecl& clsTemplateSpe)
{
#if IS_CLANG_NEWER_THAN(18)
if(const auto* ar = clsTemplateSpe.getTemplateArgsAsWritten()) {
InsertTemplateArgs(ar->arguments());
} else {
InsertTemplateArgs(clsTemplateSpe.getTemplateArgs());
}
#else
if(const TypeSourceInfo* typeAsWritten = clsTemplateSpe.getTypeAsWritten()) {
const TemplateSpecializationType* tmplSpecType = cast<TemplateSpecializationType>(typeAsWritten->getType());
InsertTemplateArgs(*tmplSpecType);
} else {
InsertTemplateArgs(clsTemplateSpe.getTemplateArgs());
}
#endif
}
//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -4536,9 +4482,7 @@ void CodeGenerator::InsertTemplateArg(const TemplateArgument& arg)
mOutputFormatHelper.Append(GetName(*arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()));
break;
case TemplateArgument::Null: mOutputFormatHelper.Append("null"sv); break;
#if IS_CLANG_NEWER_THAN(17)
case TemplateArgument::StructuralValue: ToDo(arg, mOutputFormatHelper); break;
#endif
}
}
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -4567,10 +4511,6 @@ void CodeGenerator::HandleLocalStaticNonTrivialClass(const VarDecl* stmt)
ctx.getConstantArrayType(ctx.CharTy,
llvm::APInt(ctx.getTypeSize(ctx.getSizeType()), 0),
Sizeof(stmt->getType()),
#if IS_CLANG_NEWER_THAN(17)
#else
ArrayType::
#endif
ArraySizeModifier::Normal,
0));

Expand Down Expand Up @@ -4979,11 +4919,7 @@ void CodeGenerator::InsertFunctionNameWithReturnType(const FunctionDecl& d
// template requires-clause during creation of the template head.
InsertConceptConstraint(&decl);

#if IS_CLANG_NEWER_THAN(17)
if(decl.isPureVirtual()) {
#else
if(decl.isPure()) {
#endif
mOutputFormatHelper.Append(" = 0"sv);
}

Expand Down
17 changes: 2 additions & 15 deletions CoroutinesCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,7 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro
if(const auto* cxxMethodDecl = dyn_cast_or_null<CXXMethodDecl>(&fd)) {
funParamStorage.reserve(funParams.size() + 1);

cxxMethodType = cxxMethodDecl->
#if IS_CLANG_NEWER_THAN(17)
getFunctionObjectParameterType()
#else
getThisObjectType()
#endif
;
cxxMethodType = cxxMethodDecl->getFunctionObjectParameterType();

// In case we have a member function the first parameter is a reference to this. The following code injects
// this parameter.
Expand Down Expand Up @@ -574,14 +568,7 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro
if(not ctor->param_empty() and
(getNonRefType(ctor->getParamDecl(0)) == QualType(cxxMethodType.getTypePtrOrNull(), 0))) {
if(0 == mASTData.mThisExprs.size()) {
mASTData.mThisExprs.push_back(
#if IS_CLANG_NEWER_THAN(17)
CXXThisExpr::Create(ctx, {}, Ptr(cxxMethodType), false)
#else
new(ctx) CXXThisExpr{{}, Ptr(cxxMethodType), false}
#endif

);
mASTData.mThisExprs.push_back(CXXThisExpr::Create(ctx, {}, Ptr(cxxMethodType), false));
}
} else {
(void)static_cast<bool>(derefFirstParam); // set it to false
Expand Down
2 changes: 0 additions & 2 deletions Insights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ class FindIncludes : public PPCallbacks
StringRef /*SearchPath*/,
StringRef /*RelativePath*/,
const Module* /*Imported*/,
#if IS_CLANG_NEWER_THAN(18)
bool /*ModuleImported*/,
#endif
SrcMgr::CharacteristicKind /*FileType*/) override
{
auto expansionLoc = mSm.getExpansionLoc(hashLoc);
Expand Down

0 comments on commit e25f555

Please sign in to comment.