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

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 10 12:32:04 PDT 2025


Author: Jan Svoboda
Date: 2025-04-10T12:32:01-07:00
New Revision: dcb90780817461ba30ced78338b2270fd3307873

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

LOG: [clang][index] Skip over `#include UNDEF_IDENT` in single-file-parse mode (#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.

Added: 
    clang/test/Index/single-file-parse-include-macro.c

Modified: 
    clang/lib/Lex/PPDirectives.cpp

Removed: 
    


################################################################################
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


        


More information about the cfe-commits mailing list