[libc-commits] [clang] [compiler-rt] [libc] [llvm] [Libc] Give more functions restrict qualifiers (PR #78061)

via libc-commits libc-commits at lists.llvm.org
Sat Jan 13 21:34:28 PST 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/78061

>From d281918249db16549b5a8c8bdd61c90007cbeab0 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 13 Jan 2024 14:09:17 -0500
Subject: [PATCH] [Libc] Give more functions restrict qualifiers

strsep has restrict qualifiers, as well as strtok_r.
Add the restrict qualifiers to them.

Source: https://man7.org/linux/man-pages/man3/strsep.3.html
---
 clang/lib/Headers/llvm_libc_wrappers/string.h    |  4 ++--
 .../Analysis/Inputs/system-header-simulator.h    |  6 +++---
 clang/test/Analysis/string.c                     |  4 ++--
 .../lib/sanitizer_common/sanitizer_libc.h        | 16 ++++++++--------
 libc/src/string/strsep.cpp                       |  3 ++-
 libc/src/string/strsep.h                         |  2 +-
 llvm/lib/Support/regex_impl.h                    |  2 +-
 llvm/lib/Support/regstrlcpy.c                    |  2 +-
 8 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/clang/lib/Headers/llvm_libc_wrappers/string.h b/clang/lib/Headers/llvm_libc_wrappers/string.h
index b4fbf17c7e421f..0ea49cb137606c 100644
--- a/clang/lib/Headers/llvm_libc_wrappers/string.h
+++ b/clang/lib/Headers/llvm_libc_wrappers/string.h
@@ -51,13 +51,13 @@ char *strcpy(char *__restrict, const char *__restrict) __LIBC_ATTRS;
 size_t strcspn(const char *, const char *) __LIBC_ATTRS;
 char *strdup(const char *) __LIBC_ATTRS;
 size_t strlen(const char *) __LIBC_ATTRS;
-char *strncat(char *, const char *, size_t) __LIBC_ATTRS;
+char *strncat(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
 int strncmp(const char *, const char *, size_t) __LIBC_ATTRS;
 char *strncpy(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
 char *strndup(const char *, size_t) __LIBC_ATTRS;
 size_t strnlen(const char *, size_t) __LIBC_ATTRS;
 size_t strspn(const char *, const char *) __LIBC_ATTRS;
-char *strtok(char *__restrict, const char *) __LIBC_ATTRS;
+char *strtok(char *__restrict, const char *__restrict) __LIBC_ATTRS;
 char *strtok_r(char *__restrict, const char *__restrict,
                char **__restrict) __LIBC_ATTRS;
 size_t strxfrm(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
diff --git a/clang/test/Analysis/Inputs/system-header-simulator.h b/clang/test/Analysis/Inputs/system-header-simulator.h
index cd7ac616bcc67f..5f969adfdd92cd 100644
--- a/clang/test/Analysis/Inputs/system-header-simulator.h
+++ b/clang/test/Analysis/Inputs/system-header-simulator.h
@@ -71,9 +71,9 @@ int fflush(FILE *stream);
 size_t strlen(const char *);
 
 char *strcpy(char *restrict, const char *restrict);
-char *strncpy(char *dst, const char *src, size_t n);
-char *strsep(char **stringp, const char *delim);
-void *memcpy(void *dst, const void *src, size_t n);
+char *strncpy(char *restrict dst, const char * restrict src, size_t n);
+char *strsep(char ** restrict stringp, const char * restrict delim);
+void *memcpy(void * restrict dst, const void * restrict src, size_t n);
 void *memset(void *s, int c, size_t n);
 
 typedef unsigned long __darwin_pthread_key_t;
diff --git a/clang/test/Analysis/string.c b/clang/test/Analysis/string.c
index d47de9db8228e5..85232624160c06 100644
--- a/clang/test/Analysis/string.c
+++ b/clang/test/Analysis/string.c
@@ -71,7 +71,7 @@ void clang_analyzer_eval(int);
 int scanf(const char *restrict format, ...);
 void *malloc(size_t);
 void free(void *);
-void *memcpy(void *dest, const void *src, size_t n);
+void *memcpy(void *restrict dest, const void *restrict src, size_t n);
 
 //===----------------------------------------------------------------------===
 // strlen()
@@ -1252,7 +1252,7 @@ int strncasecmp_null_argument(char *a, size_t n) {
 // strsep()
 //===----------------------------------------------------------------------===
 
-char *strsep(char **stringp, const char *delim);
+char *strsep(char ** restrict stringp, const char * restrict delim);
 
 void strsep_null_delim(char *s) {
   strsep(&s, NULL); // expected-warning{{Null pointer passed as 2nd argument to strsep()}}
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h
index 1906569e2a5fc5..bf52f50231c206 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h
@@ -41,10 +41,10 @@ s64 internal_atoll(const char *nptr);
 void *internal_memchr(const void *s, int c, uptr n);
 void *internal_memrchr(const void *s, int c, uptr n);
 int internal_memcmp(const void* s1, const void* s2, uptr n);
-ALWAYS_INLINE void *internal_memcpy(void *dest, const void *src, uptr n) {
+ALWAYS_INLINE void *internal_memcpy(void *__restrict dest, const void *__restrict src, uptr n) {
   return __sanitizer_internal_memcpy(dest, src, n);
 }
-ALWAYS_INLINE void *internal_memmove(void *dest, const void *src, uptr n) {
+ALWAYS_INLINE void *internal_memmove(void *__restrict dest, const void *__restrict src, uptr n) {
   return __sanitizer_internal_memmove(dest, src, n);
 }
 // Should not be used in performance-critical places.
@@ -57,11 +57,11 @@ int internal_strcmp(const char *s1, const char *s2);
 uptr internal_strcspn(const char *s, const char *reject);
 char *internal_strdup(const char *s);
 uptr internal_strlen(const char *s);
-uptr internal_strlcat(char *dst, const char *src, uptr maxlen);
-char *internal_strncat(char *dst, const char *src, uptr n);
+uptr internal_strlcat(char *__restrict dst, const char *__restrict src, uptr maxlen);
+char *internal_strncat(char *__restrict dst, const char *__restrict src, uptr n);
 int internal_strncmp(const char *s1, const char *s2, uptr n);
-uptr internal_strlcpy(char *dst, const char *src, uptr maxlen);
-char *internal_strncpy(char *dst, const char *src, uptr n);
+uptr internal_strlcpy(char *__restrict dst, const char *__restrict src, uptr maxlen);
+char *internal_strncpy(char *__restrict dst, const char *__restrict src, uptr n);
 uptr internal_strnlen(const char *s, uptr maxlen);
 char *internal_strrchr(const char *s, int c);
 char *internal_strstr(const char *haystack, const char *needle);
@@ -71,8 +71,8 @@ int internal_snprintf(char *buffer, uptr length, const char *format, ...)
     FORMAT(3, 4);
 uptr internal_wcslen(const wchar_t *s);
 uptr internal_wcsnlen(const wchar_t *s, uptr maxlen);
-wchar_t *internal_wcscpy(wchar_t *dst, const wchar_t *src);
-wchar_t *internal_wcsncpy(wchar_t *dst, const wchar_t *src, uptr maxlen);
+wchar_t *internal_wcscpy(wchar_t *__restrict dst, const wchar_t *__restrict src);
+wchar_t *internal_wcsncpy(wchar_t *__restrict dst, const wchar_t *__restrict src, uptr maxlen);
 // Return true if all bytes in [mem, mem+size) are zero.
 // Optimized for the case when the result is true.
 bool mem_is_zero(const char *mem, uptr size);
diff --git a/libc/src/string/strsep.cpp b/libc/src/string/strsep.cpp
index edd2cf07e20a45..5ebf2550744c30 100644
--- a/libc/src/string/strsep.cpp
+++ b/libc/src/string/strsep.cpp
@@ -12,7 +12,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LLVM_LIBC_FUNCTION(char *, strsep, (char **stringp, const char *delim)) {
+LLVM_LIBC_FUNCTION(char *, strsep,
+                   (char **__restrict stringp, const char *__restrict delim)) {
   if (!*stringp)
     return nullptr;
   return internal::string_token<false>(*stringp, delim, stringp);
diff --git a/libc/src/string/strsep.h b/libc/src/string/strsep.h
index 48f55a899d7f33..7b85202d30b2b9 100644
--- a/libc/src/string/strsep.h
+++ b/libc/src/string/strsep.h
@@ -11,7 +11,7 @@
 
 namespace LIBC_NAMESPACE {
 
-char *strsep(char **stringp, const char *delim);
+char *strsep(char **__restrict stringp, const char *__restrict delim);
 
 } // namespace LIBC_NAMESPACE
 
diff --git a/llvm/lib/Support/regex_impl.h b/llvm/lib/Support/regex_impl.h
index 8f0c532205edc3..ab7a21b1d7c0c5 100644
--- a/llvm/lib/Support/regex_impl.h
+++ b/llvm/lib/Support/regex_impl.h
@@ -99,7 +99,7 @@ size_t	llvm_regerror(int, const llvm_regex_t *, char *, size_t);
 int	llvm_regexec(const llvm_regex_t *, const char *, size_t,
                      llvm_regmatch_t [], int);
 void	llvm_regfree(llvm_regex_t *);
-size_t  llvm_strlcpy(char *dst, const char *src, size_t siz);
+size_t  llvm_strlcpy(char * __restrict dst, const char *__restrict src, size_t siz);
 
 #ifdef __cplusplus
 }
diff --git a/llvm/lib/Support/regstrlcpy.c b/llvm/lib/Support/regstrlcpy.c
index 8b68afdf75f16a..1e55c4f36af7b9 100644
--- a/llvm/lib/Support/regstrlcpy.c
+++ b/llvm/lib/Support/regstrlcpy.c
@@ -26,7 +26,7 @@
  * Returns strlen(src); if retval >= siz, truncation occurred.
  */
 size_t
-llvm_strlcpy(char *dst, const char *src, size_t siz)
+llvm_strlcpy(char *__restrict dst, const char *__restrict src, size_t siz)
 {
 	char *d = dst;
 	const char *s = src;



More information about the libc-commits mailing list