blob: e485f633f89dfc97efa864674f070b4d60655b05 [file] [log] [blame]
Richard Trieue7f7ed22017-02-22 01:11:25 +00001// 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 Trieu639d7b62017-02-22 22:22:42 +000024// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x c++ -I%t/Inputs -verify %s -std=c++1z
Richard Trieue7f7ed22017-02-22 01:11:25 +000025
26#if !defined(FIRST) && !defined(SECOND)
27#include "first.h"
28#include "second.h"
29#endif
30
31namespace AccessSpecifiers {
32#if defined(FIRST)
33struct S1 {
34};
35#elif defined(SECOND)
36struct S1 {
37 private:
38};
39#else
40S1 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)
46struct S2 {
47 public:
48};
49#elif defined(SECOND)
50struct S2 {
51 protected:
52};
53#else
54S2 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 Trieu639d7b62017-02-22 22:22:42 +000060namespace StaticAssert {
61#if defined(FIRST)
62struct S1 {
63 static_assert(1 == 1, "First");
64};
65#elif defined(SECOND)
66struct S1 {
67 static_assert(1 == 1, "Second");
68};
69#else
70S1 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)
76struct S2 {
77 static_assert(2 == 2, "Message");
78};
79#elif defined(SECOND)
80struct S2 {
81 static_assert(2 == 2);
82};
83#else
84S2 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)
90struct S3 {
91 static_assert(3 == 3, "Message");
92};
93#elif defined(SECOND)
94struct S3 {
95 static_assert(3 != 4, "Message");
96};
97#else
98S3 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)
104struct S4 {
105 static_assert(4 == 4, "Message");
106};
107#elif defined(SECOND)
108struct S4 {
109 public:
110};
111#else
112S4 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 Trieud0786092017-02-23 00:23:01 +0000118namespace Field {
119#if defined(FIRST)
120struct S1 {
121 int x;
122 private:
123 int y;
124};
125#elif defined(SECOND)
126struct S1 {
127 int x;
128 int y;
129};
130#else
131S1 s1;
132// expected-error@second.h:* {{'Field::S1' has different definitions in different modules; first difference is definition in module 'SecondModule' found field}}
133// expected-note@first.h:* {{but in 'FirstModule' found private access specifier}}
134#endif
135
136#if defined(FIRST)
137struct S2 {
138 int x;
139 int y;
140};
141#elif defined(SECOND)
142struct S2 {
143 int y;
144 int x;
145};
146#else
147S2 s2;
148// expected-error@second.h:* {{'Field::S2' has different definitions in different modules; first difference is definition in module 'SecondModule' found field 'y'}}
149// expected-note@first.h:* {{but in 'FirstModule' found field 'x'}}
150#endif
Richard Trieubcaaf962017-02-23 03:25:57 +0000151
152#if defined(FIRST)
153struct S3 {
154 double x;
155};
156#elif defined(SECOND)
157struct S3 {
158 int x;
159};
160#else
161S3 s3;
162// expected-error@first.h:* {{'Field::S3::x' from module 'FirstModule' is not present in definition of 'Field::S3' in module 'SecondModule'}}
163// expected-note@second.h:* {{declaration of 'x' does not match}}
164#endif
Richard Trieu8459ddf2017-02-24 02:59:12 +0000165
166#if defined(FIRST)
167typedef int A;
168struct S4 {
169 A x;
170};
171
172struct S5 {
173 A x;
174};
175#elif defined(SECOND)
176typedef int B;
177struct S4 {
178 B x;
179};
180
181struct S5 {
182 int x;
183};
184#else
185S4 s4;
186// expected-error@second.h:* {{'Field::S4' has different definitions in different modules; first difference is definition in module 'SecondModule' found field 'x' with type 'B' (aka 'int')}}
187// expected-note@first.h:* {{but in 'FirstModule' found field 'x' with type 'A' (aka 'int')}}
188
189S5 s5;
190// expected-error@second.h:* {{'Field::S5' has different definitions in different modules; first difference is definition in module 'SecondModule' found field 'x' with type 'int'}}
191// expected-note@first.h:* {{but in 'FirstModule' found field 'x' with type 'A' (aka 'int')}}
192#endif
193
Richard Trieu93772fc2017-02-24 20:59:28 +0000194#if defined(FIRST)
195struct S6 {
196 unsigned x;
197};
198#elif defined(SECOND)
199struct S6 {
200 unsigned x : 1;
201};
202#else
203S6 s6;
204// expected-error@second.h:* {{'Field::S6' has different definitions in different modules; first difference is definition in module 'SecondModule' found bitfield 'x'}}
205// expected-note@first.h:* {{but in 'FirstModule' found non-bitfield 'x'}}
206#endif
207
208#if defined(FIRST)
209struct S7 {
210 unsigned x : 2;
211};
212#elif defined(SECOND)
213struct S7 {
214 unsigned x : 1;
215};
216#else
217S7 s7;
218// expected-error@second.h:* {{'Field::S7' has different definitions in different modules; first difference is definition in module 'SecondModule' found bitfield 'x' with one width expression}}
219// expected-note@first.h:* {{but in 'FirstModule' found bitfield 'x' with different width expression}}
220#endif
221
222#if defined(FIRST)
223struct S8 {
224 unsigned x : 2;
225};
226#elif defined(SECOND)
227struct S8 {
228 unsigned x : 1 + 1;
229};
230#else
231S8 s8;
232// expected-error@second.h:* {{'Field::S8' has different definitions in different modules; first difference is definition in module 'SecondModule' found bitfield 'x' with one width expression}}
233// expected-note@first.h:* {{but in 'FirstModule' found bitfield 'x' with different width expression}}
234#endif
Richard Trieu8459ddf2017-02-24 02:59:12 +0000235
Richard Trieu8d543e22017-02-24 23:35:37 +0000236#if defined(FIRST)
237struct S9 {
238 mutable int x;
239};
240#elif defined(SECOND)
241struct S9 {
242 int x;
243};
244#else
245S9 s9;
246// expected-error@second.h:* {{'Field::S9' has different definitions in different modules; first difference is definition in module 'SecondModule' found non-mutable field 'x'}}
247// expected-note@first.h:* {{but in 'FirstModule' found mutable field 'x'}}
248#endif
249
250#if defined(FIRST)
251struct S10 {
252 unsigned x = 5;
253};
254#elif defined(SECOND)
255struct S10 {
256 unsigned x;
257};
258#else
259S10 s10;
260// expected-error@second.h:* {{'Field::S10' has different definitions in different modules; first difference is definition in module 'SecondModule' found field 'x' with no initalizer}}
261// expected-note@first.h:* {{but in 'FirstModule' found field 'x' with an initializer}}
262#endif
263
264#if defined(FIRST)
265struct S11 {
266 unsigned x = 5;
267};
268#elif defined(SECOND)
269struct S11 {
270 unsigned x = 7;
271};
272#else
273S11 s11;
274// expected-error@second.h:* {{'Field::S11' has different definitions in different modules; first difference is definition in module 'SecondModule' found field 'x' with an initializer}}
275// expected-note@first.h:* {{but in 'FirstModule' found field 'x' with a different initializer}}
276#endif
277
Richard Trieud0786092017-02-23 00:23:01 +0000278} // namespace Field
279
Richard Trieue7f7ed22017-02-22 01:11:25 +0000280// Naive parsing of AST can lead to cycles in processing. Ensure
281// self-references don't trigger an endless cycles of AST node processing.
282namespace SelfReference {
283#if defined(FIRST)
284template <template <int> class T> class Wrapper {};
285
286template <int N> class S {
287 S(Wrapper<::SelfReference::S> &Ref) {}
288};
289
290struct Xx {
291 struct Yy {
292 };
293};
294
295Xx::Xx::Xx::Yy yy;
296
297namespace NNS {
298template <typename> struct Foo;
299template <template <class> class T = NNS::Foo>
300struct NestedNamespaceSpecifier {};
301}
302#endif
303} // namespace SelfReference
304
305// Interesting cases that should not cause errors. struct S should not error
306// while struct T should error at the access specifier mismatch at the end.
307namespace AllDecls {
308#if defined(FIRST)
Richard Trieu8459ddf2017-02-24 02:59:12 +0000309typedef int INT;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000310struct S {
311 public:
312 private:
313 protected:
Richard Trieu639d7b62017-02-22 22:22:42 +0000314
315 static_assert(1 == 1, "Message");
316 static_assert(2 == 2);
Richard Trieud0786092017-02-23 00:23:01 +0000317
318 int x;
319 double y;
Richard Trieu8459ddf2017-02-24 02:59:12 +0000320
321 INT z;
Richard Trieu93772fc2017-02-24 20:59:28 +0000322
323 unsigned a : 1;
324 unsigned b : 2*2 + 5/2;
Richard Trieu8d543e22017-02-24 23:35:37 +0000325
326 mutable int c = sizeof(x + y);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000327};
328#elif defined(SECOND)
Richard Trieu8459ddf2017-02-24 02:59:12 +0000329typedef int INT;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000330struct S {
331 public:
332 private:
333 protected:
Richard Trieu639d7b62017-02-22 22:22:42 +0000334
335 static_assert(1 == 1, "Message");
336 static_assert(2 == 2);
Richard Trieud0786092017-02-23 00:23:01 +0000337
338 int x;
339 double y;
Richard Trieu8459ddf2017-02-24 02:59:12 +0000340
341 INT z;
Richard Trieu93772fc2017-02-24 20:59:28 +0000342
343 unsigned a : 1;
344 unsigned b : 2 * 2 + 5 / 2;
Richard Trieu8d543e22017-02-24 23:35:37 +0000345
346 mutable int c = sizeof(x + y);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000347};
348#else
349S s;
350#endif
351
352#if defined(FIRST)
Richard Trieu8459ddf2017-02-24 02:59:12 +0000353typedef int INT;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000354struct T {
355 public:
356 private:
357 protected:
358
Richard Trieu639d7b62017-02-22 22:22:42 +0000359 static_assert(1 == 1, "Message");
360 static_assert(2 == 2);
361
Richard Trieud0786092017-02-23 00:23:01 +0000362 int x;
363 double y;
364
Richard Trieu8459ddf2017-02-24 02:59:12 +0000365 INT z;
366
Richard Trieu93772fc2017-02-24 20:59:28 +0000367 unsigned a : 1;
368 unsigned b : 2 * 2 + 5 / 2;
369
Richard Trieu8d543e22017-02-24 23:35:37 +0000370 mutable int c = sizeof(x + y);
371
Richard Trieue7f7ed22017-02-22 01:11:25 +0000372 private:
373};
374#elif defined(SECOND)
Richard Trieu8459ddf2017-02-24 02:59:12 +0000375typedef int INT;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000376struct T {
377 public:
378 private:
379 protected:
380
Richard Trieu639d7b62017-02-22 22:22:42 +0000381 static_assert(1 == 1, "Message");
382 static_assert(2 == 2);
383
Richard Trieud0786092017-02-23 00:23:01 +0000384 int x;
385 double y;
386
Richard Trieu8459ddf2017-02-24 02:59:12 +0000387 INT z;
388
Richard Trieu93772fc2017-02-24 20:59:28 +0000389 unsigned a : 1;
390 unsigned b : 2 * 2 + 5 / 2;
391
Richard Trieu8d543e22017-02-24 23:35:37 +0000392 mutable int c = sizeof(x + y);
393
Richard Trieue7f7ed22017-02-22 01:11:25 +0000394 public:
395};
396#else
397T t;
398// expected-error@second.h:* {{'AllDecls::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}}
399// expected-note@first.h:* {{but in 'FirstModule' found private access specifier}}
400#endif
401}
402
403namespace FriendFunction {
404#if defined(FIRST)
405void F(int = 0);
406struct S { friend void F(int); };
407#elif defined(SECOND)
408void F(int);
409struct S { friend void F(int); };
410#else
411S s;
412#endif
413
414#if defined(FIRST)
415void G(int = 0);
416struct T {
417 friend void G(int);
418
419 private:
420};
421#elif defined(SECOND)
422void G(int);
423struct T {
424 friend void G(int);
425
426 public:
427};
428#else
429T t;
430// expected-error@second.h:* {{'FriendFunction::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}}
431// expected-note@first.h:* {{but in 'FirstModule' found private access specifier}}
432#endif
433} // namespace FriendFunction
434
435namespace ImplicitDecl {
436#if defined(FIRST)
437struct S { };
438void S_Constructors() {
439 // Trigger creation of implicit contructors
440 S foo;
441 S bar = foo;
442 S baz(bar);
443}
444#elif defined(SECOND)
445struct S { };
446#else
447S s;
448#endif
449
450#if defined(FIRST)
451struct T {
452 private:
453};
454void T_Constructors() {
455 // Trigger creation of implicit contructors
456 T foo;
457 T bar = foo;
458 T baz(bar);
459}
460#elif defined(SECOND)
461struct T {
462 public:
463};
464#else
465T t;
466// expected-error@first.h:* {{'ImplicitDecl::T' has different definitions in different modules; first difference is definition in module 'FirstModule' found private access specifier}}
467// expected-note@second.h:* {{but in 'SecondModule' found public access specifier}}
468#endif
469
470} // namespace ImplicitDelc
471
472namespace TemplatedClass {
473#if defined(FIRST)
474template <class>
475struct S {};
476#elif defined(SECOND)
477template <class>
478struct S {};
479#else
480S<int> s;
481#endif
482
483#if defined(FIRST)
484template <class>
485struct T {
486 private:
487};
488#elif defined(SECOND)
489template <class>
490struct T {
491 public:
492};
493#else
494T<int> t;
495// expected-error@second.h:* {{'TemplatedClass::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}}
496// expected-note@first.h:* {{but in 'FirstModule' found private access specifier}}
497#endif
498} // namespace TemplatedClass
499
500namespace TemplateClassWithField {
501#if defined(FIRST)
502template <class A>
503struct S {
504 A a;
505};
506#elif defined(SECOND)
507template <class A>
508struct S {
509 A a;
510};
511#else
512S<int> s;
513#endif
514
515#if defined(FIRST)
516template <class A>
517struct T {
518 A a;
519
520 private:
521};
522#elif defined(SECOND)
523template <class A>
524struct T {
525 A a;
526
527 public:
528};
529#else
530T<int> t;
531// expected-error@second.h:* {{'TemplateClassWithField::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found public access specifier}}
532// expected-note@first.h:* {{but in 'FirstModule' found private access specifier}}
533#endif
534} // namespace TemplateClassWithField
535
536namespace TemplateClassWithTemplateField {
537#if defined(FIRST)
538template <class A>
539class WrapperS;
540template <class A>
541struct S {
542 WrapperS<A> a;
543};
544#elif defined(SECOND)
545template <class A>
546class WrapperS;
547template <class A>
548struct S {
549 WrapperS<A> a;
550};
551#else
552template <class A>
553class WrapperS{};
554S<int> s;
555#endif
556
557#if defined(FIRST)
558template <class A>
559class WrapperT;
560template <class A>
561struct T {
562 WrapperT<A> a;
563
564 public:
565};
566#elif defined(SECOND)
567template <class A>
568class WrapperT;
569template <class A>
570struct T {
571 WrapperT<A> a;
572
573 private:
574};
575#else
576template <class A>
577class WrapperT{};
578T<int> t;
579// expected-error@second.h:* {{'TemplateClassWithTemplateField::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}}
580// expected-note@first.h:* {{but in 'FirstModule' found public access specifier}}
581#endif
582} // namespace TemplateClassWithTemplateField
583
584namespace EnumWithForwardDeclaration {
585#if defined(FIRST)
586enum E : int;
587struct S {
588 void get(E) {}
589};
590#elif defined(SECOND)
591enum E : int { A, B };
592struct S {
593 void get(E) {}
594};
595#else
596S s;
597#endif
598
599#if defined(FIRST)
600struct T {
601 void get(E) {}
602 public:
603};
604#elif defined(SECOND)
605struct T {
606 void get(E) {}
607 private:
608};
609#else
610T t;
611// expected-error@second.h:* {{'EnumWithForwardDeclaration::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}}
612// expected-note@first.h:* {{but in 'FirstModule' found public access specifier}}
613#endif
614} // namespace EnumWithForwardDeclaration
615
616namespace StructWithForwardDeclaration {
617#if defined(FIRST)
618struct P {};
619struct S {
620 struct P *ptr;
621};
622#elif defined(SECOND)
623struct S {
624 struct P *ptr;
625};
626#else
627S s;
628#endif
629
630#if defined(FIRST)
631struct Q {};
632struct T {
633 struct Q *ptr;
634 public:
635};
636#elif defined(SECOND)
637struct T {
638 struct Q *ptr;
639 private:
640};
641#else
642T t;
643// expected-error@second.h:* {{'StructWithForwardDeclaration::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}}
644// expected-note@first.h:* {{but in 'FirstModule' found public access specifier}}
645#endif
646} // namespace StructWithForwardDeclaration
647
648namespace StructWithForwardDeclarationNoDefinition {
649#if defined(FIRST)
650struct P;
651struct S {
652 struct P *ptr;
653};
654#elif defined(SECOND)
655struct S {
656 struct P *ptr;
657};
658#else
659S s;
660#endif
661
662#if defined(FIRST)
663struct Q;
664struct T {
665 struct Q *ptr;
666
667 public:
668};
669#elif defined(SECOND)
670struct T {
671 struct Q *ptr;
672
673 private:
674};
675#else
676T t;
677// expected-error@second.h:* {{'StructWithForwardDeclarationNoDefinition::T' has different definitions in different modules; first difference is definition in module 'SecondModule' found private access specifier}}
678// expected-note@first.h:* {{but in 'FirstModule' found public access specifier}}
679#endif
680} // namespace StructWithForwardDeclarationNoDefinition
681
682// Keep macros contained to one file.
683#ifdef FIRST
684#undef FIRST
685#endif
686#ifdef SECOND
687#undef SECOND
688#endif