Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
//===-- RenameClassTest.cpp - unit tests for renaming classes -------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ClangRenameTest.h"

namespace clang {
namespace clang_rename {
namespace test {
namespace {

class RenameClassTest : public ClangRenameTest {
public:
  RenameClassTest() {
    AppendToHeader(R"(
      namespace a {
        class Foo {
          public:
            struct Nested {
              enum NestedEnum {E1, E2};
            };
            void func() {}
          static int Constant;
        };
        class Goo {
          public:
            struct Nested {
              enum NestedEnum {E1, E2};
            };
        };
        int Foo::Constant = 1;
      } // namespace a
      namespace b {
      class Foo {};
      } // namespace b

      #define MACRO(x) x

      template<typename T> class ptr {};
    )");
  }
};

INSTANTIATE_TEST_CASE_P(
    RenameClassTests, RenameClassTest,
    testing::ValuesIn(std::vector<Case>({
        // basic classes
        {"a::Foo f;", "b::Bar f;", "", ""},
        {"::a::Foo f;", "::b::Bar f;", "", ""},
        {"void f(a::Foo f) {}", "void f(b::Bar f) {}", "", ""},
        {"void f(a::Foo *f) {}", "void f(b::Bar *f) {}", "", ""},
        {"a::Foo f() { return a::Foo(); }", "b::Bar f() { return b::Bar(); }",
         "", ""},
        {"namespace a {a::Foo f() { return Foo(); }}",
         "namespace a {b::Bar f() { return b::Bar(); }}", "", ""},
        {"void f(const a::Foo& a1) {}", "void f(const b::Bar& a1) {}", "", ""},
        {"void f(const a::Foo* a1) {}", "void f(const b::Bar* a1) {}", "", ""},
        {"namespace a { void f(Foo a1) {} }",
         "namespace a { void f(b::Bar a1) {} }", "", ""},
        {"void f(MACRO(a::Foo) a1) {}", "void f(MACRO(b::Bar) a1) {}", "", ""},
        {"void f(MACRO(a::Foo a1)) {}", "void f(MACRO(b::Bar a1)) {}", "", ""},
        {"a::Foo::Nested ns;", "b::Bar::Nested ns;", "", ""},
        {"auto t = a::Foo::Constant;", "auto t = b::Bar::Constant;", "", ""},
        {"a::Foo::Nested ns;", "a::Foo::Nested2 ns;", "a::Foo::Nested",
         "a::Foo::Nested2"},

        // use namespace and typedefs
        {"using a::Foo; Foo gA;", "using b::Bar; b::Bar gA;", "", ""},
        {"using a::Foo; void f(Foo gA) {}", "using b::Bar; void f(Bar gA) {}",
         "", ""},
        {"using a::Foo; namespace x { Foo gA; }",
         "using b::Bar; namespace x { Bar gA; }", "", ""},
        {"struct S { using T = a::Foo; T a_; };",
         "struct S { using T = b::Bar; T a_; };", "", ""},
        {"using T = a::Foo; T gA;", "using T = b::Bar; T gA;", "", ""},
        {"typedef a::Foo T; T gA;", "typedef b::Bar T; T gA;", "", ""},
        {"typedef MACRO(a::Foo) T; T gA;", "typedef MACRO(b::Bar) T; T gA;", "",
         ""},

        // struct members and other oddities
        {"struct S : public a::Foo {};", "struct S : public b::Bar {};", "",
         ""},
        {"struct F { void f(a::Foo a1) {} };",
         "struct F { void f(b::Bar a1) {} };", "", ""},
        {"struct F { a::Foo a_; };", "struct F { b::Bar a_; };", "", ""},
        {"struct F { ptr<a::Foo> a_; };", "struct F { ptr<b::Bar> a_; };", "",
         ""},

        {"void f() { a::Foo::Nested ne; }", "void f() { b::Bar::Nested ne; }",
         "", ""},
        {"void f() { a::Goo::Nested ne; }", "void f() { a::Goo::Nested ne; }",
         "", ""},
        {"void f() { a::Foo::Nested::NestedEnum e; }",
         "void f() { b::Bar::Nested::NestedEnum e; }", "", ""},
        {"void f() { auto e = a::Foo::Nested::NestedEnum::E1; }",
         "void f() { auto e = b::Bar::Nested::NestedEnum::E1; }", "", ""},
        {"void f() { auto e = a::Foo::Nested::E1; }",
         "void f() { auto e = b::Bar::Nested::E1; }", "", ""},

        // templates
        {"template <typename T> struct Foo { T t; };\n"
         "void f() { Foo<a::Foo> foo; }",
         "template <typename T> struct Foo { T t; };\n"
         "void f() { Foo<b::Bar> foo; }",
         "", ""},
        {"template <typename T> struct Foo { a::Foo a; };",
         "template <typename T> struct Foo { b::Bar a; };", "", ""},
        {"template <typename T> void f(T t) {}\n"
         "void g() { f<a::Foo>(a::Foo()); }",
         "template <typename T> void f(T t) {}\n"
         "void g() { f<b::Bar>(b::Bar()); }",
         "", ""},
        {"template <typename T> int f() { return 1; }\n"
         "template <> int f<a::Foo>() { return 2; }\n"
         "int g() { return f<a::Foo>(); }",
         "template <typename T> int f() { return 1; }\n"
         "template <> int f<b::Bar>() { return 2; }\n"
         "int g() { return f<b::Bar>(); }",
         "", ""},
        {"struct Foo { template <typename T> T foo(); };\n"
         "void g() { Foo f;  auto a = f.template foo<a::Foo>(); }",
         "struct Foo { template <typename T> T foo(); };\n"
         "void g() { Foo f;  auto a = f.template foo<b::Bar>(); }",
         "", ""},

        // The following two templates are distilled from regressions found in
        // unique_ptr<> and type_traits.h
        {"template <typename T> struct outer {\n"
         "     typedef T type;\n"
         "     type Baz();\n"
         "    };\n"
         "    outer<a::Foo> g_A;",
         "template <typename T> struct outer {\n"
         "      typedef T type;\n"
         "      type Baz();\n"
         "    };\n"
         "    outer<b::Bar> g_A;",
         "", ""},
        {"template <typename T> struct nested { typedef T type; };\n"
         "template <typename T> struct outer { typename nested<T>::type Foo(); "
         "};\n"
         "outer<a::Foo> g_A;",
         "template <typename T> struct nested { typedef T type; };\n"
         "template <typename T> struct outer { typename nested<T>::type Foo(); "
         "};\n"
         "outer<b::Bar> g_A;",
         "", ""},

        // macros
        {"#define FOO(T, t) T t\n"
         "void f() { FOO(a::Foo, a1); FOO(a::Foo, a2); }",
         "#define FOO(T, t) T t\n"
         "void f() { FOO(b::Bar, a1); FOO(b::Bar, a2); }",
         "", ""},
        {"#define FOO(n) a::Foo n\n"
         " void f() { FOO(a1); FOO(a2); }",
         "#define FOO(n) b::Bar n\n"
         " void f() { FOO(a1); FOO(a2); }",
         "", ""},

        // Pointer to member functions
        {"auto gA = &a::Foo::func;", "auto gA = &b::Bar::func;", "", ""},
        {"using a::Foo; auto gA = &Foo::func;",
         "using b::Bar; auto gA = &b::Bar::func;", "", ""},
        {"using a::Foo; namespace x { auto gA = &Foo::func; }",
         "using b::Bar; namespace x { auto gA = &Bar::func; }", "", ""},
        {"typedef a::Foo T; auto gA = &T::func;",
         "typedef b::Bar T; auto gA = &T::func;", "", ""},
        {"auto gA = &MACRO(a::Foo)::func;", "auto gA = &MACRO(b::Bar)::func;",
         "", ""},

        // Short match inside a namespace
        {"namespace a { void f(Foo a1) {} }",
         "namespace a { void f(b::Bar a1) {} }", "", ""},

        // Correct match.
        {"using a::Foo; struct F { ptr<Foo> a_; };",
         "using b::Bar; struct F { ptr<Bar> a_; };", "", ""},

        // avoid false positives
        {"void f(b::Foo a) {}", "void f(b::Foo a) {}", "", ""},
        {"namespace b { void f(Foo a) {} }", "namespace b { void f(Foo a) {} }",
         "", ""},

        // friends, everyone needs friends.
        {"class Foo { int i; friend class a::Foo; };",
         "class Foo { int i; friend class b::Bar; };", "", ""},
    })), );

TEST_P(RenameClassTest, RenameClasses) {
  auto Param = GetParam();
  std::string OldName = Param.OldName.empty() ? "a::Foo" : Param.OldName;
  std::string NewName = Param.NewName.empty() ? "b::Bar" : Param.NewName;
  std::string Actual = runClangRenameOnCode(Param.Before, OldName, NewName);
  CompareSnippets(Param.After, Actual);
}

class NamespaceDetectionTest : public ClangRenameTest {
protected:
  NamespaceDetectionTest() {
    AppendToHeader(R"(
         class Old {};
         namespace o1 {
         class Old {};
         namespace o2 {
         class Old {};
         namespace o3 {
         class Old {};
         }  // namespace o3
         }  // namespace o2
         }  // namespace o1
     )");
  }
};

INSTANTIATE_TEST_CASE_P(
    RenameClassTest, NamespaceDetectionTest,
    ::testing::ValuesIn(std::vector<Case>({
        // Test old and new namespace overlap.
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { New moo; } } }",
         "o1::o2::o3::Old", "o1::o2::o3::New"},
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { n3::New moo; } } }",
         "o1::o2::o3::Old", "o1::o2::n3::New"},
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { n2::n3::New moo; } } }",
         "o1::o2::o3::Old", "o1::n2::n3::New"},
        {"namespace o1 { namespace o2 { Old moo; } }",
         "namespace o1 { namespace o2 { New moo; } }", "::o1::o2::Old",
         "::o1::o2::New"},
        {"namespace o1 { namespace o2 { Old moo; } }",
         "namespace o1 { namespace o2 { n2::New moo; } }", "::o1::o2::Old",
         "::o1::n2::New"},
        {"namespace o1 { namespace o2 { Old moo; } }",
         "namespace o1 { namespace o2 { ::n1::n2::New moo; } }",
         "::o1::o2::Old", "::n1::n2::New"},
        {"namespace o1 { namespace o2 { Old moo; } }",
         "namespace o1 { namespace o2 { n1::n2::New moo; } }", "::o1::o2::Old",
         "n1::n2::New"},

        // Test old and new namespace with differing depths.
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { New moo; } } }",
         "o1::o2::o3::Old", "::o1::New"},
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { New moo; } } }",
         "o1::o2::o3::Old", "::o1::o2::New"},
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { New moo; } } }",
         "o1::o2::o3::Old", "o1::New"},
        {"namespace o1 { namespace o2 { namespace o3 { Old moo; } } }",
         "namespace o1 { namespace o2 { namespace o3 { New moo; } } }",
         "o1::o2::o3::Old", "o1::o2::New"},
        {"Old moo;", "o1::New moo;", "::Old", "o1::New"},
        {"Old moo;", "o1::New moo;", "Old", "o1::New"},
        {"namespace o1 { ::Old moo; }", "namespace o1 { New moo; }", "Old",
         "o1::New"},
        {"namespace o1 { namespace o2 {  Old moo; } }",
         "namespace o1 { namespace o2 {  ::New moo; } }", "::o1::o2::Old",
         "::New"},
        {"namespace o1 { namespace o2 {  Old moo; } }",
         "namespace o1 { namespace o2 {  New moo; } }", "::o1::o2::Old", "New"},

        // Test moving into the new namespace at different levels.
        {"namespace n1 { namespace n2 { o1::o2::Old moo; } }",
         "namespace n1 { namespace n2 { New moo; } }", "::o1::o2::Old",
         "::n1::n2::New"},
        {"namespace n1 { namespace n2 { o1::o2::Old moo; } }",
         "namespace n1 { namespace n2 { New moo; } }", "::o1::o2::Old",
         "n1::n2::New"},
        {"namespace n1 { namespace n2 { o1::o2::Old moo; } }",
         "namespace n1 { namespace n2 { o2::New moo; } }", "::o1::o2::Old",
         "::n1::o2::New"},
        {"namespace n1 { namespace n2 { o1::o2::Old moo; } }",
         "namespace n1 { namespace n2 { o2::New moo; } }", "::o1::o2::Old",
         "n1::o2::New"},
        {"namespace n1 { namespace n2 { o1::o2::Old moo; } }",
         "namespace n1 { namespace n2 { ::o1::o2::New moo; } }",
         "::o1::o2::Old", "::o1::o2::New"},
        {"namespace n1 { namespace n2 { o1::o2::Old moo; } }",
         "namespace n1 { namespace n2 { o1::o2::New moo; } }", "::o1::o2::Old",
         "o1::o2::New"},

        // Test friends declarations.
        {"class Foo { friend class o1::Old; };",
         "class Foo { friend class o1::New; };", "o1::Old", "o1::New"},
        {"class Foo { int i; friend class o1::Old; };",
         "class Foo { int i; friend class ::o1::New; };", "::o1::Old",
         "::o1::New"},
        {"namespace o1 { class Foo { int i; friend class Old; }; }",
         "namespace o1 { class Foo { int i; friend class New; }; }", "o1::Old",
         "o1::New"},
        {"namespace o1 { class Foo { int i; friend class Old; }; }",
         "namespace o1 { class Foo { int i; friend class New; }; }",
         "::o1::Old", "::o1::New"},
    })), );

