[clang] [clang][deps] Fix dependency scanner misidentifying 'import::' as module partition (PR #148674)
Naveen Seth Hanig via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 14 10:33:42 PDT 2025
https://github.com/naveen-seth updated https://github.com/llvm/llvm-project/pull/148674
>From bb1c20a40fa8635b8987733708979729d54aa02e Mon Sep 17 00:00:00 2001
From: Naveen Seth Hanig <naveen.hanig at outlook.com>
Date: Mon, 14 Jul 2025 18:44:01 +0200
Subject: [PATCH 1/2] [clang][deps] Fix dependency scanner misidentifying
'import::' as module partition
The dependency directive scanner was incorrectly classifying
namespaces such as `import::inner xi` as directives.
According to P1857R3, `import` should not be treated as a directive
when followed by `::`.
This change fixes that behavior.
---
clang/lib/Lex/DependencyDirectivesScanner.cpp | 7 +++++++
.../Lex/DependencyDirectivesScannerTest.cpp | 12 ++++++++++++
2 files changed, 19 insertions(+)
diff --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp
index d894c265a07a2..869c9cea566b6 100644
--- a/clang/lib/Lex/DependencyDirectivesScanner.cpp
+++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp
@@ -722,6 +722,13 @@ bool Scanner::lexModule(const char *&First, const char *const End) {
skipLine(First, End);
return false;
}
+ // A module partition starts with exactly one ':'. If we have '::', this is
+ // a scope resolution instead and shouldn't be recognized as a directive
+ // per P1857R3.
+ if (First + 1 != End && First[1] == ':') {
+ skipLine(First, End);
+ return false;
+ }
// `import:(type)name` is a valid ObjC method decl, so check one more token.
(void)lexToken(First, End);
if (!tryLexIdentifierOrSkipLine(First, End))
diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
index d2ef27155df94..4d532826cbd60 100644
--- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
+++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
@@ -1151,6 +1151,18 @@ TEST(MinimizeSourceToDependencyDirectivesTest, ObjCMethodArgs) {
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
}
+TEST(MinimizeSourceToDependencyDirectivesTest, CxxModulesImportScopeResolution) {
+ SmallString<16> Out;
+ SmallVector<dependency_directives_scan::Token, 2> Tokens;
+ SmallVector<Directive, 1> Directives;
+
+ StringRef Source = "import::inner xi = {};'\n"
+ "module::inner yi = {};";
+ ASSERT_FALSE(
+ minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
+ EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
+}
+
TEST(MinimizeSourceToDependencyDirectivesTest, TokensBeforeEOF) {
SmallString<128> Out;
>From 8549bd25ef9989c15ca8ce040a34094c374e4d71 Mon Sep 17 00:00:00 2001
From: Naveen Seth Hanig <naveen.hanig at outlook.com>
Date: Mon, 14 Jul 2025 19:33:11 +0200
Subject: [PATCH 2/2] Fix formatting
---
clang/unittests/Lex/DependencyDirectivesScannerTest.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
index 4d532826cbd60..46dbb4d4b91b4 100644
--- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
+++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
@@ -1151,7 +1151,8 @@ TEST(MinimizeSourceToDependencyDirectivesTest, ObjCMethodArgs) {
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
}
-TEST(MinimizeSourceToDependencyDirectivesTest, CxxModulesImportScopeResolution) {
+TEST(MinimizeSourceToDependencyDirectivesTest,
+ CxxModulesImportScopeResolution) {
SmallString<16> Out;
SmallVector<dependency_directives_scan::Token, 2> Tokens;
SmallVector<Directive, 1> Directives;
More information about the cfe-commits
mailing list