llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.65k stars 11.84k forks source link

[llvm 12.0.1] g++ 11 warns of mismatched-new-delete #52997

Open cktan opened 2 years ago

cktan commented 2 years ago

Hi,

Compiler: g++-11 (Ubuntu 11-20210417-1ubuntu1) 11.0.1 20210417 (experimental) [master revision c1c86ab96c2:b6fb0ccbb48:8ae884c09fbba91e9cec391290ee4a2859e7ff41]

LLVM: 12.0.1 built locally with g++-11.

I have a repro case below that caused g++ 11 to emit mismatched-new-delete warning. This may be more of a gcc-11 problem than llvm.

$ cat test.cpp
#include "llvm/IR/IRBuilder.h"

void foo(llvm::IRBuilder<>& builder, llvm::BasicBlock* block)
{
  builder.SetInsertPoint(block);
  builder.CreateICmpEQ(0, 0);
}

Compiling it with -O0:

$  g++-11 -Wall -Wextra -Wno-unused-parameter -I../llvm-12-release+assert/include -std=c++17 -fno-rtti -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -c -o test.o test.cpp
In file included from test.cpp:1:
../llvm-12-release+assert/include/llvm/IR/IRBuilder.h: In member function ‘llvm::Value* llvm::IRBuilderBase::CreateICmp(llvm::CmpInst::Predicate, llvm::Value*, llvm::Value*, const llvm::Twine&)’:
../llvm-12-release+assert/include/llvm/IR/IRBuilder.h:2385:43: warning: ‘static void llvm::User::operator delete(void*)’ called on pointer returned from a mismatched allocation function [-Wmismatched-new-delete]
 2385 |     return Insert(new ICmpInst(P, LHS, RHS), Name);
      |                                           ^

Compiling with -O1 will not result in warnings.

msebor commented 2 years ago

The GCC warning issues false positives when one of the allocation and deallocation functions is inlined into its caller but not the other. There's some detail in GCC bugs 103993 and 100861.

EugeneZelenko commented 2 years ago

Could you please try version 14 or main?