11cb2d742eb6635aeab6132ee5f0b5781d39487d7Nico Weber// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int %s 23e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu#include <stddef.h> 33e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 43e95ba94fd34c5f6420c57d7732f601875074681Richard Trieuvoid f() { 53e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu int a; 63e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu bool b; 71567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth void (^c)(); 81567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth class X; 91567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth void (X::*d) (); 101567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth extern void e(); 111567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth int f[2]; 121567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth const void *v; 133e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 143e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL + a : a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 153e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL - a : a - NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 163e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL / a : a / NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \ 173e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu // expected-warning {{division by zero is undefined}} 183e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL * a : a * NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 193e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL >> a : a >> NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 203e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL << a : a << NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 213e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL % a : a % NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \ 223e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu expected-warning {{remainder by zero is undefined}} 233e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL & a : a & NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 243e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL | a : a | NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 253e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = 0 ? NULL ^ a : a ^ NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 263e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 271567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth // Check for warnings or errors when doing arithmetic on pointers and other 281567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth // types. 291567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth v = 0 ? NULL + &a : &a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 301567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth v = 0 ? NULL + c : c + NULL; // \ 311567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth expected-error {{invalid operands to binary expression ('long' and 'void (^)()')}} \ 321567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth expected-error {{invalid operands to binary expression ('void (^)()' and 'long')}} 331567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth v = 0 ? NULL + d : d + NULL; // \ 341567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth expected-error {{invalid operands to binary expression ('long' and 'void (X::*)()')}} \ 351567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth expected-error {{invalid operands to binary expression ('void (X::*)()' and 'long')}} 3613b21be065e9feb0759303bd51b8e8653130f0fbChandler Carruth v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}} 371567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth v = 0 ? NULL + f : f + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 381567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth v = 0 ? NULL + "f" : "f" + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} 391567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth 403e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu // Using two NULLs should only give one error instead of two. 413e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL + NULL; // expected-warning{{use of NULL in arithmetic operation}} 423e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL - NULL; // expected-warning{{use of NULL in arithmetic operation}} 433e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL / NULL; // expected-warning{{use of NULL in arithmetic operation}} \ 443e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu // expected-warning{{division by zero is undefined}} 453e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL * NULL; // expected-warning{{use of NULL in arithmetic operation}} 463e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL >> NULL; // expected-warning{{use of NULL in arithmetic operation}} 473e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL << NULL; // expected-warning{{use of NULL in arithmetic operation}} 483e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL % NULL; // expected-warning{{use of NULL in arithmetic operation}} \ 493e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu // expected-warning{{remainder by zero is undefined}} 503e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL & NULL; // expected-warning{{use of NULL in arithmetic operation}} 513e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL | NULL; // expected-warning{{use of NULL in arithmetic operation}} 523e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a = NULL ^ NULL; // expected-warning{{use of NULL in arithmetic operation}} 533e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 543e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a += NULL; // expected-warning{{use of NULL in arithmetic operation}} 553e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a -= NULL; // expected-warning{{use of NULL in arithmetic operation}} 563e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a /= NULL; // expected-warning{{use of NULL in arithmetic operation}} \ 573e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu // expected-warning{{division by zero is undefined}} 583e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a *= NULL; // expected-warning{{use of NULL in arithmetic operation}} 593e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a >>= NULL; // expected-warning{{use of NULL in arithmetic operation}} 603e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a <<= NULL; // expected-warning{{use of NULL in arithmetic operation}} 613e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a %= NULL; // expected-warning{{use of NULL in arithmetic operation}} \ 623e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu // expected-warning{{remainder by zero is undefined}} 633e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a &= NULL; // expected-warning{{use of NULL in arithmetic operation}} 643e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a |= NULL; // expected-warning{{use of NULL in arithmetic operation}} 653e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}} 663e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 6779e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}} 6879e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}} 6979e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}} 7079e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}} 7179e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}} 7279e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}} 733e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 743e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = &a < NULL || NULL < &a || &a > NULL || NULL > &a; 753e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a; 763e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = &a == NULL || NULL == &a || &a != NULL || NULL != &a; 773e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 783e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = 0 == a; 793e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = 0 == &a; 803e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 812af68e4761ed30181540dafb5572993daffa4910Chandler Carruth b = NULL < NULL || NULL > NULL; 822af68e4761ed30181540dafb5572993daffa4910Chandler Carruth b = NULL <= NULL || NULL >= NULL; 832af68e4761ed30181540dafb5572993daffa4910Chandler Carruth b = NULL == NULL || NULL != NULL; 842af68e4761ed30181540dafb5572993daffa4910Chandler Carruth 8579e610a99b43b9e6df8f9b6b59dbaf5d38a682d3Richard Trieu b = ((NULL)) != a; // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}} 863e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu 871567a8ba8daaaa91a5de3c23026c9c19de017bd1Chandler Carruth // Check that even non-standard pointers don't warn. 883e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = c == NULL || NULL == c || c != NULL || NULL != c; 893e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu b = d == NULL || NULL == d || d != NULL || NULL != d; 902af68e4761ed30181540dafb5572993daffa4910Chandler Carruth b = e == NULL || NULL == e || e != NULL || NULL != e; 912af68e4761ed30181540dafb5572993daffa4910Chandler Carruth b = f == NULL || NULL == f || f != NULL || NULL != f; 922af68e4761ed30181540dafb5572993daffa4910Chandler Carruth b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f"; 93e81b43bc306d361fcf4d411aeb14b43f8464bf4aDavid Blaikie 94e81b43bc306d361fcf4d411aeb14b43f8464bf4aDavid Blaikie return NULL; // expected-error{{void function 'f' should not return a value}} 953e95ba94fd34c5f6420c57d7732f601875074681Richard Trieu} 96