summaryrefslogtreecommitdiffstats
path: root/clang/test/Analysis/use-after-move.cpp
blob: 652f08f45cb36fafb9be2b58ad404cf89c875bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.Move -verify %s\
// RUN:  -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\
// RUN:  -analyzer-config exploration_strategy=unexplored_first_queue\
// RUN:  -analyzer-checker debug.ExprInspection
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.Move -verify %s\
// RUN:  -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\
// RUN:  -analyzer-config exploration_strategy=dfs -DDFS=1\
// RUN:  -analyzer-checker debug.ExprInspection
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.Move -verify %s\
// RUN:  -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\
// RUN:  -analyzer-config exploration_strategy=unexplored_first_queue\
// RUN:  -analyzer-config alpha.cplusplus.Move:WarnOn=KnownsOnly -DPEACEFUL\
// RUN:  -analyzer-checker debug.ExprInspection
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.Move -verify %s\
// RUN:  -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\
// RUN:  -analyzer-config exploration_strategy=dfs -DDFS=1\
// RUN:  -analyzer-config alpha.cplusplus.Move:WarnOn=KnownsOnly -DPEACEFUL\
// RUN:  -analyzer-checker debug.ExprInspection
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.Move -verify %s\
// RUN:  -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\
// RUN:  -analyzer-config exploration_strategy=unexplored_first_queue\
// RUN:  -analyzer-config alpha.cplusplus.Move:WarnOn=All -DAGGRESSIVE\
// RUN:  -analyzer-checker debug.ExprInspection
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.Move -verify %s\
// RUN:  -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\
// RUN:  -analyzer-config exploration_strategy=dfs -DDFS=1\
// RUN:  -analyzer-config alpha.cplusplus.Move:WarnOn=All -DAGGRESSIVE\
// RUN:  -analyzer-checker debug.ExprInspection

#include "Inputs/system-header-simulator-cxx.h"

void clang_analyzer_warnIfReached();

class B {
public:
  B() = default;
  B(const B &) = default;
  B(B &&) = default;
  B& operator=(const B &q) = default;
  void operator=(B &&b) {
    return;
  }
  void foo() { return; }
};

class A {
  int i;
  double d;

public:
  B b;
  A(int ii = 42, double dd = 1.0) : d(dd), i(ii), b(B()) {}
  void moveconstruct(A &&other) {
    std::swap(b, other.b);
    std::swap(d, other.d);
    std::swap(i, other.i);
    return;
  }
  static A get() {
    A v(12, 13);
    return v;
  }
  A(A *a) {
    moveconstruct(std::move(*a));
  }
  A(const A &other) : i(other.i), d(other.d), b(other.b) {}
  A(A &&other) : i(other.i), d(other.d), b(std::move(other.b)) {
#ifdef AGGRESSIVE
    // expected-note@-2{{Object 'b' is moved}}
#endif
  }
  A(A &&other, char *k) {
    moveconstruct(std::move(other));
  }
  void operator=(const A &other) {
    i = other.i;
    d = other.d;
    b = other.b;
    return;
  }
  void operator=(A &&other) {
    moveconstruct(std::move(other));
    return;
  }
  int getI() { return i; }
  int foo() const;
  void bar() const;
  void reset();
  void destroy();
  void clear();
  void resize(std::size_t);
  bool empty() const;
  bool isEmpty() const;
  operator bool() const;

  void testUpdateField() {
    A a;
    A b = std::move(a);
    a.i = 1;
    a.foo(); // no-warning
  }
  void testUpdateFieldDouble() {
    A a;
    A b = std::move(a);
    a.d = 1.0;
    a.foo(); // no-warning
  }
};

int bignum();

void moveInsideFunctionCall(A a) {
  A b = std::move(a);
}
void leftRefCall(A &a) {
  a.foo();
}
void rightRefCall(A &&a) {
  a.foo();
}
void constCopyOrMoveCall(const A a) {
  a.foo();
}

void copyOrMoveCall(A a) {
  a.foo();
}

void simpleMoveCtorTest() {
  {
    A a;
    A b = std::move(a);
    a.foo();
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Method called on moved-from object 'a'}}
    // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif
  }
  {
    A a;
    A b = std::move(a);
    b = a;
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Moved-from object 'a' is copied}}
    // expected-note@-4    {{Moved-from object 'a' is copied}}
