Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4704,10 +4704,8 @@ void CodeGenerator::InsertFunctionNameWithReturnType(const FunctionDecl& d
const bool requiresComment{isCXXMethodDecl and not methodDecl->isUserProvided() and
not methodDecl->isExplicitlyDefaulted()};
// [expr.prim.lambda.closure] p7 consteval/constexpr are obtained from the call operator
const bool isLambdaStaticInvoker{isCXXMethodDecl and methodDecl->isLambdaStaticInvoker()};
const FunctionDecl& constExprDecl{not isLambdaStaticInvoker ? decl
: *methodDecl->getParent()->getLambdaCallOperator()};
const auto desugaredReturnType = GetType(GetDesugarReturnType(decl));
const bool isLambdaStaticInvoker{isCXXMethodDecl and methodDecl->isLambdaStaticInvoker()};
const auto desugaredReturnType = GetType(GetDesugarReturnType(decl));

if(methodDecl) {
if(requiresComment) {
Expand Down Expand Up @@ -4793,34 +4791,34 @@ void CodeGenerator::InsertFunctionNameWithReturnType(const FunctionDecl& d
}
}

if(constExprDecl.isConstexpr()) {
const bool skipConstexpr{isLambda and not isa<CXXConversionDecl>(constExprDecl)};
// Special treatment for a conversion operator in a captureless lambda. It appears that if the call operator
// is consteval the conversion operator must be as well, otherwise it cannot take the address of the invoke
// function.
const bool isConversionOpWithConstevalCallOp{[&]() {
if(methodDecl) {
if(const auto callOp = methodDecl->getParent()->getLambdaCallOperator()) {
return callOp->isConsteval();
}
}
if(const FunctionDecl& constExprDecl{isLambdaStaticInvoker ? *methodDecl->getParent()->getLambdaCallOperator()
: decl};
constExprDecl.isConstexpr()) {

return false;
}()};
ConstexprSpecKind constexrKind{constExprDecl.isImmediateFunction() ? ConstexprSpecKind::Consteval
: constExprDecl.getConstexprKind()};

if(not isConversionOpWithConstevalCallOp and constExprDecl.isConstexprSpecified()) {
if(skipConstexpr) {
mOutputFormatHelper.Append(kwCommentStart);
}

mOutputFormatHelper.Append(kwConstExprSpace);
if(isLambda) {
// Special treatment for a conversion operator in a captureless lambda. It appears that if the call operator
// is consteval the conversion operator must be manually promoted to consteval as well, otherwise it cannot
// take the address of the invoke function.
if(const auto* callOp{methodDecl->getParent()->getLambdaCallOperator()}; isa<CXXConversionDecl>(decl)) {
if(callOp->isConsteval()) {
constexrKind = ConstexprSpecKind::Consteval;
}

if(skipConstexpr) {
mOutputFormatHelper.Append(kwCCommentEndSpace);
} else if(llvm::SmallVector<clang::PartialDiagnosticAt, 8> Diags{};
// remove a potential constexpr from a lambda if the function is unusable in a constant expression
not clang::Expr::isPotentialConstantExpr(callOp, Diags)) {
constexrKind = ConstexprSpecKind::Unspecified;
}
}

} else if(isConversionOpWithConstevalCallOp or constExprDecl.isConsteval()) {
mOutputFormatHelper.Append(kwConstEvalSpace);
switch(constexrKind) {
case ConstexprSpecKind::Unspecified: break;
case ConstexprSpecKind::Constexpr: mOutputFormatHelper.Append(kwConstExprSpace); break;
case ConstexprSpecKind::Consteval: mOutputFormatHelper.Append(kwConstEvalSpace); break;
case ConstexprSpecKind::Constinit: break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Basic.Start.Main.P5.3Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ int main()
class __lambda_3_13
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return 2;
}
Expand All @@ -16,7 +16,7 @@ int main()
};

private:
static inline /*constexpr */ int __invoke()
static inline constexpr int __invoke()
{
return __lambda_3_13{}.operator()();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Basic.Start.Main.P5.4Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main(int argc, const char ** argv)
class __lambda_5_13
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return 2;
}
Expand All @@ -20,7 +20,7 @@ int main(int argc, const char ** argv)
};

private:
static inline /*constexpr */ int __invoke()
static inline constexpr int __invoke()
{
return __lambda_5_13{}.operator()();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ConstraintAutoParameterTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ class __lambda_6_15
{
public:
template<C type_parameter_0_0>
inline /*constexpr */ auto operator()(type_parameter_0_0 container) const
inline constexpr auto operator()(type_parameter_0_0 container) const
{
}

#ifdef INSIGHTS_USE_TEMPLATE
template<>
inline /*constexpr */ void operator()<int>(int container) const
inline constexpr void operator()<int>(int container) const
{
}
#endif

private:
template<C type_parameter_0_0>
static inline /*constexpr */ auto __invoke(type_parameter_0_0 container)
static inline constexpr auto __invoke(type_parameter_0_0 container)
{
return __lambda_6_15{}.operator()<type_parameter_0_0>(container);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/EmptyLambda.expect
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ void foo()
class __lambda_2_1
{
public:
inline /*constexpr */ void operator()() const
inline constexpr void operator()() const
{
}

Expand All @@ -15,7 +15,7 @@ void foo()
};

private:
static inline /*constexpr */ void __invoke()
static inline constexpr void __invoke()
{
__lambda_2_1{}.operator()();
}
Expand Down
30 changes: 15 additions & 15 deletions tests/ForStmtWithLambdaInInitTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main()
class __lambda_7_17
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return x * 2;
}
Expand All @@ -24,7 +24,7 @@ int main()
class __lambda_11_17
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return x * 2;
}
Expand All @@ -37,7 +37,7 @@ int main()
class __lambda_11_43
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return x * 2;
}
Expand All @@ -54,7 +54,7 @@ int main()
class __lambda_15_9
{
public:
inline /*constexpr */ void operator()() const
inline void operator()() const
{
printf("started\n");
}
Expand All @@ -66,7 +66,7 @@ int main()
};

private:
static inline /*constexpr */ void __invoke()
static inline void __invoke()
{
__lambda_15_9{}.operator()();
}
Expand All @@ -77,7 +77,7 @@ int main()
class __lambda_15_37
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return --x;
}
Expand All @@ -90,7 +90,7 @@ int main()
class __lambda_15_59
{
public:
inline /*constexpr */ void operator()() const
inline void operator()() const
{
printf("after\n");
}
Expand All @@ -102,7 +102,7 @@ int main()
};

private:
static inline /*constexpr */ void __invoke()
static inline void __invoke()
{
__lambda_15_59{}.operator()();
}
Expand All @@ -116,7 +116,7 @@ int main()
class __lambda_19_9
{
public:
inline /*constexpr */ void operator()() const
inline void operator()() const
{
printf("started\n");
}
Expand All @@ -128,7 +128,7 @@ int main()
};

private:
static inline /*constexpr */ void __invoke()
static inline void __invoke()
{
__lambda_19_9{}.operator()();
}
Expand All @@ -139,7 +139,7 @@ int main()
class __lambda_20_9
{
public:
inline /*constexpr */ void operator()() const
inline void operator()() const
{
printf("started\n");
}
Expand All @@ -151,7 +151,7 @@ int main()
};

private:
static inline /*constexpr */ void __invoke()
static inline void __invoke()
{
__lambda_20_9{}.operator()();
}
Expand All @@ -162,7 +162,7 @@ int main()
class __lambda_20_37
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return --x;
}
Expand All @@ -175,7 +175,7 @@ int main()
class __lambda_20_59
{
public:
inline /*constexpr */ void operator()() const
inline void operator()() const
{
printf("after\n");
}
Expand All @@ -187,7 +187,7 @@ int main()
};

private:
static inline /*constexpr */ void __invoke()
static inline void __invoke()
{
__lambda_20_59{}.operator()();
}
Expand Down
12 changes: 6 additions & 6 deletions tests/IfStmtWithLambdaInInitTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main()
class __lambda_3_14
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return 2;
}
Expand All @@ -17,7 +17,7 @@ int main()
};

private:
static inline /*constexpr */ int __invoke()
static inline constexpr int __invoke()
{
return __lambda_3_14{}.operator()();
}
Expand All @@ -29,7 +29,7 @@ int main()
class __lambda_3_34
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return 3;
}
Expand All @@ -41,7 +41,7 @@ int main()
};

private:
static inline /*constexpr */ int __invoke()
static inline constexpr int __invoke()
{
return __lambda_3_34{}.operator()();
}
Expand All @@ -53,7 +53,7 @@ int main()
class __lambda_3_53
{
public:
inline /*constexpr */ int operator()() const
inline constexpr int operator()() const
{
return 4;
}
Expand All @@ -65,7 +65,7 @@ int main()
};

private:
static inline /*constexpr */ int __invoke()
static inline constexpr int __invoke()
{
return __lambda_3_53{}.operator()();
}
Expand Down
Loading
Loading