[cfe-commits] r96396 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/AST/Expr.cpp lib/Sema/SemaExpr.cpp test/SemaCXX/address-of-temporary.cpp

Douglas Gregor dgregor at apple.com
Tue Feb 16 13:40:13 PST 2010


Author: dgregor
Date: Tue Feb 16 15:39:57 2010
New Revision: 96396

URL: http://llvm.org/viewvc/llvm-project?rev=96396&view=rev
Log:
Introduce a new kind of failed result for isLvalue/isModifiableLvalue
which describes temporary objects of class type in C++. Use this to
provide a more-specific, remappable diagnostic when takin the address
of such a temporary.

Added:
    cfe/trunk/test/SemaCXX/address-of-temporary.cpp   (with props)
Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=96396&r1=96395&r2=96396&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Feb 16 15:39:57 2010
@@ -150,7 +150,8 @@
     LV_InvalidExpression,
     LV_MemberFunction,
     LV_SubObjCPropertySetting,
-    LV_SubObjCPropertyGetterSetting
+    LV_SubObjCPropertyGetterSetting,
+    LV_ClassTemporary
   };
   isLvalueResult isLvalue(ASTContext &Ctx) const;
 
@@ -181,7 +182,8 @@
     MLV_NoSetterProperty,
     MLV_MemberFunction,
     MLV_SubObjCPropertySetting,
-    MLV_SubObjCPropertyGetterSetting
+    MLV_SubObjCPropertyGetterSetting,
+    MLV_ClassTemporary
   };
   isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx,
                                               SourceLocation *Loc = 0) const;

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=96396&r1=96395&r2=96396&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Feb 16 15:39:57 2010
@@ -18,8 +18,9 @@
     
 
 
-// Empty DiagGroups: these are recognized by clang but ignored.
+// Empty DiagGroups are recognized by clang but ignored.
 def : DiagGroup<"address">;
+def AddressOfTemporary : DiagGroup<"address-of-temporary">;
 def : DiagGroup<"aggregate-return">;
 def : DiagGroup<"attributes">;
 def : DiagGroup<"bad-function-cast">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=96396&r1=96395&r2=96396&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Feb 16 15:39:57 2010
@@ -1783,6 +1783,11 @@
   "must explicitly qualify member function %0 when taking its address">;
 def err_typecheck_invalid_lvalue_addrof : Error<
   "address expression must be an lvalue or a function designator">;
+def ext_typecheck_addrof_class_temporary : ExtWarn<
+  "taking the address of a temporary object of type %0">, 
+  InGroup<DiagGroup<"address-of-temporary">>, DefaultError;
+def err_typecheck_addrof_class_temporary : Error<
+  "taking the address of a temporary object of type %0">;
 def err_typecheck_unary_expr : Error<
   "invalid argument type %0 to unary expression">;
 def err_typecheck_indirection_requires_pointer : Error<

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=96396&r1=96395&r2=96396&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue Feb 16 15:39:57 2010
@@ -1120,8 +1120,15 @@
       return LV_Valid;
     break;
   case ImplicitCastExprClass:
-    return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
-                                                       : LV_InvalidExpression;
+    if (cast<ImplicitCastExpr>(this)->isLvalueCast())
+      return LV_Valid;
+
+    // If this is a conversion to a class temporary, make a note of
+    // that.
+    if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
+      return LV_ClassTemporary;
+
+    break;
   case ParenExprClass: // C99 6.5.1p5
     return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
   case BinaryOperatorClass:
@@ -1171,9 +1178,15 @@
     if (ReturnType->isLValueReferenceType())
       return LV_Valid;
 
+    // If the function is returning a class temporary, make a note of
+    // that.
+    if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
+      return LV_ClassTemporary;
+
     break;
   }
   case CompoundLiteralExprClass: // C99 6.5.2.5p5
+    // FIXME: Is this what we want in C++?
     return LV_Valid;
   case ChooseExprClass:
     // __builtin_choose_expr is an lvalue if the selected operand is.
@@ -1207,6 +1220,13 @@
     if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
           isLValueReferenceType())
       return LV_Valid;
+
+    // If this is a conversion to a class temporary, make a note of
+    // that.
+    if (Ctx.getLangOptions().CPlusPlus && 
+        cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
+      return LV_ClassTemporary;
+
     break;
   case CXXTypeidExprClass:
     // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
@@ -1253,6 +1273,11 @@
         return LV_Valid;
     break;
 
+  case Expr::CXXConstructExprClass:
+  case Expr::CXXTemporaryObjectExprClass:
+  case Expr::CXXZeroInitValueExprClass:
+    return LV_ClassTemporary;
+
   default:
     break;
   }
@@ -1296,6 +1321,8 @@
   case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
   case LV_SubObjCPropertyGetterSetting: 
     return MLV_SubObjCPropertyGetterSetting;
+  case LV_ClassTemporary:
+    return MLV_ClassTemporary;
   }
 
   // The following is illegal:

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=96396&r1=96395&r2=96396&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Feb 16 15:39:57 2010
@@ -5697,7 +5697,6 @@
   unsigned Diag = 0;
   bool NeedType = false;
   switch (IsLV) { // C99 6.5.16p2
-  default: assert(0 && "Unknown result from isModifiableLvalue!");
   case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break;
   case Expr::MLV_ArrayType:
     Diag = diag::err_typecheck_array_not_modifiable_lvalue;
@@ -5710,7 +5709,11 @@
   case Expr::MLV_LValueCast:
     Diag = diag::err_typecheck_lvalue_casts_not_supported;
     break;
+  case Expr::MLV_Valid:
+    llvm_unreachable("did not take early return for MLV_Valid");
   case Expr::MLV_InvalidExpression:
+  case Expr::MLV_MemberFunction:
+  case Expr::MLV_ClassTemporary:
     Diag = diag::err_typecheck_expression_not_modifiable_lvalue;
     break;
   case Expr::MLV_IncompleteType:
@@ -5995,6 +5998,12 @@
     return Context.getMemberPointerType(op->getType(),
                 Context.getTypeDeclType(cast<RecordDecl>(dcl->getDeclContext()))
                        .getTypePtr());
+  } else if (lval == Expr::LV_ClassTemporary) {
+    Diag(OpLoc, isSFINAEContext()? diag::err_typecheck_addrof_class_temporary
+                                 : diag::ext_typecheck_addrof_class_temporary)
+      << op->getType() << op->getSourceRange();
+    if (isSFINAEContext())
+      return QualType();
   } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) {
     // C99 6.5.3.2p1
     // The operand must be either an l-value or a function designator

Added: cfe/trunk/test/SemaCXX/address-of-temporary.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/address-of-temporary.cpp?rev=96396&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/address-of-temporary.cpp (added)
+++ cfe/trunk/test/SemaCXX/address-of-temporary.cpp Tue Feb 16 15:39:57 2010
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -Wno-error=address-of-temporary -verify %s
+struct X { 
+  X();
+  X(int);
+  X(int, int);
+};
+
+void *f0() { return &X(); } // expected-warning{{taking the address of a temporary object}}
+void *f1() { return &X(1); } // expected-warning{{taking the address of a temporary object}}
+void *f2() { return &X(1, 2); } // expected-warning{{taking the address of a temporary object}}
+void *f3() { return &(X)1; } // expected-warning{{taking the address of a temporary object}}
+

Propchange: cfe/trunk/test/SemaCXX/address-of-temporary.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaCXX/address-of-temporary.cpp

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaCXX/address-of-temporary.cpp

------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list