Coverage Report

Created: 2021-09-21 08:58

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