eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.22k stars 169 forks source link

Crash on mingw clang for virtual destructor #210

Open dariusarnold opened 4 years ago

dariusarnold commented 4 years ago

Hello, I have a problem using virtual destructors in classes that I want to mock. The code compiles with clang++ and g++ from mingw on Windows, but it only runs when compiled with g++. The executable created by clang will crash. If I make the destructor non virtual, it will run when compiled with clang++ as well. I compiled without optimizations (-O0) on both compilers. Minimal code example:

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <fakeit.hpp>

class Interface {
public:
    virtual ~Interface() = 0;
    virtual int f() = 0;
};

Interface::~Interface() {}

TEST_CASE("Test", "[tag]") {
    using namespace fakeit;
    Mock<Interface> mock;
    When(Method(mock, f)).AlwaysReturn(42);
    REQUIRE(mock.get().f() == 42);
}

G++ and clang++ versions:

$ g++ --version
g++.exe (Rev2, Built by MSYS2 project) 9.2.0

$ clang++ --version
clang version 10.0.0 (https://github.com/msys2/MINGW-packages.git 3f880aaba91a3d9cdfb222dc270274731a2119a9)
Target: i686-w64-windows-gnu
Thread model: posix

A larger example built with CMake still using mingw clang 10.0.0 gives an error message during compilation. The whole build is still successful, but the executable will crash when run.

D:/git/master/cmake/../../test/fakeit\fakeit.hpp:6142:36: note: in instantiation of template class 'fakeit::FakeObject<IGetDefaultLanguage>' requested here
        static_assert(sizeof(C) == sizeof(FakeObject<C, baseclasses...>), "This is a problem");
                                   ^
D:/git/master/cmake/../../test/fakeit\fakeit.hpp:8057:35: note: in instantiation of template class 'fakeit::DynamicProxy<IGetDefaultLanguage>' requested here
                DynamicProxy<C, baseclasses...> _proxy;
                                                ^
D:/git/master/cmake/../../test/fakeit\fakeit.hpp:8307:37: note: in instantiation of template class 'fakeit::MockImpl<IGetDefaultLanguage>' requested here
        MockImpl<C, baseclasses...> impl;
                                    ^
D:/git/master/test/src/test.cpp:14:31: note: in instantiation of template class 'fakeit::Mock<IGetDefaultLanguage>' requested here
    Mock<IGetDefaultLanguage> languageMock;

Using clang from WSL, the executable will run without crashing.

$ clang++ --version
clang version 7.0.1-8 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin