StructuralEquivalenceTest.cpp 46.8 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 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 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTStructuralEquivalence.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Testing/CommandLineArgs.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/Host.h"

#include "DeclMatcher.h"

#include "gtest/gtest.h"

namespace clang {
namespace ast_matchers {

using std::get;

struct StructuralEquivalenceTest : ::testing::Test {
  std::unique_ptr<ASTUnit> AST0, AST1;
  std::string Code0, Code1; // Buffers for SourceManager

  // Get a pair of node pointers into the synthesized AST from the given code
  // snippets. To determine the returned node, a separate matcher is specified
  // for both snippets. The first matching node is returned.
  template <typename NodeType, typename MatcherType>
  std::tuple<NodeType *, NodeType *>
  makeDecls(const std::string &SrcCode0, const std::string &SrcCode1,
            TestLanguage Lang, const MatcherType &Matcher0,
            const MatcherType &Matcher1) {
    this->Code0 = SrcCode0;
    this->Code1 = SrcCode1;
    std::vector<std::string> Args = getCommandLineArgsForTesting(Lang);

    const char *const InputFileName = "input.cc";

    AST0 = tooling::buildASTFromCodeWithArgs(Code0, Args, InputFileName);
    AST1 = tooling::buildASTFromCodeWithArgs(Code1, Args, InputFileName);

    NodeType *D0 = FirstDeclMatcher<NodeType>().match(
        AST0->getASTContext().getTranslationUnitDecl(), Matcher0);
    NodeType *D1 = FirstDeclMatcher<NodeType>().match(
        AST1->getASTContext().getTranslationUnitDecl(), Matcher1);

    return std::make_tuple(D0, D1);
  }

  std::tuple<TranslationUnitDecl *, TranslationUnitDecl *>
  makeTuDecls(const std::string &SrcCode0, const std::string &SrcCode1,
              TestLanguage Lang) {
    this->Code0 = SrcCode0;
    this->Code1 = SrcCode1;
    std::vector<std::string> Args = getCommandLineArgsForTesting(Lang);

    const char *const InputFileName = "input.cc";

    AST0 = tooling::buildASTFromCodeWithArgs(Code0, Args, InputFileName);
    AST1 = tooling::buildASTFromCodeWithArgs(Code1, Args, InputFileName);

    return std::make_tuple(AST0->getASTContext().getTranslationUnitDecl(),
                           AST1->getASTContext().getTranslationUnitDecl());
  }

  // Get a pair of node pointers into the synthesized AST from the given code
  // snippets. The same matcher is used for both snippets.
  template <typename NodeType, typename MatcherType>
  std::tuple<NodeType *, NodeType *>
  makeDecls(const std::string &SrcCode0, const std::string &SrcCode1,
            TestLanguage Lang, const MatcherType &AMatcher) {
    return makeDecls<NodeType, MatcherType>(
          SrcCode0, SrcCode1, Lang, AMatcher, AMatcher);
  }

  // Get a pair of Decl pointers to the synthesized declarations from the given
  // code snippets. We search for the first NamedDecl with given name in both
  // snippets.
  std::tuple<NamedDecl *, NamedDecl *>
  makeNamedDecls(const std::string &SrcCode0, const std::string &SrcCode1,
                 TestLanguage Lang, const char *const Identifier = "foo") {
    auto Matcher = namedDecl(hasName(Identifier));
    return makeDecls<NamedDecl>(SrcCode0, SrcCode1, Lang, Matcher);
  }

  bool testStructuralMatch(Decl *D0, Decl *D1) {
    llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls01;
    llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls10;
    StructuralEquivalenceContext Ctx01(
        D0->getASTContext(), D1->getASTContext(),
        NonEquivalentDecls01, StructuralEquivalenceKind::Default, false, false);
    StructuralEquivalenceContext Ctx10(
        D1->getASTContext(), D0->getASTContext(),
        NonEquivalentDecls10, StructuralEquivalenceKind::Default, false, false);
    bool Eq01 = Ctx01.IsEquivalent(D0, D1);
    bool Eq10 = Ctx10.IsEquivalent(D1, D0);
    EXPECT_EQ(Eq01, Eq10);
    return Eq01;
  }

