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