Skip to content

Commit ce8c19f

Browse files
authored
[clang][deps] Fix dependency scanner misidentifying 'import::' as module partition (#148674)
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.
1 parent ec2e21a commit ce8c19f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,13 @@ bool Scanner::lexModule(const char *&First, const char *const End) {
722722
skipLine(First, End);
723723
return false;
724724
}
725+
// A module partition starts with exactly one ':'. If we have '::', this is
726+
// a scope resolution instead and shouldn't be recognized as a directive
727+
// per P1857R3.
728+
if (First + 1 != End && First[1] == ':') {
729+
skipLine(First, End);
730+
return false;
731+
}
725732
// `import:(type)name` is a valid ObjC method decl, so check one more token.
726733
(void)lexToken(First, End);
727734
if (!tryLexIdentifierOrSkipLine(First, End))

clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,19 @@ TEST(MinimizeSourceToDependencyDirectivesTest, ObjCMethodArgs) {
11511151
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
11521152
}
11531153

1154+
TEST(MinimizeSourceToDependencyDirectivesTest,
1155+
CxxModulesImportScopeResolution) {
1156+
SmallString<16> Out;
1157+
SmallVector<dependency_directives_scan::Token, 2> Tokens;
1158+
SmallVector<Directive, 1> Directives;
1159+
1160+
StringRef Source = "import::inner xi = {};'\n"
1161+
"module::inner yi = {};";
1162+
ASSERT_FALSE(
1163+
minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
1164+
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
1165+
}
1166+
11541167
TEST(MinimizeSourceToDependencyDirectivesTest, TokensBeforeEOF) {
11551168
SmallString<128> Out;
11561169

0 commit comments

Comments
 (0)