c++: Follow module grammar more closely [PR110808]
commit7f24446a3cf67d1346c78b4667fba74b73a23302
authorNathaniel Shead <nathanieloshead@gmail.com>
Fri, 24 Nov 2023 05:26:43 +0000 (24 16:26 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sun, 7 Jan 2024 10:04:17 +0000 (7 21:04 +1100)
tree0b0b81f427592ab946fb6f9619bb511948563900
parente74c37aab51d2350a3417f442c3a7d119b755378
c++: Follow module grammar more closely [PR110808]

This patch cleans up the parsing of module-declarations and
import-declarations to more closely follow the grammar defined by the
standard.

For instance, currently we allow declarations like 'import A:B', even
from an unrelated source file (not part of module A), which causes
errors in merging declarations. However, the syntax in [module.import]
doesn't even allow this form of import, so this patch prevents this from
parsing at all and avoids the error that way.

Additionally, we sometimes allow statements like 'import :X' or
'module :X' even when not in a named module, and this causes segfaults,
so we disallow this too.

PR c++/110808

gcc/cp/ChangeLog:

* parser.cc (cp_parser_module_name): Rewrite to handle
module-names and module-partitions independently.
(cp_parser_module_partition): New function.
(cp_parser_module_declaration): Parse module partitions
explicitly. Don't change state if parsing module decl failed.
(cp_parser_import_declaration): Handle different kinds of
import-declarations locally.

gcc/testsuite/ChangeLog:

* g++.dg/modules/part-hdr-1_c.C: Fix syntax.
* g++.dg/modules/part-mac-1_c.C: Likewise.
* g++.dg/modules/mod-invalid-1.C: New test.
* g++.dg/modules/part-8_a.C: New test.
* g++.dg/modules/part-8_b.C: New test.
* g++.dg/modules/part-8_c.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/modules/mod-invalid-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-8_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-8_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-8_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-hdr-1_c.C
gcc/testsuite/g++.dg/modules/part-mac-1_c.C