diff options
author | Anders Carlsson <andersca@mac.com> | 2009-03-23 17:49:10 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-03-23 17:49:10 +0000 |
commit | 0d5ca29b7836d55a3a73f83d7e36b4bd35f8d85f (patch) | |
tree | ce963ce70ddff4d1226de8d14643fe5e9c8ba5f0 /clang | |
parent | 893c2c963a5db7abddfa4b37e9d69f89131aafbd (diff) | |
download | bcm5719-llvm-0d5ca29b7836d55a3a73f83d7e36b4bd35f8d85f.tar.gz bcm5719-llvm-0d5ca29b7836d55a3a73f83d7e36b4bd35f8d85f.zip |
It's an error to try to allocate an abstract object using new.
llvm-svn: 67542
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 | ||||
-rw-r--r-- | clang/lib/Sema/Sema.h | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 9 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDeclCXX.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 4 | ||||
-rw-r--r-- | clang/test/SemaCXX/abstract.cpp | 3 |
6 files changed, 21 insertions, 6 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 699c3db7b83..b00f47cf8ab 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -220,7 +220,9 @@ def err_static_assert_expression_is_not_constant : Error< 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">; diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 8bc5a6073b9..e8702bcea10 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -1634,7 +1634,8 @@ public: 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] diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index bcb21c33ce9..c964daf6fd9 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1641,7 +1641,9 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC, } // 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 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, // 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 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, // 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 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, } // 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. diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 6be2052fdf5..ad2dba7ce4b 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -784,7 +784,7 @@ namespace { } bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, - unsigned SelID) { + unsigned DiagID, unsigned SelID) { if (!getLangOptions().CPlusPlus) return false; @@ -800,7 +800,7 @@ bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, 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. diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index b3d94c372dc..ac36a1f22df 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -238,6 +238,10 @@ Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, 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); diff --git a/clang/test/SemaCXX/abstract.cpp b/clang/test/SemaCXX/abstract.cpp index e3c7651efb0..1b6099ae7a5 100644 --- a/clang/test/SemaCXX/abstract.cpp +++ b/clang/test/SemaCXX/abstract.cpp @@ -25,6 +25,8 @@ class E : D { 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 @@ void t2(C); // expected-error {{parameter type 'C' is an abstract class}} struct S { C c; // expected-error {{field type 'C' is an abstract class}} }; + |