[cfe-commits] r67542 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclCXX.cpp lib/Sema/SemaExprCXX.cpp test/SemaCXX/abstract.cpp

Anders Carlsson andersca at mac.com
Mon Mar 23 10:49:11 PDT 2009


Author: andersca
Date: Mon Mar 23 12:49:10 2009
New Revision: 67542

URL: http://llvm.org/viewvc/llvm-project?rev=67542&view=rev
Log:
It's an error to try to allocate an abstract object using new.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/SemaCXX/abstract.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Mar 23 12:49:10 2009
@@ -220,7 +220,9 @@
 def err_static_assert_failed : Error<"static_assert failed \"%0\"">;
 
 def err_abstract_type_in_decl : Error<
-  "%select{return|parameter|variable|field}0 type %1 is an abstract class">;
+  "%select{return|parameter|variable|field}1 type %0 is an abstract class">;
+def err_allocation_of_abstract_type : Error<
+  "allocation of an object of abstract type %0">;
   
 def note_pure_virtual_function : Note<
   "pure virtual function %0">;

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=67542&r1=67541&r2=67542&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Mon Mar 23 12:49:10 2009
@@ -1634,7 +1634,8 @@
                                     SourceLocation Loc, SourceRange Range);
   std::string getAmbiguousPathsDisplayString(BasePaths &Paths);
 
-  bool RequireNonAbstractType(SourceLocation Loc, QualType T, unsigned SelID);
+  bool RequireNonAbstractType(SourceLocation Loc, QualType T, 
+                              unsigned DiagID, unsigned SelID);
 
   //===--------------------------------------------------------------------===//
   // C++ Overloaded Operators [C++ 13.5]

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Mar 23 12:49:10 2009
@@ -1641,7 +1641,9 @@
   }
 
   // The variable can not have an abstract class type.
-  if (RequireNonAbstractType(D.getIdentifierLoc(), R, 2 /* variable type */))
+  if (RequireNonAbstractType(D.getIdentifierLoc(), R, 
+                             diag::err_abstract_type_in_decl, 
+                             2 /* variable type */))
     InvalidDecl = true;
     
   // The variable can not 
@@ -1815,6 +1817,7 @@
   // Check that the return type is not an abstract class type.
   if (RequireNonAbstractType(D.getIdentifierLoc(), 
                              R->getAsFunctionType()->getResultType(),
+                             diag::err_abstract_type_in_decl, 
                              0 /* return type */))
     InvalidDecl = true;
   
@@ -1996,6 +1999,7 @@
         
         // Function parameters cannot have abstract class types.
         if (RequireNonAbstractType(PVD->getLocation(), PVD->getType(),
+                                   diag::err_abstract_type_in_decl, 
                                    1 /* parameter type */))
             InvalidDecl = true;
         Params.push_back(PVD);
@@ -3534,7 +3538,8 @@
   }
   
   // Fields can not have abstract class types
-  if (RequireNonAbstractType(Loc, T, 3 /* field type */))
+  if (RequireNonAbstractType(Loc, T, diag::err_abstract_type_in_decl, 
+                             3 /* field type */))
     InvalidDecl = true;
   
   // If this is declared as a bit-field, check the bit-field.

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Mar 23 12:49:10 2009
@@ -784,7 +784,7 @@
 }
 
 bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, 
-                                  unsigned SelID) {
+                                  unsigned DiagID, unsigned SelID) {
   
   if (!getLangOptions().CPlusPlus)
     return false;
@@ -800,7 +800,7 @@
   if (!RD->isAbstract())
     return false;
   
-  Diag(Loc, diag::err_abstract_type_in_decl) << SelID << RD->getDeclName();
+  Diag(Loc, DiagID) << RD->getDeclName() << SelID;
   
   // Check if we've already emitted the list of pure virtual functions for this
   // class.

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Mon Mar 23 12:49:10 2009
@@ -238,6 +238,10 @@
   if (CheckAllocatedType(AllocType, D))
     return ExprError();
 
+  if (RequireNonAbstractType(D.getSourceRange().getBegin(), AllocType,
+                             diag::err_allocation_of_abstract_type, 0))
+    return ExprError();
+  
   QualType ResultType = AllocType->isDependentType()
                           ? Context.DependentTy
                           : Context.getPointerType(AllocType);

Modified: cfe/trunk/test/SemaCXX/abstract.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/abstract.cpp?rev=67542&r1=67541&r2=67542&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/abstract.cpp (original)
+++ cfe/trunk/test/SemaCXX/abstract.cpp Mon Mar 23 12:49:10 2009
@@ -25,6 +25,8 @@
 
 static_assert(!__is_abstract(E), "E inherits from an abstract class but implements f");
 
+C *d = new C; // expected-error {{allocation of an object of abstract type 'C'}}
+
 C c; // expected-error {{variable type 'C' is an abstract class}}
 void t1(C c); // expected-error {{parameter type 'C' is an abstract class}}
 void t2(C); // expected-error {{parameter type 'C' is an abstract class}}
@@ -32,3 +34,4 @@
 struct S {
   C c; // expected-error {{field type 'C' is an abstract class}}
 };
+





More information about the cfe-commits mailing list