Coverage Report

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