Skip to content

Commit 61e1c3d

Browse files
[Demangle] convert itaniumDemangle and nonMicrosoftDemangle to use std::string_view
D149104 converted llvm::demangle to use std::string_view. Enabling "expensive checks" (via -DLLVM_ENABLE_EXPENSIVE_CHECKS=ON) causes lld/test/wasm/why-extract.s to fail. The reason for this is obscure: Reason #10007 why std::string_view is dangerous: Consider the following pattern: std::string_view s = ...; const char *c = s.data(); std::strlen(c); Is c a NUL-terminated C style string? It depends; but if it's not then it's not safe to call std::strlen on the std::string_view::data(). std::string_view::length() should be used instead. Fixing this fixes the one lone test that caught this. microsoftDemangle, rustDemangle, and dlangDemangle should get this same treatment, too. I will do that next. Reviewed By: MaskRay, efriedma Differential Revision: https://reviews.llvm.org/D149675
1 parent fa98bdb commit 61e1c3d

File tree

7 files changed

+25
-21
lines changed

7 files changed

+25
-21
lines changed

llvm/include/llvm/Demangle/Demangle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum : int {
3131
/// Returns a non-NULL pointer to a NUL-terminated C style string
3232
/// that should be explicitly freed, if successful. Otherwise, may return
3333
/// nullptr if mangled_name is not a valid mangling or is nullptr.
34-
char *itaniumDemangle(const char *mangled_name);
34+
char *itaniumDemangle(std::string_view mangled_name);
3535

3636
enum MSDemangleFlags {
3737
MSDF_None = 0,
@@ -66,7 +66,7 @@ char *dlangDemangle(const char *MangledName);
6666
/// demangling occurred.
6767
std::string demangle(const std::string &MangledName);
6868

69-
bool nonMicrosoftDemangle(const char *MangledName, std::string &Result);
69+
bool nonMicrosoftDemangle(std::string_view MangledName, std::string &Result);
7070

7171
/// "Partial" demangler. This supports demangling a string into an AST
7272
/// (typically an intermediate stage in itaniumDemangle) and querying certain

llvm/lib/DebugInfo/Symbolize/Symbolize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ std::string
679679
LLVMSymbolizer::DemangleName(const std::string &Name,
680680
const SymbolizableModule *DbiModuleDescriptor) {
681681
std::string Result;
682-
if (nonMicrosoftDemangle(Name.c_str(), Result))
682+
if (nonMicrosoftDemangle(Name, Result))
683683
return Result;
684684

685685
if (!Name.empty() && Name.front() == '?') {
@@ -700,7 +700,7 @@ LLVMSymbolizer::DemangleName(const std::string &Name,
700700
std::string DemangledCName(demanglePE32ExternCFunc(Name));
701701
// On i386 Windows, the C name mangling for different calling conventions
702702
// may also be applied on top of the Itanium or Rust name mangling.
703-
if (nonMicrosoftDemangle(DemangledCName.c_str(), Result))
703+
if (nonMicrosoftDemangle(DemangledCName, Result))
704704
return Result;
705705
return DemangledCName;
706706
}

llvm/lib/Demangle/Demangle.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ std::string llvm::demangle(const std::string &MangledName) {
3030
std::string Result;
3131
const char *S = MangledName.c_str();
3232

33-
if (nonMicrosoftDemangle(S, Result))
33+
if (nonMicrosoftDemangle(MangledName, Result))
3434
return Result;
3535

36-
if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result))
36+
if (S[0] == '_' && nonMicrosoftDemangle(MangledName.substr(1), Result))
3737
return Result;
3838

3939
if (char *Demangled = microsoftDemangle(S, nullptr, nullptr)) {
@@ -45,14 +45,15 @@ std::string llvm::demangle(const std::string &MangledName) {
4545
return MangledName;
4646
}
4747

48-
bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) {
48+
bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
49+
std::string &Result) {
4950
char *Demangled = nullptr;
50-
if (isItaniumEncoding(MangledName))
51+
if (isItaniumEncoding(MangledName.data()))
5152
Demangled = itaniumDemangle(MangledName);
52-
else if (isRustEncoding(MangledName))
53-
Demangled = rustDemangle(MangledName);
54-
else if (isDLangEncoding(MangledName))
55-
Demangled = dlangDemangle(MangledName);
53+
else if (isRustEncoding(MangledName.data()))
54+
Demangled = rustDemangle(MangledName.data());
55+
else if (isDLangEncoding(MangledName.data()))
56+
Demangled = dlangDemangle(MangledName.data());
5657

5758
if (!Demangled)
5859
return false;

llvm/lib/Demangle/ItaniumDemangle.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,12 @@ class DefaultAllocator {
365365

366366
using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>;
367367

368-
char *llvm::itaniumDemangle(const char *MangledName) {
369-
if (!MangledName)
368+
char *llvm::itaniumDemangle(std::string_view MangledName) {
369+
if (MangledName.empty())
370370
return nullptr;
371371

372-
Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
372+
Demangler Parser(MangledName.data(),
373+
MangledName.data() + MangledName.length());
373374
Node *AST = Parser.parse();
374375
if (!AST)
375376
return nullptr;

llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/ADT/StringExtras.h"
1010
#include "llvm/Demangle/Demangle.h"
11+
#include "llvm/Demangle/StringViewExtras.h"
1112
#include "llvm/Option/Arg.h"
1213
#include "llvm/Option/ArgList.h"
1314
#include "llvm/Option/Option.h"
@@ -71,10 +72,11 @@ static void error(const Twine &Message) {
7172
}
7273

7374
static std::string demangle(const std::string &Mangled) {
74-
const char *DecoratedStr = Mangled.c_str();
75+
using llvm::itanium_demangle::starts_with;
76+
std::string_view DecoratedStr = Mangled;
7577
if (StripUnderscore)
7678
if (DecoratedStr[0] == '_')
77-
++DecoratedStr;
79+
DecoratedStr.remove_prefix(1);
7880

7981
std::string Result;
8082
if (nonMicrosoftDemangle(DecoratedStr, Result))
@@ -86,9 +88,9 @@ static std::string demangle(const std::string &Mangled) {
8688
if (Types)
8789
Undecorated = itaniumDemangle(DecoratedStr);
8890

89-
if (!Undecorated && strncmp(DecoratedStr, "__imp_", 6) == 0) {
91+
if (!Undecorated && starts_with(DecoratedStr, "__imp_")) {
9092
Prefix = "import thunk for ";
91-
Undecorated = itaniumDemangle(DecoratedStr + 6);
93+
Undecorated = itaniumDemangle(DecoratedStr.substr(6));
9294
}
9395

9496
Result = Undecorated ? Prefix + Undecorated : Mangled;

llvm/tools/llvm-nm/llvm-nm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static void darwinPrintStab(MachOObjectFile *MachO, const NMSymbol &S) {
639639

640640
static std::optional<std::string> demangle(StringRef Name) {
641641
std::string Demangled;
642-
if (nonMicrosoftDemangle(Name.str().c_str(), Demangled))
642+
if (nonMicrosoftDemangle(Name, Demangled))
643643
return Demangled;
644644
return std::nullopt;
645645
}

llvm/tools/llvm-opt-report/OptReport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static bool writeReport(LocationInfoTy &LocationInfo) {
338338

339339
bool Printed = false;
340340
if (!NoDemangle) {
341-
if (char *Demangled = itaniumDemangle(FuncName.c_str())) {
341+
if (char *Demangled = itaniumDemangle(FuncName)) {
342342
OS << Demangled;
343343
Printed = true;
344344
std::free(Demangled);

0 commit comments

Comments
 (0)