bugprone-branch-clone.cpp 26.3 KB
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
// RUN: %check_clang_tidy %s bugprone-branch-clone %t -- -- -fno-delayed-template-parsing

void test_basic1(int in, int &out) {
  if (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  else
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    out++;

  out++;
}

void test_basic2(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  }
  else {
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    out++;
  }

  out++;
}

void test_basic3(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  }
  else
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    out++;

  out++;
}

void test_basic4(int in, int &out) {
  if (in > 77) {
    out--;
  }
  else {
    out++;
  }
}

void test_basic5(int in, int &out) {
  if (in > 77) {
    out++;
  }
  else {
    out++;
    out++;
  }
}

void test_basic6(int in, int &out) {
  if (in > 77) {
    out++;
  }
  else {
    out++, out++;
  }
}

void test_basic7(int in, int &out) {
  if (in > 77) {
    out++;
    out++;
  }
  else
    out++;

  out++;
}

void test_basic8(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
    out++;
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
    out++;
    out++;
  }

  if (in % 2)
    out++;
}

void test_basic9(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    if (in % 2)
      out++;
    else
      out--;
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
    if (in % 2)
      out++;
    else
      out--;
  }
}

// If we remove the braces from the previous example, the check recognizes it
// as an `else if`.
void test_basic10(int in, int &out) {
  if (in > 77)
    if (in % 2)
      out++;
    else
      out--;
  else
    if (in % 2)
      out++;
    else
      out--;

}

void test_basic11(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    if (in % 2)
      out++;
    else
      out--;
    if (in % 3)
      out++;
    else
      out--;
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
    if (in % 2)
      out++;
    else
      out--;
    if (in % 3)
      out++;
    else
      out--;
  }
}

void test_basic12(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
  }
}

void test_basic13(int in, int &out) {
  if (in > 77) {
    // Empty compound statement is not identical to null statement.
  } else;
}

// We use a comparison that ignores redundant parentheses:
void test_basic14(int in, int &out) {
  if (in > 77)
    out += 2;
  else
    (out) += (2);
}

void test_basic15(int in, int &out) {
  if (in > 77)
    ((out += 2));
  else
    out += 2;
}

// ..but does not apply additional simplifications:
void test_basic16(int in, int &out) {
  if (in > 77)
    out += 2;
  else
    out += 1 + 1;
}

// ..and does not forget important parentheses:
int test_basic17(int a, int b, int c, int mode) {
  if (mode>8)
    return (a + b) * c;
  else
    return a + b * c;
}

//=========--------------------==========//

#define PASTE_CODE(x) x

