| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 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 |
#include <QtCore> |
| 30 |
|
| 31 |
class Class : public QObject |
| 32 |
{ |
| 33 |
Q_OBJECT |
| 34 |
|
| 35 |
class SubClass |
| 36 |
{ |
| 37 |
void f() |
| 38 |
{ |
| 39 |
tr("nested class context"); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
void f() |
| 44 |
{ |
| 45 |
tr("just class context"); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
namespace Outer { |
| 50 |
|
| 51 |
class Class : public QObject { Q_OBJECT }; |
| 52 |
|
| 53 |
namespace Middle1 { |
| 54 |
|
| 55 |
class Class : public QObject { Q_OBJECT }; |
| 56 |
|
| 57 |
namespace Inner1 { |
| 58 |
|
| 59 |
class Class : public QObject { Q_OBJECT }; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
namespace I = Inner1; |
| 64 |
|
| 65 |
class Something; |
| 66 |
class Different; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
namespace Middle2 { |
| 71 |
|
| 72 |
class Class : public QObject { Q_OBJECT }; |
| 73 |
|
| 74 |
namespace Inner2 { |
| 75 |
|
| 76 |
class Class : public QObject { Q_OBJECT }; |
| 77 |
|
| 78 |
namespace IO = Middle2; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
namespace I = Inner2; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
namespace MI = Middle1::Inner1; |
| 87 |
|
| 88 |
namespace O = ::Outer; |
| 89 |
|
| 90 |
class Middle1::Different : QObject { |
| 91 |
Q_OBJECT |
| 92 |
void f() { |
| 93 |
tr("different namespaced class def"); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
namespace O = Outer; |
| 100 |
namespace OM = Outer::Middle1; |
| 101 |
namespace OMI = Outer::Middle1::I; |
| 102 |
|
| 103 |
int main() |
| 104 |
{ |
| 105 |
Class::tr("outestmost class"); |
| 106 |
Outer::Class::tr("outer class"); |
| 107 |
Outer::MI::Class::tr("innermost one"); |
| 108 |
OMI::Class::tr("innermost two"); |
| 109 |
O::Middle1::I::Class::tr("innermost three"); |
| 110 |
O::Middle2::I::Class::tr("innermost three b"); |
| 111 |
OM::I::Class::tr("innermost four"); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
class OM::Something : QObject { |
| 116 |
Q_OBJECT |
| 117 |
void f() { |
| 118 |
tr("namespaced class def"); |
| 119 |
} |
| 120 |
void g() { |
| 121 |
tr("namespaced class def 2"); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
// QTBUG-8360 |
| 126 |
namespace A { |
| 127 |
|
| 128 |
void foo() |
| 129 |
{ |
| 130 |
using namespace A; |
| 131 |
} |
| 132 |
|
| 133 |
QString goo() |
| 134 |
{ |
| 135 |
return QObject::tr("Bla"); |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
namespace AA { |
| 142 |
namespace B { |
| 143 |
|
| 144 |
using namespace AA; |
| 145 |
|
| 146 |
namespace C { |
| 147 |
|
| 148 |
class Test : public QObject { |
| 149 |
Q_OBJECT |
| 150 |
}; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
using namespace B; |
| 157 |
using namespace C; |
| 158 |
|
| 159 |
void goo() |
| 160 |
{ |
| 161 |
AA::Test::tr("howdy?"); |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
namespace A1 { |
| 168 |
namespace B { |
| 169 |
|
| 170 |
class Test : public QObject { |
| 171 |
Q_OBJECT |
| 172 |
}; |
| 173 |
|
| 174 |
using namespace A1; |
| 175 |
|
| 176 |
void foo() |
| 177 |
{ |
| 178 |
B::B::B::Test::tr("yeeee-ha!"); |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
namespace Ns1::Ns2::Ns3 { |
| 185 |
class Cl: public QObject { |
| 186 |
Q_OBJECT |
| 187 |
void func1(); |
| 188 |
void func2(); |
| 189 |
void func3(); |
| 190 |
void func4(); |
| 191 |
}; |
| 192 |
|
| 193 |
// inside namespace |
| 194 |
|
| 195 |
void Cl::func1() { |
| 196 |
tr("message1"); |
| 197 |
} |
| 198 |
|
| 199 |
void Ns3::Cl::func2() { |
| 200 |
tr("message2"); |
| 201 |
} |
| 202 |
|
| 203 |
void Ns2::Ns3::Cl::func3() { |
| 204 |
tr("message3"); |
| 205 |
} |
| 206 |
|
| 207 |
void Ns1::Ns2::Ns3::Cl::func4() { |
| 208 |
tr("message4"); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
class cl: public QObject { |
| 213 |
Q_OBJECT |
| 214 |
void func(); |
| 215 |
}; |
| 216 |
|
| 217 |
void ::cl::func() { |
| 218 |
tr("text"); |
| 219 |
} |
| 220 |
|
| 221 |
// using namespace in parent |
| 222 |
namespace A1 { |
| 223 |
namespace A2 { |
| 224 |
class Cl : public QObject |
| 225 |
{ |
| 226 |
Q_OBJECT |
| 227 |
void func(); |
| 228 |
}; |
| 229 |
} // namespace A2 |
| 230 |
} // namespace A1 |
| 231 |
|
| 232 |
using namespace A1::A2; |
| 233 |
|
| 234 |
namespace A1 { |
| 235 |
void Cl::func() |
| 236 |
{ |
| 237 |
tr("using namespace in parent"); |
| 238 |
} |
| 239 |
} // namespace A1 |
| 240 |
|
| 241 |
// variable template |
| 242 |
|
| 243 |
namespace NS1 { |
| 244 |
namespace NS2 { |
| 245 |
|
| 246 |
template <class T> |
| 247 |
struct Cl1 |
| 248 |
{ |
| 249 |
static constexpr bool value = true; |
| 250 |
}; |
| 251 |
|
| 252 |
template <class T> |
| 253 |
constexpr bool varTemplate = NS2::Cl1<T>::value; |
| 254 |
|
| 255 |
} // namespace NS2 |
| 256 |
} // namespace NS1 |
| 257 |
|
| 258 |
class Cl2 : public QObject |
| 259 |
{ |
| 260 |
Q_OBJECT |
| 261 |
void func() { tr("context after variable template"); } |
| 262 |
}; |
| 263 |
|
| 264 |
// Direct match in parent namespace must win over using directive match |
| 265 |
namespace EdgeTest1 { |
| 266 |
namespace Inner { |
| 267 |
class Tr : public QObject { Q_OBJECT }; |
| 268 |
} |
| 269 |
namespace Outer { |
| 270 |
class Tr : public QObject { Q_OBJECT }; |
| 271 |
|
| 272 |
namespace Deep { |
| 273 |
using namespace EdgeTest1::Inner; |
| 274 |
|
| 275 |
void test() |
| 276 |
{ |
| 277 |
Tr::tr("direct parent wins over using"); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Reusing same using directive for each segment in qualified name |
| 284 |
namespace EdgeTest3 { |
| 285 |
namespace Level1 { |
| 286 |
namespace Level2 { |
| 287 |
namespace Level3 { |
| 288 |
class Test : public QObject { Q_OBJECT }; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
namespace Container { |
| 293 |
using namespace EdgeTest3::Level1; |
| 294 |
|
| 295 |
void test() |
| 296 |
{ |
| 297 |
Level2::Level3::Test::tr("reuse using per segment"); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// Multiple using directives with same target |
| 303 |
namespace EdgeTest4 { |
| 304 |
namespace Target { |
| 305 |
class Tr : public QObject { Q_OBJECT }; |
| 306 |
} |
| 307 |
namespace Middle1 { |
| 308 |
using namespace EdgeTest4::Target; |
| 309 |
} |
| 310 |
namespace Middle2 { |
| 311 |
using namespace EdgeTest4::Target; |
| 312 |
} |
| 313 |
namespace Container { |
| 314 |
using namespace EdgeTest4::Middle1; |
| 315 |
using namespace EdgeTest4::Middle2; |
| 316 |
|
| 317 |
void test() |
| 318 |
{ |
| 319 |
Tr::tr("prevent duplicate visits"); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
// Direct child in current namespace beats using directive in same namespace |
| 325 |
namespace EdgeTest5 { |
| 326 |
namespace External { |
| 327 |
class Tr : public QObject { Q_OBJECT }; |
| 328 |
} |
| 329 |
namespace MySpace { |
| 330 |
using namespace EdgeTest5::External; |
| 331 |
class Tr : public QObject { Q_OBJECT }; |
| 332 |
|
| 333 |
void test() |
| 334 |
{ |
| 335 |
Tr::tr("direct child beats using"); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
// Test visitedUsings sharing across loop iterations |
| 341 |
// Same using directive at different namespace levels |
| 342 |
namespace EdgeTest6 { |
| 343 |
namespace Target { |
| 344 |
class Tr : public QObject { Q_OBJECT }; |
| 345 |
} |
| 346 |
namespace Parent { |
| 347 |
using namespace EdgeTest6::Target; |
| 348 |
|
| 349 |
namespace Child { |
| 350 |
using namespace EdgeTest6::Target; |
| 351 |
|
| 352 |
void test() |
| 353 |
{ |
| 354 |
Tr::tr("same using at different levels"); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// Different paths to same namespace via using directives |
| 361 |
namespace EdgeTest7 { |
| 362 |
namespace Common { |
| 363 |
class Tr : public QObject { Q_OBJECT }; |
| 364 |
} |
| 365 |
namespace PathA { |
| 366 |
using namespace EdgeTest7::Common; |
| 367 |
} |
| 368 |
namespace PathB { |
| 369 |
using namespace EdgeTest7::Common; |
| 370 |
} |
| 371 |
namespace Container { |
| 372 |
using namespace EdgeTest7::PathA; |
| 373 |
|
| 374 |
namespace Inner { |
| 375 |
using namespace EdgeTest7::PathB; |
| 376 |
|
| 377 |
void test() |
| 378 |
{ |
| 379 |
Tr::tr("different paths to same namespace"); |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
// Using directive at inner level shouldn't block outer level direct match |
| 386 |
namespace EdgeTest8 { |
| 387 |
namespace Decoy { |
| 388 |
class Tr : public QObject { Q_OBJECT }; |
| 389 |
} |
| 390 |
namespace Outer { |
| 391 |
class Tr : public QObject { Q_OBJECT }; |
| 392 |
|
| 393 |
namespace Inner { |
| 394 |
using namespace EdgeTest8::Decoy; |
| 395 |
|
| 396 |
void test() |
| 397 |
{ |
| 398 |
Tr::tr("inner using shouldn't block outer direct"); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// Nested using directives with shared visitedUsings |
| 405 |
namespace EdgeTest9 { |
| 406 |
namespace Level1 { |
| 407 |
class Tr : public QObject { Q_OBJECT }; |
| 408 |
} |
| 409 |
namespace Level2 { |
| 410 |
using namespace EdgeTest9::Level1; |
| 411 |
} |
| 412 |
namespace Level3 { |
| 413 |
using namespace EdgeTest9::Level2; |
| 414 |
|
| 415 |
namespace Level4 { |
| 416 |
using namespace EdgeTest9::Level1; |
| 417 |
|
| 418 |
void test() |
| 419 |
{ |
| 420 |
Tr::tr("nested using with shared visited"); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// QTBUG-140550: Class with same name as enclosing namespace |
| 427 |
namespace Test { |
| 428 |
class Test : public QObject { |
| 429 |
Q_OBJECT |
| 430 |
public: |
| 431 |
Test(); |
| 432 |
}; |
| 433 |
|
| 434 |
Test::Test() { |
| 435 |
tr("class same name as namespace"); |
| 436 |
} |
| 437 |
} // namespace Test |
| 438 |
|
| 439 |
// QTBUG-140550: Nested namespaces with same name, class with different name |
| 440 |
namespace NsA { |
| 441 |
namespace NsA { |
| 442 |
namespace NsA { |
| 443 |
class MyClass : public QObject { |
| 444 |
Q_OBJECT |
| 445 |
public: |
| 446 |
void func1(); |
| 447 |
void func2(); |
| 448 |
}; |
| 449 |
|
| 450 |
void MyClass::func1() { |
| 451 |
tr("nested same-name ns, different class 1"); |
| 452 |
} |
| 453 |
|
| 454 |
void NsA::MyClass::func2() { |
| 455 |
tr("nested same-name ns, different class 2"); |
| 456 |
} |
| 457 |
} // namespace NsA |
| 458 |
} // namespace NsA |
| 459 |
} // namespace NsA |
| 460 |
|
| 461 |
// QTBUG-140550: Complex nesting - namespace A inside namespace B inside namespace A |
| 462 |
namespace Alpha { |
| 463 |
namespace Beta { |
| 464 |
namespace Alpha { |
| 465 |
class Widget : public QObject { |
| 466 |
Q_OBJECT |
| 467 |
public: |
| 468 |
void method(); |
| 469 |
}; |
| 470 |
|
| 471 |
void Widget::method() { |
| 472 |
tr("complex nesting different names"); |
| 473 |
} |
| 474 |
} // namespace Alpha |
| 475 |
} // namespace Beta |
| 476 |
} // namespace Alpha |
| 477 |
|
| 478 |
// Combined bug test: Same-named class with using directive (QTBUG-140548 + QTBUG-140550) |
| 479 |
namespace CombinedTest1 { |
| 480 |
namespace ExternalLib { |
| 481 |
class Tr : public QObject { |
| 482 |
Q_OBJECT |
| 483 |
public: |
| 484 |
void externalFunc() { tr("external lib tr"); } |
| 485 |
}; |
| 486 |
} |
| 487 |
|
| 488 |
namespace MyApp { |
| 489 |
using namespace CombinedTest1::ExternalLib; |
| 490 |
|
| 491 |
// Same name as enclosing namespace AND there's a using directive |
| 492 |
class MyApp : public QObject { |
| 493 |
Q_OBJECT |
| 494 |
public: |
| 495 |
void myFunc() |
| 496 |
{ |
| 497 |
// Should resolve to MyApp::MyApp (the class), not ExternalLib::Tr |
| 498 |
tr("combined test: direct class wins"); |
| 499 |
} |
| 500 |
}; |
| 501 |
|
| 502 |
namespace Inner { |
| 503 |
void testFunc() |
| 504 |
{ |
| 505 |
// Qualified call - should prefer the class with same parent namespace |
| 506 |
MyApp::tr("combined test: qualified same name"); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
// Deeply nested same-name namespaces with using directive |
| 513 |
namespace CombinedTest2 { |
| 514 |
namespace Level { |
| 515 |
namespace Level { |
| 516 |
class Helper : public QObject { |
| 517 |
Q_OBJECT |
| 518 |
public: |
| 519 |
void helperMethod() { tr("helper in Level::Level"); } |
| 520 |
}; |
| 521 |
|
| 522 |
namespace Level { |
| 523 |
using namespace CombinedTest2::Level::Level; |
| 524 |
|
| 525 |
class Level : public QObject { |
| 526 |
Q_OBJECT |
| 527 |
public: |
| 528 |
void method() |
| 529 |
{ |
| 530 |
// Should resolve to Level::Level::Level::Level (the class) |
| 531 |
tr("deeply nested same name with using"); |
| 532 |
} |
| 533 |
}; |
| 534 |
|
| 535 |
void freeFunc() |
| 536 |
{ |
| 537 |
// Should prefer the local class |
| 538 |
Level::tr("qualified deeply nested"); |
| 539 |
|
| 540 |
// Should find Helper via using directive |
| 541 |
Helper::tr("helper via using in nested"); |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
// Using directive shadowing local namespace |
| 549 |
namespace CombinedTest3 { |
| 550 |
namespace Container { |
| 551 |
class Data : public QObject { |
| 552 |
Q_OBJECT |
| 553 |
public: |
| 554 |
void process() { tr("data in original container"); } |
| 555 |
}; |
| 556 |
} |
| 557 |
|
| 558 |
namespace Outer { |
| 559 |
using namespace CombinedTest3::Container; |
| 560 |
|
| 561 |
namespace Container { |
| 562 |
// Local namespace named Container, but there's also a using directive |
| 563 |
class Item : public QObject { |
| 564 |
Q_OBJECT |
| 565 |
public: |
| 566 |
void method() |
| 567 |
{ |
| 568 |
// Should find Item in local Outer::Container |
| 569 |
tr("item in local container"); |
| 570 |
} |
| 571 |
}; |
| 572 |
|
| 573 |
void testFunc() |
| 574 |
{ |
| 575 |
// Should resolve to local namespace |
| 576 |
Item::tr("qualified local item"); |
| 577 |
|
| 578 |
// Should still find Data via using directive |
| 579 |
Data::tr("data via using directive"); |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
//#include "main.moc" |