#endif
  }
  {
    A a;
    A b = std::move(a);
    b = std::move(a);
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Moved-from object 'a' is moved}}
    // expected-note@-4    {{Moved-from object 'a' is moved}}
#endif
  }
}

void simpleMoveAssignementTest() {
  {
    A a;
    A b;
    b = std::move(a);
    a.foo();
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Method called on moved-from object 'a'}}
    // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif
  }
  {
    A a;
    A b;
    b = std::move(a);
    A c(a);
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Moved-from object 'a' is copied}}
    // expected-note@-4    {{Moved-from object 'a' is copied}}
#endif
  }
  {
    A a;
    A b;
    b = std::move(a);
    A c(std::move(a));
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Moved-from object 'a' is moved}}
    // expected-note@-4    {{Moved-from object 'a' is moved}}
#endif
  }
}

void moveInInitListTest() {
  struct S {
    A a;
  };
  A a;
  S s{std::move(a)};
  a.foo();
#ifndef PEACEFUL
  // expected-note@-3 {{Object 'a' is moved}}
  // expected-warning@-3 {{Method called on moved-from object 'a'}}
  // expected-note@-4 {{Method called on moved-from object 'a'}}
#endif
}

// Don't report a bug if the variable was assigned to in the meantime.
void reinitializationTest(int i) {
  {
    A a;
    A b;
    b = std::move(a);
    a = A();
    a.foo();
  }
  {
    A a;
    if (i == 1) {
#ifndef PEACEFUL
      // expected-note@-2 {{Assuming 'i' is not equal to 1}}
      // expected-note@-3 {{Taking false branch}}
      // And the other report:
      // expected-note@-5 {{Assuming 'i' is not equal to 1}}
      // expected-note@-6 {{Taking false branch}}
#endif
      A b;
      b = std::move(a);
      a = A();
    }
    if (i == 2) {
#ifndef PEACEFUL
      // expected-note@-2 {{Assuming 'i' is not equal to 2}}
      // expected-note@-3 {{Taking false branch}}
      // And the other report:
      // expected-note@-5 {{Assuming 'i' is not equal to 2}}
      // expected-note@-6 {{Taking false branch}}
#endif
      a.foo();    // no-warning
    }
  }
  {
    A a;
    if (i == 1) {
#ifndef PEACEFUL
      // expected-note@-2 {{Taking false branch}}
      // expected-note@-3 {{Taking false branch}}
#endif
      std::move(a);
    }
    if (i == 2) {
#ifndef PEACEFUL
      // expected-note@-2 {{Taking false branch}}
      // expected-note@-3 {{Taking false branch}}
#endif
      a = A();
      a.foo();
    }
  }
  // The built-in assignment operator should also be recognized as a
  // reinitialization. (std::move() may be called on built-in types in template
  // code.)
  {
    int a1 = 1, a2 = 2;
    std::swap(a1, a2);
  }
  // A std::move() after the assignment makes the variable invalid again.
  {
    A a;
    A b;
    b = std::move(a);
    a = A();
    b = std::move(a);
    a.foo();
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Method called on moved-from object 'a'}}
    // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif
  }
  // If a path exist where we not reinitialize the variable we report a bug.
  {
    A a;
    A b;
    b = std::move(a);
#ifndef PEACEFUL
    // expected-note@-2 {{Object 'a' is moved}}
#endif
    if (i < 10) {
#ifndef PEACEFUL
      // expected-note@-2 {{Assuming 'i' is >= 10}}
      // expected-note@-3 {{Taking false branch}}
#endif
      a = A();
    }
    if (i > 5) {
      a.foo();
#ifndef PEACEFUL
      // expected-note@-3 {{Taking true branch}}
      // expected-warning@-3 {{Method called on moved-from object 'a'}}
      // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif
    }
  }
}

// Using decltype on an expression is not a use.
void decltypeIsNotUseTest() {
  A a;
  // A b(std::move(a));
  decltype(a) other_a; // no-warning
}

