aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Sema/HeuristicResolverTest.cpp
blob: ee434f7a1d43a8da6e862b3d756b5aa4ba6c0e23 (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
//===-- HeuristicResolverTests.cpp --------------------------*- C++ -*-----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Sema/HeuristicResolver.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Tooling.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"

using namespace clang::ast_matchers;
using testing::ElementsAre;

namespace clang {
namespace {

// Helper for matching a sequence of elements with a variadic list of matchers.
// Usage: `ElementsAre(matchAdapter(Vs, MatchFunction)...)`, where `Vs...` is
//        a variadic list of matchers.
// For each `V` in `Vs`, this will match the corresponding element `E` if
// `MatchFunction(V, E)` is true.
MATCHER_P2(matchAdapter, MatcherForElement, MatchFunction, "matchAdapter") {
  return MatchFunction(MatcherForElement, arg);
}

template <typename InputNode>
using ResolveFnT = std::function<std::vector<const NamedDecl *>(
    const HeuristicResolver *, const InputNode *)>;

// Test heuristic resolution on `Code` using the resolution procedure
// `ResolveFn`, which takes a `HeuristicResolver` and an input AST node of type
// `InputNode` and returns a `std::vector<const NamedDecl *>`.
// `InputMatcher` should be an AST matcher that matches a single node to pass as
// input to `ResolveFn`, bound to the ID "input". `OutputMatchers` should be AST
// matchers that each match a single node, bound to the ID "output".
template <typename InputNode, typename InputMatcher, typename... OutputMatchers>
void expectResolution(llvm::StringRef Code, ResolveFnT<InputNode> ResolveFn,
                      const InputMatcher &IM, const OutputMatchers &...OMS) {
  auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++20"});
  auto &Ctx = TU->getASTContext();
  auto InputMatches = match(IM, Ctx);
  ASSERT_EQ(1u, InputMatches.size());
  const auto *Input = InputMatches[0].template getNodeAs<InputNode>("input");
  ASSERT_TRUE(Input);

  auto OutputNodeMatches = [&](auto &OutputMatcher, auto &Actual) {
    auto OutputMatches = match(OutputMatcher, Ctx);
    if (OutputMatches.size() != 1u)
      return false;
    const auto *ExpectedOutput =
        OutputMatches[0].template getNodeAs<NamedDecl>("output");
    if (!ExpectedOutput)
      return false;
    return ExpectedOutput == Actual;
  };

  HeuristicResolver H(Ctx);
  auto Results = ResolveFn(&H, Input);
  EXPECT_THAT(Results, ElementsAre(matchAdapter(OMS, OutputNodeMatches)...));
}

// Wrapper for the above that accepts a HeuristicResolver member function
// pointer directly.
template <typename InputNode, typename InputMatcher, typename... OutputMatchers>
void expectResolution(llvm::StringRef Code,
                      std::vector<const NamedDecl *> (
                          HeuristicResolver::*ResolveFn)(const InputNode *)
                          const,
                      const InputMatcher &IM, const OutputMatchers &...OMS) {
  expectResolution(Code, ResolveFnT<InputNode>(std::mem_fn(ResolveFn)), IM,
                   OMS...);
}

TEST(HeuristicResolver, MemberExpr) {
  std::string Code = R"cpp(
    template <typename T>
    struct S {
      void bar() {}
    };

    template <typename T>
    void foo(S<T> arg) {
      arg.bar();
    }
  )cpp";
  // Test resolution of "bar" in "arg.bar()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("bar")).bind("input"),
      cxxMethodDecl(hasName("bar")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_Overloads) {
  std::string Code = R"cpp(
    template <typename T>
    struct S {
      void bar(int);
      void bar(float);
    };

    template <typename T, typename U>
    void foo(S<T> arg, U u) {
      arg.bar(u);
    }
  )cpp";
  // Test resolution of "bar" in "arg.bar(u)". Both overloads should be found.
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("bar")).bind("input"),
      cxxMethodDecl(hasName("bar"), hasParameter(0, hasType(asString("int"))))
          .bind("output"),
      cxxMethodDecl(hasName("bar"), hasParameter(0, hasType(asString("float"))))
          .bind("output"));
}