TEST_P(NamespaceDetectionTest, RenameClasses) {
  auto Param = GetParam();
  std::string Actual =
      runClangRenameOnCode(Param.Before, Param.OldName, Param.NewName);
  CompareSnippets(Param.After, Actual);
}

class TemplatedClassRenameTest : public ClangRenameTest {
protected:
  TemplatedClassRenameTest() {
    AppendToHeader(R"(
           template <typename T> struct Old {
             T t_;
             T f() { return T(); };
             static T s(T t) { return t; }
           };
           namespace ns {
           template <typename T> struct Old {
             T t_;
             T f() { return T(); };
             static T s(T t) { return t; }
           };
           }  // namespace ns

           namespace o1 {
           namespace o2 {
           namespace o3 {
           template <typename T> struct Old {
             T t_;
             T f() { return T(); };
             static T s(T t) { return t; }
           };
           }  // namespace o3
           }  // namespace o2
           }  // namespace o1
       )");
  }
};

INSTANTIATE_TEST_CASE_P(
    RenameClassTests, TemplatedClassRenameTest,
    ::testing::ValuesIn(std::vector<Case>({
        {"Old<int> gI; Old<bool> gB;", "New<int> gI; New<bool> gB;", "Old",
         "New"},
        {"ns::Old<int> gI; ns::Old<bool> gB;",
         "ns::New<int> gI; ns::New<bool> gB;", "ns::Old", "ns::New"},
        {"auto gI = &Old<int>::f; auto gB = &Old<bool>::f;",
         "auto gI = &New<int>::f; auto gB = &New<bool>::f;", "Old", "New"},
        {"auto gI = &ns::Old<int>::f;", "auto gI = &ns::New<int>::f;",
         "ns::Old", "ns::New"},

        {"int gI = Old<int>::s(0); bool gB = Old<bool>::s(false);",
         "int gI = New<int>::s(0); bool gB = New<bool>::s(false);", "Old",
         "New"},
        {"int gI = ns::Old<int>::s(0); bool gB = ns::Old<bool>::s(false);",
         "int gI = ns::New<int>::s(0); bool gB = ns::New<bool>::s(false);",
         "ns::Old", "ns::New"},

        {"struct S { Old<int*> o_; };", "struct S { New<int*> o_; };", "Old",
         "New"},
        {"struct S { ns::Old<int*> o_; };", "struct S { ns::New<int*> o_; };",
         "ns::Old", "ns::New"},

        {"auto a = reinterpret_cast<Old<int>*>(new Old<int>);",
         "auto a = reinterpret_cast<New<int>*>(new New<int>);", "Old", "New"},
        {"auto a = reinterpret_cast<ns::Old<int>*>(new ns::Old<int>);",
         "auto a = reinterpret_cast<ns::New<int>*>(new ns::New<int>);",
         "ns::Old", "ns::New"},
        {"auto a = reinterpret_cast<const Old<int>*>(new Old<int>);",
         "auto a = reinterpret_cast<const New<int>*>(new New<int>);", "Old",
         "New"},
        {"auto a = reinterpret_cast<const ns::Old<int>*>(new ns::Old<int>);",
         "auto a = reinterpret_cast<const ns::New<int>*>(new ns::New<int>);",
         "ns::Old", "ns::New"},

        {"Old<bool>& foo();", "New<bool>& foo();", "Old", "New"},
        {"ns::Old<bool>& foo();", "ns::New<bool>& foo();", "ns::Old",
         "ns::New"},
        {"o1::o2::o3::Old<bool>& foo();", "o1::o2::o3::New<bool>& foo();",
         "o1::o2::o3::Old", "o1::o2::o3::New"},
        {"namespace ns { Old<bool>& foo(); }",
         "namespace ns { New<bool>& foo(); }", "ns::Old", "ns::New"},
        {"const Old<bool>& foo();", "const New<bool>& foo();", "Old", "New"},
        {"const ns::Old<bool>& foo();", "const ns::New<bool>& foo();",
         "ns::Old", "ns::New"},

        // FIXME: figure out why this only works when Moo gets
        // specialized at some point.
        {"template <typename T> struct Moo { Old<T> o_; }; Moo<int> m;",
         "template <typename T> struct Moo { New<T> o_; }; Moo<int> m;", "Old",
         "New"},
        {"template <typename T> struct Moo { ns::Old<T> o_; }; Moo<int> m;",
         "template <typename T> struct Moo { ns::New<T> o_; }; Moo<int> m;",
         "ns::Old", "ns::New"},
    })), );

