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