xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-self-assign.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wself-assign -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc void f() {
4*f4a2713aSLionel Sambuc   int a = 42, b = 42;
5*f4a2713aSLionel Sambuc   a = a; // expected-warning{{explicitly assigning}}
6*f4a2713aSLionel Sambuc   b = b; // expected-warning{{explicitly assigning}}
7*f4a2713aSLionel Sambuc   a = b;
8*f4a2713aSLionel Sambuc   b = a = b;
9*f4a2713aSLionel Sambuc   a = a = a; // expected-warning{{explicitly assigning}}
10*f4a2713aSLionel Sambuc   a = b = b = a;
11*f4a2713aSLionel Sambuc }
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc // Dummy type.
14*f4a2713aSLionel Sambuc struct S {};
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc void false_positives() {
17*f4a2713aSLionel Sambuc #define OP =
18*f4a2713aSLionel Sambuc #define LHS a
19*f4a2713aSLionel Sambuc #define RHS a
20*f4a2713aSLionel Sambuc   int a = 42;
21*f4a2713aSLionel Sambuc   // These shouldn't warn due to the use of the preprocessor.
22*f4a2713aSLionel Sambuc   a OP a;
23*f4a2713aSLionel Sambuc   LHS = a;
24*f4a2713aSLionel Sambuc   a = RHS;
25*f4a2713aSLionel Sambuc   LHS OP RHS;
26*f4a2713aSLionel Sambuc #undef OP
27*f4a2713aSLionel Sambuc #undef LHS
28*f4a2713aSLionel Sambuc #undef RHS
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc   S s;
31*f4a2713aSLionel Sambuc   s = s; // Not a builtin assignment operator, no warning.
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc   // Volatile stores aren't side-effect free.
34*f4a2713aSLionel Sambuc   volatile int vol_a;
35*f4a2713aSLionel Sambuc   vol_a = vol_a;
36*f4a2713aSLionel Sambuc   volatile int &vol_a_ref = vol_a;
37*f4a2713aSLionel Sambuc   vol_a_ref = vol_a_ref;
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc template <typename T> void g() {
41*f4a2713aSLionel Sambuc   T a;
42*f4a2713aSLionel Sambuc   a = a; // May or may not be a builtin assignment operator, no warning.
43*f4a2713aSLionel Sambuc }
44*f4a2713aSLionel Sambuc void instantiate() {
45*f4a2713aSLionel Sambuc   g<int>();
46*f4a2713aSLionel Sambuc   g<S>();
47*f4a2713aSLionel Sambuc }
48