Coverage Report

Created: 2021-01-23 06:44

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