TEST(HeuristicResolver, MemberExpr_SmartPointer) {
  std::string Code = R"cpp(
    template <typename> struct S { void foo() {} };
    template <typename T> struct unique_ptr {
      T* operator->();
    };
    template <typename T>
    void test(unique_ptr<S<T>>& v) {
      v->foo();
    }
  )cpp";
  // Test resolution of "foo" in "v->foo()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"),
      cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_SmartPointer_Qualified) {
  std::string Code = R"cpp(
    template <typename> struct Waldo {
      void find();
      void find() const;
    };
    template <typename T> struct unique_ptr {
      T* operator->();
    };
    template <typename T>
    void test(unique_ptr<const Waldo<T>>& w) {
      w->find();
    }
  )cpp";
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("find")).bind("input"),
      cxxMethodDecl(hasName("find"), isConst()).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_Static_Qualified) {
  std::string Code = R"cpp(
    template <typename T>
    struct Waldo {
      static void find();
    };
    template <typename T>
    void foo(const Waldo<T>& t) {
      t.find();
    }
  )cpp";
  // Test resolution of "find" in "t.find()".
  // The object being `const` should have no bearing on a call to a static
  // method.
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("find")).bind("input"),
      cxxMethodDecl(hasName("find")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_AutoTypeDeduction1) {
  std::string Code = R"cpp(
    template <typename T>
    struct A {
      int waldo;
    };
    template <typename T>
    void foo(A<T> a) {
      auto copy = a;
      copy.waldo;
    }
  )cpp";
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("waldo")).bind("input"),
      fieldDecl(hasName("waldo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_AutoTypeDeduction2) {
  std::string Code = R"cpp(
    struct B {
      int waldo;
    };

    template <typename T>
    struct A {
      B b;
    };
    template <typename T>
    void foo(A<T> a) {
      auto b = a.b;
      b.waldo;
    }
  )cpp";
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("waldo")).bind("input"),
      fieldDecl(hasName("waldo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_Chained) {
  std::string Code = R"cpp(
    struct A { void foo() {} };
    template <typename T>
    struct B {
      A func(int);
      void bar() {
        func(1).foo();
      }
    };
  )cpp";
  // Test resolution of "foo" in "func(1).foo()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"),
      cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_ReferenceType) {
  std::string Code = R"cpp(
    struct B {
      int waldo;
    };
    template <typename T>
    struct A {
      B &b;
    };
    template <typename T>
    void foo(A<T> &a) {
      a.b.waldo;
    }
  )cpp";
  // Test resolution of "waldo" in "a.b.waldo".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("waldo")).bind("input"),
      fieldDecl(hasName("waldo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_PointerType) {
  std::string Code = R"cpp(
    struct B {
      int waldo;
    };
    template <typename T>
    struct A {
      B *b;
    };
    template <typename T>
    void foo(A<T> &a) {
      a.b->waldo;
    }
  )cpp";
  // Test resolution of "waldo" in "a.b->waldo".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("waldo")).bind("input"),
      fieldDecl(hasName("waldo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_TemplateArgs) {
  std::string Code = R"cpp(
    struct Foo {
      static Foo k(int);
      template <typename T> T convert();
    };
    template <typename T>
    void test() {
      Foo::k(T()).template convert<T>();
    }
  )cpp";
  // Test resolution of "convert" in "Foo::k(T()).template convert<T>()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("convert")).bind("input"),
      functionTemplateDecl(hasName("convert")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_TypeAlias) {
  std::string Code = R"cpp(
    template <typename T>
    struct Waldo {
      void find();
    };
    template <typename T>
    using Wally = Waldo<T>;
    template <typename T>
    void foo(Wally<T> w) {
      w.find();
    }
  )cpp";
  // Test resolution of "find" in "w.find()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("find")).bind("input"),
      cxxMethodDecl(hasName("find")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_BaseClass_TypeAlias) {
  std::string Code = R"cpp(
    template <typename T>
    struct Waldo {
      void find();
    };
    template <typename T>
    using Wally = Waldo<T>;
    template <typename T>
    struct S : Wally<T> {
      void foo() {
        this->find();
      }
    };
  )cpp";
  // Test resolution of "find" in "this->find()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("find")).bind("input"),
      cxxMethodDecl(hasName("find")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_Metafunction) {
  std::string Code = R"cpp(
    template <typename T>
    struct Waldo {
      void find();
    };
    template <typename T>
    struct MetaWaldo {
      using Type = Waldo<T>;
    };
    template <typename T>
    void foo(typename MetaWaldo<T>::Type w) {
      w.find();
    }
  )cpp";
  // Test resolution of "find" in "w.find()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("find")).bind("input"),
      cxxMethodDecl(hasName("find")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_Metafunction_Enumerator) {
  std::string Code = R"cpp(
    enum class State { Hidden };
    template <typename T>
    struct Meta {
      using Type = State;
    };
    template <typename T>
    void foo(typename Meta<T>::Type t) {
      t.Hidden;
    }
  )cpp";
  // Test resolution of "Hidden" in "t.Hidden".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("Hidden")).bind("input"),
      enumConstantDecl(hasName("Hidden")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_DeducedNonTypeTemplateParameter) {
  std::string Code = R"cpp(
    template <int N>
    struct Waldo {
      const int found = N;
    };
    template <Waldo W>
    int foo() {
      return W.found;
    }
  )cpp";
  // Test resolution of "found" in "W.found".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("found")).bind("input"),
      fieldDecl(hasName("found")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_HangIssue126536) {
  std::string Code = R"cpp(
    template <class T>
    void foo() {
      T bar;
      auto baz = (bar, bar);
      baz.foo();
    }
  )cpp";
  // Test resolution of "foo" in "baz.foo()".
  // Here, we are testing that we do not get into an infinite loop.
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"));
}

TEST(HeuristicResolver, MemberExpr_DefaultTemplateArgument) {
  std::string Code = R"cpp(
    struct Default {
      void foo();
    };
    template <typename T = Default>
    void bar(T t) {
      t.foo();
    }
  )cpp";
  // Test resolution of "foo" in "t.foo()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"),
      cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_DefaultTemplateArgument_Recursive) {
  std::string Code = R"cpp(
    struct Default {
      void foo();
    };
    template <typename D = Default, typename T = D>
    void bar(T t) {
      t.foo();
    }
  )cpp";
  // Test resolution of "foo" in "t.foo()".
  expectResolution(
      Code, &HeuristicResolver::resolveMemberExpr,
      cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"),
      cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_StaticMethod) {
  std::string Code = R"cpp(
    template <typename T>
    struct S {
      static void bar() {}
    };

    template <typename T>
    void foo() {
      S<T>::bar();
    }
  )cpp";
  // Test resolution of "bar" in "S<T>::bar()".
  expectResolution(
      Code, &HeuristicResolver::resolveDeclRefExpr,
      dependentScopeDeclRefExpr(hasDependentName("bar")).bind("input"),
      cxxMethodDecl(hasName("bar")).bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_DefaultTemplateArgument) {
  std::string Code = R"cpp(
    struct Default {
      static void foo();
    };
    template <typename T = Default>
    void bar() {
      T::foo();
    }
  )cpp";
  // Test resolution of "foo" in "T::foo()".
  expectResolution(
      Code, &HeuristicResolver::resolveDeclRefExpr,
      dependentScopeDeclRefExpr(hasDependentName("foo")).bind("input"),
      cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_StaticOverloads) {
  std::string Code = R"cpp(
    template <typename T>
    struct S {
      static void bar(int);
      static void bar(float);
    };

    template <typename T, typename U>
    void foo(U u) {
      S<T>::bar(u);
    }
  )cpp";
  // Test resolution of "bar" in "S<T>::bar(u)". Both overloads should be found.
  expectResolution(
      Code, &HeuristicResolver::resolveDeclRefExpr,
      dependentScopeDeclRefExpr(hasDependentName("bar")).bind("input"),
      cxxMethodDecl(hasName("bar"), hasParameter(0, hasType(asString("int"))))
          .bind("output"),
      cxxMethodDecl(hasName("bar"), hasParameter(0, hasType(asString("float"))))
          .bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_Enumerator) {
  std::string Code = R"cpp(
    template <typename T>
    struct Foo {
      enum class E { A, B };
      E e = E::A;
    };
  )cpp";
  // Test resolution of "A" in "E::A".
  expectResolution(
      Code, &HeuristicResolver::resolveDeclRefExpr,
      dependentScopeDeclRefExpr(hasDependentName("A")).bind("input"),
      enumConstantDecl(hasName("A")).bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_RespectScope) {
  std::string Code = R"cpp(
    template <typename Info>
    struct PointerIntPair {
      void *getPointer() const { return Info::getPointer(); }
    };
  )cpp";
  // Test resolution of "getPointer" in "Info::getPointer()".
  // Here, we are testing that we do not incorrectly get the enclosing
  // getPointer() function as a result.
  expectResolution(
      Code, &HeuristicResolver::resolveDeclRefExpr,
      dependentScopeDeclRefExpr(hasDependentName("getPointer")).bind("input"));
}

