Skip to content

[clang-scan-deps] Implement P2223R2 for DependencyDirectiveScanner.cpp #143950

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 1 commit into from
Jun 13, 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
32 changes: 22 additions & 10 deletions clang/lib/Lex/DependencyDirectivesScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ static unsigned skipNewline(const char *&First, const char *End) {
return Len;
}

static bool wasLineContinuation(const char *First, unsigned EOLLen) {
return *(First - (int)EOLLen - 1) == '\\';
}

static void skipToNewlineRaw(const char *&First, const char *const End) {
for (;;) {
if (First == End)
Expand All @@ -336,13 +332,16 @@ static void skipToNewlineRaw(const char *&First, const char *const End) {
if (Len)
return;

char LastNonWhitespace = ' ';
do {
if (!isHorizontalWhitespace(*First))
LastNonWhitespace = *First;
if (++First == End)
return;
Len = isEOL(First, End);
} while (!Len);

if (First[-1] != '\\')
if (LastNonWhitespace != '\\')
return;

First += Len;
Expand Down Expand Up @@ -394,6 +393,7 @@ 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 @@ -419,6 +419,8 @@ void Scanner::skipLine(const char *&First, const char *const End) {
// Iterate over comments correctly.
if (*First != '/' || End - First < 2) {
LastTokenPtr = First;
if (!isWhitespace(*First))
LastNonWhitespace = *First;
++First;
continue;
}
Expand All @@ -431,6 +433,8 @@ void Scanner::skipLine(const char *&First, const char *const End) {

if (First[1] != '*') {
LastTokenPtr = First;
if (!isWhitespace(*First))
LastNonWhitespace = *First;
++First;
continue;
}
Expand All @@ -442,8 +446,9 @@ void Scanner::skipLine(const char *&First, const char *const End) {
return;

// Skip over the newline.
unsigned Len = skipNewline(First, End);
if (!wasLineContinuation(First, Len)) // Continue past line-continuations.
skipNewline(First, End);

if (LastNonWhitespace != '\\')
break;
}
}
Expand All @@ -468,9 +473,16 @@ static void skipWhitespace(const char *&First, const char *const End) {
if (End - First < 2)
return;

if (First[0] == '\\' && isVerticalWhitespace(First[1])) {
skipNewline(++First, End);
continue;
if (*First == '\\') {
const char *Ptr = First + 1;
while (Ptr < End && isHorizontalWhitespace(*Ptr))
++Ptr;
if (Ptr != End && isVerticalWhitespace(*Ptr)) {
skipNewline(Ptr, End);
First = Ptr;
continue;
}
return;
}

// Check for a non-comment character.
Expand Down
91 changes: 91 additions & 0 deletions clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,97 @@ TEST(MinimizeSourceToDependencyDirectivesTest,
Out.data());
}

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

ASSERT_FALSE(minimizeSourceToDependencyDirectives("// some comment \\ \n"
"module A;\n",
Out));
EXPECT_STREQ("", Out.data());
}

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

StringRef Input = "#define \\ \n"
"A\n"
"#undef\t\\ \n"
"A\n"
"#endif \\\t\t\n"
"\n"
"#if \\ \t\n"
"A\n"
"#ifdef\t\\ \n"
"A\n"
"#ifndef \\ \t\n"
"A\n"
"#elifdef \\ \n"
"A\n"
"#elifndef \\ \n"
"A\n"
"#elif \\\t\t \n"
"A\n"
"#else \\\t \t\n"
"\n"
"#include \\ \n"
"<A>\n"
"#include_next \\ \n"
"<A>\n"
"#__include_macros\\ \n"
"<A>\n"
"#import \\ \t\n"
"<A>\n"
"@import \\\t \n"
"A;\n"
"#pragma clang \\ \n"
"module \\ \n"
"import A\n"
"#pragma \\ \n"
"push_macro(A)\n"
"#pragma \\\t \n"
"pop_macro(A)\n"
"#pragma \\ \n"
"include_alias(<A>,\\ \n"
"<B>)\n"
"export \\ \n"
"module m;\n"
"import\t\\\t \n"
"m;\n"
"#pragma\t\\ \n"
"clang\t\\ \t\n"
"system_header\n";
ASSERT_FALSE(
minimizeSourceToDependencyDirectives(Input, Out, Tokens, Directives));

EXPECT_EQ(pp_define, Directives[0].Kind);
EXPECT_EQ(pp_undef, Directives[1].Kind);
EXPECT_EQ(pp_endif, Directives[2].Kind);
EXPECT_EQ(pp_if, Directives[3].Kind);
EXPECT_EQ(pp_ifdef, Directives[4].Kind);
EXPECT_EQ(pp_ifndef, Directives[5].Kind);
EXPECT_EQ(pp_elifdef, Directives[6].Kind);
EXPECT_EQ(pp_elifndef, Directives[7].Kind);
EXPECT_EQ(pp_elif, Directives[8].Kind);
EXPECT_EQ(pp_else, Directives[9].Kind);
EXPECT_EQ(pp_include, Directives[10].Kind);
EXPECT_EQ(pp_include_next, Directives[11].Kind);
EXPECT_EQ(pp___include_macros, Directives[12].Kind);
EXPECT_EQ(pp_import, Directives[13].Kind);
EXPECT_EQ(decl_at_import, Directives[14].Kind);
EXPECT_EQ(pp_pragma_import, Directives[15].Kind);
EXPECT_EQ(pp_pragma_push_macro, Directives[16].Kind);
EXPECT_EQ(pp_pragma_pop_macro, Directives[17].Kind);
EXPECT_EQ(pp_pragma_include_alias, Directives[18].Kind);
EXPECT_EQ(cxx_export_module_decl, Directives[19].Kind);
EXPECT_EQ(cxx_import_decl, Directives[20].Kind);
EXPECT_EQ(pp_pragma_system_header, Directives[21].Kind);
EXPECT_EQ(pp_eof, Directives[22].Kind);
}

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

Expand Down
Loading