aboutsummaryrefslogtreecommitdiff
path: root/clang/test/SemaObjC/warn-called-once.m
blob: f23e41e00ee29824d8949721e100c0562aa7f5d1 (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
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
// RUN: %clang_cc1 -verify -fsyntax-only -fblocks -fobjc-exceptions -Wcompletion-handler -Wno-pointer-to-int-cast %s

#define NULL (void *)0
#define nil (id)0
#define CALLED_ONCE __attribute__((called_once))
#define NORETURN __attribute__((noreturn))
#define LIKELY(X) __builtin_expect(!!(X), 1)
#define UNLIKELY(X) __builtin_expect(!!(X), 0)
#define LIKELY_WITH_PROBA(X, P) __builtin_expect_with_probability(!!(X), 1, P)
#define UNLIKELY_WITH_PROBA(X, P) __builtin_expect_with_probability(!!(X), 0, P)
#define UNPRED(X) __builtin_unpredictable((long)(X))

@protocol NSObject
@end
@interface NSObject <NSObject>
- (instancetype)init;
- (id)copy;
- (id)class;
- autorelease;
@end

typedef unsigned int NSUInteger;
typedef struct {
} NSFastEnumerationState;

@interface NSArray <__covariant NSFastEnumeration>
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
@end
@interface NSMutableArray<ObjectType> : NSArray <ObjectType>
- addObject:anObject;
@end
@class NSString, Protocol;
extern void NSLog(NSString *format, ...);

typedef int group_t;
typedef struct dispatch_queue_s *dispatch_queue_t;
typedef void (^dispatch_block_t)(void);
extern dispatch_queue_t queue;

void dispatch_group_async(dispatch_queue_t queue,
                          group_t group,
                          dispatch_block_t block);
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

void escape(void (^callback)(void));
void escape_void(void *);
void indirect_call(void (^callback)(void) CALLED_ONCE);
void indirect_conv(void (^completionHandler)(void));
void filler(void);
void exit(int) NORETURN;

void double_call_one_block(void (^callback)(void) CALLED_ONCE) {
  callback(); // expected-note{{previous call is here}}
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void double_call_one_block_parens(void (^callback)(void) CALLED_ONCE) {
  (callback)(); // expected-note{{previous call is here}}
  (callback)(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void double_call_one_block_ptr(void (*callback)(void) CALLED_ONCE) {
  callback(); // expected-note{{previous call is here}}
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void double_call_one_block_ptr_deref(void (*callback)(void) CALLED_ONCE) {
  (*callback)(); // expected-note{{previous call is here}}
  (*callback)(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void multiple_call_one_block(void (^callback)(void) CALLED_ONCE) {
  // We don't really need to repeat the same warning for the same parameter.
  callback(); // no-warning
  callback(); // no-warning
  callback(); // no-warning
  callback(); // expected-note{{previous call is here}}
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void double_call_branching_1(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    callback(); // expected-note{{previous call is here}}
  } else {
    cond += 42;
  }
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void double_call_branching_2(int cond, void (^callback)(void) CALLED_ONCE) {
  callback();
  // expected-note@-1{{previous call is here; set to nil to indicate it cannot be called afterwards}}

  if (cond) {
    callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
  } else {
    cond += 42;
  }
}

void double_call_branching_3(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    callback();
  } else {
    callback();
  }
  // no-warning
}

void double_call_branching_4(int cond1, int cond2, void (^callback)(void) CALLED_ONCE) {
  if (cond1) {
    cond2 = !cond2;
  } else {
    callback();
    // expected-note@-1{{previous call is here; set to nil to indicate it cannot be called afterwards}}
  }

  if (cond2) {
    callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
  }
}

void double_call_loop(int counter, void (^callback)(void) CALLED_ONCE) {
  while (counter > 0) {
    counter--;
    // Both note and warning are on the same line, which is a common situation
    // in loops.
    callback(); // expected-note{{previous call is here}}
    // expected-warning@-1{{'callback' parameter marked 'called_once' is called twice}}
  }
}

void never_called_trivial(void (^callback)(void) CALLED_ONCE) {
  // expected-warning@-1{{'callback' parameter marked 'called_once' is never called}}
}

int never_called_branching(int x, void (^callback)(void) CALLED_ONCE) {
  // expected-warning@-1{{'callback' parameter marked 'called_once' is never called}}
  x -= 42;

  if (x == 10) {
    return 0;
  }

  return x + 15;
}

void escaped_one_block_1(void (^callback)(void) CALLED_ONCE) {
  escape(callback); // no-warning
}

void escaped_one_block_2(void (^callback)(void) CALLED_ONCE) {
  escape(callback); // no-warning
  callback();
}

void escaped_one_path_1(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    escape(callback); // no-warning
  } else {
    callback();
  }
}

void escaped_one_path_2(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    escape(callback); // no-warning
  }

  callback();
}

void escaped_one_path_3(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    // expected-warning@-1{{'callback' parameter marked 'called_once' is never used when taking false branch}}
    escape(callback);
  }
}

void escape_in_between_1(void (^callback)(void) CALLED_ONCE) {
  callback(); // expected-note{{previous call is here}}
  escape(callback);
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void escape_in_between_2(int cond, void (^callback)(void) CALLED_ONCE) {
  callback(); // expected-note{{previous call is here}}
  if (cond) {
    escape(callback);
  }
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void escape_in_between_3(int cond, void (^callback)(void) CALLED_ONCE) {
  callback(); // expected-note{{previous call is here}}

  if (cond) {
    escape(callback);
  } else {
    escape_void((__bridge void *)callback);
  }

  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void escaped_as_void_ptr(void (^callback)(void) CALLED_ONCE) {
  escape_void((__bridge void *)callback); // no-warning
}

void indirect_call_no_warning_1(void (^callback)(void) CALLED_ONCE) {
  indirect_call(callback); // no-warning
}

void indirect_call_no_warning_2(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    indirect_call(callback);
  } else {
    callback();
  }
  // no-warning
}

void indirect_call_double_call(void (^callback)(void) CALLED_ONCE) {
  indirect_call(callback); // expected-note{{previous call is here}}
  callback();              // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void indirect_call_within_direct_call(void (^callback)(void) CALLED_ONCE,
                                      void (^meta)(void (^param)(void) CALLED_ONCE) CALLED_ONCE) {
  // TODO: Report warning for 'callback'.
  //       At the moment, it is not possible to access 'called_once' attribute from the type
  //       alone when there is no actual declaration of the marked parameter.
  meta(callback);
  callback();
  // no-warning
}

void block_call_1(void (^callback)(void) CALLED_ONCE) {
  indirect_call( // expected-note{{previous call is here}}
      ^{
        callback();
      });
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

void block_call_2(void (^callback)(void) CALLED_ONCE) {
  escape(^{
    callback();
  });
  callback();
  // no-warning
}

void block_call_3(int cond, void (^callback)(void) CALLED_ONCE) {
  ^{
    if (cond) {
      callback(); // expected-note{{previous call is here}}
    }
    callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
  }();          // no-warning
}

void block_call_4(int cond, void (^callback)(void) CALLED_ONCE) {
  ^{
    if (cond) {
      // expected-warning@-1{{'callback' parameter marked 'called_once' is never used when taking false branch}}
      escape(callback);
    }
  }(); // no-warning
}

void block_call_5(void (^outer)(void) CALLED_ONCE) {
  ^(void (^inner)(void) CALLED_ONCE) {
    // expected-warning@-1{{'inner' parameter marked 'called_once' is never called}}
  }(outer);
}

void block_with_called_once(void (^outer)(void) CALLED_ONCE) {
  escape_void((__bridge void *)^(void (^inner)(void) CALLED_ONCE) {
    inner(); // expected-note{{previous call is here}}
    inner(); // expected-warning{{'inner' parameter marked 'called_once' is called twice}}
  });
  outer(); // expected-note{{previous call is here}}
  outer(); // expected-warning{{'outer' parameter marked 'called_once' is called twice}}
}

void block_dispatch_call(int cond, void (^callback)(void) CALLED_ONCE) {
  dispatch_async(queue, ^{
    if (cond) // expected-warning{{'callback' parameter marked 'called_once' is never called when taking false branch}}
      callback();
  });
}

void block_escape_call_1(int cond, void (^callback)(void) CALLED_ONCE) {
  escape_void((__bridge void *)^{
    if (cond) {
      // no-warning
      callback();
    }
  });
}

void block_escape_call_2(int cond, void (^callback)(void) CALLED_ONCE) {
  escape_void((__bridge void *)^{
    if (cond) {
      callback(); // expected-note{{previous call is here}}
    }
    // Double call can still be reported.
    callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
  });
}

void never_called_one_exit(int cond, void (^callback)(void) CALLED_ONCE) {
  if (!cond) // expected-warning{{'callback' parameter marked 'called_once' is never called when taking true branch}}
    return;

  callback();
}

void never_called_if_then_1(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when taking true branch}}
  } else {
    callback();
  }
}

void never_called_if_then_2(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when taking true branch}}
    // This way the first statement in the basic block is different from
    // the first statement in the compound statement
    (void)cond;
  } else {
    callback();
  }
}

void never_called_if_else_1(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when taking false branch}}
    callback();
  } else {
  }
}

