]> granicus.if.org Git - clang/commitdiff
Add support to fallback on operator new when a placement operator new[] is called...
authorAaron Ballman <aaron@aaronballman.com>
Thu, 30 May 2013 01:55:39 +0000 (01:55 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Thu, 30 May 2013 01:55:39 +0000 (01:55 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182905 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExprCXX.cpp
test/CodeGenCXX/microsoft-new.cpp [new file with mode: 0644]
test/SemaCXX/microsoft-new-delete.cpp [new file with mode: 0644]

index 21f00b22fc24090482f252d80848ef80b1bd17f2..439fc1636f500d65ee175fd274f974a26409b871 100644 (file)
@@ -1554,13 +1554,28 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
                                /*AllowMissing=*/true, OperatorNew))
       return true;
   }
+
   if (!OperatorNew) {
     // Didn't find a member overload. Look for a global one.
     DeclareGlobalNewDelete();
     DeclContext *TUDecl = Context.getTranslationUnitDecl();
+    bool FallbackEnabled = IsArray && Context.getLangOpts().MicrosoftMode;
     if (FindAllocationOverload(StartLoc, Range, NewName, AllocArgs, TUDecl,
+                               /*AllowMissing=*/FallbackEnabled, OperatorNew,
+                               /*Diagnose=*/!FallbackEnabled)) {
+      if (!FallbackEnabled)
+        return true;
+
+      // MSVC will fall back on trying to find a matching global operator new
+      // if operator new[] cannot be found.  Also, MSVC will leak by not
+      // generating a call to operator delete or operator delete[], but we
+      // will not replicate that bug.
+      NewName = Context.DeclarationNames.getCXXOperatorName(OO_New);
+      DeleteName = Context.DeclarationNames.getCXXOperatorName(OO_Delete);
+      if (FindAllocationOverload(StartLoc, Range, NewName, AllocArgs, TUDecl,
                                /*AllowMissing=*/false, OperatorNew))
       return true;
+    }
   }
 
   // We don't need an operator delete if we're running under
diff --git a/test/CodeGenCXX/microsoft-new.cpp b/test/CodeGenCXX/microsoft-new.cpp
new file mode 100644 (file)
index 0000000..80a5019
--- /dev/null
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -triple i686-pc-win32 -fms-compatibility %s -emit-llvm -o - | FileCheck %s\r
+\r
+struct arbitrary_t {} arbitrary;\r
+void *operator new(unsigned int size, arbitrary_t);\r
+\r
+struct arbitrary2_t {} arbitrary2;\r
+void *operator new[](unsigned int size, arbitrary2_t);\r
+\r
+namespace PR13164 {\r
+  void f() {\r
+       // MSVC will fall back on the non-array operator new.\r
+    void *a;\r
+    int *p = new(arbitrary) int[4];\r
+    // CHECK: call i8* @_Znwj11arbitrary_t(i32 16, %struct.arbitrary_t*\r
+  }\r
+\r
+  struct S {\r
+    void *operator new[](unsigned int size, arbitrary_t);\r
+  };\r
+\r
+  void g() {\r
+    S *s = new(arbitrary) S[2];\r
+    // CHECK: call i8* @_ZN7PR131641SnaEj11arbitrary_t(i32 2, %struct.arbitrary_t*\r
+    S *s1 = new(arbitrary) S;\r
+    // CHECK: call i8* @_Znwj11arbitrary_t(i32 1, %struct.arbitrary_t*\r
+  }\r
+\r
+  struct T {\r
+    void *operator new(unsigned int size, arbitrary2_t);\r
+  };\r
+\r
+  void h() {\r
+    // This should still call the global operator new[].\r
+    T *t = new(arbitrary2) T[2];\r
+    // CHECK: call i8* @_Znaj12arbitrary2_t(i32 2, %struct.arbitrary2_t*\r
+  }\r
+}\r
diff --git a/test/SemaCXX/microsoft-new-delete.cpp b/test/SemaCXX/microsoft-new-delete.cpp
new file mode 100644 (file)
index 0000000..90b42ed
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fms-compatibility -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+struct arbitrary_t {} arbitrary;
+void *operator new(unsigned int size, arbitrary_t);
+
+void f() {
+  // Expect no error in MSVC compatibility mode
+  int *p = new(arbitrary) int[4];
+}