Coverage Report

Created: 2019-07-24 05:18

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