Skip to content

[clang-scan-deps] Ignore import/include directives with missing filenames #99520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions clang/lib/Lex/DependencyDirectivesScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,17 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
EXPECT_STREQ("@import A.B;\n", Out.data());
}

TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIncludesAndImports) {
SmallVector<char, 128> 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<char, 128> Out;

Expand Down
Loading