[PATCH] D119646: [clang] Allow consteval functions in default arguments
Evgeny Shulgin via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 12 11:50:16 PST 2022
Izaron created this revision.
Izaron added reviewers: aaron.ballman, erichkeane, andreasfertig.
Herald added a subscriber: kristof.beyls.
Izaron requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
We should not mark a function as "referenced" if we call it within a
ConstantExpr, because the expression will be folded to a value in LLVM IR.
To prevent emitting consteval function declarations, we should not "jump over"
a ConstantExpr when it is a top-level ParmVarDecl's subexpression.
Fixes https://github.com/llvm/llvm-project/issues/48230
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D119646
Files:
clang/lib/AST/Decl.cpp
clang/lib/Sema/SemaExpr.cpp
clang/test/SemaCXX/cxx2a-consteval.cpp
Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -613,6 +613,27 @@
} // namespace unevaluated
+namespace default_argument {
+
+// Previously calls of consteval functions in default arguments were rejected.
+// Now we show that we don't reject such calls.
+consteval int foo() { return 1; }
+consteval int bar(int i = foo()) { return i * i; }
+
+struct Test1 {
+ Test1(int i = bar(13)) {}
+ void v(int i = bar(13) * 2 + bar(15)) {}
+};
+Test1 t1;
+
+struct Test2 {
+ constexpr Test2(int i = bar()) {}
+ constexpr void v(int i = bar(bar(bar(foo())))) {}
+};
+Test2 t2;
+
+} // namespace default_argument
+
namespace PR50779 {
struct derp {
int b = 0;
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -18964,6 +18964,12 @@
Inherited::Visit(E);
}
+ void VisitConstantExpr(ConstantExpr *E) {
+ // Don't mark declarations within a ConstantExpression, as this expression
+ // will be evaluated and folded to a value.
+ return;
+ }
+
void VisitDeclRefExpr(DeclRefExpr *E) {
// If we were asked not to visit local variables, don't.
if (SkipLocalVariables) {
Index: clang/lib/AST/Decl.cpp
===================================================================
--- clang/lib/AST/Decl.cpp
+++ clang/lib/AST/Decl.cpp
@@ -2813,7 +2813,8 @@
Expr *Arg = getInit();
if (auto *E = dyn_cast_or_null<FullExpr>(Arg))
- return E->getSubExpr();
+ if (!isa<ConstantExpr>(E))
+ return E->getSubExpr();
return Arg;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119646.408207.patch
Type: text/x-patch
Size: 1736 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220212/efd6ed88/attachment-0001.bin>
More information about the cfe-commits
mailing list