xref: /llvm-project/clang/test/Modules/Reachability-Private.cpp (revision da00c60dae0040185dc45039c4397f6e746548e9)
19c04851cSChuanqi Xu // Tests that the definition in private module fragment is not reachable to its users.
29c04851cSChuanqi Xu //
39c04851cSChuanqi Xu // RUN: rm -rf %t
49c04851cSChuanqi Xu // RUN: mkdir -p %t
59c04851cSChuanqi Xu // RUN: split-file %s %t
69c04851cSChuanqi Xu //
7fee3ccccSIain Sandoe // RUN: %clang_cc1 -std=c++20 %t/Private.cppm -emit-module-interface \
8fee3ccccSIain Sandoe // RUN: -o %t/Private.pcm
9fee3ccccSIain Sandoe // RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp \
10fee3ccccSIain Sandoe // RUN: -DTEST_BADINLINE -verify -fsyntax-only
119c04851cSChuanqi Xu 
12*da00c60dSChuanqi Xu // Test again with reduced BMI.
13*da00c60dSChuanqi Xu // RUN: rm -rf %t
14*da00c60dSChuanqi Xu // RUN: mkdir -p %t
15*da00c60dSChuanqi Xu // RUN: split-file %s %t
16*da00c60dSChuanqi Xu //
17*da00c60dSChuanqi Xu // RUN: %clang_cc1 -std=c++20 %t/Private.cppm -emit-reduced-module-interface \
18*da00c60dSChuanqi Xu // RUN: -o %t/Private.pcm
19*da00c60dSChuanqi Xu // RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp \
20*da00c60dSChuanqi Xu // RUN: -DTEST_BADINLINE -verify -fsyntax-only
21*da00c60dSChuanqi Xu 
229c04851cSChuanqi Xu //--- Private.cppm
239c04851cSChuanqi Xu export module Private;
24fee3ccccSIain Sandoe #ifdef TEST_BADINLINE
25fee3ccccSIain Sandoe inline void fn_m(); // expected-error {{un-exported inline function not defined before the private module fragment}}
26fee3ccccSIain Sandoe                     // expected-note@Private.cppm:13 {{private module fragment begins here}}
27fee3ccccSIain Sandoe #endif
289c04851cSChuanqi Xu static void fn_s();
299c04851cSChuanqi Xu export struct X;
309c04851cSChuanqi Xu 
g(X * x)319c04851cSChuanqi Xu export void g(X *x) {
329c04851cSChuanqi Xu   fn_s(); // OK, call to static function in same translation unit
33fee3ccccSIain Sandoe #ifdef TEST_BADINLINE
34fee3ccccSIain Sandoe   fn_m(); // fn_m is not OK.
35fee3ccccSIain Sandoe #endif
369c04851cSChuanqi Xu }
379c04851cSChuanqi Xu export X *factory(); // OK
389c04851cSChuanqi Xu 
399c04851cSChuanqi Xu module :private;
409c04851cSChuanqi Xu struct X {}; // definition not reachable from importers of A
factory()419c04851cSChuanqi Xu X *factory() {
429c04851cSChuanqi Xu   return new X();
439c04851cSChuanqi Xu }
fn_m()449c04851cSChuanqi Xu void fn_m() {}
fn_s()459c04851cSChuanqi Xu void fn_s() {}
469c04851cSChuanqi Xu 
479c04851cSChuanqi Xu //--- Use.cpp
489c04851cSChuanqi Xu import Private;
foo()499c04851cSChuanqi Xu void foo() {
50fee3ccccSIain Sandoe   X x; // expected-error 1+{{missing '#include'; 'X' must be defined before it is used}}
51fee3ccccSIain Sandoe        // expected-note@Private.cppm:18 1+{{definition here is not reachable}}
529c04851cSChuanqi Xu   auto _ = factory();
539c04851cSChuanqi Xu   auto *__ = factory();
549c04851cSChuanqi Xu   X *___ = factory();
559c04851cSChuanqi Xu 
569c04851cSChuanqi Xu   g(__);
579c04851cSChuanqi Xu   g(___);
589c04851cSChuanqi Xu   g(factory());
599c04851cSChuanqi Xu }
60