Skip to content

Commit 7baad0c

Browse files
committed
[WebAssembly][MC] Use StringRef over std::string pointer
This is followup based on feedback on 5be42f3. See: https://reviews.llvm.org/D77627. Differential Revision: https://reviews.llvm.org/D77674
1 parent ca37678 commit 7baad0c

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

llvm/include/llvm/MC/MCSymbolWasm.h

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ class MCSymbolWasm : public MCSymbol {
1919
bool IsHidden = false;
2020
bool IsComdat = false;
2121
mutable bool IsUsedInGOT = false;
22+
Optional<StringRef> ImportModule;
23+
Optional<StringRef> ImportName;
24+
Optional<StringRef> ExportName;
25+
wasm::WasmSignature *Signature = nullptr;
2226
Optional<wasm::WasmGlobalType> GlobalType;
2327
Optional<wasm::WasmEventType> EventType;
2428

25-
// Non-owning pointers since MCSymbol must be trivially destructible.
26-
std::string *ImportModule = nullptr;
27-
std::string *ImportName = nullptr;
28-
std::string *ExportName = nullptr;
29-
wasm::WasmSignature *Signature = nullptr;
30-
3129
/// An expression describing how to calculate the size of a symbol. If a
3230
/// symbol has no size this field will be NULL.
3331
const MCExpr *SymbolSize = nullptr;
@@ -71,32 +69,29 @@ class MCSymbolWasm : public MCSymbol {
7169
bool isComdat() const { return IsComdat; }
7270
void setComdat(bool isComdat) { IsComdat = isComdat; }
7371

74-
bool hasImportModule() const { return ImportModule != nullptr; }
72+
bool hasImportModule() const { return ImportModule.hasValue(); }
7573
StringRef getImportModule() const {
76-
if (ImportModule)
77-
return StringRef(*ImportModule);
74+
if (ImportModule.hasValue())
75+
return ImportModule.getValue();
7876
// Use a default module name of "env" for now, for compatibility with
7977
// existing tools.
8078
// TODO(sbc): Find a way to specify a default value in the object format
8179
// without picking a hardcoded value like this.
8280
return "env";
8381
}
84-
void setImportModule(std::string *Name) { ImportModule = Name; }
82+
void setImportModule(StringRef Name) { ImportModule = Name; }
8583

86-
bool hasImportName() const { return ImportName != nullptr; }
84+
bool hasImportName() const { return ImportName.hasValue(); }
8785
StringRef getImportName() const {
88-
if (ImportName)
89-
return StringRef(*ImportName);
86+
if (ImportName.hasValue())
87+
return ImportName.getValue();
9088
return getName();
9189
}
92-
void setImportName(std::string *Name) { ImportName = Name; }
90+
void setImportName(StringRef Name) { ImportName = Name; }
9391

94-
bool hasExportName() const { return ExportName != nullptr; }
95-
StringRef getExportName() const {
96-
assert(ExportName);
97-
return StringRef(*ExportName);
98-
}
99-
void setExportName(std::string *Name) { ExportName = Name; }
92+
bool hasExportName() const { return ExportName.hasValue(); }
93+
StringRef getExportName() const { return ExportName.getValue(); }
94+
void setExportName(StringRef Name) { ExportName = Name; }
10095

10196
void setUsedInGOT() const { IsUsedInGOT = true; }
10297
bool isUsedInGOT() const { return IsUsedInGOT; }

llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
233233
Signatures.push_back(std::move(Sig));
234234
}
235235

236-
std::string *storeName(StringRef Name) {
236+
StringRef storeName(StringRef Name) {
237237
std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
238238
Names.push_back(std::move(N));
239-
return Names.back().get();
239+
return *Names.back();
240240
}
241241

242242
std::pair<StringRef, StringRef> nestingString(NestingType NT) {

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter {
2828
std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures;
2929
std::vector<std::unique_ptr<std::string>> Names;
3030

31-
std::string *storeName(StringRef Name) {
31+
StringRef storeName(StringRef Name) {
3232
std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
3333
Names.push_back(std::move(N));
34-
return Names.back().get();
34+
return *Names.back();
3535
}
3636

3737
public:

0 commit comments

Comments
 (0)