Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 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 |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame^] | 24 | // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x c++ -I%t/Inputs -verify %s -std=c++1z |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 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 | // expected-error@second.h:* {{'AccessSpecifiers::S1' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} |
| 42 | // expected-note@first.h:* {{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 | // expected-error@second.h:* {{'AccessSpecifiers::S2' has different definitions in different modules; first difference is definition in module 'SecondModule' found protected access specifier}} |
| 56 | // expected-note@first.h:* {{but in 'FirstModule' found public access specifier}} |
| 57 | #endif |
| 58 | } // namespace AccessSpecifiers |
| 59 | |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame^] | 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 | // expected-error@second.h:* {{'StaticAssert::S1' has different definitions in different modules; first difference is definition in module 'SecondModule' found static assert with message}} |
| 72 | // expected-note@first.h:* {{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 | // expected-error@second.h:* {{'StaticAssert::S2' has different definitions in different modules; first difference is definition in module 'SecondModule' found static assert with no message}} |
| 86 | // expected-note@first.h:* {{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 | // expected-error@second.h:* {{'StaticAssert::S3' has different definitions in different modules; first difference is definition in module 'SecondModule' found static assert with condition}} |
| 100 | // expected-note@first.h:* {{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 | // expected-error@second.h:* {{'StaticAssert::S4' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} |
| 114 | // expected-note@first.h:* {{but in 'FirstModule' found static assert}} |
| 115 | #endif |
| 116 | } |
| 117 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 118 | // Naive parsing of AST can lead to cycles in processing. Ensure |
| 119 | // self-references don't trigger an endless cycles of AST node processing. |
| 120 | namespace SelfReference { |
| 121 | #if defined(FIRST) |
| 122 | template <template <int> class T> class Wrapper {}; |
| 123 | |
| 124 | template <int N> class S { |
| 125 | S(Wrapper<::SelfReference::S> &Ref) {} |
| 126 | }; |
| 127 | |
| 128 | struct Xx { |
| 129 | struct Yy { |
| 130 | }; |
| 131 | }; |
| 132 | |
| 133 | Xx::Xx::Xx::Yy yy; |
| 134 | |
| 135 | namespace NNS { |
| 136 | template <typename> struct Foo; |
| 137 | template <template <class> class T = NNS::Foo> |
| 138 | struct NestedNamespaceSpecifier {}; |
| 139 | } |
| 140 | #endif |
| 141 | } // namespace SelfReference |
| 142 | |
| 143 | // Interesting cases that should not cause errors. struct S should not error |
| 144 | // while struct T should error at the access specifier mismatch at the end. |
| 145 | namespace AllDecls { |
| 146 | #if defined(FIRST) |
| 147 | struct S { |
| 148 | public: |
| 149 | private: |
| 150 | protected: |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame^] | 151 | |
| 152 | static_assert(1 == 1, "Message"); |
| 153 | static_assert(2 == 2); |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 154 | }; |
| 155 | #elif defined(SECOND) |
| 156 | struct S { |
| 157 | public: |
| 158 | private: |
| 159 | protected: |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame^] | 160 | |
| 161 | static_assert(1 == 1, "Message"); |
| 162 | static_assert(2 == 2); |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 163 | }; |
| 164 | #else |
| 165 | S s; |
| 166 | #endif |
| 167 | |
| 168 | #if defined(FIRST) |
| 169 | struct T { |
| 170 | public: |
| 171 | private: |
| 172 | protected: |
| 173 | |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame^] | 174 | static_assert(1 == 1, "Message"); |
| 175 | static_assert(2 == 2); |
| 176 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 177 | private: |
| 178 | }; |
| 179 | #elif defined(SECOND) |
| 180 | struct T { |
| 181 | public: |
| 182 | private: |
| 183 | protected: |
| 184 | |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame^] | 185 | static_assert(1 == 1, "Message"); |
| 186 | static_assert(2 == 2); |
| 187 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 188 | public: |
| 189 | }; |
| 190 | #else |
| 191 | T t; |
| 192 | // expected-error@second.h:* {{'AllDecls::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} |
| 193 | // expected-note@first.h:* {{but in 'FirstModule' found private access specifier}} |
| 194 | #endif |
| 195 | } |
| 196 | |
| 197 | namespace FriendFunction { |
| 198 | #if defined(FIRST) |
| 199 | void F(int = 0); |
| 200 | struct S { friend void F(int); }; |
| 201 | #elif defined(SECOND) |
| 202 | void F(int); |
| 203 | struct S { friend void F(int); }; |
| 204 | #else |
| 205 | S s; |
| 206 | #endif |
| 207 | |
| 208 | #if defined(FIRST) |
| 209 | void G(int = 0); |
| 210 | struct T { |
| 211 | friend void G(int); |
| 212 | |
| 213 | private: |
| 214 | }; |
| 215 | #elif defined(SECOND) |
| 216 | void G(int); |
| 217 | struct T { |
| 218 | friend void G(int); |
| 219 | |
| 220 | public: |
| 221 | }; |
| 222 | #else |
| 223 | T t; |
| 224 | // expected-error@second.h:* {{'FriendFunction::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} |
| 225 | // expected-note@first.h:* {{but in 'FirstModule' found private access specifier}} |
| 226 | #endif |
| 227 | } // namespace FriendFunction |
| 228 | |
| 229 | namespace ImplicitDecl { |
| 230 | #if defined(FIRST) |
| 231 | struct S { }; |
| 232 | void S_Constructors() { |
| 233 | // Trigger creation of implicit contructors |
| 234 | S foo; |
| 235 | S bar = foo; |
| 236 | S baz(bar); |
| 237 | } |
| 238 | #elif defined(SECOND) |
| 239 | struct S { }; |
| 240 | #else |
| 241 | S s; |
| 242 | #endif |
| 243 | |
| 244 | #if defined(FIRST) |
| 245 | struct T { |
| 246 | private: |
| 247 | }; |
| 248 | void T_Constructors() { |
| 249 | // Trigger creation of implicit contructors |
| 250 | T foo; |
| 251 | T bar = foo; |
| 252 | T baz(bar); |
| 253 | } |
| 254 | #elif defined(SECOND) |
| 255 | struct T { |
| 256 | public: |
| 257 | }; |
| 258 | #else |
| 259 | T t; |
| 260 | // expected-error@first.h:* {{'ImplicitDecl::T' has different definitions in different modules; first difference is definition in module 'FirstModule' found private access specifier}} |
| 261 | // expected-note@second.h:* {{but in 'SecondModule' found public access specifier}} |
| 262 | #endif |
| 263 | |
| 264 | } // namespace ImplicitDelc |
| 265 | |
| 266 | namespace TemplatedClass { |
| 267 | #if defined(FIRST) |
| 268 | template <class> |
| 269 | struct S {}; |
| 270 | #elif defined(SECOND) |
| 271 | template <class> |
| 272 | struct S {}; |
| 273 | #else |
| 274 | S<int> s; |
| 275 | #endif |
| 276 | |
| 277 | #if defined(FIRST) |
| 278 | template <class> |
| 279 | struct T { |
| 280 | private: |
| 281 | }; |
| 282 | #elif defined(SECOND) |
| 283 | template <class> |
| 284 | struct T { |
| 285 | public: |
| 286 | }; |
| 287 | #else |
| 288 | T<int> t; |
| 289 | // expected-error@second.h:* {{'TemplatedClass::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} |
| 290 | // expected-note@first.h:* {{but in 'FirstModule' found private access specifier}} |
| 291 | #endif |
| 292 | } // namespace TemplatedClass |
| 293 | |
| 294 | namespace TemplateClassWithField { |
| 295 | #if defined(FIRST) |
| 296 | template <class A> |
| 297 | struct S { |
| 298 | A a; |
| 299 | }; |
| 300 | #elif defined(SECOND) |
| 301 | template <class A> |
| 302 | struct S { |
| 303 | A a; |
| 304 | }; |
| 305 | #else |
| 306 | S<int> s; |
| 307 | #endif |
| 308 | |
| 309 | #if defined(FIRST) |
| 310 | template <class A> |
| 311 | struct T { |
| 312 | A a; |
| 313 | |
| 314 | private: |
| 315 | }; |
| 316 | #elif defined(SECOND) |
| 317 | template <class A> |
| 318 | struct T { |
| 319 | A a; |
| 320 | |
| 321 | public: |
| 322 | }; |
| 323 | #else |
| 324 | T<int> t; |
| 325 | // expected-error@second.h:* {{'TemplateClassWithField::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}} |
| 326 | // expected-note@first.h:* {{but in 'FirstModule' found private access specifier}} |
| 327 | #endif |
| 328 | } // namespace TemplateClassWithField |
| 329 | |
| 330 | namespace TemplateClassWithTemplateField { |
| 331 | #if defined(FIRST) |
| 332 | template <class A> |
| 333 | class WrapperS; |
| 334 | template <class A> |
| 335 | struct S { |
| 336 | WrapperS<A> a; |
| 337 | }; |
| 338 | #elif defined(SECOND) |
| 339 | template <class A> |
| 340 | class WrapperS; |
| 341 | template <class A> |
| 342 | struct S { |
| 343 | WrapperS<A> a; |
| 344 | }; |
| 345 | #else |
| 346 | template <class A> |
| 347 | class WrapperS{}; |
| 348 | S<int> s; |
| 349 | #endif |
| 350 | |
| 351 | #if defined(FIRST) |
| 352 | template <class A> |
| 353 | class WrapperT; |
| 354 | template <class A> |
| 355 | struct T { |
| 356 | WrapperT<A> a; |
| 357 | |
| 358 | public: |
| 359 | }; |
| 360 | #elif defined(SECOND) |
| 361 | template <class A> |
| 362 | class WrapperT; |
| 363 | template <class A> |
| 364 | struct T { |
| 365 | WrapperT<A> a; |
| 366 | |
| 367 | private: |
| 368 | }; |
| 369 | #else |
| 370 | template <class A> |
| 371 | class WrapperT{}; |
| 372 | T<int> t; |
| 373 | // expected-error@second.h:* {{'TemplateClassWithTemplateField::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} |
| 374 | // expected-note@first.h:* {{but in 'FirstModule' found public access specifier}} |
| 375 | #endif |
| 376 | } // namespace TemplateClassWithTemplateField |
| 377 | |
| 378 | namespace EnumWithForwardDeclaration { |
| 379 | #if defined(FIRST) |
| 380 | enum E : int; |
| 381 | struct S { |
| 382 | void get(E) {} |
| 383 | }; |
| 384 | #elif defined(SECOND) |
| 385 | enum E : int { A, B }; |
| 386 | struct S { |
| 387 | void get(E) {} |
| 388 | }; |
| 389 | #else |
| 390 | S s; |
| 391 | #endif |
| 392 | |
| 393 | #if defined(FIRST) |
| 394 | struct T { |
| 395 | void get(E) {} |
| 396 | public: |
| 397 | }; |
| 398 | #elif defined(SECOND) |
| 399 | struct T { |
| 400 | void get(E) {} |
| 401 | private: |
| 402 | }; |
| 403 | #else |
| 404 | T t; |
| 405 | // expected-error@second.h:* {{'EnumWithForwardDeclaration::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} |
| 406 | // expected-note@first.h:* {{but in 'FirstModule' found public access specifier}} |
| 407 | #endif |
| 408 | } // namespace EnumWithForwardDeclaration |
| 409 | |
| 410 | namespace StructWithForwardDeclaration { |
| 411 | #if defined(FIRST) |
| 412 | struct P {}; |
| 413 | struct S { |
| 414 | struct P *ptr; |
| 415 | }; |
| 416 | #elif defined(SECOND) |
| 417 | struct S { |
| 418 | struct P *ptr; |
| 419 | }; |
| 420 | #else |
| 421 | S s; |
| 422 | #endif |
| 423 | |
| 424 | #if defined(FIRST) |
| 425 | struct Q {}; |
| 426 | struct T { |
| 427 | struct Q *ptr; |
| 428 | public: |
| 429 | }; |
| 430 | #elif defined(SECOND) |
| 431 | struct T { |
| 432 | struct Q *ptr; |
| 433 | private: |
| 434 | }; |
| 435 | #else |
| 436 | T t; |
| 437 | // expected-error@second.h:* {{'StructWithForwardDeclaration::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} |
| 438 | // expected-note@first.h:* {{but in 'FirstModule' found public access specifier}} |
| 439 | #endif |
| 440 | } // namespace StructWithForwardDeclaration |
| 441 | |
| 442 | namespace StructWithForwardDeclarationNoDefinition { |
| 443 | #if defined(FIRST) |
| 444 | struct P; |
| 445 | struct S { |
| 446 | struct P *ptr; |
| 447 | }; |
| 448 | #elif defined(SECOND) |
| 449 | struct S { |
| 450 | struct P *ptr; |
| 451 | }; |
| 452 | #else |
| 453 | S s; |
| 454 | #endif |
| 455 | |
| 456 | #if defined(FIRST) |
| 457 | struct Q; |
| 458 | struct T { |
| 459 | struct Q *ptr; |
| 460 | |
| 461 | public: |
| 462 | }; |
| 463 | #elif defined(SECOND) |
| 464 | struct T { |
| 465 | struct Q *ptr; |
| 466 | |
| 467 | private: |
| 468 | }; |
| 469 | #else |
| 470 | T t; |
| 471 | // expected-error@second.h:* {{'StructWithForwardDeclarationNoDefinition::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}} |
| 472 | // expected-note@first.h:* {{but in 'FirstModule' found public access specifier}} |
| 473 | #endif |
| 474 | } // namespace StructWithForwardDeclarationNoDefinition |
| 475 | |
| 476 | // Keep macros contained to one file. |
| 477 | #ifdef FIRST |
| 478 | #undef FIRST |
| 479 | #endif |
| 480 | #ifdef SECOND |
| 481 | #undef SECOND |
| 482 | #endif |