void never_called_if_else_2(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when taking false branch}}
    callback();
  }
}

void never_called_two_ifs(int cond1, int cond2, void (^callback)(void) CALLED_ONCE) {
  if (cond1) {   // expected-warning{{'callback' parameter marked 'called_once' is never called when taking false branch}}
    if (cond2) { // expected-warning{{'callback' parameter marked 'called_once' is never called when taking true branch}}
      return;
    }
    callback();
  }
}

void never_called_ternary_then(int cond, void (^other)(void), void (^callback)(void) CALLED_ONCE) {
  return cond ? // expected-warning{{'callback' parameter marked 'called_once' is never called when taking true branch}}
             other()
              : callback();
}

void never_called_for_false(int size, void (^callback)(void) CALLED_ONCE) {
  for (int i = 0; i < size; ++i) {
    // expected-warning@-1{{'callback' parameter marked 'called_once' is never called when skipping the loop}}
    callback();
    break;
  }
}

void never_called_for_true(int size, void (^callback)(void) CALLED_ONCE) {
  for (int i = 0; i < size; ++i) {
    // expected-warning@-1{{'callback' parameter marked 'called_once' is never called when entering the loop}}
    return;
  }
  callback();
}

void never_called_while_false(int cond, void (^callback)(void) CALLED_ONCE) {
  while (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when skipping the loop}}
    callback();
    break;
  }
}