void loopTest() {
  {
    A a;
    for (int i = 0; i < bignum(); i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      rightRefCall(std::move(a));        // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < 2; i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is true.  Entering loop body}}
      // expected-note@-3 {{Loop condition is true.  Entering loop body}}
      // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      rightRefCall(std::move(a)); // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < bignum(); i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      leftRefCall(a);                    // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < 2; i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is true.  Entering loop body}}
      // expected-note@-3 {{Loop condition is true.  Entering loop body}}
      // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      leftRefCall(a);             // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < bignum(); i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      constCopyOrMoveCall(a);            // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < 2; i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is true.  Entering loop body}}
      // expected-note@-3 {{Loop condition is true.  Entering loop body}}
      // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      constCopyOrMoveCall(a);     // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < bignum(); i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      moveInsideFunctionCall(a);         // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < 2; i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is true.  Entering loop body}}
      // expected-note@-3 {{Loop condition is true.  Entering loop body}}
      // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      moveInsideFunctionCall(a);  // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < bignum(); i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      copyOrMoveCall(a);                 // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < 2; i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is true.  Entering loop body}}
      // expected-note@-3 {{Loop condition is true.  Entering loop body}}
      // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}}
#endif
      copyOrMoveCall(a);          // no-warning
    }
  }
  {
    A a;
    for (int i = 0; i < bignum(); i++) {
#ifndef PEACEFUL
      // expected-note@-2 {{Loop condition is true.  Entering loop body}}
      // expected-note@-3 {{Loop condition is true.  Entering loop body}}
#endif
      constCopyOrMoveCall(std::move(a));
#ifndef PEACEFUL
      // expected-note@-2    {{Object 'a' is moved}}
      // expected-warning@-3 {{Moved-from object 'a' is moved}}
      // expected-note@-4 {{Moved-from object 'a' is moved}}
#endif
    }
  }

  // Don't warn if we return after the move.
  {
    A a;
    for (int i = 0; i < 3; ++i) {
      a.bar();
      if (a.foo() > 0) {
        A b;
        b = std::move(a); // no-warning
        return;
      }
    }
  }
}

// Report a usage of a moved-from object only at the first use.
void uniqueTest(bool cond) {
  A a(42, 42.0);
  A b;
  b = std::move(a);

  if (cond) {
    a.foo();
#ifndef PEACEFUL
  // expected-note@-5 {{Object 'a' is moved}}
  // expected-note@-4 {{Assuming 'cond' is not equal to 0}}
  // expected-note@-5 {{Taking true branch}}
  // expected-warning@-5 {{Method called on moved-from object 'a'}}
  // expected-note@-6    {{Method called on moved-from object 'a'}}
#endif
  }
  if (cond) {
    a.bar(); // no-warning
  }

  a.bar(); // no-warning
}

void uniqueTest2() {
  A a;
  A a1 = std::move(a);
  a.foo();
#ifndef PEACEFUL
  // expected-note@-3 {{Object 'a' is moved}}
  // expected-warning@-3 {{Method called on moved-from object 'a'}}
  // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif

  A a2 = std::move(a); // no-warning
  a.foo();             // no-warning
}

// There are exceptions where we assume in general that the method works fine
//even on moved-from objects.
void moveSafeFunctionsTest() {
  A a;
  A b = std::move(a);
#ifndef PEACEFUL
  // expected-note@-2 {{Object 'a' is moved}}
#endif
  a.empty();          // no-warning
  a.isEmpty();        // no-warning
  (void)a;            // no-warning
  (bool)a;            // expected-warning {{expression result unused}}
  a.foo();
#ifndef PEACEFUL
  // expected-warning@-2 {{Method called on moved-from object 'a'}}
  // expected-note@-3    {{Method called on moved-from object 'a'}}
#endif
}

void moveStateResetFunctionsTest() {
  {
    A a;
    A b = std::move(a);
    a.reset(); // no-warning
    a.foo();   // no-warning
    // Test if resets the state of subregions as well.
    a.b.foo(); // no-warning
  }
  {
    A a;
    A b = std::move(a);
    a.destroy(); // no-warning
    a.foo();     // no-warning
  }
  {
    A a;
    A b = std::move(a);
    a.clear(); // no-warning
    a.foo();   // no-warning
    a.b.foo(); // no-warning
  }
  {
    A a;
    A b = std::move(a);
    a.resize(0); // no-warning
    a.foo();   // no-warning
    a.b.foo(); // no-warning
  }
}

// Moves or uses that occur as part of template arguments.
template <int>
class ClassTemplate {
public:
  void foo(A a);
};

template <int>
void functionTemplate(A a);

void templateArgIsNotUseTest() {
  {
    // A pattern like this occurs in the EXPECT_EQ and ASSERT_EQ macros in
    // Google Test.
    A a;
    ClassTemplate<sizeof(A(std::move(a)))>().foo(std::move(a)); // no-warning
  }
  {
    A a;
    functionTemplate<sizeof(A(std::move(a)))>(std::move(a)); // no-warning
  }
}

