Skip to content

Commit

Permalink
[Modules] Use vfs for (recursive) directory iteration
Browse files Browse the repository at this point in the history
Clang performs directory walk while searching headers inside modules by
using the ::sys::fs instead of ::vfs. This prevents any code that uses
the VFS (e.g, reproducer scripts) to actually find such headers, since
the VFS will never be searched for those.

Change these places to use vfs::recursive_directory_iterator and
vfs::directory_iterator instead.

Differential Revision: http://reviews.llvm.org/D20266

rdar://problem/25880368

llvm-svn: 269661
  • Loading branch information
bcardosolopes committed May 16, 2016
1 parent f3c7a35 commit b171a59
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
13 changes: 7 additions & 6 deletions clang/lib/Frontend/FrontendActions.cpp
Expand Up @@ -209,17 +209,18 @@ collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
std::error_code EC;
SmallString<128> DirNative;
llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC),
DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {

vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
for (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
// headers.
if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName()))
.Cases(".h", ".H", ".hh", ".hpp", true)
.Default(false))
continue;

const FileEntry *Header = FileMgr.getFile(Dir->path());
const FileEntry *Header = FileMgr.getFile(Dir->getName());
// FIXME: This shouldn't happen unless there is a file system race. Is
// that worth diagnosing?
if (!Header)
Expand All @@ -232,7 +233,7 @@ collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,

// Compute the relative path from the directory to this file.
SmallVector<StringRef, 16> Components;
auto PathIt = llvm::sys::path::rbegin(Dir->path());
auto PathIt = llvm::sys::path::rbegin(Dir->getName());
for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
Components.push_back(*PathIt);
SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
Expand Down
19 changes: 11 additions & 8 deletions clang/lib/Lex/HeaderSearch.cpp
Expand Up @@ -1341,19 +1341,20 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
DirNative);

// Search each of the ".framework" directories to load them as modules.
for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
if (llvm::sys::path::extension(Dir->path()) != ".framework")
if (llvm::sys::path::extension(Dir->getName()) != ".framework")
continue;

const DirectoryEntry *FrameworkDir =
FileMgr.getDirectory(Dir->path());
FileMgr.getDirectory(Dir->getName());
if (!FrameworkDir)
continue;

// Load this framework module.
loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
IsSystem);
loadFrameworkModule(llvm::sys::path::stem(Dir->getName()),
FrameworkDir, IsSystem);
}
continue;
}
Expand Down Expand Up @@ -1408,11 +1409,13 @@ void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
std::error_code EC;
SmallString<128> DirNative;
llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
bool IsFramework =
llvm::sys::path::extension(Dir->getName()) == ".framework";
if (IsFramework == SearchDir.isFramework())
loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
loadModuleMapFile(Dir->getName(), SearchDir.isSystemHeaderDirectory(),
SearchDir.isFramework());
}

Expand Down
18 changes: 11 additions & 7 deletions clang/lib/Lex/ModuleMap.cpp
Expand Up @@ -711,13 +711,15 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
= StringRef(FrameworkDir->getName());
llvm::sys::path::append(SubframeworksDirName, "Frameworks");
llvm::sys::path::native(SubframeworksDirName);
for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName, EC), DirEnd;
vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
for (vfs::directory_iterator Dir = FS.dir_begin(SubframeworksDirName, EC),
DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
if (!StringRef(Dir->path()).endswith(".framework"))
if (!StringRef(Dir->getName()).endswith(".framework"))
continue;

if (const DirectoryEntry *SubframeworkDir
= FileMgr.getDirectory(Dir->path())) {
if (const DirectoryEntry *SubframeworkDir =
FileMgr.getDirectory(Dir->getName())) {
// Note: as an egregious but useful hack, we use the real path here and
// check whether it is actually a subdirectory of the parent directory.
// This will not be the case if the 'subframework' is actually a symlink
Expand Down Expand Up @@ -1931,11 +1933,13 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
// uncommonly used Tcl module on Darwin platforms.
std::error_code EC;
SmallVector<Module::Header, 6> Headers;
for (llvm::sys::fs::recursive_directory_iterator I(Dir->getName(), EC), E;
vfs::FileSystem &FS = *SourceMgr.getFileManager().getVirtualFileSystem();
for (vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
I != E && !EC; I.increment(EC)) {
if (const FileEntry *FE = SourceMgr.getFileManager().getFile(I->path())) {
if (const FileEntry *FE =
SourceMgr.getFileManager().getFile(I->getName())) {

Module::Header Header = {I->path(), FE};
Module::Header Header = {I->getName(), FE};
Headers.push_back(std::move(Header));
}
}
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Modules/crash-vfs-umbrella-frameworks.m
Expand Up @@ -40,3 +40,16 @@
// CHECKYAML-NEXT: 'external-contents': "/[[PATH]]/i/Frameworks/B.framework/Headers/B.h"

@import I;

// Run the reproducer script - regular exit code is enough to test it works. The
// intent here is to guarantee that the collect umbrella headers into the VFS
// can be used, testing that vfs::recursive_directory_iterator is used correctly
// Make sure to erase the include paths used to build the modules to guarantee
// that the VFS overlay won't fallback to use it. Also wipe out the module cache
// to force header search.
//
// RUN: cd %t
// RUN: rm -rf i
// RUN: rm -rf crash-vfs-umbrella-*.cache/modules/*
// RUN: chmod 755 crash-vfs-*.sh
// RUN: ./crash-vfs-*.sh

0 comments on commit b171a59

Please sign in to comment.