1 // Clear and create directories 2 // RUN: rm -rf %t 3 // RUN: mkdir %t 4 // RUN: mkdir %t/cache 5 // RUN: mkdir %t/Inputs 6 7 // Build first header file 8 // RUN: echo "#define FIRST" >> %t/Inputs/first.h 9 // RUN: cat %s >> %t/Inputs/first.h 10 11 // Build second header file 12 // RUN: echo "#define SECOND" >> %t/Inputs/second.h 13 // RUN: cat %s >> %t/Inputs/second.h 14 15 // Build module map file 16 // RUN: echo "module FirstModule {" >> %t/Inputs/module.map 17 // RUN: echo " header \"first.h\"" >> %t/Inputs/module.map 18 // RUN: echo "}" >> %t/Inputs/module.map 19 // RUN: echo "module SecondModule {" >> %t/Inputs/module.map 20 // RUN: echo " header \"second.h\"" >> %t/Inputs/module.map 21 // RUN: echo "}" >> %t/Inputs/module.map 22 23 // Run test 24 // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x c++ -I%t/Inputs -verify %s -std=c++1z 25 26 #if !defined(FIRST) && !defined(SECOND) 27 #include "first.h" 28 #include "second.h" 29 #endif 30 31 namespace AccessSpecifiers { 32 #if defined(FIRST) 33 struct S1 { 34 }; 35 #elif defined(SECOND) 36 struct S1 { 37 private: 38 }; 39 #else 40 S1 s1; 41 // [email protected]:* {{'AccessSpecifiers::S1' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} 42 // [email protected]:* {{but in 'FirstModule' found end of class}} 43 #endif 44 45 #if defined(FIRST) 46 struct S2 { 47 public: 48 }; 49 #elif defined(SECOND) 50 struct S2 { 51 protected: 52 }; 53 #else 54 S2 s2; 55 // [email protected]:* {{'AccessSpecifiers::S2' has different definitions in different modules; first difference is definition in module 'SecondModule' found protected access specifier}} 56 // [email protected]:* {{but in 'FirstModule' found public access specifier}} 57 #endif 58 } // namespace AccessSpecifiers 59 60 namespace StaticAssert { 61 #if defined(FIRST) 62 struct S1 { 63 static_assert(1 == 1, "First"); 64 }; 65 #elif defined(SECOND) 66 struct S1 { 67 static_assert(1 == 1, "Second"); 68 }; 69 #else 70 S1 s1; 71 // [email protected]:* {{'StaticAssert::S1' has different definitions in different modules; first difference is definition in module 'SecondModule' found static assert with message}} 72 // [email protected]:* {{but in 'FirstModule' found static assert with different message}} 73 #endif 74 75 #if defined(FIRST) 76 struct S2 { 77 static_assert(2 == 2, "Message"); 78 }; 79 #elif defined(SECOND) 80 struct S2 { 81 static_assert(2 == 2); 82 }; 83 #else 84 S2 s2; 85 // [email protected]:* {{'StaticAssert::S2' has different definitions in different modules; first difference is definition in module 'SecondModule' found static assert with no message}} 86 // [email protected]:* {{but in 'FirstModule' found static assert with message}} 87 #endif 88 89 #if defined(FIRST) 90 struct S3 { 91 static_assert(3 == 3, "Message"); 92 }; 93 #elif defined(SECOND) 94 struct S3 { 95 static_assert(3 != 4, "Message"); 96 }; 97 #else 98 S3 s3; 99 // [email protected]:* {{'StaticAssert::S3' has different definitions in different modules; first difference is definition in module 'SecondModule' found static assert with condition}} 100 // [email protected]:* {{but in 'FirstModule' found static assert with different condition}} 101 #endif 102 103 #if defined(FIRST) 104 struct S4 { 105 static_assert(4 == 4, "Message"); 106 }; 107 #elif defined(SECOND) 108 struct S4 { 109 public: 110 }; 111 #else 112 S4 s4; 113 // [email protected]:* {{'StaticAssert::S4' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} 114 // [email protected]:* {{but in 'FirstModule' found static assert}} 115 #endif 116 } 117 118 namespace Field { 119 #if defined(FIRST) 120 struct S1 { 121 int x; 122 private: 123 int y; 124 }; 125 #elif defined(SECOND) 126 struct S1 { 127 int x; 128 int y; 129 }; 130 #else 131 S1 s1; 132 // [email protected]:* {{'Field::S1' has different definitions in different modules; first difference is definition in module 'SecondModule' found field}} 133 // [email protected]:* {{but in 'FirstModule' found private access specifier}} 134 #endif 135 136 #if defined(FIRST) 137 struct S2 { 138 int x; 139 int y; 140 }; 141 #elif defined(SECOND) 142 struct S2 { 143 int y; 144 int x; 145 }; 146 #else 147 S2 s2; 148 // [email protected]:* {{'Field::S2' has different definitions in different modules; first difference is definition in module 'SecondModule' found field 'y'}} 149 // [email protected]:* {{but in 'FirstModule' found field 'x'}} 150 #endif 151 152 #if defined(FIRST) 153 struct S3 { 154 double x; 155 }; 156 #elif defined(SECOND) 157 struct S3 { 158 int x; 159 }; 160 #else 161 S3 s3; 162 // [email protected]:* {{'Field::S3::x' from module 'FirstModule' is not present in definition of 'Field::S3' in module 'SecondModule'}} 163 // [email protected]:* {{declaration of 'x' does not match}} 164 #endif 165 } // namespace Field 166 167 // Naive parsing of AST can lead to cycles in processing. Ensure 168 // self-references don't trigger an endless cycles of AST node processing. 169 namespace SelfReference { 170 #if defined(FIRST) 171 template <template <int> class T> class Wrapper {}; 172 173 template <int N> class S { 174 S(Wrapper<::SelfReference::S> &Ref) {} 175 }; 176 177 struct Xx { 178 struct Yy { 179 }; 180 }; 181 182 Xx::Xx::Xx::Yy yy; 183 184 namespace NNS { 185 template <typename> struct Foo; 186 template <template <class> class T = NNS::Foo> 187 struct NestedNamespaceSpecifier {}; 188 } 189 #endif 190 } // namespace SelfReference 191 192 // Interesting cases that should not cause errors. struct S should not error 193 // while struct T should error at the access specifier mismatch at the end. 194 namespace AllDecls { 195 #if defined(FIRST) 196 struct S { 197 public: 198 private: 199 protected: 200 201 static_assert(1 == 1, "Message"); 202 static_assert(2 == 2); 203 204 int x; 205 double y; 206 }; 207 #elif defined(SECOND) 208 struct S { 209 public: 210 private: 211 protected: 212 213 static_assert(1 == 1, "Message"); 214 static_assert(2 == 2); 215 216 int x; 217 double y; 218 }; 219 #else 220 S s; 221 #endif 222 223 #if defined(FIRST) 224 struct T { 225 public: 226 private: 227 protected: 228 229 static_assert(1 == 1, "Message"); 230 static_assert(2 == 2); 231 232 int x; 233 double y; 234 235 private: 236 }; 237 #elif defined(SECOND) 238 struct T { 239 public: 240 private: 241 protected: 242 243 static_assert(1 == 1, "Message"); 244 static_assert(2 == 2); 245 246 int x; 247 double y; 248 249 public: 250 }; 251 #else 252 T t; 253 // [email protected]:* {{'AllDecls::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} 254 // [email protected]:* {{but in 'FirstModule' found private access specifier}} 255 #endif 256 } 257 258 namespace FriendFunction { 259 #if defined(FIRST) 260 void F(int = 0); 261 struct S { friend void F(int); }; 262 #elif defined(SECOND) 263 void F(int); 264 struct S { friend void F(int); }; 265 #else 266 S s; 267 #endif 268 269 #if defined(FIRST) 270 void G(int = 0); 271 struct T { 272 friend void G(int); 273 274 private: 275 }; 276 #elif defined(SECOND) 277 void G(int); 278 struct T { 279 friend void G(int); 280 281 public: 282 }; 283 #else 284 T t; 285 // [email protected]:* {{'FriendFunction::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} 286 // [email protected]:* {{but in 'FirstModule' found private access specifier}} 287 #endif 288 } // namespace FriendFunction 289 290 namespace ImplicitDecl { 291 #if defined(FIRST) 292 struct S { }; 293 void S_Constructors() { 294 // Trigger creation of implicit contructors 295 S foo; 296 S bar = foo; 297 S baz(bar); 298 } 299 #elif defined(SECOND) 300 struct S { }; 301 #else 302 S s; 303 #endif 304 305 #if defined(FIRST) 306 struct T { 307 private: 308 }; 309 void T_Constructors() { 310 // Trigger creation of implicit contructors 311 T foo; 312 T bar = foo; 313 T baz(bar); 314 } 315 #elif defined(SECOND) 316 struct T { 317 public: 318 }; 319 #else 320 T t; 321 // [email protected]:* {{'ImplicitDecl::T' has different definitions in different modules; first difference is definition in module 'FirstModule' found private access specifier}} 322 // [email protected]:* {{but in 'SecondModule' found public access specifier}} 323 #endif 324 325 } // namespace ImplicitDelc 326 327 namespace TemplatedClass { 328 #if defined(FIRST) 329 template <class> 330 struct S {}; 331 #elif defined(SECOND) 332 template <class> 333 struct S {}; 334 #else 335 S<int> s; 336 #endif 337 338 #if defined(FIRST) 339 template <class> 340 struct T { 341 private: 342 }; 343 #elif defined(SECOND) 344 template <class> 345 struct T { 346 public: 347 }; 348 #else 349 T<int> t; 350 // [email protected]:* {{'TemplatedClass::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} 351 // [email protected]:* {{but in 'FirstModule' found private access specifier}} 352 #endif 353 } // namespace TemplatedClass 354 355 namespace TemplateClassWithField { 356 #if defined(FIRST) 357 template <class A> 358 struct S { 359 A a; 360 }; 361 #elif defined(SECOND) 362 template <class A> 363 struct S { 364 A a; 365 }; 366 #else 367 S<int> s; 368 #endif 369 370 #if defined(FIRST) 371 template <class A> 372 struct T { 373 A a; 374 375 private: 376 }; 377 #elif defined(SECOND) 378 template <class A> 379 struct T { 380 A a; 381 382 public: 383 }; 384 #else 385 T<int> t; 386 // [email protected]:* {{'TemplateClassWithField::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} 387 // [email protected]:* {{but in 'FirstModule' found private access specifier}} 388 #endif 389 } // namespace TemplateClassWithField 390 391 namespace TemplateClassWithTemplateField { 392 #if defined(FIRST) 393 template <class A> 394 class WrapperS; 395 template <class A> 396 struct S { 397 WrapperS<A> a; 398 }; 399 #elif defined(SECOND) 400 template <class A> 401 class WrapperS; 402 template <class A> 403 struct S { 404 WrapperS<A> a; 405 }; 406 #else 407 template <class A> 408 class WrapperS{}; 409 S<int> s; 410 #endif 411 412 #if defined(FIRST) 413 template <class A> 414 class WrapperT; 415 template <class A> 416 struct T { 417 WrapperT<A> a; 418 419 public: 420 }; 421 #elif defined(SECOND) 422 template <class A> 423 class WrapperT; 424 template <class A> 425 struct T { 426 WrapperT<A> a; 427 428 private: 429 }; 430 #else 431 template <class A> 432 class WrapperT{}; 433 T<int> t; 434 // [email protected]:* {{'TemplateClassWithTemplateField::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} 435 // [email protected]:* {{but in 'FirstModule' found public access specifier}} 436 #endif 437 } // namespace TemplateClassWithTemplateField 438 439 namespace EnumWithForwardDeclaration { 440 #if defined(FIRST) 441 enum E : int; 442 struct S { 443 void get(E) {} 444 }; 445 #elif defined(SECOND) 446 enum E : int { A, B }; 447 struct S { 448 void get(E) {} 449 }; 450 #else 451 S s; 452 #endif 453 454 #if defined(FIRST) 455 struct T { 456 void get(E) {} 457 public: 458 }; 459 #elif defined(SECOND) 460 struct T { 461 void get(E) {} 462 private: 463 }; 464 #else 465 T t; 466 // [email protected]:* {{'EnumWithForwardDeclaration::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} 467 // [email protected]:* {{but in 'FirstModule' found public access specifier}} 468 #endif 469 } // namespace EnumWithForwardDeclaration 470 471 namespace StructWithForwardDeclaration { 472 #if defined(FIRST) 473 struct P {}; 474 struct S { 475 struct P *ptr; 476 }; 477 #elif defined(SECOND) 478 struct S { 479 struct P *ptr; 480 }; 481 #else 482 S s; 483 #endif 484 485 #if defined(FIRST) 486 struct Q {}; 487 struct T { 488 struct Q *ptr; 489 public: 490 }; 491 #elif defined(SECOND) 492 struct T { 493 struct Q *ptr; 494 private: 495 }; 496 #else 497 T t; 498 // [email protected]:* {{'StructWithForwardDeclaration::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} 499 // [email protected]:* {{but in 'FirstModule' found public access specifier}} 500 #endif 501 } // namespace StructWithForwardDeclaration 502 503 namespace StructWithForwardDeclarationNoDefinition { 504 #if defined(FIRST) 505 struct P; 506 struct S { 507 struct P *ptr; 508 }; 509 #elif defined(SECOND) 510 struct S { 511 struct P *ptr; 512 }; 513 #else 514 S s; 515 #endif 516 517 #if defined(FIRST) 518 struct Q; 519 struct T { 520 struct Q *ptr; 521 522 public: 523 }; 524 #elif defined(SECOND) 525 struct T { 526 struct Q *ptr; 527 528 private: 529 }; 530 #else 531 T t; 532 // [email protected]:* {{'StructWithForwardDeclarationNoDefinition::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} 533 // [email protected]:* {{but in 'FirstModule' found public access specifier}} 534 #endif 535 } // namespace StructWithForwardDeclarationNoDefinition 536 537 // Keep macros contained to one file. 538 #ifdef FIRST 539 #undef FIRST 540 #endif 541 #ifdef SECOND 542 #undef SECOND 543 #endif 544