annotate clang/test/SemaCXX/warn-self-assign-field-builtin.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -verify %s
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 struct C {
anatofuz
parents:
diff changeset
4 int a;
anatofuz
parents:
diff changeset
5 int b;
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 void f() {
anatofuz
parents:
diff changeset
8 a = a; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
9 b = b; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
10 a = b;
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 this->a = a; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
13 this->b = b; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
14 a = this->a; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
15 b = this->b; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
16 this->a = this->a; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
17 this->b = this->b; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 a = b;
anatofuz
parents:
diff changeset
20 a = this->b;
anatofuz
parents:
diff changeset
21 this->a = b;
anatofuz
parents:
diff changeset
22 this->a = this->b;
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 a *= a;
anatofuz
parents:
diff changeset
25 a /= a;
anatofuz
parents:
diff changeset
26 a %= a;
anatofuz
parents:
diff changeset
27 a += a;
anatofuz
parents:
diff changeset
28 a -= a;
anatofuz
parents:
diff changeset
29 a <<= a;
anatofuz
parents:
diff changeset
30 a >>= a;
anatofuz
parents:
diff changeset
31 a &= a;
anatofuz
parents:
diff changeset
32 a |= a;
anatofuz
parents:
diff changeset
33 a ^= a;
anatofuz
parents:
diff changeset
34 }
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 void false_positives() {
anatofuz
parents:
diff changeset
37 #define OP =
anatofuz
parents:
diff changeset
38 #define LHS a
anatofuz
parents:
diff changeset
39 #define RHS a
anatofuz
parents:
diff changeset
40 // These shouldn't warn due to the use of the preprocessor.
anatofuz
parents:
diff changeset
41 a OP a;
anatofuz
parents:
diff changeset
42 LHS = a;
anatofuz
parents:
diff changeset
43 a = RHS;
anatofuz
parents:
diff changeset
44 LHS OP RHS;
anatofuz
parents:
diff changeset
45 #undef OP
anatofuz
parents:
diff changeset
46 #undef LHS
anatofuz
parents:
diff changeset
47 #undef RHS
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 // A way to silence the warning.
anatofuz
parents:
diff changeset
50 a = (int &)a;
anatofuz
parents:
diff changeset
51 }
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 // Do not diagnose self-assigment in an unevaluated context
anatofuz
parents:
diff changeset
54 void false_positives_unevaluated_ctx() noexcept(noexcept(a = a)) // expected-warning {{expression with side effects has no effect in an unevaluated context}}
anatofuz
parents:
diff changeset
55 {
anatofuz
parents:
diff changeset
56 decltype(a = a) b = a; // expected-warning {{expression with side effects has no effect in an unevaluated context}}
anatofuz
parents:
diff changeset
57 static_assert(noexcept(a = a), ""); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
anatofuz
parents:
diff changeset
58 static_assert(sizeof(a = a), ""); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 volatile int vol_a;
anatofuz
parents:
diff changeset
62 void vol_test() {
anatofuz
parents:
diff changeset
63 // Volatile stores aren't side-effect free.
anatofuz
parents:
diff changeset
64 vol_a = vol_a;
anatofuz
parents:
diff changeset
65 volatile int &vol_a_ref = vol_a;
anatofuz
parents:
diff changeset
66 vol_a_ref = vol_a_ref;
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68 };
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 // Dummy type.
anatofuz
parents:
diff changeset
71 struct Dummy {};
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 template <typename T>
anatofuz
parents:
diff changeset
74 struct TemplateClass {
anatofuz
parents:
diff changeset
75 T var;
anatofuz
parents:
diff changeset
76 void f() {
anatofuz
parents:
diff changeset
77 var = var; // expected-warning {{assigning field to itself}}
anatofuz
parents:
diff changeset
78 }
anatofuz
parents:
diff changeset
79 };
anatofuz
parents:
diff changeset
80 void instantiate() {
anatofuz
parents:
diff changeset
81 {
anatofuz
parents:
diff changeset
82 TemplateClass<int> c;
anatofuz
parents:
diff changeset
83 c.f();
anatofuz
parents:
diff changeset
84 }
anatofuz
parents:
diff changeset
85 {
anatofuz
parents:
diff changeset
86 TemplateClass<Dummy> c;
anatofuz
parents:
diff changeset
87 c.f();
anatofuz
parents:
diff changeset
88 }
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 // It may make sense not to warn on the rest of the tests.
anatofuz
parents:
diff changeset
92 // It may be a valid use-case to self-assign to tell the compiler that
anatofuz
parents:
diff changeset
93 // it is ok to vectorize the store.
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 void f0(C *s, C *t) {
anatofuz
parents:
diff changeset
96 s->a = s->a;
anatofuz
parents:
diff changeset
97 t->a = s->a;
anatofuz
parents:
diff changeset
98 }
anatofuz
parents:
diff changeset
99
anatofuz
parents:
diff changeset
100 void f1(C &s, C &t) {
anatofuz
parents:
diff changeset
101 s.a = s.a;
anatofuz
parents:
diff changeset
102 t.a = s.a;
anatofuz
parents:
diff changeset
103 }
anatofuz
parents:
diff changeset
104
anatofuz
parents:
diff changeset
105 struct T {
anatofuz
parents:
diff changeset
106 C *s;
anatofuz
parents:
diff changeset
107 };
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 void f2(T *t, T *t2) {
anatofuz
parents:
diff changeset
110 t->s->a = t->s->a;
anatofuz
parents:
diff changeset
111 t2->s->a = t->s->a;
anatofuz
parents:
diff changeset
112 }
anatofuz
parents:
diff changeset
113
anatofuz
parents:
diff changeset
114 void f3(T &t, T &t2) {
anatofuz
parents:
diff changeset
115 t.s->a = t.s->a;
anatofuz
parents:
diff changeset
116 t2.s->a = t.s->a;
anatofuz
parents:
diff changeset
117 }