xref: /llvm-project/clang/test/CXX/module/module.interface/p2.cpp (revision db3d0e4dfa34e59fab90c0726a6722f82db48462)
1 // RUN: rm -rf %t
2 // RUN: mkdir -p %t
3 // RUN: %clang_cc1 -std=c++20 -x c++-header %S/Inputs/header.h -emit-header-unit -o %t/h.pcm
4 // RUN: %clang_cc1 -std=c++20 %s -DX_INTERFACE -emit-module-interface -o %t/x.pcm
5 // RUN: %clang_cc1 -std=c++20 %s -DY_INTERFACE -emit-module-interface -o %t/y.pcm
6 // RUN: %clang_cc1 -std=c++20 %s -DINTERFACE -fmodule-file=X=%t/x.pcm -fmodule-file=Y=%t/y.pcm -emit-module-interface -o %t/m.pcm
7 // RUN: %clang_cc1 -std=c++20 %s -DIMPLEMENTATION -I%S/Inputs -fmodule-file=%t/h.pcm \
8 // RUN:   -fmodule-file=X=%t/x.pcm -fmodule-file=Y=%t/y.pcm -fmodule-file=p2=%t/m.pcm -verify \
9 // RUN:   -Wno-experimental-header-units
10 // RUN: %clang_cc1 -std=c++20 %s -DUSER -I%S/Inputs -fmodule-file=%t/h.pcm -fmodule-file=p2=%t/m.pcm \
11 // RUN:   -fmodule-file=X=%t/x.pcm -fmodule-file=Y=%t/y.pcm -Wno-experimental-header-units -verify
12 
13 #if defined(X_INTERFACE)
14 export module X;
15 export int x;
16 
17 #elif defined(Y_INTERFACE)
18 export module Y;
19 export int y;
20 
21 #elif defined(INTERFACE)
22 export module p2;
23 export import X;
24 import Y; // not exported
25 
26 namespace A {
27   int f();
28   export int g();
29   int h();
30   namespace inner {}
31 }
32 export namespace B {
33   namespace inner {}
34 }
35 namespace B {
36   int f();
37 }
38 namespace C {}
39 namespace D { int f(); }
40 export namespace D {}
41 
42 #elif defined(IMPLEMENTATION)
43 module p2;
44 import "header.h";
45 
46 // Per [basic.scope.namespace]/2.3, exportedness has no impact on visibility
47 // within the same module.
48 //
49 // expected-no-diagnostics
50 
use()51 void use() {
52   A::f();
53   A::g();
54   A::h();
55   using namespace A::inner;
56 
57   using namespace B;
58   using namespace B::inner;
59   B::f();
60   f();
61 
62   using namespace C;
63 
64   D::f();
65 }
66 
use_header()67 int use_header() { return foo + bar::baz(); }
68 
69 #elif defined(USER)
70 import p2;
71 import "header.h";
72 
use()73 void use() {
74   // namespace A is implicitly exported by the export of A::g.
75   A::f(); // expected-error {{declaration of 'f' must be imported from module 'p2' before it is required}}
76           // expected-note@* {{declaration here is not visible}}
77   A::g();
78   A::h();                   // expected-error {{declaration of 'h' must be imported from module 'p2' before it is required}}
79                             // expected-note@* {{declaration here is not visible}}
80   using namespace A::inner; // expected-error {{declaration of 'inner' must be imported from module 'p2' before it is required}}
81 
82   // namespace B and B::inner are explicitly exported
83   using namespace B;
84   using namespace B::inner;
85   B::f(); // expected-error {{declaration of 'f' must be imported from module 'p2' before it is required}}
86           // expected-note@* {{declaration here is not visible}}
87   f();    // expected-error {{declaration of 'f' must be imported from module 'p2' before it is required}}
88           // expected-note@* {{declaration here is not visible}}
89 
90   // namespace C is not exported
91   using namespace C; // expected-error {{declaration of 'C' must be imported from module 'p2' before it is required}}
92 
93   // namespace D is exported, but D::f is not
94   D::f(); // expected-error {{declaration of 'f' must be imported from module 'p2' before it is required}}
95           // expected-note@* {{declaration here is not visible}}
96 }
97 
use_header()98 int use_header() { return foo + bar::baz(); }
99 
100 #else
101 #error unknown mode
102 #endif
103