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