Index: clang/include/clang/Lex/PPCallbacks.h =================================================================== --- clang/include/clang/Lex/PPCallbacks.h +++ clang/include/clang/Lex/PPCallbacks.h @@ -83,6 +83,16 @@ const Token &FilenameTok, SrcMgr::CharacteristicKind FileType) {} + /// Callback invoked whenever an inclusion directive results in a + /// file-not-found error. + /// + /// \param FileName The name of the file being included, as written in the + /// source code. + /// + /// \returns true to indicate that the preprocessor should skip this file + /// and not issue any diagnostic. + virtual bool FileNotFound(StringRef FileName) { return false; } + /// Callback invoked whenever an inclusion directive of /// any kind (\c \#include, \c \#import, etc.) has been processed, regardless /// of whether the inclusion will actually result in an inclusion. @@ -451,6 +461,14 @@ Second->FileSkipped(SkippedFile, FilenameTok, FileType); } + bool FileNotFound(StringRef FileName) override { + bool Skip = First->FileNotFound(FileName); + // Make sure to invoke the second callback, no matter if the first already + // returned true to skip the file. + Skip |= Second->FileNotFound(FileName); + return Skip; + } + void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, Index: clang/lib/Lex/PPDirectives.cpp =================================================================== --- clang/lib/Lex/PPDirectives.cpp +++ clang/lib/Lex/PPDirectives.cpp @@ -2019,6 +2019,10 @@ if (File) return File; + // Give the clients a chance to silently skip this include. + if (Callbacks && Callbacks->FileNotFound(Filename)) + return std::nullopt; + if (SuppressIncludeNotFoundError) return std::nullopt;