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