[PATCH] D41408: [analyzer] NFC: Fix nothrow operator new definition in a test.

Artem Dergachev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 19 11:44:36 PST 2017

NoQ created this revision.
NoQ added reviewers: dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet.
Herald added subscribers: cfe-commits, rnkovacs.

Fun C++ fact: definition

  void *operator new(std::size_t size, std::nothrow_t &nothrow) throw() { ... }

does not override the global nothrow operator new. The standard nothrow operator new would still be called when this definition is present. Because, well, the second parameter (`std::nothrow_t &nothrow`) is missing a `const` qualifier, so it's a completely different function. In fact, temporary of type `std::nothrow_t` would never be bound to a non-const reference at all. So the custom operator defined above is also very hard to call.

This patch fixes the test case. The analyzer behavior is intended (at least for now, see also discussion in https://reviews.llvm.org/D41406) regardless of whether the operator is overridden correctly or not, but tests now actually test it.

  rC Clang



Index: test/Analysis/NewDelete-custom.cpp
--- test/Analysis/NewDelete-custom.cpp
+++ test/Analysis/NewDelete-custom.cpp
@@ -4,16 +4,16 @@
 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks,unix.Malloc -std=c++11 -analyzer-config c++-allocator-inlining=true -DLEAKS=1 -DALLOCATOR_INLINING=1 -fblocks -verify %s
 #include "Inputs/system-header-simulator-cxx.h"
-#if !LEAKS
 // expected-no-diagnostics
 void *allocator(std::size_t size);
 void *operator new[](std::size_t size) throw() { return allocator(size); }
 void *operator new(std::size_t size) throw() { return allocator(size); }
-void *operator new(std::size_t size, std::nothrow_t& nothrow) throw() { return allocator(size); }
+void *operator new(std::size_t size, const std::nothrow_t &nothrow) throw() { return allocator(size); }
 void *operator new(std::size_t, double d);
 class C {
@@ -59,16 +59,13 @@
 //----- Custom NoThrow placement operators
 void testOpNewNoThrow() {
-  void *p = operator new(0, std::nothrow);
+  void *p = operator new(0, std::nothrow); // call is inlined, no warn
-#if LEAKS
-// expected-warning at -2{{Potential leak of memory pointed to by 'p'}}
 void testNewExprNoThrow() {
   int *p = new(std::nothrow) int;
-#if LEAKS
 // expected-warning at -2{{Potential leak of memory pointed to by 'p'}}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41408.127580.patch
Type: text/x-patch
Size: 1507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171219/89827c59/attachment.bin>

More information about the cfe-commits mailing list