void test_macro1(int in, int &out) {
  PASTE_CODE(
    if (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: if with identical then and else branches [bugprone-branch-clone]
      out++;
    else
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
      out++;
  )

  out--;
}

void test_macro2(int in, int &out) {
  PASTE_CODE(
    if (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: if with identical then and else branches [bugprone-branch-clone]
      out++;
  )
  else
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    out++;
}

void test_macro3(int in, int &out) {
  if (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  PASTE_CODE(
    else
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
      out++;
  )
}

void test_macro4(int in, int &out) {
  if (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  else
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    PASTE_CODE(
      out++;
    )
}

void test_macro5(int in, int &out) {
  PASTE_CODE(if) (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  PASTE_CODE(else)
// CHECK-MESSAGES: :[[@LINE-1]]:14: note: else branch starts here
    out++;
}

#define OTHERWISE_INCREASE else out++

void test_macro6(int in, int &out) {
  if (in > 77)
      out++;
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
  OTHERWISE_INCREASE;
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
// CHECK-MESSAGES: :[[@LINE-8]]:28: note: expanded from macro 'OTHERWISE_INCREASE'
}

#define COND_INCR(a, b, c) \
  do {                     \
    if ((a))               \
      (b)++;               \
    else                   \
      (c)++;               \
  } while (0)

void test_macro7(int in, int &out1, int &out2) {
  COND_INCR(in, out1, out1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE-9]]:5: note: expanded from macro 'COND_INCR'
// CHECK-MESSAGES: :[[@LINE-3]]:3: note: else branch starts here
// CHECK-MESSAGES: :[[@LINE-9]]:5: note: expanded from macro 'COND_INCR'
}

void test_macro8(int in, int &out1, int &out2) {
  COND_INCR(in, out1, out2);
}

void test_macro9(int in, int &out1, int &out2) {
  COND_INCR(in, out2, out2);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE-21]]:5: note: expanded from macro 'COND_INCR'
// CHECK-MESSAGES: :[[@LINE-3]]:3: note: else branch starts here
// CHECK-MESSAGES: :[[@LINE-21]]:5: note: expanded from macro 'COND_INCR'
}

#define CONCAT(a, b) a##b

void test_macro10(int in, int &out) {
  CONCAT(i, f) (in > 77)
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    out++;
  CONCAT(el, se)
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    out++;
}

#define PROBLEM (-1)

int test_macro11(int count) {
  if (!count)
    return PROBLEM;
  else if (count == 13)
    return -1;
  else
    return count * 2;
}

#define IF if (
#define THEN ) {
#define ELSE } else {
#define END }

void test_macro12(int in, int &out) {
  IF in > 77 THEN
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE-8]]:12: note: expanded from macro 'IF'
    out++;
    out++;
  ELSE
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
// CHECK-MESSAGES: :[[@LINE-11]]:16: note: expanded from macro 'ELSE'
    out++;
    out++;
  END
}

// A hack for implementing a switch with no fallthrough:
#define SWITCH(x) switch (x) {
#define CASE(x) break; case (x):
#define DEFAULT break; default:

void test_macro13(int in, int &out) {
  SWITCH(in)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
    CASE(1)
      out++;
      out++;
    CASE(2)
      out++;
      out++;
    CASE(3)
      out++;
      out++;
// CHECK-MESSAGES: :[[@LINE-15]]:24: note: expanded from macro 'CASE'
// CHECK-MESSAGES: :[[@LINE+1]]:9: note: last of these clones ends here
    CASE(4)
      out++;
    CASE(5)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
    CASE(6)
      out--;
    CASE(7)
      out--;
// CHECK-MESSAGES: :[[@LINE-25]]:24: note: expanded from macro 'CASE'
// CHECK-MESSAGES: :[[@LINE+2]]:9: note: last of these clones ends here
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
    CASE(8)
      out++;
      out++;
    CASE(9)
      out++;
      out++;
// CHECK-MESSAGES: :[[@LINE-34]]:24: note: expanded from macro 'CASE'
// CHECK-MESSAGES: :[[@LINE+2]]:12: note: last of these clones ends here
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
    DEFAULT
      out--;
      out--;
    CASE(10)
      out--;
      out--;
// CHECK-MESSAGES: :[[@LINE-42]]:24: note: expanded from macro 'DEFAULT'
// CHECK-MESSAGES: :[[@LINE+1]]:9: note: last of these clones ends here
    CASE(12)
      out++;
    CASE(13)
      out++;
  END
}

//=========--------------------==========//

void test_chain1(int in, int &out) {
  if (in > 77)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: end of the original
  else if (in > 55)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 1 starts here
    out++;

  out++;
}

void test_chain2(int in, int &out) {
  if (in > 77)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: end of the original
  else if (in > 55)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 1 starts here
    out++;
  else if (in > 42)
    out--;
  else if (in > 28)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 2 starts here
    out++;
  else if (in > 12) {
    out++;
    out *= 7;
  } else if (in > 7) {
// CHECK-MESSAGES: :[[@LINE-1]]:22: note: clone 3 starts here
    out++;
  }
}

void test_chain3(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
    out++;
// CHECK-MESSAGES: :[[@LINE+1]]:4: note: end of the original
  } else if (in > 55) {
// CHECK-MESSAGES: :[[@LINE-1]]:23: note: clone 1 starts here
    out++;
    out++;
  } else if (in > 42)
    out--;
  else if (in > 28) {
// CHECK-MESSAGES: :[[@LINE-1]]:21: note: clone 2 starts here
    out++;
    out++;
  } else if (in > 12) {
    out++;
    out++;
    out++;
    out *= 7;
  } else if (in > 7) {
// CHECK-MESSAGES: :[[@LINE-1]]:22: note: clone 3 starts here
    out++;
    out++;
  }
}

