Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 1 | //===- HeaderSearch.cpp - Resolve Header File Locations -------------------===// |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DirectoryLookup and HeaderSearch interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 14 | #include "clang/Lex/HeaderSearch.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | ef6b136 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Basic/IdentifierTable.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Module.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Basic/VirtualFileSystem.h" |
| 21 | #include "clang/Lex/DirectoryLookup.h" |
Richard Smith | 2aedca3 | 2015-07-01 02:29:35 +0000 | [diff] [blame] | 22 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Lex/HeaderMap.h" |
| 24 | #include "clang/Lex/HeaderSearchOptions.h" |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 25 | #include "clang/Lex/LexDiagnostic.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 26 | #include "clang/Lex/ModuleMap.h" |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/APInt.h" |
| 29 | #include "llvm/ADT/Hashing.h" |
Chris Lattner | 43fd42e | 2006-10-30 03:40:58 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/ADT/StringRef.h" |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 33 | #include "llvm/Pass.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Allocator.h" |
Ted Kremenek | ae63d10 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Capacity.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "llvm/Support/FileSystem.h" |
| 38 | #include "llvm/Support/Path.h" |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 39 | #include "llvm/Support/Timer.h" |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 40 | #include <algorithm> |
| 41 | #include <cassert> |
| 42 | #include <cstddef> |
Chris Lattner | c25d8a7 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 43 | #include <cstdio> |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 44 | #include <cstring> |
| 45 | #include <string> |
| 46 | #include <system_error> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 47 | #include <utility> |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 49 | using namespace clang; |
| 50 | |
Douglas Gregor | 99734e7 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 51 | const IdentifierInfo * |
Richard Smith | 2aedca3 | 2015-07-01 02:29:35 +0000 | [diff] [blame] | 52 | HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) { |
| 53 | if (ControllingMacro) { |
Chandler Carruth | 5966677 | 2016-11-04 06:32:57 +0000 | [diff] [blame] | 54 | if (ControllingMacro->isOutOfDate()) { |
| 55 | assert(External && "We must have an external source if we have a " |
| 56 | "controlling macro that is out of date."); |
Richard Smith | 2aedca3 | 2015-07-01 02:29:35 +0000 | [diff] [blame] | 57 | External->updateOutOfDateIdentifier( |
| 58 | *const_cast<IdentifierInfo *>(ControllingMacro)); |
Chandler Carruth | 5966677 | 2016-11-04 06:32:57 +0000 | [diff] [blame] | 59 | } |
Douglas Gregor | 99734e7 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 60 | return ControllingMacro; |
Richard Smith | 2aedca3 | 2015-07-01 02:29:35 +0000 | [diff] [blame] | 61 | } |
Douglas Gregor | 99734e7 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 62 | |
| 63 | if (!ControllingMacroID || !External) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 64 | return nullptr; |
Douglas Gregor | 99734e7 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 65 | |
| 66 | ControllingMacro = External->GetIdentifier(ControllingMacroID); |
| 67 | return ControllingMacro; |
| 68 | } |
| 69 | |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 70 | ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() = default; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 71 | |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 72 | HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts, |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 73 | SourceManager &SourceMgr, DiagnosticsEngine &Diags, |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 74 | const LangOptions &LangOpts, |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 75 | const TargetInfo *Target) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 76 | : HSOpts(std::move(HSOpts)), Diags(Diags), |
| 77 | FileMgr(SourceMgr.getFileManager()), FrameworkMap(64), |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 78 | ModMap(SourceMgr, Diags, LangOpts, Target, *this) {} |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 79 | |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 80 | HeaderSearch::~HeaderSearch() { |
| 81 | // Delete headermaps. |
| 82 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
| 83 | delete HeaderMaps[i].second; |
| 84 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 86 | void HeaderSearch::PrintStats() { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 87 | fprintf(stderr, "\n*** HeaderSearch Stats:\n"); |
| 88 | fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 89 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 90 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 91 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 92 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 93 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 94 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 95 | } |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 96 | fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); |
| 97 | fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); |
| 98 | fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 100 | fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); |
| 101 | fprintf(stderr, " %d #includes skipped due to" |
| 102 | " the multi-include optimization.\n", NumMultiIncludeFileOptzn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 104 | fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); |
| 105 | fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 108 | /// CreateHeaderMap - This method returns a HeaderMap for the specified |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 109 | /// FileEntry, uniquing them through the 'HeaderMaps' datastructure. |
Chris Lattner | 4ffe46c | 2007-12-17 18:34:53 +0000 | [diff] [blame] | 110 | const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 111 | // We expect the number of headermaps to be small, and almost always empty. |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 112 | // If it ever grows, use of a linear search should be re-evaluated. |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 113 | if (!HeaderMaps.empty()) { |
| 114 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 115 | // Pointer equality comparison of FileEntries works because they are |
| 116 | // already uniqued by inode. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | if (HeaderMaps[i].first == FE) |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 118 | return HeaderMaps[i].second; |
| 119 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 121 | if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 122 | HeaderMaps.push_back(std::make_pair(FE, HM)); |
| 123 | return HM; |
| 124 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 126 | return nullptr; |
Chris Lattner | c4ba38e | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Bruno Cardoso Lopes | 181225b | 2016-12-11 04:27:28 +0000 | [diff] [blame] | 129 | /// \brief Get filenames for all registered header maps. |
| 130 | void HeaderSearch::getHeaderMapFileNames( |
| 131 | SmallVectorImpl<std::string> &Names) const { |
| 132 | for (auto &HM : HeaderMaps) |
| 133 | Names.push_back(HM.first->getName()); |
| 134 | } |
| 135 | |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 136 | std::string HeaderSearch::getCachedModuleFileName(Module *Module) { |
Ben Langmuir | 9d6448b | 2014-08-09 00:57:23 +0000 | [diff] [blame] | 137 | const FileEntry *ModuleMap = |
| 138 | getModuleMap().getModuleMapFileForUniquing(Module); |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 139 | return getCachedModuleFileName(Module->Name, ModuleMap->getName()); |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 142 | std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName, |
| 143 | bool FileMapOnly) { |
| 144 | // First check the module name to pcm file map. |
| 145 | auto i (HSOpts->PrebuiltModuleFiles.find(ModuleName)); |
| 146 | if (i != HSOpts->PrebuiltModuleFiles.end()) |
| 147 | return i->second; |
| 148 | |
| 149 | if (FileMapOnly || HSOpts->PrebuiltModulePaths.empty()) |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 150 | return {}; |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 151 | |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 152 | // Then go through each prebuilt module directory and try to find the pcm |
| 153 | // file. |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 154 | for (const std::string &Dir : HSOpts->PrebuiltModulePaths) { |
| 155 | SmallString<256> Result(Dir); |
| 156 | llvm::sys::fs::make_absolute(Result); |
| 157 | llvm::sys::path::append(Result, ModuleName + ".pcm"); |
| 158 | if (getFileMgr().getFile(Result.str())) |
| 159 | return Result.str().str(); |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 160 | } |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 161 | return {}; |
| 162 | } |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 163 | |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 164 | std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName, |
| 165 | StringRef ModuleMapPath) { |
Richard Smith | d520a25 | 2015-07-21 18:07:47 +0000 | [diff] [blame] | 166 | // If we don't have a module cache path or aren't supposed to use one, we |
| 167 | // can't do anything. |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 168 | if (getModuleCachePath().empty()) |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 169 | return {}; |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 170 | |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 171 | SmallString<256> Result(getModuleCachePath()); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 172 | llvm::sys::fs::make_absolute(Result); |
| 173 | |
| 174 | if (HSOpts->DisableModuleHash) { |
| 175 | llvm::sys::path::append(Result, ModuleName + ".pcm"); |
| 176 | } else { |
| 177 | // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 178 | // ideally be globally unique to this particular module. Name collisions |
| 179 | // in the hash are safe (because any translation unit can only import one |
| 180 | // module with each name), but result in a loss of caching. |
| 181 | // |
| 182 | // To avoid false-negatives, we form as canonical a path as we can, and map |
| 183 | // to lower-case in case we're on a case-insensitive file system. |
Richard Smith | 3f57cff | 2017-03-09 00:58:22 +0000 | [diff] [blame] | 184 | std::string Parent = llvm::sys::path::parent_path(ModuleMapPath); |
| 185 | if (Parent.empty()) |
| 186 | Parent = "."; |
| 187 | auto *Dir = FileMgr.getDirectory(Parent); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 188 | if (!Dir) |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 189 | return {}; |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 190 | auto DirName = FileMgr.getCanonicalName(Dir); |
| 191 | auto FileName = llvm::sys::path::filename(ModuleMapPath); |
| 192 | |
| 193 | llvm::hash_code Hash = |
Adrian Prantl | 793038d3 | 2016-01-12 21:01:56 +0000 | [diff] [blame] | 194 | llvm::hash_combine(DirName.lower(), FileName.lower()); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 195 | |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 196 | SmallString<128> HashStr; |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 197 | llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 198 | llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm"); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 199 | } |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 200 | return Result.str().str(); |
| 201 | } |
| 202 | |
| 203 | Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 204 | llvm::NamedRegionTimer T("lookupmodule", "Lookup Module", IncGroupName, |
| 205 | IncGroupDescription, llvm::TimePassesIsEnabled); |
| 206 | |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 207 | // Look in the module map to determine if there is a module by this name. |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 208 | Module *Module = ModMap.findModule(ModuleName); |
Richard Smith | 47972af | 2015-06-16 00:08:24 +0000 | [diff] [blame] | 209 | if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps) |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 210 | return Module; |
Graydon Hoare | 4d86764 | 2016-12-21 00:24:39 +0000 | [diff] [blame] | 211 | |
| 212 | StringRef SearchName = ModuleName; |
| 213 | Module = lookupModule(ModuleName, SearchName); |
| 214 | |
| 215 | // The facility for "private modules" -- adjacent, optional module maps named |
| 216 | // module.private.modulemap that are supposed to define private submodules -- |
Bruno Cardoso Lopes | 29729919 | 2017-12-22 02:53:30 +0000 | [diff] [blame] | 217 | // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private. |
| 218 | // |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 219 | // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate |
Bruno Cardoso Lopes | 29729919 | 2017-12-22 02:53:30 +0000 | [diff] [blame] | 220 | // should also rename to Foo_Private. Representing private as submodules |
| 221 | // could force building unwanted dependencies into the parent module and cause |
| 222 | // dependency cycles. |
| 223 | if (!Module && SearchName.consume_back("_Private")) |
| 224 | Module = lookupModule(ModuleName, SearchName); |
Graydon Hoare | 4d86764 | 2016-12-21 00:24:39 +0000 | [diff] [blame] | 225 | if (!Module && SearchName.consume_back("Private")) |
| 226 | Module = lookupModule(ModuleName, SearchName); |
| 227 | return Module; |
| 228 | } |
| 229 | |
| 230 | Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName) { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 231 | llvm::NamedRegionTimer T("lookupmodule", "Lookup Module", IncGroupName, |
| 232 | IncGroupDescription, llvm::TimePassesIsEnabled); |
Graydon Hoare | 4d86764 | 2016-12-21 00:24:39 +0000 | [diff] [blame] | 233 | Module *Module = nullptr; |
| 234 | |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 235 | // Look through the various header search paths to load any available module |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 236 | // maps, searching for a module map that describes this module. |
| 237 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 238 | if (SearchDirs[Idx].isFramework()) { |
Graydon Hoare | 4d86764 | 2016-12-21 00:24:39 +0000 | [diff] [blame] | 239 | // Search for or infer a module map for a framework. Here we use |
| 240 | // SearchName rather than ModuleName, to permit finding private modules |
| 241 | // named FooPrivate in buggy frameworks named Foo. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 242 | SmallString<128> FrameworkDirName; |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 243 | FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName(); |
Graydon Hoare | 4d86764 | 2016-12-21 00:24:39 +0000 | [diff] [blame] | 244 | llvm::sys::path::append(FrameworkDirName, SearchName + ".framework"); |
| 245 | if (const DirectoryEntry *FrameworkDir |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 246 | = FileMgr.getDirectory(FrameworkDirName)) { |
| 247 | bool IsSystem |
| 248 | = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User; |
| 249 | Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem); |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 250 | if (Module) |
| 251 | break; |
| 252 | } |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // FIXME: Figure out how header maps and module maps will work together. |
| 256 | |
| 257 | // Only deal with normal search directories. |
| 258 | if (!SearchDirs[Idx].isNormalDir()) |
| 259 | continue; |
Douglas Gregor | 963c553 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 260 | |
| 261 | bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 262 | // Search for a module map file in this directory. |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 263 | if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem, |
| 264 | /*IsFramework*/false) == LMM_NewlyLoaded) { |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 265 | // We just loaded a module map file; check whether the module is |
| 266 | // available now. |
| 267 | Module = ModMap.findModule(ModuleName); |
| 268 | if (Module) |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | // Search for a module map in a subdirectory with the same name as the |
| 273 | // module. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 274 | SmallString<128> NestedModuleMapDirName; |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 275 | NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); |
| 276 | llvm::sys::path::append(NestedModuleMapDirName, ModuleName); |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 277 | if (loadModuleMapFile(NestedModuleMapDirName, IsSystem, |
| 278 | /*IsFramework*/false) == LMM_NewlyLoaded){ |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 279 | // If we just loaded a module map file, look for the module again. |
| 280 | Module = ModMap.findModule(ModuleName); |
| 281 | if (Module) |
| 282 | break; |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 283 | } |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 284 | |
| 285 | // If we've already performed the exhaustive search for module maps in this |
| 286 | // search directory, don't do it again. |
| 287 | if (SearchDirs[Idx].haveSearchedAllModuleMaps()) |
| 288 | continue; |
| 289 | |
| 290 | // Load all module maps in the immediate subdirectories of this search |
| 291 | // directory. |
| 292 | loadSubdirectoryModuleMaps(SearchDirs[Idx]); |
| 293 | |
| 294 | // Look again for the module. |
| 295 | Module = ModMap.findModule(ModuleName); |
| 296 | if (Module) |
| 297 | break; |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 298 | } |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 300 | return Module; |
Douglas Gregor | 1e44e02 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 303 | //===----------------------------------------------------------------------===// |
| 304 | // File lookup within a DirectoryLookup scope |
| 305 | //===----------------------------------------------------------------------===// |
| 306 | |
Chris Lattner | 8d720d0 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 307 | /// getName - Return the directory or filename corresponding to this lookup |
| 308 | /// object. |
Mehdi Amini | 99d1b29 | 2016-10-01 16:38:28 +0000 | [diff] [blame] | 309 | StringRef DirectoryLookup::getName() const { |
Chris Lattner | 8d720d0 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 310 | if (isNormalDir()) |
| 311 | return getDir()->getName(); |
| 312 | if (isFramework()) |
| 313 | return getFrameworkDir()->getName(); |
| 314 | assert(isHeaderMap() && "Unknown DirectoryLookup"); |
| 315 | return getHeaderMap()->getFileName(); |
| 316 | } |
| 317 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 318 | const FileEntry *HeaderSearch::getFileAndSuggestModule( |
Taewook Oh | f42103c | 2016-06-13 20:40:21 +0000 | [diff] [blame] | 319 | StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir, |
| 320 | bool IsSystemHeaderDir, Module *RequestingModule, |
| 321 | ModuleMap::KnownHeader *SuggestedModule) { |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 322 | // If we have a module map that might map this header, load it and |
| 323 | // check whether we'll have a suggestion for a module. |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 324 | const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true); |
Reid Kleckner | afb9aae | 2015-10-20 18:45:57 +0000 | [diff] [blame] | 325 | if (!File) |
| 326 | return nullptr; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 327 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 328 | // If there is a module that corresponds to this header, suggest it. |
| 329 | if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(), |
| 330 | RequestingModule, SuggestedModule, |
| 331 | IsSystemHeaderDir)) |
| 332 | return nullptr; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 333 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 334 | return File; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 335 | } |
Chris Lattner | 8d720d0 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 336 | |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 337 | /// LookupFile - Lookup the specified file in this search path, returning it |
| 338 | /// if it exists or returning null if not. |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 339 | const FileEntry *DirectoryLookup::LookupFile( |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 340 | StringRef &Filename, |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 341 | HeaderSearch &HS, |
Taewook Oh | f42103c | 2016-06-13 20:40:21 +0000 | [diff] [blame] | 342 | SourceLocation IncludeLoc, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 343 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | 97eec24 | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 344 | SmallVectorImpl<char> *RelativePath, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 345 | Module *RequestingModule, |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 346 | ModuleMap::KnownHeader *SuggestedModule, |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 347 | bool &InUserSpecifiedSystemFramework, |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 348 | bool &HasBeenMapped, |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 349 | SmallVectorImpl<char> &MappedName) const { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 350 | llvm::NamedRegionTimer T("lookupfile", "Lookup File", IncGroupName, |
| 351 | IncGroupDescription, llvm::TimePassesIsEnabled); |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 352 | InUserSpecifiedSystemFramework = false; |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 353 | HasBeenMapped = false; |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 354 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 355 | SmallString<1024> TmpDir; |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 356 | if (isNormalDir()) { |
| 357 | // Concatenate the requested file onto the directory. |
Eli Friedman | f7ca26a | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 358 | TmpDir = getDir()->getName(); |
| 359 | llvm::sys::path::append(TmpDir, Filename); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 360 | if (SearchPath) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 361 | StringRef SearchPathRef(getDir()->getName()); |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 362 | SearchPath->clear(); |
| 363 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 364 | } |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 365 | if (RelativePath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 366 | RelativePath->clear(); |
| 367 | RelativePath->append(Filename.begin(), Filename.end()); |
| 368 | } |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 369 | |
Taewook Oh | f42103c | 2016-06-13 20:40:21 +0000 | [diff] [blame] | 370 | return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(), |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 371 | isSystemHeaderDirectory(), |
| 372 | RequestingModule, SuggestedModule); |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 373 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 375 | if (isFramework()) |
Douglas Gregor | 97eec24 | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 376 | return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 377 | RequestingModule, SuggestedModule, |
| 378 | InUserSpecifiedSystemFramework); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 44bd21b | 2007-12-17 08:17:39 +0000 | [diff] [blame] | 380 | assert(isHeaderMap() && "Unknown directory lookup"); |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 381 | const HeaderMap *HM = getHeaderMap(); |
| 382 | SmallString<1024> Path; |
| 383 | StringRef Dest = HM->lookupFilename(Filename, Path); |
| 384 | if (Dest.empty()) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 385 | return nullptr; |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 386 | |
| 387 | const FileEntry *Result; |
| 388 | |
| 389 | // Check if the headermap maps the filename to a framework include |
| 390 | // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the |
| 391 | // framework include. |
| 392 | if (llvm::sys::path::is_relative(Dest)) { |
| 393 | MappedName.clear(); |
| 394 | MappedName.append(Dest.begin(), Dest.end()); |
| 395 | Filename = StringRef(MappedName.begin(), MappedName.size()); |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 396 | HasBeenMapped = true; |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 397 | Result = HM->LookupFile(Filename, HS.getFileMgr()); |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 398 | } else { |
| 399 | Result = HS.getFileMgr().getFile(Dest); |
| 400 | } |
| 401 | |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 402 | if (Result) { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 403 | if (SearchPath) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 404 | StringRef SearchPathRef(getName()); |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 405 | SearchPath->clear(); |
| 406 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 407 | } |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 408 | if (RelativePath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 409 | RelativePath->clear(); |
| 410 | RelativePath->append(Filename.begin(), Filename.end()); |
| 411 | } |
| 412 | } |
| 413 | return Result; |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 416 | /// \brief Given a framework directory, find the top-most framework directory. |
| 417 | /// |
| 418 | /// \param FileMgr The file manager to use for directory lookups. |
| 419 | /// \param DirName The name of the framework directory. |
| 420 | /// \param SubmodulePath Will be populated with the submodule path from the |
| 421 | /// returned top-level module to the originally named framework. |
| 422 | static const DirectoryEntry * |
| 423 | getTopFrameworkDir(FileManager &FileMgr, StringRef DirName, |
| 424 | SmallVectorImpl<std::string> &SubmodulePath) { |
| 425 | assert(llvm::sys::path::extension(DirName) == ".framework" && |
| 426 | "Not a framework directory"); |
| 427 | |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 428 | // Note: as an egregious but useful hack we use the real path here, because |
| 429 | // frameworks moving between top-level frameworks to embedded frameworks tend |
| 430 | // to be symlinked, and we base the logical structure of modules on the |
| 431 | // physical layout. In particular, we need to deal with crazy includes like |
| 432 | // |
| 433 | // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h> |
| 434 | // |
| 435 | // where 'Bar' used to be embedded in 'Foo', is now a top-level framework |
| 436 | // which one should access with, e.g., |
| 437 | // |
| 438 | // #include <Bar/Wibble.h> |
| 439 | // |
| 440 | // Similar issues occur when a top-level framework has moved into an |
| 441 | // embedded framework. |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 442 | const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName); |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 443 | DirName = FileMgr.getCanonicalName(TopFrameworkDir); |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 444 | do { |
| 445 | // Get the parent directory name. |
| 446 | DirName = llvm::sys::path::parent_path(DirName); |
| 447 | if (DirName.empty()) |
| 448 | break; |
| 449 | |
| 450 | // Determine whether this directory exists. |
| 451 | const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); |
| 452 | if (!Dir) |
| 453 | break; |
| 454 | |
| 455 | // If this is a framework directory, then we're a subframework of this |
| 456 | // framework. |
| 457 | if (llvm::sys::path::extension(DirName) == ".framework") { |
| 458 | SubmodulePath.push_back(llvm::sys::path::stem(DirName)); |
| 459 | TopFrameworkDir = Dir; |
| 460 | } |
| 461 | } while (true); |
| 462 | |
| 463 | return TopFrameworkDir; |
| 464 | } |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 465 | |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 466 | static bool needModuleLookup(Module *RequestingModule, |
| 467 | bool HasSuggestedModule) { |
| 468 | return HasSuggestedModule || |
| 469 | (RequestingModule && RequestingModule->NoUndeclaredIncludes); |
| 470 | } |
| 471 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 472 | /// DoFrameworkLookup - Do a lookup of the specified file in the current |
| 473 | /// DirectoryLookup, which is a framework directory. |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 474 | const FileEntry *DirectoryLookup::DoFrameworkLookup( |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 475 | StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath, |
| 476 | SmallVectorImpl<char> *RelativePath, Module *RequestingModule, |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 477 | ModuleMap::KnownHeader *SuggestedModule, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 478 | bool &InUserSpecifiedSystemFramework) const { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 479 | llvm::NamedRegionTimer T("lookupfw", "Lookup Framework", IncGroupName, |
| 480 | IncGroupDescription, llvm::TimePassesIsEnabled); |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 481 | FileManager &FileMgr = HS.getFileMgr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 483 | // Framework names must have a '/' in the filename. |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 484 | size_t SlashPos = Filename.find('/'); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 485 | if (SlashPos == StringRef::npos) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 487 | // Find out if this is the home for the specified framework, by checking |
Daniel Dunbar | 1713861 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 488 | // HeaderSearch. Possible answers are yes/no and unknown. |
| 489 | HeaderSearch::FrameworkCacheEntry &CacheEntry = |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 490 | HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 492 | // If it is known and in some other directory, fail. |
Daniel Dunbar | 1713861 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 493 | if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir()) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 494 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 496 | // Otherwise, construct the path to this framework dir. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 498 | // FrameworkName = "/System/Library/Frameworks/" |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 499 | SmallString<1024> FrameworkName; |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 500 | FrameworkName += getFrameworkDir()->getName(); |
Chris Lattner | b201d9b | 2006-10-30 05:09:49 +0000 | [diff] [blame] | 501 | if (FrameworkName.empty() || FrameworkName.back() != '/') |
| 502 | FrameworkName.push_back('/'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 504 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 505 | StringRef ModuleName(Filename.begin(), SlashPos); |
| 506 | FrameworkName += ModuleName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 508 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 509 | FrameworkName += ".framework/"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Daniel Dunbar | 1713861 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 511 | // If the cache entry was unresolved, populate it now. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 512 | if (!CacheEntry.Directory) { |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 513 | HS.IncrementFrameworkLookupCount(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 515 | // If the framework dir doesn't exist, we fail. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 516 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 517 | if (!Dir) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 519 | // Otherwise, if it does, remember that this is the right direntry for this |
| 520 | // framework. |
Daniel Dunbar | 1713861 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 521 | CacheEntry.Directory = getFrameworkDir(); |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 522 | |
| 523 | // If this is a user search directory, check if the framework has been |
| 524 | // user-specified as a system framework. |
| 525 | if (getDirCharacteristic() == SrcMgr::C_User) { |
| 526 | SmallString<1024> SystemFrameworkMarker(FrameworkName); |
| 527 | SystemFrameworkMarker += ".system_framework"; |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 528 | if (llvm::sys::fs::exists(SystemFrameworkMarker)) { |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 529 | CacheEntry.IsUserSpecifiedSystemFramework = true; |
| 530 | } |
| 531 | } |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 532 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 534 | // Set the 'user-specified system framework' flag. |
| 535 | InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework; |
| 536 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 537 | if (RelativePath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 538 | RelativePath->clear(); |
| 539 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 540 | } |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 542 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
Chris Lattner | b201d9b | 2006-10-30 05:09:49 +0000 | [diff] [blame] | 543 | unsigned OrigSize = FrameworkName.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Chris Lattner | b201d9b | 2006-10-30 05:09:49 +0000 | [diff] [blame] | 545 | FrameworkName += "Headers/"; |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 546 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 547 | if (SearchPath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 548 | SearchPath->clear(); |
| 549 | // Without trailing '/'. |
| 550 | SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); |
| 551 | } |
| 552 | |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 553 | FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 554 | const FileEntry *FE = FileMgr.getFile(FrameworkName, |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 555 | /*openFile=*/!SuggestedModule); |
| 556 | if (!FE) { |
| 557 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 558 | const char *Private = "Private"; |
| 559 | FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, |
| 560 | Private+strlen(Private)); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 561 | if (SearchPath) |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 562 | SearchPath->insert(SearchPath->begin()+OrigSize, Private, |
| 563 | Private+strlen(Private)); |
| 564 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 565 | FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule); |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 568 | // If we found the header and are allowed to suggest a module, do so now. |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 569 | if (FE && needModuleLookup(RequestingModule, SuggestedModule)) { |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 570 | // Find the framework in which this header occurs. |
Ben Langmuir | ef914b8 | 2014-05-15 16:20:33 +0000 | [diff] [blame] | 571 | StringRef FrameworkPath = FE->getDir()->getName(); |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 572 | bool FoundFramework = false; |
| 573 | do { |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 574 | // Determine whether this directory exists. |
| 575 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath); |
| 576 | if (!Dir) |
| 577 | break; |
| 578 | |
| 579 | // If this is a framework directory, then we're a subframework of this |
| 580 | // framework. |
| 581 | if (llvm::sys::path::extension(FrameworkPath) == ".framework") { |
| 582 | FoundFramework = true; |
| 583 | break; |
| 584 | } |
Ben Langmuir | ef914b8 | 2014-05-15 16:20:33 +0000 | [diff] [blame] | 585 | |
| 586 | // Get the parent directory name. |
| 587 | FrameworkPath = llvm::sys::path::parent_path(FrameworkPath); |
| 588 | if (FrameworkPath.empty()) |
| 589 | break; |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 590 | } while (true); |
| 591 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 592 | bool IsSystem = getDirCharacteristic() != SrcMgr::C_User; |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 593 | if (FoundFramework) { |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 594 | if (!HS.findUsableModuleForFrameworkHeader( |
| 595 | FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem)) |
| 596 | return nullptr; |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 597 | } else { |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 598 | if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule, |
| 599 | SuggestedModule, IsSystem)) |
| 600 | return nullptr; |
Douglas Gregor | 4ddf222 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
Douglas Gregor | 97eec24 | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 603 | return FE; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 606 | void HeaderSearch::setTarget(const TargetInfo &Target) { |
| 607 | ModMap.setTarget(Target); |
| 608 | } |
| 609 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 610 | //===----------------------------------------------------------------------===// |
| 611 | // Header File Location. |
| 612 | //===----------------------------------------------------------------------===// |
| 613 | |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 614 | /// \brief Return true with a diagnostic if the file that MSVC would have found |
| 615 | /// fails to match the one that Clang would have found with MSVC header search |
| 616 | /// disabled. |
| 617 | static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags, |
| 618 | const FileEntry *MSFE, const FileEntry *FE, |
| 619 | SourceLocation IncludeLoc) { |
| 620 | if (MSFE && FE != MSFE) { |
| 621 | Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName(); |
| 622 | return true; |
| 623 | } |
| 624 | return false; |
| 625 | } |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 626 | |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 627 | static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) { |
| 628 | assert(!Str.empty()); |
| 629 | char *CopyStr = Alloc.Allocate<char>(Str.size()+1); |
| 630 | std::copy(Str.begin(), Str.end(), CopyStr); |
| 631 | CopyStr[Str.size()] = '\0'; |
| 632 | return CopyStr; |
| 633 | } |
| 634 | |
James Dennett | c07ab2c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 635 | /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file, |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 636 | /// return null on failure. isAngled indicates whether the file reference is |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 637 | /// for system \#include's or not (i.e. using <> instead of ""). Includers, if |
| 638 | /// non-empty, indicates where the \#including file(s) are, in case a relative |
| 639 | /// search is needed. Microsoft mode will pass all \#including files. |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 640 | const FileEntry *HeaderSearch::LookupFile( |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 641 | StringRef Filename, SourceLocation IncludeLoc, bool isAngled, |
| 642 | const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir, |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 643 | ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers, |
| 644 | SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 645 | Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, |
Duncan P. N. Exon Smith | cfc1f6a | 2017-04-27 21:41:51 +0000 | [diff] [blame] | 646 | bool *IsMapped, bool SkipCache, bool BuildSystemModule) { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 647 | llvm::NamedRegionTimer T("lookupfile2", "Lookup File2", IncGroupName, |
| 648 | IncGroupDescription, llvm::TimePassesIsEnabled); |
Duncan P. N. Exon Smith | cfc1f6a | 2017-04-27 21:41:51 +0000 | [diff] [blame] | 649 | if (IsMapped) |
| 650 | *IsMapped = false; |
| 651 | |
Douglas Gregor | 97eec24 | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 652 | if (SuggestedModule) |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 653 | *SuggestedModule = ModuleMap::KnownHeader(); |
Douglas Gregor | 97eec24 | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 655 | // If 'Filename' is absolute, check to see if it exists and no searching. |
Michael J. Spencer | f28df4c | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 656 | if (llvm::sys::path::is_absolute(Filename)) { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 657 | CurDir = nullptr; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 658 | |
| 659 | // If this was an #include_next "/absolute/file", fail. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 660 | if (FromDir) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 662 | if (SearchPath) |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 663 | SearchPath->clear(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 664 | if (RelativePath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 665 | RelativePath->clear(); |
| 666 | RelativePath->append(Filename.begin(), Filename.end()); |
| 667 | } |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 668 | // Otherwise, just return the file. |
Taewook Oh | f42103c | 2016-06-13 20:40:21 +0000 | [diff] [blame] | 669 | return getFileAndSuggestModule(Filename, IncludeLoc, nullptr, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 670 | /*IsSystemHeaderDir*/false, |
| 671 | RequestingModule, SuggestedModule); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 672 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 674 | // This is the header that MSVC's header search would have found. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 675 | const FileEntry *MSFE = nullptr; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 676 | ModuleMap::KnownHeader MSSuggestedModule; |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 9f93e38 | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 678 | // Unless disabled, check to see if the file is in the #includer's |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 679 | // directory. This cannot be based on CurDir, because each includer could be |
| 680 | // a #include of a subdirectory (#include "foo/bar.h") and a subsequent |
| 681 | // include of "baz.h" should resolve to "whatever/foo/baz.h". |
Chris Lattner | f62f758 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 682 | // This search is not done for <> headers. |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 683 | if (!Includers.empty() && !isAngled && !NoCurDirSearch) { |
NAKAMURA Takumi | 9cb6264 | 2013-12-10 02:36:28 +0000 | [diff] [blame] | 684 | SmallString<1024> TmpDir; |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 685 | bool First = true; |
| 686 | for (const auto &IncluderAndDir : Includers) { |
| 687 | const FileEntry *Includer = IncluderAndDir.first; |
| 688 | |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 689 | // Concatenate the requested file onto the directory. |
Nikola Smiljanic | cf385dc | 2015-05-08 06:02:37 +0000 | [diff] [blame] | 690 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 691 | TmpDir = IncluderAndDir.second->getName(); |
Nikola Smiljanic | cf385dc | 2015-05-08 06:02:37 +0000 | [diff] [blame] | 692 | TmpDir.push_back('/'); |
| 693 | TmpDir.append(Filename.begin(), Filename.end()); |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 694 | |
Richard Smith | 6f548ec | 2014-03-06 18:08:08 +0000 | [diff] [blame] | 695 | // FIXME: We don't cache the result of getFileInfo across the call to |
| 696 | // getFileAndSuggestModule, because it's a reference to an element of |
| 697 | // a container that could be reallocated across this call. |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 698 | // |
Manman Ren | e4a5d37 | 2016-05-17 02:15:12 +0000 | [diff] [blame] | 699 | // If we have no includer, that means we're processing a #include |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 700 | // from a module build. We should treat this as a system header if we're |
| 701 | // building a [system] module. |
Richard Smith | 6f548ec | 2014-03-06 18:08:08 +0000 | [diff] [blame] | 702 | bool IncluderIsSystemHeader = |
Manman Ren | e39c814 | 2016-05-17 18:04:38 +0000 | [diff] [blame] | 703 | Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User : |
| 704 | BuildSystemModule; |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 705 | if (const FileEntry *FE = getFileAndSuggestModule( |
Taewook Oh | f42103c | 2016-06-13 20:40:21 +0000 | [diff] [blame] | 706 | TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 707 | RequestingModule, SuggestedModule)) { |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 708 | if (!Includer) { |
| 709 | assert(First && "only first includer can have no file"); |
| 710 | return FE; |
| 711 | } |
| 712 | |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 713 | // Leave CurDir unset. |
| 714 | // This file is a system header or C++ unfriendly if the old file is. |
| 715 | // |
| 716 | // Note that we only use one of FromHFI/ToHFI at once, due to potential |
| 717 | // reallocation of the underlying vector potentially making the first |
| 718 | // reference binding dangling. |
Richard Smith | 6f548ec | 2014-03-06 18:08:08 +0000 | [diff] [blame] | 719 | HeaderFileInfo &FromHFI = getFileInfo(Includer); |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 720 | unsigned DirInfo = FromHFI.DirInfo; |
| 721 | bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader; |
| 722 | StringRef Framework = FromHFI.Framework; |
Douglas Gregor | 03b5ebe | 2012-08-13 15:47:39 +0000 | [diff] [blame] | 723 | |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 724 | HeaderFileInfo &ToHFI = getFileInfo(FE); |
| 725 | ToHFI.DirInfo = DirInfo; |
| 726 | ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader; |
| 727 | ToHFI.Framework = Framework; |
Douglas Gregor | 03b5ebe | 2012-08-13 15:47:39 +0000 | [diff] [blame] | 728 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 729 | if (SearchPath) { |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 730 | StringRef SearchPathRef(IncluderAndDir.second->getName()); |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 731 | SearchPath->clear(); |
| 732 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 733 | } |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 734 | if (RelativePath) { |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 735 | RelativePath->clear(); |
| 736 | RelativePath->append(Filename.begin(), Filename.end()); |
| 737 | } |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 738 | if (First) |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 739 | return FE; |
| 740 | |
| 741 | // Otherwise, we found the path via MSVC header search rules. If |
| 742 | // -Wmsvc-include is enabled, we have to keep searching to see if we |
| 743 | // would've found this header in -I or -isystem directories. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 744 | if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) { |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 745 | return FE; |
| 746 | } else { |
| 747 | MSFE = FE; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 748 | if (SuggestedModule) { |
| 749 | MSSuggestedModule = *SuggestedModule; |
| 750 | *SuggestedModule = ModuleMap::KnownHeader(); |
| 751 | } |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 752 | break; |
| 753 | } |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 754 | } |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 755 | First = false; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 756 | } |
| 757 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 759 | CurDir = nullptr; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 760 | |
| 761 | // If this is a system #include, ignore the user #include locs. |
Nico Weber | 3b1d121 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 762 | unsigned i = isAngled ? AngledDirIdx : 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 764 | // If this is a #include_next request, start searching after the directory the |
| 765 | // file was found in. |
| 766 | if (FromDir) |
| 767 | i = FromDir-&SearchDirs[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Chris Lattner | d427542 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 769 | // Cache all of the lookups performed by this method. Many headers are |
| 770 | // multiply included, and the "pragma once" optimization prevents them from |
| 771 | // being relex/pp'd, but they would still have to search through a |
| 772 | // (potentially huge) series of SearchDirs to find it. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 773 | LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename]; |
Chris Lattner | d427542 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 774 | |
| 775 | // If the entry has been previously looked up, the first value will be |
| 776 | // non-zero. If the value is equal to i (the start point of our search), then |
| 777 | // this is a matching hit. |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 778 | if (!SkipCache && CacheLookup.StartIdx == i+1) { |
Chris Lattner | d427542 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 779 | // Skip querying potentially lots of directories for this lookup. |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 780 | i = CacheLookup.HitIdx; |
Duncan P. N. Exon Smith | cfc1f6a | 2017-04-27 21:41:51 +0000 | [diff] [blame] | 781 | if (CacheLookup.MappedName) { |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 782 | Filename = CacheLookup.MappedName; |
Duncan P. N. Exon Smith | cfc1f6a | 2017-04-27 21:41:51 +0000 | [diff] [blame] | 783 | if (IsMapped) |
| 784 | *IsMapped = true; |
| 785 | } |
Chris Lattner | d427542 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 786 | } else { |
| 787 | // Otherwise, this is the first query, or the previous query didn't match |
| 788 | // our search start. We will fill in our found location below, so prime the |
| 789 | // start point value. |
Argyrios Kyrtzidis | 7bd78a9 | 2014-03-29 03:22:54 +0000 | [diff] [blame] | 790 | CacheLookup.reset(/*StartIdx=*/i+1); |
Chris Lattner | d427542 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 791 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Argyrios Kyrtzidis | 75fa9ed | 2014-02-14 14:58:28 +0000 | [diff] [blame] | 793 | SmallString<64> MappedName; |
| 794 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 795 | // Check each directory in sequence to see if it contains this file. |
| 796 | for (; i != SearchDirs.size(); ++i) { |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 797 | bool InUserSpecifiedSystemFramework = false; |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 798 | bool HasBeenMapped = false; |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 799 | const FileEntry *FE = SearchDirs[i].LookupFile( |
Taewook Oh | f42103c | 2016-06-13 20:40:21 +0000 | [diff] [blame] | 800 | Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 801 | SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped, |
| 802 | MappedName); |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 803 | if (HasBeenMapped) { |
| 804 | CacheLookup.MappedName = |
| 805 | copyString(Filename, LookupFileCache.getAllocator()); |
Duncan P. N. Exon Smith | cfc1f6a | 2017-04-27 21:41:51 +0000 | [diff] [blame] | 806 | if (IsMapped) |
| 807 | *IsMapped = true; |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 808 | } |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 809 | if (!FE) continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 811 | CurDir = &SearchDirs[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 813 | // This file is a system header or C++ unfriendly if the dir is. |
Douglas Gregor | 9f93e38 | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 814 | HeaderFileInfo &HFI = getFileInfo(FE); |
| 815 | HFI.DirInfo = CurDir->getDirCharacteristic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Daniel Dunbar | 3c9bc4d | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 817 | // If the directory characteristic is User but this framework was |
| 818 | // user-specified to be treated as a system framework, promote the |
| 819 | // characteristic. |
| 820 | if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework) |
| 821 | HFI.DirInfo = SrcMgr::C_System; |
| 822 | |
Richard Smith | 8acadcb | 2012-06-13 20:27:03 +0000 | [diff] [blame] | 823 | // If the filename matches a known system header prefix, override |
| 824 | // whether the file is a system header. |
Richard Trieu | 871f5f3 | 2012-06-13 20:52:36 +0000 | [diff] [blame] | 825 | for (unsigned j = SystemHeaderPrefixes.size(); j; --j) { |
| 826 | if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) { |
| 827 | HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System |
Richard Smith | 8acadcb | 2012-06-13 20:27:03 +0000 | [diff] [blame] | 828 | : SrcMgr::C_User; |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
Douglas Gregor | 9f93e38 | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 833 | // If this file is found in a header map and uses the framework style of |
| 834 | // includes, then this header is part of a framework we're building. |
| 835 | if (CurDir->isIndexHeaderMap()) { |
| 836 | size_t SlashPos = Filename.find('/'); |
| 837 | if (SlashPos != StringRef::npos) { |
| 838 | HFI.IndexHeaderMapHeader = 1; |
| 839 | HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), |
| 840 | SlashPos)); |
| 841 | } |
| 842 | } |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 843 | |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 844 | if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) { |
| 845 | if (SuggestedModule) |
| 846 | *SuggestedModule = MSSuggestedModule; |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 847 | return MSFE; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 848 | } |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 850 | // Remember this location for the next lookup we do. |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 851 | CacheLookup.HitIdx = i; |
Chris Lattner | 712e387 | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 852 | return FE; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Douglas Gregor | d8575e1 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 855 | // If we are including a file with a quoted include "foo.h" from inside |
| 856 | // a header in a framework that is currently being built, and we couldn't |
| 857 | // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where |
| 858 | // "Foo" is the name of the framework in which the including header was found. |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 859 | if (!Includers.empty() && Includers.front().first && !isAngled && |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 860 | Filename.find('/') == StringRef::npos) { |
Manuel Klimek | 9af34ae | 2014-08-12 08:25:57 +0000 | [diff] [blame] | 861 | HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first); |
Douglas Gregor | d8575e1 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 862 | if (IncludingHFI.IndexHeaderMapHeader) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 863 | SmallString<128> ScratchFilename; |
Douglas Gregor | d8575e1 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 864 | ScratchFilename += IncludingHFI.Framework; |
| 865 | ScratchFilename += '/'; |
| 866 | ScratchFilename += Filename; |
Will Wilson | 0fafd34 | 2013-12-27 19:46:16 +0000 | [diff] [blame] | 867 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 868 | const FileEntry *FE = |
| 869 | LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, |
| 870 | CurDir, Includers.front(), SearchPath, RelativePath, |
Duncan P. N. Exon Smith | cfc1f6a | 2017-04-27 21:41:51 +0000 | [diff] [blame] | 871 | RequestingModule, SuggestedModule, IsMapped); |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 872 | |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 873 | if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) { |
| 874 | if (SuggestedModule) |
| 875 | *SuggestedModule = MSSuggestedModule; |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 876 | return MSFE; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 877 | } |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 878 | |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 879 | LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename]; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 880 | CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 881 | // FIXME: SuggestedModule. |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 882 | return FE; |
Douglas Gregor | d8575e1 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 886 | if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) { |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 887 | if (SuggestedModule) |
| 888 | *SuggestedModule = MSSuggestedModule; |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 889 | return MSFE; |
Richard Smith | 8c71eba | 2014-03-05 20:51:45 +0000 | [diff] [blame] | 890 | } |
Reid Kleckner | a97d4c0 | 2014-02-18 23:49:24 +0000 | [diff] [blame] | 891 | |
Chris Lattner | d427542 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 892 | // Otherwise, didn't find it. Remember we didn't find this. |
Argyrios Kyrtzidis | 34fad42 | 2014-03-11 06:21:28 +0000 | [diff] [blame] | 893 | CacheLookup.HitIdx = SearchDirs.size(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 894 | return nullptr; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 897 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
James Dennett | c07ab2c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 898 | /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 899 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 900 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 901 | /// for the designated file, otherwise return null. |
| 902 | const FileEntry *HeaderSearch:: |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 903 | LookupSubframeworkHeader(StringRef Filename, |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 904 | const FileEntry *ContextFileEnt, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 905 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | f5f9452 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 906 | SmallVectorImpl<char> *RelativePath, |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 907 | Module *RequestingModule, |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 908 | ModuleMap::KnownHeader *SuggestedModule) { |
Chris Lattner | 1226188 | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 909 | assert(ContextFileEnt && "No context file?"); |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 910 | llvm::NamedRegionTimer T("lookupsubfm", "Lookup SubFramework", IncGroupName, |
| 911 | IncGroupDescription, llvm::TimePassesIsEnabled); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 913 | // Framework names must have a '/' in the filename. Find it. |
Douglas Gregor | 5ca04bd | 2011-12-09 16:48:01 +0000 | [diff] [blame] | 914 | // FIXME: Should we permit '\' on Windows? |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 915 | size_t SlashPos = Filename.find('/'); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 916 | if (SlashPos == StringRef::npos) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 918 | // Look up the base framework name of the ContextFileEnt. |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 919 | StringRef ContextName = ContextFileEnt->getName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 921 | // If the context info wasn't a framework, couldn't be a subframework. |
Douglas Gregor | 5ca04bd | 2011-12-09 16:48:01 +0000 | [diff] [blame] | 922 | const unsigned DotFrameworkLen = 10; |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 923 | auto FrameworkPos = ContextName.find(".framework"); |
| 924 | if (FrameworkPos == StringRef::npos || |
| 925 | (ContextName[FrameworkPos + DotFrameworkLen] != '/' && |
| 926 | ContextName[FrameworkPos + DotFrameworkLen] != '\\')) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 927 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 929 | SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() + |
| 930 | FrameworkPos + |
| 931 | DotFrameworkLen + 1); |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 933 | // Append Frameworks/HIToolbox.framework/ |
| 934 | FrameworkName += "Frameworks/"; |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 935 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 936 | FrameworkName += ".framework/"; |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 937 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 938 | auto &CacheLookup = |
| 939 | *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos), |
| 940 | FrameworkCacheEntry())).first; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 942 | // Some other location? |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 943 | if (CacheLookup.second.Directory && |
| 944 | CacheLookup.first().size() == FrameworkName.size() && |
| 945 | memcmp(CacheLookup.first().data(), &FrameworkName[0], |
| 946 | CacheLookup.first().size()) != 0) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 947 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 949 | // Cache subframework. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 950 | if (!CacheLookup.second.Directory) { |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 951 | ++NumSubFrameworkLookups; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 953 | // If the framework dir doesn't exist, we fail. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 954 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 955 | if (!Dir) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 957 | // Otherwise, if it does, remember that this is the right direntry for this |
| 958 | // framework. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 959 | CacheLookup.second.Directory = Dir; |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame] | 960 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 962 | const FileEntry *FE = nullptr; |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 963 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 964 | if (RelativePath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 965 | RelativePath->clear(); |
| 966 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 967 | } |
| 968 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 969 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 970 | SmallString<1024> HeadersFilename(FrameworkName); |
Chris Lattner | 43fd42e | 2006-10-30 03:40:58 +0000 | [diff] [blame] | 971 | HeadersFilename += "Headers/"; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 972 | if (SearchPath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 973 | SearchPath->clear(); |
| 974 | // Without trailing '/'. |
| 975 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 976 | } |
| 977 | |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 978 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 979 | if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) { |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 980 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
Chris Lattner | 43fd42e | 2006-10-30 03:40:58 +0000 | [diff] [blame] | 981 | HeadersFilename = FrameworkName; |
| 982 | HeadersFilename += "PrivateHeaders/"; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 983 | if (SearchPath) { |
Manuel Klimek | 0c69fd2 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 984 | SearchPath->clear(); |
| 985 | // Without trailing '/'. |
| 986 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 987 | } |
| 988 | |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 989 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 990 | if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 991 | return nullptr; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 992 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 994 | // This file is a system header or C++ unfriendly if the old file is. |
Ted Kremenek | 72be068 | 2008-02-24 03:55:14 +0000 | [diff] [blame] | 995 | // |
Chris Lattner | f5c619f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 996 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 997 | // getFileInfo could resize the vector and we don't want to rely on order |
| 998 | // of evaluation. |
| 999 | unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 1000 | getFileInfo(FE).DirInfo = DirInfo; |
Douglas Gregor | f5f9452 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 1001 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 1002 | FrameworkName.pop_back(); // remove the trailing '/' |
| 1003 | if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule, |
| 1004 | SuggestedModule, /*IsSystem*/ false)) |
| 1005 | return nullptr; |
Douglas Gregor | f5f9452 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 1007 | return FE; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1010 | //===----------------------------------------------------------------------===// |
| 1011 | // File Info Management. |
| 1012 | //===----------------------------------------------------------------------===// |
| 1013 | |
Douglas Gregor | 5d1bee2 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 1014 | /// \brief Merge the header file info provided by \p OtherHFI into the current |
| 1015 | /// header file info (\p HFI) |
| 1016 | static void mergeHeaderFileInfo(HeaderFileInfo &HFI, |
| 1017 | const HeaderFileInfo &OtherHFI) { |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1018 | assert(OtherHFI.External && "expected to merge external HFI"); |
| 1019 | |
Douglas Gregor | 5d1bee2 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 1020 | HFI.isImport |= OtherHFI.isImport; |
| 1021 | HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1022 | HFI.isModuleHeader |= OtherHFI.isModuleHeader; |
Douglas Gregor | 5d1bee2 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 1023 | HFI.NumIncludes += OtherHFI.NumIncludes; |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | 5d1bee2 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 1025 | if (!HFI.ControllingMacro && !HFI.ControllingMacroID) { |
| 1026 | HFI.ControllingMacro = OtherHFI.ControllingMacro; |
| 1027 | HFI.ControllingMacroID = OtherHFI.ControllingMacroID; |
| 1028 | } |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1029 | |
| 1030 | HFI.DirInfo = OtherHFI.DirInfo; |
| 1031 | HFI.External = (!HFI.IsValid || HFI.External); |
| 1032 | HFI.IsValid = true; |
| 1033 | HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | 5d1bee2 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 1035 | if (HFI.Framework.empty()) |
| 1036 | HFI.Framework = OtherHFI.Framework; |
Douglas Gregor | 5d1bee2 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 1039 | /// getFileInfo - Return the HeaderFileInfo structure for the specified |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1040 | /// FileEntry. |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 1041 | HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1042 | if (FE->getUID() >= FileInfo.size()) |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1043 | FileInfo.resize(FE->getUID() + 1); |
| 1044 | |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1045 | HeaderFileInfo *HFI = &FileInfo[FE->getUID()]; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1046 | // FIXME: Use a generation count to check whether this is really up to date. |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1047 | if (ExternalSource && !HFI->Resolved) { |
| 1048 | HFI->Resolved = true; |
| 1049 | auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE); |
| 1050 | |
| 1051 | HFI = &FileInfo[FE->getUID()]; |
| 1052 | if (ExternalHFI.External) |
| 1053 | mergeHeaderFileInfo(*HFI, ExternalHFI); |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1056 | HFI->IsValid = true; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1057 | // We have local information about this header file, so it's no longer |
| 1058 | // strictly external. |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1059 | HFI->External = false; |
| 1060 | return *HFI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | } |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1062 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1063 | const HeaderFileInfo * |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1064 | HeaderSearch::getExistingFileInfo(const FileEntry *FE, |
| 1065 | bool WantExternal) const { |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1066 | // If we have an external source, ensure we have the latest information. |
| 1067 | // FIXME: Use a generation count to check whether this is really up to date. |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1068 | HeaderFileInfo *HFI; |
| 1069 | if (ExternalSource) { |
| 1070 | if (FE->getUID() >= FileInfo.size()) { |
| 1071 | if (!WantExternal) |
| 1072 | return nullptr; |
| 1073 | FileInfo.resize(FE->getUID() + 1); |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1074 | } |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1075 | |
| 1076 | HFI = &FileInfo[FE->getUID()]; |
| 1077 | if (!WantExternal && (!HFI->IsValid || HFI->External)) |
| 1078 | return nullptr; |
| 1079 | if (!HFI->Resolved) { |
| 1080 | HFI->Resolved = true; |
| 1081 | auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE); |
| 1082 | |
| 1083 | HFI = &FileInfo[FE->getUID()]; |
| 1084 | if (ExternalHFI.External) |
| 1085 | mergeHeaderFileInfo(*HFI, ExternalHFI); |
| 1086 | } |
| 1087 | } else if (FE->getUID() >= FileInfo.size()) { |
| 1088 | return nullptr; |
| 1089 | } else { |
| 1090 | HFI = &FileInfo[FE->getUID()]; |
Ben Langmuir | d285c50 | 2014-03-13 16:46:36 +0000 | [diff] [blame] | 1091 | } |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1092 | |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1093 | if (!HFI->IsValid || (HFI->External && !WantExternal)) |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1094 | return nullptr; |
| 1095 | |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1096 | return HFI; |
Ben Langmuir | d285c50 | 2014-03-13 16:46:36 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Douglas Gregor | 37aa493 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 1099 | bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { |
| 1100 | // Check if we've ever seen this file as a header. |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1101 | if (auto *HFI = getExistingFileInfo(File)) |
| 1102 | return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro || |
| 1103 | HFI->ControllingMacroID; |
| 1104 | return false; |
Douglas Gregor | 37aa493 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Argyrios Kyrtzidis | 6f722b4 | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 1107 | void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE, |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1108 | ModuleMap::ModuleHeaderRole Role, |
Argyrios Kyrtzidis | 6f722b4 | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 1109 | bool isCompilingModuleHeader) { |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1110 | bool isModularHeader = !(Role & ModuleMap::TextualHeader); |
| 1111 | |
| 1112 | // Don't mark the file info as non-external if there's nothing to change. |
| 1113 | if (!isCompilingModuleHeader) { |
| 1114 | if (!isModularHeader) |
| 1115 | return; |
| 1116 | auto *HFI = getExistingFileInfo(FE); |
| 1117 | if (HFI && HFI->isModuleHeader) |
| 1118 | return; |
| 1119 | } |
| 1120 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1121 | auto &HFI = getFileInfo(FE); |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1122 | HFI.isModuleHeader |= isModularHeader; |
Richard Smith | e70dadd | 2015-07-10 22:27:17 +0000 | [diff] [blame] | 1123 | HFI.isCompilingModuleHeader |= isCompilingModuleHeader; |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1126 | bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP, |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1127 | const FileEntry *File, bool isImport, |
| 1128 | bool ModulesEnabled, Module *M) { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 1129 | llvm::NamedRegionTimer T("shouldenterinc", "Should Enter Include File", |
| 1130 | IncGroupName, IncGroupDescription, |
| 1131 | llvm::TimePassesIsEnabled); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1132 | ++NumIncluded; // Count # of attempted #includes. |
| 1133 | |
| 1134 | // Get information about this file. |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 1135 | HeaderFileInfo &FileInfo = getFileInfo(File); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1137 | // FIXME: this is a workaround for the lack of proper modules-aware support |
| 1138 | // for #import / #pragma once |
Eugene Zelenko | afd1b1c | 2017-12-06 23:18:41 +0000 | [diff] [blame] | 1139 | auto TryEnterImported = [&]() -> bool { |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1140 | if (!ModulesEnabled) |
| 1141 | return false; |
Richard Smith | 040e126 | 2017-06-02 01:55:39 +0000 | [diff] [blame] | 1142 | // Ensure FileInfo bits are up to date. |
| 1143 | ModMap.resolveHeaderDirectives(File); |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1144 | // Modules with builtins are special; multiple modules use builtins as |
| 1145 | // modular headers, example: |
| 1146 | // |
| 1147 | // module stddef { header "stddef.h" export * } |
| 1148 | // |
| 1149 | // After module map parsing, this expands to: |
| 1150 | // |
| 1151 | // module stddef { |
| 1152 | // header "/path_to_builtin_dirs/stddef.h" |
| 1153 | // textual "stddef.h" |
| 1154 | // } |
| 1155 | // |
| 1156 | // It's common that libc++ and system modules will both define such |
| 1157 | // submodules. Make sure cached results for a builtin header won't |
| 1158 | // prevent other builtin modules to potentially enter the builtin header. |
| 1159 | // Note that builtins are header guarded and the decision to actually |
| 1160 | // enter them is postponed to the controlling macros logic below. |
| 1161 | bool TryEnterHdr = false; |
| 1162 | if (FileInfo.isCompilingModuleHeader && FileInfo.isModuleHeader) |
| 1163 | TryEnterHdr = File->getDir() == ModMap.getBuiltinDir() && |
| 1164 | ModuleMap::isBuiltinHeader( |
| 1165 | llvm::sys::path::filename(File->getName())); |
| 1166 | |
| 1167 | // Textual headers can be #imported from different modules. Since ObjC |
| 1168 | // headers find in the wild might rely only on #import and do not contain |
| 1169 | // controlling macros, be conservative and only try to enter textual headers |
| 1170 | // if such macro is present. |
Bruno Cardoso Lopes | 4164dd9 | 2017-08-12 01:38:26 +0000 | [diff] [blame] | 1171 | if (!FileInfo.isModuleHeader && |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1172 | FileInfo.getControllingMacro(ExternalLookup)) |
| 1173 | TryEnterHdr = true; |
| 1174 | return TryEnterHdr; |
| 1175 | }; |
| 1176 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1177 | // If this is a #import directive, check that we have not already imported |
| 1178 | // this header. |
| 1179 | if (isImport) { |
| 1180 | // If this has already been imported, don't import it again. |
| 1181 | FileInfo.isImport = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1183 | // Has this already been #import'ed or #include'd? |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1184 | if (FileInfo.NumIncludes && !TryEnterImported()) |
| 1185 | return false; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1186 | } else { |
| 1187 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 1188 | // or if this is the second #include of a #pragma once file, ignore it. |
Bruno Cardoso Lopes | ba1b5c9 | 2017-01-11 02:14:51 +0000 | [diff] [blame] | 1189 | if (FileInfo.isImport && !TryEnterImported()) |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1193 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 1194 | // if the macro that guards it is defined, we know the #include has no effect. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | if (const IdentifierInfo *ControllingMacro |
Richard Smith | e70dadd | 2015-07-10 22:27:17 +0000 | [diff] [blame] | 1196 | = FileInfo.getControllingMacro(ExternalLookup)) { |
| 1197 | // If the header corresponds to a module, check whether the macro is already |
| 1198 | // defined in that module rather than checking in the current set of visible |
| 1199 | // modules. |
| 1200 | if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M) |
| 1201 | : PP.isMacroDefined(ControllingMacro)) { |
Douglas Gregor | 99734e7 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 1202 | ++NumMultiIncludeFileOptzn; |
| 1203 | return false; |
| 1204 | } |
Richard Smith | e70dadd | 2015-07-10 22:27:17 +0000 | [diff] [blame] | 1205 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1207 | // Increment the number of times this file has been included. |
| 1208 | ++FileInfo.NumIncludes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1210 | return true; |
| 1211 | } |
| 1212 | |
Ted Kremenek | fbcce6f | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 1213 | size_t HeaderSearch::getTotalMemory() const { |
| 1214 | return SearchDirs.capacity() |
Ted Kremenek | ae63d10 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 1215 | + llvm::capacity_in_bytes(FileInfo) |
| 1216 | + llvm::capacity_in_bytes(HeaderMaps) |
Ted Kremenek | fbcce6f | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 1217 | + LookupFileCache.getAllocator().getTotalMemory() |
| 1218 | + FrameworkMap.getAllocator().getTotalMemory(); |
| 1219 | } |
Douglas Gregor | 9f93e38 | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 1220 | |
| 1221 | StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 1222 | return FrameworkNames.insert(Framework).first->first(); |
Douglas Gregor | 9f93e38 | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 1223 | } |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1224 | |
| 1225 | bool HeaderSearch::hasModuleMap(StringRef FileName, |
Douglas Gregor | 963c553 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 1226 | const DirectoryEntry *Root, |
| 1227 | bool IsSystem) { |
Richard Smith | 47972af | 2015-06-16 00:08:24 +0000 | [diff] [blame] | 1228 | if (!HSOpts->ImplicitModuleMaps) |
Argyrios Kyrtzidis | 9955dbc | 2013-12-12 16:08:33 +0000 | [diff] [blame] | 1229 | return false; |
| 1230 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1231 | SmallVector<const DirectoryEntry *, 2> FixUpDirectories; |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1232 | |
| 1233 | StringRef DirName = FileName; |
| 1234 | do { |
| 1235 | // Get the parent directory name. |
| 1236 | DirName = llvm::sys::path::parent_path(DirName); |
| 1237 | if (DirName.empty()) |
| 1238 | return false; |
Daniel Jasper | ca9f738 | 2013-09-24 09:27:13 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1240 | // Determine whether this directory exists. |
| 1241 | const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); |
| 1242 | if (!Dir) |
| 1243 | return false; |
Daniel Jasper | ca9f738 | 2013-09-24 09:27:13 +0000 | [diff] [blame] | 1244 | |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1245 | // Try to load the module map file in this directory. |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 1246 | switch (loadModuleMapFile(Dir, IsSystem, |
| 1247 | llvm::sys::path::extension(Dir->getName()) == |
| 1248 | ".framework")) { |
Douglas Gregor | 80b6904 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1249 | case LMM_NewlyLoaded: |
| 1250 | case LMM_AlreadyLoaded: |
Daniel Jasper | ca9f738 | 2013-09-24 09:27:13 +0000 | [diff] [blame] | 1251 | // Success. All of the directories we stepped through inherit this module |
| 1252 | // map file. |
| 1253 | for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) |
| 1254 | DirectoryHasModuleMap[FixUpDirectories[I]] = true; |
| 1255 | return true; |
Daniel Jasper | 97da917 | 2013-10-22 08:09:47 +0000 | [diff] [blame] | 1256 | |
| 1257 | case LMM_NoDirectory: |
| 1258 | case LMM_InvalidModuleMap: |
| 1259 | break; |
Daniel Jasper | ca9f738 | 2013-09-24 09:27:13 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1262 | // If we hit the top of our search, we're done. |
| 1263 | if (Dir == Root) |
| 1264 | return false; |
| 1265 | |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1266 | // Keep track of all of the directories we checked, so we can mark them as |
| 1267 | // having module maps if we eventually do find a module map. |
| 1268 | FixUpDirectories.push_back(Dir); |
| 1269 | } while (true); |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 1272 | ModuleMap::KnownHeader |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 1273 | HeaderSearch::findModuleForHeader(const FileEntry *File, |
| 1274 | bool AllowTextual) const { |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1275 | if (ExternalSource) { |
| 1276 | // Make sure the external source has handled header info about this file, |
| 1277 | // which includes whether the file is part of a module. |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1278 | (void)getExistingFileInfo(File); |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1279 | } |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 1280 | return ModMap.findModuleForHeader(File, AllowTextual); |
| 1281 | } |
| 1282 | |
| 1283 | static bool suggestModule(HeaderSearch &HS, const FileEntry *File, |
| 1284 | Module *RequestingModule, |
| 1285 | ModuleMap::KnownHeader *SuggestedModule) { |
| 1286 | ModuleMap::KnownHeader Module = |
| 1287 | HS.findModuleForHeader(File, /*AllowTextual*/true); |
| 1288 | if (SuggestedModule) |
| 1289 | *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader) |
| 1290 | ? ModuleMap::KnownHeader() |
| 1291 | : Module; |
| 1292 | |
| 1293 | // If this module specifies [no_undeclared_includes], we cannot find any |
| 1294 | // file that's in a non-dependency module. |
| 1295 | if (RequestingModule && Module && RequestingModule->NoUndeclaredIncludes) { |
| 1296 | HS.getModuleMap().resolveUses(RequestingModule, /*Complain*/false); |
| 1297 | if (!RequestingModule->directlyUses(Module.getModule())) { |
| 1298 | return false; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | return true; |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 1305 | bool HeaderSearch::findUsableModuleForHeader( |
| 1306 | const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule, |
| 1307 | ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 1308 | llvm::NamedRegionTimer T("findmodule4header", "Find Usable Module For Header", |
| 1309 | IncGroupName, IncGroupDescription, |
| 1310 | llvm::TimePassesIsEnabled); |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 1311 | if (File && needModuleLookup(RequestingModule, SuggestedModule)) { |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 1312 | // If there is a module that corresponds to this header, suggest it. |
| 1313 | hasModuleMap(File->getName(), Root, IsSystemHeaderDir); |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 1314 | return suggestModule(*this, File, RequestingModule, SuggestedModule); |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 1315 | } |
| 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | bool HeaderSearch::findUsableModuleForFrameworkHeader( |
| 1320 | const FileEntry *File, StringRef FrameworkName, Module *RequestingModule, |
| 1321 | ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) { |
| 1322 | // If we're supposed to suggest a module, look for one now. |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 1323 | llvm::NamedRegionTimer T( |
| 1324 | "findmodule4fwheader", "Find Usable Module For Framework Header", |
| 1325 | IncGroupName, IncGroupDescription, llvm::TimePassesIsEnabled); |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 1326 | if (needModuleLookup(RequestingModule, SuggestedModule)) { |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 1327 | // Find the top-level framework based on this framework. |
| 1328 | SmallVector<std::string, 4> SubmodulePath; |
| 1329 | const DirectoryEntry *TopFrameworkDir |
| 1330 | = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath); |
| 1331 | |
| 1332 | // Determine the name of the top-level framework. |
| 1333 | StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName()); |
| 1334 | |
| 1335 | // Load this framework module. If that succeeds, find the suggested module |
| 1336 | // for this header, if any. |
| 1337 | loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework); |
| 1338 | |
| 1339 | // FIXME: This can find a module not part of ModuleName, which is |
| 1340 | // important so that we're consistent about whether this header |
| 1341 | // corresponds to a module. Possibly we should lock down framework modules |
| 1342 | // so that this is not possible. |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 1343 | return suggestModule(*this, File, RequestingModule, SuggestedModule); |
Richard Smith | 3d5b48c | 2015-10-16 21:42:56 +0000 | [diff] [blame] | 1344 | } |
| 1345 | return true; |
| 1346 | } |
| 1347 | |
Richard Smith | 9acb99e3 | 2014-12-10 03:09:48 +0000 | [diff] [blame] | 1348 | static const FileEntry *getPrivateModuleMap(const FileEntry *File, |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1349 | FileManager &FileMgr) { |
Richard Smith | 9acb99e3 | 2014-12-10 03:09:48 +0000 | [diff] [blame] | 1350 | StringRef Filename = llvm::sys::path::filename(File->getName()); |
| 1351 | SmallString<128> PrivateFilename(File->getDir()->getName()); |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1352 | if (Filename == "module.map") |
Douglas Gregor | 8030677 | 2011-12-07 21:25:07 +0000 | [diff] [blame] | 1353 | llvm::sys::path::append(PrivateFilename, "module_private.map"); |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1354 | else if (Filename == "module.modulemap") |
| 1355 | llvm::sys::path::append(PrivateFilename, "module.private.modulemap"); |
| 1356 | else |
| 1357 | return nullptr; |
| 1358 | return FileMgr.getFile(PrivateFilename); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 1361 | bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem, |
Richard Smith | 8b70610 | 2017-05-31 20:56:55 +0000 | [diff] [blame] | 1362 | FileID ID, unsigned *Offset, |
| 1363 | StringRef OriginalModuleMapFile) { |
Richard Smith | 9acb99e3 | 2014-12-10 03:09:48 +0000 | [diff] [blame] | 1364 | // Find the directory for the module. For frameworks, that may require going |
| 1365 | // up from the 'Modules' directory. |
| 1366 | const DirectoryEntry *Dir = nullptr; |
| 1367 | if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) |
| 1368 | Dir = FileMgr.getDirectory("."); |
| 1369 | else { |
Richard Smith | 8b70610 | 2017-05-31 20:56:55 +0000 | [diff] [blame] | 1370 | if (!OriginalModuleMapFile.empty()) { |
| 1371 | // We're building a preprocessed module map. Find or invent the directory |
| 1372 | // that it originally occupied. |
| 1373 | Dir = FileMgr.getDirectory( |
| 1374 | llvm::sys::path::parent_path(OriginalModuleMapFile)); |
| 1375 | if (!Dir) { |
| 1376 | auto *FakeFile = FileMgr.getVirtualFile(OriginalModuleMapFile, 0, 0); |
| 1377 | Dir = FakeFile->getDir(); |
| 1378 | } |
| 1379 | } else { |
| 1380 | Dir = File->getDir(); |
| 1381 | } |
| 1382 | |
Richard Smith | 9acb99e3 | 2014-12-10 03:09:48 +0000 | [diff] [blame] | 1383 | StringRef DirName(Dir->getName()); |
| 1384 | if (llvm::sys::path::filename(DirName) == "Modules") { |
| 1385 | DirName = llvm::sys::path::parent_path(DirName); |
| 1386 | if (DirName.endswith(".framework")) |
| 1387 | Dir = FileMgr.getDirectory(DirName); |
| 1388 | // FIXME: This assert can fail if there's a race between the above check |
| 1389 | // and the removal of the directory. |
| 1390 | assert(Dir && "parent must exist"); |
| 1391 | } |
| 1392 | } |
| 1393 | |
Bruno Cardoso Lopes | c192d19 | 2018-01-05 22:13:56 +0000 | [diff] [blame] | 1394 | switch (loadModuleMapFileImpl(File, IsSystem, Dir, ID, Offset)) { |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1395 | case LMM_AlreadyLoaded: |
| 1396 | case LMM_NewlyLoaded: |
| 1397 | return false; |
| 1398 | case LMM_NoDirectory: |
| 1399 | case LMM_InvalidModuleMap: |
| 1400 | return true; |
| 1401 | } |
Aaron Ballman | d8de5b6 | 2014-03-20 14:22:33 +0000 | [diff] [blame] | 1402 | llvm_unreachable("Unknown load module map result"); |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Bruno Cardoso Lopes | c192d19 | 2018-01-05 22:13:56 +0000 | [diff] [blame] | 1405 | HeaderSearch::LoadModuleMapResult |
| 1406 | HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem, |
| 1407 | const DirectoryEntry *Dir, FileID ID, |
| 1408 | unsigned *Offset) { |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1409 | assert(File && "expected FileEntry"); |
| 1410 | |
Richard Smith | 9887d79 | 2014-10-17 01:42:53 +0000 | [diff] [blame] | 1411 | // Check whether we've already loaded this module map, and mark it as being |
| 1412 | // loaded in case we recursively try to load it from itself. |
| 1413 | auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true)); |
| 1414 | if (!AddResult.second) |
| 1415 | return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap; |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1416 | |
Bruno Cardoso Lopes | c192d19 | 2018-01-05 22:13:56 +0000 | [diff] [blame] | 1417 | if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) { |
Richard Smith | 9887d79 | 2014-10-17 01:42:53 +0000 | [diff] [blame] | 1418 | LoadedModuleMaps[File] = false; |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1419 | return LMM_InvalidModuleMap; |
| 1420 | } |
| 1421 | |
| 1422 | // Try to load a corresponding private module map. |
Richard Smith | 9acb99e3 | 2014-12-10 03:09:48 +0000 | [diff] [blame] | 1423 | if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) { |
Bruno Cardoso Lopes | c192d19 | 2018-01-05 22:13:56 +0000 | [diff] [blame] | 1424 | if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) { |
Richard Smith | 9887d79 | 2014-10-17 01:42:53 +0000 | [diff] [blame] | 1425 | LoadedModuleMaps[File] = false; |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1426 | return LMM_InvalidModuleMap; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | // This directory has a module map. |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1431 | return LMM_NewlyLoaded; |
| 1432 | } |
| 1433 | |
| 1434 | const FileEntry * |
| 1435 | HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) { |
Richard Smith | 47972af | 2015-06-16 00:08:24 +0000 | [diff] [blame] | 1436 | if (!HSOpts->ImplicitModuleMaps) |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1437 | return nullptr; |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1438 | // For frameworks, the preferred spelling is Modules/module.modulemap, but |
| 1439 | // module.map at the framework root is also accepted. |
| 1440 | SmallString<128> ModuleMapFileName(Dir->getName()); |
| 1441 | if (IsFramework) |
| 1442 | llvm::sys::path::append(ModuleMapFileName, "Modules"); |
| 1443 | llvm::sys::path::append(ModuleMapFileName, "module.modulemap"); |
| 1444 | if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName)) |
| 1445 | return F; |
| 1446 | |
| 1447 | // Continue to allow module.map |
| 1448 | ModuleMapFileName = Dir->getName(); |
| 1449 | llvm::sys::path::append(ModuleMapFileName, "module.map"); |
| 1450 | return FileMgr.getFile(ModuleMapFileName); |
| 1451 | } |
| 1452 | |
| 1453 | Module *HeaderSearch::loadFrameworkModule(StringRef Name, |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 1454 | const DirectoryEntry *Dir, |
| 1455 | bool IsSystem) { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1456 | if (Module *Module = ModMap.findModule(Name)) |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1457 | return Module; |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1459 | // Try to load a module map file. |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1460 | switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) { |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1461 | case LMM_InvalidModuleMap: |
Ben Langmuir | a525400 | 2015-07-02 13:19:48 +0000 | [diff] [blame] | 1462 | // Try to infer a module map from the framework directory. |
| 1463 | if (HSOpts->ImplicitModuleMaps) |
| 1464 | ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr); |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1465 | break; |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1467 | case LMM_AlreadyLoaded: |
| 1468 | case LMM_NoDirectory: |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1469 | return nullptr; |
| 1470 | |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1471 | case LMM_NewlyLoaded: |
Ben Langmuir | a525400 | 2015-07-02 13:19:48 +0000 | [diff] [blame] | 1472 | break; |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1473 | } |
Douglas Gregor | 3a5999b | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 1474 | |
Ben Langmuir | a525400 | 2015-07-02 13:19:48 +0000 | [diff] [blame] | 1475 | return ModMap.findModule(Name); |
Douglas Gregor | 56c6401 | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Douglas Gregor | 80b6904 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1478 | HeaderSearch::LoadModuleMapResult |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1479 | HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem, |
| 1480 | bool IsFramework) { |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1481 | if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1482 | return loadModuleMapFile(Dir, IsSystem, IsFramework); |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | 80b6904 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1484 | return LMM_NoDirectory; |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Douglas Gregor | 80b6904 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1487 | HeaderSearch::LoadModuleMapResult |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1488 | HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem, |
| 1489 | bool IsFramework) { |
| 1490 | auto KnownDir = DirectoryHasModuleMap.find(Dir); |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1491 | if (KnownDir != DirectoryHasModuleMap.end()) |
Richard Smith | 9887d79 | 2014-10-17 01:42:53 +0000 | [diff] [blame] | 1492 | return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap; |
Douglas Gregor | e7ab366 | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1493 | |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1494 | if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) { |
Bruno Cardoso Lopes | c192d19 | 2018-01-05 22:13:56 +0000 | [diff] [blame] | 1495 | LoadModuleMapResult Result = |
| 1496 | loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir); |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1497 | // Add Dir explicitly in case ModuleMapFile is in a subdirectory. |
| 1498 | // E.g. Foo.framework/Modules/module.modulemap |
| 1499 | // ^Dir ^ModuleMapFile |
| 1500 | if (Result == LMM_NewlyLoaded) |
| 1501 | DirectoryHasModuleMap[Dir] = true; |
Richard Smith | 9887d79 | 2014-10-17 01:42:53 +0000 | [diff] [blame] | 1502 | else if (Result == LMM_InvalidModuleMap) |
| 1503 | DirectoryHasModuleMap[Dir] = false; |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1504 | return Result; |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1505 | } |
Douglas Gregor | 80b6904 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1506 | return LMM_InvalidModuleMap; |
Douglas Gregor | af28ec8 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1507 | } |
Douglas Gregor | 718292f | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1508 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1509 | void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) { |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame^] | 1510 | llvm::NamedRegionTimer T("allmodules", "Collect All Modules", IncGroupName, |
| 1511 | IncGroupDescription, llvm::TimePassesIsEnabled); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1512 | Modules.clear(); |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1513 | |
Richard Smith | 47972af | 2015-06-16 00:08:24 +0000 | [diff] [blame] | 1514 | if (HSOpts->ImplicitModuleMaps) { |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1515 | // Load module maps for each of the header search directories. |
| 1516 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 1517 | bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); |
| 1518 | if (SearchDirs[Idx].isFramework()) { |
| 1519 | std::error_code EC; |
| 1520 | SmallString<128> DirNative; |
| 1521 | llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(), |
| 1522 | DirNative); |
| 1523 | |
| 1524 | // Search each of the ".framework" directories to load them as modules. |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1525 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
| 1526 | for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1527 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1528 | if (llvm::sys::path::extension(Dir->getName()) != ".framework") |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1529 | continue; |
| 1530 | |
| 1531 | const DirectoryEntry *FrameworkDir = |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1532 | FileMgr.getDirectory(Dir->getName()); |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1533 | if (!FrameworkDir) |
| 1534 | continue; |
| 1535 | |
| 1536 | // Load this framework module. |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1537 | loadFrameworkModule(llvm::sys::path::stem(Dir->getName()), |
| 1538 | FrameworkDir, IsSystem); |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1539 | } |
| 1540 | continue; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1541 | } |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1542 | |
| 1543 | // FIXME: Deal with header maps. |
| 1544 | if (SearchDirs[Idx].isHeaderMap()) |
| 1545 | continue; |
| 1546 | |
| 1547 | // Try to load a module map file for the search directory. |
| 1548 | loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem, |
| 1549 | /*IsFramework*/ false); |
| 1550 | |
| 1551 | // Try to load module map files for immediate subdirectories of this |
| 1552 | // search directory. |
| 1553 | loadSubdirectoryModuleMaps(SearchDirs[Idx]); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1554 | } |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1555 | } |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1556 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1557 | // Populate the list of modules. |
| 1558 | for (ModuleMap::module_iterator M = ModMap.module_begin(), |
| 1559 | MEnd = ModMap.module_end(); |
| 1560 | M != MEnd; ++M) { |
| 1561 | Modules.push_back(M->getValue()); |
| 1562 | } |
| 1563 | } |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1564 | |
Douglas Gregor | 64a1fa5 | 2013-05-10 22:52:27 +0000 | [diff] [blame] | 1565 | void HeaderSearch::loadTopLevelSystemModules() { |
Richard Smith | 47972af | 2015-06-16 00:08:24 +0000 | [diff] [blame] | 1566 | if (!HSOpts->ImplicitModuleMaps) |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1567 | return; |
| 1568 | |
Douglas Gregor | 64a1fa5 | 2013-05-10 22:52:27 +0000 | [diff] [blame] | 1569 | // Load module maps for each of the header search directories. |
| 1570 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
Douglas Gregor | 299787f | 2013-11-01 23:08:38 +0000 | [diff] [blame] | 1571 | // We only care about normal header directories. |
| 1572 | if (!SearchDirs[Idx].isNormalDir()) { |
Douglas Gregor | 64a1fa5 | 2013-05-10 22:52:27 +0000 | [diff] [blame] | 1573 | continue; |
| 1574 | } |
| 1575 | |
| 1576 | // Try to load a module map file for the search directory. |
Douglas Gregor | 963c553 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 1577 | loadModuleMapFile(SearchDirs[Idx].getDir(), |
Ben Langmuir | 984e1df | 2014-03-19 20:23:34 +0000 | [diff] [blame] | 1578 | SearchDirs[Idx].isSystemHeaderDirectory(), |
| 1579 | SearchDirs[Idx].isFramework()); |
Douglas Gregor | 64a1fa5 | 2013-05-10 22:52:27 +0000 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1583 | void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) { |
Richard Smith | 47972af | 2015-06-16 00:08:24 +0000 | [diff] [blame] | 1584 | assert(HSOpts->ImplicitModuleMaps && |
Daniel Jasper | 21a0f55 | 2014-11-25 09:45:48 +0000 | [diff] [blame] | 1585 | "Should not be loading subdirectory module maps"); |
| 1586 | |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1587 | if (SearchDir.haveSearchedAllModuleMaps()) |
| 1588 | return; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 1589 | |
| 1590 | std::error_code EC; |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1591 | SmallString<128> DirNative; |
| 1592 | llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative); |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1593 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
| 1594 | for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1595 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1596 | bool IsFramework = |
| 1597 | llvm::sys::path::extension(Dir->getName()) == ".framework"; |
Ben Langmuir | 1f6a32b | 2015-02-24 04:58:15 +0000 | [diff] [blame] | 1598 | if (IsFramework == SearchDir.isFramework()) |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 1599 | loadModuleMapFile(Dir->getName(), SearchDir.isSystemHeaderDirectory(), |
Ben Langmuir | 1f6a32b | 2015-02-24 04:58:15 +0000 | [diff] [blame] | 1600 | SearchDir.isFramework()); |
Douglas Gregor | 0339a64 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | SearchDir.setSearchedAllModuleMaps(true); |
| 1604 | } |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1605 | |
| 1606 | std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File, |
| 1607 | bool *IsSystem) { |
| 1608 | // FIXME: We assume that the path name currently cached in the FileEntry is |
Eric Liu | dffb1a8 | 2018-01-29 13:21:23 +0000 | [diff] [blame] | 1609 | // the most appropriate one for this analysis (and that it's spelled the |
| 1610 | // same way as the corresponding header search path). |
| 1611 | return suggestPathToFileForDiagnostics(File->getName(), /*BuildDir=*/"", |
| 1612 | IsSystem); |
| 1613 | } |
| 1614 | |
| 1615 | std::string HeaderSearch::suggestPathToFileForDiagnostics( |
| 1616 | llvm::StringRef File, llvm::StringRef WorkingDir, bool *IsSystem) { |
| 1617 | using namespace llvm::sys; |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1618 | |
| 1619 | unsigned BestPrefixLength = 0; |
| 1620 | unsigned BestSearchDir; |
| 1621 | |
| 1622 | for (unsigned I = 0; I != SearchDirs.size(); ++I) { |
| 1623 | // FIXME: Support this search within frameworks and header maps. |
| 1624 | if (!SearchDirs[I].isNormalDir()) |
| 1625 | continue; |
| 1626 | |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 1627 | StringRef Dir = SearchDirs[I].getDir()->getName(); |
Eric Liu | dffb1a8 | 2018-01-29 13:21:23 +0000 | [diff] [blame] | 1628 | llvm::SmallString<32> DirPath(Dir.begin(), Dir.end()); |
| 1629 | if (!WorkingDir.empty() && !path::is_absolute(Dir)) { |
| 1630 | auto err = fs::make_absolute(WorkingDir, DirPath); |
| 1631 | if (!err) |
| 1632 | path::remove_dots(DirPath, /*remove_dot_dot=*/true); |
| 1633 | Dir = DirPath; |
| 1634 | } |
| 1635 | for (auto NI = path::begin(File), NE = path::end(File), |
| 1636 | DI = path::begin(Dir), DE = path::end(Dir); |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1637 | /*termination condition in loop*/; ++NI, ++DI) { |
Eric Liu | dffb1a8 | 2018-01-29 13:21:23 +0000 | [diff] [blame] | 1638 | // '.' components in File are ignored. |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1639 | while (NI != NE && *NI == ".") |
| 1640 | ++NI; |
| 1641 | if (NI == NE) |
| 1642 | break; |
| 1643 | |
| 1644 | // '.' components in Dir are ignored. |
| 1645 | while (DI != DE && *DI == ".") |
| 1646 | ++DI; |
| 1647 | if (DI == DE) { |
Eric Liu | dffb1a8 | 2018-01-29 13:21:23 +0000 | [diff] [blame] | 1648 | // Dir is a prefix of File, up to '.' components and choice of path |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1649 | // separators. |
Eric Liu | dffb1a8 | 2018-01-29 13:21:23 +0000 | [diff] [blame] | 1650 | unsigned PrefixLength = NI - path::begin(File); |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1651 | if (PrefixLength > BestPrefixLength) { |
| 1652 | BestPrefixLength = PrefixLength; |
| 1653 | BestSearchDir = I; |
| 1654 | } |
| 1655 | break; |
| 1656 | } |
| 1657 | |
| 1658 | if (*NI != *DI) |
| 1659 | break; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | if (IsSystem) |
| 1664 | *IsSystem = BestPrefixLength ? BestSearchDir >= SystemDirIdx : false; |
Eric Liu | dffb1a8 | 2018-01-29 13:21:23 +0000 | [diff] [blame] | 1665 | return File.drop_front(BestPrefixLength); |
Richard Smith | 4eb8393 | 2016-04-27 21:57:05 +0000 | [diff] [blame] | 1666 | } |