https://gcc.gnu.org/g:a29f481bbcaf2b196f358122a5f1e45c6869df82

commit r15-1104-ga29f481bbcaf2b196f358122a5f1e45c6869df82
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Jun 4 22:27:56 2024 -0400

    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.

Diff:
---
 gcc/doc/invoke.texi                            | 17 +++++++++++++++++
 gcc/cp/module.cc                               |  4 ----
 gcc/testsuite/g++.dg/modules/dashinclude-1_b.C |  9 +++++++++
 libcpp/files.cc                                |  5 ++++-
 gcc/testsuite/g++.dg/modules/dashinclude-1_a.H |  5 +++++
 5 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index e5a5d1d9335..ca2591ce2c3 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/cp/module.cc b/gcc/cp/module.cc
index ed24814b601..21fc85150c9 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/testsuite/g++.dg/modules/dashinclude-1_b.C 
b/gcc/testsuite/g++.dg/modules/dashinclude-1_b.C
new file mode 100644
index 00000000000..6e6a33407a4
--- /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 } }
diff --git a/libcpp/files.cc b/libcpp/files.cc
index c61df339e20..78f56e30bde 100644
--- a/libcpp/files.cc
+++ b/libcpp/files.cc
@@ -1008,7 +1008,10 @@ _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, 
include_type type,
   if (decrement)
     pfile->line_table->highest_location--;
 
-  if (file->header_unit <= 0)
+  /* Normally a header unit becomes an __import directive in the current file,
+     but with -include we need something to LC_LEAVE to trigger the file_change
+     hook and continue to the next -include or the main source file.  */
+  if (file->header_unit <= 0 || type == IT_CMDLINE)
     /* Add line map and do callbacks.  */
     _cpp_do_file_change (pfile, LC_ENTER, file->path,
                       /* With preamble injection, start on line zero,
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 00000000000..c1b40a53924
--- /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; }

Reply via email to