diff options
author | Cyndy Ishida <cyndy_ishida@apple.com> | 2024-07-22 21:10:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-22 21:10:05 -0700 |
commit | 34ab855826b8cb0c3b46c770b83390bd1fe95c64 (patch) | |
tree | bfe11189e31d14a60747ebab81fa9cffc30b10cf | |
parent | 464ea880cf7710cc8675c83001d7ae020406cf42 (diff) | |
download | llvm-34ab855826b8cb0c3b46c770b83390bd1fe95c64.zip llvm-34ab855826b8cb0c3b46c770b83390bd1fe95c64.tar.gz llvm-34ab855826b8cb0c3b46c770b83390bd1fe95c64.tar.bz2 |
[clang][deps] Ignore import/include directives with missing filenames (#99520)
Previously source input like `#import ` resulted in infinite calls
append the same token into `CurDirTokens`. This patch now ignores those
directive lines if they won't actually end up being compiled. (e.g.
macro guarded)
resolves: rdar://121247565
-rw-r--r-- | clang/lib/Lex/DependencyDirectivesScanner.cpp | 12 | ||||
-rw-r--r-- | clang/unittests/Lex/DependencyDirectivesScannerTest.cpp | 11 |
2 files changed, 19 insertions, 4 deletions
diff --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp index 8a020d0..31a4c0f 100644 --- a/clang/lib/Lex/DependencyDirectivesScanner.cpp +++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp @@ -88,8 +88,8 @@ private: [[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); @@ -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; } } @@ -912,7 +912,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; diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp index 23304ff..513e184 100644 --- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp +++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp @@ -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; |