Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 2ee4280

Browse files
committed
Disallow StringRef assignment from temporary std::strings.
Similar to r283798, this prevents accidentally referring to temporary storage that goes out of scope by the end of the statement: someStringRef = getStringByValue(); someStringRef = (Twine("-") + otherString).str(); Note that once again the constructor still has this problem: StringRef someStringRef = getStringByValue(); because once again we occasionally rely on this in calls: takesStringRef(getStringByValue()); takesStringRef(Twine("-") + otherString); Still, it's a step.
1 parent 2f56c97 commit 2ee4280

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/llvm/ADT/StringRef.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ namespace llvm {
215215
return Data[Index];
216216
}
217217

218+
/// Disallow accidental assignment from a temporary std::string.
219+
StringRef &operator=(std::string &&Str) = delete;
220+
221+
/// Disambiguate assignment from const char *.
222+
///
223+
/// This assumes the argument is a valid null-terminated C string and that
224+
/// the memory will not be freed while the StringRef is in use.
225+
StringRef &operator=(const char *Str) {
226+
*this = StringRef(Str);
227+
return *this;
228+
}
229+
218230
/// @}
219231
/// @name Type Conversions
220232
/// @{

0 commit comments

Comments
 (0)