Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/fd-access-mode-target-headers.c

raw
1 /* { dg-skip-if "" { { powerpc*-*-aix* avr-*-* *-*-vxworks* } || newlib } } */
2 /* { dg-skip-if "PR analyzer/107750" { *-*-solaris2* } } */
3
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include "analyzer-decls.h"
8
9 void f (int fd) __attribute__((fd_arg(1))); /* { dg-message "argument 1 of 'f' must be an open file descriptor, due to '__attribute__\\(\\(fd_arg\\(1\\)\\)\\)'" } */
10
11 void
12 test_1 (const char *path)
13 {
14 int fd = open (path, O_RDWR);
15 close(fd);
16 f(fd); /* { dg-warning "'f' on closed file descriptor 'fd'" } */
17 /* { dg-message "\\(3\\) 'f' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" { target *-*-* } .-1 } */
18 }
19
20 void g (int fd) __attribute__((fd_arg_read(1))); /* { dg-message "argument 1 of 'g' must be a readable file descriptor, due to '__attribute__\\(\\(fd_arg_read\\(1\\)\\)\\)'" } */
21
22 void
23 test_2 (const char *path)
24 {
25 int fd = open (path, O_WRONLY);
26 if (fd != -1)
27 {
28 g (fd); /* { dg-warning "'g' on write-only file descriptor 'fd'" } */
29 }
30 close (fd);
31 }
32
33 void h (int fd) __attribute__((fd_arg_write(1))); /* { dg-message "argument 1 of 'h' must be a writable file descriptor, due to '__attribute__\\(\\(fd_arg_write\\(1\\)\\)\\)'" } */
34 void
35 test_3 (const char *path)
36 {
37 int fd = open (path, O_RDONLY);
38 if (fd != -1)
39 {
40 h (fd); /* { dg-warning "'h' on read-only file descriptor 'fd'" } */
41 }
42 close(fd);
43 }
44
45 void ff (int fd) __attribute__((fd_arg(1))); /* { dg-message "argument 1 of 'ff' must be an open file descriptor, due to '__attribute__\\(\\(fd_arg\\(1\\)\\)\\)'" } */
46
47 void test_4 (const char *path)
48 {
49 int fd = open (path, O_RDWR);
50 ff (fd); /* { dg-warning "'ff' on possibly invalid file descriptor 'fd'" } */
51 close(fd);
52 }
53
54 void test_sm_fd_constants (void)
55 {
56 __analyzer_dump_named_constant ("O_ACCMODE"); /* { dg-warning "named constant 'O_ACCMODE' has value '\[0-9\]+'" } */
57 __analyzer_dump_named_constant ("O_RDONLY"); /* { dg-warning "named constant 'O_RDONLY' has value '\[0-9\]+'" } */
58 __analyzer_dump_named_constant ("O_WRONLY"); /* { dg-warning "named constant 'O_WRONLY' has value '\[0-9\]+'" } */
59 }