Skip to content

Commit df100a2

Browse files
akyrtziyuxuanchen1997
authored andcommitted
[clang/Lex/DependencyDirectivesScanner] Ignore import/include directives with missing filenames without failing the scan (#100126)
Summary: Follow-up to `34ab855826b8cb0c3b46c770b83390bd1fe95c64`: * Don't fail the scan with an include with missing filename, it may be inside a skipped preprocessor block. Let the compilation provide any related error. * Fix an issue where the lexer was skipping through the next directive, after ignoring the include with missing filename. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251051
1 parent 23414fb commit df100a2

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,7 @@ bool Scanner::lexPPLine(const char *&First, const char *const End) {
914914
case pp_import:
915915
// Ignore missing filenames in include or import directives.
916916
if (lexIncludeFilename(First, End).is(tok::eod)) {
917-
skipDirective(Id, First, End);
918-
return true;
917+
return false;
919918
}
920919
break;
921920
default:

clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,28 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
653653
TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIncludesAndImports) {
654654
SmallVector<char, 128> Out;
655655

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));
656+
ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import\n", Out));
657+
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
658+
659+
ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include\n", Out));
660+
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
661+
662+
ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
663+
"#import \n"
664+
"#endif\n",
665+
Out));
666+
// The ifdef block is removed because it's "empty".
667+
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
668+
669+
ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
670+
"#import \n"
671+
"#define B\n"
672+
"#endif\n",
673+
Out));
674+
EXPECT_STREQ("#ifdef A\n"
675+
"#define B\n"
676+
"#endif\n",
677+
Out.data());
662678
}
663679

664680
TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) {

0 commit comments

Comments
 (0)