void never_called_while_true(int cond, void (^callback)(void) CALLED_ONCE) {
  while (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when entering the loop}}
    return;
  }
  callback();
}

void never_called_switch_case(int cond, void (^callback)(void) CALLED_ONCE) {
  switch (cond) {
  case 1:
    callback();
    break;
  case 2:
    callback();
    break;
  case 3: // expected-warning{{'callback' parameter marked 'called_once' is never called when handling this case}}
    break;
  default:
    callback();
    break;
  }
}

void never_called_switch_default(int cond, void (^callback)(void) CALLED_ONCE) {
  switch (cond) {
  case 1:
    callback();
    break;
  case 2:
    callback();
    break;
  default: // expected-warning{{'callback' parameter marked 'called_once' is never called when handling this case}}
    break;
  }
}

void never_called_switch_two_cases(int cond, void (^callback)(void) CALLED_ONCE) {
  switch (cond) {
  case 1: // expected-warning{{'callback' parameter marked 'called_once' is never called when handling this case}}
    break;
  case 2: // expected-warning{{'callback' parameter marked 'called_once' is never called when handling this case}}
    break;
  default:
    callback();
    break;
  }
}

void never_called_switch_none(int cond, void (^callback)(void) CALLED_ONCE) {
  switch (cond) { // expected-warning{{'callback' parameter marked 'called_once' is never called when none of the cases applies}}
  case 1:
    callback();
    break;
  case 2:
    callback();
    break;
  }
}

