xref: /aosp_15_r20/external/clang/test/Modules/macros.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: rm -rf %t
2*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s
3*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s -DALT
4*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s -detailed-preprocessing-record
5*67e74705SXin Li // RUN: not %clang_cc1 -E -fmodules -fimplicit-module-maps -x objective-c -fmodules-cache-path=%t -I %S/Inputs %s | FileCheck -check-prefix CHECK-PREPROCESSED %s
6*67e74705SXin Li //
7*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -x objective-c++ -verify -fmodules-cache-path=%t -I %S/Inputs %s
8*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -x objective-c++ -verify -fmodules-cache-path=%t -I %S/Inputs %s -DALT
9*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -x objective-c++ -verify -fmodules-cache-path=%t -I %S/Inputs %s -detailed-preprocessing-record
10*67e74705SXin Li // RUN: not %clang_cc1 -E -fmodules -fimplicit-module-maps -x objective-c++ -fmodules-cache-path=%t -I %S/Inputs %s | FileCheck -check-prefix CHECK-PREPROCESSED %s
11*67e74705SXin Li //
12*67e74705SXin Li // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -DLOCAL_VISIBILITY -fmodules-local-submodule-visibility -x objective-c++ -verify -fmodules-cache-path=%t -I %S/Inputs %s
13*67e74705SXin Li // FIXME: When we have a syntax for modules in C, use that.
14*67e74705SXin Li // These notes come from headers in modules, and are bogus.
15*67e74705SXin Li 
16*67e74705SXin Li // FIXME: expected-note@Inputs/macros_left.h:11{{previous definition is here}}
17*67e74705SXin Li // FIXME: expected-note@Inputs/macros_right.h:12{{previous definition is here}}
18*67e74705SXin Li // expected-note@Inputs/macros_right.h:12{{expanding this definition of 'LEFT_RIGHT_DIFFERENT'}}
19*67e74705SXin Li // expected-note@Inputs/macros_right.h:13{{expanding this definition of 'LEFT_RIGHT_DIFFERENT2'}}
20*67e74705SXin Li // expected-note@Inputs/macros_left.h:14{{other definition of 'LEFT_RIGHT_DIFFERENT'}}
21*67e74705SXin Li 
22*67e74705SXin Li @import macros;
23*67e74705SXin Li 
24*67e74705SXin Li #ifndef INTEGER
25*67e74705SXin Li #  error INTEGER macro should be visible
26*67e74705SXin Li #endif
27*67e74705SXin Li 
28*67e74705SXin Li #ifdef FLOAT
29*67e74705SXin Li #  error FLOAT macro should not be visible
30*67e74705SXin Li #endif
31*67e74705SXin Li 
32*67e74705SXin Li #ifdef MODULE
33*67e74705SXin Li #  error MODULE macro should not be visible
34*67e74705SXin Li #endif
35*67e74705SXin Li 
36*67e74705SXin Li #ifndef INDIRECTLY_IN_MACROS
37*67e74705SXin Li #  error INDIRECTLY_IN_MACROS should be visible
38*67e74705SXin Li #endif
39*67e74705SXin Li 
40*67e74705SXin Li // CHECK-PREPROCESSED: double d
41*67e74705SXin Li double d;
42*67e74705SXin Li DOUBLE *dp = &d;
43*67e74705SXin Li 
44*67e74705SXin Li #__public_macro WIBBLE // expected-error{{no macro named 'WIBBLE'}}
45*67e74705SXin Li 
f()46*67e74705SXin Li void f() {
47*67e74705SXin Li   // CHECK-PREPROCESSED: int i = INTEGER;
48*67e74705SXin Li   int i = INTEGER; // the value was exported, the macro was not.
49*67e74705SXin Li   i += macros; // expanded from __MODULE__ within the 'macros' module.
50*67e74705SXin Li }
51*67e74705SXin Li 
52*67e74705SXin Li #ifdef __MODULE__
53*67e74705SXin Li # error Not building a module!
54*67e74705SXin Li #endif
55*67e74705SXin Li 
56*67e74705SXin Li #if __building_module(macros)
57*67e74705SXin Li # error Not building a module
58*67e74705SXin Li #endif
59*67e74705SXin Li 
60*67e74705SXin Li // None of the modules we depend on have been imported, and therefore
61*67e74705SXin Li // their macros should not be visible.
62*67e74705SXin Li #ifdef LEFT
63*67e74705SXin Li #  error LEFT should not be visible
64*67e74705SXin Li #endif
65*67e74705SXin Li 
66*67e74705SXin Li #ifdef RIGHT
67*67e74705SXin Li #  error RIGHT should not be visible
68*67e74705SXin Li #endif
69*67e74705SXin Li 
70*67e74705SXin Li #ifdef TOP
71*67e74705SXin Li #  error TOP should not be visible
72*67e74705SXin Li #endif
73*67e74705SXin Li 
74*67e74705SXin Li #undef INTEGER
75*67e74705SXin Li #define INTEGER int
76*67e74705SXin Li 
77*67e74705SXin Li // Import left module (which also imports top)
78*67e74705SXin Li @import macros_left;
79*67e74705SXin Li 
80*67e74705SXin Li INTEGER my_integer = 0;
81*67e74705SXin Li 
82*67e74705SXin Li #ifndef LEFT
83*67e74705SXin Li #  error LEFT should be visible
84*67e74705SXin Li #endif
85*67e74705SXin Li 
86*67e74705SXin Li #ifdef RIGHT
87*67e74705SXin Li #  error RIGHT should not be visible
88*67e74705SXin Li #endif
89*67e74705SXin Li 
90*67e74705SXin Li #ifndef TOP
91*67e74705SXin Li #  error TOP should be visible
92*67e74705SXin Li #endif
93*67e74705SXin Li 
94*67e74705SXin Li #ifdef TOP_LEFT_UNDEF
95*67e74705SXin Li #  error TOP_LEFT_UNDEF should not be defined
96*67e74705SXin Li #endif
97*67e74705SXin Li 
test1()98*67e74705SXin Li void test1() {
99*67e74705SXin Li   int i;
100*67e74705SXin Li   TOP_RIGHT_REDEF *ip = &i;
101*67e74705SXin Li }
102*67e74705SXin Li 
103*67e74705SXin Li #define LEFT_RIGHT_DIFFERENT2 double // FIXME: expected-warning{{'LEFT_RIGHT_DIFFERENT2' macro redefined}} \
104*67e74705SXin Li                                      // expected-note{{other definition of 'LEFT_RIGHT_DIFFERENT2'}}
105*67e74705SXin Li 
106*67e74705SXin Li // Import right module (which also imports top)
107*67e74705SXin Li @import macros_right;
108*67e74705SXin Li 
109*67e74705SXin Li #undef LEFT_RIGHT_DIFFERENT3
110*67e74705SXin Li 
111*67e74705SXin Li #ifndef LEFT
112*67e74705SXin Li #  error LEFT should be visible
113*67e74705SXin Li #endif
114*67e74705SXin Li 
115*67e74705SXin Li #ifndef RIGHT
116*67e74705SXin Li #  error RIGHT should be visible
117*67e74705SXin Li #endif
118*67e74705SXin Li 
119*67e74705SXin Li #ifndef TOP
120*67e74705SXin Li #  error TOP should be visible
121*67e74705SXin Li #endif
122*67e74705SXin Li 
test2()123*67e74705SXin Li void test2() {
124*67e74705SXin Li   int i;
125*67e74705SXin Li   float f;
126*67e74705SXin Li   double d;
127*67e74705SXin Li   TOP_RIGHT_REDEF *fp = &f; // ok, right's definition overrides top's definition
128*67e74705SXin Li 
129*67e74705SXin Li   LEFT_RIGHT_IDENTICAL *ip = &i;
130*67e74705SXin Li   LEFT_RIGHT_DIFFERENT *ip2 = &i; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT'}}
131*67e74705SXin Li   LEFT_RIGHT_DIFFERENT2 *ip3 = &i; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT2}}
132*67e74705SXin Li   int LEFT_RIGHT_DIFFERENT3;
133*67e74705SXin Li }
134*67e74705SXin Li 
135*67e74705SXin Li #define LEFT_RIGHT_DIFFERENT double // FIXME: expected-warning{{'LEFT_RIGHT_DIFFERENT' macro redefined}}
136*67e74705SXin Li 
test3()137*67e74705SXin Li void test3() {
138*67e74705SXin Li   double d;
139*67e74705SXin Li   LEFT_RIGHT_DIFFERENT *dp = &d; // okay
140*67e74705SXin Li   int x = FN_ADD(1,2);
141*67e74705SXin Li }
142*67e74705SXin Li 
143*67e74705SXin Li #ifndef TOP_RIGHT_UNDEF
144*67e74705SXin Li #  error TOP_RIGHT_UNDEF should still be defined
145*67e74705SXin Li #endif
146*67e74705SXin Li 
147*67e74705SXin Li @import macros_bottom;
148*67e74705SXin Li 
TDRUf()149*67e74705SXin Li TOP_DEF_RIGHT_UNDEF *TDRUf() { return TDRUp; }
150*67e74705SXin Li 
151*67e74705SXin Li @import macros_right.undef;
152*67e74705SXin Li 
153*67e74705SXin Li int TOP_DEF_RIGHT_UNDEF; // ok, no longer defined
154*67e74705SXin Li 
155*67e74705SXin Li #ifdef LOCAL_VISIBILITY
156*67e74705SXin Li // TOP_RIGHT_UNDEF should not be undefined, because macros_right.undef does
157*67e74705SXin Li // not undefine macros_right's macro.
158*67e74705SXin Li # ifndef TOP_RIGHT_UNDEF
159*67e74705SXin Li #  error TOP_RIGHT_UNDEF should still be defined
160*67e74705SXin Li # endif
161*67e74705SXin Li #else
162*67e74705SXin Li // When macros_right.undef is built and local submodule visibility is not
163*67e74705SXin Li // enabled, macros_top is visible because the state from building
164*67e74705SXin Li // macros_right leaks through, so macros_right.undef undefines macros_top's
165*67e74705SXin Li // macro.
166*67e74705SXin Li # ifdef TOP_RIGHT_UNDEF
167*67e74705SXin Li #  error TOP_RIGHT_UNDEF should not be defined
168*67e74705SXin Li # endif
169*67e74705SXin Li #endif
170*67e74705SXin Li 
171*67e74705SXin Li #ifdef ALT
172*67e74705SXin Li int tmp = TOP_OTHER_REDEF1;
173*67e74705SXin Li #endif
174*67e74705SXin Li 
175*67e74705SXin Li @import macros_other;
176*67e74705SXin Li 
177*67e74705SXin Li #ifndef TOP_OTHER_UNDEF1
178*67e74705SXin Li # error TOP_OTHER_UNDEF1 should still be defined
179*67e74705SXin Li #endif
180*67e74705SXin Li 
181*67e74705SXin Li #ifndef TOP_OTHER_UNDEF2
182*67e74705SXin Li # error TOP_OTHER_UNDEF2 should still be defined
183*67e74705SXin Li #endif
184*67e74705SXin Li #pragma clang __debug macro TOP_OTHER_REDEF1
185*67e74705SXin Li #ifndef TOP_OTHER_REDEF1
186*67e74705SXin Li # error TOP_OTHER_REDEF1 should still be defined
187*67e74705SXin Li #endif
188*67e74705SXin Li int n1 = TOP_OTHER_REDEF1; // expected-warning{{ambiguous expansion of macro 'TOP_OTHER_REDEF1'}}
189*67e74705SXin Li // expected-note@macros_other.h:4 {{expanding this definition}}
190*67e74705SXin Li // expected-note@macros_top.h:19 {{other definition}}
191*67e74705SXin Li 
192*67e74705SXin Li #ifndef TOP_OTHER_REDEF2
193*67e74705SXin Li # error TOP_OTHER_REDEF2 should still be defined
194*67e74705SXin Li #endif
195*67e74705SXin Li int n2 = TOP_OTHER_REDEF2; // ok
196*67e74705SXin Li 
197*67e74705SXin Li int n3 = TOP_OTHER_DEF_RIGHT_UNDEF; // ok
198