aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
blob: eb852ef5ee00b2df6fa33ba395aaf41ce901c029 (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
//===-- CallHierarchyTests.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 "Annotations.h"
#include "ParsedAST.h"
#include "TestFS.h"
#include "TestTU.h"
#include "TestWorkspace.h"
#include "XRefs.h"
#include "llvm/Support/Path.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {

llvm::raw_ostream &operator<<(llvm::raw_ostream &Stream,
                              const CallHierarchyItem &Item) {
  return Stream << Item.name << "@" << Item.selectionRange;
}

llvm::raw_ostream &operator<<(llvm::raw_ostream &Stream,
                              const CallHierarchyIncomingCall &Call) {
  Stream << "{ from: " << Call.from << ", ranges: [";
  for (const auto &R : Call.fromRanges) {
    Stream << R;
    Stream << ", ";
  }
  return Stream << "] }";
}

namespace {

using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Matcher;
using ::testing::UnorderedElementsAre;

// Helpers for matching call hierarchy data structures.
MATCHER_P(withName, N, "") { return arg.name == N; }
MATCHER_P(withDetail, N, "") { return arg.detail == N; }
MATCHER_P(withFile, N, "") { return arg.uri.file() == N; }
MATCHER_P(withSelectionRange, R, "") { return arg.selectionRange == R; }

template <class ItemMatcher>
::testing::Matcher<CallHierarchyIncomingCall> from(ItemMatcher M) {
  return Field(&CallHierarchyIncomingCall::from, M);
}
template <class ItemMatcher>
::testing::Matcher<CallHierarchyOutgoingCall> to(ItemMatcher M) {
  return Field(&CallHierarchyOutgoingCall::to, M);
}
template <class... RangeMatchers>
::testing::Matcher<CallHierarchyIncomingCall> iFromRanges(RangeMatchers... M) {
  return Field(&CallHierarchyIncomingCall::fromRanges,
               UnorderedElementsAre(M...));
}
template <class... RangeMatchers>
::testing::Matcher<CallHierarchyOutgoingCall> oFromRanges(RangeMatchers... M) {
  return Field(&CallHierarchyOutgoingCall::fromRanges,
               UnorderedElementsAre(M...));
}

TEST(CallHierarchy, IncomingOneFileCpp) {
  Annotations Source(R"cpp(
    void call^ee(int);
    void caller1() {
      $Callee[[callee]](42);
    }
    void caller2() {
      $Caller1A[[caller1]]();
      $Caller1B[[caller1]]();
    }
    void caller3() {
      $Caller1C[[caller1]]();
      $Caller2[[caller2]]();
    }
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("callee")));
  auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
  ASSERT_THAT(
      IncomingLevel1,
      ElementsAre(AllOf(from(AllOf(withName("caller1"), withDetail("caller1"))),
                        iFromRanges(Source.range("Callee")))));
  auto IncomingLevel2 = incomingCalls(IncomingLevel1[0].from, Index.get());
  ASSERT_THAT(
      IncomingLevel2,
      ElementsAre(AllOf(from(AllOf(withName("caller2"), withDetail("caller2"))),
                        iFromRanges(Source.range("Caller1A"),
                                    Source.range("Caller1B"))),
                  AllOf(from(AllOf(withName("caller3"), withDetail("caller3"))),
                        iFromRanges(Source.range("Caller1C")))));

  auto IncomingLevel3 = incomingCalls(IncomingLevel2[0].from, Index.get());
  ASSERT_THAT(
      IncomingLevel3,
      ElementsAre(AllOf(from(AllOf(withName("caller3"), withDetail("caller3"))),
                        iFromRanges(Source.range("Caller2")))));

  auto IncomingLevel4 = incomingCalls(IncomingLevel3[0].from, Index.get());
  EXPECT_THAT(IncomingLevel4, IsEmpty());
}

TEST(CallHierarchy, IncomingOneFileObjC) {
  Annotations Source(R"objc(
    @implementation MyClass {}
      +(void)call^ee {}
      +(void) caller1 {
        [MyClass $Callee[[callee]]];
      }
      +(void) caller2 {
        [MyClass $Caller1A[[caller1]]];
        [MyClass $Caller1B[[caller1]]];
      }
      +(void) caller3 {
        [MyClass $Caller1C[[caller1]]];
        [MyClass $Caller2[[caller2]]];
      }
    @end
  )objc");
  TestTU TU = TestTU::withCode(Source.code());
  TU.Filename = "TestTU.m";
  auto AST = TU.build();
  auto Index = TU.index();
  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("callee")));
  auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
  ASSERT_THAT(IncomingLevel1,
              ElementsAre(AllOf(from(AllOf(withName("caller1"),
                                           withDetail("MyClass::caller1"))),
                                iFromRanges(Source.range("Callee")))));
  auto IncomingLevel2 = incomingCalls(IncomingLevel1[0].from, Index.get());
  ASSERT_THAT(IncomingLevel2,
              ElementsAre(AllOf(from(AllOf(withName("caller2"),
                                           withDetail("MyClass::caller2"))),
                                iFromRanges(Source.range("Caller1A"),
                                            Source.range("Caller1B"))),
                          AllOf(from(AllOf(withName("caller3"),
                                           withDetail("MyClass::caller3"))),
                                iFromRanges(Source.range("Caller1C")))));

  auto IncomingLevel3 = incomingCalls(IncomingLevel2[0].from, Index.get());
  ASSERT_THAT(IncomingLevel3,
              ElementsAre(AllOf(from(AllOf(withName("caller3"),
                                           withDetail("MyClass::caller3"))),
                                iFromRanges(Source.range("Caller2")))));

  auto IncomingLevel4 = incomingCalls(IncomingLevel3[0].from, Index.get());
  EXPECT_THAT(IncomingLevel4, IsEmpty());
}

