xref: /llvm-project/clang/test/Modules/Reachability-Private.cpp (revision da00c60dae0040185dc45039c4397f6e746548e9)
1 // Tests that the definition in private module fragment is not reachable to its users.
2 //
3 // RUN: rm -rf %t
4 // RUN: mkdir -p %t
5 // RUN: split-file %s %t
6 //
7 // RUN: %clang_cc1 -std=c++20 %t/Private.cppm -emit-module-interface \
8 // RUN: -o %t/Private.pcm
9 // RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp \
10 // RUN: -DTEST_BADINLINE -verify -fsyntax-only
11 
12 // Test again with reduced BMI.
13 // RUN: rm -rf %t
14 // RUN: mkdir -p %t
15 // RUN: split-file %s %t
16 //
17 // RUN: %clang_cc1 -std=c++20 %t/Private.cppm -emit-reduced-module-interface \
18 // RUN: -o %t/Private.pcm
19 // RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp \
20 // RUN: -DTEST_BADINLINE -verify -fsyntax-only
21 
22 //--- Private.cppm
23 export module Private;
24 #ifdef TEST_BADINLINE
25 inline void fn_m(); // expected-error {{un-exported inline function not defined before the private module fragment}}
26                     // expected-note@Private.cppm:13 {{private module fragment begins here}}
27 #endif
28 static void fn_s();
29 export struct X;
30 
g(X * x)31 export void g(X *x) {
32   fn_s(); // OK, call to static function in same translation unit
33 #ifdef TEST_BADINLINE
34   fn_m(); // fn_m is not OK.
35 #endif
36 }
37 export X *factory(); // OK
38 
39 module :private;
40 struct X {}; // definition not reachable from importers of A
factory()41 X *factory() {
42   return new X();
43 }
fn_m()44 void fn_m() {}
fn_s()45 void fn_s() {}
46 
47 //--- Use.cpp
48 import Private;
foo()49 void foo() {
50   X x; // expected-error 1+{{missing '#include'; 'X' must be defined before it is used}}
51        // expected-note@Private.cppm:18 1+{{definition here is not reachable}}
52   auto _ = factory();
53   auto *__ = factory();
54   X *___ = factory();
55 
56   g(__);
57   g(___);
58   g(factory());
59 }
60