================
@@ -1138,23 +1126,77 @@ 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;
+        if (&FE->getFileEntry() == FromFile) {
+          // Found it.
+          FromDir = TmpFromDir;
+          CurDir = TmpCurDir;
+          return true;
+        }
       }
-    }
+      return false;
+    };
+
+    // Candidate names for FromFile: its path relative to each search directory
+    // containing it, innermost first. Directories are matched by identity, as
+    // the file may have been reached via another spelling (e.g. "./x/...").
+    // FIXME: With nested search directories (-Ix -Ix/sub) the original name is
+    // ambiguous; we assume the innermost. Fixing this requires recording the
+    // search directory when a module header is entered.
----------------
quic-asaravan wrote:

 @ChenMiaoi  Thanks for reviewing the patch. You are right. In your -I inc -I 
inc/v1 -I sys example, the innermost-first guess found other.h in inc/v1, so 
the search started at sys and skipped inc/v1/target.h.

I've added a follow-up commit. For a module header it first tries the path the 
module map gives it (v1/other.h), and the old guesses are only a fallback. I 
added your example as a test, include_next_nested_search_dirs_outer.c. 

I left the name FindFromFile as it is, since you said you don't mind.

https://github.com/llvm/llvm-project/pull/240916
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to