TEST(CallHierarchy, MainFileOnlyRef) {
  // In addition to testing that we store refs to main-file only symbols,
  // this tests that anonymous namespaces do not interfere with the
  // symbol re-identification process in callHierarchyItemToSymbo().
  Annotations Source(R"cpp(
    void call^ee(int);
    namespace {
      void caller1() {
        $Callee[[callee]](42);
      }
    }
    void caller2() {
      $Caller1[[caller1]]();
    }
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("callee")));
  auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
  ASSERT_THAT(
      IncomingLevel1,
      ElementsAre(AllOf(from(AllOf(withName("caller1"), withDetail("caller1"))),
                        iFromRanges(Source.range("Callee")))));

  auto IncomingLevel2 = incomingCalls(IncomingLevel1[0].from, Index.get());
  EXPECT_THAT(
      IncomingLevel2,
      ElementsAre(AllOf(from(AllOf(withName("caller2"), withDetail("caller2"))),
                        iFromRanges(Source.range("Caller1")))));
}

TEST(CallHierarchy, IncomingQualified) {
  Annotations Source(R"cpp(
    namespace ns {
    struct Waldo {
      void find();
    };
    void Waldo::find() {}
    void caller1(Waldo &W) {
      W.$Caller1[[f^ind]]();
    }
    void caller2(Waldo &W) {
      W.$Caller2[[find]]();
    }
    }
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("Waldo::find")));
  auto Incoming = incomingCalls(Items[0], Index.get());
  EXPECT_THAT(
      Incoming,
      ElementsAre(
          AllOf(from(AllOf(withName("caller1"), withDetail("ns::caller1"))),
                iFromRanges(Source.range("Caller1"))),
          AllOf(from(AllOf(withName("caller2"), withDetail("ns::caller2"))),
                iFromRanges(Source.range("Caller2")))));
}