// In this chain there are two clone families; notice that the checker
// describes all branches of the first one before mentioning the second one.
void test_chain4(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
    out++;
// CHECK-MESSAGES: :[[@LINE+1]]:4: note: end of the original
  } else if (in > 55) {
// CHECK-MESSAGES: :[[@LINE-1]]:23: note: clone 1 starts here
// CHECK-MESSAGES: :[[@LINE+8]]:21: note: clone 2 starts here
// CHECK-MESSAGES: :[[@LINE+15]]:22: note: clone 3 starts here
    out++;
    out++;
  } else if (in > 42)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out--;
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: end of the original
  else if (in > 28) {
    out++;
    out++;
  } else if (in > 12) {
    out++;
    out++;
    out++;
    out *= 7;
  } else if (in > 7) {
    out++;
    out++;
  } else if (in > -3) {
// CHECK-MESSAGES: :[[@LINE-1]]:23: note: clone 1 starts here
    out--;
  }
}

void test_chain5(int in, int &out) {
  if (in > 77)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: end of the original
  else if (in > 55)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 1 starts here
    out++;
  else if (in > 42)
    out--;
  else if (in > 28)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 2 starts here
    out++;
  else if (in > 12) {
    out++;
    out *= 7;
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: clone 3 starts here
    out++;
  }
}

void test_chain6(int in, int &out) {
  if (in > 77) {
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
    out++;
// CHECK-MESSAGES: :[[@LINE+1]]:4: note: end of the original
  } else if (in > 55) {
// CHECK-MESSAGES: :[[@LINE-1]]:23: note: clone 1 starts here
    out++;
    out++;
  } else if (in > 42)
    out--;
  else if (in > 28) {
// CHECK-MESSAGES: :[[@LINE-1]]:21: note: clone 2 starts here
    out++;
    out++;
  } else if (in > 12) {
    out++;
    out++;
    out++;
    out *= 7;
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: clone 3 starts here
    out++;
    out++;
  }
}

void test_nested(int a, int b, int c, int &out) {
  if (a > 5) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+27]]:5: note: else branch starts here
    if (b > 5) {
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: repeated branch in conditional chain [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+9]]:6: note: end of the original
// CHECK-MESSAGES: :[[@LINE+8]]:24: note: clone 1 starts here
// CHECK-MESSAGES: :[[@LINE+14]]:12: note: clone 2 starts here
      if (c > 5)
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: if with identical then and else branches [bugprone-branch-clone]
        out++;
      else
// CHECK-MESSAGES: :[[@LINE-1]]:7: note: else branch starts here
        out++;
    } else if (b > 15) {
      if (c > 5)
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: if with identical then and else branches [bugprone-branch-clone]
        out++;
      else
// CHECK-MESSAGES: :[[@LINE-1]]:7: note: else branch starts here
        out++;
    } else {
      if (c > 5)
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: if with identical then and else branches [bugprone-branch-clone]
        out++;
      else
// CHECK-MESSAGES: :[[@LINE-1]]:7: note: else branch starts here
        out++;
    }
  } else {
    if (b > 5) {
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: repeated branch in conditional chain [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+9]]:6: note: end of the original
// CHECK-MESSAGES: :[[@LINE+8]]:24: note: clone 1 starts here
// CHECK-MESSAGES: :[[@LINE+14]]:12: note: clone 2 starts here
      if (c > 5)
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: if with identical then and else branches [bugprone-branch-clone]
        out++;
      else
// CHECK-MESSAGES: :[[@LINE-1]]:7: note: else branch starts here
        out++;
    } else if (b > 15) {
      if (c > 5)
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: if with identical then and else branches [bugprone-branch-clone]
        out++;
      else
// CHECK-MESSAGES: :[[@LINE-1]]:7: note: else branch starts here
        out++;
    } else {
      if (c > 5)
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: if with identical then and else branches [bugprone-branch-clone]
        out++;
      else
// CHECK-MESSAGES: :[[@LINE-1]]:7: note: else branch starts here
        out++;
    }
  }
}

