Skip to content

Commit d74bfb4

Browse files
Merge pull request #617 from andreasfertig/ranges
Replace existing code with ranges and range-algorithms.
2 parents 3b15f3d + d4ba123 commit d74bfb4

2 files changed

Lines changed: 16 additions & 28 deletions

File tree

CodeGenerator.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
}
3939
//-----------------------------------------------------------------------------
4040

41+
namespace ranges = std::ranges;
42+
//-----------------------------------------------------------------------------
4143
namespace clang::insights {
4244

4345
#define BUILD_OPT_AND(name, param) std::function name = [](param t) -> MyOptional<param>
@@ -1524,11 +1526,8 @@ void CodeGenerator::InsertMethodBody(const FunctionDecl* stmt, const size_t posB
15241526

15251527
// handle C++ [basic.start.main] §5: main can have no return statement
15261528
if(stmt->hasImplicitReturnZero()) {
1527-
// TODO replace with ranges::find_if
1528-
const auto cmpBody = dyn_cast<CompoundStmt>(stmt->getBody())->body();
1529-
mRequiresImplicitReturnZero =
1530-
std::end(cmpBody) ==
1531-
std::find_if(cmpBody.begin(), cmpBody.end(), [](const Stmt* e) { return isa<ReturnStmt>(e); });
1529+
mRequiresImplicitReturnZero = ranges::none_of(dyn_cast<CompoundStmt>(stmt->getBody())->body(),
1530+
[](const Stmt* e) { return isa<ReturnStmt>(e); });
15321531
}
15331532

15341533
const auto* body = stmt->getBody();
@@ -1754,11 +1753,10 @@ void CodeGenerator::InsertArg(const ClassTemplateDecl* stmt)
17541753
}
17551754

17561755
// Sort specializations by POI to make dependent specializations work.
1757-
std::sort(specializations.begin(),
1758-
specializations.end(),
1759-
[](const ClassTemplateSpecializationDecl* a, const ClassTemplateSpecializationDecl* b) {
1760-
return a->getPointOfInstantiation() < b->getPointOfInstantiation();
1761-
});
1756+
ranges::sort(specializations,
1757+
[](const ClassTemplateSpecializationDecl* a, const ClassTemplateSpecializationDecl* b) {
1758+
return a->getPointOfInstantiation() < b->getPointOfInstantiation();
1759+
});
17621760

17631761
for(const auto* spec : specializations) {
17641762
InsertArg(spec);
@@ -4621,17 +4619,8 @@ void CodeGenerator::HandleLambdaExpr(const LambdaExpr* lambda, LambdaHelper& lam
46214619

46224620
outputFormatHelper.AppendNewLine();
46234621
LambdaCodeGenerator codeGenerator{outputFormatHelper, mLambdaStack, mProcessingPrimaryTemplate};
4624-
codeGenerator.mCapturedThisAsCopy = [&] {
4625-
for(const auto& c : lambda->captures()) {
4626-
const auto captureKind = c.getCaptureKind();
4627-
4628-
if(c.capturesThis() and (captureKind == LCK_StarThis)) {
4629-
return true;
4630-
}
4631-
}
4632-
4633-
return false;
4634-
}();
4622+
codeGenerator.mCapturedThisAsCopy = ranges::any_of(
4623+
lambda->captures(), [](auto& c) { return (c.capturesThis() and (c.getCaptureKind() == LCK_StarThis)); });
46354624

46364625
codeGenerator.mLambdaExpr = lambda;
46374626
codeGenerator.InsertArg(lambda->getLambdaClass());

CoroutinesCodeGenerator.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include <algorithm>
1919
//-----------------------------------------------------------------------------
2020

21+
namespace ranges = std::ranges;
22+
//-----------------------------------------------------------------------------
23+
2124
namespace clang::insights {
2225

2326
constexpr std::string_view CORO_FRAME_NAME{"__f"sv};
@@ -560,8 +563,7 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro
560563
// this parameter.
561564
funParamStorage.push_back(Parameter(&fd, CORO_FRAME_ACCESS_THIS, cxxMethodType));
562565

563-
// XXX: use ranges once available
564-
std::copy(funParams.begin(), funParams.end(), std::back_inserter(funParamStorage));
566+
ranges::copy(funParams, std::back_inserter(funParamStorage));
565567

566568
funParams = funParamStorage;
567569
}
@@ -577,11 +579,8 @@ void CoroutinesCodeGenerator::InsertCoroutine(const FunctionDecl& fd, const Coro
577579
for(auto* promiseTypeRecordDecl = mASTData.mPromiseField->getType()->getAsCXXRecordDecl();
578580
auto* ctor : promiseTypeRecordDecl->ctors()) {
579581

580-
// XXX: use ranges once available
581-
if(not std::equal(
582-
ctor->param_begin(), ctor->param_end(), funParams.begin(), funParams.end(), [&](auto& a, auto& b) {
583-
return getNonRefType(a) == getNonRefType(b);
584-
})) {
582+
if(not ranges::equal(
583+
ctor->parameters(), funParams, [&](auto& a, auto& b) { return getNonRefType(a) == getNonRefType(b); })) {
585584
continue;
586585
}
587586

0 commit comments

Comments
 (0)