// Moves of global variables are not reported.
A global_a;
void globalVariablesTest() {
  std::move(global_a);
  global_a.foo(); // no-warning
}

// Moves of member variables.
class memberVariablesTest {
  A a;
  static A static_a;

  void f() {
    A b;
    b = std::move(a);
    a.foo();
#ifdef AGGRESSIVE
    // expected-note@-3{{Object 'a' is moved}}
    // expected-warning@-3 {{Method called on moved-from object 'a'}}
    // expected-note@-4{{Method called on moved-from object 'a'}}
#endif

    b = std::move(static_a);
    static_a.foo();
#ifdef AGGRESSIVE
    // expected-note@-3{{Object 'static_a' is moved}}
    // expected-warning@-3{{Method called on moved-from object 'static_a'}}
    // expected-note@-4{{Method called on moved-from object 'static_a'}}
#endif
  }
};

void PtrAndArrayTest() {
  A *Ptr = new A(1, 1.5);
  A Arr[10];
  Arr[2] = std::move(*Ptr);
  (*Ptr).foo();
#ifdef AGGRESSIVE
  // expected-note@-3{{Object is moved}}
  // expected-warning@-3{{Method called on moved-from object}}
  // expected-note@-4{{Method called on moved-from object}}
#endif

  Ptr = &Arr[1];
  Arr[3] = std::move(Arr[1]);
  Ptr->foo();
#ifdef AGGRESSIVE
  // expected-note@-3{{Object is moved}}
  // expected-warning@-3{{Method called on moved-from object}}
  // expected-note@-4{{Method called on moved-from object}}
#endif

  Arr[3] = std::move(Arr[2]);
  Arr[2].foo();
#ifdef AGGRESSIVE
  // expected-note@-3{{Object is moved}}
  // expected-warning@-3{{Method called on moved-from object}}
  // expected-note@-4{{Method called on moved-from object}}
#endif

  Arr[2] = std::move(Arr[3]); // reinitialization
  Arr[2].foo();               // no-warning
}

void exclusiveConditionsTest(bool cond) {
  A a;
  if (cond) {
    A b;
    b = std::move(a);
  }
  if (!cond) {
    a.bar(); // no-warning
  }
}

void differentBranchesTest(int i) {
  // Don't warn if the use is in a different branch from the move.
  {
    A a;
    if (i > 0) {
#ifndef PEACEFUL
    // expected-note@-2 {{Assuming 'i' is > 0}}
    // expected-note@-3 {{Taking true branch}}
#endif
      A b;
      b = std::move(a);
    } else {
      a.foo(); // no-warning
    }
  }
  // Same thing, but with a ternary operator.
  {
    A a, b;
    i > 0 ? (void)(b = std::move(a)) : a.bar(); // no-warning
#ifndef PEACEFUL
    // expected-note@-2 {{'?' condition is true}}
#endif
  }
  // A variation on the theme above.
  {
    A a;
    a.foo() > 0 ? a.foo() : A(std::move(a)).foo();
#ifdef DFS
  #ifndef PEACEFUL
    // expected-note@-3 {{Assuming the condition is false}}
    // expected-note@-4 {{'?' condition is false}}
  #endif
#else
  #ifndef PEACEFUL
    // expected-note@-8 {{Assuming the condition is true}}
    // expected-note@-9 {{'?' condition is true}}
  #endif
#endif
  }
  // Same thing, but with a switch statement.
  {
    A a, b;
    switch (i) {
#ifndef PEACEFUL
    // expected-note@-2 {{Control jumps to 'case 1:'}}
#endif
    case 1:
      b = std::move(a); // no-warning
      break;
#ifndef PEACEFUL
      // expected-note@-2 {{Execution jumps to the end of the function}}
#endif
    case 2:
      a.foo(); // no-warning
      break;
    }
  }
  // However, if there's a fallthrough, we do warn.
  {
    A a, b;
    switch (i) {
#ifndef PEACEFUL
    // expected-note@-2 {{Control jumps to 'case 1:'}}
#endif
    case 1:
      b = std::move(a);
#ifndef PEACEFUL
      // expected-note@-2 {{Object 'a' is moved}}
#endif
    case 2:
      a.foo();
#ifndef PEACEFUL
      // expected-warning@-2 {{Method called on moved-from object}}
      // expected-note@-3    {{Method called on moved-from object 'a'}}
#endif
      break;
    }
  }
}

