diff options
author | Jason Merrill <jason@redhat.com> | 2024-06-04 22:27:56 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2024-06-07 14:29:46 -0400 |
commit | a29f481bbcaf2b196f358122a5f1e45c6869df82 (patch) | |
tree | acffb45579d8e7f8d55a90e6b5f32a56e68fd812 /gcc | |
parent | 5c761395402a730535983a5e49ef1775561ebc61 (diff) | |
download | gcc-a29f481bbcaf2b196f358122a5f1e45c6869df82.zip gcc-a29f481bbcaf2b196f358122a5f1e45c6869df82.tar.gz gcc-a29f481bbcaf2b196f358122a5f1e45c6869df82.tar.bz2 |
c++: -include and header unit translation
Within a source file, #include is translated to import if a suitable header
unit is available, but this wasn't working with -include. This turned out
to be because we suppressed the translation before the beginning of the
main file. After removing that, I had to tweak libcpp file handling to
accommodate the way it moves from an -include to the main file.
gcc/ChangeLog:
* doc/invoke.texi (C++ Modules): Mention -include.
gcc/cp/ChangeLog:
* module.cc (maybe_translate_include): Allow before the main file.
libcpp/ChangeLog:
* files.cc (_cpp_stack_file): LC_ENTER for -include header unit.
gcc/testsuite/ChangeLog:
* g++.dg/modules/dashinclude-1_b.C: New test.
* g++.dg/modules/dashinclude-1_a.H: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/module.cc | 4 | ||||
-rw-r--r-- | gcc/doc/invoke.texi | 17 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/modules/dashinclude-1_a.H | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/modules/dashinclude-1_b.C | 9 |
4 files changed, 31 insertions, 4 deletions
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index ed24814..21fc851 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -19976,10 +19976,6 @@ maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc, return nullptr; } - if (!spans.init_p ()) - /* Before the main file, don't divert. */ - return nullptr; - dump.push (NULL); dump () && dump ("Checking include translation '%s'", path); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e5a5d1d..ca2591c 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -37764,6 +37764,23 @@ installed. Specifying the language as one of these variants also inhibits output of the object file, as header files have no associated object file. +Header units can be used in much the same way as precompiled headers +(@pxref{Precompiled Headers}), but with fewer restrictions: an +#include that is translated to a header unit import can appear at any +point in the source file, and multiple header units can be used +together. In particular, the @option{-include} strategy works: with +the bits/stdc++.h header used for libstdc++ precompiled headers you +can + +@smallexample +g++ -fmodules-ts -x c++-system-header -c bits/stdc++.h +g++ -fmodules-ts -include bits/stdc++.h mycode.C +@end smallexample + +and any standard library #includes in mycode.C will be skipped, +because the import brought in the whole library. This can be a simple +way to use modules to speed up compilation without any code changes. + The @option{-fmodule-only} option disables generation of the associated object file for compiling a module interface. Only the CMI is generated. This option is implied when using the diff --git a/gcc/testsuite/g++.dg/modules/dashinclude-1_a.H b/gcc/testsuite/g++.dg/modules/dashinclude-1_a.H new file mode 100644 index 0000000..c1b40a5 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/dashinclude-1_a.H @@ -0,0 +1,5 @@ +// { dg-module-do run } +// { dg-additional-options "-fmodule-header" } +// { dg-module-cmi {} } + +inline int f() { return 0; } diff --git a/gcc/testsuite/g++.dg/modules/dashinclude-1_b.C b/gcc/testsuite/g++.dg/modules/dashinclude-1_b.C new file mode 100644 index 0000000..6e6a334 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/dashinclude-1_b.C @@ -0,0 +1,9 @@ +// Test that include translation works with command-line -include. +// { dg-additional-options "-fmodules-ts -fdump-lang-module -include $srcdir/g++.dg/modules/dashinclude-1_a.H" } + +int main () +{ + return f(); +} + +// { dg-final { scan-lang-dump {Translating include to import} module } } |