c++: add module extensions

There is a trend in the broader C++ community to use a different extension
for module interface units, even though (in GCC) they are compiled in the
same way as other source files.  Let's recognize these extensions as C++.

.ixx is the MSVC standard, while the .c*m are supported by Clang.  libc++
standard headers use .cppm, as their other source files use .cpp.
Perhaps libstdc++ might use .ccm for parallel consistency?

One issue with .c++m is that libcpp/mkdeps.cc has been using it for the
phony dependencies to express module dependencies, so I'm changing mkdeps to
something less likely to be an actual file, ".c++-module".

gcc/cp/ChangeLog:

	* lang-specs.h: Add module interface extensions.

gcc/ChangeLog:

	* doc/invoke.texi: Update module extension docs.

libcpp/ChangeLog:

	* mkdeps.cc (make_write): Change .c++m to .c++-module.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/dep-1_a.C
	* g++.dg/modules/dep-1_b.C
	* g++.dg/modules/dep-2.C: Change .c++m to .c++-module.
This commit is contained in:
Jason Merrill 2024-05-16 16:09:12 -04:00
parent 18f477980c
commit ff41abdca0
6 changed files with 30 additions and 25 deletions

View file

@ -39,6 +39,12 @@ along with GCC; see the file COPYING3. If not see
{".HPP", "@c++-header", 0, 0, 0},
{".tcc", "@c++-header", 0, 0, 0},
{".hh", "@c++-header", 0, 0, 0},
/* Module interface unit. Should there also be a .C counterpart? */
{".ixx", "@c++", 0, 0, 0}, /* MSVC */
{".cppm", "@c++", 0, 0, 0}, /* Clang/libc++ */
{".cxxm", "@c++", 0, 0, 0},
{".c++m", "@c++", 0, 0, 0},
{".ccm", "@c++", 0, 0, 0},
{"@c++-header",
"%{E|M|MM:cc1plus -E %{fmodules-ts:-fdirectives-only -fmodule-header}"
" %(cpp_options) %2 %(cpp_debug_options)}"

View file

@ -2317,9 +2317,12 @@ other language.
C++ source files conventionally use one of the suffixes @samp{.C},
@samp{.cc}, @samp{.cpp}, @samp{.CPP}, @samp{.c++}, @samp{.cp}, or
@samp{.cxx}; C++ header files often use @samp{.hh}, @samp{.hpp},
@samp{.H}, or (for shared template code) @samp{.tcc}; and
preprocessed C++ files use the suffix @samp{.ii}. GCC recognizes
files with these names and compiles them as C++ programs even if you
@samp{.H}, or (for shared template code) @samp{.tcc};
preprocessed C++ files use the suffix @samp{.ii}; and C++20 module interface
units sometimes use @samp{.ixx}, @samp{.cppm}, @samp{.cxxm}, @samp{.c++m},
or @samp{.ccm}.
GCC recognizes files with these names and compiles them as C++ programs even if you
call the compiler the same way as for compiling C programs (usually
with the name @command{gcc}).
@ -37705,13 +37708,10 @@ Modular compilation is @emph{not} enabled with just the
version selected, although in pre-C++20 versions, it is of course an
extension.
No new source file suffixes are required or supported. If you wish to
use a non-standard suffix (@pxref{Overall Options}), you also need
to provide a @option{-x c++} option too.@footnote{Some users like to
distinguish module interface files with a new suffix, such as naming
the source @code{module.cppm}, which involves
teaching all tools about the new suffix. A different scheme, such as
naming @code{module-m.cpp} would be less invasive.}
No new source file suffixes are required. A few suffixes preferred
for module interface units by other compilers (e.g. @samp{.ixx},
@samp{.cppm}) are supported, but files with these suffixes are treated
the same as any other C++ source file.
Compiling a module interface unit produces an additional output (to
the assembly or object file), called a Compiled Module Interface

View file

@ -4,6 +4,6 @@ export module m:part;
// { dg-module-cmi m:part }
// All The Backslashes!
// { dg-final { scan-file dep-1_a.d {\nm:part\.c\+\+m: gcm.cache/m-part\.gcm} } }
// { dg-final { scan-file dep-1_a.d {\nm:part\.c\+\+-module: gcm.cache/m-part\.gcm} } }
// { dg-final { scan-file dep-1_a.d {\ngcm.cache/m-part\.gcm:| dep-1_a\.o} } }
// { dg-final { scan-file dep-1_a.d {\n\.PHONY: m:part\.c\+\+m} } }
// { dg-final { scan-file dep-1_a.d {\n\.PHONY: m:part\.c\+\+-module} } }

View file

@ -3,8 +3,8 @@ export module m;
// { dg-module-cmi m }
export import :part;
// { dg-final { scan-file dep-1_b.d {\ndep-1_b\.s gcm.cache/m\.gcm: m:part\.c\+\+m} } }
// { dg-final { scan-file dep-1_b.d {\nm\.c\+\+m: gcm.cache/m\.gcm} } }
// { dg-final { scan-file dep-1_b.d {\n\.PHONY: m\.c\+\+m} } }
// { dg-final { scan-file dep-1_b.d {\ndep-1_b\.s gcm.cache/m\.gcm: m:part\.c\+\+-module} } }
// { dg-final { scan-file dep-1_b.d {\nm\.c\+\+-module: gcm.cache/m\.gcm} } }
// { dg-final { scan-file dep-1_b.d {\n\.PHONY: m\.c\+\+-module} } }
// { dg-final { scan-file dep-1_b.d {\ngcm.cache/m\.gcm:| dep-1_b.o} } }
// { dg-final { scan-file dep-1_b.d {\nCXX_IMPORTS \+= m:part\.c\+\+m} } }
// { dg-final { scan-file dep-1_b.d {\nCXX_IMPORTS \+= m:part\.c\+\+-module} } }

View file

@ -5,8 +5,8 @@ module m:part;
// { dg-module-cmi !m:part }
// All The Backslashes!
// { dg-final { scan-file dep-2.d {\nm:part\.c\+\+m: gcm.cache/m-part\.gcm} } }
// { dg-final { scan-file dep-2.d {\nm:part\.c\+\+-module: gcm.cache/m-part\.gcm} } }
// { dg-final { scan-file dep-2.d {\ngcm.cache/m:part\.gcm:| dep-2\.o} } }
// { dg-final { scan-file dep-2.d {\n\.PHONY: m:part\.c\+\+m} } }
// { dg-final { scan-file dep-2.d {\n\.PHONY: m:part\.c\+\+-module} } }
// { dg-final { scan-file dep-2.i {\nmodule m:part;\n} } }

View file

@ -411,8 +411,7 @@ make_write_vec (const mkdeps::vec<const char *> &vec, FILE *fp,
return col;
}
/* Write the dependencies to a Makefile. If PHONY is true, add
.PHONY targets for all the dependencies too. */
/* Write the dependencies to a Makefile. */
static void
make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
@ -453,7 +452,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
column = make_write_name (d->cmi_name, fp, column, colmax);
fputs (":", fp);
column++;
column = make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
column = make_write_vec (d->modules, fp, column, colmax, 0, ".c++-module");
fputs ("\n", fp);
}
@ -463,7 +462,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
{
/* module-name : cmi-name */
column = make_write_name (d->module_name, fp, 0, colmax,
true, ".c++m");
true, ".c++-module");
fputs (":", fp);
column++;
column = make_write_name (d->cmi_name, fp, column, colmax);
@ -471,7 +470,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
column = fprintf (fp, ".PHONY:");
column = make_write_name (d->module_name, fp, column, colmax,
true, ".c++m");
true, ".c++-module");
fputs ("\n", fp);
}
@ -488,11 +487,11 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
fputs ("\n", fp);
}
}
if (d->modules.size ())
{
column = fprintf (fp, "CXX_IMPORTS +=");
make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
make_write_vec (d->modules, fp, column, colmax, 0, ".c++-module");
fputs ("\n", fp);
}
}