From 67bf2e7706b82d5debd5c189d9454130df7db0d8 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 15 Nov 2009 18:59:32 +0000 Subject: [PATCH] allocation functions are always static. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88858 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 11 ++++++++++- test/CXX/special/class.free/p1.cpp | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/CXX/special/class.free/p1.cpp diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 34e9acb9ad..82e9acd91a 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -2612,10 +2612,19 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, return 0; } + bool isStatic = SC == FunctionDecl::Static; + + // [class.free]p1: + // Any allocation function for a class T is a static member + // (even if not explicitly declared static). + if (Name.getCXXOverloadedOperator() == OO_New || + Name.getCXXOverloadedOperator() == OO_Array_New) + isStatic = true; + // This is a C++ method declaration. NewFD = CXXMethodDecl::Create(Context, cast(DC), D.getIdentifierLoc(), Name, R, DInfo, - (SC == FunctionDecl::Static), isInline); + isStatic, isInline); isVirtualOkay = (SC != FunctionDecl::Static); } else { diff --git a/test/CXX/special/class.free/p1.cpp b/test/CXX/special/class.free/p1.cpp new file mode 100644 index 0000000000..bf99a2782b --- /dev/null +++ b/test/CXX/special/class.free/p1.cpp @@ -0,0 +1,11 @@ +// RUN: clang-cc -fsyntax-only -verify %s +#include + +struct A { + void *operator new(size_t) { + return this; // expected-error {{invalid use of 'this' outside of a nonstatic member function}} + } + void *operator new[](size_t) { + return this; // expected-error {{invalid use of 'this' outside of a nonstatic member function}} + } +};