From 7a759271c54b78eb940bf12274bcc535a3df22e3 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig Date: Mon, 14 Jul 2025 19:17:10 +0200 Subject: [PATCH 1/2] [clang][deps] Recognize 'module;' in dependency directive scanner With this change, the dependency directive scanner now properly identifies "module;" as a directive, as per P1857R3. Previously, the global module fragment was not recognized by the scanner. --- clang/lib/Lex/DependencyDirectivesScanner.cpp | 7 +++++++ clang/unittests/Lex/DependencyDirectivesScannerTest.cpp | 9 +++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp index d894c265a07a2..8822e760274d0 100644 --- a/clang/lib/Lex/DependencyDirectivesScanner.cpp +++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp @@ -728,6 +728,13 @@ bool Scanner::lexModule(const char *&First, const char *const End) { return false; break; } + case ';': { + // Handle the global module fragment `module;`. + if (Id == "module" && !Export) + break; + skipLine(First, End); + return false; + } case '<': case '"': break; diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp index d2ef27155df94..92f6f401ec6b7 100644 --- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp +++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp @@ -1122,16 +1122,17 @@ ort \ )"; ASSERT_FALSE( minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives)); - EXPECT_STREQ("#include \"textual-header.h\"\nexport module m;" + EXPECT_STREQ("module;#include \"textual-header.h\"\nexport module m;" "exp\\\nort import:l[[rename]];" "import<<=3;import a b d e d e f e;" "import foo[[no_unique_address]];import foo();" "import f(:sefse);import f(->a=3);" "\n", Out.data()); - ASSERT_EQ(Directives.size(), 11u); - EXPECT_EQ(Directives[0].Kind, pp_include); - EXPECT_EQ(Directives[1].Kind, cxx_export_module_decl); + ASSERT_EQ(Directives.size(), 12u); + EXPECT_EQ(Directives[0].Kind, cxx_module_decl); + EXPECT_EQ(Directives[1].Kind, pp_include); + EXPECT_EQ(Directives[2].Kind, cxx_export_module_decl); } TEST(MinimizeSourceToDependencyDirectivesTest, ObjCMethodArgs) { From 25553f716701cb627e221deddace2d2956efc593 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig Date: Mon, 14 Jul 2025 19:53:07 +0200 Subject: [PATCH 2/2] Add newline after 'module;' to be valid C++ --- clang/unittests/Lex/DependencyDirectivesScannerTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp index 92f6f401ec6b7..a28989e6cf174 100644 --- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp +++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp @@ -1122,7 +1122,7 @@ ort \ )"; ASSERT_FALSE( minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives)); - EXPECT_STREQ("module;#include \"textual-header.h\"\nexport module m;" + EXPECT_STREQ("module\n;#include \"textual-header.h\"\nexport module m;" "exp\\\nort import:l[[rename]];" "import<<=3;import a b d e d e f e;" "import foo[[no_unique_address]];import foo();"