Bug Summary

File:tools/clang/lib/Lex/ModuleMap.cpp
Warning:line 2706, column 7
Value stored to 'Framework' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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