TEST(HeuristicResolver, DeclRefExpr_Nested) {
  std::string Code = R"cpp(
    struct S {
      static int Waldo;
    };
    template <typename T>
    struct Meta {
      using Type = S;
    };
    template <typename T>
    void foo() {
      Meta<T>::Type::Waldo;
    }
  )cpp";
  // Test resolution of "Waldo" in "Meta<T>::Type::Waldo".
  expectResolution(
      Code, &HeuristicResolver::resolveDeclRefExpr,
      dependentScopeDeclRefExpr(hasDependentName("Waldo")).bind("input"),
      varDecl(hasName("Waldo")).bind("output"));
}

TEST(HeuristicResolver, DependentNameType) {
  std::string Code = R"cpp(
    template <typename>
    struct A {
      struct B {};
    };
    template <typename T>
    void foo(typename A<T>::B);
  )cpp";
  // Tests resolution of "B" in "A<T>::B".
  expectResolution(
      Code, &HeuristicResolver::resolveDependentNameType,
      functionDecl(hasParameter(0, hasType(dependentNameType().bind("input")))),
      classTemplateDecl(
          has(cxxRecordDecl(has(cxxRecordDecl(hasName("B")).bind("output"))))));
}

TEST(HeuristicResolver, DependentNameType_Nested) {
  std::string Code = R"cpp(
    template <typename>
    struct A {
      struct B {
        struct C {};
      };
    };
    template <typename T>
    void foo(typename A<T>::B::C);
  )cpp";
  // Tests resolution of "C" in "A<T>::B::C".
  expectResolution(
      Code, &HeuristicResolver::resolveDependentNameType,
      functionDecl(hasParameter(0, hasType(dependentNameType().bind("input")))),
      classTemplateDecl(has(cxxRecordDecl(has(
          cxxRecordDecl(has(cxxRecordDecl(hasName("C")).bind("output"))))))));
}