//=========--------------------==========//

template <class T>
void test_template_not_instantiated(const T &t) {
  int a;
  if (t)
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    a++;
  else
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    a++;
}

template <class T>
void test_template_instantiated(const T &t) {
  int a;
  if (t)
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    a++;
  else
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: else branch starts here
    a++;
}

template void test_template_instantiated<int>(const int &t);

template <class T>
void test_template2(T t, int a) {
  if (a) {
    T b(0);
    a += b;
  } else {
    int b(0);
    a += b;
  }
}

template void test_template2<int>(int t, int a);

template <class T>
void test_template3(T t, int a) {
  if (a) {
    T b(0);
    a += b;
  } else {
    int b(0);
    a += b;
  }
}

template void test_template3<short>(short t, int a);

template <class T>
void test_template_two_instances(T t, int &a) {
  if (a) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    a += int(t);
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
    a += int(t);
  }
}

template void test_template_two_instances<short>(short t, int &a);
template void test_template_two_instances<long>(long t, int &a);

class C {
  int member;
  void inline_method(int arg) {
    if (arg)
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: if with identical then and else branches [bugprone-branch-clone]
      member = 3;
    else
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
      member = 3;
  }
  int other_method();
};

int C::other_method() {
  if (member) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if with identical then and else branches [bugprone-branch-clone]
    return 8;
  } else {
// CHECK-MESSAGES: :[[@LINE-1]]:5: note: else branch starts here
    return 8;
  }
}

//=========--------------------==========//

int simple_switch(char ch) {
  switch (ch) {
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
  case 'a':
    return 10;
  case 'A':
    return 10;
// CHECK-MESSAGES: :[[@LINE-1]]:14: note: last of these clones ends here
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
  case 'b':
    return 11;
  case 'B':
    return 11;
// CHECK-MESSAGES: :[[@LINE-1]]:14: note: last of these clones ends here
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
  case 'c':
    return 10;
  case 'C':
    return 10;
// CHECK-MESSAGES: :[[@LINE-1]]:14: note: last of these clones ends here
  default:
    return 0;
  }
}

int long_sequence_switch(char ch) {
  switch (ch) {
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: switch has 7 consecutive identical branches [bugprone-branch-clone]
  case 'a':
    return 10;
  case 'A':
    return 10;
  case 'b':
    return 10;
  case 'B':
    return 10;
  case 'c':
    return 10;
  case 'C':
    return 10;
  default:
    return 10;
// CHECK-MESSAGES: :[[@LINE-1]]:14: note: last of these clones ends here
  }
}

int nested_switch(int a, int b, int c) {
  switch (a) {
// CHECK-MESSAGES: :[[@LINE+2]]:3: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+114]]:6: note: last of these clones ends here
  case 1:
    switch (b) {
// CHECK-MESSAGES: :[[@LINE+2]]:5: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+33]]:8: note: last of these clones ends here
    case 1:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    case 2:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    default:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    }
  case 2:
    switch (b) {
// CHECK-MESSAGES: :[[@LINE+2]]:5: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+33]]:8: note: last of these clones ends here
    case 1:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    case 2:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    default:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    }
  default:
    switch (b) {
// CHECK-MESSAGES: :[[@LINE+2]]:5: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
// CHECK-MESSAGES: :[[@LINE+33]]:8: note: last of these clones ends here
    case 1:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    case 2:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    default:
      switch (c) {
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: switch has 3 consecutive identical branches [bugprone-branch-clone]
      case 1:
        return 42;
      case 2:
        return 42;
      default:
        return 42;
// CHECK-MESSAGES: :[[@LINE-1]]:18: note: last of these clones ends here
      }
    }
  }
}

//=========--------------------==========//

