Skip to content

[clang][index] Skip over #include UNDEF_IDENT in single-file-parse mode #135218

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

Conversation

jansvoboda11
Copy link
Contributor

In the 'single-file-parse' mode, seeing #include UNDEFINED_IDENTIFIER should not be treated as an error. The identifier might be defined in a header that we decided to skip, resulting in a nonsensical diagnostic from the user point of view.

@jansvoboda11 jansvoboda11 requested a review from akyrtzi April 10, 2025 17:33
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 10, 2025

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

Changes

In the 'single-file-parse' mode, seeing #include UNDEFINED_IDENTIFIER should not be treated as an error. The identifier might be defined in a header that we decided to skip, resulting in a nonsensical diagnostic from the user point of view.


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

2 Files Affected:

  • (modified) clang/lib/Lex/PPDirectives.cpp (+9)
  • (added) clang/test/Index/single-file-parse-include-macro.c (+10)
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 0b53524e23641..d97a6e8d64f9c 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -2073,6 +2073,15 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
     return;
 
   if (FilenameTok.isNot(tok::header_name)) {
+    if (FilenameTok.is(tok::identifier) && PPOpts.SingleFileParseMode) {
+      // If we saw #include IDENTIFIER and lexing didn't turn in into a header
+      // name, it was undefined. In 'single-file-parse' mode, just skip the
+      // directive without emitting diagnostics - the identifier might be
+      // normally defined in previously-skipped include directive.
+      DiscardUntilEndOfDirective();
+      return;
+    }
+
     Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
     if (FilenameTok.isNot(tok::eod))
       DiscardUntilEndOfDirective();
diff --git a/clang/test/Index/single-file-parse-include-macro.c b/clang/test/Index/single-file-parse-include-macro.c
new file mode 100644
index 0000000000000..6b6ecfd76942c
--- /dev/null
+++ b/clang/test/Index/single-file-parse-include-macro.c
@@ -0,0 +1,10 @@
+// RUN: split-file %s %t
+// RUN: c-index-test -single-file-parse %t/tu.c 2>&1 | FileCheck --allow-empty %t/tu.c
+
+//--- header1.h
+#define HEADER2_H "header2.h"
+//--- header2.h
+//--- tu.c
+#include "header1.h"
+// CHECK-NOT: tu.c:[[@LINE+1]]:10: error: expected "FILENAME" or <FILENAME>
+#include HEADER2_H

@jansvoboda11 jansvoboda11 merged commit dcb9078 into llvm:main Apr 10, 2025
14 checks passed
@jansvoboda11 jansvoboda11 deleted the single-file-parse-mode-include-undef-ident branch April 10, 2025 19:33
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…mode (llvm#135218)

In the 'single-file-parse' mode, seeing `#include UNDEFINED_IDENTIFIER`
should not be treated as an error. The identifier might be defined in a
header that we decided to skip, resulting in a nonsensical diagnostic
from the user point of view.
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