/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/ModuleMap.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- ModuleMap.cpp - Describe the layout of modules ---------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines the ModuleMap implementation, which describes the layout |
10 | | // of a module as it relates to headers. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Lex/ModuleMap.h" |
15 | | #include "clang/Basic/CharInfo.h" |
16 | | #include "clang/Basic/Diagnostic.h" |
17 | | #include "clang/Basic/FileManager.h" |
18 | | #include "clang/Basic/LLVM.h" |
19 | | #include "clang/Basic/LangOptions.h" |
20 | | #include "clang/Basic/Module.h" |
21 | | #include "clang/Basic/SourceLocation.h" |
22 | | #include "clang/Basic/SourceManager.h" |
23 | | #include "clang/Basic/TargetInfo.h" |
24 | | #include "clang/Lex/HeaderSearch.h" |
25 | | #include "clang/Lex/HeaderSearchOptions.h" |
26 | | #include "clang/Lex/LexDiagnostic.h" |
27 | | #include "clang/Lex/Lexer.h" |
28 | | #include "clang/Lex/LiteralSupport.h" |
29 | | #include "clang/Lex/Token.h" |
30 | | #include "llvm/ADT/DenseMap.h" |
31 | | #include "llvm/ADT/STLExtras.h" |
32 | | #include "llvm/ADT/SmallPtrSet.h" |
33 | | #include "llvm/ADT/SmallString.h" |
34 | | #include "llvm/ADT/SmallVector.h" |
35 | | #include "llvm/ADT/StringMap.h" |
36 | | #include "llvm/ADT/StringRef.h" |
37 | | #include "llvm/ADT/StringSwitch.h" |
38 | | #include "llvm/Support/Allocator.h" |
39 | | #include "llvm/Support/Compiler.h" |
40 | | #include "llvm/Support/ErrorHandling.h" |
41 | | #include "llvm/Support/MemoryBuffer.h" |
42 | | #include "llvm/Support/Path.h" |
43 | | #include "llvm/Support/VirtualFileSystem.h" |
44 | | #include "llvm/Support/raw_ostream.h" |
45 | | #include <algorithm> |
46 | | #include <cassert> |
47 | | #include <cstdint> |
48 | | #include <cstring> |
49 | | #include <optional> |
50 | | #include <string> |
51 | | #include <system_error> |
52 | | #include <utility> |
53 | | |
54 | | using namespace clang; |
55 | | |
56 | 0 | void ModuleMapCallbacks::anchor() {} |
57 | | |
58 | 18.4k | void ModuleMap::resolveLinkAsDependencies(Module *Mod) { |
59 | 18.4k | auto PendingLinkAs = PendingLinkAsModule.find(Mod->Name); |
60 | 18.4k | if (PendingLinkAs != PendingLinkAsModule.end()) { |
61 | 5 | for (auto &Name : PendingLinkAs->second) { |
62 | 5 | auto *M = findModule(Name.getKey()); |
63 | 5 | if (M) |
64 | 5 | M->UseExportAsModuleLinkName = true; |
65 | 5 | } |
66 | 5 | } |
67 | 18.4k | } |
68 | | |
69 | 33 | void ModuleMap::addLinkAsDependency(Module *Mod) { |
70 | 33 | if (findModule(Mod->ExportAsModule)) |
71 | 13 | Mod->UseExportAsModuleLinkName = true; |
72 | 20 | else |
73 | 20 | PendingLinkAsModule[Mod->ExportAsModule].insert(Mod->Name); |
74 | 33 | } |
75 | | |
76 | 4.02M | Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) { |
77 | 4.02M | switch ((int)Role) { |
78 | 3.86M | case NormalHeader: |
79 | 3.86M | return Module::HK_Normal; |
80 | 199 | case PrivateHeader: |
81 | 199 | return Module::HK_Private; |
82 | 144k | case TextualHeader: |
83 | 144k | return Module::HK_Textual; |
84 | 42 | case PrivateHeader | TextualHeader: |
85 | 42 | return Module::HK_PrivateTextual; |
86 | 22.3k | case ExcludedHeader: |
87 | 22.3k | return Module::HK_Excluded; |
88 | 4.02M | } |
89 | 0 | llvm_unreachable("unknown header role"); |
90 | 0 | } |
91 | | |
92 | | ModuleMap::ModuleHeaderRole |
93 | 2.02M | ModuleMap::headerKindToRole(Module::HeaderKind Kind) { |
94 | 2.02M | switch ((int)Kind) { |
95 | 1.94M | case Module::HK_Normal: |
96 | 1.94M | return NormalHeader; |
97 | 68 | case Module::HK_Private: |
98 | 68 | return PrivateHeader; |
99 | 73.5k | case Module::HK_Textual: |
100 | 73.5k | return TextualHeader; |
101 | 21 | case Module::HK_PrivateTextual: |
102 | 21 | return ModuleHeaderRole(PrivateHeader | TextualHeader); |
103 | 10.3k | case Module::HK_Excluded: |
104 | 10.3k | return ExcludedHeader; |
105 | 2.02M | } |
106 | 0 | llvm_unreachable("unknown header kind"); |
107 | 0 | } |
108 | | |
109 | 2.38M | bool ModuleMap::isModular(ModuleHeaderRole Role) { |
110 | 2.38M | return !(Role & (ModuleMap::TextualHeader | ModuleMap::ExcludedHeader)); |
111 | 2.38M | } |
112 | | |
113 | | Module::ExportDecl |
114 | | ModuleMap::resolveExport(Module *Mod, |
115 | | const Module::UnresolvedExportDecl &Unresolved, |
116 | 5.12k | bool Complain) const { |
117 | | // We may have just a wildcard. |
118 | 5.12k | if (Unresolved.Id.empty()) { |
119 | 4.41k | assert(Unresolved.Wildcard && "Invalid unresolved export"); |
120 | 4.41k | return Module::ExportDecl(nullptr, true); |
121 | 4.41k | } |
122 | | |
123 | | // Resolve the module-id. |
124 | 706 | Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); |
125 | 706 | if (!Context) |
126 | 196 | return {}; |
127 | | |
128 | 510 | return Module::ExportDecl(Context, Unresolved.Wildcard); |
129 | 706 | } |
130 | | |
131 | | Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, |
132 | 789 | bool Complain) const { |
133 | | // Find the starting module. |
134 | 789 | Module *Context = lookupModuleUnqualified(Id[0].first, Mod); |
135 | 789 | if (!Context) { |
136 | 196 | if (Complain) |
137 | 0 | Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified) |
138 | 0 | << Id[0].first << Mod->getFullModuleName(); |
139 | | |
140 | 196 | return nullptr; |
141 | 196 | } |
142 | | |
143 | | // Dig into the module path. |
144 | 741 | for (unsigned I = 1, N = Id.size(); 593 I != N; ++I148 ) { |
145 | 156 | Module *Sub = lookupModuleQualified(Id[I].first, Context); |
146 | 156 | if (!Sub) { |
147 | 8 | if (Complain) |
148 | 0 | Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) |
149 | 0 | << Id[I].first << Context->getFullModuleName() |
150 | 0 | << SourceRange(Id[0].second, Id[I-1].second); |
151 | | |
152 | 8 | return nullptr; |
153 | 8 | } |
154 | | |
155 | 148 | Context = Sub; |
156 | 148 | } |
157 | | |
158 | 585 | return Context; |
159 | 593 | } |
160 | | |
161 | | /// Append to \p Paths the set of paths needed to get to the |
162 | | /// subframework in which the given module lives. |
163 | | static void appendSubframeworkPaths(Module *Mod, |
164 | 11.6k | SmallVectorImpl<char> &Path) { |
165 | | // Collect the framework names from the given module to the top-level module. |
166 | 11.6k | SmallVector<StringRef, 2> Paths; |
167 | 32.7k | for (; Mod; Mod = Mod->Parent21.0k ) { |
168 | 21.0k | if (Mod->IsFramework) |
169 | 12.5k | Paths.push_back(Mod->Name); |
170 | 21.0k | } |
171 | | |
172 | 11.6k | if (Paths.empty()) |
173 | 2 | return; |
174 | | |
175 | | // Add Frameworks/Name.framework for each subframework. |
176 | 11.6k | for (StringRef Framework : llvm::drop_begin(llvm::reverse(Paths))) |
177 | 847 | llvm::sys::path::append(Path, "Frameworks", Framework + ".framework"); |
178 | 11.6k | } |
179 | | |
180 | | OptionalFileEntryRef ModuleMap::findHeader( |
181 | | Module *M, const Module::UnresolvedHeaderDirective &Header, |
182 | 1.99M | SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework) { |
183 | | // Search for the header file within the module's home directory. |
184 | 1.99M | auto Directory = M->Directory; |
185 | 1.99M | SmallString<128> FullPathName(Directory->getName()); |
186 | | |
187 | 1.99M | auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef { |
188 | 1.99M | auto File = |
189 | 1.99M | expectedToOptional(SourceMgr.getFileManager().getFileRef(Filename)); |
190 | 1.99M | if (!File || (1.99M Header.Size1.99M && File->getSize() != *Header.Size33 ) || |
191 | 1.99M | (1.99M Header.ModTime1.99M && File->getModificationTime() != *Header.ModTime31 )) |
192 | 7.98k | return std::nullopt; |
193 | 1.99M | return *File; |
194 | 1.99M | }; |
195 | | |
196 | 1.99M | auto GetFrameworkFile = [&]() -> OptionalFileEntryRef { |
197 | 11.6k | unsigned FullPathLength = FullPathName.size(); |
198 | 11.6k | appendSubframeworkPaths(M, RelativePathName); |
199 | 11.6k | unsigned RelativePathLength = RelativePathName.size(); |
200 | | |
201 | | // Check whether this file is in the public headers. |
202 | 11.6k | llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); |
203 | 11.6k | llvm::sys::path::append(FullPathName, RelativePathName); |
204 | 11.6k | if (auto File = GetFile(FullPathName)) |
205 | 11.5k | return File; |
206 | | |
207 | | // Check whether this file is in the private headers. |
208 | | // Ideally, private modules in the form 'FrameworkName.Private' should |
209 | | // be defined as 'module FrameworkName.Private', and not as |
210 | | // 'framework module FrameworkName.Private', since a 'Private.Framework' |
211 | | // does not usually exist. However, since both are currently widely used |
212 | | // for private modules, make sure we find the right path in both cases. |
213 | 174 | if (M->IsFramework && M->Name == "Private"86 ) |
214 | 10 | RelativePathName.clear(); |
215 | 164 | else |
216 | 164 | RelativePathName.resize(RelativePathLength); |
217 | 174 | FullPathName.resize(FullPathLength); |
218 | 174 | llvm::sys::path::append(RelativePathName, "PrivateHeaders", |
219 | 174 | Header.FileName); |
220 | 174 | llvm::sys::path::append(FullPathName, RelativePathName); |
221 | 174 | return GetFile(FullPathName); |
222 | 11.6k | }; |
223 | | |
224 | 1.99M | if (llvm::sys::path::is_absolute(Header.FileName)) { |
225 | 2 | RelativePathName.clear(); |
226 | 2 | RelativePathName.append(Header.FileName.begin(), Header.FileName.end()); |
227 | 2 | return GetFile(Header.FileName); |
228 | 2 | } |
229 | | |
230 | 1.99M | if (M->isPartOfFramework()) |
231 | 11.6k | return GetFrameworkFile(); |
232 | | |
233 | | // Lookup for normal headers. |
234 | 1.98M | llvm::sys::path::append(RelativePathName, Header.FileName); |
235 | 1.98M | llvm::sys::path::append(FullPathName, RelativePathName); |
236 | 1.98M | auto NormalHdrFile = GetFile(FullPathName); |
237 | | |
238 | 1.98M | if (!NormalHdrFile && Directory->getName().endswith(".framework")7.79k ) { |
239 | | // The lack of 'framework' keyword in a module declaration it's a simple |
240 | | // mistake we can diagnose when the header exists within the proper |
241 | | // framework style path. |
242 | 2 | FullPathName.assign(Directory->getName()); |
243 | 2 | RelativePathName.clear(); |
244 | 2 | if (GetFrameworkFile()) { |
245 | 2 | Diags.Report(Header.FileNameLoc, |
246 | 2 | diag::warn_mmap_incomplete_framework_module_declaration) |
247 | 2 | << Header.FileName << M->getFullModuleName(); |
248 | 2 | NeedsFramework = true; |
249 | 2 | } |
250 | 2 | return std::nullopt; |
251 | 2 | } |
252 | | |
253 | 1.98M | return NormalHdrFile; |
254 | 1.98M | } |
255 | | |
256 | | void ModuleMap::resolveHeader(Module *Mod, |
257 | | const Module::UnresolvedHeaderDirective &Header, |
258 | 1.99M | bool &NeedsFramework) { |
259 | 1.99M | SmallString<128> RelativePathName; |
260 | 1.99M | if (OptionalFileEntryRef File = |
261 | 1.99M | findHeader(Mod, Header, RelativePathName, NeedsFramework)) { |
262 | 1.99M | if (Header.IsUmbrella) { |
263 | 3.71k | const DirectoryEntry *UmbrellaDir = &File->getDir().getDirEntry(); |
264 | 3.71k | if (Module *UmbrellaMod = UmbrellaDirs[UmbrellaDir]) |
265 | 0 | Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) |
266 | 0 | << UmbrellaMod->getFullModuleName(); |
267 | 3.71k | else |
268 | | // Record this umbrella header. |
269 | 3.71k | setUmbrellaHeaderAsWritten(Mod, *File, Header.FileName, |
270 | 3.71k | RelativePathName.str()); |
271 | 1.98M | } else { |
272 | 1.98M | Module::Header H = {Header.FileName, std::string(RelativePathName.str()), |
273 | 1.98M | *File}; |
274 | 1.98M | addHeader(Mod, H, headerKindToRole(Header.Kind)); |
275 | 1.98M | } |
276 | 1.99M | } else if (7.81k Header.HasBuiltinHeader7.81k && !Header.Size3.07k && !Header.ModTime3.07k ) { |
277 | | // There's a builtin header but no corresponding on-disk header. Assume |
278 | | // this was supposed to modularize the builtin header alone. |
279 | 4.74k | } else if (Header.Kind == Module::HK_Excluded) { |
280 | | // Ignore missing excluded header files. They're optional anyway. |
281 | 3.09k | } else { |
282 | | // If we find a module that has a missing header, we mark this module as |
283 | | // unavailable and store the header directive for displaying diagnostics. |
284 | 3.09k | Mod->MissingHeaders.push_back(Header); |
285 | | // A missing header with stat information doesn't make the module |
286 | | // unavailable; this keeps our behavior consistent as headers are lazily |
287 | | // resolved. (Such a module still can't be built though, except from |
288 | | // preprocessed source.) |
289 | 3.09k | if (!Header.Size && !Header.ModTime3.08k ) |
290 | 3.08k | Mod->markUnavailable(/*Unimportable=*/false); |
291 | 3.09k | } |
292 | 1.99M | } |
293 | | |
294 | | bool ModuleMap::resolveAsBuiltinHeader( |
295 | 1.99M | Module *Mod, const Module::UnresolvedHeaderDirective &Header) { |
296 | 1.99M | if (Header.Kind == Module::HK_Excluded || |
297 | 1.99M | llvm::sys::path::is_absolute(Header.FileName)1.98M || |
298 | 1.99M | Mod->isPartOfFramework()1.98M || !Mod->IsSystem1.97M || Header.IsUmbrella1.87M || |
299 | 1.99M | !BuiltinIncludeDir1.87M || BuiltinIncludeDir == Mod->Directory1.87M || |
300 | 1.99M | !isBuiltinHeader(Header.FileName)1.80M ) |
301 | 1.98M | return false; |
302 | | |
303 | | // This is a system module with a top-level header. This header |
304 | | // may have a counterpart (or replacement) in the set of headers |
305 | | // supplied by Clang. Find that builtin header. |
306 | 19.6k | SmallString<128> Path; |
307 | 19.6k | llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), Header.FileName); |
308 | 19.6k | auto File = SourceMgr.getFileManager().getOptionalFileRef(Path); |
309 | 19.6k | if (!File) |
310 | 0 | return false; |
311 | | |
312 | 19.6k | auto Role = headerKindToRole(Header.Kind); |
313 | 19.6k | Module::Header H = {Header.FileName, std::string(Path.str()), *File}; |
314 | 19.6k | addHeader(Mod, H, Role); |
315 | 19.6k | return true; |
316 | 19.6k | } |
317 | | |
318 | | ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, |
319 | | const LangOptions &LangOpts, const TargetInfo *Target, |
320 | | HeaderSearch &HeaderInfo) |
321 | 93.8k | : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target), |
322 | 93.8k | HeaderInfo(HeaderInfo) { |
323 | 93.8k | MMapLangOpts.LineComment = true; |
324 | 93.8k | } |
325 | | |
326 | 87.7k | ModuleMap::~ModuleMap() { |
327 | 87.7k | for (auto &M : Modules) |
328 | 1.50M | delete M.getValue(); |
329 | 87.7k | for (auto *M : ShadowModules) |
330 | 3 | delete M; |
331 | 87.7k | } |
332 | | |
333 | 93.7k | void ModuleMap::setTarget(const TargetInfo &Target) { |
334 | 93.7k | assert((!this->Target || this->Target == &Target) && |
335 | 93.7k | "Improper target override"); |
336 | 93.7k | this->Target = &Target; |
337 | 93.7k | } |
338 | | |
339 | | /// "Sanitize" a filename so that it can be used as an identifier. |
340 | | static StringRef sanitizeFilenameAsIdentifier(StringRef Name, |
341 | 5.84k | SmallVectorImpl<char> &Buffer) { |
342 | 5.84k | if (Name.empty()) |
343 | 0 | return Name; |
344 | | |
345 | 5.84k | if (!isValidAsciiIdentifier(Name)) { |
346 | | // If we don't already have something with the form of an identifier, |
347 | | // create a buffer with the sanitized name. |
348 | 66 | Buffer.clear(); |
349 | 66 | if (isDigit(Name[0])) |
350 | 2 | Buffer.push_back('_'); |
351 | 66 | Buffer.reserve(Buffer.size() + Name.size()); |
352 | 770 | for (unsigned I = 0, N = Name.size(); I != N; ++I704 ) { |
353 | 704 | if (isAsciiIdentifierContinue(Name[I])) |
354 | 640 | Buffer.push_back(Name[I]); |
355 | 64 | else |
356 | 64 | Buffer.push_back('_'); |
357 | 704 | } |
358 | | |
359 | 66 | Name = StringRef(Buffer.data(), Buffer.size()); |
360 | 66 | } |
361 | | |
362 | 5.84k | while (llvm::StringSwitch<bool>(Name) |
363 | 1.84M | #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) |
364 | 397k | #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) |
365 | 5.84k | #include "clang/Basic/TokenKinds.def" |
366 | 5.84k | .Default(false)) { |
367 | 2 | if (Name.data() != Buffer.data()) |
368 | 2 | Buffer.append(Name.begin(), Name.end()); |
369 | 2 | Buffer.push_back('_'); |
370 | 2 | Name = StringRef(Buffer.data(), Buffer.size()); |
371 | 2 | } |
372 | | |
373 | 5.84k | return Name; |
374 | 5.84k | } |
375 | | |
376 | | /// Determine whether the given file name is the name of a builtin |
377 | | /// header, supplied by Clang to replace, override, or augment existing system |
378 | | /// headers. |
379 | 1.80M | bool ModuleMap::isBuiltinHeader(StringRef FileName) { |
380 | 1.80M | return llvm::StringSwitch<bool>(FileName) |
381 | 1.80M | .Case("float.h", true) |
382 | 1.80M | .Case("iso646.h", true) |
383 | 1.80M | .Case("limits.h", true) |
384 | 1.80M | .Case("stdalign.h", true) |
385 | 1.80M | .Case("stdarg.h", true) |
386 | 1.80M | .Case("stdatomic.h", true) |
387 | 1.80M | .Case("stdbool.h", true) |
388 | 1.80M | .Case("stddef.h", true) |
389 | 1.80M | .Case("stdint.h", true) |
390 | 1.80M | .Case("tgmath.h", true) |
391 | 1.80M | .Case("unwind.h", true) |
392 | 1.80M | .Default(false); |
393 | 1.80M | } |
394 | | |
395 | 14.8k | bool ModuleMap::isBuiltinHeader(const FileEntry *File) { |
396 | 14.8k | return File->getDir() == BuiltinIncludeDir && |
397 | 14.8k | ModuleMap::isBuiltinHeader(llvm::sys::path::filename(File->getName()))3.07k ; |
398 | 14.8k | } |
399 | | |
400 | | ModuleMap::HeadersMap::iterator |
401 | 11.4M | ModuleMap::findKnownHeader(const FileEntry *File) { |
402 | 11.4M | resolveHeaderDirectives(File); |
403 | 11.4M | HeadersMap::iterator Known = Headers.find(File); |
404 | 11.4M | if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps && |
405 | 11.4M | Known == Headers.end()142k && ModuleMap::isBuiltinHeader(File)9.80k ) { |
406 | 74 | HeaderInfo.loadTopLevelSystemModules(); |
407 | 74 | return Headers.find(File); |
408 | 74 | } |
409 | 11.4M | return Known; |
410 | 11.4M | } |
411 | | |
412 | | ModuleMap::KnownHeader ModuleMap::findHeaderInUmbrellaDirs( |
413 | 11.2M | FileEntryRef File, SmallVectorImpl<DirectoryEntryRef> &IntermediateDirs) { |
414 | 11.2M | if (UmbrellaDirs.empty()) |
415 | 11.2M | return {}; |
416 | | |
417 | 7.52k | OptionalDirectoryEntryRef Dir = File.getDir(); |
418 | | |
419 | | // Note: as an egregious but useful hack we use the real path here, because |
420 | | // frameworks moving from top-level frameworks to embedded frameworks tend |
421 | | // to be symlinked from the top-level location to the embedded location, |
422 | | // and we need to resolve lookups as if we had found the embedded location. |
423 | 7.52k | StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir); |
424 | | |
425 | | // Keep walking up the directory hierarchy, looking for a directory with |
426 | | // an umbrella header. |
427 | 47.0k | do { |
428 | 47.0k | auto KnownDir = UmbrellaDirs.find(*Dir); |
429 | 47.0k | if (KnownDir != UmbrellaDirs.end()) |
430 | 4.04k | return KnownHeader(KnownDir->second, NormalHeader); |
431 | | |
432 | 43.0k | IntermediateDirs.push_back(*Dir); |
433 | | |
434 | | // Retrieve our parent path. |
435 | 43.0k | DirName = llvm::sys::path::parent_path(DirName); |
436 | 43.0k | if (DirName.empty()) |
437 | 3.48k | break; |
438 | | |
439 | | // Resolve the parent path to a directory entry. |
440 | 39.5k | Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName); |
441 | 39.5k | } while (Dir); |
442 | 3.48k | return {}; |
443 | 7.52k | } |
444 | | |
445 | | static bool violatesPrivateInclude(Module *RequestingModule, |
446 | | const FileEntry *IncFileEnt, |
447 | 46.8k | ModuleMap::KnownHeader Header) { |
448 | 46.8k | #ifndef NDEBUG |
449 | 46.8k | if (Header.getRole() & ModuleMap::PrivateHeader) { |
450 | | // Check for consistency between the module header role |
451 | | // as obtained from the lookup and as obtained from the module. |
452 | | // This check is not cheap, so enable it only for debugging. |
453 | 59 | bool IsPrivate = false; |
454 | 59 | SmallVectorImpl<Module::Header> *HeaderList[] = { |
455 | 59 | &Header.getModule()->Headers[Module::HK_Private], |
456 | 59 | &Header.getModule()->Headers[Module::HK_PrivateTextual]}; |
457 | 59 | for (auto *Hs : HeaderList) |
458 | 118 | IsPrivate |= llvm::any_of( |
459 | 118 | *Hs, [&](const Module::Header &H) { return H.Entry == IncFileEnt; }61 ); |
460 | 59 | assert(IsPrivate && "inconsistent headers and roles"); |
461 | 59 | } |
462 | 46.8k | #endif |
463 | 46.8k | return !Header.isAccessibleFrom(RequestingModule); |
464 | 46.8k | } |
465 | | |
466 | 7.61M | static Module *getTopLevelOrNull(Module *M) { |
467 | 7.61M | return M ? M->getTopLevelModule()97.9k : nullptr7.51M ; |
468 | 7.61M | } |
469 | | |
470 | | void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, |
471 | | bool RequestingModuleIsModuleInterface, |
472 | | SourceLocation FilenameLoc, |
473 | 3.80M | StringRef Filename, FileEntryRef File) { |
474 | | // No errors for indirect modules. This may be a bit of a problem for modules |
475 | | // with no source files. |
476 | 3.80M | if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) |
477 | 1.94k | return; |
478 | | |
479 | 3.80M | if (RequestingModule) { |
480 | 47.4k | resolveUses(RequestingModule, /*Complain=*/false); |
481 | 47.4k | resolveHeaderDirectives(RequestingModule, /*File=*/std::nullopt); |
482 | 47.4k | } |
483 | | |
484 | 3.80M | bool Excluded = false; |
485 | 3.80M | Module *Private = nullptr; |
486 | 3.80M | Module *NotUsed = nullptr; |
487 | | |
488 | 3.80M | HeadersMap::iterator Known = findKnownHeader(File); |
489 | 3.80M | if (Known != Headers.end()) { |
490 | 46.8k | for (const KnownHeader &Header : Known->second) { |
491 | | // Excluded headers don't really belong to a module. |
492 | 46.8k | if (Header.getRole() == ModuleMap::ExcludedHeader) { |
493 | 46 | Excluded = true; |
494 | 46 | continue; |
495 | 46 | } |
496 | | |
497 | | // Remember private headers for later printing of a diagnostic. |
498 | 46.8k | if (violatesPrivateInclude(RequestingModule, File, Header)) { |
499 | 31 | Private = Header.getModule(); |
500 | 31 | continue; |
501 | 31 | } |
502 | | |
503 | | // If uses need to be specified explicitly, we are only allowed to return |
504 | | // modules that are explicitly used by the requesting module. |
505 | 46.8k | if (RequestingModule && LangOpts.ModulesDeclUse45.5k && |
506 | 46.8k | !RequestingModule->directlyUses(Header.getModule())75 ) { |
507 | 14 | NotUsed = Header.getModule(); |
508 | 14 | continue; |
509 | 14 | } |
510 | | |
511 | | // We have found a module that we can happily use. |
512 | 46.8k | return; |
513 | 46.8k | } |
514 | | |
515 | 70 | Excluded = true; |
516 | 70 | } |
517 | | |
518 | | // We have found a header, but it is private. |
519 | 3.75M | if (Private) { |
520 | 17 | Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module) |
521 | 17 | << Filename; |
522 | 17 | return; |
523 | 17 | } |
524 | | |
525 | | // We have found a module, but we don't use it. |
526 | 3.75M | if (NotUsed) { |
527 | 12 | Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module_indirect) |
528 | 12 | << RequestingModule->getTopLevelModule()->Name << Filename |
529 | 12 | << NotUsed->Name; |
530 | 12 | return; |
531 | 12 | } |
532 | | |
533 | 3.75M | if (Excluded || isHeaderInUmbrellaDirs(File)3.75M ) |
534 | 38 | return; |
535 | | |
536 | | // At this point, only non-modular includes remain. |
537 | | |
538 | 3.75M | if (RequestingModule && LangOpts.ModulesStrictDeclUse1.87k ) { |
539 | 6 | Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) |
540 | 6 | << RequestingModule->getTopLevelModule()->Name << Filename; |
541 | 3.75M | } else if (RequestingModule && RequestingModuleIsModuleInterface1.86k && |
542 | 3.75M | LangOpts.isCompilingModule()1.85k ) { |
543 | | // Do not diagnose when we are not compiling a module. |
544 | 1.84k | diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? |
545 | 47 | diag::warn_non_modular_include_in_framework_module : |
546 | 1.84k | diag::warn_non_modular_include_in_module1.80k ; |
547 | 1.84k | Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName() |
548 | 1.84k | << File.getName(); |
549 | 1.84k | } |
550 | 3.75M | } |
551 | | |
552 | | static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New, |
553 | 155 | const ModuleMap::KnownHeader &Old) { |
554 | | // Prefer available modules. |
555 | | // FIXME: Considering whether the module is available rather than merely |
556 | | // importable is non-hermetic and can result in surprising behavior for |
557 | | // prebuilt modules. Consider only checking for importability here. |
558 | 155 | if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable()111 ) |
559 | 1 | return true; |
560 | | |
561 | | // Prefer a public header over a private header. |
562 | 154 | if ((New.getRole() & ModuleMap::PrivateHeader) != |
563 | 154 | (Old.getRole() & ModuleMap::PrivateHeader)) |
564 | 4 | return !(New.getRole() & ModuleMap::PrivateHeader); |
565 | | |
566 | | // Prefer a non-textual header over a textual header. |
567 | 150 | if ((New.getRole() & ModuleMap::TextualHeader) != |
568 | 150 | (Old.getRole() & ModuleMap::TextualHeader)) |
569 | 45 | return !(New.getRole() & ModuleMap::TextualHeader); |
570 | | |
571 | | // Prefer a non-excluded header over an excluded header. |
572 | 105 | if ((New.getRole() == ModuleMap::ExcludedHeader) != |
573 | 105 | (Old.getRole() == ModuleMap::ExcludedHeader)) |
574 | 0 | return New.getRole() != ModuleMap::ExcludedHeader; |
575 | | |
576 | | // Don't have a reason to choose between these. Just keep the first one. |
577 | 105 | return false; |
578 | 105 | } |
579 | | |
580 | | ModuleMap::KnownHeader ModuleMap::findModuleForHeader(FileEntryRef File, |
581 | | bool AllowTextual, |
582 | 7.61M | bool AllowExcluded) { |
583 | 7.61M | auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { |
584 | 7.61M | if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader1.89k ) |
585 | 214 | return {}; |
586 | 7.61M | return R; |
587 | 7.61M | }; |
588 | | |
589 | 7.61M | HeadersMap::iterator Known = findKnownHeader(File); |
590 | 7.61M | if (Known != Headers.end()) { |
591 | 86.5k | ModuleMap::KnownHeader Result; |
592 | | // Iterate over all modules that 'File' is part of to find the best fit. |
593 | 86.7k | for (KnownHeader &H : Known->second) { |
594 | | // Cannot use a module if the header is excluded in it. |
595 | 86.7k | if (!AllowExcluded86.7k && H.getRole() == ModuleMap::ExcludedHeader) |
596 | 140 | continue; |
597 | | // Prefer a header from the source module over all others. |
598 | 86.6k | if (H.getModule()->getTopLevelModule() == SourceModule) |
599 | 65.7k | return MakeResult(H); |
600 | 20.8k | if (!Result || isBetterKnownHeader(H, Result)155 ) |
601 | 20.7k | Result = H; |
602 | 20.8k | } |
603 | 20.8k | return MakeResult(Result); |
604 | 86.5k | } |
605 | | |
606 | 7.52M | return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File)); |
607 | 7.61M | } |
608 | | |
609 | | ModuleMap::KnownHeader |
610 | 7.52M | ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File) { |
611 | 7.52M | assert(!Headers.count(File) && "already have a module for this header"); |
612 | | |
613 | 7.52M | SmallVector<DirectoryEntryRef, 2> SkippedDirs; |
614 | 7.52M | KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); |
615 | 7.52M | if (H) { |
616 | 4.04k | Module *Result = H.getModule(); |
617 | | |
618 | | // Search up the module stack until we find a module with an umbrella |
619 | | // directory. |
620 | 4.04k | Module *UmbrellaModule = Result; |
621 | 4.04k | while (!UmbrellaModule->getEffectiveUmbrellaDir() && UmbrellaModule->Parent0 ) |
622 | 0 | UmbrellaModule = UmbrellaModule->Parent; |
623 | | |
624 | 4.04k | if (UmbrellaModule->InferSubmodules) { |
625 | 3.98k | OptionalFileEntryRef UmbrellaModuleMap = |
626 | 3.98k | getModuleMapFileForUniquing(UmbrellaModule); |
627 | | |
628 | | // Infer submodules for each of the directories we found between |
629 | | // the directory of the umbrella header and the directory where |
630 | | // the actual header is located. |
631 | 3.98k | bool Explicit = UmbrellaModule->InferExplicitSubmodules; |
632 | | |
633 | 3.98k | for (DirectoryEntryRef SkippedDir : llvm::reverse(SkippedDirs)) { |
634 | | // Find or create the module that corresponds to this directory name. |
635 | 66 | SmallString<32> NameBuf; |
636 | 66 | StringRef Name = sanitizeFilenameAsIdentifier( |
637 | 66 | llvm::sys::path::stem(SkippedDir.getName()), NameBuf); |
638 | 66 | Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, |
639 | 66 | Explicit).first; |
640 | 66 | InferredModuleAllowedBy[Result] = UmbrellaModuleMap; |
641 | 66 | Result->IsInferred = true; |
642 | | |
643 | | // Associate the module and the directory. |
644 | 66 | UmbrellaDirs[SkippedDir] = Result; |
645 | | |
646 | | // If inferred submodules export everything they import, add a |
647 | | // wildcard to the set of exports. |
648 | 66 | if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()62 ) |
649 | 62 | Result->Exports.push_back(Module::ExportDecl(nullptr, true)); |
650 | 66 | } |
651 | | |
652 | | // Infer a submodule with the same name as this header file. |
653 | 3.98k | SmallString<32> NameBuf; |
654 | 3.98k | StringRef Name = sanitizeFilenameAsIdentifier( |
655 | 3.98k | llvm::sys::path::stem(File.getName()), NameBuf); |
656 | 3.98k | Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, |
657 | 3.98k | Explicit).first; |
658 | 3.98k | InferredModuleAllowedBy[Result] = UmbrellaModuleMap; |
659 | 3.98k | Result->IsInferred = true; |
660 | 3.98k | Result->addTopHeader(File); |
661 | | |
662 | | // If inferred submodules export everything they import, add a |
663 | | // wildcard to the set of exports. |
664 | 3.98k | if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()3.95k ) |
665 | 3.95k | Result->Exports.push_back(Module::ExportDecl(nullptr, true)); |
666 | 3.98k | } else { |
667 | | // Record each of the directories we stepped through as being part of |
668 | | // the module we found, since the umbrella header covers them all. |
669 | 58 | for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I1 ) |
670 | 1 | UmbrellaDirs[SkippedDirs[I]] = Result; |
671 | 57 | } |
672 | | |
673 | 4.04k | KnownHeader Header(Result, NormalHeader); |
674 | 4.04k | Headers[File].push_back(Header); |
675 | 4.04k | return Header; |
676 | 4.04k | } |
677 | | |
678 | 7.52M | return {}; |
679 | 7.52M | } |
680 | | |
681 | | ArrayRef<ModuleMap::KnownHeader> |
682 | 969 | ModuleMap::findAllModulesForHeader(FileEntryRef File) { |
683 | 969 | HeadersMap::iterator Known = findKnownHeader(File); |
684 | 969 | if (Known != Headers.end()) |
685 | 947 | return Known->second; |
686 | | |
687 | 22 | if (findOrCreateModuleForHeaderInUmbrellaDir(File)) |
688 | 0 | return Headers.find(File)->second; |
689 | | |
690 | 22 | return std::nullopt; |
691 | 22 | } |
692 | | |
693 | | ArrayRef<ModuleMap::KnownHeader> |
694 | 36.4k | ModuleMap::findResolvedModulesForHeader(const FileEntry *File) const { |
695 | | // FIXME: Is this necessary? |
696 | 36.4k | resolveHeaderDirectives(File); |
697 | 36.4k | auto It = Headers.find(File); |
698 | 36.4k | if (It == Headers.end()) |
699 | 8.79k | return std::nullopt; |
700 | 27.6k | return It->second; |
701 | 36.4k | } |
702 | | |
703 | 135 | bool ModuleMap::isHeaderInUnavailableModule(FileEntryRef Header) const { |
704 | 135 | return isHeaderUnavailableInModule(Header, nullptr); |
705 | 135 | } |
706 | | |
707 | | bool ModuleMap::isHeaderUnavailableInModule( |
708 | 1.35k | FileEntryRef Header, const Module *RequestingModule) const { |
709 | 1.35k | resolveHeaderDirectives(Header); |
710 | 1.35k | HeadersMap::const_iterator Known = Headers.find(Header); |
711 | 1.35k | if (Known != Headers.end()) { |
712 | 104 | for (SmallVectorImpl<KnownHeader>::const_iterator |
713 | 104 | I = Known->second.begin(), |
714 | 104 | E = Known->second.end(); |
715 | 195 | I != E; ++I91 ) { |
716 | | |
717 | 107 | if (I->getRole() == ModuleMap::ExcludedHeader) |
718 | 7 | continue; |
719 | | |
720 | 100 | if (I->isAvailable() && |
721 | 100 | (17 !RequestingModule17 || |
722 | 17 | I->getModule()->isSubModuleOf(RequestingModule))) { |
723 | | // When no requesting module is available, the caller is looking if a |
724 | | // header is part a module by only looking into the module map. This is |
725 | | // done by warn_uncovered_module_header checks; don't consider textual |
726 | | // headers part of it in this mode, otherwise we get misleading warnings |
727 | | // that a umbrella header is not including a textual header. |
728 | 16 | if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader0 ) |
729 | 0 | continue; |
730 | 16 | return false; |
731 | 16 | } |
732 | 100 | } |
733 | 88 | return true; |
734 | 104 | } |
735 | | |
736 | 1.25k | OptionalDirectoryEntryRef Dir = Header.getDir(); |
737 | 1.25k | SmallVector<DirectoryEntryRef, 2> SkippedDirs; |
738 | 1.25k | StringRef DirName = Dir->getName(); |
739 | | |
740 | 1.26k | auto IsUnavailable = [&](const Module *M) { |
741 | 1.26k | return !M->isAvailable() && (0 !RequestingModule0 || |
742 | 0 | M->isSubModuleOf(RequestingModule)); |
743 | 1.26k | }; |
744 | | |
745 | | // Keep walking up the directory hierarchy, looking for a directory with |
746 | | // an umbrella header. |
747 | 1.25k | do { |
748 | 1.25k | auto KnownDir = UmbrellaDirs.find(*Dir); |
749 | 1.25k | if (KnownDir != UmbrellaDirs.end()) { |
750 | 1.25k | Module *Found = KnownDir->second; |
751 | 1.25k | if (IsUnavailable(Found)) |
752 | 0 | return true; |
753 | | |
754 | | // Search up the module stack until we find a module with an umbrella |
755 | | // directory. |
756 | 1.25k | Module *UmbrellaModule = Found; |
757 | 1.25k | while (!UmbrellaModule->getEffectiveUmbrellaDir() && |
758 | 1.25k | UmbrellaModule->Parent0 ) |
759 | 0 | UmbrellaModule = UmbrellaModule->Parent; |
760 | | |
761 | 1.25k | if (UmbrellaModule->InferSubmodules) { |
762 | 1.24k | for (DirectoryEntryRef SkippedDir : llvm::reverse(SkippedDirs)) { |
763 | | // Find or create the module that corresponds to this directory name. |
764 | 4 | SmallString<32> NameBuf; |
765 | 4 | StringRef Name = sanitizeFilenameAsIdentifier( |
766 | 4 | llvm::sys::path::stem(SkippedDir.getName()), NameBuf); |
767 | 4 | Found = lookupModuleQualified(Name, Found); |
768 | 4 | if (!Found) |
769 | 4 | return false; |
770 | 0 | if (IsUnavailable(Found)) |
771 | 0 | return true; |
772 | 0 | } |
773 | | |
774 | | // Infer a submodule with the same name as this header file. |
775 | 1.24k | SmallString<32> NameBuf; |
776 | 1.24k | StringRef Name = sanitizeFilenameAsIdentifier( |
777 | 1.24k | llvm::sys::path::stem(Header.getName()), |
778 | 1.24k | NameBuf); |
779 | 1.24k | Found = lookupModuleQualified(Name, Found); |
780 | 1.24k | if (!Found) |
781 | 1.24k | return false; |
782 | 1.24k | } |
783 | | |
784 | 6 | return IsUnavailable(Found); |
785 | 1.25k | } |
786 | | |
787 | 5 | SkippedDirs.push_back(*Dir); |
788 | | |
789 | | // Retrieve our parent path. |
790 | 5 | DirName = llvm::sys::path::parent_path(DirName); |
791 | 5 | if (DirName.empty()) |
792 | 0 | break; |
793 | | |
794 | | // Resolve the parent path to a directory entry. |
795 | 5 | Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName); |
796 | 5 | } while (Dir); |
797 | | |
798 | 0 | return false; |
799 | 1.25k | } |
800 | | |
801 | 75.9M | Module *ModuleMap::findModule(StringRef Name) const { |
802 | 75.9M | llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); |
803 | 75.9M | if (Known != Modules.end()) |
804 | 72.5M | return Known->getValue(); |
805 | | |
806 | 3.36M | return nullptr; |
807 | 75.9M | } |
808 | | |
809 | | Module *ModuleMap::lookupModuleUnqualified(StringRef Name, |
810 | 789 | Module *Context) const { |
811 | 2.10k | for(; Context; Context = Context->Parent1.31k ) { |
812 | 1.50k | if (Module *Sub = lookupModuleQualified(Name, Context)) |
813 | 182 | return Sub; |
814 | 1.50k | } |
815 | | |
816 | 607 | return findModule(Name); |
817 | 789 | } |
818 | | |
819 | 4.95M | Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ |
820 | 4.95M | if (!Context) |
821 | 3.49M | return findModule(Name); |
822 | | |
823 | 1.46M | return Context->findSubmodule(Name); |
824 | 4.95M | } |
825 | | |
826 | | std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name, |
827 | | Module *Parent, |
828 | | bool IsFramework, |
829 | 2.96M | bool IsExplicit) { |
830 | | // Try to find an existing module with this name. |
831 | 2.96M | if (Module *Sub = lookupModuleQualified(Name, Parent)) |
832 | 814k | return std::make_pair(Sub, false); |
833 | | |
834 | | // Create a new module with this name. |
835 | 2.15M | Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, |
836 | 2.15M | IsExplicit, NumCreatedModules++); |
837 | 2.15M | if (!Parent) { |
838 | 1.51M | if (LangOpts.CurrentModule == Name) |
839 | 3.42k | SourceModule = Result; |
840 | 1.51M | Modules[Name] = Result; |
841 | 1.51M | ModuleScopeIDs[Result] = CurrentModuleScopeID; |
842 | 1.51M | } |
843 | 2.15M | return std::make_pair(Result, true); |
844 | 2.96M | } |
845 | | |
846 | | Module *ModuleMap::createGlobalModuleFragmentForModuleUnit(SourceLocation Loc, |
847 | 182 | Module *Parent) { |
848 | 182 | auto *Result = new Module("<global>", Loc, Parent, /*IsFramework*/ false, |
849 | 182 | /*IsExplicit*/ true, NumCreatedModules++); |
850 | 182 | Result->Kind = Module::ExplicitGlobalModuleFragment; |
851 | | // If the created module isn't owned by a parent, send it to PendingSubmodules |
852 | | // to wait for its parent. |
853 | 182 | if (!Result->Parent) |
854 | 172 | PendingSubmodules.emplace_back(Result); |
855 | 182 | return Result; |
856 | 182 | } |
857 | | |
858 | | Module *ModuleMap::createImplicitGlobalModuleFragmentForModuleUnit( |
859 | 20 | SourceLocation Loc, bool IsExported, Module *Parent) { |
860 | 20 | assert(Parent && "We should only create an implicit global module fragment " |
861 | 20 | "in a module purview"); |
862 | | // Note: Here the `IsExplicit` parameter refers to the semantics in clang |
863 | | // modules. All the non-explicit submodules in clang modules will be exported |
864 | | // too. Here we simplify the implementation by using the concept. |
865 | 20 | auto *Result = new Module(IsExported ? "<exported implicit global>"6 |
866 | 20 | : "<implicit global>"14 , |
867 | 20 | Loc, Parent, /*IsFramework*/ false, |
868 | 20 | /*IsExplicit*/ !IsExported, NumCreatedModules++); |
869 | 20 | Result->Kind = Module::ImplicitGlobalModuleFragment; |
870 | 20 | return Result; |
871 | 20 | } |
872 | | |
873 | | Module * |
874 | | ModuleMap::createPrivateModuleFragmentForInterfaceUnit(Module *Parent, |
875 | 18 | SourceLocation Loc) { |
876 | 18 | auto *Result = |
877 | 18 | new Module("<private>", Loc, Parent, /*IsFramework*/ false, |
878 | 18 | /*IsExplicit*/ true, NumCreatedModules++); |
879 | 18 | Result->Kind = Module::PrivateModuleFragment; |
880 | 18 | return Result; |
881 | 18 | } |
882 | | |
883 | | Module *ModuleMap::createModuleUnitWithKind(SourceLocation Loc, StringRef Name, |
884 | 536 | Module::ModuleKind Kind) { |
885 | 536 | auto *Result = |
886 | 536 | new Module(Name, Loc, nullptr, /*IsFramework*/ false, |
887 | 536 | /*IsExplicit*/ false, NumCreatedModules++); |
888 | 536 | Result->Kind = Kind; |
889 | | |
890 | | // Reparent any current global module fragment as a submodule of this module. |
891 | 536 | for (auto &Submodule : PendingSubmodules) { |
892 | 169 | Submodule->setParent(Result); |
893 | 169 | Submodule.release(); // now owned by parent |
894 | 169 | } |
895 | 536 | PendingSubmodules.clear(); |
896 | 536 | return Result; |
897 | 536 | } |
898 | | |
899 | | Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc, |
900 | 497 | StringRef Name) { |
901 | 497 | assert(LangOpts.CurrentModule == Name && "module name mismatch"); |
902 | 497 | assert(!Modules[Name] && "redefining existing module"); |
903 | | |
904 | 497 | auto *Result = |
905 | 497 | createModuleUnitWithKind(Loc, Name, Module::ModuleInterfaceUnit); |
906 | 497 | Modules[Name] = SourceModule = Result; |
907 | | |
908 | | // Mark the main source file as being within the newly-created module so that |
909 | | // declarations and macros are properly visibility-restricted to it. |
910 | 497 | auto *MainFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
911 | 497 | assert(MainFile && "no input file for module interface"); |
912 | 497 | Headers[MainFile].push_back(KnownHeader(Result, PrivateHeader)); |
913 | | |
914 | 497 | return Result; |
915 | 497 | } |
916 | | |
917 | | Module *ModuleMap::createModuleForImplementationUnit(SourceLocation Loc, |
918 | 39 | StringRef Name) { |
919 | 39 | assert(LangOpts.CurrentModule == Name && "module name mismatch"); |
920 | | // The interface for this implementation must exist and be loaded. |
921 | 39 | assert(Modules[Name] && Modules[Name]->Kind == Module::ModuleInterfaceUnit && |
922 | 39 | "creating implementation module without an interface"); |
923 | | |
924 | | // Create an entry in the modules map to own the implementation unit module. |
925 | | // User module names must not start with a period (so that this cannot clash |
926 | | // with any legal user-defined module name). |
927 | 39 | StringRef IName = ".ImplementationUnit"; |
928 | 39 | assert(!Modules[IName] && "multiple implementation units?"); |
929 | | |
930 | 39 | auto *Result = |
931 | 39 | createModuleUnitWithKind(Loc, Name, Module::ModuleImplementationUnit); |
932 | 39 | Modules[IName] = SourceModule = Result; |
933 | | |
934 | | // Check that the main file is present. |
935 | 39 | assert(SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) && |
936 | 39 | "no input file for module implementation"); |
937 | | |
938 | 39 | return Result; |
939 | 39 | } |
940 | | |
941 | | Module *ModuleMap::createHeaderUnit(SourceLocation Loc, StringRef Name, |
942 | 30 | Module::Header H) { |
943 | 30 | assert(LangOpts.CurrentModule == Name && "module name mismatch"); |
944 | 30 | assert(!Modules[Name] && "redefining existing module"); |
945 | | |
946 | 30 | auto *Result = new Module(Name, Loc, nullptr, /*IsFramework*/ false, |
947 | 30 | /*IsExplicit*/ false, NumCreatedModules++); |
948 | 30 | Result->Kind = Module::ModuleHeaderUnit; |
949 | 30 | Modules[Name] = SourceModule = Result; |
950 | 30 | addHeader(Result, H, NormalHeader); |
951 | 30 | return Result; |
952 | 30 | } |
953 | | |
954 | | /// For a framework module, infer the framework against which we |
955 | | /// should link. |
956 | 1.78k | static void inferFrameworkLink(Module *Mod) { |
957 | 1.78k | assert(Mod->IsFramework && "Can only infer linking for framework modules"); |
958 | 1.78k | assert(!Mod->isSubFramework() && |
959 | 1.78k | "Can only infer linking for top-level frameworks"); |
960 | | |
961 | 1.78k | Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, |
962 | 1.78k | /*IsFramework=*/true)); |
963 | 1.78k | } |
964 | | |
965 | | Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir, |
966 | 418 | bool IsSystem, Module *Parent) { |
967 | 418 | Attributes Attrs; |
968 | 418 | Attrs.IsSystem = IsSystem; |
969 | 418 | return inferFrameworkModule(FrameworkDir, Attrs, Parent); |
970 | 418 | } |
971 | | |
972 | | Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir, |
973 | 543 | Attributes Attrs, Module *Parent) { |
974 | | // Note: as an egregious but useful hack we use the real path here, because |
975 | | // we might be looking at an embedded framework that symlinks out to a |
976 | | // top-level framework, and we need to infer as if we were naming the |
977 | | // top-level framework. |
978 | 543 | StringRef FrameworkDirName = |
979 | 543 | SourceMgr.getFileManager().getCanonicalName(FrameworkDir); |
980 | | |
981 | | // In case this is a case-insensitive filesystem, use the canonical |
982 | | // directory name as the ModuleName, since modules are case-sensitive. |
983 | | // FIXME: we should be able to give a fix-it hint for the correct spelling. |
984 | 543 | SmallString<32> ModuleNameStorage; |
985 | 543 | StringRef ModuleName = sanitizeFilenameAsIdentifier( |
986 | 543 | llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage); |
987 | | |
988 | | // Check whether we've already found this module. |
989 | 543 | if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) |
990 | 205 | return Mod; |
991 | | |
992 | 338 | FileManager &FileMgr = SourceMgr.getFileManager(); |
993 | | |
994 | | // If the framework has a parent path from which we're allowed to infer |
995 | | // a framework module, do so. |
996 | 338 | OptionalFileEntryRef ModuleMapFile; |
997 | 338 | if (!Parent) { |
998 | | // Determine whether we're allowed to infer a module map. |
999 | 213 | bool canInfer = false; |
1000 | 213 | if (llvm::sys::path::has_parent_path(FrameworkDirName)) { |
1001 | | // Figure out the parent path. |
1002 | 213 | StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); |
1003 | 213 | if (auto ParentDir = FileMgr.getOptionalDirectoryRef(Parent)) { |
1004 | | // Check whether we have already looked into the parent directory |
1005 | | // for a module map. |
1006 | 213 | llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator |
1007 | 213 | inferred = InferredDirectories.find(*ParentDir); |
1008 | 213 | if (inferred == InferredDirectories.end()) { |
1009 | | // We haven't looked here before. Load a module map, if there is |
1010 | | // one. |
1011 | 158 | bool IsFrameworkDir = Parent.endswith(".framework"); |
1012 | 158 | if (OptionalFileEntryRef ModMapFile = |
1013 | 158 | HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) { |
1014 | 149 | parseModuleMapFile(*ModMapFile, Attrs.IsSystem, *ParentDir); |
1015 | 149 | inferred = InferredDirectories.find(*ParentDir); |
1016 | 149 | } |
1017 | | |
1018 | 158 | if (inferred == InferredDirectories.end()) |
1019 | 9 | inferred = InferredDirectories.insert( |
1020 | 9 | std::make_pair(*ParentDir, InferredDirectory())).first; |
1021 | 158 | } |
1022 | | |
1023 | 213 | if (inferred->second.InferModules) { |
1024 | | // We're allowed to infer for this directory, but make sure it's okay |
1025 | | // to infer this particular module. |
1026 | 172 | StringRef Name = llvm::sys::path::stem(FrameworkDirName); |
1027 | 172 | canInfer = |
1028 | 172 | !llvm::is_contained(inferred->second.ExcludedModules, Name); |
1029 | | |
1030 | 172 | Attrs.IsSystem |= inferred->second.Attrs.IsSystem; |
1031 | 172 | Attrs.IsExternC |= inferred->second.Attrs.IsExternC; |
1032 | 172 | Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; |
1033 | 172 | Attrs.NoUndeclaredIncludes |= |
1034 | 172 | inferred->second.Attrs.NoUndeclaredIncludes; |
1035 | 172 | ModuleMapFile = inferred->second.ModuleMapFile; |
1036 | 172 | } |
1037 | 213 | } |
1038 | 213 | } |
1039 | | |
1040 | | // If we're not allowed to infer a framework module, don't. |
1041 | 213 | if (!canInfer) |
1042 | 43 | return nullptr; |
1043 | 213 | } else { |
1044 | 125 | OptionalFileEntryRefDegradesToFileEntryPtr ModuleMapRef = |
1045 | 125 | getModuleMapFileForUniquing(Parent); |
1046 | 125 | ModuleMapFile = ModuleMapRef; |
1047 | 125 | } |
1048 | | |
1049 | | // Look for an umbrella header. |
1050 | 295 | SmallString<128> UmbrellaName = FrameworkDir.getName(); |
1051 | 295 | llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); |
1052 | 295 | auto UmbrellaHeader = FileMgr.getOptionalFileRef(UmbrellaName); |
1053 | | |
1054 | | // FIXME: If there's no umbrella header, we could probably scan the |
1055 | | // framework to load *everything*. But, it's not clear that this is a good |
1056 | | // idea. |
1057 | 295 | if (!UmbrellaHeader) |
1058 | 0 | return nullptr; |
1059 | | |
1060 | 295 | Module *Result = new Module(ModuleName, SourceLocation(), Parent, |
1061 | 295 | /*IsFramework=*/true, /*IsExplicit=*/false, |
1062 | 295 | NumCreatedModules++); |
1063 | 295 | InferredModuleAllowedBy[Result] = ModuleMapFile; |
1064 | 295 | Result->IsInferred = true; |
1065 | 295 | if (!Parent) { |
1066 | 170 | if (LangOpts.CurrentModule == ModuleName) |
1067 | 3 | SourceModule = Result; |
1068 | 170 | Modules[ModuleName] = Result; |
1069 | 170 | ModuleScopeIDs[Result] = CurrentModuleScopeID; |
1070 | 170 | } |
1071 | | |
1072 | 295 | Result->IsSystem |= Attrs.IsSystem; |
1073 | 295 | Result->IsExternC |= Attrs.IsExternC; |
1074 | 295 | Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; |
1075 | 295 | Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes; |
1076 | 295 | Result->Directory = FrameworkDir; |
1077 | | |
1078 | | // Chop off the first framework bit, as that is implied. |
1079 | 295 | StringRef RelativePath = UmbrellaName.str().substr( |
1080 | 295 | Result->getTopLevelModule()->Directory->getName().size()); |
1081 | 295 | RelativePath = llvm::sys::path::relative_path(RelativePath); |
1082 | | |
1083 | | // umbrella header "umbrella-header-name" |
1084 | 295 | setUmbrellaHeaderAsWritten(Result, *UmbrellaHeader, ModuleName + ".h", |
1085 | 295 | RelativePath); |
1086 | | |
1087 | | // export * |
1088 | 295 | Result->Exports.push_back(Module::ExportDecl(nullptr, true)); |
1089 | | |
1090 | | // module * { export * } |
1091 | 295 | Result->InferSubmodules = true; |
1092 | 295 | Result->InferExportWildcard = true; |
1093 | | |
1094 | | // Look for subframeworks. |
1095 | 295 | std::error_code EC; |
1096 | 295 | SmallString<128> SubframeworksDirName = FrameworkDir.getName(); |
1097 | 295 | llvm::sys::path::append(SubframeworksDirName, "Frameworks"); |
1098 | 295 | llvm::sys::path::native(SubframeworksDirName); |
1099 | 295 | llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); |
1100 | 295 | for (llvm::vfs::directory_iterator |
1101 | 295 | Dir = FS.dir_begin(SubframeworksDirName, EC), |
1102 | 295 | DirEnd; |
1103 | 426 | Dir != DirEnd && !EC131 ; Dir.increment(EC)131 ) { |
1104 | 131 | if (!StringRef(Dir->path()).endswith(".framework")) |
1105 | 0 | continue; |
1106 | | |
1107 | 131 | if (auto SubframeworkDir = FileMgr.getOptionalDirectoryRef(Dir->path())) { |
1108 | | // Note: as an egregious but useful hack, we use the real path here and |
1109 | | // check whether it is actually a subdirectory of the parent directory. |
1110 | | // This will not be the case if the 'subframework' is actually a symlink |
1111 | | // out to a top-level framework. |
1112 | 131 | StringRef SubframeworkDirName = |
1113 | 131 | FileMgr.getCanonicalName(*SubframeworkDir); |
1114 | 131 | bool FoundParent = false; |
1115 | 342 | do { |
1116 | | // Get the parent directory name. |
1117 | 342 | SubframeworkDirName |
1118 | 342 | = llvm::sys::path::parent_path(SubframeworkDirName); |
1119 | 342 | if (SubframeworkDirName.empty()) |
1120 | 6 | break; |
1121 | | |
1122 | 336 | if (auto SubDir = FileMgr.getDirectory(SubframeworkDirName)) { |
1123 | 336 | if (*SubDir == FrameworkDir) { |
1124 | 125 | FoundParent = true; |
1125 | 125 | break; |
1126 | 125 | } |
1127 | 336 | } |
1128 | 336 | } while (true211 ); |
1129 | | |
1130 | 131 | if (!FoundParent) |
1131 | 6 | continue; |
1132 | | |
1133 | | // FIXME: Do we want to warn about subframeworks without umbrella headers? |
1134 | 125 | inferFrameworkModule(*SubframeworkDir, Attrs, Result); |
1135 | 125 | } |
1136 | 131 | } |
1137 | | |
1138 | | // If the module is a top-level framework, automatically link against the |
1139 | | // framework. |
1140 | 295 | if (!Result->isSubFramework()) |
1141 | 170 | inferFrameworkLink(Result); |
1142 | | |
1143 | 295 | return Result; |
1144 | 295 | } |
1145 | | |
1146 | | Module *ModuleMap::createShadowedModule(StringRef Name, bool IsFramework, |
1147 | 3 | Module *ShadowingModule) { |
1148 | | |
1149 | | // Create a new module with this name. |
1150 | 3 | Module *Result = |
1151 | 3 | new Module(Name, SourceLocation(), /*Parent=*/nullptr, IsFramework, |
1152 | 3 | /*IsExplicit=*/false, NumCreatedModules++); |
1153 | 3 | Result->ShadowingModule = ShadowingModule; |
1154 | 3 | Result->markUnavailable(/*Unimportable*/true); |
1155 | 3 | ModuleScopeIDs[Result] = CurrentModuleScopeID; |
1156 | 3 | ShadowModules.push_back(Result); |
1157 | | |
1158 | 3 | return Result; |
1159 | 3 | } |
1160 | | |
1161 | | void ModuleMap::setUmbrellaHeaderAsWritten( |
1162 | | Module *Mod, FileEntryRef UmbrellaHeader, const Twine &NameAsWritten, |
1163 | 4.01k | const Twine &PathRelativeToRootModuleDirectory) { |
1164 | 4.01k | Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); |
1165 | 4.01k | Mod->Umbrella = UmbrellaHeader; |
1166 | 4.01k | Mod->UmbrellaAsWritten = NameAsWritten.str(); |
1167 | 4.01k | Mod->UmbrellaRelativeToRootModuleDirectory = |
1168 | 4.01k | PathRelativeToRootModuleDirectory.str(); |
1169 | 4.01k | UmbrellaDirs[UmbrellaHeader.getDir()] = Mod; |
1170 | | |
1171 | | // Notify callbacks that we just added a new header. |
1172 | 4.01k | for (const auto &Cb : Callbacks) |
1173 | 174 | Cb->moduleMapAddUmbrellaHeader(UmbrellaHeader); |
1174 | 4.01k | } |
1175 | | |
1176 | | void ModuleMap::setUmbrellaDirAsWritten( |
1177 | | Module *Mod, DirectoryEntryRef UmbrellaDir, const Twine &NameAsWritten, |
1178 | 8.80k | const Twine &PathRelativeToRootModuleDirectory) { |
1179 | 8.80k | Mod->Umbrella = UmbrellaDir; |
1180 | 8.80k | Mod->UmbrellaAsWritten = NameAsWritten.str(); |
1181 | 8.80k | Mod->UmbrellaRelativeToRootModuleDirectory = |
1182 | 8.80k | PathRelativeToRootModuleDirectory.str(); |
1183 | 8.80k | UmbrellaDirs[UmbrellaDir] = Mod; |
1184 | 8.80k | } |
1185 | | |
1186 | | void ModuleMap::addUnresolvedHeader(Module *Mod, |
1187 | | Module::UnresolvedHeaderDirective Header, |
1188 | 1.99M | bool &NeedsFramework) { |
1189 | | // If there is a builtin counterpart to this file, add it now so it can |
1190 | | // wrap the system header. |
1191 | 1.99M | if (resolveAsBuiltinHeader(Mod, Header)) { |
1192 | | // If we have both a builtin and system version of the file, the |
1193 | | // builtin version may want to inject macros into the system header, so |
1194 | | // force the system header to be treated as a textual header in this |
1195 | | // case. |
1196 | 19.6k | Header.Kind = headerRoleToKind(ModuleMap::ModuleHeaderRole( |
1197 | 19.6k | headerKindToRole(Header.Kind) | ModuleMap::TextualHeader)); |
1198 | 19.6k | Header.HasBuiltinHeader = true; |
1199 | 19.6k | } |
1200 | | |
1201 | | // If possible, don't stat the header until we need to. This requires the |
1202 | | // user to have provided us with some stat information about the file. |
1203 | | // FIXME: Add support for lazily stat'ing umbrella headers and excluded |
1204 | | // headers. |
1205 | 1.99M | if ((Header.Size || Header.ModTime1.99M ) && !Header.IsUmbrella50 && |
1206 | 1.99M | Header.Kind != Module::HK_Excluded50 ) { |
1207 | | // We expect more variation in mtime than size, so if we're given both, |
1208 | | // use the mtime as the key. |
1209 | 50 | if (Header.ModTime) |
1210 | 44 | LazyHeadersByModTime[*Header.ModTime].push_back(Mod); |
1211 | 6 | else |
1212 | 6 | LazyHeadersBySize[*Header.Size].push_back(Mod); |
1213 | 50 | Mod->UnresolvedHeaders.push_back(Header); |
1214 | 50 | return; |
1215 | 50 | } |
1216 | | |
1217 | | // We don't have stat information or can't defer looking this file up. |
1218 | | // Perform the lookup now. |
1219 | 1.99M | resolveHeader(Mod, Header, NeedsFramework); |
1220 | 1.99M | } |
1221 | | |
1222 | 11.4M | void ModuleMap::resolveHeaderDirectives(const FileEntry *File) const { |
1223 | 11.4M | auto BySize = LazyHeadersBySize.find(File->getSize()); |
1224 | 11.4M | if (BySize != LazyHeadersBySize.end()) { |
1225 | 1 | for (auto *M : BySize->second) |
1226 | 1 | resolveHeaderDirectives(M, File); |
1227 | 1 | LazyHeadersBySize.erase(BySize); |
1228 | 1 | } |
1229 | | |
1230 | 11.4M | auto ByModTime = LazyHeadersByModTime.find(File->getModificationTime()); |
1231 | 11.4M | if (ByModTime != LazyHeadersByModTime.end()) { |
1232 | 18 | for (auto *M : ByModTime->second) |
1233 | 34 | resolveHeaderDirectives(M, File); |
1234 | 18 | LazyHeadersByModTime.erase(ByModTime); |
1235 | 18 | } |
1236 | 11.4M | } |
1237 | | |
1238 | | void ModuleMap::resolveHeaderDirectives( |
1239 | 67.2k | Module *Mod, std::optional<const FileEntry *> File) const { |
1240 | 67.2k | bool NeedsFramework = false; |
1241 | 67.2k | SmallVector<Module::UnresolvedHeaderDirective, 1> NewHeaders; |
1242 | 67.2k | const auto Size = File ? (*File)->getSize()35 : 067.2k ; |
1243 | 67.2k | const auto ModTime = File ? (*File)->getModificationTime()35 : 067.2k ; |
1244 | | |
1245 | 67.2k | for (auto &Header : Mod->UnresolvedHeaders) { |
1246 | 61 | if (File && (14 (14 Header.ModTime14 && Header.ModTime != ModTime12 ) || |
1247 | 14 | (13 Header.Size13 && Header.Size != Size13 ))) |
1248 | 13 | NewHeaders.push_back(Header); |
1249 | 48 | else |
1250 | | // This operation is logically const; we're just changing how we represent |
1251 | | // the header information for this file. |
1252 | 48 | const_cast<ModuleMap *>(this)->resolveHeader(Mod, Header, NeedsFramework); |
1253 | 61 | } |
1254 | 67.2k | Mod->UnresolvedHeaders.swap(NewHeaders); |
1255 | 67.2k | } |
1256 | | |
1257 | | void ModuleMap::addHeader(Module *Mod, Module::Header Header, |
1258 | 2.38M | ModuleHeaderRole Role, bool Imported) { |
1259 | 2.38M | KnownHeader KH(Mod, Role); |
1260 | | |
1261 | | // Only add each header to the headers list once. |
1262 | | // FIXME: Should we diagnose if a header is listed twice in the |
1263 | | // same module definition? |
1264 | 2.38M | auto &HeaderList = Headers[Header.Entry]; |
1265 | 2.38M | if (llvm::is_contained(HeaderList, KH)) |
1266 | 376k | return; |
1267 | | |
1268 | 2.00M | HeaderList.push_back(KH); |
1269 | 2.00M | Mod->Headers[headerRoleToKind(Role)].push_back(Header); |
1270 | | |
1271 | 2.00M | bool isCompilingModuleHeader = Mod->isForBuilding(LangOpts); |
1272 | 2.00M | if (!Imported || isCompilingModuleHeader596 ) { |
1273 | | // When we import HeaderFileInfo, the external source is expected to |
1274 | | // set the isModuleHeader flag itself. |
1275 | 2.00M | HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, |
1276 | 2.00M | isCompilingModuleHeader); |
1277 | 2.00M | } |
1278 | | |
1279 | | // Notify callbacks that we just added a new header. |
1280 | 2.00M | for (const auto &Cb : Callbacks) |
1281 | 14.6k | Cb->moduleMapAddHeader(Header.Entry.getName()); |
1282 | 2.00M | } |
1283 | | |
1284 | | OptionalFileEntryRef |
1285 | 544k | ModuleMap::getContainingModuleMapFile(const Module *Module) const { |
1286 | 544k | if (Module->DefinitionLoc.isInvalid()) |
1287 | 5.57k | return std::nullopt; |
1288 | | |
1289 | 539k | return SourceMgr.getFileEntryRefForID( |
1290 | 539k | SourceMgr.getFileID(Module->DefinitionLoc)); |
1291 | 544k | } |
1292 | | |
1293 | | OptionalFileEntryRef |
1294 | 520k | ModuleMap::getModuleMapFileForUniquing(const Module *M) const { |
1295 | 520k | if (M->IsInferred) { |
1296 | 4.93k | assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); |
1297 | 4.93k | return InferredModuleAllowedBy.find(M)->second; |
1298 | 4.93k | } |
1299 | 515k | return getContainingModuleMapFile(M); |
1300 | 520k | } |
1301 | | |
1302 | | void ModuleMap::setInferredModuleAllowedBy(Module *M, |
1303 | 108 | OptionalFileEntryRef ModMap) { |
1304 | 108 | assert(M->IsInferred && "module not inferred"); |
1305 | 108 | InferredModuleAllowedBy[M] = ModMap; |
1306 | 108 | } |
1307 | | |
1308 | | std::error_code |
1309 | 12.0k | ModuleMap::canonicalizeModuleMapPath(SmallVectorImpl<char> &Path) { |
1310 | 12.0k | StringRef Dir = llvm::sys::path::parent_path({Path.data(), Path.size()}); |
1311 | | |
1312 | | // Do not canonicalize within the framework; the module map parser expects |
1313 | | // Modules/ not Versions/A/Modules. |
1314 | 12.0k | if (llvm::sys::path::filename(Dir) == "Modules") { |
1315 | 380 | StringRef Parent = llvm::sys::path::parent_path(Dir); |
1316 | 380 | if (Parent.endswith(".framework")) |
1317 | 378 | Dir = Parent; |
1318 | 380 | } |
1319 | | |
1320 | 12.0k | FileManager &FM = SourceMgr.getFileManager(); |
1321 | 12.0k | auto DirEntry = FM.getDirectoryRef(Dir.empty() ? "."3 : Dir12.0k ); |
1322 | 12.0k | if (!DirEntry) |
1323 | 0 | return llvm::errorToErrorCode(DirEntry.takeError()); |
1324 | | |
1325 | | // Canonicalize the directory. |
1326 | 12.0k | StringRef CanonicalDir = FM.getCanonicalName(*DirEntry); |
1327 | 12.0k | if (CanonicalDir != Dir) |
1328 | 575 | llvm::sys::path::replace_path_prefix(Path, Dir, CanonicalDir); |
1329 | | |
1330 | | // In theory, the filename component should also be canonicalized if it |
1331 | | // on a case-insensitive filesystem. However, the extra canonicalization is |
1332 | | // expensive and if clang looked up the filename it will always be lowercase. |
1333 | | |
1334 | | // Remove ., remove redundant separators, and switch to native separators. |
1335 | | // This is needed for separators between CanonicalDir and the filename. |
1336 | 12.0k | llvm::sys::path::remove_dots(Path); |
1337 | | |
1338 | 12.0k | return std::error_code(); |
1339 | 12.0k | } |
1340 | | |
1341 | | void ModuleMap::addAdditionalModuleMapFile(const Module *M, |
1342 | 343 | FileEntryRef ModuleMap) { |
1343 | 343 | AdditionalModMaps[M].insert(ModuleMap); |
1344 | 343 | } |
1345 | | |
1346 | 0 | LLVM_DUMP_METHOD void ModuleMap::dump() { |
1347 | 0 | llvm::errs() << "Modules:"; |
1348 | 0 | for (llvm::StringMap<Module *>::iterator M = Modules.begin(), |
1349 | 0 | MEnd = Modules.end(); |
1350 | 0 | M != MEnd; ++M) |
1351 | 0 | M->getValue()->print(llvm::errs(), 2); |
1352 | |
|
1353 | 0 | llvm::errs() << "Headers:"; |
1354 | 0 | for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); |
1355 | 0 | H != HEnd; ++H) { |
1356 | 0 | llvm::errs() << " \"" << H->first->getName() << "\" -> "; |
1357 | 0 | for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), |
1358 | 0 | E = H->second.end(); |
1359 | 0 | I != E; ++I) { |
1360 | 0 | if (I != H->second.begin()) |
1361 | 0 | llvm::errs() << ","; |
1362 | 0 | llvm::errs() << I->getModule()->getFullModuleName(); |
1363 | 0 | } |
1364 | 0 | llvm::errs() << "\n"; |
1365 | 0 | } |
1366 | 0 | } |
1367 | | |
1368 | 13.0k | bool ModuleMap::resolveExports(Module *Mod, bool Complain) { |
1369 | 13.0k | auto Unresolved = std::move(Mod->UnresolvedExports); |
1370 | 13.0k | Mod->UnresolvedExports.clear(); |
1371 | 13.0k | for (auto &UE : Unresolved) { |
1372 | 5.12k | Module::ExportDecl Export = resolveExport(Mod, UE, Complain); |
1373 | 5.12k | if (Export.getPointer() || Export.getInt()4.61k ) |
1374 | 4.92k | Mod->Exports.push_back(Export); |
1375 | 196 | else |
1376 | 196 | Mod->UnresolvedExports.push_back(UE); |
1377 | 5.12k | } |
1378 | 13.0k | return !Mod->UnresolvedExports.empty(); |
1379 | 13.0k | } |
1380 | | |
1381 | 76.7k | bool ModuleMap::resolveUses(Module *Mod, bool Complain) { |
1382 | 76.7k | auto Unresolved = std::move(Mod->UnresolvedDirectUses); |
1383 | 76.7k | Mod->UnresolvedDirectUses.clear(); |
1384 | 76.7k | for (auto &UDU : Unresolved) { |
1385 | 82 | Module *DirectUse = resolveModuleId(UDU, Mod, Complain); |
1386 | 82 | if (DirectUse) |
1387 | 74 | Mod->DirectUses.push_back(DirectUse); |
1388 | 8 | else |
1389 | 8 | Mod->UnresolvedDirectUses.push_back(UDU); |
1390 | 82 | } |
1391 | 76.7k | return !Mod->UnresolvedDirectUses.empty(); |
1392 | 76.7k | } |
1393 | | |
1394 | 13.0k | bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { |
1395 | 13.0k | auto Unresolved = std::move(Mod->UnresolvedConflicts); |
1396 | 13.0k | Mod->UnresolvedConflicts.clear(); |
1397 | 13.0k | for (auto &UC : Unresolved) { |
1398 | 1 | if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) { |
1399 | 1 | Module::Conflict Conflict; |
1400 | 1 | Conflict.Other = OtherMod; |
1401 | 1 | Conflict.Message = UC.Message; |
1402 | 1 | Mod->Conflicts.push_back(Conflict); |
1403 | 1 | } else |
1404 | 0 | Mod->UnresolvedConflicts.push_back(UC); |
1405 | 1 | } |
1406 | 13.0k | return !Mod->UnresolvedConflicts.empty(); |
1407 | 13.0k | } |
1408 | | |
1409 | | //----------------------------------------------------------------------------// |
1410 | | // Module map file parser |
1411 | | //----------------------------------------------------------------------------// |
1412 | | |
1413 | | namespace clang { |
1414 | | |
1415 | | /// A token in a module map file. |
1416 | | struct MMToken { |
1417 | | enum TokenKind { |
1418 | | Comma, |
1419 | | ConfigMacros, |
1420 | | Conflict, |
1421 | | EndOfFile, |
1422 | | HeaderKeyword, |
1423 | | Identifier, |
1424 | | Exclaim, |
1425 | | ExcludeKeyword, |
1426 | | ExplicitKeyword, |
1427 | | ExportKeyword, |
1428 | | ExportAsKeyword, |
1429 | | ExternKeyword, |
1430 | | FrameworkKeyword, |
1431 | | LinkKeyword, |
1432 | | ModuleKeyword, |
1433 | | Period, |
1434 | | PrivateKeyword, |
1435 | | UmbrellaKeyword, |
1436 | | UseKeyword, |
1437 | | RequiresKeyword, |
1438 | | Star, |
1439 | | StringLiteral, |
1440 | | IntegerLiteral, |
1441 | | TextualKeyword, |
1442 | | LBrace, |
1443 | | RBrace, |
1444 | | LSquare, |
1445 | | RSquare |
1446 | | } Kind; |
1447 | | |
1448 | | SourceLocation::UIntTy Location; |
1449 | | unsigned StringLength; |
1450 | | union { |
1451 | | // If Kind != IntegerLiteral. |
1452 | | const char *StringData; |
1453 | | |
1454 | | // If Kind == IntegerLiteral. |
1455 | | uint64_t IntegerValue; |
1456 | | }; |
1457 | | |
1458 | 18.7M | void clear() { |
1459 | 18.7M | Kind = EndOfFile; |
1460 | 18.7M | Location = 0; |
1461 | 18.7M | StringLength = 0; |
1462 | 18.7M | StringData = nullptr; |
1463 | 18.7M | } |
1464 | | |
1465 | 37.0M | bool is(TokenKind K) const { return Kind == K; } |
1466 | | |
1467 | 21.0M | SourceLocation getLocation() const { |
1468 | 21.0M | return SourceLocation::getFromRawEncoding(Location); |
1469 | 21.0M | } |
1470 | | |
1471 | 92 | uint64_t getInteger() const { |
1472 | 92 | return Kind == IntegerLiteral ? IntegerValue : 00 ; |
1473 | 92 | } |
1474 | | |
1475 | 5.86M | StringRef getString() const { |
1476 | 5.86M | return Kind == IntegerLiteral ? StringRef()0 |
1477 | 5.86M | : StringRef(StringData, StringLength); |
1478 | 5.86M | } |
1479 | | }; |
1480 | | |
1481 | | class ModuleMapParser { |
1482 | | Lexer &L; |
1483 | | SourceManager &SourceMgr; |
1484 | | |
1485 | | /// Default target information, used only for string literal |
1486 | | /// parsing. |
1487 | | const TargetInfo *Target; |
1488 | | |
1489 | | DiagnosticsEngine &Diags; |
1490 | | ModuleMap ⤅ |
1491 | | |
1492 | | /// The current module map file. |
1493 | | FileEntryRef ModuleMapFile; |
1494 | | |
1495 | | /// Source location of most recent parsed module declaration |
1496 | | SourceLocation CurrModuleDeclLoc; |
1497 | | |
1498 | | /// The directory that file names in this module map file should |
1499 | | /// be resolved relative to. |
1500 | | DirectoryEntryRef Directory; |
1501 | | |
1502 | | /// Whether this module map is in a system header directory. |
1503 | | bool IsSystem; |
1504 | | |
1505 | | /// Whether an error occurred. |
1506 | | bool HadError = false; |
1507 | | |
1508 | | /// Stores string data for the various string literals referenced |
1509 | | /// during parsing. |
1510 | | llvm::BumpPtrAllocator StringData; |
1511 | | |
1512 | | /// The current token. |
1513 | | MMToken Tok; |
1514 | | |
1515 | | /// The active module. |
1516 | | Module *ActiveModule = nullptr; |
1517 | | |
1518 | | /// Whether a module uses the 'requires excluded' hack to mark its |
1519 | | /// contents as 'textual'. |
1520 | | /// |
1521 | | /// On older Darwin SDK versions, 'requires excluded' is used to mark the |
1522 | | /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as |
1523 | | /// non-modular headers. For backwards compatibility, we continue to |
1524 | | /// support this idiom for just these modules, and map the headers to |
1525 | | /// 'textual' to match the original intent. |
1526 | | llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack; |
1527 | | |
1528 | | /// Consume the current token and return its location. |
1529 | | SourceLocation consumeToken(); |
1530 | | |
1531 | | /// Skip tokens until we reach the a token with the given kind |
1532 | | /// (or the end of the file). |
1533 | | void skipUntil(MMToken::TokenKind K); |
1534 | | |
1535 | | using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>; |
1536 | | |
1537 | | bool parseModuleId(ModuleId &Id); |
1538 | | void parseModuleDecl(); |
1539 | | void parseExternModuleDecl(); |
1540 | | void parseRequiresDecl(); |
1541 | | void parseHeaderDecl(MMToken::TokenKind, SourceLocation LeadingLoc); |
1542 | | void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); |
1543 | | void parseExportDecl(); |
1544 | | void parseExportAsDecl(); |
1545 | | void parseUseDecl(); |
1546 | | void parseLinkDecl(); |
1547 | | void parseConfigMacros(); |
1548 | | void parseConflict(); |
1549 | | void parseInferredModuleDecl(bool Framework, bool Explicit); |
1550 | | |
1551 | | /// Private modules are canonicalized as Foo_Private. Clang provides extra |
1552 | | /// module map search logic to find the appropriate private module when PCH |
1553 | | /// is used with implicit module maps. Warn when private modules are written |
1554 | | /// in other ways (FooPrivate and Foo.Private), providing notes and fixits. |
1555 | | void diagnosePrivateModules(SourceLocation ExplicitLoc, |
1556 | | SourceLocation FrameworkLoc); |
1557 | | |
1558 | | using Attributes = ModuleMap::Attributes; |
1559 | | |
1560 | | bool parseOptionalAttributes(Attributes &Attrs); |
1561 | | |
1562 | | public: |
1563 | | explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, |
1564 | | const TargetInfo *Target, DiagnosticsEngine &Diags, |
1565 | | ModuleMap &Map, FileEntryRef ModuleMapFile, |
1566 | | DirectoryEntryRef Directory, bool IsSystem) |
1567 | 10.7k | : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), |
1568 | 10.7k | ModuleMapFile(ModuleMapFile), Directory(Directory), |
1569 | 10.7k | IsSystem(IsSystem) { |
1570 | 10.7k | Tok.clear(); |
1571 | 10.7k | consumeToken(); |
1572 | 10.7k | } |
1573 | | |
1574 | | bool parseModuleMapFile(); |
1575 | | |
1576 | 0 | bool terminatedByDirective() { return false; } |
1577 | 3.24k | SourceLocation getLocation() { return Tok.getLocation(); } |
1578 | | }; |
1579 | | |
1580 | | } // namespace clang |
1581 | | |
1582 | 18.7M | SourceLocation ModuleMapParser::consumeToken() { |
1583 | 18.7M | SourceLocation Result = Tok.getLocation(); |
1584 | | |
1585 | 18.7M | retry: |
1586 | 18.7M | Tok.clear(); |
1587 | 18.7M | Token LToken; |
1588 | 18.7M | L.LexFromRawLexer(LToken); |
1589 | 18.7M | Tok.Location = LToken.getLocation().getRawEncoding(); |
1590 | 18.7M | switch (LToken.getKind()) { |
1591 | 9.03M | case tok::raw_identifier: { |
1592 | 9.03M | StringRef RI = LToken.getRawIdentifier(); |
1593 | 9.03M | Tok.StringData = RI.data(); |
1594 | 9.03M | Tok.StringLength = RI.size(); |
1595 | 9.03M | Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) |
1596 | 9.03M | .Case("config_macros", MMToken::ConfigMacros) |
1597 | 9.03M | .Case("conflict", MMToken::Conflict) |
1598 | 9.03M | .Case("exclude", MMToken::ExcludeKeyword) |
1599 | 9.03M | .Case("explicit", MMToken::ExplicitKeyword) |
1600 | 9.03M | .Case("export", MMToken::ExportKeyword) |
1601 | 9.03M | .Case("export_as", MMToken::ExportAsKeyword) |
1602 | 9.03M | .Case("extern", MMToken::ExternKeyword) |
1603 | 9.03M | .Case("framework", MMToken::FrameworkKeyword) |
1604 | 9.03M | .Case("header", MMToken::HeaderKeyword) |
1605 | 9.03M | .Case("link", MMToken::LinkKeyword) |
1606 | 9.03M | .Case("module", MMToken::ModuleKeyword) |
1607 | 9.03M | .Case("private", MMToken::PrivateKeyword) |
1608 | 9.03M | .Case("requires", MMToken::RequiresKeyword) |
1609 | 9.03M | .Case("textual", MMToken::TextualKeyword) |
1610 | 9.03M | .Case("umbrella", MMToken::UmbrellaKeyword) |
1611 | 9.03M | .Case("use", MMToken::UseKeyword) |
1612 | 9.03M | .Default(MMToken::Identifier); |
1613 | 9.03M | break; |
1614 | 0 | } |
1615 | | |
1616 | 2.28k | case tok::comma: |
1617 | 2.28k | Tok.Kind = MMToken::Comma; |
1618 | 2.28k | break; |
1619 | | |
1620 | 10.6k | case tok::eof: |
1621 | 10.6k | Tok.Kind = MMToken::EndOfFile; |
1622 | 10.6k | break; |
1623 | | |
1624 | 2.00M | case tok::l_brace: |
1625 | 2.00M | Tok.Kind = MMToken::LBrace; |
1626 | 2.00M | break; |
1627 | | |
1628 | 1.45M | case tok::l_square: |
1629 | 1.45M | Tok.Kind = MMToken::LSquare; |
1630 | 1.45M | break; |
1631 | | |
1632 | 43.2k | case tok::period: |
1633 | 43.2k | Tok.Kind = MMToken::Period; |
1634 | 43.2k | break; |
1635 | | |
1636 | 2.00M | case tok::r_brace: |
1637 | 2.00M | Tok.Kind = MMToken::RBrace; |
1638 | 2.00M | break; |
1639 | | |
1640 | 1.45M | case tok::r_square: |
1641 | 1.45M | Tok.Kind = MMToken::RSquare; |
1642 | 1.45M | break; |
1643 | | |
1644 | 689k | case tok::star: |
1645 | 689k | Tok.Kind = MMToken::Star; |
1646 | 689k | break; |
1647 | | |
1648 | 2.87k | case tok::exclaim: |
1649 | 2.87k | Tok.Kind = MMToken::Exclaim; |
1650 | 2.87k | break; |
1651 | | |
1652 | 2.01M | case tok::string_literal: { |
1653 | 2.01M | if (LToken.hasUDSuffix()) { |
1654 | 0 | Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); |
1655 | 0 | HadError = true; |
1656 | 0 | goto retry; |
1657 | 0 | } |
1658 | | |
1659 | | // Parse the string literal. |
1660 | 2.01M | LangOptions LangOpts; |
1661 | 2.01M | StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); |
1662 | 2.01M | if (StringLiteral.hadError) |
1663 | 0 | goto retry; |
1664 | | |
1665 | | // Copy the string literal into our string data allocator. |
1666 | 2.01M | unsigned Length = StringLiteral.GetStringLength(); |
1667 | 2.01M | char *Saved = StringData.Allocate<char>(Length + 1); |
1668 | 2.01M | memcpy(Saved, StringLiteral.GetString().data(), Length); |
1669 | 2.01M | Saved[Length] = 0; |
1670 | | |
1671 | | // Form the token. |
1672 | 2.01M | Tok.Kind = MMToken::StringLiteral; |
1673 | 2.01M | Tok.StringData = Saved; |
1674 | 2.01M | Tok.StringLength = Length; |
1675 | 2.01M | break; |
1676 | 2.01M | } |
1677 | | |
1678 | 92 | case tok::numeric_constant: { |
1679 | | // We don't support any suffixes or other complications. |
1680 | 92 | SmallString<32> SpellingBuffer; |
1681 | 92 | SpellingBuffer.resize(LToken.getLength() + 1); |
1682 | 92 | const char *Start = SpellingBuffer.data(); |
1683 | 92 | unsigned Length = |
1684 | 92 | Lexer::getSpelling(LToken, Start, SourceMgr, Map.LangOpts); |
1685 | 92 | uint64_t Value; |
1686 | 92 | if (StringRef(Start, Length).getAsInteger(0, Value)) { |
1687 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token); |
1688 | 0 | HadError = true; |
1689 | 0 | goto retry; |
1690 | 0 | } |
1691 | | |
1692 | 92 | Tok.Kind = MMToken::IntegerLiteral; |
1693 | 92 | Tok.IntegerValue = Value; |
1694 | 92 | break; |
1695 | 92 | } |
1696 | | |
1697 | 0 | case tok::comment: |
1698 | 0 | goto retry; |
1699 | | |
1700 | 92 | case tok::hash: |
1701 | | // A module map can be terminated prematurely by |
1702 | | // #pragma clang module contents |
1703 | | // When building the module, we'll treat the rest of the file as the |
1704 | | // contents of the module. |
1705 | 92 | { |
1706 | 368 | auto NextIsIdent = [&](StringRef Str) -> bool { |
1707 | 368 | L.LexFromRawLexer(LToken); |
1708 | 368 | return !LToken.isAtStartOfLine() && LToken.is(tok::raw_identifier) && |
1709 | 368 | LToken.getRawIdentifier() == Str; |
1710 | 368 | }; |
1711 | 92 | if (NextIsIdent("pragma") && NextIsIdent("clang") && |
1712 | 92 | NextIsIdent("module") && NextIsIdent("contents")) { |
1713 | 92 | Tok.Kind = MMToken::EndOfFile; |
1714 | 92 | break; |
1715 | 92 | } |
1716 | 92 | } |
1717 | 92 | [[fallthrough]];0 |
1718 | | |
1719 | 0 | default: |
1720 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token); |
1721 | 0 | HadError = true; |
1722 | 0 | goto retry; |
1723 | 18.7M | } |
1724 | | |
1725 | 18.7M | return Result; |
1726 | 18.7M | } |
1727 | | |
1728 | 209 | void ModuleMapParser::skipUntil(MMToken::TokenKind K) { |
1729 | 209 | unsigned braceDepth = 0; |
1730 | 209 | unsigned squareDepth = 0; |
1731 | 1.45k | do { |
1732 | 1.45k | switch (Tok.Kind) { |
1733 | 1 | case MMToken::EndOfFile: |
1734 | 1 | return; |
1735 | | |
1736 | 96 | case MMToken::LBrace: |
1737 | 96 | if (Tok.is(K) && braceDepth == 00 && squareDepth == 00 ) |
1738 | 0 | return; |
1739 | | |
1740 | 96 | ++braceDepth; |
1741 | 96 | break; |
1742 | | |
1743 | 0 | case MMToken::LSquare: |
1744 | 0 | if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) |
1745 | 0 | return; |
1746 | | |
1747 | 0 | ++squareDepth; |
1748 | 0 | break; |
1749 | | |
1750 | 304 | case MMToken::RBrace: |
1751 | 304 | if (braceDepth > 0) |
1752 | 96 | --braceDepth; |
1753 | 208 | else if (Tok.is(K)) |
1754 | 208 | return; |
1755 | 96 | break; |
1756 | | |
1757 | 96 | case MMToken::RSquare: |
1758 | 0 | if (squareDepth > 0) |
1759 | 0 | --squareDepth; |
1760 | 0 | else if (Tok.is(K)) |
1761 | 0 | return; |
1762 | 0 | break; |
1763 | | |
1764 | 1.04k | default: |
1765 | 1.04k | if (braceDepth == 0 && squareDepth == 0741 && Tok.is(K)741 ) |
1766 | 0 | return; |
1767 | 1.04k | break; |
1768 | 1.45k | } |
1769 | | |
1770 | 1.24k | consumeToken(); |
1771 | 1.24k | } while (true); |
1772 | 209 | } |
1773 | | |
1774 | | /// Parse a module-id. |
1775 | | /// |
1776 | | /// module-id: |
1777 | | /// identifier |
1778 | | /// identifier '.' module-id |
1779 | | /// |
1780 | | /// \returns true if an error occurred, false otherwise. |
1781 | 1.98M | bool ModuleMapParser::parseModuleId(ModuleId &Id) { |
1782 | 1.98M | Id.clear(); |
1783 | 1.98M | do { |
1784 | 1.98M | if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)627 ) { |
1785 | 1.98M | Id.push_back( |
1786 | 1.98M | std::make_pair(std::string(Tok.getString()), Tok.getLocation())); |
1787 | 1.98M | consumeToken(); |
1788 | 1.98M | } else { |
1789 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); |
1790 | 0 | return true; |
1791 | 0 | } |
1792 | | |
1793 | 1.98M | if (!Tok.is(MMToken::Period)) |
1794 | 1.98M | break; |
1795 | | |
1796 | 347 | consumeToken(); |
1797 | 347 | } while (true); |
1798 | | |
1799 | 1.98M | return false; |
1800 | 1.98M | } |
1801 | | |
1802 | | namespace { |
1803 | | |
1804 | | /// Enumerates the known attributes. |
1805 | | enum AttributeKind { |
1806 | | /// An unknown attribute. |
1807 | | AT_unknown, |
1808 | | |
1809 | | /// The 'system' attribute. |
1810 | | AT_system, |
1811 | | |
1812 | | /// The 'extern_c' attribute. |
1813 | | AT_extern_c, |
1814 | | |
1815 | | /// The 'exhaustive' attribute. |
1816 | | AT_exhaustive, |
1817 | | |
1818 | | /// The 'no_undeclared_includes' attribute. |
1819 | | AT_no_undeclared_includes |
1820 | | }; |
1821 | | |
1822 | | } // namespace |
1823 | | |
1824 | | /// Private modules are canonicalized as Foo_Private. Clang provides extra |
1825 | | /// module map search logic to find the appropriate private module when PCH |
1826 | | /// is used with implicit module maps. Warn when private modules are written |
1827 | | /// in other ways (FooPrivate and Foo.Private), providing notes and fixits. |
1828 | | void ModuleMapParser::diagnosePrivateModules(SourceLocation ExplicitLoc, |
1829 | 149 | SourceLocation FrameworkLoc) { |
1830 | 149 | auto GenNoteAndFixIt = [&](StringRef BadName, StringRef Canonical, |
1831 | 149 | const Module *M, SourceRange ReplLoc) { |
1832 | 72 | auto D = Diags.Report(ActiveModule->DefinitionLoc, |
1833 | 72 | diag::note_mmap_rename_top_level_private_module); |
1834 | 72 | D << BadName << M->Name; |
1835 | 72 | D << FixItHint::CreateReplacement(ReplLoc, Canonical); |
1836 | 72 | }; |
1837 | | |
1838 | 1.47k | for (auto E = Map.module_begin(); E != Map.module_end(); ++E1.32k ) { |
1839 | 1.32k | auto const *M = E->getValue(); |
1840 | 1.32k | if (M->Directory != ActiveModule->Directory) |
1841 | 1.09k | continue; |
1842 | | |
1843 | 227 | SmallString<128> FullName(ActiveModule->getFullModuleName()); |
1844 | 227 | if (!FullName.startswith(M->Name) && !FullName.endswith("Private")39 ) |
1845 | 0 | continue; |
1846 | 227 | SmallString<128> FixedPrivModDecl; |
1847 | 227 | SmallString<128> Canonical(M->Name); |
1848 | 227 | Canonical.append("_Private"); |
1849 | | |
1850 | | // Foo.Private -> Foo_Private |
1851 | 227 | if (ActiveModule->Parent && ActiveModule->Name == "Private"140 && !M->Parent100 && |
1852 | 227 | M->Name == ActiveModule->Parent->Name100 ) { |
1853 | 64 | Diags.Report(ActiveModule->DefinitionLoc, |
1854 | 64 | diag::warn_mmap_mismatched_private_submodule) |
1855 | 64 | << FullName; |
1856 | | |
1857 | 64 | SourceLocation FixItInitBegin = CurrModuleDeclLoc; |
1858 | 64 | if (FrameworkLoc.isValid()) |
1859 | 14 | FixItInitBegin = FrameworkLoc; |
1860 | 64 | if (ExplicitLoc.isValid()) |
1861 | 56 | FixItInitBegin = ExplicitLoc; |
1862 | | |
1863 | 64 | if (FrameworkLoc.isValid() || ActiveModule->Parent->IsFramework50 ) |
1864 | 64 | FixedPrivModDecl.append("framework "); |
1865 | 64 | FixedPrivModDecl.append("module "); |
1866 | 64 | FixedPrivModDecl.append(Canonical); |
1867 | | |
1868 | 64 | GenNoteAndFixIt(FullName, FixedPrivModDecl, M, |
1869 | 64 | SourceRange(FixItInitBegin, ActiveModule->DefinitionLoc)); |
1870 | 64 | continue; |
1871 | 64 | } |
1872 | | |
1873 | | // FooPrivate and whatnots -> Foo_Private |
1874 | 163 | if (!ActiveModule->Parent && !M->Parent87 && M->Name != ActiveModule->Name87 && |
1875 | 163 | ActiveModule->Name != Canonical41 ) { |
1876 | 8 | Diags.Report(ActiveModule->DefinitionLoc, |
1877 | 8 | diag::warn_mmap_mismatched_private_module_name) |
1878 | 8 | << ActiveModule->Name; |
1879 | 8 | GenNoteAndFixIt(ActiveModule->Name, Canonical, M, |
1880 | 8 | SourceRange(ActiveModule->DefinitionLoc)); |
1881 | 8 | } |
1882 | 163 | } |
1883 | 149 | } |
1884 | | |
1885 | | /// Parse a module declaration. |
1886 | | /// |
1887 | | /// module-declaration: |
1888 | | /// 'extern' 'module' module-id string-literal |
1889 | | /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] |
1890 | | /// { module-member* } |
1891 | | /// |
1892 | | /// module-member: |
1893 | | /// requires-declaration |
1894 | | /// header-declaration |
1895 | | /// submodule-declaration |
1896 | | /// export-declaration |
1897 | | /// export-as-declaration |
1898 | | /// link-declaration |
1899 | | /// |
1900 | | /// submodule-declaration: |
1901 | | /// module-declaration |
1902 | | /// inferred-submodule-declaration |
1903 | 2.00M | void ModuleMapParser::parseModuleDecl() { |
1904 | 2.00M | assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || |
1905 | 2.00M | Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); |
1906 | 2.00M | if (Tok.is(MMToken::ExternKeyword)) { |
1907 | 254 | parseExternModuleDecl(); |
1908 | 254 | return; |
1909 | 254 | } |
1910 | | |
1911 | | // Parse 'explicit' or 'framework' keyword, if present. |
1912 | 2.00M | SourceLocation ExplicitLoc; |
1913 | 2.00M | SourceLocation FrameworkLoc; |
1914 | 2.00M | bool Explicit = false; |
1915 | 2.00M | bool Framework = false; |
1916 | | |
1917 | | // Parse 'explicit' keyword, if present. |
1918 | 2.00M | if (Tok.is(MMToken::ExplicitKeyword)) { |
1919 | 62.5k | ExplicitLoc = consumeToken(); |
1920 | 62.5k | Explicit = true; |
1921 | 62.5k | } |
1922 | | |
1923 | | // Parse 'framework' keyword, if present. |
1924 | 2.00M | if (Tok.is(MMToken::FrameworkKeyword)) { |
1925 | 3.21k | FrameworkLoc = consumeToken(); |
1926 | 3.21k | Framework = true; |
1927 | 3.21k | } |
1928 | | |
1929 | | // Parse 'module' keyword. |
1930 | 2.00M | if (!Tok.is(MMToken::ModuleKeyword)) { |
1931 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
1932 | 0 | consumeToken(); |
1933 | 0 | HadError = true; |
1934 | 0 | return; |
1935 | 0 | } |
1936 | 2.00M | CurrModuleDeclLoc = consumeToken(); // 'module' keyword |
1937 | | |
1938 | | // If we have a wildcard for the module name, this is an inferred submodule. |
1939 | | // Parse it. |
1940 | 2.00M | if (Tok.is(MMToken::Star)) |
1941 | 12.7k | return parseInferredModuleDecl(Framework, Explicit); |
1942 | | |
1943 | | // Parse the module name. |
1944 | 1.98M | ModuleId Id; |
1945 | 1.98M | if (parseModuleId(Id)) { |
1946 | 0 | HadError = true; |
1947 | 0 | return; |
1948 | 0 | } |
1949 | | |
1950 | 1.98M | if (ActiveModule) { |
1951 | 476k | if (Id.size() > 1) { |
1952 | 0 | Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) |
1953 | 0 | << SourceRange(Id.front().second, Id.back().second); |
1954 | |
|
1955 | 0 | HadError = true; |
1956 | 0 | return; |
1957 | 0 | } |
1958 | 1.51M | } else if (Id.size() == 1 && Explicit1.51M ) { |
1959 | | // Top-level modules can't be explicit. |
1960 | 0 | Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); |
1961 | 0 | Explicit = false; |
1962 | 0 | ExplicitLoc = SourceLocation(); |
1963 | 0 | HadError = true; |
1964 | 0 | } |
1965 | | |
1966 | 1.98M | Module *PreviousActiveModule = ActiveModule; |
1967 | 1.98M | if (Id.size() > 1) { |
1968 | | // This module map defines a submodule. Go find the module of which it |
1969 | | // is a submodule. |
1970 | 347 | ActiveModule = nullptr; |
1971 | 347 | const Module *TopLevelModule = nullptr; |
1972 | 697 | for (unsigned I = 0, N = Id.size() - 1; I != N; ++I350 ) { |
1973 | 350 | if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { |
1974 | 348 | if (I == 0) |
1975 | 346 | TopLevelModule = Next; |
1976 | 348 | ActiveModule = Next; |
1977 | 348 | continue; |
1978 | 348 | } |
1979 | | |
1980 | 2 | Diags.Report(Id[I].second, diag::err_mmap_missing_parent_module) |
1981 | 2 | << Id[I].first << (ActiveModule != nullptr) |
1982 | 2 | << (ActiveModule |
1983 | 2 | ? ActiveModule->getTopLevelModule()->getFullModuleName()1 |
1984 | 2 | : ""1 ); |
1985 | 2 | HadError = true; |
1986 | 2 | } |
1987 | | |
1988 | 347 | if (TopLevelModule && |
1989 | 347 | ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)346 ) { |
1990 | 343 | assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && |
1991 | 343 | "submodule defined in same file as 'module *' that allowed its " |
1992 | 343 | "top-level module"); |
1993 | 343 | Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); |
1994 | 343 | } |
1995 | 347 | } |
1996 | | |
1997 | 1.98M | StringRef ModuleName = Id.back().first; |
1998 | 1.98M | SourceLocation ModuleNameLoc = Id.back().second; |
1999 | | |
2000 | | // Parse the optional attribute list. |
2001 | 1.98M | Attributes Attrs; |
2002 | 1.98M | if (parseOptionalAttributes(Attrs)) |
2003 | 1 | return; |
2004 | | |
2005 | | // Parse the opening brace. |
2006 | 1.98M | if (!Tok.is(MMToken::LBrace)) { |
2007 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) |
2008 | 0 | << ModuleName; |
2009 | 0 | HadError = true; |
2010 | 0 | return; |
2011 | 0 | } |
2012 | 1.98M | SourceLocation LBraceLoc = consumeToken(); |
2013 | | |
2014 | | // Determine whether this (sub)module has already been defined. |
2015 | 1.98M | Module *ShadowingModule = nullptr; |
2016 | 1.98M | if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { |
2017 | | // We might see a (re)definition of a module that we already have a |
2018 | | // definition for in four cases: |
2019 | | // - If we loaded one definition from an AST file and we've just found a |
2020 | | // corresponding definition in a module map file, or |
2021 | 209 | bool LoadedFromASTFile = Existing->IsFromModuleFile; |
2022 | | // - If we previously inferred this module from different module map file. |
2023 | 209 | bool Inferred = Existing->IsInferred; |
2024 | | // - If we're building a framework that vends a module map, we might've |
2025 | | // previously seen the one in intermediate products and now the system |
2026 | | // one. |
2027 | | // FIXME: If we're parsing module map file that looks like this: |
2028 | | // framework module FW { ... } |
2029 | | // module FW.Sub { ... } |
2030 | | // We can't check the framework qualifier, since it's not attached to |
2031 | | // the definition of Sub. Checking that qualifier on \c Existing is |
2032 | | // not correct either, since we might've previously seen: |
2033 | | // module FW { ... } |
2034 | | // module FW.Sub { ... } |
2035 | | // We should enforce consistency of redefinitions so that we can rely |
2036 | | // that \c Existing is part of a framework iff the redefinition of FW |
2037 | | // we have just skipped had it too. Once we do that, stop checking |
2038 | | // the local framework qualifier and only rely on \c Existing. |
2039 | 209 | bool PartOfFramework = Framework || Existing->isPartOfFramework()205 ; |
2040 | | // - If we're building a (preprocessed) module and we've just loaded the |
2041 | | // module map file from which it was created. |
2042 | 209 | bool ParsedAsMainInput = |
2043 | 209 | Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap && |
2044 | 209 | Map.LangOpts.CurrentModule == ModuleName4 && |
2045 | 209 | SourceMgr.getDecomposedLoc(ModuleNameLoc).first != |
2046 | 2 | SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first; |
2047 | 209 | if (LoadedFromASTFile || Inferred192 || PartOfFramework192 || ParsedAsMainInput191 ) { |
2048 | 20 | ActiveModule = PreviousActiveModule; |
2049 | | // Skip the module definition. |
2050 | 20 | skipUntil(MMToken::RBrace); |
2051 | 20 | if (Tok.is(MMToken::RBrace)) |
2052 | 20 | consumeToken(); |
2053 | 0 | else { |
2054 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
2055 | 0 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
2056 | 0 | HadError = true; |
2057 | 0 | } |
2058 | 20 | return; |
2059 | 20 | } |
2060 | | |
2061 | 189 | if (!Existing->Parent && Map.mayShadowNewModule(Existing)) { |
2062 | 3 | ShadowingModule = Existing; |
2063 | 186 | } else { |
2064 | | // This is not a shawdowed module decl, it is an illegal redefinition. |
2065 | 186 | Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) |
2066 | 186 | << ModuleName; |
2067 | 186 | Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); |
2068 | | |
2069 | | // Skip the module definition. |
2070 | 186 | skipUntil(MMToken::RBrace); |
2071 | 186 | if (Tok.is(MMToken::RBrace)) |
2072 | 186 | consumeToken(); |
2073 | | |
2074 | 186 | HadError = true; |
2075 | 186 | return; |
2076 | 186 | } |
2077 | 189 | } |
2078 | | |
2079 | | // Start defining this module. |
2080 | 1.98M | if (ShadowingModule) { |
2081 | 3 | ActiveModule = |
2082 | 3 | Map.createShadowedModule(ModuleName, Framework, ShadowingModule); |
2083 | 1.98M | } else { |
2084 | 1.98M | ActiveModule = |
2085 | 1.98M | Map.findOrCreateModule(ModuleName, ActiveModule, Framework, Explicit) |
2086 | 1.98M | .first; |
2087 | 1.98M | } |
2088 | | |
2089 | 1.98M | ActiveModule->DefinitionLoc = ModuleNameLoc; |
2090 | 1.98M | if (Attrs.IsSystem || IsSystem559k ) |
2091 | 1.86M | ActiveModule->IsSystem = true; |
2092 | 1.98M | if (Attrs.IsExternC) |
2093 | 21.9k | ActiveModule->IsExternC = true; |
2094 | 1.98M | if (Attrs.NoUndeclaredIncludes) |
2095 | 1.02k | ActiveModule->NoUndeclaredIncludes = true; |
2096 | 1.98M | ActiveModule->Directory = Directory; |
2097 | | |
2098 | 1.98M | StringRef MapFileName(ModuleMapFile.getName()); |
2099 | 1.98M | if (MapFileName.endswith("module.private.modulemap") || |
2100 | 1.98M | MapFileName.endswith("module_private.map")1.98M ) { |
2101 | 292 | ActiveModule->ModuleMapIsPrivate = true; |
2102 | 292 | } |
2103 | | |
2104 | | // Private modules named as FooPrivate, Foo.Private or similar are likely a |
2105 | | // user error; provide warnings, notes and fixits to direct users to use |
2106 | | // Foo_Private instead. |
2107 | 1.98M | SourceLocation StartLoc = |
2108 | 1.98M | SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
2109 | 1.98M | if (Map.HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps && |
2110 | 1.98M | !Diags.isIgnored(diag::warn_mmap_mismatched_private_submodule, |
2111 | 1.98M | StartLoc) && |
2112 | 1.98M | !Diags.isIgnored(diag::warn_mmap_mismatched_private_module_name, |
2113 | 574k | StartLoc) && |
2114 | 1.98M | ActiveModule->ModuleMapIsPrivate574k ) |
2115 | 149 | diagnosePrivateModules(ExplicitLoc, FrameworkLoc); |
2116 | | |
2117 | 1.98M | bool Done = false; |
2118 | 5.51M | do { |
2119 | 5.51M | switch (Tok.Kind) { |
2120 | 0 | case MMToken::EndOfFile: |
2121 | 1.98M | case MMToken::RBrace: |
2122 | 1.98M | Done = true; |
2123 | 1.98M | break; |
2124 | | |
2125 | 624 | case MMToken::ConfigMacros: |
2126 | 624 | parseConfigMacros(); |
2127 | 624 | break; |
2128 | | |
2129 | 3 | case MMToken::Conflict: |
2130 | 3 | parseConflict(); |
2131 | 3 | break; |
2132 | | |
2133 | 62.4k | case MMToken::ExplicitKeyword: |
2134 | 62.6k | case MMToken::ExternKeyword: |
2135 | 63.3k | case MMToken::FrameworkKeyword: |
2136 | 488k | case MMToken::ModuleKeyword: |
2137 | 488k | parseModuleDecl(); |
2138 | 488k | break; |
2139 | | |
2140 | 995k | case MMToken::ExportKeyword: |
2141 | 995k | parseExportDecl(); |
2142 | 995k | break; |
2143 | | |
2144 | 22 | case MMToken::ExportAsKeyword: |
2145 | 22 | parseExportAsDecl(); |
2146 | 22 | break; |
2147 | | |
2148 | 372 | case MMToken::UseKeyword: |
2149 | 372 | parseUseDecl(); |
2150 | 372 | break; |
2151 | | |
2152 | 28.7k | case MMToken::RequiresKeyword: |
2153 | 28.7k | parseRequiresDecl(); |
2154 | 28.7k | break; |
2155 | | |
2156 | 53.8k | case MMToken::TextualKeyword: |
2157 | 53.8k | parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); |
2158 | 53.8k | break; |
2159 | | |
2160 | 12.5k | case MMToken::UmbrellaKeyword: { |
2161 | 12.5k | SourceLocation UmbrellaLoc = consumeToken(); |
2162 | 12.5k | if (Tok.is(MMToken::HeaderKeyword)) |
2163 | 3.72k | parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); |
2164 | 8.84k | else |
2165 | 8.84k | parseUmbrellaDirDecl(UmbrellaLoc); |
2166 | 12.5k | break; |
2167 | 63.3k | } |
2168 | | |
2169 | 12.0k | case MMToken::ExcludeKeyword: |
2170 | 12.0k | parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); |
2171 | 12.0k | break; |
2172 | | |
2173 | 89 | case MMToken::PrivateKeyword: |
2174 | 89 | parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); |
2175 | 89 | break; |
2176 | | |
2177 | 1.93M | case MMToken::HeaderKeyword: |
2178 | 1.93M | parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); |
2179 | 1.93M | break; |
2180 | | |
2181 | 8.11k | case MMToken::LinkKeyword: |
2182 | 8.11k | parseLinkDecl(); |
2183 | 8.11k | break; |
2184 | | |
2185 | 3 | default: |
2186 | 3 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); |
2187 | 3 | consumeToken(); |
2188 | 3 | break; |
2189 | 5.51M | } |
2190 | 5.51M | } while (!Done5.51M ); |
2191 | | |
2192 | 1.98M | if (Tok.is(MMToken::RBrace)) |
2193 | 1.98M | consumeToken(); |
2194 | 18.4E | else { |
2195 | 18.4E | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
2196 | 18.4E | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
2197 | 18.4E | HadError = true; |
2198 | 18.4E | } |
2199 | | |
2200 | | // If the active module is a top-level framework, and there are no link |
2201 | | // libraries, automatically link against the framework. |
2202 | 1.98M | if (ActiveModule->IsFramework && !ActiveModule->isSubFramework()2.52k && |
2203 | 1.98M | ActiveModule->LinkLibraries.empty()1.70k ) |
2204 | 1.61k | inferFrameworkLink(ActiveModule); |
2205 | | |
2206 | | // If the module meets all requirements but is still unavailable, mark the |
2207 | | // whole tree as unavailable to prevent it from building. |
2208 | 1.98M | if (!ActiveModule->IsAvailable && !ActiveModule->IsUnimportable25.4k && |
2209 | 1.98M | ActiveModule->Parent2.47k ) { |
2210 | 726 | ActiveModule->getTopLevelModule()->markUnavailable(/*Unimportable=*/false); |
2211 | 726 | ActiveModule->getTopLevelModule()->MissingHeaders.append( |
2212 | 726 | ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); |
2213 | 726 | } |
2214 | | |
2215 | | // We're done parsing this module. Pop back to the previous module. |
2216 | 1.98M | ActiveModule = PreviousActiveModule; |
2217 | 1.98M | } |
2218 | | |
2219 | | /// Parse an extern module declaration. |
2220 | | /// |
2221 | | /// extern module-declaration: |
2222 | | /// 'extern' 'module' module-id string-literal |
2223 | 254 | void ModuleMapParser::parseExternModuleDecl() { |
2224 | 254 | assert(Tok.is(MMToken::ExternKeyword)); |
2225 | 254 | SourceLocation ExternLoc = consumeToken(); // 'extern' keyword |
2226 | | |
2227 | | // Parse 'module' keyword. |
2228 | 254 | if (!Tok.is(MMToken::ModuleKeyword)) { |
2229 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
2230 | 0 | consumeToken(); |
2231 | 0 | HadError = true; |
2232 | 0 | return; |
2233 | 0 | } |
2234 | 254 | consumeToken(); // 'module' keyword |
2235 | | |
2236 | | // Parse the module name. |
2237 | 254 | ModuleId Id; |
2238 | 254 | if (parseModuleId(Id)) { |
2239 | 0 | HadError = true; |
2240 | 0 | return; |
2241 | 0 | } |
2242 | | |
2243 | | // Parse the referenced module map file name. |
2244 | 254 | if (!Tok.is(MMToken::StringLiteral)) { |
2245 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); |
2246 | 0 | HadError = true; |
2247 | 0 | return; |
2248 | 0 | } |
2249 | 254 | std::string FileName = std::string(Tok.getString()); |
2250 | 254 | consumeToken(); // filename |
2251 | | |
2252 | 254 | StringRef FileNameRef = FileName; |
2253 | 254 | SmallString<128> ModuleMapFileName; |
2254 | 254 | if (llvm::sys::path::is_relative(FileNameRef)) { |
2255 | 254 | ModuleMapFileName += Directory.getName(); |
2256 | 254 | llvm::sys::path::append(ModuleMapFileName, FileName); |
2257 | 254 | FileNameRef = ModuleMapFileName; |
2258 | 254 | } |
2259 | 254 | if (auto File = SourceMgr.getFileManager().getOptionalFileRef(FileNameRef)) |
2260 | 254 | Map.parseModuleMapFile( |
2261 | 254 | *File, IsSystem, |
2262 | 254 | Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd |
2263 | 254 | ? Directory8 |
2264 | 254 | : File->getDir()246 , |
2265 | 254 | FileID(), nullptr, ExternLoc); |
2266 | 254 | } |
2267 | | |
2268 | | /// Whether to add the requirement \p Feature to the module \p M. |
2269 | | /// |
2270 | | /// This preserves backwards compatibility for two hacks in the Darwin system |
2271 | | /// module map files: |
2272 | | /// |
2273 | | /// 1. The use of 'requires excluded' to make headers non-modular, which |
2274 | | /// should really be mapped to 'textual' now that we have this feature. We |
2275 | | /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to |
2276 | | /// true. Later, this bit will be used to map all the headers inside this |
2277 | | /// module to 'textual'. |
2278 | | /// |
2279 | | /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private. |
2280 | | /// |
2281 | | /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement |
2282 | | /// was never correct and causes issues now that we check it, so drop it. |
2283 | | static bool shouldAddRequirement(Module *M, StringRef Feature, |
2284 | 30.3k | bool &IsRequiresExcludedHack) { |
2285 | 30.3k | if (Feature == "excluded" && |
2286 | 30.3k | (92 M->fullModuleNameIs({"Darwin", "C", "excluded"})92 || |
2287 | 92 | M->fullModuleNameIs({"Tcl", "Private"})46 )) { |
2288 | 92 | IsRequiresExcludedHack = true; |
2289 | 92 | return false; |
2290 | 30.2k | } else if (Feature == "cplusplus" && M->fullModuleNameIs({"IOKit", "avc"})2.42k ) { |
2291 | 46 | return false; |
2292 | 46 | } |
2293 | | |
2294 | 30.2k | return true; |
2295 | 30.3k | } |
2296 | | |
2297 | | /// Parse a requires declaration. |
2298 | | /// |
2299 | | /// requires-declaration: |
2300 | | /// 'requires' feature-list |
2301 | | /// |
2302 | | /// feature-list: |
2303 | | /// feature ',' feature-list |
2304 | | /// feature |
2305 | | /// |
2306 | | /// feature: |
2307 | | /// '!'[opt] identifier |
2308 | 28.7k | void ModuleMapParser::parseRequiresDecl() { |
2309 | 28.7k | assert(Tok.is(MMToken::RequiresKeyword)); |
2310 | | |
2311 | | // Parse 'requires' keyword. |
2312 | 28.7k | consumeToken(); |
2313 | | |
2314 | | // Parse the feature-list. |
2315 | 30.3k | do { |
2316 | 30.3k | bool RequiredState = true; |
2317 | 30.3k | if (Tok.is(MMToken::Exclaim)) { |
2318 | 2.87k | RequiredState = false; |
2319 | 2.87k | consumeToken(); |
2320 | 2.87k | } |
2321 | | |
2322 | 30.3k | if (!Tok.is(MMToken::Identifier)) { |
2323 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); |
2324 | 0 | HadError = true; |
2325 | 0 | return; |
2326 | 0 | } |
2327 | | |
2328 | | // Consume the feature name. |
2329 | 30.3k | std::string Feature = std::string(Tok.getString()); |
2330 | 30.3k | consumeToken(); |
2331 | | |
2332 | 30.3k | bool IsRequiresExcludedHack = false; |
2333 | 30.3k | bool ShouldAddRequirement = |
2334 | 30.3k | shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack); |
2335 | | |
2336 | 30.3k | if (IsRequiresExcludedHack) |
2337 | 92 | UsesRequiresExcludedHack.insert(ActiveModule); |
2338 | | |
2339 | 30.3k | if (ShouldAddRequirement) { |
2340 | | // Add this feature. |
2341 | 30.2k | ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts, |
2342 | 30.2k | *Map.Target); |
2343 | 30.2k | } |
2344 | | |
2345 | 30.3k | if (!Tok.is(MMToken::Comma)) |
2346 | 28.7k | break; |
2347 | | |
2348 | | // Consume the comma. |
2349 | 1.65k | consumeToken(); |
2350 | 1.65k | } while (true); |
2351 | 28.7k | } |
2352 | | |
2353 | | /// Parse a header declaration. |
2354 | | /// |
2355 | | /// header-declaration: |
2356 | | /// 'textual'[opt] 'header' string-literal |
2357 | | /// 'private' 'textual'[opt] 'header' string-literal |
2358 | | /// 'exclude' 'header' string-literal |
2359 | | /// 'umbrella' 'header' string-literal |
2360 | | /// |
2361 | | /// FIXME: Support 'private textual header'. |
2362 | | void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, |
2363 | 1.99M | SourceLocation LeadingLoc) { |
2364 | | // We've already consumed the first token. |
2365 | 1.99M | ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; |
2366 | | |
2367 | 1.99M | if (LeadingToken == MMToken::PrivateKeyword) { |
2368 | 89 | Role = ModuleMap::PrivateHeader; |
2369 | | // 'private' may optionally be followed by 'textual'. |
2370 | 89 | if (Tok.is(MMToken::TextualKeyword)) { |
2371 | 21 | LeadingToken = Tok.Kind; |
2372 | 21 | consumeToken(); |
2373 | 21 | } |
2374 | 1.99M | } else if (LeadingToken == MMToken::ExcludeKeyword) { |
2375 | 12.0k | Role = ModuleMap::ExcludedHeader; |
2376 | 12.0k | } |
2377 | | |
2378 | 1.99M | if (LeadingToken == MMToken::TextualKeyword) |
2379 | 53.9k | Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); |
2380 | | |
2381 | 1.99M | if (UsesRequiresExcludedHack.count(ActiveModule)) { |
2382 | | // Mark this header 'textual' (see doc comment for |
2383 | | // Module::UsesRequiresExcludedHack). |
2384 | 46 | Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); |
2385 | 46 | } |
2386 | | |
2387 | 1.99M | if (LeadingToken != MMToken::HeaderKeyword) { |
2388 | 69.7k | if (!Tok.is(MMToken::HeaderKeyword)) { |
2389 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
2390 | 0 | << (LeadingToken == MMToken::PrivateKeyword ? "private" : |
2391 | 0 | LeadingToken == MMToken::ExcludeKeyword ? "exclude" : |
2392 | 0 | LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); |
2393 | 0 | return; |
2394 | 0 | } |
2395 | 69.7k | consumeToken(); |
2396 | 69.7k | } |
2397 | | |
2398 | | // Parse the header name. |
2399 | 1.99M | if (!Tok.is(MMToken::StringLiteral)) { |
2400 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
2401 | 0 | << "header"; |
2402 | 0 | HadError = true; |
2403 | 0 | return; |
2404 | 0 | } |
2405 | 1.99M | Module::UnresolvedHeaderDirective Header; |
2406 | 1.99M | Header.FileName = std::string(Tok.getString()); |
2407 | 1.99M | Header.FileNameLoc = consumeToken(); |
2408 | 1.99M | Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; |
2409 | 1.99M | Header.Kind = Map.headerRoleToKind(Role); |
2410 | | |
2411 | | // Check whether we already have an umbrella. |
2412 | 1.99M | if (Header.IsUmbrella && ActiveModule->Umbrella3.72k ) { |
2413 | 0 | Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) |
2414 | 0 | << ActiveModule->getFullModuleName(); |
2415 | 0 | HadError = true; |
2416 | 0 | return; |
2417 | 0 | } |
2418 | | |
2419 | | // If we were given stat information, parse it so we can skip looking for |
2420 | | // the file. |
2421 | 1.99M | if (Tok.is(MMToken::LBrace)) { |
2422 | 53 | SourceLocation LBraceLoc = consumeToken(); |
2423 | | |
2424 | 147 | while (!Tok.is(MMToken::RBrace) && !Tok.is(MMToken::EndOfFile)94 ) { |
2425 | 94 | enum Attribute { Size, ModTime, Unknown }; |
2426 | 94 | StringRef Str = Tok.getString(); |
2427 | 94 | SourceLocation Loc = consumeToken(); |
2428 | 94 | switch (llvm::StringSwitch<Attribute>(Str) |
2429 | 94 | .Case("size", Size) |
2430 | 94 | .Case("mtime", ModTime) |
2431 | 94 | .Default(Unknown)) { |
2432 | 49 | case Size: |
2433 | 49 | if (Header.Size) |
2434 | 1 | Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str; |
2435 | 49 | if (!Tok.is(MMToken::IntegerLiteral)) { |
2436 | 1 | Diags.Report(Tok.getLocation(), |
2437 | 1 | diag::err_mmap_invalid_header_attribute_value) << Str; |
2438 | 1 | skipUntil(MMToken::RBrace); |
2439 | 1 | break; |
2440 | 1 | } |
2441 | 48 | Header.Size = Tok.getInteger(); |
2442 | 48 | consumeToken(); |
2443 | 48 | break; |
2444 | | |
2445 | 44 | case ModTime: |
2446 | 44 | if (Header.ModTime) |
2447 | 0 | Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str; |
2448 | 44 | if (!Tok.is(MMToken::IntegerLiteral)) { |
2449 | 0 | Diags.Report(Tok.getLocation(), |
2450 | 0 | diag::err_mmap_invalid_header_attribute_value) << Str; |
2451 | 0 | skipUntil(MMToken::RBrace); |
2452 | 0 | break; |
2453 | 0 | } |
2454 | 44 | Header.ModTime = Tok.getInteger(); |
2455 | 44 | consumeToken(); |
2456 | 44 | break; |
2457 | | |
2458 | 1 | case Unknown: |
2459 | 1 | Diags.Report(Loc, diag::err_mmap_expected_header_attribute); |
2460 | 1 | skipUntil(MMToken::RBrace); |
2461 | 1 | break; |
2462 | 94 | } |
2463 | 94 | } |
2464 | | |
2465 | 53 | if (Tok.is(MMToken::RBrace)) |
2466 | 53 | consumeToken(); |
2467 | 0 | else { |
2468 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
2469 | 0 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
2470 | 0 | HadError = true; |
2471 | 0 | } |
2472 | 53 | } |
2473 | | |
2474 | 1.99M | bool NeedsFramework = false; |
2475 | 1.99M | Map.addUnresolvedHeader(ActiveModule, std::move(Header), NeedsFramework); |
2476 | | |
2477 | 1.99M | if (NeedsFramework) |
2478 | 2 | Diags.Report(CurrModuleDeclLoc, diag::note_mmap_add_framework_keyword) |
2479 | 2 | << ActiveModule->getFullModuleName() |
2480 | 2 | << FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module"); |
2481 | 1.99M | } |
2482 | | |
2483 | | static int compareModuleHeaders(const Module::Header *A, |
2484 | 552 | const Module::Header *B) { |
2485 | 552 | return A->NameAsWritten.compare(B->NameAsWritten); |
2486 | 552 | } |
2487 | | |
2488 | | /// Parse an umbrella directory declaration. |
2489 | | /// |
2490 | | /// umbrella-dir-declaration: |
2491 | | /// umbrella string-literal |
2492 | 8.84k | void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { |
2493 | | // Parse the directory name. |
2494 | 8.84k | if (!Tok.is(MMToken::StringLiteral)) { |
2495 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) |
2496 | 0 | << "umbrella"; |
2497 | 0 | HadError = true; |
2498 | 0 | return; |
2499 | 0 | } |
2500 | | |
2501 | 8.84k | std::string DirName = std::string(Tok.getString()); |
2502 | 8.84k | std::string DirNameAsWritten = DirName; |
2503 | 8.84k | SourceLocation DirNameLoc = consumeToken(); |
2504 | | |
2505 | | // Check whether we already have an umbrella. |
2506 | 8.84k | if (ActiveModule->Umbrella) { |
2507 | 0 | Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) |
2508 | 0 | << ActiveModule->getFullModuleName(); |
2509 | 0 | HadError = true; |
2510 | 0 | return; |
2511 | 0 | } |
2512 | | |
2513 | | // Look for this file. |
2514 | 8.84k | OptionalDirectoryEntryRef Dir; |
2515 | 8.84k | if (llvm::sys::path::is_absolute(DirName)) { |
2516 | 0 | Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName); |
2517 | 8.84k | } else { |
2518 | 8.84k | SmallString<128> PathName; |
2519 | 8.84k | PathName = Directory.getName(); |
2520 | 8.84k | llvm::sys::path::append(PathName, DirName); |
2521 | 8.84k | Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(PathName); |
2522 | 8.84k | } |
2523 | | |
2524 | 8.84k | if (!Dir) { |
2525 | 0 | Diags.Report(DirNameLoc, diag::warn_mmap_umbrella_dir_not_found) |
2526 | 0 | << DirName; |
2527 | 0 | return; |
2528 | 0 | } |
2529 | | |
2530 | 8.84k | if (UsesRequiresExcludedHack.count(ActiveModule)) { |
2531 | | // Mark this header 'textual' (see doc comment for |
2532 | | // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the |
2533 | | // directory is relatively expensive, in practice this only applies to the |
2534 | | // uncommonly used Tcl module on Darwin platforms. |
2535 | 46 | std::error_code EC; |
2536 | 46 | SmallVector<Module::Header, 6> Headers; |
2537 | 46 | llvm::vfs::FileSystem &FS = |
2538 | 46 | SourceMgr.getFileManager().getVirtualFileSystem(); |
2539 | 46 | for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E; |
2540 | 552 | I != E && !EC506 ; I.increment(EC)506 ) { |
2541 | 506 | if (auto FE = SourceMgr.getFileManager().getOptionalFileRef(I->path())) { |
2542 | 460 | Module::Header Header = {"", std::string(I->path()), *FE}; |
2543 | 460 | Headers.push_back(std::move(Header)); |
2544 | 460 | } |
2545 | 506 | } |
2546 | | |
2547 | | // Sort header paths so that the pcm doesn't depend on iteration order. |
2548 | 46 | llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders); |
2549 | | |
2550 | 46 | for (auto &Header : Headers) |
2551 | 460 | Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader); |
2552 | 46 | return; |
2553 | 46 | } |
2554 | | |
2555 | 8.79k | if (Module *OwningModule = Map.UmbrellaDirs[*Dir]) { |
2556 | 0 | Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) |
2557 | 0 | << OwningModule->getFullModuleName(); |
2558 | 0 | HadError = true; |
2559 | 0 | return; |
2560 | 0 | } |
2561 | | |
2562 | | // Record this umbrella directory. |
2563 | 8.79k | Map.setUmbrellaDirAsWritten(ActiveModule, *Dir, DirNameAsWritten, DirName); |
2564 | 8.79k | } |
2565 | | |
2566 | | /// Parse a module export declaration. |
2567 | | /// |
2568 | | /// export-declaration: |
2569 | | /// 'export' wildcard-module-id |
2570 | | /// |
2571 | | /// wildcard-module-id: |
2572 | | /// identifier |
2573 | | /// '*' |
2574 | | /// identifier '.' wildcard-module-id |
2575 | 995k | void ModuleMapParser::parseExportDecl() { |
2576 | 995k | assert(Tok.is(MMToken::ExportKeyword)); |
2577 | 995k | SourceLocation ExportLoc = consumeToken(); |
2578 | | |
2579 | | // Parse the module-id with an optional wildcard at the end. |
2580 | 995k | ModuleId ParsedModuleId; |
2581 | 995k | bool Wildcard = false; |
2582 | 1.03M | do { |
2583 | | // FIXME: Support string-literal module names here. |
2584 | 1.03M | if (Tok.is(MMToken::Identifier)) { |
2585 | 374k | ParsedModuleId.push_back( |
2586 | 374k | std::make_pair(std::string(Tok.getString()), Tok.getLocation())); |
2587 | 374k | consumeToken(); |
2588 | | |
2589 | 374k | if (Tok.is(MMToken::Period)) { |
2590 | 42.9k | consumeToken(); |
2591 | 42.9k | continue; |
2592 | 42.9k | } |
2593 | | |
2594 | 331k | break; |
2595 | 374k | } |
2596 | | |
2597 | 664k | if(Tok.is(MMToken::Star)) { |
2598 | 664k | Wildcard = true; |
2599 | 664k | consumeToken(); |
2600 | 664k | break; |
2601 | 664k | } |
2602 | | |
2603 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); |
2604 | 0 | HadError = true; |
2605 | 0 | return; |
2606 | 664k | } while (true42.9k ); |
2607 | | |
2608 | 995k | Module::UnresolvedExportDecl Unresolved = { |
2609 | 995k | ExportLoc, ParsedModuleId, Wildcard |
2610 | 995k | }; |
2611 | 995k | ActiveModule->UnresolvedExports.push_back(Unresolved); |
2612 | 995k | } |
2613 | | |
2614 | | /// Parse a module export_as declaration. |
2615 | | /// |
2616 | | /// export-as-declaration: |
2617 | | /// 'export_as' identifier |
2618 | 22 | void ModuleMapParser::parseExportAsDecl() { |
2619 | 22 | assert(Tok.is(MMToken::ExportAsKeyword)); |
2620 | 22 | consumeToken(); |
2621 | | |
2622 | 22 | if (!Tok.is(MMToken::Identifier)) { |
2623 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); |
2624 | 0 | HadError = true; |
2625 | 0 | return; |
2626 | 0 | } |
2627 | | |
2628 | 22 | if (ActiveModule->Parent) { |
2629 | 1 | Diags.Report(Tok.getLocation(), diag::err_mmap_submodule_export_as); |
2630 | 1 | consumeToken(); |
2631 | 1 | return; |
2632 | 1 | } |
2633 | | |
2634 | 21 | if (!ActiveModule->ExportAsModule.empty()) { |
2635 | 2 | if (ActiveModule->ExportAsModule == Tok.getString()) { |
2636 | 1 | Diags.Report(Tok.getLocation(), diag::warn_mmap_redundant_export_as) |
2637 | 1 | << ActiveModule->Name << Tok.getString(); |
2638 | 1 | } else { |
2639 | 1 | Diags.Report(Tok.getLocation(), diag::err_mmap_conflicting_export_as) |
2640 | 1 | << ActiveModule->Name << ActiveModule->ExportAsModule |
2641 | 1 | << Tok.getString(); |
2642 | 1 | } |
2643 | 2 | } |
2644 | | |
2645 | 21 | ActiveModule->ExportAsModule = std::string(Tok.getString()); |
2646 | 21 | Map.addLinkAsDependency(ActiveModule); |
2647 | | |
2648 | 21 | consumeToken(); |
2649 | 21 | } |
2650 | | |
2651 | | /// Parse a module use declaration. |
2652 | | /// |
2653 | | /// use-declaration: |
2654 | | /// 'use' wildcard-module-id |
2655 | 372 | void ModuleMapParser::parseUseDecl() { |
2656 | 372 | assert(Tok.is(MMToken::UseKeyword)); |
2657 | 372 | auto KWLoc = consumeToken(); |
2658 | | // Parse the module-id. |
2659 | 372 | ModuleId ParsedModuleId; |
2660 | 372 | parseModuleId(ParsedModuleId); |
2661 | | |
2662 | 372 | if (ActiveModule->Parent) |
2663 | 1 | Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule); |
2664 | 371 | else |
2665 | 371 | ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); |
2666 | 372 | } |
2667 | | |
2668 | | /// Parse a link declaration. |
2669 | | /// |
2670 | | /// module-declaration: |
2671 | | /// 'link' 'framework'[opt] string-literal |
2672 | 8.11k | void ModuleMapParser::parseLinkDecl() { |
2673 | 8.11k | assert(Tok.is(MMToken::LinkKeyword)); |
2674 | 8.11k | SourceLocation LinkLoc = consumeToken(); |
2675 | | |
2676 | | // Parse the optional 'framework' keyword. |
2677 | 8.11k | bool IsFramework = false; |
2678 | 8.11k | if (Tok.is(MMToken::FrameworkKeyword)) { |
2679 | 714 | consumeToken(); |
2680 | 714 | IsFramework = true; |
2681 | 714 | } |
2682 | | |
2683 | | // Parse the library name |
2684 | 8.11k | if (!Tok.is(MMToken::StringLiteral)) { |
2685 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) |
2686 | 0 | << IsFramework << SourceRange(LinkLoc); |
2687 | 0 | HadError = true; |
2688 | 0 | return; |
2689 | 0 | } |
2690 | | |
2691 | 8.11k | std::string LibraryName = std::string(Tok.getString()); |
2692 | 8.11k | consumeToken(); |
2693 | 8.11k | ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, |
2694 | 8.11k | IsFramework)); |
2695 | 8.11k | } |
2696 | | |
2697 | | /// Parse a configuration macro declaration. |
2698 | | /// |
2699 | | /// module-declaration: |
2700 | | /// 'config_macros' attributes[opt] config-macro-list? |
2701 | | /// |
2702 | | /// config-macro-list: |
2703 | | /// identifier (',' identifier)? |
2704 | 624 | void ModuleMapParser::parseConfigMacros() { |
2705 | 624 | assert(Tok.is(MMToken::ConfigMacros)); |
2706 | 624 | SourceLocation ConfigMacrosLoc = consumeToken(); |
2707 | | |
2708 | | // Only top-level modules can have configuration macros. |
2709 | 624 | if (ActiveModule->Parent) { |
2710 | 0 | Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); |
2711 | 0 | } |
2712 | | |
2713 | | // Parse the optional attributes. |
2714 | 624 | Attributes Attrs; |
2715 | 624 | if (parseOptionalAttributes(Attrs)) |
2716 | 0 | return; |
2717 | | |
2718 | 624 | if (Attrs.IsExhaustive && !ActiveModule->Parent) { |
2719 | 624 | ActiveModule->ConfigMacrosExhaustive = true; |
2720 | 624 | } |
2721 | | |
2722 | | // If we don't have an identifier, we're done. |
2723 | | // FIXME: Support macros with the same name as a keyword here. |
2724 | 624 | if (!Tok.is(MMToken::Identifier)) |
2725 | 0 | return; |
2726 | | |
2727 | | // Consume the first identifier. |
2728 | 624 | if (!ActiveModule->Parent) { |
2729 | 624 | ActiveModule->ConfigMacros.push_back(Tok.getString().str()); |
2730 | 624 | } |
2731 | 624 | consumeToken(); |
2732 | | |
2733 | 1.24k | do { |
2734 | | // If there's a comma, consume it. |
2735 | 1.24k | if (!Tok.is(MMToken::Comma)) |
2736 | 624 | break; |
2737 | 624 | consumeToken(); |
2738 | | |
2739 | | // We expect to see a macro name here. |
2740 | | // FIXME: Support macros with the same name as a keyword here. |
2741 | 624 | if (!Tok.is(MMToken::Identifier)) { |
2742 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); |
2743 | 0 | break; |
2744 | 0 | } |
2745 | | |
2746 | | // Consume the macro name. |
2747 | 624 | if (!ActiveModule->Parent) { |
2748 | 624 | ActiveModule->ConfigMacros.push_back(Tok.getString().str()); |
2749 | 624 | } |
2750 | 624 | consumeToken(); |
2751 | 624 | } while (true); |
2752 | 624 | } |
2753 | | |
2754 | | /// Format a module-id into a string. |
2755 | 0 | static std::string formatModuleId(const ModuleId &Id) { |
2756 | 0 | std::string result; |
2757 | 0 | { |
2758 | 0 | llvm::raw_string_ostream OS(result); |
2759 | |
|
2760 | 0 | for (unsigned I = 0, N = Id.size(); I != N; ++I) { |
2761 | 0 | if (I) |
2762 | 0 | OS << "."; |
2763 | 0 | OS << Id[I].first; |
2764 | 0 | } |
2765 | 0 | } |
2766 | |
|
2767 | 0 | return result; |
2768 | 0 | } |
2769 | | |
2770 | | /// Parse a conflict declaration. |
2771 | | /// |
2772 | | /// module-declaration: |
2773 | | /// 'conflict' module-id ',' string-literal |
2774 | 3 | void ModuleMapParser::parseConflict() { |
2775 | 3 | assert(Tok.is(MMToken::Conflict)); |
2776 | 3 | SourceLocation ConflictLoc = consumeToken(); |
2777 | 3 | Module::UnresolvedConflict Conflict; |
2778 | | |
2779 | | // Parse the module-id. |
2780 | 3 | if (parseModuleId(Conflict.Id)) |
2781 | 0 | return; |
2782 | | |
2783 | | // Parse the ','. |
2784 | 3 | if (!Tok.is(MMToken::Comma)) { |
2785 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) |
2786 | 0 | << SourceRange(ConflictLoc); |
2787 | 0 | return; |
2788 | 0 | } |
2789 | 3 | consumeToken(); |
2790 | | |
2791 | | // Parse the message. |
2792 | 3 | if (!Tok.is(MMToken::StringLiteral)) { |
2793 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) |
2794 | 0 | << formatModuleId(Conflict.Id); |
2795 | 0 | return; |
2796 | 0 | } |
2797 | 3 | Conflict.Message = Tok.getString().str(); |
2798 | 3 | consumeToken(); |
2799 | | |
2800 | | // Add this unresolved conflict. |
2801 | 3 | ActiveModule->UnresolvedConflicts.push_back(Conflict); |
2802 | 3 | } |
2803 | | |
2804 | | /// Parse an inferred module declaration (wildcard modules). |
2805 | | /// |
2806 | | /// module-declaration: |
2807 | | /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] |
2808 | | /// { inferred-module-member* } |
2809 | | /// |
2810 | | /// inferred-module-member: |
2811 | | /// 'export' '*' |
2812 | | /// 'exclude' identifier |
2813 | 12.7k | void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { |
2814 | 12.7k | assert(Tok.is(MMToken::Star)); |
2815 | 12.7k | SourceLocation StarLoc = consumeToken(); |
2816 | 12.7k | bool Failed = false; |
2817 | | |
2818 | | // Inferred modules must be submodules. |
2819 | 12.7k | if (!ActiveModule && !Framework682 ) { |
2820 | 0 | Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); |
2821 | 0 | Failed = true; |
2822 | 0 | } |
2823 | | |
2824 | 12.7k | if (ActiveModule) { |
2825 | | // Inferred modules must have umbrella directories. |
2826 | 12.0k | if (!Failed && ActiveModule->IsAvailable && |
2827 | 12.0k | !ActiveModule->getEffectiveUmbrellaDir()11.5k ) { |
2828 | 0 | Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); |
2829 | 0 | Failed = true; |
2830 | 0 | } |
2831 | | |
2832 | | // Check for redefinition of an inferred module. |
2833 | 12.0k | if (!Failed && ActiveModule->InferSubmodules) { |
2834 | 0 | Diags.Report(StarLoc, diag::err_mmap_inferred_redef); |
2835 | 0 | if (ActiveModule->InferredSubmoduleLoc.isValid()) |
2836 | 0 | Diags.Report(ActiveModule->InferredSubmoduleLoc, |
2837 | 0 | diag::note_mmap_prev_definition); |
2838 | 0 | Failed = true; |
2839 | 0 | } |
2840 | | |
2841 | | // Check for the 'framework' keyword, which is not permitted here. |
2842 | 12.0k | if (Framework) { |
2843 | 0 | Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); |
2844 | 0 | Framework = false; |
2845 | 0 | } |
2846 | 12.0k | } else if (682 Explicit682 ) { |
2847 | 0 | Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); |
2848 | 0 | Explicit = false; |
2849 | 0 | } |
2850 | | |
2851 | | // If there were any problems with this inferred submodule, skip its body. |
2852 | 12.7k | if (Failed) { |
2853 | 0 | if (Tok.is(MMToken::LBrace)) { |
2854 | 0 | consumeToken(); |
2855 | 0 | skipUntil(MMToken::RBrace); |
2856 | 0 | if (Tok.is(MMToken::RBrace)) |
2857 | 0 | consumeToken(); |
2858 | 0 | } |
2859 | 0 | HadError = true; |
2860 | 0 | return; |
2861 | 0 | } |
2862 | | |
2863 | | // Parse optional attributes. |
2864 | 12.7k | Attributes Attrs; |
2865 | 12.7k | if (parseOptionalAttributes(Attrs)) |
2866 | 0 | return; |
2867 | | |
2868 | 12.7k | if (ActiveModule) { |
2869 | | // Note that we have an inferred submodule. |
2870 | 12.0k | ActiveModule->InferSubmodules = true; |
2871 | 12.0k | ActiveModule->InferredSubmoduleLoc = StarLoc; |
2872 | 12.0k | ActiveModule->InferExplicitSubmodules = Explicit; |
2873 | 12.0k | } else { |
2874 | | // We'll be inferring framework modules for this directory. |
2875 | 682 | Map.InferredDirectories[Directory].InferModules = true; |
2876 | 682 | Map.InferredDirectories[Directory].Attrs = Attrs; |
2877 | 682 | Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; |
2878 | | // FIXME: Handle the 'framework' keyword. |
2879 | 682 | } |
2880 | | |
2881 | | // Parse the opening brace. |
2882 | 12.7k | if (!Tok.is(MMToken::LBrace)) { |
2883 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); |
2884 | 0 | HadError = true; |
2885 | 0 | return; |
2886 | 0 | } |
2887 | 12.7k | SourceLocation LBraceLoc = consumeToken(); |
2888 | | |
2889 | | // Parse the body of the inferred submodule. |
2890 | 12.7k | bool Done = false; |
2891 | 25.4k | do { |
2892 | 25.4k | switch (Tok.Kind) { |
2893 | 0 | case MMToken::EndOfFile: |
2894 | 12.7k | case MMToken::RBrace: |
2895 | 12.7k | Done = true; |
2896 | 12.7k | break; |
2897 | | |
2898 | 624 | case MMToken::ExcludeKeyword: |
2899 | 624 | if (ActiveModule) { |
2900 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
2901 | 0 | << (ActiveModule != nullptr); |
2902 | 0 | consumeToken(); |
2903 | 0 | break; |
2904 | 0 | } |
2905 | | |
2906 | 624 | consumeToken(); |
2907 | | // FIXME: Support string-literal module names here. |
2908 | 624 | if (!Tok.is(MMToken::Identifier)) { |
2909 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); |
2910 | 0 | break; |
2911 | 0 | } |
2912 | | |
2913 | 624 | Map.InferredDirectories[Directory].ExcludedModules.push_back( |
2914 | 624 | std::string(Tok.getString())); |
2915 | 624 | consumeToken(); |
2916 | 624 | break; |
2917 | | |
2918 | 12.0k | case MMToken::ExportKeyword: |
2919 | 12.0k | if (!ActiveModule) { |
2920 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
2921 | 0 | << (ActiveModule != nullptr); |
2922 | 0 | consumeToken(); |
2923 | 0 | break; |
2924 | 0 | } |
2925 | | |
2926 | 12.0k | consumeToken(); |
2927 | 12.0k | if (Tok.is(MMToken::Star)) |
2928 | 12.0k | ActiveModule->InferExportWildcard = true; |
2929 | 0 | else |
2930 | 0 | Diags.Report(Tok.getLocation(), |
2931 | 0 | diag::err_mmap_expected_export_wildcard); |
2932 | 12.0k | consumeToken(); |
2933 | 12.0k | break; |
2934 | | |
2935 | 0 | case MMToken::ExplicitKeyword: |
2936 | 0 | case MMToken::ModuleKeyword: |
2937 | 0 | case MMToken::HeaderKeyword: |
2938 | 0 | case MMToken::PrivateKeyword: |
2939 | 0 | case MMToken::UmbrellaKeyword: |
2940 | 0 | default: |
2941 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) |
2942 | 0 | << (ActiveModule != nullptr); |
2943 | 0 | consumeToken(); |
2944 | 0 | break; |
2945 | 25.4k | } |
2946 | 25.4k | } while (!Done); |
2947 | | |
2948 | 12.7k | if (Tok.is(MMToken::RBrace)) |
2949 | 12.7k | consumeToken(); |
2950 | 0 | else { |
2951 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); |
2952 | 0 | Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); |
2953 | 0 | HadError = true; |
2954 | 0 | } |
2955 | 12.7k | } |
2956 | | |
2957 | | /// Parse optional attributes. |
2958 | | /// |
2959 | | /// attributes: |
2960 | | /// attribute attributes |
2961 | | /// attribute |
2962 | | /// |
2963 | | /// attribute: |
2964 | | /// [ identifier ] |
2965 | | /// |
2966 | | /// \param Attrs Will be filled in with the parsed attributes. |
2967 | | /// |
2968 | | /// \returns true if an error occurred, false otherwise. |
2969 | 2.00M | bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { |
2970 | 2.00M | bool HadError = false; |
2971 | | |
2972 | 3.45M | while (Tok.is(MMToken::LSquare)) { |
2973 | | // Consume the '['. |
2974 | 1.45M | SourceLocation LSquareLoc = consumeToken(); |
2975 | | |
2976 | | // Check whether we have an attribute name here. |
2977 | 1.45M | if (!Tok.is(MMToken::Identifier)) { |
2978 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); |
2979 | 0 | skipUntil(MMToken::RSquare); |
2980 | 0 | if (Tok.is(MMToken::RSquare)) |
2981 | 0 | consumeToken(); |
2982 | 0 | HadError = true; |
2983 | 0 | } |
2984 | | |
2985 | | // Decode the attribute name. |
2986 | 1.45M | AttributeKind Attribute |
2987 | 1.45M | = llvm::StringSwitch<AttributeKind>(Tok.getString()) |
2988 | 1.45M | .Case("exhaustive", AT_exhaustive) |
2989 | 1.45M | .Case("extern_c", AT_extern_c) |
2990 | 1.45M | .Case("no_undeclared_includes", AT_no_undeclared_includes) |
2991 | 1.45M | .Case("system", AT_system) |
2992 | 1.45M | .Default(AT_unknown); |
2993 | 1.45M | switch (Attribute) { |
2994 | 0 | case AT_unknown: |
2995 | 0 | Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) |
2996 | 0 | << Tok.getString(); |
2997 | 0 | break; |
2998 | | |
2999 | 1.42M | case AT_system: |
3000 | 1.42M | Attrs.IsSystem = true; |
3001 | 1.42M | break; |
3002 | | |
3003 | 21.9k | case AT_extern_c: |
3004 | 21.9k | Attrs.IsExternC = true; |
3005 | 21.9k | break; |
3006 | | |
3007 | 624 | case AT_exhaustive: |
3008 | 624 | Attrs.IsExhaustive = true; |
3009 | 624 | break; |
3010 | | |
3011 | 1.02k | case AT_no_undeclared_includes: |
3012 | 1.02k | Attrs.NoUndeclaredIncludes = true; |
3013 | 1.02k | break; |
3014 | 1.45M | } |
3015 | 1.45M | consumeToken(); |
3016 | | |
3017 | | // Consume the ']'. |
3018 | 1.45M | if (!Tok.is(MMToken::RSquare)) { |
3019 | 1 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); |
3020 | 1 | Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); |
3021 | 1 | skipUntil(MMToken::RSquare); |
3022 | 1 | HadError = true; |
3023 | 1 | } |
3024 | | |
3025 | 1.45M | if (Tok.is(MMToken::RSquare)) |
3026 | 1.45M | consumeToken(); |
3027 | 1.45M | } |
3028 | | |
3029 | 2.00M | return HadError; |
3030 | 2.00M | } |
3031 | | |
3032 | | /// Parse a module map file. |
3033 | | /// |
3034 | | /// module-map-file: |
3035 | | /// module-declaration* |
3036 | 10.7k | bool ModuleMapParser::parseModuleMapFile() { |
3037 | 1.52M | do { |
3038 | 1.52M | switch (Tok.Kind) { |
3039 | 10.7k | case MMToken::EndOfFile: |
3040 | 10.7k | return HadError; |
3041 | | |
3042 | 109 | case MMToken::ExplicitKeyword: |
3043 | 149 | case MMToken::ExternKeyword: |
3044 | 1.50M | case MMToken::ModuleKeyword: |
3045 | 1.51M | case MMToken::FrameworkKeyword: |
3046 | 1.51M | parseModuleDecl(); |
3047 | 1.51M | break; |
3048 | | |
3049 | 0 | case MMToken::Comma: |
3050 | 0 | case MMToken::ConfigMacros: |
3051 | 0 | case MMToken::Conflict: |
3052 | 0 | case MMToken::Exclaim: |
3053 | 0 | case MMToken::ExcludeKeyword: |
3054 | 0 | case MMToken::ExportKeyword: |
3055 | 0 | case MMToken::ExportAsKeyword: |
3056 | 0 | case MMToken::HeaderKeyword: |
3057 | 0 | case MMToken::Identifier: |
3058 | 0 | case MMToken::LBrace: |
3059 | 0 | case MMToken::LinkKeyword: |
3060 | 0 | case MMToken::LSquare: |
3061 | 0 | case MMToken::Period: |
3062 | 0 | case MMToken::PrivateKeyword: |
3063 | 0 | case MMToken::RBrace: |
3064 | 0 | case MMToken::RSquare: |
3065 | 0 | case MMToken::RequiresKeyword: |
3066 | 0 | case MMToken::Star: |
3067 | 0 | case MMToken::StringLiteral: |
3068 | 0 | case MMToken::IntegerLiteral: |
3069 | 0 | case MMToken::TextualKeyword: |
3070 | 0 | case MMToken::UmbrellaKeyword: |
3071 | 0 | case MMToken::UseKeyword: |
3072 | 0 | Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); |
3073 | 0 | HadError = true; |
3074 | 0 | consumeToken(); |
3075 | 0 | break; |
3076 | 1.52M | } |
3077 | 1.52M | } while (true1.51M ); |
3078 | 10.7k | } |
3079 | | |
3080 | | bool ModuleMap::parseModuleMapFile(FileEntryRef File, bool IsSystem, |
3081 | | DirectoryEntryRef Dir, FileID ID, |
3082 | | unsigned *Offset, |
3083 | 10.7k | SourceLocation ExternModuleLoc) { |
3084 | 10.7k | assert(Target && "Missing target information"); |
3085 | 10.7k | llvm::DenseMap<const FileEntry *, bool>::iterator Known |
3086 | 10.7k | = ParsedModuleMap.find(File); |
3087 | 10.7k | if (Known != ParsedModuleMap.end()) |
3088 | 19 | return Known->second; |
3089 | | |
3090 | | // If the module map file wasn't already entered, do so now. |
3091 | 10.7k | if (ID.isInvalid()) { |
3092 | 7.45k | auto FileCharacter = |
3093 | 7.45k | IsSystem ? SrcMgr::C_System_ModuleMap5.27k : SrcMgr::C_User_ModuleMap2.18k ; |
3094 | 7.45k | ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter); |
3095 | 7.45k | } |
3096 | | |
3097 | 10.7k | assert(Target && "Missing target information"); |
3098 | 10.7k | std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(ID); |
3099 | 10.7k | if (!Buffer) |
3100 | 0 | return ParsedModuleMap[File] = true; |
3101 | 10.7k | assert((!Offset || *Offset <= Buffer->getBufferSize()) && |
3102 | 10.7k | "invalid buffer offset"); |
3103 | | |
3104 | | // Parse this module map file. |
3105 | 10.7k | Lexer L(SourceMgr.getLocForStartOfFile(ID), MMapLangOpts, |
3106 | 10.7k | Buffer->getBufferStart(), |
3107 | 10.7k | Buffer->getBufferStart() + (Offset ? *Offset3.24k : 07.45k ), |
3108 | 10.7k | Buffer->getBufferEnd()); |
3109 | 10.7k | SourceLocation Start = L.getSourceLocation(); |
3110 | 10.7k | ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, |
3111 | 10.7k | IsSystem); |
3112 | 10.7k | bool Result = Parser.parseModuleMapFile(); |
3113 | 10.7k | ParsedModuleMap[File] = Result; |
3114 | | |
3115 | 10.7k | if (Offset) { |
3116 | 3.24k | auto Loc = SourceMgr.getDecomposedLoc(Parser.getLocation()); |
3117 | 3.24k | assert(Loc.first == ID && "stopped in a different file?"); |
3118 | 3.24k | *Offset = Loc.second; |
3119 | 3.24k | } |
3120 | | |
3121 | | // Notify callbacks that we parsed it. |
3122 | 10.7k | for (const auto &Cb : Callbacks) |
3123 | 354 | Cb->moduleMapFileRead(Start, File, IsSystem); |
3124 | | |
3125 | 10.7k | return Result; |
3126 | 10.7k | } |