aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/FrontendAction.cpp
diff options
context:
space:
mode:
authorRichard Howell <rmaz@users.noreply.github.com>2025-09-02 08:56:55 -0700
committerGitHub <noreply@github.com>2025-09-02 08:56:55 -0700
commitbde2abd3a6299dc60fd6ec6726b77808d0c17dbd (patch)
tree8dda69713075955891a3fd18014db7558d9549c3 /clang/lib/Frontend/FrontendAction.cpp
parent85136993b6aea653bf78b8f1eb86a49ada9c02c7 (diff)
downloadllvm-bde2abd3a6299dc60fd6ec6726b77808d0c17dbd.zip
llvm-bde2abd3a6299dc60fd6ec6726b77808d0c17dbd.tar.gz
llvm-bde2abd3a6299dc60fd6ec6726b77808d0c17dbd.tar.bz2
[clang] load umbrella dir headers in sorted order (#156108)
Clang modules sort the umbrella dir headers by name before adding to the module's includes to ensure deterministic output across different file systems. This is insufficient however, as the header search table is also serialized. This includes all the loaded headers by file reference, which are allocated incrementally. To ensure stable output we have to also create the file references in sorted order.
Diffstat (limited to 'clang/lib/Frontend/FrontendAction.cpp')
-rw-r--r--clang/lib/Frontend/FrontendAction.cpp40
1 files changed, 21 insertions, 19 deletions
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index a7d6a068..6b1fcac 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -623,7 +623,7 @@ static std::error_code collectModuleHeaderIncludes(
llvm::sys::path::native(UmbrellaDir->Entry.getName(), DirNative);
llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
- SmallVector<std::pair<std::string, FileEntryRef>, 8> Headers;
+ SmallVector<std::pair<std::string, std::string>, 8> HeaderPaths;
for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
Dir != End && !EC; Dir.increment(EC)) {
// Check whether this entry has an extension typically associated with
@@ -633,17 +633,6 @@ static std::error_code collectModuleHeaderIncludes(
.Default(false))
continue;
- auto Header = FileMgr.getOptionalFileRef(Dir->path());
- // FIXME: This shouldn't happen unless there is a file system race. Is
- // that worth diagnosing?
- if (!Header)
- continue;
-
- // If this header is marked 'unavailable' in this module, don't include
- // it.
- if (ModMap.isHeaderUnavailableInModule(*Header, Module))
- continue;
-
// Compute the relative path from the directory to this file.
SmallVector<StringRef, 16> Components;
auto PathIt = llvm::sys::path::rbegin(Dir->path());
@@ -655,20 +644,33 @@ static std::error_code collectModuleHeaderIncludes(
++It)
llvm::sys::path::append(RelativeHeader, *It);
- std::string RelName = RelativeHeader.c_str();
- Headers.push_back(std::make_pair(RelName, *Header));
+ HeaderPaths.push_back(
+ std::make_pair(Dir->path().str(), RelativeHeader.c_str()));
}
if (EC)
return EC;
// Sort header paths and make the header inclusion order deterministic
- // across different OSs and filesystems.
- llvm::sort(Headers, llvm::less_first());
- for (auto &H : Headers) {
+ // across different OSs and filesystems. As the header search table
+ // serialization order depends on the file reference UID, we need to create
+ // file references in deterministic order too.
+ llvm::sort(HeaderPaths, llvm::less_first());
+ for (auto &[Path, RelPath] : HeaderPaths) {
+ auto Header = FileMgr.getOptionalFileRef(Path);
+ // FIXME: This shouldn't happen unless there is a file system race. Is
+ // that worth diagnosing?
+ if (!Header)
+ continue;
+
+ // If this header is marked 'unavailable' in this module, don't include
+ // it.
+ if (ModMap.isHeaderUnavailableInModule(*Header, Module))
+ continue;
+
// Include this header as part of the umbrella directory.
- Module->addTopHeader(H.second);
- addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
+ Module->addTopHeader(*Header);
+ addHeaderInclude(RelPath, Includes, LangOpts, Module->IsExternC);
}
}