Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang-tidy -fix "fixes" code in template, inserts name of instantiated type #29735

Open Quuxplusone opened 8 years ago

Quuxplusone commented 8 years ago
Bugzilla Link PR30762
Status NEW
Importance P normal
Reported by Stefan Walk (stefan.walk+llvm@gmail.com)
Reported on 2016-10-21 12:18:45 -0700
Last modified on 2016-10-21 12:18:45 -0700
Version unspecified
Hardware PC All
CC alexfh@google.com, djasper@google.com, klimek@google.com
Fixed by commit(s)
Attachments hello.cpp (712 bytes, application/octet-stream)
Blocks
Blocked by
See also
Created attachment 17475
Testcase for the bug

The automatic fix for the "destructor is called, class has virtual members but
no virtual destructor" warning can break templated code. Testcase is below & in
attachment, when using -Wall -Wextra with the below code and running clang-tidy
-fix it will add a qualification for to the "my_t->~T();" line, which is
obviously wrong - it should not touch that template, other classes might be
using it.

/* CMakeLists.txt for compile_commands.json

cmake_minimum_required (VERSION 2.8.11)
project (HELLO)
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
add_executable(hello hello.cpp)
*/

/* hello.cpp */
#include <new>

template <typename T>
struct StupidContainer
{
    char* data;
    T* my_t;
    StupidContainer()
    {
        data = new char[sizeof(T)];
        my_t = reinterpret_cast<T*>(data);
        new (my_t) T();
    }
    ~StupidContainer()
    {
        my_t->~T();
        delete[] data;
    }

};

struct BuggyClass {
    virtual void f() {}
    ~BuggyClass() {}
};

int main()
{
    StupidContainer<BuggyClass> foobar;
}
Quuxplusone commented 8 years ago

Attached hello.cpp (712 bytes, application/octet-stream): Testcase for the bug