enum YesNoOrMaybe {
  YES,
  NO,
  MAYBE
};

void exhaustive_switch(enum YesNoOrMaybe cond, void (^callback)(void) CALLED_ONCE) {
  switch (cond) {
  case YES:
    callback();
    break;
  case NO:
    callback();
    break;
  case MAYBE:
    callback();
    break;
  }
  // no-warning
}

void called_twice_exceptions(void (^callback)(void) CALLED_ONCE) {
  // TODO: Obj-C exceptions are not supported in CFG,
  //       we should report warnings in these as well.
  @try {
    callback();
    callback();
  }
  @finally {
    callback();
  }
}

void noreturn_1(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    exit(1);
  } else {
    callback();
  }
  // no-warning
}

void noreturn_2(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    callback();
    exit(1);
  } else {
    callback();
  }
  // no-warning
}

void noreturn_3(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    exit(1);
  }

  callback();
  // no-warning
}

void noreturn_4(void (^callback)(void) CALLED_ONCE) {
  exit(1);
  // no-warning
}

void noreturn_5(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    // NOTE: This is an ambiguous case caused by the fact that we do a backward
    //       analysis.  We can probably report it here, but for the sake of
    //       the simplicity of our analysis, we don't.
    if (cond == 42) {
      callback();
    }
    exit(1);
  }
  callback();
  // no-warning
}

void never_called_noreturn_1(int cond, void (^callback)(void) CALLED_ONCE) {
  // expected-warning@-1{{'callback' parameter marked 'called_once' is never called}}
  if (cond) {
    exit(1);
  }
}

void double_call_noreturn(int cond, void (^callback)(void) CALLED_ONCE) {
  callback(); // expected-note{{previous call is here}}

  if (cond) {
    if (cond == 42) {
      callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
    }
    exit(1);
  }
}

void call_with_check_1(void (^callback)(void) CALLED_ONCE) {
  if (callback)
    callback();
  // no-warning
}

