[Bug c++/70834] Incorrect warning for placement new when conditionally used

2021-05-18 Thread frankhb1989 at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

frankhb1989 at gmail dot com changed:

   What|Removed |Added

 CC||frankhb1989 at gmail dot com

--- Comment #8 from frankhb1989 at gmail dot com ---
(In reply to Martin Sebor from comment #7)
> No change in GCC 11.  This false positive too will be avoided by running
> -Wplacement-new later, when the program is in SSA form, rather than in the
> front end.

Actually worse in GCC 11. Try this case:

#include 
#include 
#include 
#include 
#include 

struct hdr_t
{
void* p_block;
};

constexpr auto offset = sizeof(hdr_t) > alignof(hdr_t) ? sizeof(hdr_t) :
alignof(hdr_t);


void* operator new(std::size_t bytes, std::size_t alignment)
{
auto space(offset + bytes + alignment);
auto ptr(static_cast(std::malloc(space)));
void* p([offset]);

if(std::align(alignment, bytes, p, space))
{
(::new(static_cast(static_cast(p) - offset))
hdr_t)->p_block = ptr;
//  (::new(ptr + (std::size_t(static_cast(p) - ptr)) -
offset) hdr_t)->p_block = ptr;
return p;
}
throw std::bad_alloc();
}

int main()
{}

This is warned in G++ 11.1, but not G++ 10.2.

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2021-05-03 Thread msebor at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

Martin Sebor  changed:

   What|Removed |Added

   Last reconfirmed|2017-01-03 00:00:00 |2021-5-3
 Blocks||100399
  Known to fail|8.0 |11.1.0, 12.0, 8.4.0

--- Comment #7 from Martin Sebor  ---
No change in GCC 11.  This false positive too will be avoided by running
-Wplacement-new later, when the program is in SSA form, rather than in the
front end.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100399
[Bug 100399] bogus/missing -Wplacement-new

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2017-11-27 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

Martin Sebor  changed:

   What|Removed |Added

  Known to fail||6.4.0, 7.2.0, 8.0

--- Comment #6 from Martin Sebor  ---
No change in 8.0.

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2017-01-03 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

--- Comment #5 from Martin Sebor  ---
As a workaround until the warning is made smarter, partial specialization can
be used to avoid the compile-time conditional, for example like so:

struct Test {
  char buf[4];

  template
  struct Ctor {
static T *construct(void*) {
  return new T;  // else make it on the heap
}
  };

  template T* construct () {
return Ctor::construct (buf);
  }
};

template 
struct Test::Ctor {
  static T* construct (void *p) {
return new (p) T;
  }
};

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2017-01-03 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

Martin Sebor  changed:

   What|Removed |Added

 CC||msebor at gcc dot gnu.org

--- Comment #4 from Martin Sebor  ---
I'm hoping to move -Wplacement-new to the middle end in GCC 8 and have it make
use of object size checking similarly to -Wstringop-overflow.

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2017-01-03 Thread manu at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

Manuel López-Ibáñez  changed:

   What|Removed |Added

   Keywords||diagnostic
 Status|UNCONFIRMED |NEW
   Last reconfirmed||2017-01-03
 CC||manu at gcc dot gnu.org
 Ever confirmed|0   |1

--- Comment #3 from Manuel López-Ibáñez  ---
The warning comes from the FE, which knows nothing about control-flow. We have
many open PRs regarding this (see PR23577) but it is not something that will
change in the near future.

It may work if you use ?: since that is optimised very early (but perhaps not
before this warning, I don't know).

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2017-01-03 Thread matt at godbolt dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

--- Comment #2 from Matt Godbolt  ---
if constexpr is of course the right solution here, for certain.

I appreciate the compiler determining which if() branch to take in the
non-constexpr case is tricky.

[Bug c++/70834] Incorrect warning for placement new when conditionally used

2017-01-03 Thread roel at abittechnical dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

--- Comment #1 from Roel Standaert  ---
I guess you can't really expect the compiler to correctly infer the value of
the if statement at compile time?

If std::enable_if is used instead of an if statement, GCC won't generate a
warning: https://godbolt.org/g/GO7Ao7

Also, if you use C++17's "if constexpr", it's fine:
https://godbolt.org/g/H58gBI