aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
blob: c2dd8e1bb8eefaaefe8daa9987675aa2202c2f5a (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
//===-- AddUsingTests.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 "Config.h"
#include "TweakTesting.h"
#include "support/Context.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "gtest/gtest.h"
#include <string>
#include <utility>

namespace clang {
namespace clangd {
namespace {

TWEAK_TEST(AddUsing);

TEST_F(AddUsingTest, Prepare) {
  Config Cfg;
  Cfg.Style.FullyQualifiedNamespaces.push_back("ban");
  WithContextValue WithConfig(Config::Key, std::move(Cfg));

  const std::string Header = R"cpp(
#define NS(name) one::two::name
namespace ban { void foo() {} }
namespace banana { void foo() {} }
namespace one {
void oo() {}
template<typename TT> class tt {};
namespace two {
enum ee { ee_enum_value };
void ff() {}
class cc {
public:
  struct st {};
  static void mm() {}
  cc operator|(const cc& x) const { return x; }
};
}
})cpp";

  EXPECT_AVAILABLE(Header + "void fun() { o^n^e^:^:^t^w^o^:^:^f^f(); }");
  EXPECT_AVAILABLE(Header + "void fun() { o^n^e^::^o^o(); }");
  EXPECT_AVAILABLE(Header + "void fun() { o^n^e^:^:^t^w^o^:^:^e^e E; }");
  EXPECT_AVAILABLE(Header + "void fun() { o^n^e^:^:^t^w^o:^:^c^c C; }");
  EXPECT_UNAVAILABLE(Header +
                     "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^m^m(); }");
  EXPECT_UNAVAILABLE(Header +
                     "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^s^t inst; }");
  EXPECT_UNAVAILABLE(Header +
                     "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^s^t inst; }");
  EXPECT_UNAVAILABLE(Header + "void fun() { N^S(c^c) inst; }");
  // This used to crash. Ideally we would support this case, but for now we just
  // test that we don't crash.
  EXPECT_UNAVAILABLE(Header +
                     "template<typename TT> using foo = one::tt<T^T>;");
  // Test that we don't crash or misbehave on unnamed DeclRefExpr.
  EXPECT_UNAVAILABLE(Header +
                     "void fun() { one::two::cc() ^| one::two::cc(); }");
  // Do not offer code action when operating on a banned namespace.
  EXPECT_UNAVAILABLE(Header + "void fun() { ban::fo^o(); }");
  EXPECT_UNAVAILABLE(Header + "void fun() { ::ban::fo^o(); }");
  EXPECT_AVAILABLE(Header + "void fun() { banana::fo^o(); }");

  // NestedNameSpecifier, but no namespace.
  EXPECT_UNAVAILABLE(Header + "class Foo {}; class F^oo foo;");

  // Nested macro case.
  EXPECT_AVAILABLE(R"cpp(
  #define ID2(X) X
  #define ID(Y, X) Y;ID2(X)
  namespace ns { struct Foo{}; }
  ID(int xyz, ns::F^oo) f;)cpp");

  // Check that we do not trigger in header files.
  FileName = "test.h";
  ExtraArgs.push_back("-xc++-header"); // .h file is treated a C by default.
  EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }");
  FileName = "test.hpp";
  EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }");
}

TEST_F(AddUsingTest, Crash1072) {
  // Used to crash when traversing catch(...)
  // https://github.com/clangd/clangd/issues/1072
  const char *Code = R"cpp(
  namespace ns { class A; }
  ns::^A *err;
  void catchall() {
    try {} catch(...) {}
  }
  )cpp";
  EXPECT_AVAILABLE(Code);
}

TEST_F(AddUsingTest, Apply) {
  FileName = "test.cpp";
  struct {
    llvm::StringRef TestSource;
    llvm::StringRef ExpectedSource;
  } Cases[]{
      {
          // Function, no other using, namespace.
          R"cpp(
#include "test.hpp"
namespace {
void fun() {
  ^one::two::ff();
}
})cpp",
          R"cpp(
#include "test.hpp"
namespace {using one::two::ff;

void fun() {
  ff();
}
})cpp",
      },
      // Type, no other using, namespace.
      {
          R"cpp(
#include "test.hpp"
namespace {
void fun() {
  ::one::t^wo::cc inst;
}
})cpp",
          R"cpp(
