Skip to content

[clang/Lex/DependencyDirectivesScanner] Ignore import/include directives with missing filenames without failing the scan #100126

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
Jul 23, 2024

Conversation

akyrtzi
Copy link
Contributor

@akyrtzi akyrtzi commented Jul 23, 2024

Follow-up to 34ab855826b8cb0c3b46c770b83390bd1fe95c64:

  • Don't fail the scan with an include with missing filename, it may be inside a skipped preprocessor block. Let the compilation provide any related error.
  • Fix an issue where the lexer was skipping through the next directive, after ignoring the include with missing filename.

…ves with missing filenames without failing the scan

Follow-up to `34ab855826b8cb0c3b46c770b83390bd1fe95c64`:

* Don't fail the scan with an include with missing filename, it may be inside a skipped preprocessor block.
  Let the compilation provide any related error.
* Fix an issue where the lexer was skipping through the next directive, after ignoring the include with missing filename.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 23, 2024

@llvm/pr-subscribers-clang

Author: Argyrios Kyrtzidis (akyrtzi)

Changes

Follow-up to 34ab855826b8cb0c3b46c770b83390bd1fe95c64:

  • Don't fail the scan with an include with missing filename, it may be inside a skipped preprocessor block. Let the compilation provide any related error.
  • Fix an issue where the lexer was skipping through the next directive, after ignoring the include with missing filename.

Full diff: https://github.com/llvm/llvm-project/pull/100126.diff

2 Files Affected:

  • (modified) clang/lib/Lex/DependencyDirectivesScanner.cpp (+1-2)
  • (modified) clang/unittests/Lex/DependencyDirectivesScannerTest.cpp (+22-6)
diff --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp
index 31a4c0f52b465..088d1cc96e3a2 100644
--- a/clang/lib/Lex/DependencyDirectivesScanner.cpp
+++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp
@@ -914,8 +914,7 @@ bool Scanner::lexPPLine(const char *&First, const char *const End) {
   case pp_import:
     // Ignore missing filenames in include or import directives.
     if (lexIncludeFilename(First, End).is(tok::eod)) {
-      skipDirective(Id, First, End);
-      return true;
+      return false;
     }
     break;
   default:
diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
index 513e184be09ec..bdb5e23510118 100644
--- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
+++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
@@ -653,12 +653,28 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
 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));
+  ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import\n", Out));
+  EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
+
+  ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include\n", Out));
+  EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
+
+  ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
+                                                    "#import \n"
+                                                    "#endif\n",
+                                                    Out));
+  // The ifdef block is removed because it's "empty".
+  EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
+
+  ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
+                                                    "#import \n"
+                                                    "#define B\n"
+                                                    "#endif\n",
+                                                    Out));
+  EXPECT_STREQ("#ifdef A\n"
+               "#define B\n"
+               "#endif\n",
+               Out.data());
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) {

@akyrtzi akyrtzi merged commit d19e71d into llvm:main Jul 23, 2024
7 of 10 checks passed
@akyrtzi akyrtzi deleted the akyrtzi/followup-121247565 branch July 23, 2024 20:00
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…ves with missing filenames without failing the scan (#100126)

Summary:
Follow-up to `34ab855826b8cb0c3b46c770b83390bd1fe95c64`:

* Don't fail the scan with an include with missing filename, it may be
inside a skipped preprocessor block. Let the compilation provide any
related error.
* Fix an issue where the lexer was skipping through the next directive,
after ignoring the include with missing filename.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251051
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants