From ea344c8140e731b361ec0e2b264c1f486b383025 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sat, 3 Dec 2016 01:14:32 +0000 Subject: [PATCH] DR616, and part of P0135R1: member access (or pointer-to-member access) on a temporary produces an xvalue, not a prvalue. Support this by materializing the temporary prior to performing the member access. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@288563 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Expr.h | 5 ++++ include/clang/Sema/Sema.h | 5 ++++ lib/Analysis/ThreadSafety.cpp | 2 +- lib/Analysis/ThreadSafetyCommon.cpp | 3 ++ lib/Sema/SemaExpr.cpp | 6 ++-- lib/Sema/SemaExprCXX.cpp | 11 ++++--- lib/Sema/SemaExprMember.cpp | 9 ++++++ lib/Sema/SemaInit.cpp | 30 +++++++++++++------ test/Analysis/temp-obj-dtors-cfg-output.cpp | 6 ++-- test/CXX/drs/dr4xx.cpp | 2 +- test/CXX/drs/dr6xx.cpp | 20 ++++++++----- test/CXX/expr/expr.const/p2-0x.cpp | 6 ++-- test/CodeGenCXX/compound-literals.cpp | 20 +++++++++---- test/SemaCXX/constexpr-value-init.cpp | 2 +- test/SemaCXX/expression-traits.cpp | 2 +- test/SemaObjC/assign-rvalue-message.m | 5 ++-- unittests/ASTMatchers/ASTMatchersNodeTest.cpp | 2 +- www/cxx_dr_status.html | 8 ++--- 18 files changed, 99 insertions(+), 45 deletions(-) diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 64e363bc98..763a067956 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -839,6 +839,11 @@ public: const Expr *skipRValueSubobjectAdjustments( SmallVectorImpl &CommaLHS, SmallVectorImpl &Adjustments) const; + const Expr *skipRValueSubobjectAdjustments() const { + SmallVector CommaLHSs; + SmallVector Adjustments; + return skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); + } static bool classof(const Stmt *T) { return T->getStmtClass() >= firstExprConstant && diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 2fa91a3afb..9531440c4f 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -8715,6 +8715,11 @@ public: // argument, and arguments that have type float are promoted to double. ExprResult DefaultArgumentPromotion(Expr *E); + /// If \p E is a prvalue denoting an unmaterialized temporary, materialize + /// it as an xvalue. In C++98, the result will still be a prvalue, because + /// we don't have xvalues there. + ExprResult TemporaryMaterializationConversion(Expr *E); + // Used for emitting the right warning by DefaultVariadicArgumentPromotion enum VariadicCallType { VariadicFunction, diff --git a/lib/Analysis/ThreadSafety.cpp b/lib/Analysis/ThreadSafety.cpp index 984a5d42ad..879a15c9c2 100644 --- a/lib/Analysis/ThreadSafety.cpp +++ b/lib/Analysis/ThreadSafety.cpp @@ -1583,7 +1583,7 @@ void BuildLockset::warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, /// a pointer marked with pt_guarded_by. void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK, ProtectedOperationKind POK) { - Exp = Exp->IgnoreParenCasts(); + Exp = Exp->IgnoreImplicit()->IgnoreParenCasts(); SourceLocation Loc = Exp->getExprLoc(); diff --git a/lib/Analysis/ThreadSafetyCommon.cpp b/lib/Analysis/ThreadSafetyCommon.cpp index 96b317fef7..cbd5464c34 100644 --- a/lib/Analysis/ThreadSafetyCommon.cpp +++ b/lib/Analysis/ThreadSafetyCommon.cpp @@ -233,6 +233,9 @@ til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) { return translate(cast(S)->getSubExpr(), Ctx); case Stmt::CXXBindTemporaryExprClass: return translate(cast(S)->getSubExpr(), Ctx); + case Stmt::MaterializeTemporaryExprClass: + return translate(cast(S)->GetTemporaryExpr(), + Ctx); // Collect all literals case Stmt::CharacterLiteralClass: diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 667ea6cb4e..4415100ab4 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -9810,8 +9810,8 @@ static bool IsReadonlyMessage(Expr *E, Sema &S) { const MemberExpr *ME = dyn_cast(E); if (!ME) return false; if (!isa(ME->getMemberDecl())) return false; - ObjCMessageExpr *Base = - dyn_cast(ME->getBase()->IgnoreParenImpCasts()); + ObjCMessageExpr *Base = dyn_cast( + ME->getBase()->IgnoreImplicit()->IgnoreParenImpCasts()); if (!Base) return false; return Base->getMethodDecl() != nullptr; } @@ -9894,7 +9894,7 @@ static void DiagnoseConstAssignment(Sema &S, const Expr *E, while (true) { IsDereference = NextIsDereference; - E = E->IgnoreParenImpCasts(); + E = E->IgnoreImplicit()->IgnoreParenImpCasts(); if (const MemberExpr *ME = dyn_cast(E)) { NextIsDereference = ME->isArrow(); const ValueDecl *VD = ME->getMemberDecl(); diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 79b5356d79..28ccd60750 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -4984,11 +4984,14 @@ QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, !RHS.get()->getType()->isPlaceholderType() && "placeholders should have been weeded out by now"); - // The LHS undergoes lvalue conversions if this is ->*. - if (isIndirect) { + // The LHS undergoes lvalue conversions if this is ->*, and undergoes the + // temporary materialization conversion otherwise. + if (isIndirect) LHS = DefaultLvalueConversion(LHS.get()); - if (LHS.isInvalid()) return QualType(); - } + else if (LHS.get()->isRValue()) + LHS = TemporaryMaterializationConversion(LHS.get()); + if (LHS.isInvalid()) + return QualType(); // The RHS always undergoes lvalue conversions. RHS = DefaultLvalueConversion(RHS.get()); diff --git a/lib/Sema/SemaExprMember.cpp b/lib/Sema/SemaExprMember.cpp index eddf0d69a2..806a3d813e 100644 --- a/lib/Sema/SemaExprMember.cpp +++ b/lib/Sema/SemaExprMember.cpp @@ -969,6 +969,15 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, BaseType = BaseType->castAs()->getPointeeType(); } R.setBaseObjectType(BaseType); + + // C++1z [expr.ref]p2: + // For the first option (dot) the first expression shall be a glvalue [...] + if (!IsArrow && BaseExpr->isRValue()) { + ExprResult Converted = TemporaryMaterializationConversion(BaseExpr); + if (Converted.isInvalid()) + return ExprError(); + BaseExpr = Converted.get(); + } LambdaScopeInfo *const CurLSI = getCurLambda(); // If this is an implicit member reference and the overloaded diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index f5e38b4fbd..3937aad012 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -5963,10 +5963,7 @@ performReferenceExtension(Expr *Init, // Step over any subobject adjustments; we may have a materialized // temporary inside them. - SmallVector CommaLHSs; - SmallVector Adjustments; - Init = const_cast( - Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); + Init = const_cast(Init->skipRValueSubobjectAdjustments()); // Per current approach for DR1376, look through casts to reference type // when performing lifetime extension. @@ -5996,10 +5993,7 @@ performReferenceExtension(Expr *Init, static void performLifetimeExtension(Expr *Init, const InitializedEntity *ExtendingEntity) { // Dig out the expression which constructs the extended temporary. - SmallVector CommaLHSs; - SmallVector Adjustments; - Init = const_cast( - Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); + Init = const_cast(Init->skipRValueSubobjectAdjustments()); if (CXXBindTemporaryExpr *BTE = dyn_cast(Init)) Init = BTE->getSubExpr(); @@ -6218,6 +6212,22 @@ Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, return MTE; } +ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { + // In C++98, we don't want to implicitly create an xvalue. + // FIXME: This means that AST consumers need to deal with "prvalues" that + // denote materialized temporaries. Maybe we should add another ValueKind + // for "xvalue pretending to be a prvalue" for C++98 support. + if (!E->isRValue() || !getLangOpts().CPlusPlus11) + return E; + + // C++1z [conv.rval]/1: T shall be a complete type. + QualType T = E->getType(); + if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) + return ExprError(); + + return CreateMaterializeTemporaryExpr(E->getType(), E, false); +} + ExprResult InitializationSequence::Perform(Sema &S, const InitializedEntity &Entity, @@ -6316,7 +6326,9 @@ InitializationSequence::Perform(Sema &S, if (Args.size() == 1 && Args[0]->getType()->isArrayType() && Entity.getType()->isPointerType() && InitializedEntityOutlivesFullExpression(Entity)) { - Expr *Init = Args[0]; + const Expr *Init = Args[0]->skipRValueSubobjectAdjustments(); + if (auto *MTE = dyn_cast(Init)) + Init = MTE->GetTemporaryExpr(); Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) diff --git a/test/Analysis/temp-obj-dtors-cfg-output.cpp b/test/Analysis/temp-obj-dtors-cfg-output.cpp index b425d91611..a9e4556259 100644 --- a/test/Analysis/temp-obj-dtors-cfg-output.cpp +++ b/test/Analysis/temp-obj-dtors-cfg-output.cpp @@ -1093,9 +1093,9 @@ int testConsistencyNestedNormalReturn(bool value) { // CHECK: 1: int a; // CHECK: 2: NoReturn() (CXXConstructExpr, class NoReturn) // CHECK: 3: [B2.2] (BindTemporary) -// CHECK: 4: [B2.3].f -// CHECK: 5: [B2.4]() -// CHECK: 6: ~NoReturn() (Temporary object destructor) +// CHECK: [[MEMBER:[45]]]: [B2.{{[34]}}].f +// CHECK: {{[56]}}: [B2.[[MEMBER]]]() +// CHECK: {{[67]}}: ~NoReturn() (Temporary object destructor) // CHECK: Preds (1): B3 // CHECK: Succs (1): B0 // CHECK: [B0 (EXIT)] diff --git a/test/CXX/drs/dr4xx.cpp b/test/CXX/drs/dr4xx.cpp index b1c21f8631..ff23ab39ea 100644 --- a/test/CXX/drs/dr4xx.cpp +++ b/test/CXX/drs/dr4xx.cpp @@ -327,7 +327,7 @@ namespace dr420 { // dr420: yes namespace dr421 { // dr421: yes struct X { X(); int n; int &r; }; - int *p = &X().n; // expected-error {{taking the address of a temporary}} + int *p = &X().n; // expected-error-re {{{{taking the address of a temporary|cannot take the address of an rvalue}}}} int *q = &X().r; } diff --git a/test/CXX/drs/dr6xx.cpp b/test/CXX/drs/dr6xx.cpp index 1d37a6d3e8..74b9a546f6 100644 --- a/test/CXX/drs/dr6xx.cpp +++ b/test/CXX/drs/dr6xx.cpp @@ -142,15 +142,21 @@ namespace dr615 { // dr615: yes static int n = f(); } -namespace dr616 { // dr616: no +namespace dr616 { // dr616: 4.0 #if __cplusplus >= 201103L struct S { int n; } s; - // FIXME: These should all be 'int &&' - using T = decltype(S().n); - using T = decltype(static_cast(s).n); - using T = decltype(S().*&S::n); // expected-note 2{{previous}} - using T = decltype(static_cast(s).*&S::n); // expected-error {{different type}} - using T = int&&; // expected-error {{different type}} + S f(); + using T = decltype((S().n)); + using T = decltype((static_cast(s).n)); + using T = decltype((f().n)); + using T = decltype(S().*&S::n); + using T = decltype(static_cast(s).*&S::n); + using T = decltype(f().*&S::n); + using T = int&&; + + using U = decltype(S().n); + using U = decltype(static_cast(s).n); + using U = int; #endif } diff --git a/test/CXX/expr/expr.const/p2-0x.cpp b/test/CXX/expr/expr.const/p2-0x.cpp index b9927e49c7..6d46bf5d77 100644 --- a/test/CXX/expr/expr.const/p2-0x.cpp +++ b/test/CXX/expr/expr.const/p2-0x.cpp @@ -33,11 +33,11 @@ struct NonConstexpr3 { int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}} }; struct NonConstexpr4 { - NonConstexpr4(); // expected-note {{declared here}} + NonConstexpr4(); int n; }; struct NonConstexpr5 { - int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}} + int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-literal type 'NonConstexpr4' cannot be used in a constant expression}} }; // - an invocation of an undefined constexpr function or an undefined @@ -321,7 +321,7 @@ namespace LValueToRValue { // temporary object whose lifetime has not ended, initialized with a // constant expression; constexpr volatile S f() { return S(); } - static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here! + static_assert(f().i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} } diff --git a/test/CodeGenCXX/compound-literals.cpp b/test/CodeGenCXX/compound-literals.cpp index 1e525e237e..0169d5be85 100644 --- a/test/CodeGenCXX/compound-literals.cpp +++ b/test/CodeGenCXX/compound-literals.cpp @@ -38,23 +38,33 @@ int g() { return v[0]; } +// GCC's compound-literals-in-C++ extension lifetime-extends a compound literal +// (or a C++11 list-initialized temporary!) if: +// - it is at global scope +// - it has array type +// - it has a constant initializer + struct Z { int i[3]; }; int *p = (Z){ {1, 2, 3} }.i; // CHECK: define {{.*}}__cxx_global_var_init() -// CHECK: store i32* getelementptr inbounds (%struct.Z, %struct.Z* @.compoundliteral, i32 0, i32 0, i32 0), i32** @p +// CHECK: alloca %struct.Z +// CHECK: store i32* %{{.*}}, i32** @p +int *q = (int [5]){1, 2, 3, 4, 5}; +// CHECK-LABEL: define {{.*}}__cxx_global_var_init.1() +// CHECK: store i32* getelementptr inbounds ([5 x i32], [5 x i32]* @.compoundliteral, i32 0, i32 0), i32** @q int *PR21912_1 = (int []){}; -// CHECK-LABEL: define {{.*}}__cxx_global_var_init.1() -// CHECK: store i32* getelementptr inbounds ([0 x i32], [0 x i32]* @.compoundliteral.2, i32 0, i32 0), i32** @PR21912_1 +// CHECK-LABEL: define {{.*}}__cxx_global_var_init.2() +// CHECK: store i32* getelementptr inbounds ([0 x i32], [0 x i32]* @.compoundliteral.3, i32 0, i32 0), i32** @PR21912_1 union PR21912Ty { long long l; double d; }; union PR21912Ty *PR21912_2 = (union PR21912Ty[]){{.d = 2.0}, {.l = 3}}; -// CHECK-LABEL: define {{.*}}__cxx_global_var_init.3() -// CHECK: store %union.PR21912Ty* getelementptr inbounds ([2 x %union.PR21912Ty], [2 x %union.PR21912Ty]* bitcast (<{ { double }, %union.PR21912Ty }>* @.compoundliteral.4 to [2 x %union.PR21912Ty]*), i32 0, i32 0), %union.PR21912Ty** @PR21912_2 +// CHECK-LABEL: define {{.*}}__cxx_global_var_init.4() +// CHECK: store %union.PR21912Ty* getelementptr inbounds ([2 x %union.PR21912Ty], [2 x %union.PR21912Ty]* bitcast (<{ { double }, %union.PR21912Ty }>* @.compoundliteral.5 to [2 x %union.PR21912Ty]*), i32 0, i32 0), %union.PR21912Ty** @PR21912_2 // This compound literal should have local scope. int computed_with_lambda = [] { diff --git a/test/SemaCXX/constexpr-value-init.cpp b/test/SemaCXX/constexpr-value-init.cpp index 3c969e2af4..53271e0da3 100644 --- a/test/SemaCXX/constexpr-value-init.cpp +++ b/test/SemaCXX/constexpr-value-init.cpp @@ -34,7 +34,7 @@ struct V : virtual C {}; template struct Z : T { constexpr Z() : V() {} }; -constexpr int n = Z().c; // expected-error {{constant expression}} expected-note {{virtual base class}} +constexpr int n = Z().c; // expected-error {{constant expression}} expected-note {{non-literal type 'Z'}} struct E { A a[2]; diff --git a/test/SemaCXX/expression-traits.cpp b/test/SemaCXX/expression-traits.cpp index 51bb90e8bb..d965d14747 100644 --- a/test/SemaCXX/expression-traits.cpp +++ b/test/SemaCXX/expression-traits.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -fcxx-exceptions %s +// RUN: %clang_cc1 -std=c++98 -fsyntax-only -verify -fcxx-exceptions %s // // Tests for "expression traits" intrinsics such as __is_lvalue_expr. diff --git a/test/SemaObjC/assign-rvalue-message.m b/test/SemaObjC/assign-rvalue-message.m index a90cc50cdd..c2cc1c8274 100644 --- a/test/SemaObjC/assign-rvalue-message.m +++ b/test/SemaObjC/assign-rvalue-message.m @@ -1,5 +1,6 @@ // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -Wno-objc-root-class %s -// RUN: %clang_cc1 -x objective-c++ -triple x86_64-apple-darwin10 -fsyntax-only -verify -Wno-objc-root-class %s +// RUN: %clang_cc1 -std=c++98 -x objective-c++ -triple x86_64-apple-darwin10 -fsyntax-only -verify -Wno-objc-root-class %s +// RUN: %clang_cc1 -std=c++11 -x objective-c++ -triple x86_64-apple-darwin10 -fsyntax-only -verify -Wno-objc-root-class %s // rdar://9005189 @interface Foo @@ -19,6 +20,6 @@ struct Bar { - (void)baz { bar.x = 0; - [self bar].x = 10; // expected-error {{assigning to 'readonly' return result of an Objective-C message not allowed}} + [self bar].x = 10; // expected-error-re {{{{assigning to 'readonly' return result of an Objective-C message not allowed|expression is not assignable}}}} } @end diff --git a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp index a981e10f19..dd45ca3ced 100644 --- a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp @@ -594,7 +594,7 @@ TEST(MaterializeTemporaryExpr, MatchesTemporary) { materializeTemporaryExpr())); EXPECT_TRUE( - notMatches(ClassString + + matches(ClassString + "string GetStringByValue();" "void run() { int k = GetStringByValue().length(); }", materializeTemporaryExpr())); diff --git a/www/cxx_dr_status.html b/www/cxx_dr_status.html index 293535238b..a73a5f6379 100644 --- a/www/cxx_dr_status.html +++ b/www/cxx_dr_status.html @@ -813,7 +813,7 @@ 129 CD3 Stability of uninitialized auto variables - Duplicate of 616 + Duplicate of 616 130 @@ -1480,7 +1480,7 @@ accessible? 240 CD3 Uninitialized values and undefined behavior - Duplicate of 616 + Duplicate of 616 241 @@ -1913,7 +1913,7 @@ of class templates 312 CD3 “use” of invalid pointer value not defined - Duplicate of 616 + Duplicate of 616 313 @@ -3739,7 +3739,7 @@ and POD class 616 CD3 Definition of “indeterminate value” - No + SVN 617 -- 2.40.0