Skip to content

Commit 18bd00a

Browse files
derekmaurocopybara-github
authored andcommitted
Add further diagnostics under clang for string_view(nullptr)
Detection isn't perfect, but it is better than nothing: https://godbolt.org/z/YzeMeb58j PiperOrigin-RevId: 837204651 Change-Id: Id1027c4c27bd95ad923e4c5d242a28079b16db79
1 parent 9d35bf5 commit 18bd00a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

absl/strings/string_view.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ ABSL_NAMESPACE_END
5858

5959
#else // ABSL_USES_STD_STRING_VIEW
6060

61+
#if ABSL_HAVE_ATTRIBUTE(diagnose_if)
62+
#define ABSL_INTERNAL_DIAGNOSE_IF_NULLPTR(x) \
63+
__attribute__((diagnose_if( \
64+
x == nullptr, \
65+
"null passed to a callee that requires a non-null argument", "error")))
66+
#else
67+
#define ABSL_INTERNAL_DIAGNOSE_IF_NULLPTR(x)
68+
#endif
69+
6170
#if ABSL_HAVE_BUILTIN(__builtin_memcmp) || \
6271
(defined(__GNUC__) && !defined(__clang__)) || \
6372
(defined(_MSC_VER) && _MSC_VER >= 1928)
@@ -225,7 +234,7 @@ class ABSL_ATTRIBUTE_VIEW string_view {
225234
// instead (see below).
226235
// The length check is skipped since it is unnecessary and causes code bloat.
227236
constexpr string_view( // NOLINT(runtime/explicit)
228-
const char* absl_nonnull str)
237+
const char* absl_nonnull str) ABSL_INTERNAL_DIAGNOSE_IF_NULLPTR(str)
229238
: ptr_(str), length_(str ? StrlenInternal(str) : 0) {
230239
ABSL_HARDENING_ASSERT(str != nullptr);
231240
}
@@ -779,6 +788,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece);
779788
ABSL_NAMESPACE_END
780789
} // namespace absl
781790

791+
#undef ABSL_INTERNAL_DIAGNOSE_IF_NULLPTR
782792
#undef ABSL_INTERNAL_STRING_VIEW_MEMCMP
783793

784794
#endif // ABSL_USES_STD_STRING_VIEW

0 commit comments

Comments
 (0)