]> granicus.if.org Git - clang/commitdiff
allocation functions are always static.
authorAnders Carlsson <andersca@mac.com>
Sun, 15 Nov 2009 18:59:32 +0000 (18:59 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 15 Nov 2009 18:59:32 +0000 (18:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88858 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/CXX/special/class.free/p1.cpp [new file with mode: 0644]

index 34e9acb9adb3e10f1b339ef8049e31a2ddfcc13c..82e9acd91a93d2bb1effc33f96b5b8fa1d64df2c 100644 (file)
@@ -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<CXXRecordDecl>(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 (file)
index 0000000..bf99a27
--- /dev/null
@@ -0,0 +1,11 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+#include <stddef.h>
+
+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}}
+  }
+};