TEST(CallHierarchy, OutgoingOneFile) {
  // Test outgoing call on the main file, with namespaces and methods
  Annotations Source(R"cpp(
    void callee(int);
    namespace ns {
      struct Foo {
        void caller1();
      };
      void Foo::caller1() {
        $Callee[[callee]](42);
      }
    }
    namespace {
      void caller2(ns::Foo& F) {
        F.$Caller1A[[caller1]]();
        F.$Caller1B[[caller1]]();
      }
    }
    void call^er3(ns::Foo& F) {
      F.$Caller1C[[caller1]]();
      $Caller2[[caller2]](F);
    }
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("caller3")));
  auto OugoingLevel1 = outgoingCalls(Items[0], Index.get());
  ASSERT_THAT(
      OugoingLevel1,
      ElementsAre(
          AllOf(to(AllOf(withName("caller1"), withDetail("ns::Foo::caller1"))),
                oFromRanges(Source.range("Caller1C"))),
          AllOf(to(AllOf(withName("caller2"), withDetail("caller2"))),
                oFromRanges(Source.range("Caller2")))));

  auto OutgoingLevel2 = outgoingCalls(OugoingLevel1[1].to, Index.get());
  ASSERT_THAT(
      OutgoingLevel2,
      ElementsAre(AllOf(
          to(AllOf(withName("caller1"), withDetail("ns::Foo::caller1"))),
          oFromRanges(Source.range("Caller1A"), Source.range("Caller1B")))));

  auto OutgoingLevel3 = outgoingCalls(OutgoingLevel2[0].to, Index.get());
  ASSERT_THAT(
      OutgoingLevel3,
      ElementsAre(AllOf(to(AllOf(withName("callee"), withDetail("callee"))),
                        oFromRanges(Source.range("Callee")))));

  auto OutgoingLevel4 = outgoingCalls(OutgoingLevel3[0].to, Index.get());
  EXPECT_THAT(OutgoingLevel4, IsEmpty());
}

