xref: /llvm-project/clang/test/Modules/config_macros.m (revision ee044d5e651787c5d73b37b2cbb7ca6444bb0502)
1// This test verifies that config macro warnings are emitted when it looks like
2// the user expected a `#define` to impact the import of a module.
3
4// RUN: rm -rf %t
5// RUN: split-file %s %t
6
7// Prebuild the `config` module so it's in the module cache.
8// RUN: %clang_cc1 -std=c99 -fmodules -fimplicit-module-maps -x objective-c -fmodules-cache-path=%t -DWANT_FOO=1 -emit-module -fmodule-name=config %t/module.modulemap
9
10// Verify that each time the `config` module is imported the current macro state
11// is checked.
12// RUN: %clang_cc1 -std=c99 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %t -DWANT_FOO=1 %t/config.m -verify
13
14// Verify that warnings are emitted before building a module in case the command
15// line macro state causes the module to fail to build.
16// RUN: %clang_cc1 -std=c99 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %t %t/config_error.m -verify
17
18//--- module.modulemap
19
20module config {
21  header "config.h"
22  config_macros [exhaustive] WANT_FOO, WANT_BAR
23}
24
25module config_error {
26  header "config_error.h"
27  config_macros SOME_VALUE
28}
29
30//--- config.h
31
32#ifdef WANT_FOO
33int* foo(void);
34#endif
35
36#ifdef WANT_BAR
37char *bar(void);
38#endif
39
40//--- config_error.h
41
42struct my_thing {
43  char buf[SOME_VALUE];
44};
45
46//--- config.m
47
48@import config;
49
50int *test_foo(void) {
51  return foo();
52}
53
54char *test_bar(void) {
55  return bar(); // expected-error{{call to undeclared function 'bar'; ISO C99 and later do not support implicit function declarations}} \
56                // expected-error{{incompatible integer to pointer conversion}}
57}
58
59#undef WANT_FOO // expected-note{{macro was #undef'd here}}
60@import config; // expected-warning{{#undef of configuration macro 'WANT_FOO' has no effect on the import of 'config'; pass '-UWANT_FOO' on the command line to configure the module}}
61
62#define WANT_FOO 2 // expected-note{{macro was defined here}}
63@import config; // expected-warning{{definition of configuration macro 'WANT_FOO' has no effect on the import of 'config'; pass '-DWANT_FOO=...' on the command line to configure the module}}
64
65#undef WANT_FOO
66#define WANT_FOO 1
67@import config; // okay
68
69#define WANT_BAR 1 // expected-note{{macro was defined here}}
70@import config; // expected-warning{{definition of configuration macro 'WANT_BAR' has no effect on the import of 'config'; pass '-DWANT_BAR=...' on the command line to configure the module}}
71
72//--- config_error.m
73
74#define SOME_VALUE 5 // expected-note{{macro was defined here}}
75@import config_error; // expected-error{{could not build module}} \
76                      // expected-warning{{definition of configuration macro 'SOME_VALUE' has no effect on the import of 'config_error';}}
77