diff --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp index 57652be8244b4..4bb57a0751d42 100644 --- a/clang/lib/Lex/DependencyDirectivesScanner.cpp +++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp @@ -88,8 +88,8 @@ struct Scanner { [[nodiscard]] dependency_directives_scan::Token & lexToken(const char *&First, const char *const End); - dependency_directives_scan::Token &lexIncludeFilename(const char *&First, - const char *const End); + [[nodiscard]] dependency_directives_scan::Token & + lexIncludeFilename(const char *&First, const char *const End); void skipLine(const char *&First, const char *const End); void skipDirective(StringRef Name, const char *&First, const char *const End); @@ -544,7 +544,7 @@ Scanner::lexIncludeFilename(const char *&First, const char *const End) { void Scanner::lexPPDirectiveBody(const char *&First, const char *const End) { while (true) { const dependency_directives_scan::Token &Tok = lexToken(First, End); - if (Tok.is(tok::eod)) + if (Tok.is(tok::eod) || Tok.is(tok::eof)) break; } } @@ -901,7 +901,11 @@ bool Scanner::lexPPLine(const char *&First, const char *const End) { case pp___include_macros: case pp_include_next: case pp_import: - lexIncludeFilename(First, End); + // Ignore missing filenames in include or import directives. + if (lexIncludeFilename(First, End).is(tok::eod)) { + skipDirective(Id, First, End); + return true; + } break; default: break; diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp index 94af9688a96e2..91f1dca2aadd5 100644 --- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp +++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp @@ -650,6 +650,17 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) { EXPECT_STREQ("@import A.B;\n", Out.data()); } +TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIncludesAndImports) { + SmallVector Out; + + ASSERT_TRUE(minimizeSourceToDependencyDirectives("#import\n", Out)); + ASSERT_TRUE(minimizeSourceToDependencyDirectives("#include\n", Out)); + ASSERT_TRUE(minimizeSourceToDependencyDirectives("#ifdef A\n" + "#import \n" + "#endif\n", + Out)); +} + TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) { SmallVector Out;