hokein created this revision. hokein added a reviewer: sammccall. Herald added a project: clang. hokein requested review of this revision.
Introduce a new diagnostic, and respect the bracket-depth (256) by default. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D86936 Files: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/TreeTransform.h clang/test/SemaCXX/fold_expr_expansion_limit.cpp Index: clang/test/SemaCXX/fold_expr_expansion_limit.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/fold_expr_expansion_limit.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -fsyntax-only -fbracket-depth 2 -verify -std=c++17 %s + +template <class T, T...V> struct seq { + constexpr bool zero() { return (true && ... && (V == 0)); }; // expected-error {{fold expression expansion level 3 exceeded maximum of 2}} +}; +constexpr unsigned N = 3; +auto x = __make_integer_seq<seq, int, N>{}; +static_assert(!x.zero(), ""); // expected-error {{static_assert expression is not an integral constant expression}} \ + expected-note {{in instantiation of member function}} Index: clang/lib/Sema/TreeTransform.h =================================================================== --- clang/lib/Sema/TreeTransform.h +++ clang/lib/Sema/TreeTransform.h @@ -13192,6 +13192,13 @@ Callee, E->getBeginLoc(), LHS.get(), E->getOperator(), E->getEllipsisLoc(), RHS.get(), E->getEndLoc(), NumExpansions); } + if (NumExpansions && SemaRef.getLangOpts().BracketDepth < NumExpansions) { + SemaRef.Diag(E->getEllipsisLoc(), + clang::diag::err_fold_expression_expansion_exceeded) + << *NumExpansions << SemaRef.getLangOpts().BracketDepth + << E->getSourceRange(); + return ExprError(); + } // The transform has determined that we should perform an elementwise // expansion of the pattern. Do so. Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5089,6 +5089,8 @@ "with no fallback value">; def err_fold_expression_bad_operand : Error< "expression not permitted as operand of fold expression">; +def err_fold_expression_expansion_exceeded: Error< + "fold expression expansion level %0 exceeded maximum of %1">; def err_unexpected_typedef : Error< "unexpected type name %0: expected expression">;
Index: clang/test/SemaCXX/fold_expr_expansion_limit.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/fold_expr_expansion_limit.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -fsyntax-only -fbracket-depth 2 -verify -std=c++17 %s + +template <class T, T...V> struct seq { + constexpr bool zero() { return (true && ... && (V == 0)); }; // expected-error {{fold expression expansion level 3 exceeded maximum of 2}} +}; +constexpr unsigned N = 3; +auto x = __make_integer_seq<seq, int, N>{}; +static_assert(!x.zero(), ""); // expected-error {{static_assert expression is not an integral constant expression}} \ + expected-note {{in instantiation of member function}} Index: clang/lib/Sema/TreeTransform.h =================================================================== --- clang/lib/Sema/TreeTransform.h +++ clang/lib/Sema/TreeTransform.h @@ -13192,6 +13192,13 @@ Callee, E->getBeginLoc(), LHS.get(), E->getOperator(), E->getEllipsisLoc(), RHS.get(), E->getEndLoc(), NumExpansions); } + if (NumExpansions && SemaRef.getLangOpts().BracketDepth < NumExpansions) { + SemaRef.Diag(E->getEllipsisLoc(), + clang::diag::err_fold_expression_expansion_exceeded) + << *NumExpansions << SemaRef.getLangOpts().BracketDepth + << E->getSourceRange(); + return ExprError(); + } // The transform has determined that we should perform an elementwise // expansion of the pattern. Do so. Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5089,6 +5089,8 @@ "with no fallback value">; def err_fold_expression_bad_operand : Error< "expression not permitted as operand of fold expression">; +def err_fold_expression_expansion_exceeded: Error< + "fold expression expansion level %0 exceeded maximum of %1">; def err_unexpected_typedef : Error< "unexpected type name %0: expected expression">;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits