Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Module.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Module.cpp - Describe a module -------------------------------------===//
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 Module class, which describes a module in the source
10
// code.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Basic/Module.h"
15
#include "clang/Basic/CharInfo.h"
16
#include "clang/Basic/FileManager.h"
17
#include "clang/Basic/LangOptions.h"
18
#include "clang/Basic/SourceLocation.h"
19
#include "clang/Basic/TargetInfo.h"
20
#include "llvm/ADT/ArrayRef.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include "llvm/ADT/StringMap.h"
23
#include "llvm/ADT/StringRef.h"
24
#include "llvm/ADT/StringSwitch.h"
25
#include "llvm/Support/Compiler.h"
26
#include "llvm/Support/ErrorHandling.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include <algorithm>
29
#include <cassert>
30
#include <functional>
31
#include <string>
32
#include <utility>
33
#include <vector>
34
35
using namespace clang;
36
37
Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
38
               bool IsFramework, bool IsExplicit, unsigned VisibilityID)
39
    : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
40
      VisibilityID(VisibilityID), IsUnimportable(false),
41
      HasIncompatibleModuleFile(false), IsAvailable(true),
42
      IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit),
43
      IsSystem(false), IsExternC(false), IsInferred(false),
44
      InferSubmodules(false), InferExplicitSubmodules(false),
45
      InferExportWildcard(false), ConfigMacrosExhaustive(false),
46
      NoUndeclaredIncludes(false), ModuleMapIsPrivate(false),
47
1.93M
      NameVisibility(Hidden) {
48
1.93M
  if (Parent) {
49
1.81M
    IsAvailable = Parent->isAvailable();
50
1.81M
    IsUnimportable = Parent->isUnimportable();
51
1.81M
    IsSystem = Parent->IsSystem;
52
1.81M
    IsExternC = Parent->IsExternC;
53
1.81M
    NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
54
1.81M
    ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
55
56
1.81M
    Parent->SubModuleIndex[Name] = Parent->SubModules.size();
57
1.81M
    Parent->SubModules.push_back(this);
58
1.81M
  }
59
1.93M
}
60
61
1.35M
Module::~Module() {
62
1.35M
  for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
63
2.61M
       I != IEnd; 
++I1.25M
) {
64
1.25M
    delete *I;
65
1.25M
  }
66
1.35M
}
67
68
63.6k
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
69
63.6k
  StringRef Platform = Target.getPlatformName();
70
63.6k
  StringRef Env = Target.getTriple().getEnvironmentName();
71
72
  // Attempt to match platform and environment.
73
63.6k
  if (Platform == Feature || 
Target.getTriple().getOSName() == Feature63.6k
||
74
63.6k
      
Env == Feature63.5k
)
75
58
    return true;
76
77
63.5k
  auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
78
3
    auto Pos = LHS.find('-');
79
3
    if (Pos == StringRef::npos)
80
0
      return false;
81
3
    SmallString<128> NewLHS = LHS.slice(0, Pos);
82
3
    NewLHS += LHS.slice(Pos+1, LHS.size());
83
3
    return NewLHS == RHS;
84
3
  };
85
86
63.5k
  SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
87
  // Darwin has different but equivalent variants for simulators, example:
88
  //   1. x86_64-apple-ios-simulator
89
  //   2. x86_64-apple-iossimulator
90
  // where both are valid examples of the same platform+environment but in the
91
  // variant (2) the simulator is hardcoded as part of the platform name. Both
92
  // forms above should match for "iossimulator" requirement.
93
63.5k
  if (Target.getTriple().isOSDarwin() && 
PlatformEnv.endswith("simulator")62.9k
)
94
3
    return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
95
96
63.5k
  return PlatformEnv == Feature;
97
63.5k
}
98
99
/// Determine whether a translation unit built using the current
100
/// language options has the given feature.
101
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
102
66.8k
                       const TargetInfo &Target) {
103
66.8k
  bool HasFeature = llvm::StringSwitch<bool>(Feature)
104
66.8k
                        .Case("altivec", LangOpts.AltiVec)
105
66.8k
                        .Case("blocks", LangOpts.Blocks)
106
66.8k
                        .Case("coroutines", LangOpts.Coroutines)
107
66.8k
                        .Case("cplusplus", LangOpts.CPlusPlus)
108
66.8k
                        .Case("cplusplus11", LangOpts.CPlusPlus11)
109
66.8k
                        .Case("cplusplus14", LangOpts.CPlusPlus14)
110
66.8k
                        .Case("cplusplus17", LangOpts.CPlusPlus17)
111
66.8k
                        .Case("c99", LangOpts.C99)
112
66.8k
                        .Case("c11", LangOpts.C11)
113
66.8k
                        .Case("c17", LangOpts.C17)
114
66.8k
                        .Case("freestanding", LangOpts.Freestanding)
115
66.8k
                        .Case("gnuinlineasm", LangOpts.GNUAsm)
116
66.8k
                        .Case("objc", LangOpts.ObjC)
117
66.8k
                        .Case("objc_arc", LangOpts.ObjCAutoRefCount)
118
66.8k
                        .Case("opencl", LangOpts.OpenCL)
119
66.8k
                        .Case("tls", Target.isTLSSupported())
120
66.8k
                        .Case("zvector", LangOpts.ZVector)
121
66.8k
                        .Default(Target.hasFeature(Feature) ||
122
66.8k
                                 
isPlatformEnvironment(Target, Feature)63.6k
);
123
66.8k
  if (!HasFeature)
124
47.7k
    HasFeature = llvm::is_contained(LangOpts.ModuleFeatures, Feature);
125
66.8k
  return HasFeature;
126
66.8k
}
127
128
bool Module::isUnimportable(const LangOptions &LangOpts,
129
                            const TargetInfo &Target, Requirement &Req,
130
23
                            Module *&ShadowingModule) const {
131
23
  if (!IsUnimportable)
132
2
    return false;
133
134
21
  for (const Module *Current = this; Current; 
Current = Current->Parent0
) {
135
21
    if (Current->ShadowingModule) {
136
1
      ShadowingModule = Current->ShadowingModule;
137
1
      return true;
138
1
    }
139
20
    for (unsigned I = 0, N = Current->Requirements.size(); I != N; 
++I0
) {
140
20
      if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
141
20
              Current->Requirements[I].second) {
142
20
        Req = Current->Requirements[I];
143
20
        return true;
144
20
      }
145
20
    }
146
20
  }
147
148
0
  llvm_unreachable("could not find a reason why module is unimportable");
149
0
}
150
151
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
152
                         Requirement &Req,
153
                         UnresolvedHeaderDirective &MissingHeader,
154
35.8k
                         Module *&ShadowingModule) const {
155
35.8k
  if (IsAvailable)
156
35.7k
    return true;
157
158
23
  if (isUnimportable(LangOpts, Target, Req, ShadowingModule))
159
21
    return false;
160
161
  // FIXME: All missing headers are listed on the top-level module. Should we
162
  // just look there?
163
3
  
for (const Module *Current = this; 2
Current;
Current = Current->Parent1
) {
164
3
    if (!Current->MissingHeaders.empty()) {
165
2
      MissingHeader = Current->MissingHeaders.front();
166
2
      return false;
167
2
    }
168
3
  }
169
170
0
  llvm_unreachable("could not find a reason why module is unavailable");
171
0
}
172
173
46.2k
bool Module::isSubModuleOf(const Module *Other) const {
174
155k
  for (auto *Parent = this; Parent; 
Parent = Parent->Parent108k
) {
175
154k
    if (Parent == Other)
176
45.7k
      return true;
177
154k
  }
178
450
  return false;
179
46.2k
}
180
181
7.00M
const Module *Module::getTopLevelModule() const {
182
7.00M
  const Module *Result = this;
183
18.7M
  while (Result->Parent)
184
11.7M
    Result = Result->Parent;
185
186
7.00M
  return Result;
187
7.00M
}
188
189
static StringRef getModuleNameFromComponent(
190
3
    const std::pair<std::string, SourceLocation> &IdComponent) {
191
3
  return IdComponent.first;
192
3
}
193
194
8.99k
static StringRef getModuleNameFromComponent(StringRef R) { return R; }
195
196
template<typename InputIter>
197
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
198
4.61k
                          bool AllowStringLiterals = true) {
199
13.6k
  for (InputIter It = Begin; It != End; 
++It9.00k
) {
200
9.00k
    if (It != Begin)
201
4.39k
      OS << ".";
202
203
9.00k
    StringRef Name = getModuleNameFromComponent(*It);
204
9.00k
    if (!AllowStringLiterals || 
isValidAsciiIdentifier(Name)509
)
205
8.99k
      OS << Name;
206
10
    else {
207
10
      OS << '"';
208
10
      OS.write_escaped(Name);
209
10
      OS << '"';
210
10
    }
211
9.00k
  }
212
4.61k
}
Module.cpp:void printModuleId<std::__1::reverse_iterator<llvm::StringRef*> >(llvm::raw_ostream&, std::__1::reverse_iterator<llvm::StringRef*>, std::__1::reverse_iterator<llvm::StringRef*>, bool)
Line
Count
Source
198
4.36k
                          bool AllowStringLiterals = true) {
199
13.1k
  for (InputIter It = Begin; It != End; 
++It8.75k
) {
200
8.75k
    if (It != Begin)
201
4.39k
      OS << ".";
202
203
8.75k
    StringRef Name = getModuleNameFromComponent(*It);
204
8.75k
    if (!AllowStringLiterals || 
isValidAsciiIdentifier(Name)264
)
205
8.74k
      OS << Name;
206
8
    else {
207
8
      OS << '"';
208
8
      OS.write_escaped(Name);
209
8
      OS << '"';
210
8
    }
211
8.75k
  }
212
4.36k
}
Module.cpp:void printModuleId<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>(llvm::raw_ostream&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, bool)
Line
Count
Source
198
242
                          bool AllowStringLiterals = true) {
199
484
  for (InputIter It = Begin; It != End; 
++It242
) {
200
242
    if (It != Begin)
201
0
      OS << ".";
202
203
242
    StringRef Name = getModuleNameFromComponent(*It);
204
242
    if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
205
240
      OS << Name;
206
2
    else {
207
2
      OS << '"';
208
2
      OS.write_escaped(Name);
209
2
      OS << '"';
210
2
    }
211
242
  }
212
242
}
Module.cpp:void printModuleId<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, clang::SourceLocation> const*>(llvm::raw_ostream&, std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, clang::SourceLocation> const*, std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, clang::SourceLocation> const*, bool)
Line
Count
Source
198
14
                          bool AllowStringLiterals = true) {
199
17
  for (InputIter It = Begin; It != End; 
++It3
) {
200
3
    if (It != Begin)
201
0
      OS << ".";
202
203
3
    StringRef Name = getModuleNameFromComponent(*It);
204
3
    if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
205
3
      OS << Name;
206
0
    else {
207
0
      OS << '"';
208
0
      OS.write_escaped(Name);
209
0
      OS << '"';
210
0
    }
211
3
  }
212
14
}
213
214
template<typename Container>
215
14
static void printModuleId(raw_ostream &OS, const Container &C) {
216
14
  return printModuleId(OS, C.begin(), C.end());
217
14
}
218
219
4.36k
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
220
4.36k
  SmallVector<StringRef, 2> Names;
221
222
  // Build up the set of module names (from innermost to outermost).
223
13.1k
  for (const Module *M = this; M; 
M = M->Parent8.75k
)
224
8.75k
    Names.push_back(M->Name);
225
226
4.36k
  std::string Result;
227
228
4.36k
  llvm::raw_string_ostream Out(Result);
229
4.36k
  printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
230
4.36k
  Out.flush();
231
232
4.36k
  return Result;
233
4.36k
}
234
235
9.33k
bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
236
9.65k
  for (const Module *M = this; M; 
M = M->Parent322
) {
237
9.52k
    if (nameParts.empty() || M->Name != nameParts.back())
238
9.19k
      return false;
239
322
    nameParts = nameParts.drop_back();
240
322
  }
241
138
  return nameParts.empty();
242
9.33k
}
243
244
90.1k
Module::DirectoryName Module::getUmbrellaDir() const {
245
90.1k
  if (Header U = getUmbrellaHeader())
246
12.0k
    return {"", "", U.Entry->getDir()};
247
248
78.0k
  return {UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
249
78.0k
          Umbrella.dyn_cast<const DirectoryEntry *>()};
250
90.1k
}
251
252
28.3k
void Module::addTopHeader(const FileEntry *File) {
253
28.3k
  assert(File);
254
0
  TopHeaders.insert(File);
255
28.3k
}
256
257
26.2k
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
258
26.2k
  if (!TopHeaderNames.empty()) {
259
1
    for (std::vector<std::string>::iterator
260
3
           I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; 
++I2
) {
261
2
      if (auto FE = FileMgr.getFile(*I))
262
2
        TopHeaders.insert(*FE);
263
2
    }
264
1
    TopHeaderNames.clear();
265
1
  }
266
267
26.2k
  return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
268
26.2k
}
269
270
46.0k
bool Module::directlyUses(const Module *Requested) {
271
46.0k
  auto *Top = getTopLevelModule();
272
273
  // A top-level module implicitly uses itself.
274
46.0k
  if (Requested->isSubModuleOf(Top))
275
45.7k
    return true;
276
277
386
  for (auto *Use : Top->DirectUses)
278
86
    if (Requested->isSubModuleOf(Use))
279
26
      return true;
280
281
  // Anyone is allowed to use our builtin stddef.h and its accompanying module.
282
360
  if (!Requested->Parent && 
Requested->Name == "_Builtin_stddef_max_align_t"172
)
283
155
    return true;
284
285
205
  if (NoUndeclaredIncludes)
286
193
    UndeclaredUses.insert(Requested);
287
288
205
  return false;
289
360
}
290
291
void Module::addRequirement(StringRef Feature, bool RequiredState,
292
                            const LangOptions &LangOpts,
293
66.8k
                            const TargetInfo &Target) {
294
66.8k
  Requirements.push_back(Requirement(std::string(Feature), RequiredState));
295
296
  // If this feature is currently available, we're done.
297
66.8k
  if (hasFeature(Feature, LangOpts, Target) == RequiredState)
298
23.2k
    return;
299
300
43.6k
  markUnavailable(/*Unimportable*/true);
301
43.6k
}
302
303
47.2k
void Module::markUnavailable(bool Unimportable) {
304
81.4k
  auto needUpdate = [Unimportable](Module *M) {
305
81.4k
    return M->IsAvailable || 
(18.9k
!M->IsUnimportable18.9k
&&
Unimportable3.25k
);
306
81.4k
  };
307
308
47.2k
  if (!needUpdate(this))
309
15.4k
    return;
310
311
31.7k
  SmallVector<Module *, 2> Stack;
312
31.7k
  Stack.push_back(this);
313
63.5k
  while (!Stack.empty()) {
314
31.8k
    Module *Current = Stack.back();
315
31.8k
    Stack.pop_back();
316
317
31.8k
    if (!needUpdate(Current))
318
0
      continue;
319
320
31.8k
    Current->IsAvailable = false;
321
31.8k
    Current->IsUnimportable |= Unimportable;
322
31.8k
    for (submodule_iterator Sub = Current->submodule_begin(),
323
31.8k
                         SubEnd = Current->submodule_end();
324
34.2k
         Sub != SubEnd; 
++Sub2.37k
) {
325
2.37k
      if (needUpdate(*Sub))
326
128
        Stack.push_back(*Sub);
327
2.37k
    }
328
31.8k
  }
329
31.7k
}
330
331
4.47M
Module *Module::findSubmodule(StringRef Name) const {
332
4.47M
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
333
4.47M
  if (Pos == SubModuleIndex.end())
334
3.29M
    return nullptr;
335
336
1.17M
  return SubModules[Pos->getValue()];
337
4.47M
}
338
339
47
Module *Module::findOrInferSubmodule(StringRef Name) {
340
47
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
341
47
  if (Pos != SubModuleIndex.end())
342
46
    return SubModules[Pos->getValue()];
343
1
  if (!InferSubmodules)
344
0
    return nullptr;
345
1
  Module *Result = new Module(Name, SourceLocation(), this, false, InferExplicitSubmodules, 0);
346
1
  Result->InferExplicitSubmodules = InferExplicitSubmodules;
347
1
  Result->InferSubmodules = InferSubmodules;
348
1
  Result->InferExportWildcard = InferExportWildcard;
349
1
  if (Result->InferExportWildcard)
350
1
    Result->Exports.push_back(Module::ExportDecl(nullptr, true));
351
1
  return Result;
352
1
}
353
354
3.04M
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
355
  // All non-explicit submodules are exported.
356
3.04M
  for (std::vector<Module *>::const_iterator I = SubModules.begin(),
357
3.04M
                                             E = SubModules.end();
358
5.67M
       I != E; 
++I2.62M
) {
359
2.62M
    Module *Mod = *I;
360
2.62M
    if (!Mod->IsExplicit)
361
2.59M
      Exported.push_back(Mod);
362
2.62M
  }
363
364
  // Find re-exported modules by filtering the list of imported modules.
365
3.04M
  bool AnyWildcard = false;
366
3.04M
  bool UnrestrictedWildcard = false;
367
3.04M
  SmallVector<Module *, 4> WildcardRestrictions;
368
5.11M
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I2.06M
) {
369
2.06M
    Module *Mod = Exports[I].getPointer();
370
2.06M
    if (!Exports[I].getInt()) {
371
      // Export a named module directly; no wildcards involved.
372
148k
      Exported.push_back(Mod);
373
374
148k
      continue;
375
148k
    }
376
377
    // Wildcard export: export all of the imported modules that match
378
    // the given pattern.
379
1.92M
    AnyWildcard = true;
380
1.92M
    if (UnrestrictedWildcard)
381
7.51k
      continue;
382
383
1.91M
    if (Module *Restriction = Exports[I].getPointer())
384
3
      WildcardRestrictions.push_back(Restriction);
385
1.91M
    else {
386
1.91M
      WildcardRestrictions.clear();
387
1.91M
      UnrestrictedWildcard = true;
388
1.91M
    }
389
1.91M
  }
390
391
  // If there were any wildcards, push any imported modules that were
392
  // re-exported by the wildcard restriction.
393
3.04M
  if (!AnyWildcard)
394
1.13M
    return;
395
396
9.93M
  
for (unsigned I = 0, N = Imports.size(); 1.91M
I != N;
++I8.01M
) {
397
8.01M
    Module *Mod = Imports[I];
398
8.01M
    bool Acceptable = UnrestrictedWildcard;
399
8.01M
    if (!Acceptable) {
400
      // Check whether this module meets one of the restrictions.
401
9
      for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; 
++R3
) {
402
6
        Module *Restriction = WildcardRestrictions[R];
403
6
        if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
404
3
          Acceptable = true;
405
3
          break;
406
3
        }
407
6
      }
408
6
    }
409
410
8.01M
    if (!Acceptable)
411
3
      continue;
412
413
8.01M
    Exported.push_back(Mod);
414
8.01M
  }
415
1.91M
}
416
417
240
void Module::buildVisibleModulesCache() const {
418
240
  assert(VisibleModulesCache.empty() && "cache does not need building");
419
420
  // This module is visible to itself.
421
0
  VisibleModulesCache.insert(this);
422
423
  // Every imported module is visible.
424
240
  SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
425
87.1k
  while (!Stack.empty()) {
426
86.9k
    Module *CurrModule = Stack.pop_back_val();
427
428
    // Every module transitively exported by an imported module is visible.
429
86.9k
    if (VisibleModulesCache.insert(CurrModule).second)
430
35.4k
      CurrModule->getExportedModules(Stack);
431
86.9k
  }
432
240
}
433
434
242
void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) const {
435
242
  OS.indent(Indent);
436
242
  if (IsFramework)
437
217
    OS << "framework ";
438
242
  if (IsExplicit)
439
0
    OS << "explicit ";
440
242
  OS << "module ";
441
242
  printModuleId(OS, &Name, &Name + 1);
442
443
242
  if (IsSystem || 
IsExternC180
) {
444
67
    OS.indent(Indent + 2);
445
67
    if (IsSystem)
446
62
      OS << " [system]";
447
67
    if (IsExternC)
448
65
      OS << " [extern_c]";
449
67
  }
450
451
242
  OS << " {\n";
452
453
242
  if (!Requirements.empty()) {
454
0
    OS.indent(Indent + 2);
455
0
    OS << "requires ";
456
0
    for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
457
0
      if (I)
458
0
        OS << ", ";
459
0
      if (!Requirements[I].second)
460
0
        OS << "!";
461
0
      OS << Requirements[I].first;
462
0
    }
463
0
    OS << "\n";
464
0
  }
465
466
242
  if (Header H = getUmbrellaHeader()) {
467
217
    OS.indent(Indent + 2);
468
217
    OS << "umbrella header \"";
469
217
    OS.write_escaped(H.NameAsWritten);
470
217
    OS << "\"\n";
471
217
  } else 
if (DirectoryName 25
D25
= getUmbrellaDir()) {
472
0
    OS.indent(Indent + 2);
473
0
    OS << "umbrella \"";
474
0
    OS.write_escaped(D.NameAsWritten);
475
0
    OS << "\"\n";
476
0
  }
477
478
242
  if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
479
0
    OS.indent(Indent + 2);
480
0
    OS << "config_macros ";
481
0
    if (ConfigMacrosExhaustive)
482
0
      OS << "[exhaustive]";
483
0
    for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
484
0
      if (I)
485
0
        OS << ", ";
486
0
      OS << ConfigMacros[I];
487
0
    }
488
0
    OS << "\n";
489
0
  }
490
491
242
  struct {
492
242
    StringRef Prefix;
493
242
    HeaderKind Kind;
494
242
  } Kinds[] = {{"", HK_Normal},
495
242
               {"textual ", HK_Textual},
496
242
               {"private ", HK_Private},
497
242
               {"private textual ", HK_PrivateTextual},
498
242
               {"exclude ", HK_Excluded}};
499
500
1.21k
  for (auto &K : Kinds) {
501
1.21k
    assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
502
41
    for (auto &H : Headers[K.Kind]) {
503
41
      OS.indent(Indent + 2);
504
41
      OS << K.Prefix << "header \"";
505
41
      OS.write_escaped(H.NameAsWritten);
506
41
      OS << "\" { size " << H.Entry->getSize()
507
41
         << " mtime " << H.Entry->getModificationTime() << " }\n";
508
41
    }
509
1.21k
  }
510
484
  for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
511
484
    for (auto &U : *Unresolved) {
512
0
      OS.indent(Indent + 2);
513
0
      OS << Kinds[U.Kind].Prefix << "header \"";
514
0
      OS.write_escaped(U.FileName);
515
0
      OS << "\"";
516
0
      if (U.Size || U.ModTime) {
517
0
        OS << " {";
518
0
        if (U.Size)
519
0
          OS << " size " << *U.Size;
520
0
        if (U.ModTime)
521
0
          OS << " mtime " << *U.ModTime;
522
0
        OS << " }";
523
0
      }
524
0
      OS << "\n";
525
0
    }
526
484
  }
527
528
242
  if (!ExportAsModule.empty()) {
529
0
    OS.indent(Indent + 2);
530
0
    OS << "export_as" << ExportAsModule << "\n";
531
0
  }
532
533
242
  for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
534
367
       MI != MIEnd; 
++MI125
)
535
    // Print inferred subframework modules so that we don't need to re-infer
536
    // them (requires expensive directory iteration + stat calls) when we build
537
    // the module. Regular inferred submodules are OK, as we need to look at all
538
    // those header files anyway.
539
125
    if (!(*MI)->IsInferred || 
(*MI)->IsFramework119
)
540
124
      (*MI)->print(OS, Indent + 2, Dump);
541
542
459
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I217
) {
543
217
    OS.indent(Indent + 2);
544
217
    OS << "export ";
545
217
    if (Module *Restriction = Exports[I].getPointer()) {
546
0
      OS << Restriction->getFullModuleName(true);
547
0
      if (Exports[I].getInt())
548
0
        OS << ".*";
549
217
    } else {
550
217
      OS << "*";
551
217
    }
552
217
    OS << "\n";
553
217
  }
554
555
255
  for (unsigned I = 0, N = UnresolvedExports.size(); I != N; 
++I13
) {
556
13
    OS.indent(Indent + 2);
557
13
    OS << "export ";
558
13
    printModuleId(OS, UnresolvedExports[I].Id);
559
13
    if (UnresolvedExports[I].Wildcard)
560
11
      OS << (UnresolvedExports[I].Id.empty() ? "*" : 
".*"0
);
561
13
    OS << "\n";
562
13
  }
563
564
242
  if (Dump) {
565
0
    for (Module *M : Imports) {
566
0
      OS.indent(Indent + 2);
567
0
      llvm::errs() << "import " << M->getFullModuleName() << "\n";
568
0
    }
569
0
  }
570
571
242
  for (unsigned I = 0, N = DirectUses.size(); I != N; 
++I0
) {
572
0
    OS.indent(Indent + 2);
573
0
    OS << "use ";
574
0
    OS << DirectUses[I]->getFullModuleName(true);
575
0
    OS << "\n";
576
0
  }
577
578
243
  for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; 
++I1
) {
579
1
    OS.indent(Indent + 2);
580
1
    OS << "use ";
581
1
    printModuleId(OS, UnresolvedDirectUses[I]);
582
1
    OS << "\n";
583
1
  }
584
585
312
  for (unsigned I = 0, N = LinkLibraries.size(); I != N; 
++I70
) {
586
70
    OS.indent(Indent + 2);
587
70
    OS << "link ";
588
70
    if (LinkLibraries[I].IsFramework)
589
70
      OS << "framework ";
590
70
    OS << "\"";
591
70
    OS.write_escaped(LinkLibraries[I].Library);
592
70
    OS << "\"";
593
70
  }
594
595
242
  for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; 
++I0
) {
596
0
    OS.indent(Indent + 2);
597
0
    OS << "conflict ";
598
0
    printModuleId(OS, UnresolvedConflicts[I].Id);
599
0
    OS << ", \"";
600
0
    OS.write_escaped(UnresolvedConflicts[I].Message);
601
0
    OS << "\"\n";
602
0
  }
603
604
242
  for (unsigned I = 0, N = Conflicts.size(); I != N; 
++I0
) {
605
0
    OS.indent(Indent + 2);
606
0
    OS << "conflict ";
607
0
    OS << Conflicts[I].Other->getFullModuleName(true);
608
0
    OS << ", \"";
609
0
    OS.write_escaped(Conflicts[I].Message);
610
0
    OS << "\"\n";
611
0
  }
612
613
242
  if (InferSubmodules) {
614
217
    OS.indent(Indent + 2);
615
217
    if (InferExplicitSubmodules)
616
0
      OS << "explicit ";
617
217
    OS << "module * {\n";
618
217
    if (InferExportWildcard) {
619
217
      OS.indent(Indent + 4);
620
217
      OS << "export *\n";
621
217
    }
622
217
    OS.indent(Indent + 2);
623
217
    OS << "}\n";
624
217
  }
625
626
242
  OS.indent(Indent);
627
242
  OS << "}\n";
628
242
}
629
630
0
LLVM_DUMP_METHOD void Module::dump() const {
631
0
  print(llvm::errs(), 0, true);
632
0
}
633
634
void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
635
344k
                                  VisibleCallback Vis, ConflictCallback Cb) {
636
344k
  assert(Loc.isValid() && "setVisible expects a valid import location");
637
344k
  if (isVisible(M))
638
290k
    return;
639
640
53.7k
  ++Generation;
641
642
53.7k
  struct Visiting {
643
53.7k
    Module *M;
644
53.7k
    Visiting *ExportedBy;
645
53.7k
  };
646
647
5.54M
  std::function<void(Visiting)> VisitModule = [&](Visiting V) {
648
    // Nothing to do for a module that's already visible.
649
5.54M
    unsigned ID = V.M->getVisibilityID();
650
5.54M
    if (ImportLocs.size() <= ID)
651
289k
      ImportLocs.resize(ID + 1);
652
5.25M
    else if (ImportLocs[ID].isValid())
653
3.92M
      return;
654
655
1.62M
    ImportLocs[ID] = Loc;
656
1.62M
    Vis(M);
657
658
    // Make any exported modules visible.
659
1.62M
    SmallVector<Module *, 16> Exports;
660
1.62M
    V.M->getExportedModules(Exports);
661
5.50M
    for (Module *E : Exports) {
662
      // Don't import non-importable modules.
663
5.50M
      if (!E->isUnimportable())
664
5.49M
        VisitModule({E, &V});
665
5.50M
    }
666
667
1.62M
    for (auto &C : V.M->Conflicts) {
668
2
      if (isVisible(C.Other)) {
669
2
        llvm::SmallVector<Module*, 8> Path;
670
4
        for (Visiting *I = &V; I; 
I = I->ExportedBy2
)
671
2
          Path.push_back(I->M);
672
2
        Cb(Path, C.Other, C.Message);
673
2
      }
674
2
    }
675
1.62M
  };
676
53.7k
  VisitModule({M, nullptr});
677
53.7k
}
678
679
ASTSourceDescriptor::ASTSourceDescriptor(Module &M)
680
695k
    : Signature(M.Signature), ClangModule(&M) {
681
695k
  if (M.Directory)
682
515k
    Path = M.Directory->getName();
683
695k
  if (auto File = M.getASTFile())
684
42.2k
    ASTFile = File->getName();
685
695k
}
686
687
30.8k
std::string ASTSourceDescriptor::getModuleName() const {
688
30.8k
  if (ClangModule)
689
30.7k
    return ClangModule->Name;
690
122
  else
691
122
    return std::string(PCHModuleName);
692
30.8k
}