  bool testStructuralMatch(std::tuple<Decl *, Decl *> t) {
    return testStructuralMatch(get<0>(t), get<1>(t));
  }
};

TEST_F(StructuralEquivalenceTest, Int) {
  auto Decls = makeNamedDecls("int foo;", "int foo;", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, IntVsSignedInt) {
  auto Decls = makeNamedDecls("int foo;", "signed int foo;", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, Char) {
  auto Decls = makeNamedDecls("char foo;", "char foo;", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

// This test is disabled for now.
// FIXME Whether this is equivalent is dependendant on the target.
TEST_F(StructuralEquivalenceTest, DISABLED_CharVsSignedChar) {
  auto Decls = makeNamedDecls("char foo;", "signed char foo;", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, ForwardRecordDecl) {
  auto Decls = makeNamedDecls("struct foo;", "struct foo;", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, IntVsSignedIntInStruct) {
  auto Decls = makeNamedDecls("struct foo { int x; };",
                              "struct foo { signed int x; };", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, CharVsSignedCharInStruct) {
  auto Decls = makeNamedDecls("struct foo { char x; };",
                              "struct foo { signed char x; };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, IntVsSignedIntTemplateSpec) {
  auto Decls = makeDecls<ClassTemplateSpecializationDecl>(
      R"(template <class T> struct foo; template<> struct foo<int>{};)",
      R"(template <class T> struct foo; template<> struct foo<signed int>{};)",
      Lang_CXX03, classTemplateSpecializationDecl());
  auto Spec0 = get<0>(Decls);
  auto Spec1 = get<1>(Decls);
  EXPECT_TRUE(testStructuralMatch(Spec0, Spec1));
}

TEST_F(StructuralEquivalenceTest, CharVsSignedCharTemplateSpec) {
  auto Decls = makeDecls<ClassTemplateSpecializationDecl>(
      R"(template <class T> struct foo; template<> struct foo<char>{};)",
      R"(template <class T> struct foo; template<> struct foo<signed char>{};)",
      Lang_CXX03, classTemplateSpecializationDecl());
  auto Spec0 = get<0>(Decls);
  auto Spec1 = get<1>(Decls);
  EXPECT_FALSE(testStructuralMatch(Spec0, Spec1));
}

TEST_F(StructuralEquivalenceTest, CharVsSignedCharTemplateSpecWithInheritance) {
  auto Decls = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
      struct true_type{};
      template <class T> struct foo;
      template<> struct foo<char> : true_type {};
      )",
      R"(
      struct true_type{};
      template <class T> struct foo;
      template<> struct foo<signed char> : true_type {};
      )",
      Lang_CXX03, classTemplateSpecializationDecl());
  EXPECT_FALSE(testStructuralMatch(Decls));
}

// This test is disabled for now.
// FIXME Enable it, once the check is implemented.
TEST_F(StructuralEquivalenceTest, DISABLED_WrongOrderInNamespace) {
  auto Code =
      R"(
      namespace NS {
      template <class T> class Base {
          int a;
      };
      class Derived : Base<Derived> {
      };
      }
      void foo(NS::Derived &);
      )";
  auto Decls = makeNamedDecls(Code, Code, Lang_CXX03);

  NamespaceDecl *NS =
      LastDeclMatcher<NamespaceDecl>().match(get<1>(Decls), namespaceDecl());
  ClassTemplateDecl *TD = LastDeclMatcher<ClassTemplateDecl>().match(
      get<1>(Decls), classTemplateDecl(hasName("Base")));

  // Reorder the decls, move the TD to the last place in the DC.
  NS->removeDecl(TD);
  NS->addDeclInternal(TD);

  EXPECT_FALSE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, WrongOrderOfFieldsInClass) {
  auto Code = "class X { int a; int b; };";
  auto Decls = makeNamedDecls(Code, Code, Lang_CXX03, "X");

  CXXRecordDecl *RD = FirstDeclMatcher<CXXRecordDecl>().match(
      get<1>(Decls), cxxRecordDecl(hasName("X")));
  FieldDecl *FD =
      FirstDeclMatcher<FieldDecl>().match(get<1>(Decls), fieldDecl(hasName("a")));

  // Reorder the FieldDecls
  RD->removeDecl(FD);
  RD->addDeclInternal(FD);

  EXPECT_FALSE(testStructuralMatch(Decls));
}

struct StructuralEquivalenceFunctionTest : StructuralEquivalenceTest {
};

TEST_F(StructuralEquivalenceFunctionTest, TemplateVsNonTemplate) {
  auto t = makeNamedDecls("void foo();", "template<class T> void foo();",
                          Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, DifferentOperators) {
  auto t = makeDecls<FunctionDecl>(
      "struct X{}; bool operator<(X, X);", "struct X{}; bool operator==(X, X);",
      Lang_CXX03, functionDecl(hasOverloadedOperatorName("<")),
      functionDecl(hasOverloadedOperatorName("==")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, SameOperators) {
  auto t = makeDecls<FunctionDecl>(
      "struct X{}; bool operator<(X, X);", "struct X{}; bool operator<(X, X);",
      Lang_CXX03, functionDecl(hasOverloadedOperatorName("<")),
      functionDecl(hasOverloadedOperatorName("<")));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, CtorVsDtor) {
  auto t = makeDecls<FunctionDecl>("struct X{ X(); };", "struct X{ ~X(); };",
                                   Lang_CXX03, cxxConstructorDecl(),
                                   cxxDestructorDecl());
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ParamConstWithRef) {
  auto t =
      makeNamedDecls("void foo(int&);", "void foo(const int&);", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ParamConstSimple) {
  auto t = makeNamedDecls("void foo(int);", "void foo(const int);", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(t));
  // consider this OK
}

TEST_F(StructuralEquivalenceFunctionTest, Throw) {
  auto t = makeNamedDecls("void foo();", "void foo() throw();", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, Noexcept) {
  auto t = makeNamedDecls("void foo();",
                          "void foo() noexcept;", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ThrowVsNoexcept) {
  auto t = makeNamedDecls("void foo() throw();",
                          "void foo() noexcept;", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ThrowVsNoexceptFalse) {
  auto t = makeNamedDecls("void foo() throw();",
                          "void foo() noexcept(false);", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ThrowVsNoexceptTrue) {
  auto t = makeNamedDecls("void foo() throw();",
                          "void foo() noexcept(true);", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NoexceptNonMatch) {
  auto t = makeNamedDecls("void foo() noexcept(false);",
                          "void foo() noexcept(true);", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NoexceptMatch) {
  auto t = makeNamedDecls("void foo() noexcept(false);",
                          "void foo() noexcept(false);", Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NoexceptVsNoexceptFalse) {
  auto t = makeNamedDecls("void foo() noexcept;",
                          "void foo() noexcept(false);", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NoexceptVsNoexceptTrue) {
  auto t = makeNamedDecls("void foo() noexcept;",
                          "void foo() noexcept(true);", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ReturnType) {
  auto t = makeNamedDecls("char foo();", "int foo();", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ReturnConst) {
  auto t = makeNamedDecls("char foo();", "const char foo();", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ReturnRef) {
  auto t = makeNamedDecls("char &foo();",
                          "char &&foo();", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ParamCount) {
  auto t = makeNamedDecls("void foo(int);", "void foo(int, int);", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ParamType) {
  auto t = makeNamedDecls("void foo(int);", "void foo(char);", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ParamName) {
  auto t = makeNamedDecls("void foo(int a);", "void foo(int b);", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, Variadic) {
  auto t =
      makeNamedDecls("void foo(int x...);", "void foo(int x);", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, ParamPtr) {
  auto t = makeNamedDecls("void foo(int *);", "void foo(int);", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NameInParen) {
  auto t = makeNamedDecls("void ((foo))();", "void foo();", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NameInParenWithExceptionSpec) {
  auto t = makeNamedDecls(
      "void (foo)() throw(int);",
      "void (foo)() noexcept;",
      Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, NameInParenWithConst) {
  auto t = makeNamedDecls(
      "struct A { void (foo)() const; };",
      "struct A { void (foo)(); };",
      Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, FunctionsWithDifferentNoreturnAttr) {
  auto t = makeNamedDecls("__attribute__((noreturn)) void foo();",
                          "                          void foo();", Lang_C99);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest,
    FunctionsWithDifferentCallingConventions) {
  // These attributes may not be available on certain platforms.
  if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getArch() !=
      llvm::Triple::x86_64)
    return;
  auto t = makeNamedDecls("__attribute__((preserve_all)) void foo();",
                          "__attribute__((ms_abi))   void foo();", Lang_C99);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceFunctionTest, FunctionsWithDifferentSavedRegsAttr) {
  if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getArch() !=
      llvm::Triple::x86_64)
    return;
  auto t = makeNamedDecls(
      "__attribute__((no_caller_saved_registers)) void foo();",
      "                                           void foo();", Lang_C99);
  EXPECT_FALSE(testStructuralMatch(t));
}

struct StructuralEquivalenceCXXMethodTest : StructuralEquivalenceTest {
};

TEST_F(StructuralEquivalenceCXXMethodTest, Virtual) {
  auto t = makeDecls<CXXMethodDecl>("struct X { void foo(); };",
                                    "struct X { virtual void foo(); };",
                                    Lang_CXX03, cxxMethodDecl(hasName("foo")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Pure) {
  auto t = makeNamedDecls("struct X { virtual void foo(); };",
                          "struct X { virtual void foo() = 0; };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, DISABLED_Final) {
  // The final-ness is not checked yet.
  auto t =
      makeNamedDecls("struct X { virtual void foo(); };",
                     "struct X { virtual void foo() final; };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Const) {
  auto t = makeNamedDecls("struct X { void foo(); };",
                          "struct X { void foo() const; };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Static) {
  auto t = makeNamedDecls("struct X { void foo(); };",
                          "struct X { static void foo(); };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Ref1) {
  auto t = makeNamedDecls("struct X { void foo(); };",
                          "struct X { void foo() &&; };", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Ref2) {
  auto t = makeNamedDecls("struct X { void foo() &; };",
                          "struct X { void foo() &&; };", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, AccessSpecifier) {
  auto t = makeDecls<CXXMethodDecl>("struct X { public: void foo(); };",
                                    "struct X { private: void foo(); };",
                                    Lang_CXX03, cxxMethodDecl(hasName("foo")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Delete) {
  auto t = makeNamedDecls("struct X { void foo(); };",
                          "struct X { void foo() = delete; };", Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Constructor) {
  auto t = makeDecls<FunctionDecl>("void foo();", "struct foo { foo(); };",
                                   Lang_CXX03, functionDecl(hasName("foo")),
                                   cxxConstructorDecl(hasName("foo")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, ConstructorParam) {
  auto t = makeDecls<CXXConstructorDecl>("struct X { X(); };",
                                         "struct X { X(int); };", Lang_CXX03,
                                         cxxConstructorDecl(hasName("X")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, ConstructorExplicit) {
  auto t = makeDecls<CXXConstructorDecl>("struct X { X(int); };",
                                         "struct X { explicit X(int); };",
                                         Lang_CXX11,
                                         cxxConstructorDecl(hasName("X")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, ConstructorDefault) {
  auto t = makeDecls<CXXConstructorDecl>("struct X { X(); };",
                                         "struct X { X() = default; };",
                                         Lang_CXX11,
                                         cxxConstructorDecl(hasName("X")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Conversion) {
  auto t = makeDecls<CXXConversionDecl>("struct X { operator bool(); };",
                                        "struct X { operator char(); };",
                                         Lang_CXX11,
                                         cxxConversionDecl());
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, Operator) {
  auto t =
      makeDecls<FunctionDecl>("struct X { int operator +(int); };",
                              "struct X { int operator -(int); };", Lang_CXX03,
                              functionDecl(hasOverloadedOperatorName("+")),
                              functionDecl(hasOverloadedOperatorName("-")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, OutOfClass1) {
  auto t = makeDecls<FunctionDecl>(
      "struct X { virtual void f(); }; void X::f() { }",
      "struct X { virtual void f() { }; };", Lang_CXX03,
      functionDecl(allOf(hasName("f"), isDefinition())));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceCXXMethodTest, OutOfClass2) {
  auto t = makeDecls<FunctionDecl>(
      "struct X { virtual void f(); }; void X::f() { }",
      "struct X { void f(); }; void X::f() { }", Lang_CXX03,
      functionDecl(allOf(hasName("f"), isDefinition())));
  EXPECT_FALSE(testStructuralMatch(t));
}

struct StructuralEquivalenceRecordTest : StructuralEquivalenceTest {
  // FIXME Use a common getRecordDecl with ASTImporterTest.cpp!
  RecordDecl *getRecordDecl(FieldDecl *FD) {
    auto *ET = cast<ElaboratedType>(FD->getType().getTypePtr());
    return cast<RecordType>(ET->getNamedType().getTypePtr())->getDecl();
  };
};

TEST_F(StructuralEquivalenceRecordTest, Name) {
  auto t = makeDecls<CXXRecordDecl>("struct A{ };", "struct B{ };", Lang_CXX03,
                                    cxxRecordDecl(hasName("A")),
                                    cxxRecordDecl(hasName("B")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, Fields) {
  auto t = makeNamedDecls("struct foo{ int x; };", "struct foo{ char x; };",
                          Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, DISABLED_Methods) {
  // Currently, methods of a class are not checked at class equivalence.
  auto t = makeNamedDecls("struct foo{ int x(); };", "struct foo{ char x(); };",
                          Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, Bases) {
  auto t = makeNamedDecls("struct A{ }; struct foo: A { };",
                          "struct B{ }; struct foo: B { };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, InheritanceVirtual) {
  auto t =
      makeNamedDecls("struct A{ }; struct foo: A { };",
                     "struct A{ }; struct foo: virtual A { };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, DISABLED_InheritanceType) {
  // Access specifier in inheritance is not checked yet.
  auto t =
      makeNamedDecls("struct A{ }; struct foo: public A { };",
                     "struct A{ }; struct foo: private A { };", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, Match) {
  auto Code = R"(
      struct A{ };
      struct B{ };
      struct foo: A, virtual B {
        void x();
        int a;
      };
      )";
  auto t = makeNamedDecls(Code, Code, Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, UnnamedRecordsShouldBeInequivalent) {
  auto t = makeTuDecls(
      R"(
      struct A {
        struct {
          struct A *next;
        } entry0;
        struct {
          struct A *next;
        } entry1;
      };
      )",
      "", Lang_C99);
  auto *TU = get<0>(t);
  auto *Entry0 =
      FirstDeclMatcher<FieldDecl>().match(TU, fieldDecl(hasName("entry0")));
  auto *Entry1 =
      FirstDeclMatcher<FieldDecl>().match(TU, fieldDecl(hasName("entry1")));
  auto *R0 = getRecordDecl(Entry0);
  auto *R1 = getRecordDecl(Entry1);

  ASSERT_NE(R0, R1);
  EXPECT_TRUE(testStructuralMatch(R0, R0));
  EXPECT_TRUE(testStructuralMatch(R1, R1));
  EXPECT_FALSE(testStructuralMatch(R0, R1));
}

TEST_F(StructuralEquivalenceRecordTest, AnonymousRecordsShouldBeInequivalent) {
  auto t = makeTuDecls(
      R"(
      struct X {
        struct {
          int a;
        };
        struct {
          int b;
        };
      };
      )",
      "", Lang_C99);
  auto *TU = get<0>(t);
  auto *A = FirstDeclMatcher<IndirectFieldDecl>().match(
      TU, indirectFieldDecl(hasName("a")));
  auto *FA = cast<FieldDecl>(A->chain().front());
  RecordDecl *RA = cast<RecordType>(FA->getType().getTypePtr())->getDecl();
  auto *B = FirstDeclMatcher<IndirectFieldDecl>().match(
      TU, indirectFieldDecl(hasName("b")));
  auto *FB = cast<FieldDecl>(B->chain().front());
  RecordDecl *RB = cast<RecordType>(FB->getType().getTypePtr())->getDecl();

  ASSERT_NE(RA, RB);
  EXPECT_TRUE(testStructuralMatch(RA, RA));
  EXPECT_TRUE(testStructuralMatch(RB, RB));
  EXPECT_FALSE(testStructuralMatch(RA, RB));
}

TEST_F(StructuralEquivalenceRecordTest,
       RecordsAreInequivalentIfOrderOfAnonRecordsIsDifferent) {
  auto t = makeTuDecls(
      R"(
      struct X {
        struct { int a; };
        struct { int b; };
      };
      )",
      R"(
      struct X { // The order is reversed.
        struct { int b; };
        struct { int a; };
      };
      )",
      Lang_C99);

  auto *TU = get<0>(t);
  auto *A = FirstDeclMatcher<IndirectFieldDecl>().match(
      TU, indirectFieldDecl(hasName("a")));
  auto *FA = cast<FieldDecl>(A->chain().front());
  RecordDecl *RA = cast<RecordType>(FA->getType().getTypePtr())->getDecl();

  auto *TU1 = get<1>(t);
  auto *A1 = FirstDeclMatcher<IndirectFieldDecl>().match(
      TU1, indirectFieldDecl(hasName("a")));
  auto *FA1 = cast<FieldDecl>(A1->chain().front());
  RecordDecl *RA1 = cast<RecordType>(FA1->getType().getTypePtr())->getDecl();

  RecordDecl *X =
      FirstDeclMatcher<RecordDecl>().match(TU, recordDecl(hasName("X")));
  RecordDecl *X1 =
      FirstDeclMatcher<RecordDecl>().match(TU1, recordDecl(hasName("X")));
  ASSERT_NE(X, X1);
  EXPECT_FALSE(testStructuralMatch(X, X1));

  ASSERT_NE(RA, RA1);
  EXPECT_TRUE(testStructuralMatch(RA, RA));
  EXPECT_TRUE(testStructuralMatch(RA1, RA1));
  EXPECT_FALSE(testStructuralMatch(RA1, RA));
}

TEST_F(StructuralEquivalenceRecordTest,
       UnnamedRecordsShouldBeInequivalentEvenIfTheSecondIsBeingDefined) {
  auto Code =
      R"(
      struct A {
        struct {
          struct A *next;
        } entry0;
        struct {
          struct A *next;
        } entry1;
      };
      )";
  auto t = makeTuDecls(Code, Code, Lang_C99);

  auto *FromTU = get<0>(t);
  auto *Entry1 =
      FirstDeclMatcher<FieldDecl>().match(FromTU, fieldDecl(hasName("entry1")));

  auto *ToTU = get<1>(t);
  auto *Entry0 =
      FirstDeclMatcher<FieldDecl>().match(ToTU, fieldDecl(hasName("entry0")));
  auto *A =
      FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("A")));
  A->startDefinition(); // Set isBeingDefined, getDefinition() will return a
                        // nullptr. This may be the case during ASTImport.

  auto *R0 = getRecordDecl(Entry0);
  auto *R1 = getRecordDecl(Entry1);

  ASSERT_NE(R0, R1);
  EXPECT_TRUE(testStructuralMatch(R0, R0));
  EXPECT_TRUE(testStructuralMatch(R1, R1));
  EXPECT_FALSE(testStructuralMatch(R0, R1));
}

TEST_F(StructuralEquivalenceRecordTest, TemplateVsNonTemplate) {
  auto t = makeDecls<CXXRecordDecl>("struct A { };",
                                    "template<class T> struct A { };",
                                    Lang_CXX03, cxxRecordDecl(hasName("A")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest,
    FwdDeclRecordShouldBeEqualWithFwdDeclRecord) {
  auto t = makeNamedDecls("class foo;", "class foo;", Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest,
       FwdDeclRecordShouldBeEqualWithRecordWhichHasDefinition) {
  auto t =
      makeNamedDecls("class foo;", "class foo { int A; };", Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest,
       RecordShouldBeEqualWithRecordWhichHasDefinition) {
  auto t = makeNamedDecls("class foo { int A; };", "class foo { int A; };",
                          Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, RecordsWithDifferentBody) {
  auto t = makeNamedDecls("class foo { int B; };", "class foo { int A; };",
                          Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, SameFriendMultipleTimes) {
  auto t = makeNamedDecls("struct foo { friend class X; };",
                          "struct foo { friend class X; friend class X; };",
                          Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, SameFriendsDifferentOrder) {
  auto t = makeNamedDecls("struct foo { friend class X; friend class Y; };",
                          "struct foo { friend class Y; friend class X; };",
                          Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceRecordTest, SameFriendsSameOrder) {
  auto t = makeNamedDecls("struct foo { friend class X; friend class Y; };",
                          "struct foo { friend class X; friend class Y; };",
                          Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

struct StructuralEquivalenceLambdaTest : StructuralEquivalenceTest {};

TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithDifferentMethods) {
  // Get the LambdaExprs, unfortunately we can't match directly the underlying
  // implicit CXXRecordDecl of the Lambda classes.
  auto t = makeDecls<LambdaExpr>(
      "void f() { auto L0 = [](int){}; }",
      "void f() { auto L1 = [](){}; }",
      Lang_CXX11,
      lambdaExpr(),
      lambdaExpr());
  CXXRecordDecl *L0 = get<0>(t)->getLambdaClass();
  CXXRecordDecl *L1 = get<1>(t)->getLambdaClass();
  EXPECT_FALSE(testStructuralMatch(L0, L1));
}

TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithEqMethods) {
  auto t = makeDecls<LambdaExpr>(
      "void f() { auto L0 = [](int){}; }",
      "void f() { auto L1 = [](int){}; }",
      Lang_CXX11,
      lambdaExpr(),
      lambdaExpr());
  CXXRecordDecl *L0 = get<0>(t)->getLambdaClass();
  CXXRecordDecl *L1 = get<1>(t)->getLambdaClass();
  EXPECT_TRUE(testStructuralMatch(L0, L1));
}

TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithDifferentFields) {
  auto t = makeDecls<LambdaExpr>(
      "void f() { char* X; auto L0 = [X](){}; }",
      "void f() { float X; auto L1 = [X](){}; }",
      Lang_CXX11,
      lambdaExpr(),
      lambdaExpr());
  CXXRecordDecl *L0 = get<0>(t)->getLambdaClass();
  CXXRecordDecl *L1 = get<1>(t)->getLambdaClass();
  EXPECT_FALSE(testStructuralMatch(L0, L1));
}

TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithEqFields) {
  auto t = makeDecls<LambdaExpr>(
      "void f() { float X; auto L0 = [X](){}; }",
      "void f() { float X; auto L1 = [X](){}; }",
      Lang_CXX11,
      lambdaExpr(),
      lambdaExpr());
  CXXRecordDecl *L0 = get<0>(t)->getLambdaClass();
  CXXRecordDecl *L1 = get<1>(t)->getLambdaClass();
  EXPECT_TRUE(testStructuralMatch(L0, L1));
}

TEST_F(StructuralEquivalenceTest, CompareSameDeclWithMultiple) {
  auto t = makeNamedDecls("struct A{ }; struct B{ }; void foo(A a, A b);",
                          "struct A{ }; struct B{ }; void foo(A a, B b);",
                          Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceTest, ExplicitBoolDifferent) {
  auto Decls = makeNamedDecls("struct foo {explicit(false) foo(int);};",
                              "struct foo {explicit(true) foo(int);};", Lang_CXX20);
  CXXConstructorDecl *First = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<0>(Decls), cxxConstructorDecl(hasName("foo")));
  CXXConstructorDecl *Second = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<1>(Decls), cxxConstructorDecl(hasName("foo")));
  EXPECT_FALSE(testStructuralMatch(First, Second));
}

TEST_F(StructuralEquivalenceTest, ExplicitBoolSame) {
  auto Decls = makeNamedDecls("struct foo {explicit(true) foo(int);};",
                              "struct foo {explicit(true) foo(int);};", Lang_CXX20);
  CXXConstructorDecl *First = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<0>(Decls), cxxConstructorDecl(hasName("foo")));
  CXXConstructorDecl *Second = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<1>(Decls), cxxConstructorDecl(hasName("foo")));
  EXPECT_TRUE(testStructuralMatch(First, Second));
}

struct StructuralEquivalenceEnumTest : StructuralEquivalenceTest {};

TEST_F(StructuralEquivalenceEnumTest, FwdDeclEnumShouldBeEqualWithFwdDeclEnum) {
  auto t = makeNamedDecls("enum class foo;", "enum class foo;", Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceEnumTest,
       FwdDeclEnumShouldBeEqualWithEnumWhichHasDefinition) {
  auto t =
      makeNamedDecls("enum class foo;", "enum class foo { A };", Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceEnumTest,
       EnumShouldBeEqualWithEnumWhichHasDefinition) {
  auto t = makeNamedDecls("enum class foo { A };", "enum class foo { A };",
                          Lang_CXX11);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceEnumTest, EnumsWithDifferentBody) {
  auto t = makeNamedDecls("enum class foo { B };", "enum class foo { A };",
                          Lang_CXX11);
  EXPECT_FALSE(testStructuralMatch(t));
}

struct StructuralEquivalenceTemplateTest : StructuralEquivalenceTest {};

TEST_F(StructuralEquivalenceTemplateTest, ExactlySameTemplates) {
  auto t = makeNamedDecls("template <class T> struct foo;",
                          "template <class T> struct foo;", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceTemplateTest, DifferentTemplateArgName) {
  auto t = makeNamedDecls("template <class T> struct foo;",
                          "template <class U> struct foo;", Lang_CXX03);
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceTemplateTest, DifferentTemplateArgKind) {
  auto t = makeNamedDecls("template <class T> struct foo;",
                          "template <int T> struct foo;", Lang_CXX03);
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceTemplateTest, ExplicitBoolSame) {
  auto Decls = makeNamedDecls(
      "template <bool b> struct foo {explicit(b) foo(int);};",
      "template <bool b> struct foo {explicit(b) foo(int);};", Lang_CXX20);
  CXXConstructorDecl *First = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<0>(Decls), cxxConstructorDecl(hasName("foo<b>")));
  CXXConstructorDecl *Second = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<1>(Decls), cxxConstructorDecl(hasName("foo<b>")));
  EXPECT_TRUE(testStructuralMatch(First, Second));
}

TEST_F(StructuralEquivalenceTemplateTest, ExplicitBoolDifference) {
  auto Decls = makeNamedDecls(
      "template <bool b> struct foo {explicit(b) foo(int);};",
      "template <bool b> struct foo {explicit(!b) foo(int);};", Lang_CXX20);
  CXXConstructorDecl *First = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<0>(Decls), cxxConstructorDecl(hasName("foo<b>")));
  CXXConstructorDecl *Second = FirstDeclMatcher<CXXConstructorDecl>().match(
      get<1>(Decls), cxxConstructorDecl(hasName("foo<b>")));
  EXPECT_FALSE(testStructuralMatch(First, Second));
}

TEST_F(StructuralEquivalenceTemplateTest,
       TemplateVsSubstTemplateTemplateParmInArgEq) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
template <typename P1> class Arg { };
template <template <typename PP1> class P1> class Primary { };

void f() {
  // Make specialization with simple template.
  Primary <Arg> A;
}
      )",
      R"(
template <typename P1> class Arg { };
template <template <typename PP1> class P1> class Primary { };

template <template <typename PP1> class P1> class Templ {
  void f() {
    // Make specialization with substituted template template param.
    Primary <P1> A;
  };
};

// Instantiate with substitution Arg into P1.
template class Templ <Arg>;
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceTemplateTest,
       TemplateVsSubstTemplateTemplateParmInArgNotEq) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
template <typename P1> class Arg { };
template <template <typename PP1> class P1> class Primary { };

void f() {
  // Make specialization with simple template.
  Primary <Arg> A;
}
      )",
      R"(
// Arg is different from the other, this should cause non-equivalence.
template <typename P1> class Arg { int X; };
template <template <typename PP1> class P1> class Primary { };

template <template <typename PP1> class P1> class Templ {
  void f() {
    // Make specialization with substituted template template param.
    Primary <P1> A;
  };
};

// Instantiate with substitution Arg into P1.
template class Templ <Arg>;
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_FALSE(testStructuralMatch(t));
}

struct StructuralEquivalenceDependentTemplateArgsTest
    : StructuralEquivalenceTemplateTest {};

TEST_F(StructuralEquivalenceDependentTemplateArgsTest,
       SameStructsInDependentArgs) {
  std::string Code =
      R"(
      template <typename>
      struct S1;

      template <typename>
      struct enable_if;

      struct S
      {
        template <typename T, typename enable_if<S1<T>>::type>
        void f();
      };
      )";
  auto t = makeDecls<FunctionTemplateDecl>(Code, Code, Lang_CXX11,
                                           functionTemplateDecl(hasName("f")));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceDependentTemplateArgsTest,
       DifferentStructsInDependentArgs) {
  std::string Code =
      R"(
      template <typename>
      struct S1;

      template <typename>
      struct S2;

      template <typename>
      struct enable_if;
      )";
  auto t = makeDecls<FunctionTemplateDecl>(Code + R"(
      struct S
      {
        template <typename T, typename enable_if<S1<T>>::type>
        void f();
      };
      )",
                                           Code + R"(
      struct S
      {
        template <typename T, typename enable_if<S2<T>>::type>
        void f();
      };
      )",
                                           Lang_CXX11,
                                           functionTemplateDecl(hasName("f")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceDependentTemplateArgsTest,
       SameStructsInDependentScopeDeclRefExpr) {
  std::string Code =
      R"(
      template <typename>
      struct S1;

      template <bool>
      struct enable_if;

      struct S
      {
        template <typename T, typename enable_if<S1<T>::value>::type>
        void f();   // DependentScopeDeclRefExpr:^^^^^^^^^^^^
      };
      )";
  auto t = makeDecls<FunctionTemplateDecl>(Code, Code, Lang_CXX11,
                                           functionTemplateDecl(hasName("f")));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceDependentTemplateArgsTest,
       DifferentStructsInDependentScopeDeclRefExpr) {
  std::string Code =
      R"(
      template <typename>
      struct S1;

      template <typename>
      struct S2;

      template <bool>
      struct enable_if;
      )";
  auto t = makeDecls<FunctionTemplateDecl>(Code + R"(
      struct S
      {
        template <typename T, typename enable_if<S1<T>::value>::type>
        void f();   // DependentScopeDeclRefExpr:^^^^^^^^^^^^
      };
      )",
                                           Code + R"(
      struct S
      {
        template <typename T, typename enable_if<S2<T>::value>::type>
        void f();
      };
      )",
                                           Lang_CXX03,
                                           functionTemplateDecl(hasName("f")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceDependentTemplateArgsTest,
       DifferentValueInDependentScopeDeclRefExpr) {
  std::string Code =
      R"(
      template <typename>
      struct S1;

      template <bool>
      struct enable_if;
      )";
  auto t = makeDecls<FunctionTemplateDecl>(Code + R"(
      struct S
      {
        template <typename T, typename enable_if<S1<T>::value1>::type>
        void f();   // DependentScopeDeclRefExpr:^^^^^^^^^^^^
      };
      )",
                                           Code + R"(
      struct S
      {
        template <typename T, typename enable_if<S1<T>::value2>::type>
        void f();
      };
      )",
                                           Lang_CXX03,
                                           functionTemplateDecl(hasName("f")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(
    StructuralEquivalenceTemplateTest,
    ClassTemplSpecWithQualifiedAndNonQualifiedTypeArgsShouldBeEqual) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
      template <class T> struct Primary {};
      namespace N {
        struct Arg;
      }
      // Explicit instantiation with qualified name.
      template struct Primary<N::Arg>;
      )",
      R"(
      template <class T> struct Primary {};
      namespace N {
        struct Arg;
      }
      using namespace N;
      // Explicit instantiation with UNqualified name.
      template struct Primary<Arg>;
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(
    StructuralEquivalenceTemplateTest,
    ClassTemplSpecWithInequivalentQualifiedAndNonQualifiedTypeArgs) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
      template <class T> struct Primary {};
      namespace N {
        struct Arg { int a; };
      }
      // Explicit instantiation with qualified name.
      template struct Primary<N::Arg>;
      )",
      R"(
      template <class T> struct Primary {};
      namespace N {
        // This struct is not equivalent with the other in the prev TU.
        struct Arg { double b; }; // -- Field mismatch.
      }
      using namespace N;
      // Explicit instantiation with UNqualified name.
      template struct Primary<Arg>;
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(
    StructuralEquivalenceTemplateTest,
    ClassTemplSpecWithQualifiedAndNonQualifiedTemplArgsShouldBeEqual) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
      template <template <class> class T> struct Primary {};
      namespace N {
        template <class T> struct Arg;
      }
      // Explicit instantiation with qualified name.
      template struct Primary<N::Arg>;
      )",
      R"(
      template <template <class> class T> struct Primary {};
      namespace N {
        template <class T> struct Arg;
      }
      using namespace N;
      // Explicit instantiation with UNqualified name.
      template struct Primary<Arg>;
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(
    StructuralEquivalenceTemplateTest,
    ClassTemplSpecWithInequivalentQualifiedAndNonQualifiedTemplArgs) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
      template <template <class> class T> struct Primary {};
      namespace N {
        template <class T> struct Arg { int a; };
      }
      // Explicit instantiation with qualified name.
      template struct Primary<N::Arg>;
      )",
      R"(
      template <template <class> class T> struct Primary {};
      namespace N {
        // This template is not equivalent with the other in the prev TU.
        template <class T> struct Arg { double b; }; // -- Field mismatch.
      }
      using namespace N;
      // Explicit instantiation with UNqualified name.
      template struct Primary<Arg>;
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(
    StructuralEquivalenceTemplateTest,
    ClassTemplSpecWithInequivalentShadowedTemplArg) {
  auto t = makeDecls<ClassTemplateSpecializationDecl>(
      R"(
      template <template <class> class T> struct Primary {};
      template <class T> struct Arg { int a; };
      // Explicit instantiation with ::Arg
      template struct Primary<Arg>;
      )",
      R"(
      template <template <class> class T> struct Primary {};
      template <class T> struct Arg { int a; };
      namespace N {
        // This template is not equivalent with the other in the global scope.
        template <class T> struct Arg { double b; }; // -- Field mismatch.
        // Explicit instantiation with N::Arg which shadows ::Arg
        template struct Primary<Arg>;
      }
      )",
      Lang_CXX03, classTemplateSpecializationDecl(hasName("Primary")));
  EXPECT_FALSE(testStructuralMatch(t));
}
struct StructuralEquivalenceCacheTest : public StructuralEquivalenceTest {
  llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;

  template <typename NodeType, typename MatcherType>
  std::pair<NodeType *, NodeType *>
  findDeclPair(std::tuple<TranslationUnitDecl *, TranslationUnitDecl *> TU,
               MatcherType M) {
    NodeType *D0 = FirstDeclMatcher<NodeType>().match(get<0>(TU), M);
    NodeType *D1 = FirstDeclMatcher<NodeType>().match(get<1>(TU), M);
    return {D0, D1};
  }

  template <typename NodeType>
  bool isInNonEqCache(std::pair<NodeType *, NodeType *> D) {
    return NonEquivalentDecls.count(D) > 0;
  }
};

TEST_F(StructuralEquivalenceCacheTest, SimpleNonEq) {
  auto TU = makeTuDecls(
      R"(
      class A {};
      class B {};
      void x(A, A);
      )",
      R"(
      class A {};
      class B {};
      void x(A, B);
      )",
      Lang_CXX03);

  StructuralEquivalenceContext Ctx(
      get<0>(TU)->getASTContext(), get<1>(TU)->getASTContext(),
      NonEquivalentDecls, StructuralEquivalenceKind::Default, false, false);

  auto X = findDeclPair<FunctionDecl>(TU, functionDecl(hasName("x")));
  EXPECT_FALSE(Ctx.IsEquivalent(X.first, X.second));

  EXPECT_FALSE(isInNonEqCache(findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("A"), unless(isImplicit())))));
  EXPECT_FALSE(isInNonEqCache(findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("B"), unless(isImplicit())))));
}

TEST_F(StructuralEquivalenceCacheTest, SpecialNonEq) {
  auto TU = makeTuDecls(
      R"(
      class A {};
      class B { int i; };
      void x(A *);
      void y(A *);
      class C {
        friend void x(A *);
        friend void y(A *);
      };
      )",
      R"(
      class A {};
      class B { int i; };
      void x(A *);
      void y(B *);
      class C {
        friend void x(A *);
        friend void y(B *);
      };
      )",
      Lang_CXX03);

  StructuralEquivalenceContext Ctx(
      get<0>(TU)->getASTContext(), get<1>(TU)->getASTContext(),
      NonEquivalentDecls, StructuralEquivalenceKind::Default, false, false);

  auto C = findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("C"), unless(isImplicit())));
  EXPECT_FALSE(Ctx.IsEquivalent(C.first, C.second));

  EXPECT_FALSE(isInNonEqCache(C));
  EXPECT_FALSE(isInNonEqCache(findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("A"), unless(isImplicit())))));
  EXPECT_FALSE(isInNonEqCache(findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("B"), unless(isImplicit())))));
  EXPECT_FALSE(isInNonEqCache(
      findDeclPair<FunctionDecl>(TU, functionDecl(hasName("x")))));
  EXPECT_FALSE(isInNonEqCache(
      findDeclPair<FunctionDecl>(TU, functionDecl(hasName("y")))));
}

TEST_F(StructuralEquivalenceCacheTest, Cycle) {
  auto TU = makeTuDecls(
      R"(
      class C;
      class A { C *c; };
      void x(A *);
      class C {
        friend void x(A *);
      };
      )",
      R"(
      class C;
      class A { C *c; };
      void x(A *);
      class C {
        friend void x(A *);
      };
      )",
      Lang_CXX03);

  StructuralEquivalenceContext Ctx(
      get<0>(TU)->getASTContext(), get<1>(TU)->getASTContext(),
      NonEquivalentDecls, StructuralEquivalenceKind::Default, false, false);

  auto C = findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("C"), unless(isImplicit())));
  EXPECT_TRUE(Ctx.IsEquivalent(C.first, C.second));

  EXPECT_FALSE(isInNonEqCache(C));
  EXPECT_FALSE(isInNonEqCache(findDeclPair<CXXRecordDecl>(
      TU, cxxRecordDecl(hasName("A"), unless(isImplicit())))));
  EXPECT_FALSE(isInNonEqCache(
      findDeclPair<FunctionDecl>(TU, functionDecl(hasName("x")))));
}

} // end namespace ast_matchers
} // end namespace clang