// This should not produce warnings, as in switch statements we only report
// identical branches when they are consecutive. Also note that a branch
// terminated by a break is different from a branch terminated by the end of
// the switch statement.
int interleaved_cases(int a, int b) {
  switch (a) {
  case 3:
  case 4:
    b = 2;
    break;
  case 5:
    b = 3;
    break;
  case 6:
    b = 2;
    break;
  case 7:
    if (b % 2) {
      b++;
    } else {
      b++;
      break;
    }
    b = 2;
    break;
  case 8:
    b = 2;
  case 9:
    b = 3;
    break;
  default:
    b = 3;
  }
  return b;
}


// A case: or default: is only considered to be the start of a branch if it is a direct child of the CompoundStmt forming the body of the switch
int buried_cases(int foo) {
  switch (foo) {
    {
    case 36:
      return 8;
    default:
      return 8;
    }
  }
}

// Here the `case 7:` is a child statement of the GotoLabelStmt, so the checker
// thinks that it is part of the `case 9:` branch. While this result is
// counterintuitve, mixing goto labels and switch statements in this fashion is
// pretty rare, so it does not deserve a special case in the checker code.
int decorated_cases(int z) {
  if (!(z % 777)) {
    goto lucky;
  }
  switch (z) {
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
  case 1:
  case 2:
  case 3:
    z++;
    break;
  case 4:
  case 5:
    z++;
    break;
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: last of these clones ends here
  case 9:
    z++;
    break;
  lucky:
  case 7:
    z += 3;
    z *= 2;
    break;
  case 92:
    z += 3;
    z *= 2;
    break;
  default:
    z++;
  }
  return z + 92;
}

// The child of the switch statement is not neccessarily a compound statement,
// do not crash in this unusual case.
char no_real_body(int in, int &out) {
  switch (in)
  case 42:
    return 'A';

  if (in > 77)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: repeated branch in conditional chain [bugprone-branch-clone]
    out++;
// CHECK-MESSAGES: :[[@LINE-1]]:10: note: end of the original
  else if (in > 55)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 1 starts here
    out++;
  else if (in > 34)
// CHECK-MESSAGES: :[[@LINE+1]]:5: note: clone 2 starts here
    out++;

  return '|';
}

// Duff's device [https://en.wikipedia.org/wiki/Duff's_device]
// The check does not try to distinguish branches in this sort of convoluted
// code, but it should avoid crashing.
void send(short *to, short *from, int count)
{
    int n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
            } while (--n > 0);
    }
}

//=========--------------------==========//

void ternary1(bool b, int &x) {
// CHECK-MESSAGES: :[[@LINE+1]]:6: warning: conditional operator with identical true and false expressions [bugprone-branch-clone]
  (b ? x : x) = 42;
}

int ternary2(bool b, int x) {
// CHECK-MESSAGES: :[[@LINE+1]]:12: warning: conditional operator with identical true and false expressions [bugprone-branch-clone]
  return b ? 42 : 42;
}

int ternary3(bool b, int x) {
  return b ? 42 : 43;
}

int ternary4(bool b, int x) {
  return b ? true ? 45 : 44 : false ? 45 : 44;
}

// We do not detect chains of conditional operators.
int ternary5(bool b1, bool b2, int x) {
  return b1 ? 42 : b2 ? 43 : 42;
}

#define SWITCH_WITH_LBRACE(b) switch (b) {
#define SEMICOLON_CASE_COLON(b)                                                \
  ;                                                                            \
  case b:
int d, e;
void dontCrash() {
  SWITCH_WITH_LBRACE(d)
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
  SEMICOLON_CASE_COLON(1)
    e++;
    e++;
  SEMICOLON_CASE_COLON(2)
    e++;
    e++;
  // CHECK-MESSAGES: :[[@LINE-11]]:3: note: expanded from macro 'SEMICOLON_CASE_COLON'
  // CHECK-MESSAGES: :[[@LINE+1]]:23: note: last of these clones ends here
  SEMICOLON_CASE_COLON(3);
  }
}