1 //===-- AddUsingTests.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Config.h" 10 #include "TweakTesting.h" 11 #include "gmock/gmock.h" 12 #include "gtest/gtest.h" 13 14 namespace clang { 15 namespace clangd { 16 namespace { 17 18 TWEAK_TEST(AddUsing); 19 20 TEST_F(AddUsingTest, Prepare) { 21 Config Cfg; 22 Cfg.Style.FullyQualifiedNamespaces.push_back("ban"); 23 WithContextValue WithConfig(Config::Key, std::move(Cfg)); 24 25 const std::string Header = R"cpp( 26 #define NS(name) one::two::name 27 namespace ban { void foo() {} } 28 namespace banana { void foo() {} } 29 namespace one { 30 void oo() {} 31 template<typename TT> class tt {}; 32 namespace two { 33 enum ee {}; 34 void ff() {} 35 class cc { 36 public: 37 struct st {}; 38 static void mm() {} 39 cc operator|(const cc& x) const { return x; } 40 }; 41 } 42 })cpp"; 43 44 EXPECT_AVAILABLE(Header + "void fun() { o^n^e^:^:^t^w^o^:^:^f^f(); }"); 45 EXPECT_AVAILABLE(Header + "void fun() { o^n^e^::^o^o(); }"); 46 EXPECT_AVAILABLE(Header + "void fun() { o^n^e^:^:^t^w^o^:^:^e^e E; }"); 47 EXPECT_AVAILABLE(Header + "void fun() { o^n^e^:^:^t^w^o:^:^c^c C; }"); 48 EXPECT_UNAVAILABLE(Header + 49 "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^m^m(); }"); 50 EXPECT_UNAVAILABLE(Header + 51 "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^s^t inst; }"); 52 EXPECT_UNAVAILABLE(Header + 53 "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^s^t inst; }"); 54 EXPECT_UNAVAILABLE(Header + "void fun() { N^S(c^c) inst; }"); 55 // This used to crash. Ideally we would support this case, but for now we just 56 // test that we don't crash. 57 EXPECT_UNAVAILABLE(Header + 58 "template<typename TT> using foo = one::tt<T^T>;"); 59 // Test that we don't crash or misbehave on unnamed DeclRefExpr. 60 EXPECT_UNAVAILABLE(Header + 61 "void fun() { one::two::cc() ^| one::two::cc(); }"); 62 // Do not offer code action when operating on a banned namespace. 63 EXPECT_UNAVAILABLE(Header + "void fun() { ban::fo^o(); }"); 64 EXPECT_UNAVAILABLE(Header + "void fun() { ::ban::fo^o(); }"); 65 EXPECT_AVAILABLE(Header + "void fun() { banana::fo^o(); }"); 66 67 // Do not offer code action on typo-corrections. 68 EXPECT_UNAVAILABLE(Header + "/*error-ok*/c^c C;"); 69 70 // NestedNameSpecifier, but no namespace. 71 EXPECT_UNAVAILABLE(Header + "class Foo {}; class F^oo foo;"); 72 73 // Check that we do not trigger in header files. 74 FileName = "test.h"; 75 ExtraArgs.push_back("-xc++-header"); // .h file is treated a C by default. 76 EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }"); 77 FileName = "test.hpp"; 78 EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }"); 79 } 80 81 TEST_F(AddUsingTest, Apply) { 82 FileName = "test.cpp"; 83 struct { 84 llvm::StringRef TestSource; 85 llvm::StringRef ExpectedSource; 86 } Cases[]{{ 87 // Function, no other using, namespace. 88 R"cpp( 89 #include "test.hpp" 90 namespace { 91 void fun() { 92 ^o^n^e^:^:^t^w^o^:^:^f^f(); 93 } 94 })cpp", 95 R"cpp( 96 #include "test.hpp" 97 namespace {using one::two::ff; 98 99 void fun() { 100 ff(); 101 } 102 })cpp", 103 }, 104 // Type, no other using, namespace. 105 { 106 R"cpp( 107 #include "test.hpp" 108 namespace { 109 void fun() { 110 ::on^e::t^wo::c^c inst; 111 } 112 })cpp", 113 R"cpp( 114 #include "test.hpp" 115 namespace {using ::one::two::cc; 116 117 void fun() { 118 cc inst; 119 } 120 })cpp", 121 }, 122 // Type, no other using, no namespace. 123 { 124 R"cpp( 125 #include "test.hpp" 126 127 void fun() { 128 on^e::t^wo::e^e inst; 129 })cpp", 130 R"cpp( 131 #include "test.hpp" 132 133 using one::two::ee; 134 135 void fun() { 136 ee inst; 137 })cpp"}, 138 // Function, other usings. 139 { 140 R"cpp( 141 #include "test.hpp" 142 143 using one::two::cc; 144 using one::two::ee; 145 146 namespace { 147 void fun() { 148 one::two::f^f(); 149 } 150 })cpp", 151 R"cpp( 152 #include "test.hpp" 153 154 using one::two::cc; 155 using one::two::ff;using one::two::ee; 156 157 namespace { 158 void fun() { 159 ff(); 160 } 161 })cpp", 162 }, 163 // Function, other usings inside namespace. 164 { 165 R"cpp( 166 #include "test.hpp" 167 168 using one::two::cc; 169 170 namespace { 171 172 using one::two::ff; 173 174 void fun() { 175 o^ne::o^o(); 176 } 177 })cpp", 178 R"cpp( 179 #include "test.hpp" 180 181 using one::two::cc; 182 183 namespace { 184 185 using one::oo;using one::two::ff; 186 187 void fun() { 188 oo(); 189 } 190 })cpp"}, 191 // Using comes after cursor. 192 { 193 R"cpp( 194 #include "test.hpp" 195 196 namespace { 197 198 void fun() { 199 one::t^wo::ff(); 200 } 201 202 using one::two::cc; 203 204 })cpp", 205 R"cpp( 206 #include "test.hpp" 207 208 namespace {using one::two::ff; 209 210 211 void fun() { 212 ff(); 213 } 214 215 using one::two::cc; 216 217 })cpp"}, 218 // Pointer type. 219 {R"cpp( 220 #include "test.hpp" 221 222 void fun() { 223 one::two::c^c *p; 224 })cpp", 225 R"cpp( 226 #include "test.hpp" 227 228 using one::two::cc; 229 230 void fun() { 231 cc *p; 232 })cpp"}, 233 // Namespace declared via macro. 234 {R"cpp( 235 #include "test.hpp" 236 #define NS_BEGIN(name) namespace name { 237 238 NS_BEGIN(foo) 239 240 void fun() { 241 one::two::f^f(); 242 } 243 })cpp", 244 R"cpp( 245 #include "test.hpp" 246 #define NS_BEGIN(name) namespace name { 247 248 using one::two::ff; 249 250 NS_BEGIN(foo) 251 252 void fun() { 253 ff(); 254 } 255 })cpp"}, 256 // Inside macro argument. 257 {R"cpp( 258 #include "test.hpp" 259 #define CALL(name) name() 260 261 void fun() { 262 CALL(one::t^wo::ff); 263 })cpp", 264 R"cpp( 265 #include "test.hpp" 266 #define CALL(name) name() 267 268 using one::two::ff; 269 270 void fun() { 271 CALL(ff); 272 })cpp"}, 273 // Parent namespace != lexical parent namespace 274 {R"cpp( 275 #include "test.hpp" 276 namespace foo { void fun(); } 277 278 void foo::fun() { 279 one::two::f^f(); 280 })cpp", 281 R"cpp( 282 #include "test.hpp" 283 using one::two::ff; 284 285 namespace foo { void fun(); } 286 287 void foo::fun() { 288 ff(); 289 })cpp"}, 290 // If all other using are fully qualified, add :: 291 {R"cpp( 292 #include "test.hpp" 293 294 using ::one::two::cc; 295 using ::one::two::ee; 296 297 void fun() { 298 one::two::f^f(); 299 })cpp", 300 R"cpp( 301 #include "test.hpp" 302 303 using ::one::two::cc; 304 using ::one::two::ff;using ::one::two::ee; 305 306 void fun() { 307 ff(); 308 })cpp"}, 309 // Make sure we don't add :: if it's already there 310 {R"cpp( 311 #include "test.hpp" 312 313 using ::one::two::cc; 314 using ::one::two::ee; 315 316 void fun() { 317 ::one::two::f^f(); 318 })cpp", 319 R"cpp( 320 #include "test.hpp" 321 322 using ::one::two::cc; 323 using ::one::two::ff;using ::one::two::ee; 324 325 void fun() { 326 ff(); 327 })cpp"}, 328 // If even one using doesn't start with ::, do not add it 329 {R"cpp( 330 #include "test.hpp" 331 332 using ::one::two::cc; 333 using one::two::ee; 334 335 void fun() { 336 one::two::f^f(); 337 })cpp", 338 R"cpp( 339 #include "test.hpp" 340 341 using ::one::two::cc; 342 using one::two::ff;using one::two::ee; 343 344 void fun() { 345 ff(); 346 })cpp"}, 347 // using alias; insert using for the spelled name. 348 {R"cpp( 349 #include "test.hpp" 350 351 void fun() { 352 one::u^u u; 353 })cpp", 354 R"cpp( 355 #include "test.hpp" 356 357 using one::uu; 358 359 void fun() { 360 uu u; 361 })cpp"}, 362 // using namespace. 363 {R"cpp( 364 #include "test.hpp" 365 using namespace one; 366 namespace { 367 two::c^c C; 368 })cpp", 369 R"cpp( 370 #include "test.hpp" 371 using namespace one; 372 namespace {using two::cc; 373 374 cc C; 375 })cpp"}, 376 // Type defined in main file, make sure using is after that. 377 {R"cpp( 378 namespace xx { 379 struct yy {}; 380 } 381 382 x^x::yy X; 383 )cpp", 384 R"cpp( 385 namespace xx { 386 struct yy {}; 387 } 388 389 using xx::yy; 390 391 yy X; 392 )cpp"}, 393 // Type defined in main file via "using", insert after that. 394 {R"cpp( 395 #include "test.hpp" 396 397 namespace xx { 398 using yy = one::two::cc; 399 } 400 401 x^x::yy X; 402 )cpp", 403 R"cpp( 404 #include "test.hpp" 405 406 namespace xx { 407 using yy = one::two::cc; 408 } 409 410 using xx::yy; 411 412 yy X; 413 )cpp"}, 414 // Using must come after function definition. 415 {R"cpp( 416 namespace xx { 417 void yy(); 418 } 419 420 void fun() { 421 x^x::yy(); 422 } 423 )cpp", 424 R"cpp( 425 namespace xx { 426 void yy(); 427 } 428 429 using xx::yy; 430 431 void fun() { 432 yy(); 433 } 434 )cpp"}, 435 // Existing using with non-namespace part. 436 {R"cpp( 437 #include "test.hpp" 438 using one::two::ee::ee_one; 439 one::t^wo::cc c; 440 )cpp", 441 R"cpp( 442 #include "test.hpp" 443 using one::two::cc;using one::two::ee::ee_one; 444 cc c; 445 )cpp"}, 446 // Template (like std::vector). 447 {R"cpp( 448 #include "test.hpp" 449 one::v^ec<int> foo; 450 )cpp", 451 R"cpp( 452 #include "test.hpp" 453 using one::vec; 454 455 vec<int> foo; 456 )cpp"}}; 457 llvm::StringMap<std::string> EditedFiles; 458 for (const auto &Case : Cases) { 459 for (const auto &SubCase : expandCases(Case.TestSource)) { 460 ExtraFiles["test.hpp"] = R"cpp( 461 namespace one { 462 void oo() {} 463 namespace two { 464 enum ee {ee_one}; 465 void ff() {} 466 class cc { 467 public: 468 struct st { struct nested {}; }; 469 static void mm() {} 470 }; 471 } 472 using uu = two::cc; 473 template<typename T> struct vec {}; 474 })cpp"; 475 EXPECT_EQ(apply(SubCase, &EditedFiles), Case.ExpectedSource); 476 } 477 } 478 } 479 480 } // namespace 481 } // namespace clangd 482 } // namespace clang 483