Skip to content

[clang-scan-deps] Fix "unterminated conditional directive" bug #146645

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 3 commits into from
Jul 4, 2025
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
4 changes: 3 additions & 1 deletion clang/lib/Lex/DependencyDirectivesScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ static bool isQuoteCppDigitSeparator(const char *const Start,
}

void Scanner::skipLine(const char *&First, const char *const End) {
char LastNonWhitespace = ' ';
for (;;) {
assert(First <= End);
if (First == End)
Expand All @@ -430,6 +429,9 @@ void Scanner::skipLine(const char *&First, const char *const End) {
return;
}
const char *Start = First;
// Use `LastNonWhitespace`to track if a line-continuation has ever been seen
// before a new-line character:
char LastNonWhitespace = ' ';
while (First != End && !isVerticalWhitespace(*First)) {
// Iterate over strings correctly to avoid comments and newlines.
if (*First == '"' ||
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,37 @@ TEST(MinimizeSourceToDependencyDirectivesTest,
EXPECT_EQ(pp_eof, Directives[22].Kind);
}

TEST(MinimizeSourceToDependencyDirectivesTest,
TestFixedBugThatReportUnterminatedDirectiveFalsely) {
SmallVector<char, 512> Out;
SmallVector<dependency_directives_scan::Token, 16> Tokens;
SmallVector<Directive, 16> Directives;

StringRef Input = "#ifndef __TEST \n"
"#define __TEST \n"
"#if defined(__TEST_DUMMY) \n"
"#if defined(__TEST_DUMMY2) \n"
"#pragma GCC warning \\ \n"
"\"hello!\"\n"
"#else\n"
Copy link
Contributor Author

@ziqingluo-90 ziqingluo-90 Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #else directive was and is still missed by the dep-scan but I think it is irrelevant to this fix.

Copy link
Contributor

@Bigcheese Bigcheese Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that everything here should be removed except for:

#ifndef __TEST
#define __TEST
#endif

But I don't think that matters for this fix.

"#pragma GCC error \\ \n"
"\"world!\" \n"
"#endif // defined(__TEST_DUMMY2) \n"
"#endif // defined(__TEST_DUMMY) \n"
"#endif // #ifndef __TEST \n";
ASSERT_FALSE( // False on no error:
minimizeSourceToDependencyDirectives(Input, Out, Tokens, Directives));
ASSERT_TRUE(Directives.size() == 8);
EXPECT_EQ(pp_ifndef, Directives[0].Kind);
EXPECT_EQ(pp_define, Directives[1].Kind);
EXPECT_EQ(pp_if, Directives[2].Kind);
EXPECT_EQ(pp_if, Directives[3].Kind);
EXPECT_EQ(pp_endif, Directives[4].Kind);
EXPECT_EQ(pp_endif, Directives[5].Kind);
EXPECT_EQ(pp_endif, Directives[6].Kind);
EXPECT_EQ(pp_eof, Directives[7].Kind);
}

TEST(MinimizeSourceToDependencyDirectivesTest, PoundWarningAndError) {
SmallVector<char, 128> Out;

Expand Down