xref: /llvm-project/clang/test/Modules/cxx20-hu-04.cpp (revision 5523fefb01c282c4cbcaf6314a9aaf658c6c145f)
1 // Test macro preservation in C++20 Header Units.
2 
3 // RUN: rm -rf %t
4 // RUN: mkdir -p %t
5 // RUN: split-file %s %t
6 // RUN: cd %t
7 
8 // RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-01.h \
9 // RUN: -o hu-01.pcm
10 
11 // RUN: %clang_cc1 -std=c++20 -module-file-info hu-01.pcm | \
12 // RUN: FileCheck --check-prefix=CHECK-HU %s -DTDIR=%t
13 
14 // RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \
15 // RUN: -DFOO -fmodule-file=hu-01.pcm -o hu-02.pcm  -Rmodule-import 2>&1 | \
16 // RUN: FileCheck --check-prefix=CHECK-IMP %s -DTDIR=%t
17 
18 // RUN: %clang_cc1 -std=c++20 -module-file-info hu-02.pcm | \
19 // RUN: FileCheck --check-prefix=CHECK-HU2 %s -DTDIR=%t
20 
21 // RUN: %clang_cc1 -std=c++20 -emit-module-interface importer-01.cpp \
22 // RUN:  -fmodule-file=hu-02.pcm -o B.pcm -verify
23 
24 // RUN: %clang_cc1 -std=c++20 -emit-module-interface importer-02.cpp \
25 // RUN:  -fmodule-file=hu-02.pcm -o C.pcm -Rmodule-import 2>&1 | \
26 // RUN:  FileCheck --check-prefix=CHECK-IMP-HU2 %s -DTDIR=%t
27 
28 //--- hu-01.h
29 #ifndef __GUARD
30 #define __GUARD
31 
32 int baz(int);
33 #define FORTYTWO 42
34 
35 #define SHOULD_NOT_BE_DEFINED -1
36 #undef SHOULD_NOT_BE_DEFINED
37 
38 #endif // __GUARD
39 // expected-no-diagnostics
40 
41 // CHECK-HU:  ====== C++20 Module structure ======
42 // CHECK-HU-NEXT:  Header Unit '.{{/|\\\\?}}hu-01.h' is the Primary Module at index #1
43 
44 //--- hu-02.h
45 export import "hu-01.h"; // expected-warning {{the implementation of header units is in an experimental phase}}
46 #if !defined(FORTYTWO) || FORTYTWO != 42
47 #error FORTYTWO missing in hu-02
48 #endif
49 
50 #ifndef __GUARD
51 #error __GUARD missing in hu-02
52 #endif
53 
54 #ifdef SHOULD_NOT_BE_DEFINED
55 #error SHOULD_NOT_BE_DEFINED is visible
56 #endif
57 
58 #define KAP 6174
59 
60 #ifdef FOO
61 #define FOO_BRANCH(X) (X) + 1
foo(int x)62 inline int foo(int x) {
63   if (x == FORTYTWO)
64     return FOO_BRANCH(x);
65   return FORTYTWO;
66 }
67 #else
68 #define BAR_BRANCH(X) (X) + 2
69 inline int bar(int x) {
70   if (x == FORTYTWO)
71     return BAR_BRANCH(x);
72   return FORTYTWO;
73 }
74 #endif
75 
76 // CHECK-IMP: remark: importing module '.{{/|\\\\?}}hu-01.h' from 'hu-01.pcm'
77 // CHECK-HU2:  ====== C++20 Module structure ======
78 // CHECK-HU2-NEXT:  Header Unit '.{{/|\\\\?}}hu-02.h' is the Primary Module at index #2
79 // CHECK-HU2-NEXT:   Exports:
80 // CHECK-HU2-NEXT:    Header Unit '.{{/|\\\\?}}hu-01.h' is at index #1
81 // expected-no-diagnostics
82 
83 //--- importer-01.cpp
84 export module B;
85 import "hu-02.h"; // expected-warning {{the implementation of header units is in an experimental phase}}
86 
success(int x)87 int success(int x) {
88   return foo(FORTYTWO + x + KAP);
89 }
90 
fail(int x)91 int fail(int x) {
92   return bar(FORTYTWO + x + KAP); // expected-error {{use of undeclared identifier 'bar'}}
93   // expected-note@* {{'baz' declared here}}
94 }
95 
96 //--- importer-02.cpp
97 export module C;
98 import "hu-02.h"; // expected-warning {{the implementation of header units is in an experimental phase}}
99 
success(int x)100 int success(int x) {
101   return foo(FORTYTWO + x + KAP);
102 }
103 
104 // CHECK-IMP-HU2: remark: importing module '.{{/|\\\\?}}hu-02.h' from 'hu-02.pcm'
105 // CHECK-IMP-HU2: remark: importing module '.{{/|\\\\?}}hu-01.h' into '.{{/|\\\\?}}hu-02.h' from '[[TDIR]]{{[/\\]}}hu-01.pcm'
106