#include "test.hpp"
namespace {using ::one::two::cc;

void fun() {
  cc inst;
}
})cpp",
      },
      // Type, no other using, no namespace.
      {
          R"cpp(
#include "test.hpp"

void fun() {
  one::two::e^e inst;
})cpp",
          R"cpp(
#include "test.hpp"

using one::two::ee;

void fun() {
  ee inst;
})cpp"},
      // Function, other usings.
      {
          R"cpp(
#include "test.hpp"

using one::two::cc;
using one::two::ee;

namespace {
void fun() {
  one::two::f^f();
}
})cpp",
          R"cpp(
#include "test.hpp"

using one::two::cc;
using one::two::ff;using one::two::ee;

namespace {
void fun() {
  ff();
}
})cpp",
      },
      // Function, other usings inside namespace.
      {
          R"cpp(
#include "test.hpp"

using one::two::cc;

namespace {

using one::two::ff;

void fun() {
  o^ne::oo();
}
})cpp",
          R"cpp(
#include "test.hpp"

using one::two::cc;

namespace {

using one::oo;using one::two::ff;

void fun() {
  oo();
}
})cpp"},
      // Using comes after cursor.
      {
          R"cpp(
#include "test.hpp"

namespace {

void fun() {
  one::t^wo::ff();
}

using one::two::cc;

})cpp",
          R"cpp(
#include "test.hpp"

namespace {using one::two::ff;


void fun() {
  ff();
}

using one::two::cc;

})cpp"},
      // Pointer type.
      {R"cpp(
#include "test.hpp"

void fun() {
  one::two::c^c *p;
})cpp",
       R"cpp(
#include "test.hpp"

using one::two::cc;

void fun() {
  cc *p;
})cpp"},
      // Namespace declared via macro.
      {R"cpp(
#include "test.hpp"
#define NS_BEGIN(name) namespace name {

NS_BEGIN(foo)

void fun() {
  one::two::f^f();
}
})cpp",
       R"cpp(
#include "test.hpp"
#define NS_BEGIN(name) namespace name {

using one::two::ff;

NS_BEGIN(foo)

void fun() {
  ff();
}
})cpp"},
      // Inside macro argument.
      {R"cpp(
#include "test.hpp"
#define CALL(name) name()

void fun() {
  CALL(one::t^wo::ff);
})cpp",
       R"cpp(
#include "test.hpp"
#define CALL(name) name()

using one::two::ff;

void fun() {
  CALL(ff);
})cpp"},
      // Parent namespace != lexical parent namespace
      {R"cpp(
#include "test.hpp"
namespace foo { void fun(); }

void foo::fun() {
  one::two::f^f();
})cpp",
       R"cpp(
#include "test.hpp"
using one::two::ff;

namespace foo { void fun(); }

void foo::fun() {
  ff();
})cpp"},
      // Inside a lambda.
      {
          R"cpp(
namespace NS {
void unrelated();
void foo();
}

auto L = [] {
  using NS::unrelated;
  NS::f^oo();
};)cpp",
          R"cpp(
namespace NS {
void unrelated();
void foo();
}

auto L = [] {
  using NS::foo;using NS::unrelated;
  foo();
};)cpp",
      },
      // If all other using are fully qualified, add ::
      {R"cpp(
#include "test.hpp"

using ::one::two::cc;
using ::one::two::ee;

void fun() {
  one::two::f^f();
})cpp",
       R"cpp(
#include "test.hpp"

using ::one::two::cc;
using ::one::two::ff;using ::one::two::ee;

void fun() {
  ff();
})cpp"},
      // Make sure we don't add :: if it's already there
      {R"cpp(
#include "test.hpp"

using ::one::two::cc;
using ::one::two::ee;

void fun() {
  ::one::two::f^f();
})cpp",
       R"cpp(
#include "test.hpp"

using ::one::two::cc;
using ::one::two::ff;using ::one::two::ee;

