Skip to content

Commit cf12d9a

Browse files
authored
Merge pull request #5285 from akyrtzi/pr/stable/depscan-hashhash
[Lex/DependencyDirectivesScanner] Handle the case where the source line starts with a `tok::hashhash`
2 parents 3999c7b + fe3d05c commit cf12d9a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,14 @@ bool Scanner::lexPPLine(const char *&First, const char *const End) {
742742

743743
// Lex '#'.
744744
const dependency_directives_scan::Token &HashTok = lexToken(First, End);
745+
if (HashTok.is(tok::hashhash)) {
746+
// A \p tok::hashhash at this location is passed by the preprocessor to the
747+
// parser to interpret, like any other token. So for dependency scanning
748+
// skip it like a normal token not affecting the preprocessor.
749+
skipLine(First, End);
750+
assert(First <= End);
751+
return false;
752+
}
745753
assert(HashTok.is(tok::hash));
746754
(void)HashTok;
747755

clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ TEST(MinimizeSourceToDependencyDirectivesTest, EmptyHash) {
124124
EXPECT_STREQ("#define MACRO a\n", Out.data());
125125
}
126126

127+
TEST(MinimizeSourceToDependencyDirectivesTest, HashHash) {
128+
SmallVector<char, 128> Out;
129+
130+
StringRef Source = R"(
131+
#define S
132+
#if 0
133+
##pragma cool
134+
##include "t.h"
135+
#endif
136+
#define E
137+
)";
138+
ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
139+
EXPECT_STREQ("#define S\n#if 0\n#endif\n#define E\n", Out.data());
140+
}
141+
127142
TEST(MinimizeSourceToDependencyDirectivesTest, Define) {
128143
SmallVector<char, 128> Out;
129144
SmallVector<dependency_directives_scan::Token, 4> Tokens;

0 commit comments

Comments
 (0)