(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
attr-malloc.c
       1  /* PR middle-end/94527 - Add an attribute that marks a function as freeing
       2     an object
       3     Verify that attribute malloc with one or two arguments is accepted where
       4     intended and rejected where it's invalid.
       5     { dg-options "-Wall -ftrack-macro-expansion=0" } */
       6  
       7  #define A(...) __attribute__ ((malloc (__VA_ARGS__)))
       8  
       9  A (0) void* alloc_zero (int);           // { dg-error "'malloc' attribute argument 1 does not name a function" }
      10  
      11  A ("") void* alloc_string (int);        // { dg-error "'malloc' attribute argument 1 does not name a function" }
      12  
      13  int var;
      14  A (var) void* alloc_var (int);          // { dg-error "'malloc' attribute argument 1 does not name a function" }
      15  
      16  typedef struct Type { int i; } Type;
      17  A (Type) void* alloc_type (int);        // { dg-error "expected expression|identifier" }
      18  
      19  A (unknown) void* alloc_unknown (int);  // { dg-error "'unknown' undeclared" }
      20  
      21  void fv_ ();                            // { dg-message "declared here" }
      22  A (fv_) void* alloc_fv_ (int);          // { dg-error "'malloc' attribute argument 1 must take a pointer type as its first argument" }
      23  
      24  void fvi (int);                         // { dg-message "declared here" }
      25  A (fvi) void* alloc_fvi (int);          // { dg-error "'malloc' attribute argument 1 must take a pointer type as its first argument; have 'int'" }
      26  
      27  void fvv (void);                        // { dg-message "declared here" }
      28  A (fvv) void* alloc_fvv (int);          // { dg-error "'malloc' attribute argument 1 must take a pointer type as its first argument; have 'void'" }
      29  
      30  void fvi_ (int, ...);                   // { dg-message "declared here" }
      31  A (fvi_) void* alloc_fvi_ (int);        // { dg-error "'malloc' attribute argument 1 must take a pointer type as its first argument; have 'int'" }
      32  
      33  void fvi_vp (Type, void*);              // { dg-message "declared here" }
      34  A (fvi_vp) void* alloc_fvi_vp (int);    // { dg-error "'malloc' attribute argument 1 must take a pointer type as its first argument; have 'Type'" }
      35  
      36  
      37  void fpv (void*);
      38  A (fpv) void* alloc_fpv (int);
      39  
      40  void fpv_i (void*, int);
      41  A (fpv_i) void* alloc_fpv_i (int);
      42  
      43  void fpv_pv (void*, void*);
      44  A (fpv_i) void* alloc_fpv_pv (int);
      45  
      46  
      47  void gpc (char*);
      48  void hpi (int*);
      49  A (fpv) A (gpc) A (hpi) Type* alloc_fpv_gpv (int);
      50  
      51  
      52  /* Verify that the attribute can be applied to <stdio.h> functions.  */
      53  typedef struct FILE FILE;
      54  typedef __SIZE_TYPE__ size_t;
      55  
      56  int   fclose (FILE*);
      57  FILE* fdopen (int);
      58  FILE* fopen (const char*, const char*);
      59  FILE* freopen (const char*, const char*, FILE*);
      60  int   pclose (FILE*);
      61  FILE* popen (const char*, const char*);
      62  FILE* tmpfile (void);
      63  
      64  A (fclose) A (freopen, 3) A (pclose)
      65    FILE* fdopen (int);
      66  A (fclose) A (freopen, 3) A (pclose)
      67    FILE* fopen (const char*, const char*);
      68  A (fclose) A (freopen, 3) A (pclose)
      69    FILE* fmemopen(void *, size_t, const char *);
      70  A (fclose) A (freopen, 3) A (pclose)
      71    FILE* freopen (const char*, const char*, FILE*);
      72  A (fclose) A (freopen, 3) A (pclose)
      73    FILE* popen (const char*, const char*);
      74  A (fclose) A (freopen, 3) A (pclose)
      75    FILE* tmpfile (void);