[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.
This commit is contained in:
Jan Svoboda 2025-04-10 12:32:01 -07:00 committed by GitHub
parent 72436b37bf
commit dcb9078081
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View File

@ -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();

View File

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