Coverage Report

Created: 2022-01-18 06:27

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