Daily bump.
[gcc.git] / gcc / testsuite / g++.dg / warn / Waddress-5.C
blob1de88076f7767d769acb185ece6647adc3d0036b
1 /* PR c/102103 - missing warning comparing array address to null
2    { dg-do compile }
3    { dg-options "-Wall" } */
5 #if __cplusplus < 201103L
6 # define nullptr __null
7 #endif
9 struct A
11   void f ();
12   virtual void vf ();
13   virtual void pvf () = 0;
15   static void sf ();
17   int *p;
18   int a[2];
21 void T (bool);
23 void warn_memptr_if ()
25   // Exercise warnings for addresses of nonstatic member functions.
26   // On targets with TARGET_PTRMEMFUNC_VBIT_LOCATION ==
27   // ptrmemfunc_vbit_in_delta, cp_build_binary_op recurses to compare
28   // the pfn from the ptrmemfunc with null, so we get two warnings.
29   // This matches both.  ??? Should we disable one of them?
30   if (&A::f == 0)         // { dg-warning "A::f" }
31     T (0);
33   if (&A::vf)             // { dg-warning "-Waddress" }
34     T (0);
36   if (&A::pvf != 0)       // { dg-warning "-Waddress" }
37     T (0);
39   // Exercise warnings for addresses of static member functions.
40   if (&A::sf == 0)        // { dg-warning "-Waddress" }
41     T (0);
43   if (&A::sf)             // { dg-warning "-Waddress" }
44     T (0);
46   // Exercise warnings for addresses of nonstatic data members.
47   if (&A::p == 0)         // { dg-warning "the address '&A::p'" }
48     T (0);
50   if (&A::a == nullptr)   // { dg-warning "-Waddress" }
51     T (0);
54 void warn_memptr_bool ()
56   // Exercise warnings for addresses of nonstatic member functions.
57   T (&A::f == 0);         // { dg-warning "-Waddress" }
58   T (&A::vf);             // { dg-warning "-Waddress" }
59   T (&A::pvf != 0);       // { dg-warning "-Waddress" }
61   // Exercise warnings for addresses of static member functions.
62   T (&A::sf == 0);        // { dg-warning "-Waddress" }
63   T (&A::sf);             // { dg-warning "-Waddress" }
65   // Exercise warnings for addresses of nonstatic data members.
66   T (&A::p == 0);         // { dg-warning "-Waddress" }
67   T (&A::a == nullptr);   // { dg-warning "-Waddress" }
71 /* Verify that no warnings are issued for a dependent expression in
72    a template.  */
74 template <int>
75 struct B
77   // This is why.
78   struct F { void* operator& () const { return 0; } } f;
81 template <class Type, int N>
82 void nowarn_dependent (Type targ)
84   T (&Type::x == 0);
85   T (&targ == 0);
87   Type tarr[1];
88   T (&tarr[0] == nullptr);
90   T (&B<N>::f == 0);
92   /* Like in the case above, the address-of operator could be a member
93      of B<N>::vf that returns zero.  */
94   T (&B<N>::vf);
95   T (&B<N>::pvf != 0);
96   T (&B<N>::p == 0);
97   T (&B<N>::a == 0);
101 /* Verify that in an uninstantiated template warnings are not issued
102    for dependent expressions but are issued otherwise.  */
104 template <class Type>
105 void warn_non_dependent (Type targ, Type *tptr, int i)
107   /* The address of a pointer to a dependent type cannot be null but
108      the warning doesn't have a chance to see it.  */
109   T (&tptr == 0);       // { dg-warning "-Waddress" "pr102378" { xfail *-*-* } }
110   T (&i == 0);          // { dg-warning "-Waddress" }
112   int iarr[1];
113   T (&iarr == 0);       // { dg-warning "-Waddress" }
114   T (&*iarr != 0);      // { dg-warning "-Waddress" "pr102378" { xfail *-*-* } }
115   T (&iarr[0] == 0);    // { dg-warning "-Waddress" }
117   Type tarr[1];
118   T (&tarr == nullptr);   // { dg-warning "-Waddress" "pr102378" { xfail *-*-* } }