TEST(CallHierarchy, MultiFileCpp) {
  // The test uses a .hh suffix for header files to get clang
  // to parse them in C++ mode. .h files are parsed in C mode
  // by default, which causes problems because e.g. symbol
  // USRs are different in C mode (do not include function signatures).

  Annotations CalleeH(R"cpp(
    void calle^e(int);
  )cpp");
  Annotations CalleeC(R"cpp(
    #include "callee.hh"
    void calle^e(int) {}
  )cpp");
  Annotations Caller1H(R"cpp(
    namespace nsa {
      void caller1();
    }
  )cpp");
  Annotations Caller1C(R"cpp(
    #include "callee.hh"
    #include "caller1.hh"
    namespace nsa {
      void caller1() {
        [[calle^e]](42);
      }
    }
  )cpp");
  Annotations Caller2H(R"cpp(
    namespace nsb {
      void caller2();
    }
  )cpp");
  Annotations Caller2C(R"cpp(
    #include "caller1.hh"
    #include "caller2.hh"
    namespace nsb {
      void caller2() {
        nsa::$A[[caller1]]();
        nsa::$B[[caller1]]();
      }
    }
  )cpp");
  Annotations Caller3H(R"cpp(
    namespace nsa {
      void call^er3();
    }
  )cpp");
  Annotations Caller3C(R"cpp(
    #include "caller1.hh"
    #include "caller2.hh"
    namespace nsa {
      void call^er3() {
        $Caller1[[caller1]]();
        nsb::$Caller2[[caller2]]();
      }
    }
  )cpp");

  TestWorkspace Workspace;
  Workspace.addSource("callee.hh", CalleeH.code());
  Workspace.addSource("caller1.hh", Caller1H.code());
  Workspace.addSource("caller2.hh", Caller2H.code());
  Workspace.addSource("caller3.hh", Caller3H.code());
  Workspace.addMainFile("callee.cc", CalleeC.code());
  Workspace.addMainFile("caller1.cc", Caller1C.code());
  Workspace.addMainFile("caller2.cc", Caller2C.code());
  Workspace.addMainFile("caller3.cc", Caller3C.code());

  auto Index = Workspace.index();

  auto CheckIncomingCalls = [&](ParsedAST &AST, Position Pos, PathRef TUPath) {
    std::vector<CallHierarchyItem> Items =
        prepareCallHierarchy(AST, Pos, TUPath);
    ASSERT_THAT(Items, ElementsAre(withName("callee")));
    auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
    ASSERT_THAT(IncomingLevel1,
                ElementsAre(AllOf(from(AllOf(withName("caller1"),
                                             withDetail("nsa::caller1"))),
                                  iFromRanges(Caller1C.range()))));

    auto IncomingLevel2 = incomingCalls(IncomingLevel1[0].from, Index.get());
    ASSERT_THAT(
        IncomingLevel2,
        ElementsAre(
            AllOf(from(AllOf(withName("caller2"), withDetail("nsb::caller2"))),
                  iFromRanges(Caller2C.range("A"), Caller2C.range("B"))),
            AllOf(from(AllOf(withName("caller3"), withDetail("nsa::caller3"))),
                  iFromRanges(Caller3C.range("Caller1")))));

    auto IncomingLevel3 = incomingCalls(IncomingLevel2[0].from, Index.get());
    ASSERT_THAT(IncomingLevel3,
                ElementsAre(AllOf(from(AllOf(withName("caller3"),
                                             withDetail("nsa::caller3"))),
                                  iFromRanges(Caller3C.range("Caller2")))));

    auto IncomingLevel4 = incomingCalls(IncomingLevel3[0].from, Index.get());
    EXPECT_THAT(IncomingLevel4, IsEmpty());
  };

  auto CheckOutgoingCalls = [&](ParsedAST &AST, Position Pos, PathRef TUPath,
                                bool IsDeclaration) {
    std::vector<CallHierarchyItem> Items =
        prepareCallHierarchy(AST, Pos, TUPath);
    ASSERT_THAT(
        Items,
        ElementsAre(AllOf(
            withName("caller3"),
            withFile(testPath(IsDeclaration ? "caller3.hh" : "caller3.cc")))));
    auto OutgoingLevel1 = outgoingCalls(Items[0], Index.get());
    ASSERT_THAT(
        OutgoingLevel1,
        // fromRanges are interpreted in the context of Items[0]'s file.
        // If that's the header, we can't get ranges from the implementation
        // file!
        ElementsAre(
            AllOf(to(AllOf(withName("caller1"), withDetail("nsa::caller1"))),
                  IsDeclaration ? oFromRanges()
                                : oFromRanges(Caller3C.range("Caller1"))),
            AllOf(to(AllOf(withName("caller2"), withDetail("nsb::caller2"))),
                  IsDeclaration ? oFromRanges()
                                : oFromRanges(Caller3C.range("Caller2")))));

    auto OutgoingLevel2 = outgoingCalls(OutgoingLevel1[1].to, Index.get());
    ASSERT_THAT(OutgoingLevel2,
                ElementsAre(AllOf(
                    to(AllOf(withName("caller1"), withDetail("nsa::caller1"))),
                    oFromRanges(Caller2C.range("A"), Caller2C.range("B")))));

    auto OutgoingLevel3 = outgoingCalls(OutgoingLevel2[0].to, Index.get());
    ASSERT_THAT(
        OutgoingLevel3,
        ElementsAre(AllOf(to(AllOf(withName("callee"), withDetail("callee"))),
                          oFromRanges(Caller1C.range()))));

    auto OutgoingLevel4 = outgoingCalls(OutgoingLevel3[0].to, Index.get());
    EXPECT_THAT(OutgoingLevel4, IsEmpty());
  };

  // Check that invoking from a call site works.
  auto AST = Workspace.openFile("caller1.cc");
  ASSERT_TRUE(bool(AST));
  CheckIncomingCalls(*AST, Caller1C.point(), testPath("caller1.cc"));

  // Check that invoking from the declaration site works.
  AST = Workspace.openFile("callee.hh");
  ASSERT_TRUE(bool(AST));
  CheckIncomingCalls(*AST, CalleeH.point(), testPath("callee.hh"));
  AST = Workspace.openFile("caller3.hh");
  ASSERT_TRUE(bool(AST));
  CheckOutgoingCalls(*AST, Caller3H.point(), testPath("caller3.hh"), true);

  // Check that invoking from the definition site works.
  AST = Workspace.openFile("callee.cc");
  ASSERT_TRUE(bool(AST));
  CheckIncomingCalls(*AST, CalleeC.point(), testPath("callee.cc"));
  AST = Workspace.openFile("caller3.cc");
  ASSERT_TRUE(bool(AST));
  CheckOutgoingCalls(*AST, Caller3C.point(), testPath("caller3.cc"), false);
}