void tempTest() {
  A a = A::get();
  A::get().foo(); // no-warning
  for (int i = 0; i < bignum(); i++) {
    A::get().foo(); // no-warning
  }
}

void interFunTest1(A &a) {
  a.bar();
#ifndef PEACEFUL
  // expected-warning@-2 {{Method called on moved-from object 'a'}}
  // expected-note@-3    {{Method called on moved-from object 'a'}}
#endif
}

void interFunTest2() {
  A a;
  A b;
  b = std::move(a);
  interFunTest1(a);
#ifndef PEACEFUL
  // expected-note@-3 {{Object 'a' is moved}}
  // expected-note@-3 {{Calling 'interFunTest1'}}
#endif
}

void foobar(A a, int i);
void foobar(int i, A a);

void paramEvaluateOrderTest() {
  A a;
  foobar(std::move(a), a.getI());
#ifndef PEACEFUL
  // expected-note@-2 {{Object 'a' is moved}}
  // expected-warning@-3 {{Method called on moved-from object 'a'}}
  // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif

  //FALSE NEGATIVE since parameters evaluate order is undefined
  foobar(a.getI(), std::move(a)); //no-warning
}

void not_known_pass_by_ref(A &a);
void not_known_pass_by_const_ref(const A &a);
void not_known_pass_by_rvalue_ref(A &&a);
void not_known_pass_by_ptr(A *a);
void not_known_pass_by_const_ptr(const A *a);

void regionAndPointerEscapeTest() {
  {
    A a;
    A b;
    b = std::move(a);
    not_known_pass_by_ref(a);
    a.foo(); // no-warning
  }
  {
    A a;
    A b;
    b = std::move(a);
    not_known_pass_by_const_ref(a);
    a.foo();
#ifndef PEACEFUL
    // expected-note@-4{{Object 'a' is moved}}
    // expected-warning@-3{{Method called on moved-from object 'a'}}
    // expected-note@-4   {{Method called on moved-from object 'a'}}
#endif
  }
  {
    A a;
    A b;
    b = std::move(a);
    not_known_pass_by_rvalue_ref(std::move(a));
    a.foo(); // no-warning
  }
  {
    A a;
    A b;
    b = std::move(a);
    not_known_pass_by_ptr(&a);
    a.foo(); // no-warning
  }
  {
    A a;
    A b;
    b = std::move(a);
    not_known_pass_by_const_ptr(&a);
    a.foo();
#ifndef PEACEFUL
    // expected-note@-4{{Object 'a' is moved}}
    // expected-warning@-3{{Method called on moved-from object 'a'}}
    // expected-note@-4   {{Method called on moved-from object 'a'}}
#endif
  }
}

// A declaration statement containing multiple declarations sequences the
// initializer expressions.
void declarationSequenceTest() {
  {
    A a;
    A a1 = a, a2 = std::move(a); // no-warning
  }
  {
    A a;
    A a1 = std::move(a), a2 = a;
#ifndef PEACEFUL
    // expected-note@-2 {{Object 'a' is moved}}
    // expected-warning@-3 {{Moved-from object 'a' is copied}}
    // expected-note@-4    {{Moved-from object 'a' is copied}}
#endif
  }
}

