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