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