Skip to content

Commit 34ab855

Browse files
authored
[clang][deps] Ignore import/include directives with missing filenames (#99520)
Previously source input like `#import ` resulted in infinite calls append the same token into `CurDirTokens`. This patch now ignores those directive lines if they won't actually end up being compiled. (e.g. macro guarded) resolves: rdar://121247565
1 parent 464ea88 commit 34ab855

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ struct Scanner {
8888
[[nodiscard]] dependency_directives_scan::Token &
8989
lexToken(const char *&First, const char *const End);
9090

91-
dependency_directives_scan::Token &lexIncludeFilename(const char *&First,
92-
const char *const End);
91+
[[nodiscard]] dependency_directives_scan::Token &
92+
lexIncludeFilename(const char *&First, const char *const End);
9393

9494
void skipLine(const char *&First, const char *const End);
9595
void skipDirective(StringRef Name, const char *&First, const char *const End);
@@ -544,7 +544,7 @@ Scanner::lexIncludeFilename(const char *&First, const char *const End) {
544544
void Scanner::lexPPDirectiveBody(const char *&First, const char *const End) {
545545
while (true) {
546546
const dependency_directives_scan::Token &Tok = lexToken(First, End);
547-
if (Tok.is(tok::eod))
547+
if (Tok.is(tok::eod) || Tok.is(tok::eof))
548548
break;
549549
}
550550
}
@@ -912,7 +912,11 @@ bool Scanner::lexPPLine(const char *&First, const char *const End) {
912912
case pp___include_macros:
913913
case pp_include_next:
914914
case pp_import:
915-
lexIncludeFilename(First, End);
915+
// Ignore missing filenames in include or import directives.
916+
if (lexIncludeFilename(First, End).is(tok::eod)) {
917+
skipDirective(Id, First, End);
918+
return true;
919+
}
916920
break;
917921
default:
918922
break;

clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,17 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
650650
EXPECT_STREQ("@import A.B;\n", Out.data());
651651
}
652652

653+
TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIncludesAndImports) {
654+
SmallVector<char, 128> Out;
655+
656+
ASSERT_TRUE(minimizeSourceToDependencyDirectives("#import\n", Out));
657+
ASSERT_TRUE(minimizeSourceToDependencyDirectives("#include\n", Out));
658+
ASSERT_TRUE(minimizeSourceToDependencyDirectives("#ifdef A\n"
659+
"#import \n"
660+
"#endif\n",
661+
Out));
662+
}
663+
653664
TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) {
654665
SmallVector<char, 128> Out;
655666

0 commit comments

Comments
 (0)