From eb0c532faa826e512c4513aae918125f72007748 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Mon, 23 Mar 2009 19:10:31 +0000 Subject: [PATCH] More improvements to abstract type checking. Handle arrays correctly, and make sure to check parameter types before they decay. llvm-svn: 67550 --- clang/lib/Sema/SemaDecl.cpp | 13 +++++++------ clang/lib/Sema/SemaDeclCXX.cpp | 3 +++ clang/lib/Sema/SemaExprCXX.cpp | 4 ++++ clang/test/SemaCXX/abstract.cpp | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index c964daf6fd96..d71417b95775 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1996,12 +1996,6 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { ParmVarDecl *PVD = (ParmVarDecl *)FTI.ArgInfo[i].Param; - - // 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); } } @@ -2611,6 +2605,13 @@ Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { // FIXME: If a source translation tool needs to see the original type, then // we need to consider storing both types (in ParmVarDecl)... // + + // Parameters can not be abstract class types. + if (RequireNonAbstractType(D.getIdentifierLoc(), parmDeclType, + diag::err_abstract_type_in_decl, + 1 /* parameter type */)) + D.setInvalidType(true); + if (parmDeclType->isArrayType()) { // int x[restrict 4] -> int *restrict parmDeclType = Context.getArrayDecayedType(parmDeclType); diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index ad2dba7ce4b8..6f33c0052651 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -788,6 +788,9 @@ bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, if (!getLangOptions().CPlusPlus) return false; + + if (const ArrayType *AT = Context.getAsArrayType(T)) + return RequireNonAbstractType(Loc, AT->getElementType(), DiagID, SelID); const RecordType *RT = T->getAsRecordType(); if (!RT) diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index ac36a1f22df9..271f1da3adb3 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -197,6 +197,10 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, diag::err_invalid_incomplete_type_use, FullRange)) return ExprError(); + if (RequireNonAbstractType(TyBeginLoc, Ty, + diag::err_allocation_of_abstract_type, 0)) + return ExprError(); + exprs.release(); return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); } diff --git a/clang/test/SemaCXX/abstract.cpp b/clang/test/SemaCXX/abstract.cpp index 1b6099ae7a56..08baacc49d6e 100644 --- a/clang/test/SemaCXX/abstract.cpp +++ b/clang/test/SemaCXX/abstract.cpp @@ -35,3 +35,20 @@ struct S { C c; // expected-error {{field type 'C' is an abstract class}} }; +void t3(const C&); + +void f() { + C(); // expected-error {{allocation of an object of abstract type 'C'}} + t3(C()); // expected-error {{allocation of an object of abstract type 'C'}} +} + +C e[2]; // expected-error {{variable type 'C' is an abstract class}} + +void t4(C c[2]); // expected-error {{parameter type 'C' is an abstract class}} + +void t5(void (*)(C)); // expected-error {{parameter type 'C' is an abstract class}} + +typedef void (*Func)(C); // expected-error {{parameter type 'C' is an abstract class}} +void t6(Func); + +