[clang] [Clang][Lex] Fix #include_next of a differently named header from a module header (PR #240916)
Chen Miao via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 21:52:51 PST 2026
================
@@ -1138,23 +1138,86 @@ OptionalFileEntryRef Preprocessor::LookupFile(
if (FromFile) {
// We're supposed to start looking from after a particular file. Search
// the include path until we find that file or run out of files.
- ConstSearchDirIterator TmpCurDir = CurDir;
- ConstSearchDirIterator TmpFromDir = nullptr;
- while (OptionalFileEntryRef FE = HeaderInfo.LookupFile(
- Filename, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir,
- Includers, SearchPath, RelativePath, RequestingModule,
- SuggestedModule, /*IsMapped=*/nullptr,
- /*IsFrameworkFound=*/nullptr, SkipCache)) {
- // Keep looking as if this file did a #include_next.
- TmpFromDir = TmpCurDir;
- ++TmpFromDir;
- if (&FE->getFileEntry() == FromFile) {
- // Found it.
- FromDir = TmpFromDir;
- CurDir = TmpCurDir;
- break;
+ // Filename may differ from FromFile's name (e.g. #include_next <wchar.h>
+ // from __mbstate_t.h), so locate FromFile by its own name instead.
+ auto FindFromFile = [&](StringRef LocateName) {
+ bool SameName = LocateName == Filename;
+ ConstSearchDirIterator TmpCurDir = CurDir;
+ ConstSearchDirIterator TmpFromDir = nullptr;
+ while (OptionalFileEntryRef FE = HeaderInfo.LookupFile(
+ LocateName, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir,
+ Includers, SameName ? SearchPath : nullptr,
+ SameName ? RelativePath : nullptr, RequestingModule,
+ SameName ? SuggestedModule : nullptr, /*IsMapped=*/nullptr,
+ /*IsFrameworkFound=*/nullptr, SkipCache,
+ /*BuildSystemModule=*/false, /*OpenFile=*/true,
+ /*CacheFailures=*/true, /*DiagnoseShadowing=*/false)) {
+ // Keep looking as if this file did a #include_next.
+ TmpFromDir = TmpCurDir;
+ ++TmpFromDir;
----------------
ChenMiaoi wrote:
``` cc
// module.modulemap
module Wrapper {
header "/absolute/path/to/repro/inc/wrapper.h"
export *
}
// target.h
int target(void);
// wrapper.h
#include_next <target.h>
// main.cc
#include <wrapper.h>
int use(void) { return target(); }
```
This introduces an assertion failure when a module map lists a header by absolute path. `HeaderSearch::LookupFile` can return the file for that path while setting `TmpCurDir` to null. Incrementing it here then triggers the `Invalid iterator` assertion.
https://github.com/llvm/llvm-project/pull/240916
More information about the cfe-commits
mailing list