<div dir="ltr">After some discussion on IRC, I removed this from StringRef. It doesn't seem appropriate for every function that ever returns a StringRef to require having its return value checked. One could easily imagine a function like this:<div><br></div><div>StringRef insert(StringRef S) {</div><div> // compute a hash of S, store it in some hash table, then return the hash as a StringRef.</div><div>}</div><div><br></div><div>where someone doesn't care about the actual hash code, they just want to insert it. This is a contrived example, but in any case, this is not much different than const char *. Just because a function returns a const char* doesn't mean I care about the return value.</div><div><br></div><div>LMK your thoughts on this, the change was done in r287856</div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Oct 16, 2016 at 11:44 PM Justin Bogner via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: bogner<br class="gmail_msg">
Date: Mon Oct 17 01:35:23 2016<br class="gmail_msg">
New Revision: 284364<br class="gmail_msg">
<br class="gmail_msg">
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=284364&view=rev" rel="noreferrer" class="gmail_msg" target="_blank">http://llvm.org/viewvc/llvm-project?rev=284364&view=rev</a><br class="gmail_msg">
Log:<br class="gmail_msg">
ADT: Use LLVM_NODISCARD instead of LLVM_ATTRIBUTE_UNUSED_RESULT for StringRef<br class="gmail_msg">
<br class="gmail_msg">
Instead of annotating (most of) the StringRef API, we can just<br class="gmail_msg">
annotate the type directly. This is less code and it will warn in more<br class="gmail_msg">
cases.<br class="gmail_msg">
<br class="gmail_msg">
Modified:<br class="gmail_msg">
llvm/trunk/include/llvm/ADT/StringRef.h<br class="gmail_msg">
<br class="gmail_msg">
Modified: llvm/trunk/include/llvm/ADT/StringRef.h<br class="gmail_msg">
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=284364&r1=284363&r2=284364&view=diff" rel="noreferrer" class="gmail_msg" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=284364&r1=284363&r2=284364&view=diff</a><br class="gmail_msg">
==============================================================================<br class="gmail_msg">
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)<br class="gmail_msg">
+++ llvm/trunk/include/llvm/ADT/StringRef.h Mon Oct 17 01:35:23 2016<br class="gmail_msg">
@@ -44,7 +44,7 @@ namespace llvm {<br class="gmail_msg">
/// situations where the character data resides in some other buffer, whose<br class="gmail_msg">
/// lifetime extends past that of the StringRef. For this reason, it is not in<br class="gmail_msg">
/// general safe to store a StringRef.<br class="gmail_msg">
- class StringRef {<br class="gmail_msg">
+ class LLVM_NODISCARD StringRef {<br class="gmail_msg">
public:<br class="gmail_msg">
typedef const char *iterator;<br class="gmail_msg">
typedef const char *const_iterator;<br class="gmail_msg">
@@ -281,8 +281,8 @@ namespace llvm {<br class="gmail_msg">
///<br class="gmail_msg">
/// \returns The index of the first character satisfying \p F starting from<br class="gmail_msg">
/// \p From, or npos if not found.<br class="gmail_msg">
+ LLVM_NODISCARD<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
size_t find_if(function_ref<bool(char)> F, size_t From = 0) const {<br class="gmail_msg">
StringRef S = drop_front(From);<br class="gmail_msg">
while (!S.empty()) {<br class="gmail_msg">
@@ -297,8 +297,8 @@ namespace llvm {<br class="gmail_msg">
///<br class="gmail_msg">
/// \returns The index of the first character not satisfying \p F starting<br class="gmail_msg">
/// from \p From, or npos if not found.<br class="gmail_msg">
+ LLVM_NODISCARD<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
size_t find_if_not(function_ref<bool(char)> F, size_t From = 0) const {<br class="gmail_msg">
return find_if([F](char c) { return !F(c); }, From);<br class="gmail_msg">
}<br class="gmail_msg">
@@ -500,7 +500,6 @@ namespace llvm {<br class="gmail_msg">
/// exceeds the number of characters remaining in the string, the string<br class="gmail_msg">
/// suffix (starting with \p Start) will be returned.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef substr(size_t Start, size_t N = npos) const {<br class="gmail_msg">
Start = std::min(Start, Length);<br class="gmail_msg">
return StringRef(Data + Start, std::min(N, Length - Start));<br class="gmail_msg">
@@ -510,7 +509,6 @@ namespace llvm {<br class="gmail_msg">
/// elements remaining. If \p N is greater than the length of the<br class="gmail_msg">
/// string, the entire string is returned.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef take_front(size_t N = 1) const {<br class="gmail_msg">
if (N >= size())<br class="gmail_msg">
return *this;<br class="gmail_msg">
@@ -521,7 +519,6 @@ namespace llvm {<br class="gmail_msg">
/// elements remaining. If \p N is greater than the length of the<br class="gmail_msg">
/// string, the entire string is returned.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef take_back(size_t N = 1) const {<br class="gmail_msg">
if (N >= size())<br class="gmail_msg">
return *this;<br class="gmail_msg">
@@ -531,7 +528,6 @@ namespace llvm {<br class="gmail_msg">
/// Return the longest prefix of 'this' such that every character<br class="gmail_msg">
/// in the prefix satisfies the given predicate.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef take_while(function_ref<bool(char)> F) const {<br class="gmail_msg">
return substr(0, find_if_not(F));<br class="gmail_msg">
}<br class="gmail_msg">
@@ -539,7 +535,6 @@ namespace llvm {<br class="gmail_msg">
/// Return the longest prefix of 'this' such that no character in<br class="gmail_msg">
/// the prefix satisfies the given predicate.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef take_until(function_ref<bool(char)> F) const {<br class="gmail_msg">
return substr(0, find_if(F));<br class="gmail_msg">
}<br class="gmail_msg">
@@ -547,7 +542,6 @@ namespace llvm {<br class="gmail_msg">
/// Return a StringRef equal to 'this' but with the first \p N elements<br class="gmail_msg">
/// dropped.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef drop_front(size_t N = 1) const {<br class="gmail_msg">
assert(size() >= N && "Dropping more elements than exist");<br class="gmail_msg">
return substr(N);<br class="gmail_msg">
@@ -556,7 +550,6 @@ namespace llvm {<br class="gmail_msg">
/// Return a StringRef equal to 'this' but with the last \p N elements<br class="gmail_msg">
/// dropped.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef drop_back(size_t N = 1) const {<br class="gmail_msg">
assert(size() >= N && "Dropping more elements than exist");<br class="gmail_msg">
return substr(0, size()-N);<br class="gmail_msg">
@@ -565,7 +558,6 @@ namespace llvm {<br class="gmail_msg">
/// Return a StringRef equal to 'this', but with all characters satisfying<br class="gmail_msg">
/// the given predicate dropped from the beginning of the string.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef drop_while(function_ref<bool(char)> F) const {<br class="gmail_msg">
return substr(find_if_not(F));<br class="gmail_msg">
}<br class="gmail_msg">
@@ -573,15 +565,14 @@ namespace llvm {<br class="gmail_msg">
/// Return a StringRef equal to 'this', but with all characters not<br class="gmail_msg">
/// satisfying the given predicate dropped from the beginning of the string.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef drop_until(function_ref<bool(char)> F) const {<br class="gmail_msg">
return substr(find_if(F));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
/// Returns true if this StringRef has the given prefix and removes that<br class="gmail_msg">
/// prefix.<br class="gmail_msg">
+ LLVM_NODISCARD<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
bool consume_front(StringRef Prefix) {<br class="gmail_msg">
if (!startswith(Prefix))<br class="gmail_msg">
return false;<br class="gmail_msg">
@@ -592,8 +583,8 @@ namespace llvm {<br class="gmail_msg">
<br class="gmail_msg">
/// Returns true if this StringRef has the given suffix and removes that<br class="gmail_msg">
/// suffix.<br class="gmail_msg">
+ LLVM_NODISCARD<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
bool consume_back(StringRef Suffix) {<br class="gmail_msg">
if (!endswith(Suffix))<br class="gmail_msg">
return false;<br class="gmail_msg">
@@ -614,7 +605,6 @@ namespace llvm {<br class="gmail_msg">
/// will be returned. If this is less than \p Start, an empty string will<br class="gmail_msg">
/// be returned.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef slice(size_t Start, size_t End) const {<br class="gmail_msg">
Start = std::min(Start, Length);<br class="gmail_msg">
End = std::min(std::max(Start, End), Length);<br class="gmail_msg">
@@ -709,42 +699,36 @@ namespace llvm {<br class="gmail_msg">
<br class="gmail_msg">
/// Return string with consecutive \p Char characters starting from the<br class="gmail_msg">
/// the left removed.<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef ltrim(char Char) const {<br class="gmail_msg">
return drop_front(std::min(Length, find_first_not_of(Char)));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
/// Return string with consecutive characters in \p Chars starting from<br class="gmail_msg">
/// the left removed.<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {<br class="gmail_msg">
return drop_front(std::min(Length, find_first_not_of(Chars)));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
/// Return string with consecutive \p Char characters starting from the<br class="gmail_msg">
/// right removed.<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef rtrim(char Char) const {<br class="gmail_msg">
return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
/// Return string with consecutive characters in \p Chars starting from<br class="gmail_msg">
/// the right removed.<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {<br class="gmail_msg">
return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
/// Return string with consecutive \p Char characters starting from the<br class="gmail_msg">
/// left and right removed.<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef trim(char Char) const {<br class="gmail_msg">
return ltrim(Char).rtrim(Char);<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
/// Return string with consecutive characters in \p Chars starting from<br class="gmail_msg">
/// the left and right removed.<br class="gmail_msg">
- LLVM_ATTRIBUTE_UNUSED_RESULT<br class="gmail_msg">
StringRef trim(StringRef Chars = " \t\n\v\f\r") const {<br class="gmail_msg">
return ltrim(Chars).rtrim(Chars);<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
llvm-commits mailing list<br class="gmail_msg">
<a href="mailto:llvm-commits@lists.llvm.org" class="gmail_msg" target="_blank">llvm-commits@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br class="gmail_msg">
</blockquote></div>