// The logical operators && and || sequence their operands.
void logicalOperatorsSequenceTest() {
  {
    A a;
    if (a.foo() > 0 && A(std::move(a)).foo() > 0) {
#ifndef PEACEFUL
      // expected-note@-2 {{Assuming the condition is false}}
      // expected-note@-3 {{Left side of '&&' is false}}
      // expected-note@-4 {{Taking false branch}}
      // And the other report:
      // expected-note@-6 {{Assuming the condition is false}}
      // expected-note@-7 {{Left side of '&&' is false}}
      // expected-note@-8 {{Taking false branch}}
      A().bar();
#endif
    }
  }
  // A variation: Negate the result of the && (which pushes the && further down
  // into the AST).
  {
    A a;
    if (!(a.foo() > 0 && A(std::move(a)).foo() > 0)) {
#ifndef PEACEFUL
      // expected-note@-2 {{Assuming the condition is false}}
      // expected-note@-3 {{Left side of '&&' is false}}
      // expected-note@-4 {{Taking true branch}}
      // And the other report:
      // expected-note@-6 {{Assuming the condition is false}}
      // expected-note@-7 {{Left side of '&&' is false}}
      // expected-note@-8 {{Taking true branch}}
#endif
      A().bar();
    }
  }
  {
    A a;
    if (A(std::move(a)).foo() > 0 && a.foo() > 0) {
#ifndef PEACEFUL
      // expected-note@-2 {{Object 'a' is moved}}
      // expected-note@-3 {{Assuming the condition is true}}
      // expected-note@-4 {{Left side of '&&' is true}}
      // expected-warning@-5 {{Method called on moved-from object 'a'}}
      // expected-note@-6    {{Method called on moved-from object 'a'}}
      // And the other report:
      // expected-note@-8 {{Assuming the condition is false}}
      // expected-note@-9 {{Left side of '&&' is false}}
      // expected-note@-10{{Taking false branch}}
#endif
      A().bar();
    }
  }
  {
    A a;
    if (a.foo() > 0 || A(std::move(a)).foo() > 0) {
#ifndef PEACEFUL
      // expected-note@-2 {{Assuming the condition is true}}
      // expected-note@-3 {{Left side of '||' is true}}
      // expected-note@-4 {{Taking true branch}}
#endif
      A().bar();
    }
  }
  {
    A a;
    if (A(std::move(a)).foo() > 0 || a.foo() > 0) {
#ifndef PEACEFUL
      // expected-note@-2 {{Object 'a' is moved}}
      // expected-note@-3 {{Assuming the condition is false}}
      // expected-note@-4 {{Left side of '||' is false}}
      // expected-warning@-5 {{Method called on moved-from object 'a'}}
      // expected-note@-6    {{Method called on moved-from object 'a'}}
#endif
      A().bar();
    }
  }
}

// A range-based for sequences the loop variable declaration before the body.
void forRangeSequencesTest() {
  A v[2] = {A(), A()};
  for (A &a : v) {
    A b;
    b = std::move(a); // no-warning
  }
}

// If a variable is declared in an if statement, the declaration of the variable
// (which is treated like a reinitialization by the check) is sequenced before
// the evaluation of the condition (which constitutes a use).
void ifStmtSequencesDeclAndConditionTest() {
  for (int i = 0; i < 3; ++i) {
    if (A a = A()) {
      A b;
      b = std::move(a); // no-warning
    }
  }
}

struct C : public A {
  [[clang::reinitializes]] void reinit();
};

void subRegionMoveTest() {
  {
    A a;
    B b = std::move(a.b);
    a.b.foo();
#ifdef AGGRESSIVE
    // expected-note@-3{{Object 'b' is moved}}
    // expected-warning@-3{{Method called on moved-from object 'b'}}
    // expected-note@-4 {{Method called on moved-from object 'b'}}
#endif
  }
  {
    A a;
    A a1 = std::move(a);
    a.b.foo();
#ifdef AGGRESSIVE
    // expected-note@-3{{Calling move constructor for 'A'}}
    // expected-note@-4{{Returning from move constructor for 'A'}}
    // expected-warning@-4{{Method called on moved-from object 'b'}}
    // expected-note@-5{{Method called on moved-from object 'b'}}
#endif
  }
  // Don't report a misuse if any SuperRegion is already reported.
  {
    A a;
    A a1 = std::move(a);
    a.foo();
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'a' is moved}}
    // expected-warning@-3 {{Method called on moved-from object 'a'}}
    // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif
    a.b.foo();           // no-warning
  }
  {
    C c;
    C c1 = std::move(c);
    c.foo();
#ifndef PEACEFUL
    // expected-note@-3 {{Object 'c' is moved}}
    // expected-warning@-3 {{Method called on moved-from object 'c'}}
    // expected-note@-4    {{Method called on moved-from object 'c'}}
#endif
    c.b.foo();           // no-warning
  }
}

void resetSuperClass() {
  C c;
  C c1 = std::move(c);
  c.clear();
  C c2 = c; // no-warning
}

void resetSuperClass2() {
  C c;
  C c1 = std::move(c);
  c.reinit();
  C c2 = c; // no-warning
}

void reportSuperClass() {
  C c;
  C c1 = std::move(c);
  c.foo();
#ifndef PEACEFUL
  // expected-note@-3 {{Object 'c' is moved}}
  // expected-warning@-3 {{Method called on moved-from object 'c'}}
  // expected-note@-4    {{Method called on moved-from object 'c'}}
#endif
  C c2 = c;            // no-warning
}

