[cfe-dev] How to deal with mangled names matching with extern "C" ones?

Andrey Bokhanko andreybokhanko at gmail.com
Tue Jul 7 03:26:41 PDT 2015


Hi All,

The following test:

extern "C" void _ZN1TD1Ev();

struct T {
  ~T() {}
};

int main() {
  _ZN1TD1Ev();
  T t;
}

crashes clang.

The problem here is when CodeGen generates code for t's destructor
call, it tries to find destructor's declaration using its mangled name
("_ZN1TD1Ev") as a key... and finds extern "C" function's declaration
instead. Of course, number of arguments is different, so an assert
hits.

I see two ways to resolve this:

1) Check that C++ mangled names don't match with extern "C" ones in
Sema. This is logical and easy enough to do when a C++ declaration
follows extern "C" (as in the example above) -- one simply has to
create a mangled name for C++ method and look for it among existing
declarations. But what to do when extern "C" follows C++ method
declaration (just put T's structure declaration before extern "C" in
the example above)? In this case, *all* existing declarations should
be mangled and checked against matching with extern "C" function name
in hand. This would be an overkill.

2) Check that what is returned in CodeGen (through
"CodeGenModule::GetGlobalValue") is suitable for the type of call we
have to generate (at least, matches number of arguments -- maybe
something else?) If not, print an error message. Granted, this is not
an ideal solution, but at least not crashes clang and doesn't add too
much overhead to detect this [arguably extremely rare] case.

Any suggestions here? Any other way to deal with this? Personally, I'm
leaning towards 2).

Yours,
Andrey Bokhanko
==============
Software Engineer
Intel Compiler Team
Intel



More information about the cfe-dev mailing list