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