[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-07 Thread via cfe-commits

https://github.com/yronglin created 
https://github.com/llvm/llvm-project/pull/87930

This PR remove `InMaterializeTemporaryObjectContext` , because it's redundant, 
materialize non-cv void prvalue temporaries in discarded expressions can only 
appear under lifetime-extension context.

>From c8ed2449651a4e716f4a127d5b4c3f5c283d3abf Mon Sep 17 00:00:00 2001
From: yronglin 
Date: Mon, 8 Apr 2024 01:04:42 +0800
Subject: [PATCH] [Clang] Refactor implementation of "Lifetime extension in
 range-based for loops"

Signed-off-by: yronglin 
---
 clang/include/clang/Sema/Sema.h   | 47 ---
 clang/lib/Parse/ParseDecl.cpp |  4 --
 clang/lib/Sema/SemaExpr.cpp   |  7 ++-
 clang/lib/Sema/SemaExprCXX.cpp|  2 +-
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  1 -
 clang/lib/Sema/TreeTransform.h|  5 --
 6 files changed, 4 insertions(+), 62 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 56d66a4486e0e7..ba9624b0085c48 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5075,34 +5075,6 @@ class Sema final : public SemaBase {
 /// example, in a for-range initializer).
 bool InLifetimeExtendingContext = false;
 
-/// Whether we are currently in a context in which all temporaries must be
-/// materialized.
-///
-/// [class.temporary]/p2:
-/// The materialization of a temporary object is generally delayed as long
-/// as possible in order to avoid creating unnecessary temporary objects.
-///
-/// Temporary objects are materialized:
-///   (2.1) when binding a reference to a prvalue ([dcl.init.ref],
-///   [expr.type.conv], [expr.dynamic.cast], [expr.static.cast],
-///   [expr.const.cast], [expr.cast]),
-///
-///   (2.2) when performing member access on a class prvalue ([expr.ref],
-///   [expr.mptr.oper]),
-///
-///   (2.3) when performing an array-to-pointer conversion or subscripting
-///   on an array prvalue ([conv.array], [expr.sub]),
-///
-///   (2.4) when initializing an object of type
-///   std​::​initializer_list from a braced-init-list
-///   ([dcl.init.list]),
-///
-///   (2.5) for certain unevaluated operands ([expr.typeid], [expr.sizeof])
-///
-///   (2.6) when a prvalue that has type other than cv void appears as a
-///   discarded-value expression ([expr.context]).
-bool InMaterializeTemporaryObjectContext = false;
-
 // When evaluating immediate functions in the initializer of a default
 // argument or default member initializer, this is the declaration whose
 // default initializer is being evaluated and the location of the call
@@ -6383,19 +6355,6 @@ class Sema final : public SemaBase {
 }
   }
 
-  /// keepInMaterializeTemporaryObjectContext - Pull down
-  /// InMaterializeTemporaryObjectContext flag from previous context.
-  void keepInMaterializeTemporaryObjectContext() {
-if (ExprEvalContexts.size() > 2 &&
-ExprEvalContexts[ExprEvalContexts.size() - 2]
-.InMaterializeTemporaryObjectContext) {
-  auto &LastRecord = ExprEvalContexts.back();
-  auto &PrevRecord = ExprEvalContexts[ExprEvalContexts.size() - 2];
-  LastRecord.InMaterializeTemporaryObjectContext =
-  PrevRecord.InMaterializeTemporaryObjectContext;
-}
-  }
-
   DefaultedComparisonKind getDefaultedComparisonKind(const FunctionDecl *FD) {
 return getDefaultedFunctionKind(FD).asComparison();
   }
@@ -6539,12 +6498,6 @@ class Sema final : public SemaBase {
   /// used in initializer of the field.
   llvm::MapVector DeleteExprs;
 
-  bool isInMaterializeTemporaryObjectContext() const {
-assert(!ExprEvalContexts.empty() &&
-   "Must be in an expression evaluation context");
-return ExprEvalContexts.back().InMaterializeTemporaryObjectContext;
-  }
-
   ParsedType getInheritingConstructorName(CXXScopeSpec &SS,
   SourceLocation NameLoc,
   IdentifierInfo &Name);
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 0aa14b0510746b..ae5d2cae06e536 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -2379,10 +2379,6 @@ Parser::DeclGroupPtrTy 
Parser::ParseDeclGroup(ParsingDeclSpec &DS,
   if (getLangOpts().CPlusPlus23) {
 auto &LastRecord = Actions.ExprEvalContexts.back();
 LastRecord.InLifetimeExtendingContext = true;
-
-// Materialize non-`cv void` prvalue temporaries in discarded
-// expressions. These materialized temporaries may be 
lifetime-extented.
-LastRecord.InMaterializeTemporaryObjectContext = true;
   }
 
   if (getLangOpts().OpenMP)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8db4fffeecfe35..3d6c649d8df862 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/cla

[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-07 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: None (yronglin)


Changes

This PR remove `InMaterializeTemporaryObjectContext` , because it's redundant, 
materialize non-cv void prvalue temporaries in discarded expressions can only 
appear under lifetime-extension context.

---
Full diff: https://github.com/llvm/llvm-project/pull/87930.diff


6 Files Affected:

- (modified) clang/include/clang/Sema/Sema.h (-47) 
- (modified) clang/lib/Parse/ParseDecl.cpp (-4) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+3-4) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+1-1) 
- (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (-1) 
- (modified) clang/lib/Sema/TreeTransform.h (-5) 


``diff
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 56d66a4486e0e7..ba9624b0085c48 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5075,34 +5075,6 @@ class Sema final : public SemaBase {
 /// example, in a for-range initializer).
 bool InLifetimeExtendingContext = false;
 
-/// Whether we are currently in a context in which all temporaries must be
-/// materialized.
-///
-/// [class.temporary]/p2:
-/// The materialization of a temporary object is generally delayed as long
-/// as possible in order to avoid creating unnecessary temporary objects.
-///
-/// Temporary objects are materialized:
-///   (2.1) when binding a reference to a prvalue ([dcl.init.ref],
-///   [expr.type.conv], [expr.dynamic.cast], [expr.static.cast],
-///   [expr.const.cast], [expr.cast]),
-///
-///   (2.2) when performing member access on a class prvalue ([expr.ref],
-///   [expr.mptr.oper]),
-///
-///   (2.3) when performing an array-to-pointer conversion or subscripting
-///   on an array prvalue ([conv.array], [expr.sub]),
-///
-///   (2.4) when initializing an object of type
-///   std​::​initializer_list from a braced-init-list
-///   ([dcl.init.list]),
-///
-///   (2.5) for certain unevaluated operands ([expr.typeid], [expr.sizeof])
-///
-///   (2.6) when a prvalue that has type other than cv void appears as a
-///   discarded-value expression ([expr.context]).
-bool InMaterializeTemporaryObjectContext = false;
-
 // When evaluating immediate functions in the initializer of a default
 // argument or default member initializer, this is the declaration whose
 // default initializer is being evaluated and the location of the call
@@ -6383,19 +6355,6 @@ class Sema final : public SemaBase {
 }
   }
 
-  /// keepInMaterializeTemporaryObjectContext - Pull down
-  /// InMaterializeTemporaryObjectContext flag from previous context.
-  void keepInMaterializeTemporaryObjectContext() {
-if (ExprEvalContexts.size() > 2 &&
-ExprEvalContexts[ExprEvalContexts.size() - 2]
-.InMaterializeTemporaryObjectContext) {
-  auto &LastRecord = ExprEvalContexts.back();
-  auto &PrevRecord = ExprEvalContexts[ExprEvalContexts.size() - 2];
-  LastRecord.InMaterializeTemporaryObjectContext =
-  PrevRecord.InMaterializeTemporaryObjectContext;
-}
-  }
-
   DefaultedComparisonKind getDefaultedComparisonKind(const FunctionDecl *FD) {
 return getDefaultedFunctionKind(FD).asComparison();
   }
@@ -6539,12 +6498,6 @@ class Sema final : public SemaBase {
   /// used in initializer of the field.
   llvm::MapVector DeleteExprs;
 
-  bool isInMaterializeTemporaryObjectContext() const {
-assert(!ExprEvalContexts.empty() &&
-   "Must be in an expression evaluation context");
-return ExprEvalContexts.back().InMaterializeTemporaryObjectContext;
-  }
-
   ParsedType getInheritingConstructorName(CXXScopeSpec &SS,
   SourceLocation NameLoc,
   IdentifierInfo &Name);
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 0aa14b0510746b..ae5d2cae06e536 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -2379,10 +2379,6 @@ Parser::DeclGroupPtrTy 
Parser::ParseDeclGroup(ParsingDeclSpec &DS,
   if (getLangOpts().CPlusPlus23) {
 auto &LastRecord = Actions.ExprEvalContexts.back();
 LastRecord.InLifetimeExtendingContext = true;
-
-// Materialize non-`cv void` prvalue temporaries in discarded
-// expressions. These materialized temporaries may be 
lifetime-extented.
-LastRecord.InMaterializeTemporaryObjectContext = true;
   }
 
   if (getLangOpts().OpenMP)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8db4fffeecfe35..3d6c649d8df862 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6331,7 +6331,6 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation 
CallLoc,
   // Pass down lifetime extending flag, and collect temporaries in
   // CreateMaterializeTemporaryExpr when we 

[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-08 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

Give @hubert-reinterpretcast a few days to possibly review, otherwise LGTM

https://github.com/llvm/llvm-project/pull/87930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-09 Thread via cfe-commits

yronglin wrote:

> Give @hubert-reinterpretcast a few days to possibly review, otherwise LGTM

Thanks for your review!

https://github.com/llvm/llvm-project/pull/87930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-15 Thread via cfe-commits

yronglin wrote:

friendly ping~ @hubert-reinterpretcast 

https://github.com/llvm/llvm-project/pull/87930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-15 Thread via cfe-commits

cor3ntin wrote:

@yronglin fee free to merge. Thanks!

https://github.com/llvm/llvm-project/pull/87930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-15 Thread via cfe-commits

yronglin wrote:

> @yronglin fee free to merge. Thanks!

Thanks for your confirmation!

https://github.com/llvm/llvm-project/pull/87930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Refactor implementation of "Lifetime extension in range-based for loops" (PR #87930)

2024-04-15 Thread via cfe-commits

https://github.com/yronglin closed 
https://github.com/llvm/llvm-project/pull/87930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits