fix Issue 23387 - ImportC: identical structs defined in two C files lead to duplicate .init symbol on macOS

This commit is contained in:
Walter Bright 2022-10-08 23:33:14 -07:00
parent 96df7f49dd
commit 7fe0aee2a4
4 changed files with 58 additions and 1 deletions

View file

@ -657,7 +657,24 @@ Symbol *toInitializer(AggregateDeclaration ad)
else
{
auto stag = fake_classsym(Id.ClassInfo);
auto s = toSymbolX(ad, "__init", SC.extern_, stag.Stype, "Z");
Symbol* s;
Module m = ad.getModule();
if (m.filetype == FileType.c)
{
/* For ImportC structs, the module names are stripped from the mangled name.
* This leads to name collisions. Add the module name back in.
*/
import dmd.common.outbuffer : OutBuffer;
OutBuffer buf;
buf.writestring("__init");
buf.writestring(m.toChars());
s = toSymbolX(ad, buf.peekChars(), SC.extern_, stag.Stype, "Z");
}
else
s = toSymbolX(ad, "__init", SC.extern_, stag.Stype, "Z");
s.Sfl = FLextern;
s.Sflags |= SFLnodebug;
if (sd)

View file

@ -0,0 +1,5 @@
typedef struct Foo *FooRef;
struct Foo {
int x;
};
void free_foo(FooRef foo) { }

View file

@ -0,0 +1,5 @@
typedef struct Foo *FooRef;
struct Foo {
int x;
};
FooRef make_foo(void) { return 0; }

View file

@ -0,0 +1,30 @@
/* COMPILE_SEPARATELY:
* EXTRA_SOURCES: imports/maker.i imports/freer.i
*/
// https://issues.dlang.org/show_bug.cgi?id=23387
/+ maker.i
typedef struct Foo *FooRef;
struct Foo {
int x;
};
FooRef make_foo(void);
+/
import imports.maker;
/+ freer.i
typedef struct Foo *FooRef;
struct Foo {
int x;
};
void free_foo(FooRef foo);
+/
import imports.freer;
int main(){
FooRef f = make_foo();
free_foo(f);
return 0;
}