[cfe-commits] r86755 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDeclCXX.cpp test/CodeGenCXX/new-operator-phi.cpp test/SemaCXX/new-delete.cpp
Fariborz Jahanian
fjahanian at apple.com
Tue Nov 10 15:47:18 PST 2009
Author: fjahanian
Date: Tue Nov 10 17:47:18 2009
New Revision: 86755
URL: http://llvm.org/viewvc/llvm-project?rev=86755&view=rev
Log:
Diagnose illegally typed operator new/new[].
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
cfe/trunk/test/CodeGenCXX/new-operator-phi.cpp
cfe/trunk/test/SemaCXX/new-delete.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=86755&r1=86754&r2=86755&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Nov 10 17:47:18 2009
@@ -2108,6 +2108,10 @@
def err_operator_overload_variadic : Error<"overloaded %0 cannot be variadic">;
def err_operator_overload_static : Error<
"overloaded %0 cannot be a static member function">;
+def err_operator_new_param_type : Error<
+ "%0 takes type size_t (%1) as first parameter">;
+def err_operator_new_result_type : Error<
+ "%0 must return type %1">;
def err_operator_overload_default_arg : Error<
"parameter of overloaded %0 cannot have a default argument">;
def err_operator_overload_must_be : Error<
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=86755&r1=86754&r2=86755&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Nov 10 17:47:18 2009
@@ -4006,9 +4006,28 @@
// found in the rest of this subclause do not apply to them unless
// explicitly stated in 3.7.3.
// FIXME: Write a separate routine for checking this. For now, just allow it.
- if (Op == OO_New || Op == OO_Array_New ||
- Op == OO_Delete || Op == OO_Array_Delete)
+ if (Op == OO_Delete || Op == OO_Array_Delete)
return false;
+
+ if (Op == OO_New || Op == OO_Array_New) {
+ bool ret = false;
+ if (FunctionDecl::param_iterator Param = FnDecl->param_begin()) {
+ QualType SizeTy = Context.getCanonicalType(Context.getSizeType());
+ QualType T = Context.getCanonicalType((*Param)->getType());
+ if (!T->isDependentType() && SizeTy != T) {
+ Diag(FnDecl->getLocation(),
+ diag::err_operator_new_param_type) << FnDecl->getDeclName()
+ << SizeTy;
+ ret = true;
+ }
+ }
+ QualType ResultTy = Context.getCanonicalType(FnDecl->getResultType());
+ if (!ResultTy->isDependentType() && ResultTy != Context.VoidPtrTy)
+ return Diag(FnDecl->getLocation(),
+ diag::err_operator_new_result_type) << FnDecl->getDeclName()
+ << Context.VoidPtrTy;
+ return ret;
+ }
// C++ [over.oper]p6:
// An operator function shall either be a non-static member
Modified: cfe/trunk/test/CodeGenCXX/new-operator-phi.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/new-operator-phi.cpp?rev=86755&r1=86754&r2=86755&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/new-operator-phi.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/new-operator-phi.cpp Tue Nov 10 17:47:18 2009
@@ -1,7 +1,7 @@
// RUN: clang-cc -emit-llvm-only -verify %s
// PR5454
-class X {static void * operator new(unsigned size) throw(); X(int); };
+class X {static void * operator new(unsigned long size) throw(); X(int); };
int a(), b();
void b(int x)
{
Modified: cfe/trunk/test/SemaCXX/new-delete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/new-delete.cpp?rev=86755&r1=86754&r2=86755&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/new-delete.cpp (original)
+++ cfe/trunk/test/SemaCXX/new-delete.cpp Tue Nov 10 17:47:18 2009
@@ -137,3 +137,31 @@
public:
void Destroy() const { delete this; }
};
+
+class Base {
+public:
+ static int operator new(unsigned size) throw(); // expected-error {{'operator new' takes type size_t}} \
+ // expected-error {{operator new' must return type 'void *'}}
+ static int operator new[] (unsigned size) throw(); // expected-error {{'operator new[]' takes type size_t}} \
+ // expected-error {{operator new[]' must return type 'void *'}}
+};
+
+class Tier {};
+class Comp : public Tier {};
+
+class Thai : public Base {
+public:
+ Thai(const Tier *adoptDictionary);
+};
+
+void loadEngineFor() {
+ const Comp *dict;
+ new Thai(dict);
+}
+
+template <class T> struct TBase {
+ void* operator new(T size, int); // expected-error {{'operator new' takes type size_t}}
+};
+
+TBase<int> t1; // expected-note {{in instantiation of template class 'struct TBase<int>' requested here}}
+
More information about the cfe-commits
mailing list