TEST(CallHierarchy, IncomingMultiFileObjC) {
  // The test uses a .mi suffix for header files to get clang
  // to parse them in ObjC mode. .h files are parsed in C mode
  // by default, which causes problems because e.g. symbol
  // USRs are different in C mode (do not include function signatures).

  Annotations CalleeH(R"objc(
    @interface CalleeClass
      +(void)call^ee;
    @end
  )objc");
  Annotations CalleeC(R"objc(
    #import "callee.mi"
    @implementation CalleeClass {}
      +(void)call^ee {}
    @end
  )objc");
  Annotations Caller1H(R"objc(
    @interface Caller1Class
      +(void)caller1;
    @end
  )objc");
  Annotations Caller1C(R"objc(
    #import "callee.mi"
    #import "caller1.mi"
    @implementation Caller1Class {}
      +(void)caller1 {
        [CalleeClass [[calle^e]]];
      }
    @end
  )objc");
  Annotations Caller2H(R"objc(
    @interface Caller2Class
      +(void)caller2;
    @end
  )objc");
  Annotations Caller2C(R"objc(
    #import "caller1.mi"
    #import "caller2.mi"
    @implementation Caller2Class {}
      +(void)caller2 {
        [Caller1Class $A[[caller1]]];
        [Caller1Class $B[[caller1]]];
      }
    @end
  )objc");
  Annotations Caller3C(R"objc(
    #import "caller1.mi"
    #import "caller2.mi"
    @implementation Caller3Class {}
      +(void)caller3 {
        [Caller1Class $Caller1[[caller1]]];
        [Caller2Class $Caller2[[caller2]]];
      }
    @end
  )objc");

  TestWorkspace Workspace;
  Workspace.addSource("callee.mi", CalleeH.code());
  Workspace.addSource("caller1.mi", Caller1H.code());
  Workspace.addSource("caller2.mi", Caller2H.code());
  Workspace.addMainFile("callee.m", CalleeC.code());
  Workspace.addMainFile("caller1.m", Caller1C.code());
  Workspace.addMainFile("caller2.m", Caller2C.code());
  Workspace.addMainFile("caller3.m", Caller3C.code());
  auto Index = Workspace.index();

  auto CheckCallHierarchy = [&](ParsedAST &AST, Position Pos, PathRef TUPath) {
    std::vector<CallHierarchyItem> Items =
        prepareCallHierarchy(AST, Pos, TUPath);
    ASSERT_THAT(Items, ElementsAre(withName("callee")));
    auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
    ASSERT_THAT(IncomingLevel1,
                ElementsAre(AllOf(from(withName("caller1")),
                                  iFromRanges(Caller1C.range()))));

    auto IncomingLevel2 = incomingCalls(IncomingLevel1[0].from, Index.get());
    ASSERT_THAT(IncomingLevel2,
                ElementsAre(AllOf(from(withName("caller2")),
                                  iFromRanges(Caller2C.range("A"),
                                              Caller2C.range("B"))),
                            AllOf(from(withName("caller3")),
                                  iFromRanges(Caller3C.range("Caller1")))));

    auto IncomingLevel3 = incomingCalls(IncomingLevel2[0].from, Index.get());
    ASSERT_THAT(IncomingLevel3,
                ElementsAre(AllOf(from(withName("caller3")),
                                  iFromRanges(Caller3C.range("Caller2")))));

    auto IncomingLevel4 = incomingCalls(IncomingLevel3[0].from, Index.get());
    EXPECT_THAT(IncomingLevel4, IsEmpty());
  };

  // Check that invoking from a call site works.
  auto AST = Workspace.openFile("caller1.m");
  ASSERT_TRUE(bool(AST));
  CheckCallHierarchy(*AST, Caller1C.point(), testPath("caller1.m"));

  // Check that invoking from the declaration site works.
  AST = Workspace.openFile("callee.mi");
  ASSERT_TRUE(bool(AST));
  CheckCallHierarchy(*AST, CalleeH.point(), testPath("callee.mi"));

  // Check that invoking from the definition site works.
  AST = Workspace.openFile("callee.m");
  ASSERT_TRUE(bool(AST));
  CheckCallHierarchy(*AST, CalleeC.point(), testPath("callee.m"));
}