TEST(HeuristicResolver, DependentNameType_Recursion) {
  std::string Code = R"cpp(
    template <int N>
    struct Waldo {
      using Type = typename Waldo<N - 1>::Type::Next;
    };
  )cpp";
  // Test resolution of "Next" in "typename Waldo<N - 1>::Type::Next".
  // Here, we are testing that we do not get into an infinite recursion.
  expectResolution(Code, &HeuristicResolver::resolveDependentNameType,
                   typeAliasDecl(hasType(dependentNameType().bind("input"))));
}

TEST(HeuristicResolver, DependentNameType_MutualRecursion) {
  std::string Code = R"cpp(
    template <int N>
    struct Odd;
    template <int N>
    struct Even {
      using Type = typename Odd<N - 1>::Type::Next;
    };
    template <int N>
    struct Odd {
      using Type = typename Even<N - 1>::Type::Next;
    };
  )cpp";
  // Test resolution of "Next" in "typename Even<N - 1>::Type::Next".
  // Similar to the above but we have two mutually recursive templates.
  expectResolution(
      Code, &HeuristicResolver::resolveDependentNameType,
      classTemplateDecl(hasName("Odd"),
                        has(cxxRecordDecl(has(typeAliasDecl(
                            hasType(dependentNameType().bind("input"))))))));
}

