[clang] 34ab855 - [clang][deps] Ignore import/include directives with missing filenames (#99520)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 22 21:10:08 PDT 2024


Author: Cyndy Ishida
Date: 2024-07-22T21:10:05-07:00
New Revision: 34ab855826b8cb0c3b46c770b83390bd1fe95c64

URL: https://github.com/llvm/llvm-project/commit/34ab855826b8cb0c3b46c770b83390bd1fe95c64
DIFF: https://github.com/llvm/llvm-project/commit/34ab855826b8cb0c3b46c770b83390bd1fe95c64.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Lex/DependencyDirectivesScanner.cpp
    clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp
index 8a020d0e95fe3..31a4c0f52b465 100644
--- a/clang/lib/Lex/DependencyDirectivesScanner.cpp
+++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp
@@ -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);
@@ -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 23304fff950eb..513e184be09ec 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;
 


        


More information about the cfe-commits mailing list