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