annotate clang/test/SemaCXX/null_in_arithmetic_ops.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int -Wno-tautological-pointer-compare %s
anatofuz
parents:
diff changeset
2 #include <stddef.h>
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 void f() {
anatofuz
parents:
diff changeset
5 int a;
anatofuz
parents:
diff changeset
6 bool b;
anatofuz
parents:
diff changeset
7 void (^c)();
anatofuz
parents:
diff changeset
8 class X;
anatofuz
parents:
diff changeset
9 void (X::*d) ();
anatofuz
parents:
diff changeset
10 extern void e();
anatofuz
parents:
diff changeset
11 int f[2];
anatofuz
parents:
diff changeset
12 const void *v;
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 a = 0 ? NULL + a : a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
15 a = 0 ? NULL - a : a - NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
16 a = 0 ? NULL / a : a / NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
anatofuz
parents:
diff changeset
17 // expected-warning {{division by zero is undefined}}
anatofuz
parents:
diff changeset
18 a = 0 ? NULL * a : a * NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
19 a = 0 ? NULL >> a : a >> NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
20 a = 0 ? NULL << a : a << NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
21 a = 0 ? NULL % a : a % NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \
anatofuz
parents:
diff changeset
22 expected-warning {{remainder by zero is undefined}}
anatofuz
parents:
diff changeset
23 a = 0 ? NULL & a : a & NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
24 a = 0 ? NULL | a : a | NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
25 a = 0 ? NULL ^ a : a ^ NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 // Check for warnings or errors when doing arithmetic on pointers and other
anatofuz
parents:
diff changeset
28 // types.
anatofuz
parents:
diff changeset
29 v = 0 ? NULL + &a : &a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
30 v = 0 ? NULL + c : c + NULL; // \
anatofuz
parents:
diff changeset
31 expected-error {{invalid operands to binary expression ('long' and 'void (^)()')}} \
anatofuz
parents:
diff changeset
32 expected-error {{invalid operands to binary expression ('void (^)()' and 'long')}}
anatofuz
parents:
diff changeset
33 v = 0 ? NULL + d : d + NULL; // \
anatofuz
parents:
diff changeset
34 expected-error {{invalid operands to binary expression ('long' and 'void (X::*)()')}} \
anatofuz
parents:
diff changeset
35 expected-error {{invalid operands to binary expression ('void (X::*)()' and 'long')}}
anatofuz
parents:
diff changeset
36 v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}}
anatofuz
parents:
diff changeset
37 v = 0 ? NULL + f : f + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
38 v = 0 ? NULL + "f" : "f" + NULL; // expected-warning 2{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 // Using two NULLs should only give one error instead of two.
anatofuz
parents:
diff changeset
41 a = NULL + NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
42 a = NULL - NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
43 a = NULL / NULL; // expected-warning{{use of NULL in arithmetic operation}} \
anatofuz
parents:
diff changeset
44 // expected-warning{{division by zero is undefined}}
anatofuz
parents:
diff changeset
45 a = NULL * NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
46 a = NULL >> NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
47 a = NULL << NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
48 a = NULL % NULL; // expected-warning{{use of NULL in arithmetic operation}} \
anatofuz
parents:
diff changeset
49 // expected-warning{{remainder by zero is undefined}}
anatofuz
parents:
diff changeset
50 a = NULL & NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
51 a = NULL | NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
52 a = NULL ^ NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 a += NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
55 a -= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
56 a /= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
anatofuz
parents:
diff changeset
57 // expected-warning{{division by zero is undefined}}
anatofuz
parents:
diff changeset
58 a *= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
59 a >>= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
60 a <<= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
61 a %= NULL; // expected-warning{{use of NULL in arithmetic operation}} \
anatofuz
parents:
diff changeset
62 // expected-warning{{remainder by zero is undefined}}
anatofuz
parents:
diff changeset
63 a &= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
64 a |= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
65 a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}}
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
anatofuz
parents:
diff changeset
68 b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
anatofuz
parents:
diff changeset
69 b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
anatofuz
parents:
diff changeset
70 b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
anatofuz
parents:
diff changeset
71 b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
anatofuz
parents:
diff changeset
72 b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 b = &a < NULL || NULL < &a || &a > NULL || NULL > &a; // expected-error 4{{ordered comparison between pointer and zero}}
anatofuz
parents:
diff changeset
75 b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a; // expected-error 4{{ordered comparison between pointer and zero}}
anatofuz
parents:
diff changeset
76 b = &a == NULL || NULL == &a || &a != NULL || NULL != &a;
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 b = 0 == a;
anatofuz
parents:
diff changeset
79 b = 0 == &a;
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 b = NULL < NULL || NULL > NULL;
anatofuz
parents:
diff changeset
82 b = NULL <= NULL || NULL >= NULL;
anatofuz
parents:
diff changeset
83 b = NULL == NULL || NULL != NULL;
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 b = ((NULL)) != a; // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}}
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 // Check that even non-standard pointers don't warn.
anatofuz
parents:
diff changeset
88 b = c == NULL || NULL == c || c != NULL || NULL != c;
anatofuz
parents:
diff changeset
89 b = d == NULL || NULL == d || d != NULL || NULL != d;
anatofuz
parents:
diff changeset
90 b = e == NULL || NULL == e || e != NULL || NULL != e;
anatofuz
parents:
diff changeset
91 b = f == NULL || NULL == f || f != NULL || NULL != f;
anatofuz
parents:
diff changeset
92 b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f";
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 return NULL; // expected-error{{void function 'f' should not return a value}}
anatofuz
parents:
diff changeset
95 }