142583320cf7977fd474572b9fe78e13eaf45fb8fDouglas Gregor// RUN: rm -rf %t
2953a61f26bf79932b9699b09add4c388764de170Douglas Gregor// RUN: %clang_cc1 -fmodules -x objective-c -emit-module -fmodules-cache-path=%t -fmodule-name=macros_top %S/Inputs/module.map
3953a61f26bf79932b9699b09add4c388764de170Douglas Gregor// RUN: %clang_cc1 -fmodules -x objective-c -emit-module -fmodules-cache-path=%t -fmodule-name=macros_left %S/Inputs/module.map
4953a61f26bf79932b9699b09add4c388764de170Douglas Gregor// RUN: %clang_cc1 -fmodules -x objective-c -emit-module -fmodules-cache-path=%t -fmodule-name=macros_right %S/Inputs/module.map
5953a61f26bf79932b9699b09add4c388764de170Douglas Gregor// RUN: %clang_cc1 -fmodules -x objective-c -emit-module -fmodules-cache-path=%t -fmodule-name=macros %S/Inputs/module.map
6651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -fmodules -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s
7651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: not %clang_cc1 -E -fmodules -x objective-c -fmodules-cache-path=%t -I %S/Inputs %s | FileCheck -check-prefix CHECK-PREPROCESSED %s
8c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor// FIXME: When we have a syntax for modules in C, use that.
96c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor// These notes come from headers in modules, and are bogus.
10e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor
11b42f200777a66b98989160bf3987ce431540a584Andy Gibbs// FIXME: expected-note@Inputs/macros_left.h:11{{previous definition is here}}
12b42f200777a66b98989160bf3987ce431540a584Andy Gibbs// FIXME: expected-note@Inputs/macros_right.h:12{{previous definition is here}}
13b42f200777a66b98989160bf3987ce431540a584Andy Gibbs// expected-note@Inputs/macros_right.h:12{{expanding this definition of 'LEFT_RIGHT_DIFFERENT'}}
14b42f200777a66b98989160bf3987ce431540a584Andy Gibbs// expected-note@Inputs/macros_right.h:13{{expanding this definition of 'LEFT_RIGHT_DIFFERENT2'}}
15b42f200777a66b98989160bf3987ce431540a584Andy Gibbs// expected-note@Inputs/macros_left.h:14{{other definition of 'LEFT_RIGHT_DIFFERENT'}}
167143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
171b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor@import macros;
187143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
197143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#ifndef INTEGER
207143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#  error INTEGER macro should be visible
217143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#endif
227143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
237143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#ifdef FLOAT
247143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#  error FLOAT macro should not be visible
257143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#endif
267143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
277143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#ifdef MODULE
287143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#  error MODULE macro should not be visible
297143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor#endif
307143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
31de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor// CHECK-PREPROCESSED: double d
327143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregordouble d;
337143aab97c6e849a5a5005b7853b8c7d5af008edDouglas GregorDOUBLE *dp = &d;
347143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
351ac13c37d8af0145b7e03fea70b7b1476b8828c9Douglas Gregor#__public_macro WIBBLE // expected-error{{no macro named 'WIBBLE'}}
367143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor
37ce835df9301d5bee7fc2c70344e05541f21a5ddaDouglas Gregorvoid f() {
38de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor  // CHECK-PREPROCESSED: int i = INTEGER;
39ce835df9301d5bee7fc2c70344e05541f21a5ddaDouglas Gregor  int i = INTEGER; // the value was exported, the macro was not.
40b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  i += macros; // expanded from __MODULE__ within the 'macros' module.
41ce835df9301d5bee7fc2c70344e05541f21a5ddaDouglas Gregor}
42b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
43b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor#ifdef __MODULE__
44b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor# error Not building a module!
45b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor#endif
46b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
47b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor#if __building_module(macros)
48b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor# error Not building a module
49b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor#endif
506c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
516c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor// None of the modules we depend on have been imported, and therefore
526c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor// their macros should not be visible.
536c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifdef LEFT
546c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error LEFT should not be visible
556c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
566c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
576c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifdef RIGHT
586c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error RIGHT should not be visible
596c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
606c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
616c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifdef TOP
626c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error TOP should not be visible
636c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
646c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
656c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor// Import left module (which also imports top)
661b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor@import macros_left;
676c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
686c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifndef LEFT
696c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error LEFT should be visible
706c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
716c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
726c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifdef RIGHT
736c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error RIGHT should not be visible
746c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
756c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
766c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifndef TOP
776c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error TOP should be visible
786c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
796c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifdef TOP_LEFT_UNDEF
81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#  error TOP_LEFT_UNDEF should not be defined
826c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
836c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
846c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregorvoid test1() {
856c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  int i;
866c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  TOP_RIGHT_REDEF *ip = &i;
876c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor}
886c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
8935803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis#define LEFT_RIGHT_DIFFERENT2 double // FIXME: expected-warning{{'LEFT_RIGHT_DIFFERENT2' macro redefined}} \
9035803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis                                     // expected-note{{other definition of 'LEFT_RIGHT_DIFFERENT2'}}
916c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
926c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor// Import right module (which also imports top)
931b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor@import macros_right;
946c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
956c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#undef LEFT_RIGHT_DIFFERENT3
966c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
976c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifndef LEFT
986c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error LEFT should be visible
996c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
1006c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
1016c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifndef RIGHT
1026c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error RIGHT should be visible
1036c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
1046c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
1056c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#ifndef TOP
1066c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#  error TOP should be visible
1076c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#endif
1086c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
1096c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregorvoid test2() {
1106c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  int i;
1116c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  float f;
1126c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  double d;
113651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  TOP_RIGHT_REDEF *fp = &f; // ok, right's definition overrides top's definition
1146c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
11535803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  LEFT_RIGHT_IDENTICAL *ip = &i;
11635803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  LEFT_RIGHT_DIFFERENT *ip2 = &i; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT'}}
11735803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  LEFT_RIGHT_DIFFERENT2 *ip3 = &i; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT2}}
1186c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  int LEFT_RIGHT_DIFFERENT3;
1196c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor}
1206c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
1216c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor#define LEFT_RIGHT_DIFFERENT double // FIXME: expected-warning{{'LEFT_RIGHT_DIFFERENT' macro redefined}}
1226c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
1236c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregorvoid test3() {
1246c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  double d;
1256c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor  LEFT_RIGHT_DIFFERENT *dp = &d; // okay
126bd25ff8632e18bb3af761369fd3b0f1b48fdf061Argyrios Kyrtzidis  int x = FN_ADD(1,2);
1276c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor}
12854c8a40ed658676b1f3f983728feae488c501477Douglas Gregor
12954c8a40ed658676b1f3f983728feae488c501477Douglas Gregor#ifndef TOP_RIGHT_UNDEF
13054c8a40ed658676b1f3f983728feae488c501477Douglas Gregor#  error TOP_RIGHT_UNDEF should still be defined
13154c8a40ed658676b1f3f983728feae488c501477Douglas Gregor#endif
13254c8a40ed658676b1f3f983728feae488c501477Douglas Gregor
1331b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor@import macros_right.undef;
13454c8a40ed658676b1f3f983728feae488c501477Douglas Gregor
135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// FIXME: When macros_right.undef is built, macros_top is visible because
136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// the state from building macros_right leaks through, so macros_right.undef
137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// undefines macros_top's macro.
138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifdef TOP_RIGHT_UNDEF
139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines# error TOP_RIGHT_UNDEF should not be defined
140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
141651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
142651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines@import macros_other;
143651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
144651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifndef TOP_OTHER_UNDEF1
145651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines# error TOP_OTHER_UNDEF1 should still be defined
146651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
147651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
148651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifndef TOP_OTHER_UNDEF2
149651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines# error TOP_OTHER_UNDEF2 should still be defined
15054c8a40ed658676b1f3f983728feae488c501477Douglas Gregor#endif
151651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
152651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifndef TOP_OTHER_REDEF1
153651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines# error TOP_OTHER_REDEF1 should still be defined
154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
155651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint n1 = TOP_OTHER_REDEF1; // expected-warning{{ambiguous expansion of macro 'TOP_OTHER_REDEF1'}}
156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// expected-note@macros_top.h:19 {{expanding this definition}}
157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// expected-note@macros_other.h:4 {{other definition}}
158651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifndef TOP_OTHER_REDEF2
160651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines# error TOP_OTHER_REDEF2 should still be defined
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint n2 = TOP_OTHER_REDEF2; // ok
163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint n3 = TOP_OTHER_DEF_RIGHT_UNDEF; // ok
165