TEST(HeuristicResolver, NestedNameSpecifier) {
  // Test resolution of "B" in "A<T>::B::C".
  // Unlike the "C", the "B" does not get its own DependentNameTypeLoc node,
  // so the resolution uses the NestedNameSpecifier as input.
  std::string Code = R"cpp(
    template <typename>
    struct A {
      struct B {
        struct C {};
      };
    };
    template <typename T>
    void foo(typename A<T>::B::C);
  )cpp";
  // Adapt the call to resolveNestedNameSpecifierToType() to the interface
  // expected by expectResolution() (returning a vector of decls).
  ResolveFnT<NestedNameSpecifier> ResolveFn =
      [](const HeuristicResolver *H,
         const NestedNameSpecifier *NNS) -> std::vector<const NamedDecl *> {
    return {H->resolveNestedNameSpecifierToType(NNS)->getAsCXXRecordDecl()};
  };
  expectResolution(Code, ResolveFn,
                   nestedNameSpecifier(hasPrefix(specifiesType(hasDeclaration(
                                           classTemplateDecl(hasName("A"))))))
                       .bind("input"),
                   classTemplateDecl(has(cxxRecordDecl(
                       has(cxxRecordDecl(hasName("B")).bind("output"))))));
}

TEST(HeuristicResolver, TemplateSpecializationType) {
  std::string Code = R"cpp(
    template <typename>
    struct A {
      template <typename>
      struct B {};
    };
    template <typename T>
    void foo(typename A<T>::template B<int>);
  )cpp";
  // Test resolution of "B" in "A<T>::template B<int>".
  expectResolution(Code, &HeuristicResolver::resolveTemplateSpecializationType,
                   functionDecl(hasParameter(0, hasType(type().bind("input")))),
                   classTemplateDecl(has(cxxRecordDecl(
                       has(classTemplateDecl(hasName("B")).bind("output"))))));
}

TEST(HeuristicResolver, DependentCall_NonMember) {
  std::string Code = R"cpp(
    template <typename T>
    void nonmember(T);
    template <typename T>
    void bar(T t) {
      nonmember(t);
    }
  )cpp";
  // Test resolution of "nonmember" in "nonmember(t)".
  expectResolution(Code, &HeuristicResolver::resolveCalleeOfCallExpr,
                   callExpr(callee(unresolvedLookupExpr(hasAnyDeclaration(
                                functionTemplateDecl(hasName("nonmember"))))))
                       .bind("input"),
                   functionTemplateDecl(hasName("nonmember")).bind("output"));
}

TEST(HeuristicResolver, DependentCall_Member) {
  std::string Code = R"cpp(
    template <typename T>
    struct A {
      void member(T);
    };
    template <typename T>
    void bar(A<T> a, T t) {
      a.member(t);
    }
  )cpp";
  // Test resolution of "member" in "a.member(t)".
  expectResolution(
      Code, &HeuristicResolver::resolveCalleeOfCallExpr,
      callExpr(callee(cxxDependentScopeMemberExpr(hasMemberName("member"))))
          .bind("input"),
      cxxMethodDecl(hasName("member")).bind("output"));
}

TEST(HeuristicResolver, DependentCall_StaticMember) {
  std::string Code = R"cpp(
    template <typename T>
    struct A {
      static void static_member(T);
    };
    template <typename T>
    void bar(T t) {
      A<T>::static_member(t);
    }
  )cpp";
  // Test resolution of "static_member" in "A<T>::static_member(t)".
  expectResolution(Code, &HeuristicResolver::resolveCalleeOfCallExpr,
                   callExpr(callee(dependentScopeDeclRefExpr(
                                hasDependentName("static_member"))))
                       .bind("input"),
                   cxxMethodDecl(hasName("static_member")).bind("output"));
}

