Kenneth Zheng
Kenneth Zheng

Reputation: 29

CLANG compiler 7.0.0 will been crashed when compile one C++ code

I've written one C++ code to test virtual method. But this clang compiler + VS2017 will throw one exception and then crashed on. I don't test it in the CLANG compiler in linux, But the GCC compiler 7.3.0 worked normal and then it can found the error code in lines:38 "virual ~Tuna() { cout << "Tuna Destructor." << endl; }". Please see below test C++ code for it:

test.cpp: (CLANG 7.3.0 throws exception and then crash at line 38. but GCC compiler worked normally on)

//#include <bits/stdc++.h>
#include <iostream>

using namespace std;

class Fish {
private:
public:
  Fish() { cout << "Fish Constructor." << endl; }

  Fish(const Fish &copySource) { cout << "Fish Copy Constructor." << endl; }

  Fish(Fish &&moveSource) { cout << "Fish Move Constructor." << endl; }

  ~Fish() { cout << "Fish Destructor." << endl; }

  const Fish &operator=(const Fish &copySource)
  {
    cout << "Fish Assignment Operator=" << endl;
    if (this != &copySource) {}
    return *this;
  }

  operator const char *() { return typeid(Fish).name(); }

  virtual void Swim() { cout << "Fish swims!" << endl; }
};

class Tuna : public Fish {
private:
public:
  Tuna() { cout << "Tuna Constructor." << endl; }

  Tuna(const Tuna &copySource) { cout << "Tuna Copy Constructor." << endl; }

  Tuna(Tuna &&moveSource) { cout << "Tuna Move Constructor." << endl; }

  virual ~Tuna() { cout << "Tuna Destructor." << endl; }

  const Tuna &operator=(const Tuna &copySource)
  {
    cout << "Tuna Assignment Operator=" << endl;
    if (this != &copySource) {}
    return *this;
  }

  operator const char *() { return typeid(Tuna).name(); }

  void Swim() override { cout << "Tuna swims!" << endl; }
};

class Carp : public Tuna {
private:
public:
  Carp() { cout << "Carp Constructor." << endl; }

  Carp(const Carp &copySource) { cout << "Carp Copy Constructor." << endl; }

  Carp(Carp &&moveSource) { cout << "Carp Move Constructor." << endl; }

  ~Carp() { cout << "Carp Destructor." << endl; }

  const Carp &operator=(const Carp &copySource)
  {
    cout << "Carp Assignment Operator=" << endl;
    if (this != &copySource) {}
    return *this;
  }

  operator const char *() { return typeid(Carp).name(); }

  void Swim() override { cout << "Carp swims!" << endl; }
};

void MakeFishSwim(Fish &inputFish)
{
  // Calling Fish::Swim()
  inputFish.Swim();
}

void DeleteFishMemory(Fish *pFish)
{
  if (pFish) {
    delete pFish;
    pFish = nullptr;
  }
}

void DeleteTunaMemory(Tuna *pTuna)
{
  if (pTuna) {
    delete pTuna;
    pTuna = nullptr;
  }
}

auto main() -> decltype(0)
{
  cout << "Allocating a Carp on the free store:" << endl;
  Tuna *pCarp = new Carp;

  cout << "Deleting the Carp:" << endl;
  // DeleteTunaMemory(pCarp);

  DeleteFishMemory(pCarp);

  cout << "Instantiating a Carp on the stack:" << endl;
  Carp myDinner;
  cout << "Automatic desturction as it goes ot of scope:" << endl;

  return 0;
}

CLANG 7.0.0 will throw below error messages:

clang version 7.0.0 (trunk)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\gcc\..\LLVM\bin
 "C:\\gcc\\..\\llvm\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-msvc19.11.25506 -emit-obj -mincremental-linker-compatible -disable-free -main-file-name 11_2_inherit.cpp -static-define -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir "C:\\gcc\\..\\llvm\\lib\\clang\\7.0.0" -internal-isystem "C:\\gcc\\..\\llvm\\lib\\clang\\7.0.0\\include" -internal-isystem "C:\\VS2017\\VC\\Tools\\MSVC\\14.11.25503\\\\ATLMFC\\include" -internal-isystem "C:\\VS2017\\VC\\Tools\\MSVC\\14.11.25503\\\\include" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\ucrt" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\shared" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\um" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\winrt" -O3 -Wall -Wno-invalid-source-encoding -std=c++17 -fdeprecated-macro -fdebug-compilation-dir "C:\\gcc\\project\\21day\\11\\clang_bug" -ferror-limit 19 -fmessage-length 80 -fmodules -fimplicit-module-maps "-fmodules-cache-path=C:\\Users\\honzheng\\AppData\\Local\\Temp\\org.llvm.clang.honzheng\\ModuleCache" -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.11.25506 -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fseh-exceptions -fpack-struct=1 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -flto-visibility-public-std -o "C:\\Users\\honzheng\\AppData\\Local\\Temp\\11_2_inherit-83936f.o" -x c++ 11_2_inherit.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0-r325576 default target x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
 C:\gcc\..\llvm\lib\clang\7.0.0\include
 C:\VS2017\VC\Tools\MSVC\14.11.25503\\ATLMFC\include
 C:\VS2017\VC\Tools\MSVC\14.11.25503\\include
 C:\VS2017\SDK\10\include\10.0.15063.0\ucrt
 C:\VS2017\SDK\10\include\10.0.15063.0\shared
 C:\VS2017\SDK\10\include\10.0.15063.0\um
 C:\VS2017\SDK\10\include\10.0.15063.0\winrt