TEST_P(TemplatedClassRenameTest, RenameTemplateClasses) {
  auto Param = GetParam();
  std::string Actual =
      runClangRenameOnCode(Param.Before, Param.OldName, Param.NewName);
  CompareSnippets(Param.After, Actual);
}

TEST_F(ClangRenameTest, RenameClassWithOutOfLineMembers) {
  std::string Before = R"(
      class Old {
       public:
        Old();
        ~Old();

        Old* next();

       private:
        Old* next_;
      };

      Old::Old() {}
      Old::~Old() {}
      Old* Old::next() { return next_; }
    )";
  std::string Expected = R"(
      class New {
       public:
        New();
        ~New();

        New* next();

       private:
        New* next_;
      };

      New::New() {}
      New::~New() {}
      New* New::next() { return next_; }
    )";
  std::string After = runClangRenameOnCode(Before, "Old", "New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, RenameClassWithInlineMembers) {
  std::string Before = R"(
      class Old {
       public:
        Old() {}
        ~Old() {}

        Old* next() { return next_; }

       private:
        Old* next_;
      };
    )";
  std::string Expected = R"(
      class New {
       public:
        New() {}
        ~New() {}

        New* next() { return next_; }

       private:
        New* next_;
      };
    )";
  std::string After = runClangRenameOnCode(Before, "Old", "New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, RenameClassWithNamespaceWithInlineMembers) {
  std::string Before = R"(
      namespace ns {
      class Old {
       public:
        Old() {}
        ~Old() {}

        Old* next() { return next_; }

       private:
        Old* next_;
      };
      }  // namespace ns
    )";
  std::string Expected = R"(
      namespace ns {
      class New {
       public:
        New() {}
        ~New() {}

        New* next() { return next_; }

       private:
        New* next_;
      };
      }  // namespace ns
    )";
  std::string After = runClangRenameOnCode(Before, "ns::Old", "ns::New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, RenameClassWithNamespaceWithOutOfInlineMembers) {
  std::string Before = R"(
      namespace ns {
      class Old {
       public:
        Old();
        ~Old();

        Old* next();

       private:
        Old* next_;
      };

      Old::Old() {}
      Old::~Old() {}
      Old* Old::next() { return next_; }
      }  // namespace ns
    )";
  std::string Expected = R"(
      namespace ns {
      class New {
       public:
        New();
        ~New();

        New* next();

       private:
        New* next_;
      };

      New::New() {}
      New::~New() {}
      New* New::next() { return next_; }
      }  // namespace ns
    )";
  std::string After = runClangRenameOnCode(Before, "ns::Old", "ns::New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, RenameClassInInheritedConstructor) {
  // `using Base::Base;` will generate an implicit constructor containing usage
  // of `::ns::Old` which should not be matched.
  std::string Before = R"(
      namespace ns {
      class Old;
      class Old {
        int x;
      };
      class Base {
       protected:
        Old *moo_;
       public:
        Base(Old *moo) : moo_(moo) {}
      };
      class Derived : public Base {
       public:
         using Base::Base;
      };
      }  // namespace ns
      int main() {
        ::ns::Old foo;
        ::ns::Derived d(&foo);
        return 0;
      })";
  std::string Expected = R"(
      namespace ns {
      class New;
      class New {
        int x;
      };
      class Base {
       protected:
        New *moo_;
       public:
        Base(New *moo) : moo_(moo) {}
      };
      class Derived : public Base {
       public:
         using Base::Base;
      };
      }  // namespace ns
      int main() {
        ::ns::New foo;
        ::ns::Derived d(&foo);
        return 0;
      })";
  std::string After = runClangRenameOnCode(Before, "ns::Old", "ns::New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, DontRenameReferencesInImplicitFunction) {
  std::string Before = R"(
      namespace ns {
      class Old {
      };
      } // namespace ns
      struct S {
        int y;
        ns::Old old;
      };
      void f() {
        S s1, s2, s3;
        // This causes an implicit assignment operator to be created.
        s1 = s2 = s3;
      }
      )";
  std::string Expected = R"(
      namespace ns {
      class New {
      };
      } // namespace ns
      struct S {
        int y;
        ::new_ns::New old;
      };
      void f() {
        S s1, s2, s3;
        // This causes an implicit assignment operator to be created.
        s1 = s2 = s3;
      }
      )";
  std::string After = runClangRenameOnCode(Before, "ns::Old", "::new_ns::New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, ReferencesInLambdaFunctionParameters) {
  std::string Before = R"(
      template <class T>
      class function;
      template <class R, class... ArgTypes>
      class function<R(ArgTypes...)> {
      public:
        template <typename Functor>
        function(Functor f) {}

        function() {}

        R operator()(ArgTypes...) const {}
      };

      namespace ns {
      class Old {};
      void f() {
        function<void(Old)> func;
      }
      }  // namespace ns)";
  std::string Expected = R"(
      template <class T>
      class function;
      template <class R, class... ArgTypes>
      class function<R(ArgTypes...)> {
      public:
        template <typename Functor>
        function(Functor f) {}

        function() {}

        R operator()(ArgTypes...) const {}
      };

      namespace ns {
      class New {};
      void f() {
        function<void(::new_ns::New)> func;
      }
      }  // namespace ns)";
  std::string After = runClangRenameOnCode(Before, "ns::Old", "::new_ns::New");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, DontChangeIfSameName) {
  std::string Before = R"(
      namespace foo {
      class Old {
       public:
         static void foo() {}
      };
      }

      void f(foo::Old * x) {
        foo::Old::foo() ;
      }
      using foo::Old;)";
  std::string Expected = R"(
      namespace foo {
      class Old {
       public:
         static void foo() {}
      };
      }

      void f(foo::Old * x) {
        foo::Old::foo() ;
      }
      using foo::Old;)";
  std::string After = runClangRenameOnCode(Before, "foo::Old", "foo::Old");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, ChangeIfNewNameWithLeadingDotDot) {
  std::string Before = R"(
      namespace foo {
      class Old {
       public:
         static void foo() {}
      };
      }

      void f(foo::Old * x) {
        foo::Old::foo() ;
      }
      using foo::Old;)";
  std::string Expected = R"(
      namespace foo {
      class Old {
       public:
         static void foo() {}
      };
      }

      void f(::foo::Old * x) {
        ::foo::Old::foo() ;
      }
      using ::foo::Old;)";
  std::string After = runClangRenameOnCode(Before, "foo::Old", "::foo::Old");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, ChangeIfSameNameWithLeadingDotDot) {
  std::string Before = R"(
      namespace foo {
      class Old {
       public:
         static void foo() {}
      };
      }

      void f(foo::Old * x) {
        foo::Old::foo() ;
      }
      using foo::Old;)";
  std::string Expected = R"(
      namespace foo {
      class Old {
       public:
         static void foo() {}
      };
      }

      void f(::foo::Old * x) {
        ::foo::Old::foo() ;
      }
      using ::foo::Old;)";
  std::string After = runClangRenameOnCode(Before, "::foo::Old", "::foo::Old");
  CompareSnippets(Expected, After);
}

TEST_F(RenameClassTest, UsingAlias) {
  std::string Before = R"(
      namespace a { struct A {}; }

      namespace foo {
      using Alias = a::A;
      Alias a;
      })";
  std::string Expected = R"(
      namespace a { struct B {}; }

      namespace foo {
      using Alias = b::B;
      Alias a;
      })";
  std::string After = runClangRenameOnCode(Before, "a::A", "b::B");
  CompareSnippets(Expected, After);
}

TEST_F(ClangRenameTest, NestedTemplates) {
  std::string Before = R"(
      namespace a { template <typename T> struct A {}; }
      a::A<a::A<int>> foo;)";
  std::string Expected = R"(
      namespace a { template <typename T> struct B {}; }
      b::B<b::B<int>> foo;)";
  std::string After = runClangRenameOnCode(Before, "a::A", "b::B");
  CompareSnippets(Expected, After);
}


} // anonymous namespace
} // namespace test
} // namespace clang_rename
} // namesdpace clang