Coverage Report

Created: 2020-09-22 08:39

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