diff --git a/lld/test/wasm/cxx-mangling.ll b/lld/test/wasm/cxx-mangling.ll index 0ab48748aca2..e1f4ea4950a6 100644 --- a/lld/test/wasm/cxx-mangling.ll +++ b/lld/test/wasm/cxx-mangling.ll @@ -1,8 +1,8 @@ ; RUN: llc -filetype=obj %s -o %t.o ; RUN: wasm-ld --export=_Z3fooi --demangle -o %t_demangle.wasm %t.o -; RUN: obj2yaml %t_demangle.wasm | FileCheck %s +; RUN: obj2yaml %t_demangle.wasm | FileCheck --check-prefixes=CHECK,DEMANGLE %s ; RUN: wasm-ld --export=_Z3fooi --no-demangle -o %t_nodemangle.wasm %t.o -; RUN: obj2yaml %t_nodemangle.wasm | FileCheck %s +; RUN: obj2yaml %t_nodemangle.wasm | FileCheck --check-prefixes=CHECK,MANGLE %s target triple = "wasm32-unknown-unknown" @@ -58,9 +58,11 @@ define void @_start() { ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Name: __wasm_call_ctors ; CHECK-NEXT: - Index: 1 -; CHECK-NEXT: Name: 'undefined function bar(int)' +; DEMANGLE-NEXT: Name: 'undefined function bar(int)' +; MANGLE-NEXT: Name: undefined function _Z3bari ; CHECK-NEXT: - Index: 2 -; CHECK-NEXT: Name: 'foo(int)' +; DEMANGLE-NEXT: Name: 'foo(int)' +; MANGLE-NEXT: Name: _Z3fooi ; CHECK-NEXT: - Index: 3 ; CHECK-NEXT: Name: _start ; CHECK-NEXT: ... diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index acdb5f93f154..d9060939646c 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -310,10 +310,8 @@ static void handleWeakUndefines() { // Add a synthetic dummy for weak undefined functions. These dummies will // be GC'd if not used as the target of any "call" instructions. - Optional SymName = demangleItanium(Sym->getName()); - StringRef DebugName = - Saver.save("undefined function " + - (SymName ? StringRef(*SymName) : Sym->getName())); + std::string SymName = toString(*Sym); + StringRef DebugName = Saver.save("undefined function " + SymName); SyntheticFunction *Func = make(Sig, Sym->getName(), DebugName); Func->setBody(UnreachableFn); diff --git a/lld/wasm/Symbols.cpp b/lld/wasm/Symbols.cpp index 035c7f2d725c..0e60262dcc8c 100644 --- a/lld/wasm/Symbols.cpp +++ b/lld/wasm/Symbols.cpp @@ -226,10 +226,14 @@ void SectionSymbol::setOutputSectionIndex(uint32_t Index) { void LazySymbol::fetch() { cast(File)->addMember(&ArchiveSymbol); } std::string lld::toString(const wasm::Symbol &Sym) { + return lld::maybeDemangleSymbol(Sym.getName()); +} + +std::string lld::maybeDemangleSymbol(StringRef Name) { if (Config->Demangle) - if (Optional S = demangleItanium(Sym.getName())) + if (Optional S = demangleItanium(Name)) return *S; - return Sym.getName(); + return Name; } std::string lld::toString(wasm::Symbol::Kind Kind) { diff --git a/lld/wasm/Symbols.h b/lld/wasm/Symbols.h index 815cc97d22d1..c8c47257cd40 100644 --- a/lld/wasm/Symbols.h +++ b/lld/wasm/Symbols.h @@ -349,6 +349,7 @@ T *replaceSymbol(Symbol *S, ArgT &&... Arg) { // Returns a symbol name for an error message. std::string toString(const wasm::Symbol &Sym); std::string toString(wasm::Symbol::Kind Kind); +std::string maybeDemangleSymbol(StringRef Name); } // namespace lld diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index c26ae53311b5..cb413de67bab 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -554,8 +554,7 @@ void Writer::createNameSection() { for (const Symbol *S : ImportedSymbols) { if (auto *F = dyn_cast(S)) { writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); - Optional Name = demangleItanium(F->getName()); - writeStr(Sub.OS, Name ? StringRef(*Name) : F->getName(), "symbol name"); + writeStr(Sub.OS, toString(*S), "symbol name"); } } for (const InputFunction *F : InputFunctions) { @@ -564,8 +563,7 @@ void Writer::createNameSection() { if (!F->getDebugName().empty()) { writeStr(Sub.OS, F->getDebugName(), "symbol name"); } else { - Optional Name = demangleItanium(F->getName()); - writeStr(Sub.OS, Name ? StringRef(*Name) : F->getName(), "symbol name"); + writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name"); } } }