void fun() {
  ff();
})cpp"},
      // If even one using doesn't start with ::, do not add it
      {R"cpp(
#include "test.hpp"

using ::one::two::cc;
using one::two::ee;

void fun() {
  one::two::f^f();
})cpp",
       R"cpp(
#include "test.hpp"

using ::one::two::cc;
using one::two::ff;using one::two::ee;

void fun() {
  ff();
})cpp"},
      // using alias; insert using for the spelled name.
      {R"cpp(
#include "test.hpp"

void fun() {
  one::u^u u;
})cpp",
       R"cpp(
#include "test.hpp"

using one::uu;

void fun() {
  uu u;
})cpp"},
      // using namespace.
      {R"cpp(
#include "test.hpp"
using namespace one;
namespace {
two::c^c C;
})cpp",
       R"cpp(
#include "test.hpp"
using namespace one;
namespace {using two::cc;

cc C;
})cpp"},
      // Type defined in main file, make sure using is after that.
      {R"cpp(
namespace xx {
  struct yy {};
}

x^x::yy X;
)cpp",
       R"cpp(
namespace xx {
  struct yy {};
}

using xx::yy;

yy X;
)cpp"},
      // Type defined in main file via "using", insert after that.
      {R"cpp(
#include "test.hpp"

namespace xx {
  using yy = one::two::cc;
}

x^x::yy X;
)cpp",
       R"cpp(
#include "test.hpp"

namespace xx {
  using yy = one::two::cc;
}

using xx::yy;

yy X;
)cpp"},
      // Using must come after function definition.
      {R"cpp(
namespace xx {
  void yy();
}

void fun() {
  x^x::yy();
}
)cpp",
       R"cpp(
namespace xx {
  void yy();
}

using xx::yy;

void fun() {
  yy();
}
)cpp"},
      // Existing using with non-namespace part.
      {R"cpp(
#include "test.hpp"
using one::two::ee::ee_one;
one::t^wo::cc c;
)cpp",
       R"cpp(
#include "test.hpp"
using one::two::cc;using one::two::ee::ee_one;
cc c;
)cpp"},
      // Template (like std::vector).
      {R"cpp(
#include "test.hpp"
one::v^ec<int> foo;
)cpp",
       R"cpp(
#include "test.hpp"
using one::vec;

vec<int> foo;
)cpp"},
      // Typo correction.
      {R"cpp(
// error-ok
#include "test.hpp"
c^c C;
)cpp",
       R"cpp(
// error-ok
#include "test.hpp"
using one::two::cc;

cc C;
)cpp"},
      {R"cpp(
// error-ok
#include "test.hpp"
void foo() {
  switch(one::two::ee{}) { case two::ee_^one:break; }
}
)cpp",
       R"cpp(
// error-ok
#include "test.hpp"
using one::two::ee_one;

void foo() {
  switch(one::two::ee{}) { case ee_one:break; }
}
)cpp"},
      {R"cpp(
#include "test.hpp"
void foo() {
  one::f^unc_temp<int>();
})cpp",
       R"cpp(
#include "test.hpp"
using one::func_temp;

void foo() {
  func_temp<int>();
})cpp"},
      {R"cpp(
#include "test.hpp"
void foo() {
  one::va^r_temp<int>;
})cpp",
       R"cpp(
#include "test.hpp"
using one::var_temp;

void foo() {
  var_temp<int>;
})cpp"},
  };
  llvm::StringMap<std::string> EditedFiles;
  for (const auto &Case : Cases) {
    ExtraFiles["test.hpp"] = R"cpp(
namespace one {
void oo() {}
namespace two {
enum ee {ee_one};
void ff() {}
class cc {
public:
  struct st { struct nested {}; };
  static void mm() {}
};
}
using uu = two::cc;
template<typename T> struct vec {};
template <typename T> void func_temp();
template <typename T> T var_temp();
})cpp";
    // Typo correction is disabled in msvc-compatibility mode.
    ExtraArgs.push_back("-fno-ms-compatibility");
    EXPECT_EQ(apply(Case.TestSource, &EditedFiles), Case.ExpectedSource);
  }
}

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