1 // RUN: rm -rf %t
2 // RUN: mkdir -p %t
3 // RUN: echo '#ifndef FOO_H' > %t/foo.h
4 // RUN: echo '#define FOO_H' >> %t/foo.h
5 // RUN: echo 'extern int in_header;' >> %t/foo.h
6 // RUN: echo '#endif' >> %t/foo.h
7 // RUN: %clang_cc1 -std=c++2a -I%t -emit-module-interface -DINTERFACE %s -o %t.pcm
8 // RUN: %clang_cc1 -std=c++2a -I%t -fmodule-file=%t.pcm -DIMPLEMENTATION %s -verify -fno-modules-error-recovery
9 // RUN: %clang_cc1 -std=c++2a -I%t -fmodule-file=%t.pcm %s -verify -fno-modules-error-recovery
10 
11 #ifdef INTERFACE
12 module;
13 #include "foo.h"
14 // FIXME: The following need to be moved to a header file. The global module
15 // fragment is only permitted to contain preprocessor directives.
16 int global_module_fragment;
17 export module A;
18 export int exported;
19 int not_exported;
20 static int internal;
21 
22 module :private;
23 int not_exported_private;
24 static int internal_private;
25 #else
26 
27 #ifdef IMPLEMENTATION
28 module;
29 #endif
30 
31 void test_early() {
32   in_header = 1; // expected-error {{missing '#include "foo.h"'; 'in_header' must be declared before it is used}}
33   // expected-note@* {{not visible}}
34 
35   global_module_fragment = 1; // expected-error {{missing '#include'; 'global_module_fragment' must be declared before it is used}}
36                               // [email protected]:16 {{not visible}}
37 
38   exported = 1; // expected-error {{must be imported from module 'A'}}
39                 // [email protected]:18 {{declaration here is not visible}}
40 
41   not_exported = 1; // expected-error {{declaration of 'not_exported' must be imported from module 'A' before it is required}}
42                     // [email protected]:19 {{declaration here is not visible}}
43 
44   // FIXME: We need better diagnostic message for static variable.
45   internal = 1; // expected-error {{declaration of 'internal' must be imported from module 'A' before it is required}}
46                 // [email protected]:20 {{declaration here is not visible}}
47 
48   not_exported_private = 1; // expected-error {{undeclared identifier}}
49 
50   internal_private = 1; // expected-error {{undeclared identifier}}
51 }
52 
53 #ifdef IMPLEMENTATION
54 module A;
55 #else
56 import A;
57 #endif
58 
59 void test_late() {
60   in_header = 1; // expected-error {{missing '#include "foo.h"'; 'in_header' must be declared before it is used}}
61   // expected-note@* {{not visible}}
62 
63   global_module_fragment = 1; // expected-error {{missing '#include'; 'global_module_fragment' must be declared before it is used}}
64   // [email protected]:16 {{not visible}}
65 
66   exported = 1;
67 
68   not_exported = 1;
69 #ifndef IMPLEMENTATION
70   // expected-error@-2 {{declaration of 'not_exported' must be imported from module 'A' before it is required}}
71   // [email protected]:19 {{declaration here is not visible}}
72 #endif
73 
74   internal = 1;
75 #ifndef IMPLEMENTATION
76   // expected-error@-2 {{declaration of 'internal' must be imported from module 'A' before it is required}}
77   // [email protected]:20 {{declaration here is not visible}}
78 #endif
79 
80   not_exported_private = 1;
81 #ifndef IMPLEMENTATION
82   // FIXME: should not be visible here
83   // expected-error@-3 {{undeclared identifier}}
84 #endif
85 
86   internal_private = 1;
87 #ifndef IMPLEMENTATION
88   // FIXME: should not be visible here
89   // expected-error@-3 {{undeclared identifier}}
90 #endif
91 }
92 
93 #endif
94