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