yronglin ea321392eb
[C++][Modules] A module directive may only appear as the first preprocessing tokens in a file (#144233)
This PR is 2nd part of
[P1857R3](https://github.com/llvm/llvm-project/pull/107168)
implementation, and mainly implement the restriction `A module directive
may only appear as the first preprocessing tokens in a file (excluding
the global module fragment.)`:
[cpp.pre](https://eel.is/c++draft/cpp.pre):
```
module-file:
    pp-global-module-fragment[opt] pp-module group[opt] pp-private-module-fragment[opt]
```

We also refine tests use `split-file` instead of conditional macro.

Signed-off-by: yronglin <yronglin777@gmail.com>
2025-06-21 18:58:56 +08:00

46 lines
1.4 KiB
C++

// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: split-file %s %t
// RUN: echo 'export module foo;' > %t.cppm
// RUN: echo 'export int n;' >> %t.cppm
// RUN: %clang_cc1 -std=c++2a %t.cppm -emit-module-interface -o %t.pcm
// RUN: %clang_cc1 -std=c++2a -fmodule-file=foo=%t.pcm -verify -DMODE=0 %t/A.cppm
// RUN: %clang_cc1 -std=c++2a -fmodule-file=foo=%t.pcm -verify -DMODE=1 %t/B.cppm
// RUN: %clang_cc1 -std=c++2a -fmodule-file=foo=%t.pcm -verify -DMODE=2 %t/C.cppm
// RUN: %clang_cc1 -std=c++2a -fmodule-file=foo=%t.pcm -verify -DMODE=3 %t/D.cppm
// RUN: %clang_cc1 -std=c++2a -fmodule-file=foo=%t.pcm -verify -DMODE=4 %t/E.cppm
// RUN: %clang_cc1 -std=c++2a -fmodule-file=foo=%t.pcm -verify -DMODE=5 %t/F.cppm
//--- A.cppm
// no module declaration
// expected-no-diagnostics
//--- B.cppm
// expected-no-diagnostics
module foo; // Implementation, implicitly imports foo.
#define IMPORTED
int k = n;
//--- C.cppm
export module foo;
int k = n; // expected-error {{use of undeclared identifier 'n'}}
//--- D.cppm
export module bar; // A different module
int k = n; // expected-error {{use of undeclared identifier 'n'}}
//--- E.cppm
module foo:bar; // Partition implementation
//#define IMPORTED (we don't import foo here)
int k = n; // expected-error {{use of undeclared identifier 'n'}}
//--- F.cppm
export module foo:bar; // Partition interface
//#define IMPORTED (we don't import foo here)
int k = n; // expected-error {{use of undeclared identifier 'n'}}