This is an archive of the discontinued LLVM Phabricator instance.

libcxx: Add _LIBCPP_NODISCARD_EXT to 37 more functions
ClosedPublic

Authored by thakis on Apr 2 2019, 11:58 AM.

Details

Summary

This builds on the work done in r342808 and adds _LIBCPP_NODISCARD_EXT to 37 more functions, namely:

adjacent_find, all_of, any_of, binary_search, clamp, count_if, count, equal_range, equal, find_end, find_first_of, find_if_not, find_if, find, includes, is_heap_until, is_heap, is_partitioned, is_permutation, is_sorted_until, is_sorted, lexicographical_compare, lower_bound, max_element, max, min_element, min, minmax_element, minmax, mismatch, none_of, remove_if, remove, search_n, search, unique, upper_bound

The motivation here is that we noticed that find_if is nodiscard with Visual Studio's standard library, and we deemed that useful (https://crbug.com/948122). https://devblogs.microsoft.com/cppblog/c17-progress-in-vs-2017-15-5-and-15-6/ says "Our criteria for emitting the warning are: discarding the return value is a guaranteed leak [...], discarding the return value is near-guaranteed to be incorrect (e.g. remove()/remove_if()/unique()), or the function is essentially a pure observer (e.g. vector::empty() and std::is_sorted())." so I went through algorithm and tried to apply these criteria.

Some of these, like vector::empty() are already nodiscard per C++ standard and didn't need changing.

I didn't (yet?) go over std::string::find* methods which should probably have _LIBCPP_NODISCARD_EXT too (but not as part of this change).

Diff Detail

Repository
rCXX libc++

Event Timeline

thakis created this revision.Apr 2 2019, 11:58 AM

The test is obviously incomplete. I figured I'd send this out and check if you're generally ok with this; if so I'll update the two test files to make the test comprehensive.

thakis marked an inline comment as done.Apr 2 2019, 12:08 PM
thakis added inline comments.
libcxx/include/algorithm
931 ↗(On Diff #193337)

Further down I only added nodiscard on the public versions of these functions. I intend to revert the changes to __find_end.

thakis updated this revision to Diff 193339.Apr 2 2019, 12:15 PM
thakis retitled this revision from libcxx: Add _LIBCPP_NODISCARD_EXT to 37 more functions to libcxx: Add _LIBCPP_NODISCARD_EXT to 38 more functions.
thakis edited the summary of this revision. (Show Details)

minor updates (remove nodiscard from a few internal functions, sort list in rst file), and add equal_range to rst file which I had changed but not listed

The test is obviously incomplete.

Obviously.

This looks like a good start to me; modulo some spacing nits.

libcxx/include/algorithm
1079 ↗(On Diff #193339)

What's with the added space here?

1093 ↗(On Diff #193339)

Two spaces here? (and other places)

STL_MSFT added inline comments.Apr 2 2019, 3:18 PM
libcxx/include/algorithm
2599 ↗(On Diff #193339)

You seem to have missed marking this overload.

thakis updated this revision to Diff 193394.Apr 2 2019, 5:12 PM
thakis marked 2 inline comments as done.
thakis retitled this revision from libcxx: Add _LIBCPP_NODISCARD_EXT to 38 more functions to libcxx: Add _LIBCPP_NODISCARD_EXT to 37 more functions.
thakis edited the summary of this revision. (Show Details)

Now with comprehensive tests; they even found a bug (I missed a minmax_element overload). Also, there's no std::find_first_not_of().

I'm a bit worried about nodiscard_extensions.pass.cpp, because lit by default does not show warnings; just "N warnings in tests" in the summary.

Other than that this looks fine to me.

thakis added a comment.Apr 3 2019, 8:12 AM

I'm a bit worried about nodiscard_extensions.pass.cpp, because lit by default does not show warnings; just "N warnings in tests" in the summary.

(That would apply to the previous version of the test too, right?)

lit prints this for me (note -Werror): llvm-lit: /Users/thakis/src/llvm-project/libcxx/utils/libcxx/test/config.py:167: note: Using warnings: ['-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER', '-Wall', '-Wextra', '-Werror', '-Wuser-defined-warnings', '-Wshadow', '-Wno-unused-command-line-argument', '-Wno-attributes', '-Wno-pessimizing-move', '-Wno-c++11-extensions', '-Wno-user-defined-literals', '-Wno-noexcept-type', '-Wsign-compare', '-Wunused-variable', '-Wunused-parameter', '-Wunreachable-code']

Does that address your concern?

I'm a bit worried about nodiscard_extensions.pass.cpp, because lit by default does not show warnings; just "N warnings in tests" in the summary.

(That would apply to the previous version of the test too, right?)

lit prints this for me (note -Werror):

[snip]

Does that address your concern?

Not really, because of the "for me" bit. [ I believe that it's something that people can configure. ]
What I want to ensure is that this test doesn't start giving warnings, and I'm not sure how to do that.

I will do some looking into this and get back to you. Don't make any changes for the moment.

EricWF accepted this revision.Apr 3 2019, 10:45 AM

When LIT says "N warnings in tests" It's not talking about warnings compiling code; it's refering to warnings while configuring the test suite.

Our tests all run with -Werror enabled and system header pragmas within libc++ disabled.

This LGTM.

This revision is now accepted and ready to land.Apr 3 2019, 10:45 AM
This revision was automatically updated to reflect the committed changes.