llvm-project/clang/test/Modules/cxx20-include-translation.cpp
Iain Sandoe af2d11b1d5 [C++20][Modules] Implement include translation.
This addresses [cpp.include]/7

(when encountering #include header-name)

If the header identified by the header-name denotes an importable header, it
is implementation-defined whether the #include preprocessing directive is
instead replaced by an import directive.

In this implementation, include translation is performed _only_ for headers
in the Global Module fragment, so:
```
module;
 #include "will-be-translated.h" // IFF the header unit is available.

export module M;
 #include "will-not-be-translated.h" // even if the header unit is available
```
The reasoning is that, in general, includes in the module purview would not
be validly translatable (they would have to immediately follow the module
decl and without any other intervening decls).  Otherwise that would violate
the rules on contiguous import directives.

This would be quite complex to track in the preprocessor, and for relatively
little gain (the user can 'import "will-not-be-translated.h";' instead.)

TODO: This is one area where it becomes increasingly difficult to disambiguate
clang modules in C++ from C++ standard modules.  That needs to be addressed in
both the driver and the FE.

Differential Revision: https://reviews.llvm.org/D128981
2022-07-10 11:06:51 +01:00

110 lines
2.5 KiB
C++

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
// RUN: cd %t
//
// RUN: %clang_cc1 -std=c++20 -xc++-user-header h1.h -emit-header-unit -o h1.pcm
// RUN: %clang_cc1 -std=c++20 -xc++-user-header h2.h -emit-header-unit -o h2.pcm
// RUN: %clang_cc1 -std=c++20 -xc++-user-header h3.h -emit-header-unit -o h3.pcm
// RUN: %clang_cc1 -std=c++20 -xc++-user-header h4.h -emit-header-unit -o h4.pcm
// RUN: %clang_cc1 -std=c++20 Xlate.cpp -emit-module-interface -o Xlate.pcm \
// RUN: -fmodule-file=h1.pcm -fmodule-file=h2.pcm -fmodule-file=h3.pcm \
// RUN: -fmodule-file=h4.pcm -fsyntax-only -Wauto-import -verify
// Check that we do the intended translation and not more.
// RUN: %clang_cc1 -std=c++20 Xlate.cpp \
// RUN: -fmodule-file=h1.pcm -fmodule-file=h2.pcm -fmodule-file=h3.pcm \
// RUN: -fmodule-file=h4.pcm -E -undef | FileCheck %s
// We expect no diagnostics here, the used functions should all be available.
// RUN: %clang_cc1 -std=c++20 Xlate.cpp -emit-module-interface \
// RUN: -fmodule-file=h1.pcm -fmodule-file=h2.pcm -fmodule-file=h3.pcm \
// RUN: -fmodule-file=h4.pcm -fsyntax-only
// The content of the headers is not terribly important, we just want to check
// whether they are textually included or include-translated.
//--- h1.h
#ifndef H1_GUARD
#define H1_GUARD
#define ONE 1
void foo();
#endif // H1_GUARD
//--- h2.h
#ifndef H2_GUARD
#define H2_GUARD
#define TWO 2
void bar();
#endif // H2_GUARD
//--- h3.h
#ifndef H3_GUARD
#define H3_GUARD
#define THREE 3
void baz();
#endif // H3_GUARD
//--- h4.h
#ifndef H4_GUARD
#define H4_GUARD
#define FOUR 4
void boo();
#endif // H4_GUARD
//--- h5.h
#ifndef H5_GUARD
#define H5_GUARD
#define FIVE 5
void five();
#endif // H4_GUARD
//--- Xlate.cpp
/* some comment ...
... */
module /*nothing here*/;
// This should be include-translated, when the header unit for h1 is available.
#include "h1.h" // expected-warning {{treating #include as an import of module './h1.h'}}
// Import of a header unit is allowed, named modules are not.
import "h2.h";
// A regular, untranslated, header
#include "h5.h"
export module Xlate;
// This is OK, the import immediately follows the module decl.
import "h3.h";
// This should *not* be include-translated, even if header unit for h4 is
// available.
#include "h4.h"
export void charlie() {
foo();
bar();
baz();
boo();
five();
}
// CHECK: #pragma clang module import "./h1.h"
// CHECK: import ./h2.h
// CHECK: import ./h3.h
// CHECK-NOT: #pragma clang module import "./h4.h"