Skip to content

Commit e06b5a2

Browse files
authored
[libc] Give more functions restrict qualifiers (NFC) (#78061)
strsep, strtok_r, strlcpy, and strlcat take restricted pointers as parameters. Add the restrict qualifiers to them. Sources: https://man7.org/linux/man-pages/man3/strsep.3.html https://man7.org/linux/man-pages/man3/strtok_r.3.html https://man.freebsd.org/cgi/man.cgi?strlcpy
1 parent e2ce91f commit e06b5a2

File tree

8 files changed

+16
-15
lines changed

8 files changed

+16
-15
lines changed

clang/lib/Headers/llvm_libc_wrappers/string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ char *strcpy(char *__restrict, const char *__restrict) __LIBC_ATTRS;
5151
size_t strcspn(const char *, const char *) __LIBC_ATTRS;
5252
char *strdup(const char *) __LIBC_ATTRS;
5353
size_t strlen(const char *) __LIBC_ATTRS;
54-
char *strncat(char *, const char *, size_t) __LIBC_ATTRS;
54+
char *strncat(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
5555
int strncmp(const char *, const char *, size_t) __LIBC_ATTRS;
5656
char *strncpy(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
5757
char *strndup(const char *, size_t) __LIBC_ATTRS;
5858
size_t strnlen(const char *, size_t) __LIBC_ATTRS;
5959
size_t strspn(const char *, const char *) __LIBC_ATTRS;
60-
char *strtok(char *__restrict, const char *) __LIBC_ATTRS;
60+
char *strtok(char *__restrict, const char *__restrict) __LIBC_ATTRS;
6161
char *strtok_r(char *__restrict, const char *__restrict,
6262
char **__restrict) __LIBC_ATTRS;
6363
size_t strxfrm(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;

clang/test/Analysis/Inputs/system-header-simulator-for-simple-stream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
typedef struct __sFILE {
99
unsigned char *_p;
1010
} FILE;
11-
FILE *fopen(const char * restrict, const char * restrict) __asm("_" "fopen" );
11+
FILE *fopen(const char *restrict, const char *restrict) __asm("_" "fopen" );
1212
int fputc(int, FILE *);
13-
int fputs(const char * restrict, FILE * restrict) __asm("_" "fputs" );
13+
int fputs(const char *restrict, FILE *restrict) __asm("_" "fputs" );
1414
int fclose(FILE *);
1515
void exit(int);
1616

clang/test/Analysis/Inputs/system-header-simulator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ int fflush(FILE *stream);
7171
size_t strlen(const char *);
7272

7373
char *strcpy(char *restrict, const char *restrict);
74-
char *strncpy(char *dst, const char *src, size_t n);
75-
char *strsep(char **stringp, const char *delim);
76-
void *memcpy(void *dst, const void *src, size_t n);
74+
char *strncpy(char *restrict dst, const char *restrict src, size_t n);
75+
char *strsep(char **restrict stringp, const char *restrict delim);
76+
void *memcpy(void *restrict dst, const void *restrict src, size_t n);
7777
void *memset(void *s, int c, size_t n);
7878

7979
typedef unsigned long __darwin_pthread_key_t;

clang/test/Analysis/bsd-string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#define NULL ((void *)0)
88

99
typedef __typeof(sizeof(int)) size_t;
10-
size_t strlcpy(char *dst, const char *src, size_t n);
11-
size_t strlcat(char *dst, const char *src, size_t n);
10+
size_t strlcpy(char *restrict dst, const char *restrict src, size_t n);
11+
size_t strlcat(char *restrict dst, const char *restrict src, size_t n);
1212
size_t strlen(const char *s);
1313
void clang_analyzer_eval(int);
1414

clang/test/Analysis/string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void clang_analyzer_eval(int);
7171
int scanf(const char *restrict format, ...);
7272
void *malloc(size_t);
7373
void free(void *);
74-
void *memcpy(void *dest, const void *src, size_t n);
74+
void *memcpy(void *restrict dest, const void *restrict src, size_t n);
7575

7676
//===----------------------------------------------------------------------===
7777
// strlen()
@@ -1252,7 +1252,7 @@ int strncasecmp_null_argument(char *a, size_t n) {
12521252
// strsep()
12531253
//===----------------------------------------------------------------------===
12541254

1255-
char *strsep(char **stringp, const char *delim);
1255+
char *strsep(char ** restrict stringp, const char * restrict delim);
12561256

12571257
void strsep_null_delim(char *s) {
12581258
strsep(&s, NULL); // expected-warning{{Null pointer passed as 2nd argument to strsep()}}

libc/spec/bsd_ext.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ def BsdExtensions : StandardSpec<"BSDExtensions"> {
88
FunctionSpec<
99
"strlcat",
1010
RetValSpec<SizeTType>,
11-
[ArgSpec<CharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<SizeTType>]
11+
[ArgSpec<ConstCharRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>, ArgSpec<SizeTType>]
1212
>,
1313
FunctionSpec<
1414
"strlcpy",
1515
RetValSpec<SizeTType>,
16-
[ArgSpec<CharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<SizeTType>]
16+
[ArgSpec<ConstCharRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>, ArgSpec<SizeTType>]
1717
>,
1818
FunctionSpec<
1919
"strsep",

libc/src/string/strsep.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
namespace LIBC_NAMESPACE {
1414

15-
LLVM_LIBC_FUNCTION(char *, strsep, (char **stringp, const char *delim)) {
15+
LLVM_LIBC_FUNCTION(char *, strsep,
16+
(char **__restrict stringp, const char *__restrict delim)) {
1617
if (!*stringp)
1718
return nullptr;
1819
return internal::string_token<false>(*stringp, delim, stringp);

libc/src/string/strsep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace LIBC_NAMESPACE {
1313

14-
char *strsep(char **stringp, const char *delim);
14+
char *strsep(char **__restrict stringp, const char *__restrict delim);
1515

1616
} // namespace LIBC_NAMESPACE
1717

0 commit comments

Comments
 (0)