blob: 45f0821c9ffec0e0a7373e5ab630e65c9ce74620 [file] [log] [blame]
Richard Smithf74d9462017-04-28 01:49:42 +00001// RUN: rm -rf %t
Richard Smith8128f332017-05-05 22:18:51 +00002// RUN: mkdir %t
Richard Smithf74d9462017-04-28 01:49:42 +00003
4// RUN: not %clang_cc1 -fmodules -fmodule-name=file -I%S/Inputs/preprocess -x c++-module-map %S/Inputs/preprocess/module.modulemap -E 2>&1 | FileCheck %s --check-prefix=MISSING-FWD
5// MISSING-FWD: module 'fwd' is needed
6
Richard Smith8128f332017-05-05 22:18:51 +00007// RUN: %clang_cc1 -fmodules -fmodule-name=fwd -I%S/Inputs/preprocess -x c++-module-map %S/Inputs/preprocess/module.modulemap -emit-module -o %t/fwd.pcm
8
9// Check that we can preprocess modules, and get the expected output.
10// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -I%S/Inputs/preprocess -x c++-module-map %S/Inputs/preprocess/module.modulemap -E -o %t/no-rewrite.ii
11// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -I%S/Inputs/preprocess -x c++-module-map %S/Inputs/preprocess/module.modulemap -E -frewrite-includes -o %t/rewrite.ii
12//
13// RUN: FileCheck %s --input-file %t/no-rewrite.ii --check-prefix=CHECK --check-prefix=NO-REWRITE
14// RUN: FileCheck %s --input-file %t/rewrite.ii --check-prefix=CHECK --check-prefix=REWRITE
15
16// Check that we can build a module from the preprocessed output.
Richard Smith8128f332017-05-05 22:18:51 +000017// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -x c++-module-map-cpp-output %t/no-rewrite.ii -emit-module -o %t/no-rewrite.pcm
18// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -x c++-module-map-cpp-output %t/rewrite.ii -emit-module -o %t/rewrite.pcm
19
Richard Smith4a3751f2017-05-08 20:30:47 +000020// Check that we can load the original module map in the same compilation (this
21// could happen if we had a redundant -fmodule-map-file= in the original
22// build).
23// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -fmodule-map-file=%S/Inputs/preprocess/module.modulemap -x c++-module-map-cpp-output %t/rewrite.ii -emit-module -o /dev/null
24
Richard Smith8128f332017-05-05 22:18:51 +000025// Check the module we built works.
Richard Smith54f04402017-05-18 02:29:20 +000026// RUN: %clang_cc1 -fmodules -fmodule-file=%t/no-rewrite.pcm %s -I%t -verify -fno-modules-error-recovery
27// RUN: %clang_cc1 -fmodules -fmodule-file=%t/rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DREWRITE
Richard Smith8b706102017-05-31 20:56:55 +000028// RUN: %clang_cc1 -fmodules -fmodule-file=%t/no-rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DINCLUDE -I%S/Inputs/preprocess
29// RUN: %clang_cc1 -fmodules -fmodule-file=%t/rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DREWRITE -DINCLUDE -I%S/Inputs/preprocess
Richard Smith8128f332017-05-05 22:18:51 +000030
Richard Smith040e1262017-06-02 01:55:39 +000031// Now try building the module when the header files are missing.
32// RUN: cp %S/Inputs/preprocess/fwd.h %S/Inputs/preprocess/file.h %S/Inputs/preprocess/file2.h %S/Inputs/preprocess/module.modulemap %t
33// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -I%t -x c++-module-map %t/module.modulemap -E -frewrite-includes -o %t/copy.ii
34// RUN: rm %t/fwd.h %t/file.h %t/file2.h %t/module.modulemap
35// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -x c++-module-map-cpp-output %t/copy.ii -emit-module -o %t/copy.pcm
36
Richard Smithab755972017-06-05 18:10:11 +000037// Check that our module contains correct mapping information for the headers.
Richard Smith040e1262017-06-02 01:55:39 +000038// RUN: cp %S/Inputs/preprocess/fwd.h %S/Inputs/preprocess/file.h %S/Inputs/preprocess/file2.h %S/Inputs/preprocess/module.modulemap %t
39// RUN: %clang_cc1 -fmodules -fmodule-file=%t/copy.pcm %s -I%t -verify -fno-modules-error-recovery -DCOPY -DINCLUDE
Richard Smith6b199342017-06-05 18:39:31 +000040// RUN: rm %t/fwd.h %t/file.h %t/file2.h %t/module.modulemap
Richard Smith8128f332017-05-05 22:18:51 +000041
Richard Smithab755972017-06-05 18:10:11 +000042// Check that we can preprocess from a .pcm file and that we get the same result as preprocessing from the original sources.
43// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -I%S/Inputs/preprocess -x c++-module-map %S/Inputs/preprocess/module.modulemap -emit-module -o %t/file.pcm
44// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -I%S/Inputs/preprocess %t/file.pcm -E -frewrite-includes -o %t/file.rewrite.ii
Richard Smith6b199342017-06-05 18:39:31 +000045// FIXME: This check fails on Windows targets, due to canonicalization of directory separators.
46// FIXME: cmp %t/rewrite.ii %t/file.rewrite.ii
47// FIXME: Instead, just check that the preprocessed output is functionally equivalent to the output when preprocessing from the original sources.
48// RUN: FileCheck %s --input-file %t/file.rewrite.ii --check-prefix=CHECK --check-prefix=REWRITE
49// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -x c++-module-map-cpp-output %t/file.rewrite.ii -emit-module -o %t/file.rewrite.pcm
50// RUN: %clang_cc1 -fmodules -fmodule-file=%t/file.rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DFILE_REWRITE
51// RUN: %clang_cc1 -fmodules -fmodule-file=%t/file.rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DFILE_REWRITE -DINCLUDE -I%S/Inputs/preprocess
Richard Smithab755972017-06-05 18:10:11 +000052
Richard Smith8128f332017-05-05 22:18:51 +000053// == module map
54// CHECK: # 1 "{{.*}}module.modulemap"
55// CHECK: module file {
Richard Smith040e1262017-06-02 01:55:39 +000056// CHECK: header "file.h" { size
57// CHECK: header "file2.h" { size
Richard Smith8128f332017-05-05 22:18:51 +000058// CHECK: }
Richard Smithd1386302017-05-04 00:29:54 +000059
60// == file.h
Richard Smithf74d9462017-04-28 01:49:42 +000061// CHECK: # 1 "<module-includes>"
Richard Smithd1386302017-05-04 00:29:54 +000062// REWRITE: #if 0
63// REWRITE: #include "file.h"
64// REWRITE: #endif
65//
66// FIXME: It would be preferable to consistently put the module begin/end in
67// the same file, but the relative ordering of PP callbacks and module
68// begin/end tokens makes that difficult.
69//
70// REWRITE: #pragma clang module begin file
Richard Smithf74d9462017-04-28 01:49:42 +000071// CHECK: # 1 "{{.*}}file.h" 1
Richard Smithd1386302017-05-04 00:29:54 +000072// NO-REWRITE: #pragma clang module begin file
73// NO-REWRITE: # 1 "{{.*}}file.h"{{$}}
74//
Richard Smithf74d9462017-04-28 01:49:42 +000075// CHECK: struct __FILE;
Richard Smithd1386302017-05-04 00:29:54 +000076// CHECK: #pragma clang module import fwd /* clang {{-E|-frewrite-includes}}: implicit import
Richard Smithf74d9462017-04-28 01:49:42 +000077// CHECK: typedef struct __FILE FILE;
Richard Smithd1386302017-05-04 00:29:54 +000078//
79// REWRITE: #pragma clang module end
Richard Smithf74d9462017-04-28 01:49:42 +000080// CHECK: # 2 "<module-includes>" 2
Richard Smithd1386302017-05-04 00:29:54 +000081// NO-REWRITE: #pragma clang module end
82
83// == file2.h
84// REWRITE: #if 0
85// REWRITE: #include "file2.h"
86// REWRITE: #endif
87//
88// REWRITE: #pragma clang module begin file
89// CHECK: # 1 "{{.*}}file2.h" 1
90// NO-REWRITE: #pragma clang module begin file
91//
92// ==== recursively re-enter file.h
93// REWRITE: #if 0
94// REWRITE: #include "file.h"
95// REWRITE: #endif
96//
97// REWRITE: #pragma clang module begin file
98// CHECK: # 1 "{{.*}}file.h" 1
99// NO-REWRITE: #pragma clang module begin file
100// NO-REWRITE: # 1 "{{.*}}file.h"{{$}}
101//
102// CHECK: struct __FILE;
103// CHECK: #pragma clang module import fwd /* clang {{-E|-frewrite-includes}}: implicit import
104// CHECK: typedef struct __FILE FILE;
105//
106// REWRITE: #pragma clang module end
107// CHECK: # 2 "{{.*}}file2.h" 2
108// NO-REWRITE: #pragma clang module end
109// NO-REWRITE: # 2 "{{.*}}file2.h"{{$}}
110// ==== return to file2.h
111//
112// CHECK: extern int file2;
113//
114// REWRITE: #pragma clang module end
115// CHECK: # 3 "<module-includes>" 2
116// NO-REWRITE: #pragma clang module end
Richard Smith8128f332017-05-05 22:18:51 +0000117
118
Richard Smith54f04402017-05-18 02:29:20 +0000119__FILE *a; // expected-error {{declaration of '__FILE' must be imported}}
Richard Smith6b199342017-06-05 18:39:31 +0000120#if FILE_REWRITE
121// expected-note@file.rewrite.ii:1 {{here}}
122#elif REWRITE
Richard Smith54f04402017-05-18 02:29:20 +0000123// expected-note@rewrite.ii:1 {{here}}
Richard Smith040e1262017-06-02 01:55:39 +0000124#elif COPY
125// expected-note@copy.ii:1 {{here}}
Richard Smith54f04402017-05-18 02:29:20 +0000126#else
127// expected-note@no-rewrite.ii:1 {{here}}
128#endif
Richard Smith8128f332017-05-05 22:18:51 +0000129
Richard Smith8b706102017-05-31 20:56:55 +0000130#ifdef INCLUDE
131#include "file.h"
132#else
Richard Smith8128f332017-05-05 22:18:51 +0000133#pragma clang module import file
Richard Smith8b706102017-05-31 20:56:55 +0000134#endif
Richard Smith8128f332017-05-05 22:18:51 +0000135
136FILE *b;
Richard Smith8b706102017-05-31 20:56:55 +0000137int x = file2; // ok, found in file2.h, even under -DINCLUDE