| 1 |
// PR66516 - missing diagnostic on taking the address of a builtin function |
| 2 |
// { dg-do compile } |
| 3 |
|
| 4 |
namespace std { |
| 5 |
// Define type_info type to be able to use typeid in tests without |
| 6 |
// having to include <typeinfo>. |
| 7 |
struct type_info { |
| 8 |
const char *name_; |
| 9 |
|
| 10 |
explicit type_info (const char *s): name_ (s) { } |
| 11 |
const char* name() const { return name_; } |
| 12 |
}; |
| 13 |
} |
| 14 |
|
| 15 |
// Extern "C" since builtin functions used in tests have C linkage. |
| 16 |
extern "C" { |
| 17 |
|
| 18 |
typedef void (F)(); |
| 19 |
typedef __UINTPTR_TYPE__ uintptr_t; |
| 20 |
|
| 21 |
// Utility function to test passing built-in functions as an ordinary |
| 22 |
// argument and via the ellipsis. |
| 23 |
static void func_arg (F*, ...); |
| 24 |
|
| 25 |
// Utility function with which, along with the built-in function, |
| 26 |
// to instantiate the C98 multi-parameter or C11 variadic tempates |
| 27 |
// below. |
| 28 |
void f () { } |
| 29 |
|
| 30 |
} // extern "C" |
| 31 |
|
| 32 |
|
| 33 |
// Utility templates to test specializing templates on pointers and |
| 34 |
// references to built-in functions. |
| 35 |
template <F*> struct TestPointer { }; |
| 36 |
template <F&> struct TestReference { }; |
| 37 |
|
| 38 |
#if 201103 <= __cplusplus |
| 39 |
|
| 40 |
template <F*...> struct TestPointers { }; |
| 41 |
template <F&...> struct TestReferences { }; |
| 42 |
|
| 43 |
#else |
| 44 |
|
| 45 |
template <F* = &f, F* = &f> struct TestPointers { }; |
| 46 |
template <F& = f, F& = f> struct TestReferences { }; |
| 47 |
|
| 48 |
#endif |
| 49 |
|
| 50 |
static F* test_taking_address_of_gcc_builtin () |
| 51 |
{ |
| 52 |
enum UINTPTR_E { e = ~(uintptr_t)0 }; |
| 53 |
|
| 54 |
F *p; |
| 55 |
void *q; |
| 56 |
uintptr_t a; |
| 57 |
|
| 58 |
__builtin_trap (); // okay |
| 59 |
(void)__builtin_trap; // okay |
| 60 |
__builtin_trap; // okay (if pointless) |
| 61 |
|
| 62 |
{ |
| 63 |
typedef __typeof__ (__builtin_trap) F; // okay |
| 64 |
} |
| 65 |
|
| 66 |
#if 201103 <= __cplusplus |
| 67 |
{ |
| 68 |
typedef decltype (__builtin_trap) F; // okay |
| 69 |
|
| 70 |
a = noexcept (&__builtin_trap); |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
// Address and indirection operators. |
| 75 |
p = &__builtin_trap; // { dg-error "built-in" } |
| 76 |
p = *__builtin_trap; // { dg-error "built-in" } |
| 77 |
|
| 78 |
// Unary NOT. |
| 79 |
// GCC issues two diagnostics here for some reason, so account for both. |
| 80 |
a = !__builtin_trap; // { dg-error "built-in|unary" } |
| 81 |
|
| 82 |
// Casts. |
| 83 |
p = (F*)__builtin_trap; // { dg-error "built-in" } |
| 84 |
|
| 85 |
p = &(F&)__builtin_trap; // { dg-error "built-in" } |
| 86 |
|
| 87 |
p = &reinterpret_cast<F&>(__builtin_trap); // { dg-error "built-in" } |
| 88 |
p = &static_cast<F&>(__builtin_trap); // { dg-error "built-in" } |
| 89 |
|
| 90 |
p = reinterpret_cast<F*>(__builtin_trap); // { dg-error "built-in" } |
| 91 |
p = static_cast<F*>(__builtin_trap); // { dg-error "built-in" } |
| 92 |
|
| 93 |
// Expect a diagnostic for an invalid static_cast of a function to |
| 94 |
// either uintptr_t or enum, rather than one for the argument being |
| 95 |
// a built-in function, since the former is more relevant than the latter. |
| 96 |
a = static_cast<uintptr_t>(__builtin_trap); // { dg-error "7:invalid .static_cast." } |
| 97 |
a = static_cast<UINTPTR_E>(__builtin_trap); // { dg-error "7:invalid .static_cast." } |
| 98 |
|
| 99 |
// Reinterpret cast can cast a function to uintptr_t or enum, |
| 100 |
// so verify that a diagnostic is issued for the use of a builtin. |
| 101 |
a = reinterpret_cast<uintptr_t>(__builtin_trap); // { dg-error "built-in" } |
| 102 |
a = reinterpret_cast<UINTPTR_E>(__builtin_trap); // { dg-error "built-in" } |
| 103 |
|
| 104 |
// Additive operator. Ill-formed but allowed with -fpermissive. |
| 105 |
p = __builtin_trap + 0; // { dg-error "built-in" } |
| 106 |
p = __builtin_trap - 0; // { dg-error "built-in" } |
| 107 |
a = __builtin_trap - p; // { dg-error "built-in" } |
| 108 |
a = p - __builtin_trap; // { dg-error "built-in" } |
| 109 |
|
| 110 |
// Relational operators. Ill-formed but allowed with -fpermissive. |
| 111 |
a = __builtin_trap < p; // { dg-error "built-in|invalid template-argument-list" } |
| 112 |
a = p < __builtin_trap; // { dg-error "built-in" } |
| 113 |
|
| 114 |
a = __builtin_trap <= p; // { dg-error "built-in" } |
| 115 |
a = p <= __builtin_trap; // { dg-error "built-in" } |
| 116 |
|
| 117 |
a = __builtin_trap > p; // { dg-error "built-in" } |
| 118 |
a = p > __builtin_trap; // { dg-error "built-in" } |
| 119 |
|
| 120 |
a = __builtin_trap > p; // { dg-error "built-in" } |
| 121 |
a = p > __builtin_trap; // { dg-error "built-in" } |
| 122 |
|
| 123 |
a = __builtin_trap <= p; // { dg-error "built-in" } |
| 124 |
a = p <= __builtin_trap; // { dg-error "built-in" } |
| 125 |
|
| 126 |
a = __builtin_trap <= p; // { dg-error "built-in" } |
| 127 |
a = p <= __builtin_trap; // { dg-error "built-in" } |
| 128 |
|
| 129 |
// Equality operators. |
| 130 |
a = __builtin_trap == p; // { dg-error "built-in" } |
| 131 |
a = p == __builtin_trap; // { dg-error "built-in" } |
| 132 |
a = __builtin_trap != p; // { dg-error "built-in" } |
| 133 |
a = p != __builtin_trap; // { dg-error "built-in" } |
| 134 |
|
| 135 |
// Logical AND and OR. |
| 136 |
a = __builtin_trap && p; // { dg-error "built-in" } |
| 137 |
a = p && __builtin_trap; // { dg-error "built-in" } |
| 138 |
|
| 139 |
a = __builtin_trap || p; // { dg-error "built-in" } |
| 140 |
a = p || __builtin_trap; // { dg-error "built-in" } |
| 141 |
|
| 142 |
// Conditional operator. |
| 143 |
a = __builtin_trap ? 1 : 0; // { dg-error "built-in" } |
| 144 |
p = a ? __builtin_trap : 0; // { dg-error "built-in" } |
| 145 |
p = a ? 0 : __builtin_trap; // { dg-error "built-in" } |
| 146 |
|
| 147 |
// Assignment operator. |
| 148 |
p = __builtin_trap; // { dg-error "built-in" } |
| 149 |
|
| 150 |
// Passing as an argument. |
| 151 |
func_arg (__builtin_trap); // { dg-error "built-in" } |
| 152 |
func_arg (&__builtin_trap); // { dg-error "built-in" } |
| 153 |
func_arg (*__builtin_trap); // { dg-error "built-in" } |
| 154 |
|
| 155 |
// Passing through ellipsis. |
| 156 |
func_arg (0, __builtin_trap); // { dg-error "built-in" } |
| 157 |
func_arg (0, &__builtin_trap); // { dg-error "built-in" } |
| 158 |
func_arg (0, *__builtin_trap); // { dg-error "built-in" } |
| 159 |
|
| 160 |
{ |
| 161 |
// Template specialization. |
| 162 |
// GCC issues two diagnostics and we must account for both. |
| 163 |
TestPointer<__builtin_trap> tp; // { dg-error "built-in|could not convert" } |
| 164 |
TestReference<__builtin_trap> tr; // { dg-error "built-in|could not convert" } |
| 165 |
|
| 166 |
TestPointers<__builtin_trap> tp1; // { dg-error "built-in|could not convert" } |
| 167 |
TestReferences<__builtin_trap> tr1; // { dg-error "built-in|could not convert" } |
| 168 |
|
| 169 |
TestPointers<f, __builtin_trap> tp2; // { dg-error "built-in|could not convert" } |
| 170 |
TestReferences<f, __builtin_trap> tr2; // { dg-error "built-in|could not convert" } |
| 171 |
|
| 172 |
TestPointers<__builtin_trap, f> tp3; // { dg-error "built-in|could not convert" } |
| 173 |
TestReferences<__builtin_trap, f> tr3; // { dg-error "built-in|could not convert" } |
| 174 |
} |
| 175 |
|
| 176 |
try { |
| 177 |
throw __builtin_trap; // { dg-error "built-in" } |
| 178 |
} |
| 179 |
catch (F) { } |
| 180 |
|
| 181 |
return __builtin_trap; // { dg-error "built-in" } |
| 182 |
|
| 183 |
(void)a; |
| 184 |
(void)p; |
| 185 |
(void)q; |
| 186 |
} |
| 187 |
|
| 188 |
// Make sure operators new and delete don't trigger false positives |
| 189 |
// (they return true from DECL_IS_BUILTIN(DECL) -- see tree.h). |
| 190 |
void test_taking_address_of_op_new_and_delete () |
| 191 |
{ |
| 192 |
typedef __SIZE_TYPE__ size_t; |
| 193 |
|
| 194 |
typedef void* (OpNew) (size_t); |
| 195 |
typedef void (OpDelete) (void*); |
| 196 |
|
| 197 |
OpNew &newr = operator new; |
| 198 |
OpNew &newra = operator new[]; |
| 199 |
OpNew *newp = &operator new; |
| 200 |
newp = &operator new[]; |
| 201 |
|
| 202 |
OpDelete &delr = operator delete; |
| 203 |
OpDelete &delra = operator delete[]; |
| 204 |
OpDelete *delp = &operator delete; |
| 205 |
delp = &operator delete[]; |
| 206 |
|
| 207 |
(void)newr; |
| 208 |
(void)newra; |
| 209 |
(void)newp; |
| 210 |
(void)delr; |
| 211 |
(void)delra; |
| 212 |
(void)delp; |
| 213 |
} |
| 214 |
|
| 215 |
// Helper declaration to verify that it's possible to take the address |
| 216 |
// of a user-declared function that's also a GCC built-in. |
| 217 |
extern int abs (int); |
| 218 |
|
| 219 |
typedef __SIZE_TYPE__ size_t; |
| 220 |
extern size_t strlen (const char*); |
| 221 |
|
| 222 |
// Creating a reference to or taking the address of a built-in with |
| 223 |
// a library "fallback" must be allowed. |
| 224 |
void test_taking_address_of_library_builtin () |
| 225 |
{ |
| 226 |
{ |
| 227 |
typedef int F (int); |
| 228 |
|
| 229 |
F &r1 = __builtin_abs; |
| 230 |
F &r2 = *__builtin_abs; |
| 231 |
F *p = __builtin_abs; |
| 232 |
p = &__builtin_abs; |
| 233 |
p = *__builtin_abs; |
| 234 |
(void)p; |
| 235 |
(void)r1; |
| 236 |
(void)r2; |
| 237 |
} |
| 238 |
|
| 239 |
{ |
| 240 |
typedef int F (int); |
| 241 |
|
| 242 |
F &r1 = abs; |
| 243 |
F &r2 = *abs; |
| 244 |
F *p = abs; |
| 245 |
p = &abs; |
| 246 |
p = *abs; |
| 247 |
(void)p; |
| 248 |
(void)r1; |
| 249 |
(void)r2; |
| 250 |
} |
| 251 |
|
| 252 |
{ |
| 253 |
typedef __SIZE_TYPE__ size_t; |
| 254 |
typedef size_t F (const char*); |
| 255 |
F &r1 = __builtin_strlen; |
| 256 |
F &r2 = *__builtin_strlen; |
| 257 |
F *p = __builtin_strlen; |
| 258 |
p = &__builtin_strlen; |
| 259 |
p = *__builtin_strlen; |
| 260 |
(void)p; |
| 261 |
(void)r1; |
| 262 |
(void)r2; |
| 263 |
} |
| 264 |
|
| 265 |
{ |
| 266 |
typedef size_t F (const char*); |
| 267 |
F &r1 = strlen; |
| 268 |
F &r2 = *strlen; |
| 269 |
F *p = strlen; |
| 270 |
p = &strlen; |
| 271 |
p = *strlen; |
| 272 |
(void)p; |
| 273 |
(void)r1; |
| 274 |
(void)r2; |
| 275 |
} |
| 276 |
} |