TEST(HeuristicResolver, DependentCall_Overload) {
  std::string Code = R"cpp(
    void overload(int);
    void overload(double);
    template <typename T>
    void bar(T t) {
      overload(t);
    }
  )cpp";
  // Test resolution of "overload" in "overload(t)". Both overload should be
  // found.
  expectResolution(Code, &HeuristicResolver::resolveCalleeOfCallExpr,
                   callExpr(callee(unresolvedLookupExpr(hasAnyDeclaration(
                                functionDecl(hasName("overload"))))))
                       .bind("input"),
                   functionDecl(hasName("overload"),
                                hasParameter(0, hasType(asString("double"))))
                       .bind("output"),
                   functionDecl(hasName("overload"),
                                hasParameter(0, hasType(asString("int"))))
                       .bind("output"));
}

TEST(HeuristicResolver, UsingValueDecl) {
  std::string Code = R"cpp(
    template <typename T>
    struct Base {
      void waldo();
    };
    template <typename T>
    struct Derived : Base<T> {
      using Base<T>::waldo;
    };
  )cpp";
  // Test resolution of "waldo" in "Base<T>::waldo".
  expectResolution(Code, &HeuristicResolver::resolveUsingValueDecl,
                   unresolvedUsingValueDecl(hasName("waldo")).bind("input"),
                   cxxMethodDecl(hasName("waldo")).bind("output"));
}

// `arg` is a ParamVarDecl*, `Expected` is a string
MATCHER_P(ParamNameMatcher, Expected, "paramNameMatcher") {
  EXPECT_TRUE(arg);
  if (IdentifierInfo *Ident = arg->getDeclName().getAsIdentifierInfo()) {
    return Ident->getName() == Expected;
  }
  return false;
}

// Helper function for testing HeuristicResolver::getProtoTypeLoc.
// Takes a matcher that selects a callee expression bound to the ID "input",
// calls getProtoTypeLoc() on it, and checks that the call found a
// FunctionProtoTypeLoc encoding the given parameter names.
template <typename InputMatcher, typename... ParameterNames>
void expectParameterNames(ASTContext &Ctx, const InputMatcher &IM,
                          ParameterNames... ExpectedParameterNames) {
  auto InputMatches = match(IM, Ctx);
  ASSERT_EQ(1u, InputMatches.size());
  const auto *Input = InputMatches[0].template getNodeAs<Expr>("input");
  ASSERT_TRUE(Input);

  HeuristicResolver H(Ctx);
  auto Loc = H.getFunctionProtoTypeLoc(Input);
  ASSERT_TRUE(Loc);
  EXPECT_THAT(Loc.getParams(),
              ElementsAre(ParamNameMatcher(ExpectedParameterNames)...));
}

TEST(HeuristicResolver, ProtoTypeLoc) {
  std::string Code = R"cpp(
    void (*f1)(int param1);
    void (__stdcall *f2)(int param2);
    using f3_t = void(*)(int param3);
    f3_t f3;
    using f4_t = void(__stdcall *)(int param4);
    f4_t f4;
    struct S {
      void (*f5)(int param5);
      using f6_t = void(*)(int param6);
      f6_t f6;
    };
    void bar() {
      f1(42);
      f2(42);
      f3(42);
      f4(42);
      S s;
      s.f5(42);
      s.f6(42);
    }
  )cpp";
  auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++20"});
  auto &Ctx = TU->getASTContext();
  auto checkFreeFunction = [&](llvm::StringRef FunctionName,
                               llvm::StringRef ParamName) {
    expectParameterNames(
        Ctx,
        callExpr(
            callee(implicitCastExpr(hasSourceExpression(declRefExpr(
                                        to(namedDecl(hasName(FunctionName))))))
                       .bind("input"))),
        ParamName);
  };
  checkFreeFunction("f1", "param1");
  checkFreeFunction("f2", "param2");
  checkFreeFunction("f3", "param3");
  checkFreeFunction("f4", "param4");
  auto checkMemberFunction = [&](llvm::StringRef MemberName,
                                 llvm::StringRef ParamName) {
    expectParameterNames(
        Ctx,
        callExpr(callee(implicitCastExpr(hasSourceExpression(memberExpr(
                                             member(hasName(MemberName)))))
                            .bind("input"))),
        ParamName);
  };
  checkMemberFunction("f5", "param5");
  checkMemberFunction("f6", "param6");
}

} // namespace
} // namespace clang