diff options
Diffstat (limited to 'lib/Lex/HeaderSearch.cpp')
-rw-r--r-- | lib/Lex/HeaderSearch.cpp | 99 |
1 files changed, 78 insertions, 21 deletions
diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp index 2d005dd2e1f0..e5cc30e41c57 100644 --- a/lib/Lex/HeaderSearch.cpp +++ b/lib/Lex/HeaderSearch.cpp @@ -14,7 +14,6 @@ #include "clang/Lex/HeaderSearch.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/IdentifierTable.h" -#include "clang/Frontend/PCHContainerOperations.h" #include "clang/Lex/ExternalPreprocessorSource.h" #include "clang/Lex/HeaderMap.h" #include "clang/Lex/HeaderSearchOptions.h" @@ -29,6 +28,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include <cstdio> +#include <utility> #if defined(LLVM_ON_UNIX) #include <limits.h> #endif @@ -56,9 +56,9 @@ HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts, SourceManager &SourceMgr, DiagnosticsEngine &Diags, const LangOptions &LangOpts, const TargetInfo *Target) - : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()), - FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this), - LangOpts(LangOpts) { + : HSOpts(std::move(HSOpts)), Diags(Diags), + FileMgr(SourceMgr.getFileManager()), FrameworkMap(64), + ModMap(SourceMgr, Diags, LangOpts, Target, *this) { AngledDirIdx = 0; SystemDirIdx = 0; NoCurDirSearch = false; @@ -250,8 +250,9 @@ const char *DirectoryLookup::getName() const { } const FileEntry *HeaderSearch::getFileAndSuggestModule( - StringRef FileName, const DirectoryEntry *Dir, bool IsSystemHeaderDir, - Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) { + StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir, + bool IsSystemHeaderDir, Module *RequestingModule, + ModuleMap::KnownHeader *SuggestedModule) { // If we have a module map that might map this header, load it and // check whether we'll have a suggestion for a module. const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true); @@ -272,6 +273,7 @@ const FileEntry *HeaderSearch::getFileAndSuggestModule( const FileEntry *DirectoryLookup::LookupFile( StringRef &Filename, HeaderSearch &HS, + SourceLocation IncludeLoc, SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, Module *RequestingModule, @@ -297,7 +299,7 @@ const FileEntry *DirectoryLookup::LookupFile( RelativePath->append(Filename.begin(), Filename.end()); } - return HS.getFileAndSuggestModule(TmpDir, getDir(), + return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(), isSystemHeaderDirectory(), RequestingModule, SuggestedModule); } @@ -567,7 +569,7 @@ const FileEntry *HeaderSearch::LookupFile( ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers, SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, - bool SkipCache) { + bool SkipCache, bool BuildSystemModule) { if (SuggestedModule) *SuggestedModule = ModuleMap::KnownHeader(); @@ -585,7 +587,7 @@ const FileEntry *HeaderSearch::LookupFile( RelativePath->append(Filename.begin(), Filename.end()); } // Otherwise, just return the file. - return getFileAndSuggestModule(Filename, nullptr, + return getFileAndSuggestModule(Filename, IncludeLoc, nullptr, /*IsSystemHeaderDir*/false, RequestingModule, SuggestedModule); } @@ -615,13 +617,14 @@ const FileEntry *HeaderSearch::LookupFile( // getFileAndSuggestModule, because it's a reference to an element of // a container that could be reallocated across this call. // - // FIXME: If we have no includer, that means we're processing a #include + // If we have no includer, that means we're processing a #include // from a module build. We should treat this as a system header if we're // building a [system] module. bool IncluderIsSystemHeader = - Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User; + Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User : + BuildSystemModule; if (const FileEntry *FE = getFileAndSuggestModule( - TmpDir, IncluderAndDir.second, IncluderIsSystemHeader, + TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader, RequestingModule, SuggestedModule)) { if (!Includer) { assert(First && "only first includer can have no file"); @@ -712,7 +715,7 @@ const FileEntry *HeaderSearch::LookupFile( bool InUserSpecifiedSystemFramework = false; bool HasBeenMapped = false; const FileEntry *FE = SearchDirs[i].LookupFile( - Filename, *this, SearchPath, RelativePath, RequestingModule, + Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule, SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped, MappedName); if (HasBeenMapped) { @@ -1343,19 +1346,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; } @@ -1410,13 +1414,66 @@ 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()); } SearchDir.setSearchedAllModuleMaps(true); } + +std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File, + bool *IsSystem) { + // FIXME: We assume that the path name currently cached in the FileEntry is + // the most appropriate one for this analysis (and that it's spelled the same + // way as the corresponding header search path). + const char *Name = File->getName(); + + unsigned BestPrefixLength = 0; + unsigned BestSearchDir; + + for (unsigned I = 0; I != SearchDirs.size(); ++I) { + // FIXME: Support this search within frameworks and header maps. + if (!SearchDirs[I].isNormalDir()) + continue; + + const char *Dir = SearchDirs[I].getDir()->getName(); + for (auto NI = llvm::sys::path::begin(Name), + NE = llvm::sys::path::end(Name), + DI = llvm::sys::path::begin(Dir), + DE = llvm::sys::path::end(Dir); + /*termination condition in loop*/; ++NI, ++DI) { + // '.' components in Name are ignored. + while (NI != NE && *NI == ".") + ++NI; + if (NI == NE) + break; + + // '.' components in Dir are ignored. + while (DI != DE && *DI == ".") + ++DI; + if (DI == DE) { + // Dir is a prefix of Name, up to '.' components and choice of path + // separators. + unsigned PrefixLength = NI - llvm::sys::path::begin(Name); + if (PrefixLength > BestPrefixLength) { + BestPrefixLength = PrefixLength; + BestSearchDir = I; + } + break; + } + + if (*NI != *DI) + break; + } + } + + if (IsSystem) + *IsSystem = BestPrefixLength ? BestSearchDir >= SystemDirIdx : false; + return Name + BestPrefixLength; +} |