diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp index 06643d4bdc211..9b30ab3cb94b3 100644 --- a/clang/lib/CodeGen/CGObjCGNU.cpp +++ b/clang/lib/CodeGen/CGObjCGNU.cpp @@ -179,8 +179,16 @@ class CGObjCGNU : public CGObjCRuntime { (R.getVersion() >= VersionTuple(major, minor)); } - std::string ManglePublicSymbol(StringRef Name) { - return (StringRef(CGM.getTriple().isOSBinFormatCOFF() ? "$_" : "._") + Name).str(); + const std::string ManglePublicSymbol(StringRef Name) { + StringRef prefix = "._"; + + // Exported symbols in Emscripten must be a valid Javascript identifier. + auto triple = CGM.getTriple(); + if (triple.isOSBinFormatCOFF() || triple.isOSBinFormatWasm()) { + prefix = "$_"; + } + + return (prefix + Name).str(); } std::string SymbolForProtocol(Twine Name) { @@ -4106,8 +4114,7 @@ llvm::Function *CGObjCGNU::ModuleInitFunction() { if (!ClassAliases.empty()) { llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty}; llvm::FunctionType *RegisterAliasTy = - llvm::FunctionType::get(Builder.getVoidTy(), - ArgTypes, false); + llvm::FunctionType::get(BoolTy, ArgTypes, false); llvm::Function *RegisterAlias = llvm::Function::Create( RegisterAliasTy, llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np", diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 30d3e5293a31b..6cbec5e17ae1a 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -8001,7 +8001,8 @@ ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, if ((runtime.getKind() == ObjCRuntime::GNUstep) && (runtime.getVersion() >= VersionTuple(2, 0))) if (!getToolChain().getTriple().isOSBinFormatELF() && - !getToolChain().getTriple().isOSBinFormatCOFF()) { + !getToolChain().getTriple().isOSBinFormatCOFF() && + !getToolChain().getTriple().isOSBinFormatWasm()) { getToolChain().getDriver().Diag( diag::err_drv_gnustep_objc_runtime_incompatible_binary) << runtime.getVersion().getMajor(); diff --git a/clang/test/CodeGenObjC/gnustep2-wasm32-symbols.m b/clang/test/CodeGenObjC/gnustep2-wasm32-symbols.m new file mode 100644 index 0000000000000..7da73b8f1903e --- /dev/null +++ b/clang/test/CodeGenObjC/gnustep2-wasm32-symbols.m @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -triple wasm32-unknown-emscripten -emit-llvm -fobjc-runtime=gnustep-2.2 -o - %s | FileCheck %s + +@class NSString; + +@protocol AProtocol +- (void) meth; +@end + +@interface AClass +@end + +@implementation AClass +- (void) meth {} +@end + +// Make sure that all public symbols are mangled correctly. All exported symbols +// must be valid Javascript identifiers in Emscripten. +// CHECK: $"$_OBJC_PROTOCOL_AProtocol" = comdat any +// CHECK: @"$_OBJC_METACLASS_AClass" +// CHECK: @"$_OBJC_PROTOCOL_AProtocol" +// CHECK: @"$_OBJC_CLASS_AClass" +// CHECK: @"$_OBJC_REF_CLASS_AClass" +// CHECK: @"$_OBJC_INIT_CLASS_AClass"