TEST(CallHierarchy, CallInLocalVarDecl) {
  // Tests that local variable declarations are not treated as callers
  // (they're not indexed, so they can't be represented as call hierarchy
  // items); instead, the caller should be the containing function.
  // However, namespace-scope variable declarations should be treated as
  // callers because those are indexed and there is no enclosing entity
  // that would be a useful caller.
  Annotations Source(R"cpp(
    int call^ee();
    void caller1() {
      $call1[[callee]]();
    }
    void caller2() {
      int localVar = $call2[[callee]]();
    }
    int caller3 = $call3[[callee]]();
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("callee")));

  auto Incoming = incomingCalls(Items[0], Index.get());
  ASSERT_THAT(Incoming, ElementsAre(AllOf(from(withName("caller1")),
                                          iFromRanges(Source.range("call1"))),
                                    AllOf(from(withName("caller2")),
                                          iFromRanges(Source.range("call2"))),
                                    AllOf(from(withName("caller3")),
                                          iFromRanges(Source.range("call3")))));
}

TEST(CallHierarchy, HierarchyOnField) {
  // Tests that the call hierarchy works on fields.
  Annotations Source(R"cpp(
    struct Vars {
      int v^ar1 = 1;
    };
    void caller() {
      Vars values;
      values.$Callee[[var1]];
    }
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("var1")));
  auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
  ASSERT_THAT(IncomingLevel1,
              ElementsAre(AllOf(from(withName("caller")),
                                iFromRanges(Source.range("Callee")))));
}

TEST(CallHierarchy, HierarchyOnVar) {
  // Tests that the call hierarchy works on non-local variables.
  Annotations Source(R"cpp(
    int v^ar = 1;
    void caller() {
      $Callee[[var]];
    }
  )cpp");
  TestTU TU = TestTU::withCode(Source.code());
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("var")));
  auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
  ASSERT_THAT(IncomingLevel1,
              ElementsAre(AllOf(from(withName("caller")),
                                iFromRanges(Source.range("Callee")))));
}

TEST(CallHierarchy, CallInDifferentFileThanCaller) {
  Annotations Header(R"cpp(
    #define WALDO void caller() {
  )cpp");
  Annotations Source(R"cpp(
    void call^ee();
    WALDO
      callee();
    }
  )cpp");
  auto TU = TestTU::withCode(Source.code());
  TU.HeaderCode = Header.code();
  auto AST = TU.build();
  auto Index = TU.index();

  std::vector<CallHierarchyItem> Items =
      prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
  ASSERT_THAT(Items, ElementsAre(withName("callee")));

  auto Incoming = incomingCalls(Items[0], Index.get());

  // The only call site is in the source file, which is a different file from
  // the declaration of the function containing the call, which is in the
  // header. The protocol does not allow us to represent such calls, so we drop
  // them. (The call hierarchy item itself is kept.)
  EXPECT_THAT(Incoming,
              ElementsAre(AllOf(from(withName("caller")), iFromRanges())));
}

} // namespace
} // namespace clangd
} // namespace clang