void call_with_check_2(void (^callback)(void) CALLED_ONCE) {
  if (!callback) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_check_3(void (^callback)(void) CALLED_ONCE) {
  if (callback != NULL)
    callback();
  // no-warning
}

void call_with_check_4(void (^callback)(void) CALLED_ONCE) {
  if (NULL != callback)
    callback();
  // no-warning
}

void call_with_check_5(void (^callback)(void) CALLED_ONCE) {
  if (callback == NULL) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_check_6(void (^callback)(void) CALLED_ONCE) {
  if (NULL == callback) {
  } else {
    callback();
  }
  // no-warning
}

int call_with_check_7(int (^callback)(void) CALLED_ONCE) {
  return callback ? callback() : 0;
  // no-warning
}

void call_with_builtin_check_1(int (^callback)(void) CALLED_ONCE) {
  if (LIKELY(callback))
    callback();
  // no-warning
}

void call_with_builtin_check_2(int (^callback)(void) CALLED_ONCE) {
  if (!UNLIKELY(callback)) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_builtin_check_3(int (^callback)(void) CALLED_ONCE) {
  if (__builtin_expect((long)callback, 0L)) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_builtin_check_4(int (^callback)(void) CALLED_ONCE) {
  if (__builtin_expect(0L, (long)callback)) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_builtin_check_5(int (^callback)(void) CALLED_ONCE) {
  if (LIKELY_WITH_PROBA(callback, 0.9))
    callback();
  // no-warning
}

void call_with_builtin_check_6(int (^callback)(void) CALLED_ONCE) {
  if (!UNLIKELY_WITH_PROBA(callback, 0.9)) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_builtin_check_7(int (^callback)(void) CALLED_ONCE) {
  if (UNPRED(callback)) {
  } else {
    callback();
  }
  // no-warning
}

void call_with_builtin_check_8(int (^callback)(void) CALLED_ONCE) {
  if (LIKELY(callback != nil))
    callback();
  // no-warning
}

void call_with_builtin_check_9(int (^callback)(void) CALLED_ONCE) {
  if (!UNLIKELY(callback == NULL))
    callback();
  // no-warning
}

void unreachable_true_branch(void (^callback)(void) CALLED_ONCE) {
  if (0) {

  } else {
    callback();
  }
  // no-warning
}

void unreachable_false_branch(void (^callback)(void) CALLED_ONCE) {
  if (1) {
    callback();
  }
  // no-warning
}

void never_called_conv_1(void (^completionHandler)(void)) {
  // expected-warning@-1{{completion handler is never called}}
}

void never_called_conv_2(void (^completion)(void)) {
  // expected-warning@-1{{completion handler is never called}}
}

void never_called_conv_WithCompletion(void (^callback)(void)) {
  // expected-warning@-1{{completion handler is never called}}
}

void indirectly_called_conv(void (^completionHandler)(void)) {
  indirect_conv(completionHandler);
  // no-warning
}

void escape_through_assignment_1(void (^callback)(void) CALLED_ONCE) {
  id escapee;
  escapee = callback;
  escape(escapee);
  // no-warning
}

void escape_through_assignment_2(void (^callback)(void) CALLED_ONCE) {
  id escapee = callback;
  escape(escapee);
  // no-warning
}

void escape_through_assignment_3(void (^callback1)(void) CALLED_ONCE,
                                 void (^callback2)(void) CALLED_ONCE) {
  id escapee1 = callback1, escapee2 = callback2;
  escape(escapee1);
  escape(escapee2);
  // no-warning
}

void not_called_in_throw_branch_1(id exception, void (^callback)(void) CALLED_ONCE) {
  if (exception) {
    @throw exception;
  }

  callback();
}

void not_called_in_throw_branch_2(id exception, void (^callback)(void) CALLED_ONCE) {
  // expected-warning@-1{{'callback' parameter marked 'called_once' is never called}}
  if (exception) {
    @throw exception;
  }
}

void conventional_error_path_1(int error, void (^completionHandler)(void)) {
  if (error) {
    // expected-warning@-1{{completion handler is never called when taking true branch}}
    // This behavior might be tweaked in the future
    return;
  }

  completionHandler();
}

void conventional_error_path_2(int error, void (^callback)(void) CALLED_ONCE) {
  // Conventions do not apply to explicitly marked parameters.
  if (error) {
    // expected-warning@-1{{'callback' parameter marked 'called_once' is never called when taking true branch}}
    return;
  }

  callback();
}

void suppression_1(void (^callback)(void) CALLED_ONCE) {
  // This is a way to tell the analysis that we know about this path,
  // and we do not want to call the callback here.
  (void)callback; // no-warning
}

void suppression_2(int cond, void (^callback)(void) CALLED_ONCE) {
  if (cond) {
    (void)callback; // no-warning
  } else {
    callback();
  }
}

void suppression_3(int cond, void (^callback)(void) CALLED_ONCE) {
  // Even if we do this on one of the paths, it doesn't mean we should
  // forget about other paths.
  if (cond) {
    // expected-warning@-1{{'callback' parameter marked 'called_once' is never used when taking false branch}}
    (void)callback;
  }
}

@interface TestBase : NSObject
- (void)escape:(void (^)(void))callback;
- (void)indirect_call:(void (^)(void))CALLED_ONCE callback;
- (void)indirect_call_conv_1:(int)cond
           completionHandler:(void (^)(void))completionHandler;
- (void)indirect_call_conv_2:(int)cond
           completionHandler:(void (^)(void))handler;
- (void)indirect_call_conv_3WithCompletion:(void (^)(void))handler;
- (void)indirect_call_conv_4:(void (^)(void))handler
    __attribute__((swift_async(swift_private, 1)));
- (void)exit:(int)code NORETURN;
- (int)condition;
@end

@interface TestClass : TestBase
@property(strong) NSMutableArray *handlers;
@property(strong) id storedHandler;
@property int wasCanceled;
@property(getter=hasErrors) int error;
@end

@implementation TestClass

- (void)double_indirect_call_1:(void (^)(void))CALLED_ONCE callback {
  [self indirect_call:callback]; // expected-note{{previous call is here}}
  [self indirect_call:callback]; // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

- (void)double_indirect_call_2:(void (^)(void))CALLED_ONCE callback {
  [self indirect_call_conv_1:0 // expected-note{{previous call is here}}
           completionHandler:callback];
  [self indirect_call_conv_1:1 // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
           completionHandler:callback];
}

- (void)double_indirect_call_3:(void (^)(void))completionHandler {
  [self indirect_call_conv_2:0 // expected-note{{previous call is here}}
           completionHandler:completionHandler];
  [self indirect_call_conv_2:1 // expected-warning{{completion handler is called twice}}
           completionHandler:completionHandler];
}

- (void)double_indirect_call_4:(void (^)(void))completion {
  [self indirect_call_conv_2:0 // expected-note{{previous call is here}}
           completionHandler:completion];
  [self indirect_call_conv_2:1 // expected-warning{{completion handler is called twice}}
           completionHandler:completion];
}

- (void)double_indirect_call_5:(void (^)(void))withCompletionHandler {
  [self indirect_call_conv_2:0 // expected-note{{previous call is here}}
           completionHandler:withCompletionHandler];
  [self indirect_call_conv_2:1 // expected-warning{{completion handler is called twice}}
           completionHandler:withCompletionHandler];
}

- (void)double_indirect_call_6:(void (^)(void))completionHandler {
  [self indirect_call_conv_3WithCompletion: // expected-note{{previous call is here}}
            completionHandler];
  [self indirect_call_conv_3WithCompletion: // expected-warning{{completion handler is called twice}}
            completionHandler];
}

- (void)double_indirect_call_7:(void (^)(void))completionHandler {
  [self indirect_call_conv_4: // expected-note{{previous call is here}}
            completionHandler];
  [self indirect_call_conv_4: // expected-warning{{completion handler is called twice}}
            completionHandler];
}

- (void)never_called_trivial:(void (^)(void))CALLED_ONCE callback {
  // expected-warning@-1{{'callback' parameter marked 'called_once' is never called}}
  filler();
}

- (void)noreturn:(int)cond callback:(void (^)(void))CALLED_ONCE callback {
  if (cond) {
    [self exit:1];
  }

  callback();
  // no-warning
}

- (void)escaped_one_path:(int)cond callback:(void (^)(void))CALLED_ONCE callback {
  if (cond) {
    [self escape:callback]; // no-warning
  } else {
    callback();
  }
}

- (void)block_call_1:(void (^)(void))CALLED_ONCE callback {
  // We consider captures by blocks as escapes
  [self indirect_call:(^{ // expected-note{{previous call is here}}
          callback();
        })];
  callback(); // expected-warning{{'callback' parameter marked 'called_once' is called twice}}
}

- (void)block_call_2:(int)cond callback:(void (^)(void))CALLED_ONCE callback {
  [self indirect_call:
            ^{
              if (cond) {
                // expected-warning@-1{{'callback' parameter marked 'called_once' is never used when taking false branch}}
                [self escape:callback];
              }
            }];
}

- (void)block_call_3:(int)cond
    completionHandler:(void (^)(void))callback {
  [self indirect_call:
            ^{
              if (cond) {
                // expected-warning@-1{{completion handler is never used when taking false branch}}
                [self escape:callback];
              }
            }];
}

- (void)block_call_4WithCompletion:(void (^)(void))callback {
  [self indirect_call:
            ^{
              if ([self condition]) {
                // expected-warning@-1{{completion handler is never used when taking false branch}}
                [self escape:callback];
              }
            }];
}

- (void)never_called_conv:(void (^)(void))completionHandler {
  // expected-warning@-1{{completion handler is never called}}
  filler();
}

- (void)indirectly_called_conv:(void (^)(void))completionHandler {
  indirect_conv(completionHandler);
  // no-warning
}

- (void)never_called_one_exit_conv:(int)cond completionHandler:(void (^)(void))handler {
  if (!cond) // expected-warning{{completion handler is never called when taking true branch}}
    return;

  handler();
}

- (void)escape_through_assignment:(void (^)(void))completionHandler {
  _storedHandler = completionHandler;
  // no-warning
}

- (void)escape_through_copy:(void (^)(void))completionHandler {
  _storedHandler = [completionHandler copy];
  // no-warning
}

- (void)escape_through_copy_and_autorelease:(void (^)(void))completionHandler {
  _storedHandler = [[completionHandler copy] autorelease];
  // no-warning
}

- (void)complex_escape:(void (^)(void))completionHandler {
  if (completionHandler) {
    [_handlers addObject:[[completionHandler copy] autorelease]];
  }
  // no-warning
}

- (void)test_crash:(void (^)(void))completionHandler cond:(int)cond {
  if (cond) {
    // expected-warning@-1{{completion handler is never used when taking false branch}}
    for (id _ in _handlers) {
    }

    [_handlers addObject:completionHandler];
  }
}

- (void)conventional_error_path_1:(void (^)(void))completionHandler {
  if (self.wasCanceled)
    // expected-warning@-1{{completion handler is never called when taking true branch}}
    // This behavior might be tweaked in the future
    return;

  completionHandler();
}

- (void)conventional_error_path_2:(void (^)(void))completionHandler {
  if (self.wasCanceled)
    // expected-warning@-1{{completion handler is never used when taking true branch}}
    // This behavior might be tweaked in the future
    return;

  [_handlers addObject:completionHandler];
}

- (void)conventional_error_path_3:(void (^)(void))completionHandler {
  if (self.hasErrors)
    // expected-warning@-1{{completion handler is never called when taking true branch}}
    // This behavior might be tweaked in the future
    return;

  completionHandler();
}

- (void)conventional_error_path_3:(int)cond completionHandler:(void (^)(void))handler {
  if (self.wasCanceled)
    // expected-warning@-1{{completion handler is never called when taking true branch}}
    // TODO: When we have an error on some other path, in order not to prevent it from
    //       being reported, we report this one as well.
    //       Probably, we should address this at some point.
    return;

  if (cond) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    handler();
  }
}

#define NSAssert(condition, desc, ...) NSLog(desc, ##__VA_ARGS__);

- (void)empty_base_1:(void (^)(void))completionHandler {
  NSAssert(0, @"Subclass must implement");
  // no-warning
}

- (void)empty_base_2:(void (^)(void))completionHandler {
  // no-warning
}

- (int)empty_base_3:(void (^)(void))completionHandler {
  return 1;
  // no-warning
}

- (int)empty_base_4:(void (^)(void))completionHandler {
  NSAssert(0, @"Subclass must implement");
  return 1;
  // no-warning
}

- (int)empty_base_5:(void (^)(void))completionHandler {
  NSAssert(0, @"%@ doesn't support", [self class]);
  return 1;
  // no-warning
}

#undef NSAssert
#define NSAssert(condition, desc, ...) \
  if (!(condition)) {                  \
    NSLog(desc, ##__VA_ARGS__);        \
  }

- (int)empty_base_6:(void (^)(void))completionHandler {
  NSAssert(0, @"%@ doesn't support", [self class]);
  return 1;
  // no-warning
}

#undef NSAssert
#define NSAssert(condition, desc, ...) \
  do {                                 \
    NSLog(desc, ##__VA_ARGS__);        \
  } while (0)

- (int)empty_base_7:(void (^)(void))completionHandler {
  NSAssert(0, @"%@ doesn't support", [self class]);
  return 1;
  // no-warning
}

- (void)two_conditions_1:(int)first
                  second:(int)second
       completionHandler:(void (^)(void))completionHandler {
  if (first && second) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    completionHandler();
  }
}

- (void)two_conditions_2:(int)first
                  second:(int)second
       completionHandler:(void (^)(void))completionHandler {
  if (first || second) {
    // expected-warning@-1{{completion handler is never called when taking true branch}}
    return;
  }

  completionHandler();
}

- (void)testWithCompletionHandler:(void (^)(void))callback {
  if ([self condition]) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    callback();
  }
}

- (void)testWithCompletion:(void (^)(void))callback {
  if ([self condition]) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    callback();
  }
}

- (void)test:(int)cond fooWithReplyTo:(void (^)(void))handler {
  if (cond) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    handler();
  }
}

- (void)test:(int)cond with:(void (^)(void))fooWithCompletionBlock {
  if (cond) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    fooWithCompletionBlock();
  }
}

- (void)completion_handler_wrong_type:(int (^)(void))completionHandler {
  // We don't want to consider completion handlers with non-void return types.
  if ([self condition]) {
    // no-warning
    completionHandler();
  }
}

- (void)test_swift_async_none:(int)cond
            completionHandler:(void (^)(void))handler __attribute__((swift_async(none))) {
  if (cond) {
    // no-warning
    handler();
  }
}

- (void)test_swift_async_param:(int)cond
                      callback:(void (^)(void))callback
    __attribute__((swift_async(swift_private, 2))) {
  if (cond) {
    // expected-warning@-1{{completion handler is never called when taking false branch}}
    callback();
  }
}

- (void)test_nil_suggestion:(int)cond1
                     second:(int)cond2
                 completion:(void (^)(void))handler {
  if (cond1) {
    handler();
    // expected-note@-1{{previous call is here; set to nil to indicate it cannot be called afterwards}}
  }

  if (cond2) {
    handler(); // expected-warning{{completion handler is called twice}}
  }
}

- (void)test_nil_suppression_1:(int)cond1
                        second:(int)cond2
                    completion:(void (^)(void))handler {
  if (cond1) {
    handler();
    handler = nil;
    // no-warning
  }

  if (cond2) {
    handler();
  }
}

- (void)test_nil_suppression_2:(int)cond1
                        second:(int)cond2
                    completion:(void (^)(void))handler {
  if (cond1) {
    handler();
    handler = NULL;
    // no-warning
  }

  if (cond2) {
    handler();
  }
}

- (void)test_nil_suppression_3:(int)cond1
                        second:(int)cond2
                    completion:(void (^)(void))handler {
  if (cond1) {
    handler();
    handler = 0;
    // no-warning
  }

  if (cond2) {
    handler();
  }
}

- (void)test_escape_before_branch:(int)cond
                   withCompletion:(void (^)(void))handler {
  if (cond) {
    filler();
  }

  void (^copiedHandler)(void) = ^{
    handler();
  };

  if (cond) {
    // no-warning
    handler();
  } else {
    copiedHandler();
  }
}

- (void)test_escape_after_branch:(int)cond
                  withCompletion:(void (^)(void))handler {
  if (cond) {
    // no-warning
    handler();
  }

  escape(handler);
}

- (void)test_termination:(int)cond
                  withCompletion:(void (^)(void))handler {
  // The code below was able to cause non-termination but should be
  // fixed now:
  do {
    escape(handler);    
    handler();    // expected-warning{{completion handler is called twice}} expected-note{{previous call is here; set to nil to indicate it cannot be called afterwards}}
  } while (cond);
}

typedef void (^DeferredBlock)(void);
static inline void DefferedCallback(DeferredBlock *inBlock) { (*inBlock)(); }
#define _DEFERCONCAT(a, b) a##b
#define _DEFERNAME(a) _DEFERCONCAT(__DeferredVar_, a)
#define DEFER __extension__ __attribute__((cleanup(DefferedCallback), unused)) \
                  DeferredBlock _DEFERNAME(__COUNTER__) = ^

- (void)test_cleanup_1:(int)cond
        withCompletion:(void (^)(void))handler {
  int error = 0;
  DEFER {
    if (error)
      handler();
  };

  if (cond) {
    error = 1;
  } else {
    // no-warning
    handler();
  }
}

- (void)test_cleanup_2:(int)cond
        withCompletion:(void (^)(void))handler {
  int error = 0;
  DEFER {
    if (error)
      handler();
  };

  if (cond) {
    error = 1;
  } else {
    handler(); // expected-note{{previous call is here}}
  }

  // We still can warn about double call even in this case.
  handler(); // expected-warning{{completion handler is called twice}}
}

- (void)initWithAdditions:(int)cond
           withCompletion:(void (^)(void))handler {
  self = [self init];
  if (self) {
    escape(handler);
  }
  // no-warning
}

@end