From bec53e9a3fc9d04855588abd1021c762d48ea866 Mon Sep 17 00:00:00 2001 From: Erik Pilkington Date: Tue, 26 Mar 2019 23:21:19 +0000 Subject: [PATCH] [Sema] Fix an assert when a block captures a constexpr local MarkVarDeclODRUsed indirectly calls captureInBlock, which creates a copy expression. The copy expression is insulated in it's own ExpressionEvaluationContext, so it saves, mutates, and restores MaybeODRUseExprs as CleanupVarDeclMarking is iterating through it, leading to a crash. Fix this by iterating through a local copy of MaybeODRUseExprs. rdar://47493525 https://reviews.llvm.org/D59670 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@357040 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 16 ++++++++-------- lib/Sema/SemaExpr.cpp | 11 ++++++++--- test/SemaCXX/blocks.cpp | 8 ++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 376c376ed0..e8d568482b 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -587,13 +587,13 @@ public: /// element type here is ExprWithCleanups::Object. SmallVector ExprCleanupObjects; - /// Store a list of either DeclRefExprs or MemberExprs - /// that contain a reference to a variable (constant) that may or may not - /// be odr-used in this Expr, and we won't know until all lvalue-to-rvalue - /// and discarded value conversions have been applied to all subexpressions - /// of the enclosing full expression. This is cleared at the end of each - /// full expression. - llvm::SmallPtrSet MaybeODRUseExprs; + /// Store a set of either DeclRefExprs or MemberExprs that contain a reference + /// to a variable (constant) that may or may not be odr-used in this Expr, and + /// we won't know until all lvalue-to-rvalue and discarded value conversions + /// have been applied to all subexpressions of the enclosing full expression. + /// This is cleared at the end of each full expression. + using MaybeODRUseExprSet = llvm::SmallPtrSet; + MaybeODRUseExprSet MaybeODRUseExprs; std::unique_ptr PreallocatedFunctionScope; @@ -1029,7 +1029,7 @@ public: /// context (i.e. the number of TypoExprs created). unsigned NumTypos; - llvm::SmallPtrSet SavedMaybeODRUseExprs; + MaybeODRUseExprSet SavedMaybeODRUseExprs; /// The lambdas that are present within this context, if it /// is indeed an unevaluated context. diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 29b8bff947..85d10e7228 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -15690,7 +15690,12 @@ ExprResult Sema::ActOnConstantExpression(ExprResult Res) { } void Sema::CleanupVarDeclMarking() { - for (Expr *E : MaybeODRUseExprs) { + // Iterate through a local copy in case MarkVarDeclODRUsed makes a recursive + // call. + MaybeODRUseExprSet LocalMaybeODRUseExprs; + std::swap(LocalMaybeODRUseExprs, MaybeODRUseExprs); + + for (Expr *E : LocalMaybeODRUseExprs) { VarDecl *Var; SourceLocation Loc; if (DeclRefExpr *DRE = dyn_cast(E)) { @@ -15707,10 +15712,10 @@ void Sema::CleanupVarDeclMarking() { /*MaxFunctionScopeIndex Pointer*/ nullptr); } - MaybeODRUseExprs.clear(); + assert(MaybeODRUseExprs.empty() && + "MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?"); } - static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, VarDecl *Var, Expr *E) { assert((!E || isa(E) || isa(E)) && diff --git a/test/SemaCXX/blocks.cpp b/test/SemaCXX/blocks.cpp index 0521802172..aacf63cfab 100644 --- a/test/SemaCXX/blocks.cpp +++ b/test/SemaCXX/blocks.cpp @@ -145,3 +145,11 @@ namespace test6c { A::foo(); }); } } + +namespace test7 { +struct S {}; +void f() { + constexpr S s; + auto some_block = ^{ (void)s; }; +} +} -- 2.50.1