End of search list.
11_2_inherit.cpp:38:3: error: unknown type name 'virual'
  virual ~Tuna() { cout << "Tuna Destructor." << endl; }
  ^
Assertion failed: (!needsOverloadResolutionForDestructor() || (data().DeclaredSpecialMembers & SMF_Destructor)) && "this property has not yet been computed by Sema", file C:\src\llvm_package_325576\llvm\tools\clang\include\clang/AST/DeclCXX.h, line 893
Wrote crash dump file "C:\Users\honzheng\AppData\Local\Temp\clang++.exe-7a6ec9.dmp"
0x00007FF7441F2E06 (0x0000000000000001 0x000000F9FE78C5A0 0x000002A000000001 0x00007FFFB1FABF01)
0x00007FFFB200DC17 (0x0000000000000001 0x00007FF700000000 0x0000000000000000 0x000000F9FE78C620), raise() + 0x1E7 bytes(s)
0x00007FFFB200EAA1 (0x0002000000000003 0x000000F900000003 0x00007FFFB20647D0 0x00007FF7473AA9BC), abort() + 0x31 bytes(s)
0x00007FFFB201080A (0x0000000000000000 0x000002A0240F85A8 0x000002A0240F9CC0 0x000000000000037D), _get_wpgmptr() + 0x1C9A bytes(s)
0x00007FFFB2010701 (0x000000000000037D 0x00007FF7473AA9BC 0x000002A0240F85A8 0x000002A0240F9CC0), _get_wpgmptr() + 0x1B91 bytes(s)
0x00007FFFB2010A5F (0x000002A0240F85A8 0x0000000000000000 0x0000000000000000 0x0000000000000000), _wassert() + 0x3F bytes(s)
0x00007FF74540D741 (0x000017758A124773 0x00000000240F8570 0x000002A0240F86E8 0x000002A021587C90)
0x00007FF745415993 (0x00007FF700000000 0x000000F9FE78CD70 0x000000F9FE78CD70 0x000000F9FE78CDB0)
0x00007FF7450DDD57 (0x000002A02151A780 0x000002A02151A768 0x000000F9FE78D1F0 0x00007FF7451C6977)
0x00007FF7450DBA8F (0x0000000000000020 0x000000F9FE78D7A0 0x0000000000000003 0x00007FFFB5817A88)
0x00007FF7450F5606 (0x0000000000000000 0x000002A000000000 0x0000000000000040 0x000002A0213F0CC0)
0x00007FF7450B0187 (0x000002A0240CBA20 0x00007FFFB2010A20 0x0000000000000000 0x000002A02150A060)
0x00007FF7450AFD4C (0x000017758A124C23 0x000017758A125703 0x000002A02151E670 0x0000000000000008)
0x00007FF7450AE935 (0x000002A0214EE8C0 0x00007FF74618C87C 0x000002A02158BF40 0x00007FF7461BF500)
0x00007FF7450AD636 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FF7450AA8B6 (0x0000000000000000 0x0000000000000000 0x000000F9FE78F4E0 0x000002A0214A8A80)
0x00007FF7448EAE10 (0x000002A0214A8A80 0x000002A000000000 0x000002A0214B03C0 0x00007FF74494033F)
0x00007FF7448A741F (0x000002A0214BDE01 0x0000000000000000 0x0000000000000000 0x000002A0214FB8A0)
0x00007FF7449410E9 (0x0000000000000382 0x0000000000003800 0x0000000000000301 0x00007FFFB58180B7)
0x00007FF742907803 (0x000000000000004C 0x000002A002000002 0x000000F9FE78E7C4 0x000000F9FE78E7C0)
0x00007FF7429049E5 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FF7461BB6E8 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FFFB36E8364 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000), BaseThreadInitThunk() + 0x14 bytes(s)
0x00007FFFB5847091 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000), RtlUserThreadStart() + 0x21 bytes(s)
clang++.exe: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 7.0.0 (trunk)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\gcc\..\LLVM\bin
clang++.exe: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang++.exe: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++.exe: note: diagnostic msg: C:\Users\honzheng\AppData\Local\Temp\11_2_inherit-47fdbe.cpp
clang++.exe: note: diagnostic msg: C:\Users\honzheng\AppData\Local\Temp\11_2_inherit-47fdbe.cache
clang++.exe: note: diagnostic msg: C:\Users\honzheng\AppData\Local\Temp\11_2_inherit-47fdbe.sh
clang++.exe: note: diagnostic msg:

********************

It seem that the the CLANG compiler can't identifer this error code in line 38: "virual ~Tuna() { cout << "Tuna Destructor." << endl; }". But GCC compiler 7.3.0 worked normally, can report the error line code and doesn't crash on.

I think that it should been one GCC compiler 7.0.0's internal issue.

Upvotes: 2

Views: 712

Answers (1)

Evan
Evan

Reputation: 528

First, you're running a debug build of clang, which is why you're seeing the stack trace. Running a release build of clang just shows you an error message saying that you have misspelled virtual. Notice that above the stack trace, it still tells you that the error is in your spelling of virtual. Clang tries to continue compiling what it can given the error so that you can get more error messages that need to be fixed rather than just one-at-a time.

In short

  • get a release build of clang
  • double-check your spelling

Also, you may want to label your Fish destructor as virtual as well, so ensure that it dispatches to the right derived destructor.

Upvotes: 1

Related Questions