struct Empty {};

Empty inlinedCall() {
  // Used to warn because region 'e' failed to be cleaned up because no symbols
  // have ever died during the analysis and the checkDeadSymbols callback
  // was skipped entirely.
  Empty e{};
  return e; // no-warning
}

void checkInlinedCallZombies() {
  while (true)
    inlinedCall();
}

void checkLoopZombies() {
  while (true) {
    Empty e{};
    Empty f = std::move(e); // no-warning
  }
}

void checkMoreLoopZombies1(bool flag) {
  while (flag) {
    Empty e{};
    if (true)
      e; // expected-warning {{expression result unused}}
    Empty f = std::move(e); // no-warning
  }
}

bool coin();

void checkMoreLoopZombies2(bool flag) {
  while (flag) {
    Empty e{};
    while (coin())
      e; // expected-warning {{expression result unused}}
    Empty f = std::move(e); // no-warning
  }
}

void checkMoreLoopZombies3(bool flag) {
  while (flag) {
    Empty e{};
    do
      e; // expected-warning {{expression result unused}}
    while (coin());
    Empty f = std::move(e); // no-warning
  }
}

void checkMoreLoopZombies4(bool flag) {
  while (flag) {
    Empty e{};
    for (; coin();)
      e; // expected-warning {{expression result unused}}
    Empty f = std::move(e); // no-warning
  }
}

struct MoveOnlyWithDestructor {
  MoveOnlyWithDestructor();
  ~MoveOnlyWithDestructor();
  MoveOnlyWithDestructor(const MoveOnlyWithDestructor &m) = delete;
  MoveOnlyWithDestructor(MoveOnlyWithDestructor &&m);
};

MoveOnlyWithDestructor foo() {
  MoveOnlyWithDestructor m;
  return m;
}

class HasSTLField {
  std::vector<int> V;
  void testVector() {
    // Warn even in non-aggressive mode when it comes to STL, because
    // in STL the object is left in "valid but unspecified state" after move.
    std::vector<int> W = std::move(V); // expected-note{{Object 'V' of type 'std::vector' is left in a valid but unspecified state after move}}
    V.push_back(123); // expected-warning{{Method called on moved-from object 'V'}}
                      // expected-note@-1{{Method called on moved-from object 'V'}}
  }

  std::unique_ptr<int> P;
  void testUniquePtr() {
    // unique_ptr remains in a well-defined state after move.
    std::unique_ptr<int> Q = std::move(P);
    P.get();
#ifdef AGGRESSIVE
    // expected-warning@-2{{Method called on moved-from object 'P'}}
    // expected-note@-4{{Object 'P' is moved}}
    // expected-note@-4{{Method called on moved-from object 'P'}}
#endif

    // Because that well-defined state is null, dereference is still UB.
    // Note that in aggressive mode we already warned about 'P',
    // so no extra warning is generated.
    *P += 1;
#ifndef AGGRESSIVE
    // expected-warning@-2{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}}
    // expected-note@-14{{Smart pointer 'P' of type 'std::unique_ptr' is reset to null when moved from}}
    // expected-note@-4{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}}
#endif

    // The program should have crashed by now.
    clang_analyzer_warnIfReached(); // no-warning
  }
};

void localRValueMove(A &&a) {
  A b = std::move(a);
  a.foo();
#ifndef PEACEFUL
  // expected-note@-3 {{Object 'a' is moved}}
  // expected-warning@-3 {{Method called on moved-from object 'a'}}
  // expected-note@-4    {{Method called on moved-from object 'a'}}
#endif
}

void localUniquePtr(std::unique_ptr<int> P) {
  // Even though unique_ptr is safe to use after move,
  // reusing a local variable this way usually indicates a bug.
  std::unique_ptr<int> Q = std::move(P);
  P.get();
#ifndef PEACEFUL
  // expected-note@-3 {{Object 'P' is moved}}
  // expected-warning@-3 {{Method called on moved-from object 'P'}}
  // expected-note@-4    {{Method called on moved-from object 'P'}}
#endif
}

void localUniquePtrWithArrow(std::unique_ptr<A> P) {
  std::unique_ptr<A> Q = std::move(P); // expected-note{{Smart pointer 'P' of type 'std::unique_ptr' is reset to null when moved from}}
  P->foo(); // expected-warning{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}}
            // expected-note@-1{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}}
}
OpenPOWER on IntegriCloud