Coverage Report

Created: 2021-09-21 08:58

/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.68M
      NameVisibility(Hidden) {
48
1.68M
  if (Parent) {
49
1.56M
    IsAvailable = Parent->isAvailable();
50
1.56M
    IsUnimportable = Parent->isUnimportable();
51
1.56M
    IsSystem = Parent->IsSystem;
52
1.56M
    IsExternC = Parent->IsExternC;
53
1.56M
    NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
54
1.56M
    ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
55
56
1.56M
    Parent->SubModuleIndex[Name] = Parent->SubModules.size();
57
1.56M
    Parent->SubModules.push_back(this);
58
1.56M
  }
59
1.68M
}
60
61
1.17M
Module::~Module() {
62
1.17M
  for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
63
2.24M
       I != IEnd; 
++I1.06M
) {
64
1.06M
    delete *I;
65
1.06M
  }
66
1.17M
}
67
68
57.9k
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
69
57.9k
  StringRef Platform = Target.getPlatformName();
70
57.9k
  StringRef Env = Target.getTriple().getEnvironmentName();
71
72
  // Attempt to match platform and environment.
73
57.9k
  if (Platform == Feature || 
Target.getTriple().getOSName() == Feature57.9k
||
74
57.9k
      
Env == Feature57.9k
)
75
58
    return true;
76
77
57.8k
  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
57.8k
  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
57.8k
  if (Target.getTriple().isOSDarwin() && 
PlatformEnv.endswith("simulator")57.0k
)
94
3
    return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
95
96
57.8k
  return PlatformEnv == Feature;
97
57.8k
}
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
61.1k
                       const TargetInfo &Target) {
103
61.1k
  bool HasFeature = llvm::StringSwitch<bool>(Feature)
104
61.1k
                        .Case("altivec", LangOpts.AltiVec)
105
61.1k
                        .Case("blocks", LangOpts.Blocks)
106
61.1k
                        .Case("coroutines", LangOpts.Coroutines)
107
61.1k
                        .Case("cplusplus", LangOpts.CPlusPlus)
108
61.1k
                        .Case("cplusplus11", LangOpts.CPlusPlus11)
109
61.1k
                        .Case("cplusplus14", LangOpts.CPlusPlus14)
110
61.1k
                        .Case("cplusplus17", LangOpts.CPlusPlus17)
111
61.1k
                        .Case("c99", LangOpts.C99)
112
61.1k
                        .Case("c11", LangOpts.C11)
113
61.1k
                        .Case("c17", LangOpts.C17)
114
61.1k
                        .Case("freestanding", LangOpts.Freestanding)
115
61.1k
                        .Case("gnuinlineasm", LangOpts.GNUAsm)
116
61.1k
                        .Case("objc", LangOpts.ObjC)
117
61.1k
                        .Case("objc_arc", LangOpts.ObjCAutoRefCount)
118
61.1k
                        .Case("opencl", LangOpts.OpenCL)
119
61.1k
                        .Case("tls", Target.isTLSSupported())
120
61.1k
                        .Case("zvector", LangOpts.ZVector)
121
61.1k
                        .Default(Target.hasFeature(Feature) ||
122
61.1k
                                 
isPlatformEnvironment(Target, Feature)57.9k
);
123
61.1k
  if (!HasFeature)
124
42.4k
    HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
125
42.4k
                           LangOpts.ModuleFeatures.end(),
126
42.4k
                           Feature) != LangOpts.ModuleFeatures.end();
127
61.1k
  return HasFeature;
128
61.1k
}
129
130
bool Module::isUnimportable(const LangOptions &LangOpts,
131
                            const TargetInfo &Target, Requirement &Req,
132
65
                            Module *&ShadowingModule) const {
133
65
  if (!IsUnimportable)
134
2
    return false;
135
136
63
  for (const Module *Current = this; Current; 
Current = Current->Parent0
) {
137
63
    if (Current->ShadowingModule) {
138
1
      ShadowingModule = Current->ShadowingModule;
139
1
      return true;
140
1
    }
141
62
    for (unsigned I = 0, N = Current->Requirements.size(); I != N; 
++I0
) {
142
62
      if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
143
62
              Current->Requirements[I].second) {
144
62
        Req = Current->Requirements[I];
145
62
        return true;
146
62
      }
147
62
    }
148
62
  }
149
150
0
  llvm_unreachable("could not find a reason why module is unimportable");
151
0
}
152
153
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
154
                         Requirement &Req,
155
                         UnresolvedHeaderDirective &MissingHeader,
156
30.2k
                         Module *&ShadowingModule) const {
157
30.2k
  if (IsAvailable)
158
30.2k
    return true;
159
160
65
  if (isUnimportable(LangOpts, Target, Req, ShadowingModule))
161
63
    return false;
162
163
  // FIXME: All missing headers are listed on the top-level module. Should we
164
  // just look there?
165
3
  
for (const Module *Current = this; 2
Current;
Current = Current->Parent1
) {
166
3
    if (!Current->MissingHeaders.empty()) {
167
2
      MissingHeader = Current->MissingHeaders.front();
168
2
      return false;
169
2
    }
170
3
  }
171
172
0
  llvm_unreachable("could not find a reason why module is unavailable");
173
0
}
174
175
44.2k
bool Module::isSubModuleOf(const Module *Other) const {
176
148k
  for (auto *Parent = this; Parent; 
Parent = Parent->Parent104k
) {
177
148k
    if (Parent == Other)
178
43.8k
      return true;
179
148k
  }
180
414
  return false;
181
44.2k
}
182
183
6.82M
const Module *Module::getTopLevelModule() const {
184
6.82M
  const Module *Result = this;
185
18.2M
  while (Result->Parent)
186
11.3M
    Result = Result->Parent;
187
188
6.82M
  return Result;
189
6.82M
}
190
191
static StringRef getModuleNameFromComponent(
192
3
    const std::pair<std::string, SourceLocation> &IdComponent) {
193
3
  return IdComponent.first;
194
3
}
195
196
9.17k
static StringRef getModuleNameFromComponent(StringRef R) { return R; }
197
198
template<typename InputIter>
199
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
200
4.64k
                          bool AllowStringLiterals = true) {
201
13.8k
  for (InputIter It = Begin; It != End; 
++It9.18k
) {
202
9.18k
    if (It != Begin)
203
4.54k
      OS << ".";
204
205
9.18k
    StringRef Name = getModuleNameFromComponent(*It);
206
9.18k
    if (!AllowStringLiterals || 
isValidAsciiIdentifier(Name)499
)
207
9.17k
      OS << Name;
208
10
    else {
209
10
      OS << '"';
210
10
      OS.write_escaped(Name);
211
10
      OS << '"';
212
10
    }
213
9.18k
  }
214
4.64k
}
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
200
4.40k
                          bool AllowStringLiterals = true) {
201
13.3k
  for (InputIter It = Begin; It != End; 
++It8.94k
) {
202
8.94k
    if (It != Begin)
203
4.54k
      OS << ".";
204
205
8.94k
    StringRef Name = getModuleNameFromComponent(*It);
206
8.94k
    if (!AllowStringLiterals || 
isValidAsciiIdentifier(Name)261
)
207
8.93k
      OS << Name;
208
8
    else {
209
8
      OS << '"';
210
8
      OS.write_escaped(Name);
211
8
      OS << '"';
212
8
    }
213
8.94k
  }
214
4.40k
}
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
200
235
                          bool AllowStringLiterals = true) {
201
470
  for (InputIter It = Begin; It != End; 
++It235
) {
202
235
    if (It != Begin)
203
0
      OS << ".";
204
205
235
    StringRef Name = getModuleNameFromComponent(*It);
206
235
    if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
207
233
      OS << Name;
208
2
    else {
209
2
      OS << '"';
210
2
      OS.write_escaped(Name);
211
2
      OS << '"';
212
2
    }
213
235
  }
214
235
}
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
200
14
                          bool AllowStringLiterals = true) {
201
17
  for (InputIter It = Begin; It != End; 
++It3
) {
202
3
    if (It != Begin)
203
0
      OS << ".";
204
205
3
    StringRef Name = getModuleNameFromComponent(*It);
206
3
    if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
207
3
      OS << Name;
208
0
    else {
209
0
      OS << '"';
210
0
      OS.write_escaped(Name);
211
0
      OS << '"';
212
0
    }
213
3
  }
214
14
}
215
216
template<typename Container>
217
14
static void printModuleId(raw_ostream &OS, const Container &C) {
218
14
  return printModuleId(OS, C.begin(), C.end());
219
14
}
220
221
4.40k
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
222
4.40k
  SmallVector<StringRef, 2> Names;
223
224
  // Build up the set of module names (from innermost to outermost).
225
13.3k
  for (const Module *M = this; M; 
M = M->Parent8.94k
)
226
8.94k
    Names.push_back(M->Name);
227
228
4.40k
  std::string Result;
229
230
4.40k
  llvm::raw_string_ostream Out(Result);
231
4.40k
  printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
232
4.40k
  Out.flush();
233
234
4.40k
  return Result;
235
4.40k
}
236
237
8.91k
bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
238
9.23k
  for (const Module *M = this; M; 
M = M->Parent322
) {
239
9.09k
    if (nameParts.empty() || M->Name != nameParts.back())
240
8.77k
      return false;
241
322
    nameParts = nameParts.drop_back();
242
322
  }
243
138
  return nameParts.empty();
244
8.91k
}
245
246
83.0k
Module::DirectoryName Module::getUmbrellaDir() const {
247
83.0k
  if (Header U = getUmbrellaHeader())
248
11.3k
    return {"", "", U.Entry->getDir()};
249
250
71.7k
  return {UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
251
71.7k
          Umbrella.dyn_cast<const DirectoryEntry *>()};
252
83.0k
}
253
254
25.1k
void Module::addTopHeader(const FileEntry *File) {
255
25.1k
  assert(File);
256
0
  TopHeaders.insert(File);
257
25.1k
}
258
259
23.1k
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
260
23.1k
  if (!TopHeaderNames.empty()) {
261
1
    for (std::vector<std::string>::iterator
262
3
           I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; 
++I2
) {
263
2
      if (auto FE = FileMgr.getFile(*I))
264
2
        TopHeaders.insert(*FE);
265
2
    }
266
1
    TopHeaderNames.clear();
267
1
  }
268
269
23.1k
  return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
270
23.1k
}
271
272
44.1k
bool Module::directlyUses(const Module *Requested) const {
273
44.1k
  auto *Top = getTopLevelModule();
274
275
  // A top-level module implicitly uses itself.
276
44.1k
  if (Requested->isSubModuleOf(Top))
277
43.8k
    return true;
278
279
350
  for (auto *Use : Top->DirectUses)
280
86
    if (Requested->isSubModuleOf(Use))
281
26
      return true;
282
283
  // Anyone is allowed to use our builtin stddef.h and its accompanying module.
284
324
  if (!Requested->Parent && 
Requested->Name == "_Builtin_stddef_max_align_t"162
)
285
148
    return true;
286
287
176
  return false;
288
324
}
289
290
void Module::addRequirement(StringRef Feature, bool RequiredState,
291
                            const LangOptions &LangOpts,
292
61.1k
                            const TargetInfo &Target) {
293
61.1k
  Requirements.push_back(Requirement(std::string(Feature), RequiredState));
294
295
  // If this feature is currently available, we're done.
296
61.1k
  if (hasFeature(Feature, LangOpts, Target) == RequiredState)
297
22.7k
    return;
298
299
38.4k
  markUnavailable(/*Unimportable*/true);
300
38.4k
}
301
302
41.4k
void Module::markUnavailable(bool Unimportable) {
303
69.4k
  auto needUpdate = [Unimportable](Module *M) {
304
69.4k
    return M->IsAvailable || 
(17.1k
!M->IsUnimportable17.1k
&&
Unimportable2.65k
);
305
69.4k
  };
306
307
41.4k
  if (!needUpdate(this))
308
14.7k
    return;
309
310
26.6k
  SmallVector<Module *, 2> Stack;
311
26.6k
  Stack.push_back(this);
312
53.4k
  while (!Stack.empty()) {
313
26.7k
    Module *Current = Stack.back();
314
26.7k
    Stack.pop_back();
315
316
26.7k
    if (!needUpdate(Current))
317
0
      continue;
318
319
26.7k
    Current->IsAvailable = false;
320
26.7k
    Current->IsUnimportable |= Unimportable;
321
26.7k
    for (submodule_iterator Sub = Current->submodule_begin(),
322
26.7k
                         SubEnd = Current->submodule_end();
323
28.0k
         Sub != SubEnd; 
++Sub1.27k
) {
324
1.27k
      if (needUpdate(*Sub))
325
90
        Stack.push_back(*Sub);
326
1.27k
    }
327
26.7k
  }
328
26.6k
}
329
330
3.79M
Module *Module::findSubmodule(StringRef Name) const {
331
3.79M
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
332
3.79M
  if (Pos == SubModuleIndex.end())
333
2.80M
    return nullptr;
334
335
989k
  return SubModules[Pos->getValue()];
336
3.79M
}
337
338
48
Module *Module::findOrInferSubmodule(StringRef Name) {
339
48
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
340
48
  if (Pos != SubModuleIndex.end())
341
47
    return SubModules[Pos->getValue()];
342
1
  if (!InferSubmodules)
343
0
    return nullptr;
344
1
  Module *Result = new Module(Name, SourceLocation(), this, false, InferExplicitSubmodules, 0);
345
1
  Result->InferExplicitSubmodules = InferExplicitSubmodules;
346
1
  Result->InferSubmodules = InferSubmodules;
347
1
  Result->InferExportWildcard = InferExportWildcard;
348
1
  if (Result->InferExportWildcard)
349
1
    Result->Exports.push_back(Module::ExportDecl(nullptr, true));
350
1
  return Result;
351
1
}
352
353
2.74M
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
354
  // All non-explicit submodules are exported.
355
2.74M
  for (std::vector<Module *>::const_iterator I = SubModules.begin(),
356
2.74M
                                             E = SubModules.end();
357
5.08M
       I != E; 
++I2.33M
) {
358
2.33M
    Module *Mod = *I;
359
2.33M
    if (!Mod->IsExplicit)
360
2.30M
      Exported.push_back(Mod);
361
2.33M
  }
362
363
  // Find re-exported modules by filtering the list of imported modules.
364
2.74M
  bool AnyWildcard = false;
365
2.74M
  bool UnrestrictedWildcard = false;
366
2.74M
  SmallVector<Module *, 4> WildcardRestrictions;
367
4.86M
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I2.12M
) {
368
2.12M
    Module *Mod = Exports[I].getPointer();
369
2.12M
    if (!Exports[I].getInt()) {
370
      // Export a named module directly; no wildcards involved.
371
153k
      Exported.push_back(Mod);
372
373
153k
      continue;
374
153k
    }
375
376
    // Wildcard export: export all of the imported modules that match
377
    // the given pattern.
378
1.96M
    AnyWildcard = true;
379
1.96M
    if (UnrestrictedWildcard)
380
7.41k
      continue;
381
382
1.96M
    if (Module *Restriction = Exports[I].getPointer())
383
3
      WildcardRestrictions.push_back(Restriction);
384
1.96M
    else {
385
1.96M
      WildcardRestrictions.clear();
386
1.96M
      UnrestrictedWildcard = true;
387
1.96M
    }
388
1.96M
  }
389
390
  // If there were any wildcards, push any imported modules that were
391
  // re-exported by the wildcard restriction.
392
2.74M
  if (!AnyWildcard)
393
781k
    return;
394
395
9.17M
  
for (unsigned I = 0, N = Imports.size(); 1.96M
I != N;
++I7.21M
) {
396
7.21M
    Module *Mod = Imports[I];
397
7.21M
    bool Acceptable = UnrestrictedWildcard;
398
7.21M
    if (!Acceptable) {
399
      // Check whether this module meets one of the restrictions.
400
9
      for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; 
++R3
) {
401
6
        Module *Restriction = WildcardRestrictions[R];
402
6
        if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
403
3
          Acceptable = true;
404
3
          break;
405
3
        }
406
6
      }
407
6
    }
408
409
7.21M
    if (!Acceptable)
410
3
      continue;
411
412
7.21M
    Exported.push_back(Mod);
413
7.21M
  }
414
1.96M
}
415
416
225
void Module::buildVisibleModulesCache() const {
417
225
  assert(VisibleModulesCache.empty() && "cache does not need building");
418
419
  // This module is visible to itself.
420
0
  VisibleModulesCache.insert(this);
421
422
  // Every imported module is visible.
423
225
  SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
424
123k
  while (!Stack.empty()) {
425
122k
    Module *CurrModule = Stack.pop_back_val();
426
427
    // Every module transitively exported by an imported module is visible.
428
122k
    if (VisibleModulesCache.insert(CurrModule).second)
429
50.1k
      CurrModule->getExportedModules(Stack);
430
122k
  }
431
225
}
432
433
235
void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) const {
434
235
  OS.indent(Indent);
435
235
  if (IsFramework)
436
210
    OS << "framework ";
437
235
  if (IsExplicit)
438
0
    OS << "explicit ";
439
235
  OS << "module ";
440
235
  printModuleId(OS, &Name, &Name + 1);
441
442
235
  if (IsSystem || 
IsExternC180
) {
443
60
    OS.indent(Indent + 2);
444
60
    if (IsSystem)
445
55
      OS << " [system]";
446
60
    if (IsExternC)
447
58
      OS << " [extern_c]";
448
60
  }
449
450
235
  OS << " {\n";
451
452
235
  if (!Requirements.empty()) {
453
0
    OS.indent(Indent + 2);
454
0
    OS << "requires ";
455
0
    for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
456
0
      if (I)
457
0
        OS << ", ";
458
0
      if (!Requirements[I].second)
459
0
        OS << "!";
460
0
      OS << Requirements[I].first;
461
0
    }
462
0
    OS << "\n";
463
0
  }
464
465
235
  if (Header H = getUmbrellaHeader()) {
466
210
    OS.indent(Indent + 2);
467
210
    OS << "umbrella header \"";
468
210
    OS.write_escaped(H.NameAsWritten);
469
210
    OS << "\"\n";
470
210
  } else 
if (DirectoryName 25
D25
= getUmbrellaDir()) {
471
0
    OS.indent(Indent + 2);
472
0
    OS << "umbrella \"";
473
0
    OS.write_escaped(D.NameAsWritten);
474
0
    OS << "\"\n";
475
0
  }
476
477
235
  if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
478
0
    OS.indent(Indent + 2);
479
0
    OS << "config_macros ";
480
0
    if (ConfigMacrosExhaustive)
481
0
      OS << "[exhaustive]";
482
0
    for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
483
0
      if (I)
484
0
        OS << ", ";
485
0
      OS << ConfigMacros[I];
486
0
    }
487
0
    OS << "\n";
488
0
  }
489
490
235
  struct {
491
235
    StringRef Prefix;
492
235
    HeaderKind Kind;
493
235
  } Kinds[] = {{"", HK_Normal},
494
235
               {"textual ", HK_Textual},
495
235
               {"private ", HK_Private},
496
235
               {"private textual ", HK_PrivateTextual},
497
235
               {"exclude ", HK_Excluded}};
498
499
1.17k
  for (auto &K : Kinds) {
500
1.17k
    assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
501
41
    for (auto &H : Headers[K.Kind]) {
502
41
      OS.indent(Indent + 2);
503
41
      OS << K.Prefix << "header \"";
504
41
      OS.write_escaped(H.NameAsWritten);
505
41
      OS << "\" { size " << H.Entry->getSize()
506
41
         << " mtime " << H.Entry->getModificationTime() << " }\n";
507
41
    }
508
1.17k
  }
509
470
  for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
510
470
    for (auto &U : *Unresolved) {
511
0
      OS.indent(Indent + 2);
512
0
      OS << Kinds[U.Kind].Prefix << "header \"";
513
0
      OS.write_escaped(U.FileName);
514
0
      OS << "\"";
515
0
      if (U.Size || U.ModTime) {
516
0
        OS << " {";
517
0
        if (U.Size)
518
0
          OS << " size " << *U.Size;
519
0
        if (U.ModTime)
520
0
          OS << " mtime " << *U.ModTime;
521
0
        OS << " }";
522
0
      }
523
0
      OS << "\n";
524
0
    }
525
470
  }
526
527
235
  if (!ExportAsModule.empty()) {
528
0
    OS.indent(Indent + 2);
529
0
    OS << "export_as" << ExportAsModule << "\n";
530
0
  }
531
532
235
  for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
533
354
       MI != MIEnd; 
++MI119
)
534
    // Print inferred subframework modules so that we don't need to re-infer
535
    // them (requires expensive directory iteration + stat calls) when we build
536
    // the module. Regular inferred submodules are OK, as we need to look at all
537
    // those header files anyway.
538
119
    if (!(*MI)->IsInferred || 
(*MI)->IsFramework113
)
539
118
      (*MI)->print(OS, Indent + 2, Dump);
540
541
445
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I210
) {
542
210
    OS.indent(Indent + 2);
543
210
    OS << "export ";
544
210
    if (Module *Restriction = Exports[I].getPointer()) {
545
0
      OS << Restriction->getFullModuleName(true);
546
0
      if (Exports[I].getInt())
547
0
        OS << ".*";
548
210
    } else {
549
210
      OS << "*";
550
210
    }
551
210
    OS << "\n";
552
210
  }
553
554
248
  for (unsigned I = 0, N = UnresolvedExports.size(); I != N; 
++I13
) {
555
13
    OS.indent(Indent + 2);
556
13
    OS << "export ";
557
13
    printModuleId(OS, UnresolvedExports[I].Id);
558
13
    if (UnresolvedExports[I].Wildcard)
559
11
      OS << (UnresolvedExports[I].Id.empty() ? "*" : 
".*"0
);
560
13
    OS << "\n";
561
13
  }
562
563
235
  if (Dump) {
564
0
    for (Module *M : Imports) {
565
0
      OS.indent(Indent + 2);
566
0
      llvm::errs() << "import " << M->getFullModuleName() << "\n";
567
0
    }
568
0
  }
569
570
235
  for (unsigned I = 0, N = DirectUses.size(); I != N; 
++I0
) {
571
0
    OS.indent(Indent + 2);
572
0
    OS << "use ";
573
0
    OS << DirectUses[I]->getFullModuleName(true);
574
0
    OS << "\n";
575
0
  }
576
577
236
  for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; 
++I1
) {
578
1
    OS.indent(Indent + 2);
579
1
    OS << "use ";
580
1
    printModuleId(OS, UnresolvedDirectUses[I]);
581
1
    OS << "\n";
582
1
  }
583
584
304
  for (unsigned I = 0, N = LinkLibraries.size(); I != N; 
++I69
) {
585
69
    OS.indent(Indent + 2);
586
69
    OS << "link ";
587
69
    if (LinkLibraries[I].IsFramework)
588
69
      OS << "framework ";
589
69
    OS << "\"";
590
69
    OS.write_escaped(LinkLibraries[I].Library);
591
69
    OS << "\"";
592
69
  }
593
594
235
  for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; 
++I0
) {
595
0
    OS.indent(Indent + 2);
596
0
    OS << "conflict ";
597
0
    printModuleId(OS, UnresolvedConflicts[I].Id);
598
0
    OS << ", \"";
599
0
    OS.write_escaped(UnresolvedConflicts[I].Message);
600
0
    OS << "\"\n";
601
0
  }
602
603
235
  for (unsigned I = 0, N = Conflicts.size(); I != N; 
++I0
) {
604
0
    OS.indent(Indent + 2);
605
0
    OS << "conflict ";
606
0
    OS << Conflicts[I].Other->getFullModuleName(true);
607
0
    OS << ", \"";
608
0
    OS.write_escaped(Conflicts[I].Message);
609
0
    OS << "\"\n";
610
0
  }
611
612
235
  if (InferSubmodules) {
613
210
    OS.indent(Indent + 2);
614
210
    if (InferExplicitSubmodules)
615
0
      OS << "explicit ";
616
210
    OS << "module * {\n";
617
210
    if (InferExportWildcard) {
618
210
      OS.indent(Indent + 4);
619
210
      OS << "export *\n";
620
210
    }
621
210
    OS.indent(Indent + 2);
622
210
    OS << "}\n";
623
210
  }
624
625
235
  OS.indent(Indent);
626
235
  OS << "}\n";
627
235
}
628
629
0
LLVM_DUMP_METHOD void Module::dump() const {
630
0
  print(llvm::errs(), 0, true);
631
0
}
632
633
void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
634
274k
                                  VisibleCallback Vis, ConflictCallback Cb) {
635
274k
  assert(Loc.isValid() && "setVisible expects a valid import location");
636
274k
  if (isVisible(M))
637
226k
    return;
638
639
48.3k
  ++Generation;
640
641
48.3k
  struct Visiting {
642
48.3k
    Module *M;
643
48.3k
    Visiting *ExportedBy;
644
48.3k
  };
645
646
4.57M
  std::function<void(Visiting)> VisitModule = [&](Visiting V) {
647
    // Nothing to do for a module that's already visible.
648
4.57M
    unsigned ID = V.M->getVisibilityID();
649
4.57M
    if (ImportLocs.size() <= ID)
650
281k
      ImportLocs.resize(ID + 1);
651
4.29M
    else if (ImportLocs[ID].isValid())
652
3.21M
      return;
653
654
1.35M
    ImportLocs[ID] = Loc;
655
1.35M
    Vis(M);
656
657
    // Make any exported modules visible.
658
1.35M
    SmallVector<Module *, 16> Exports;
659
1.35M
    V.M->getExportedModules(Exports);
660
4.53M
    for (Module *E : Exports) {
661
      // Don't import non-importable modules.
662
4.53M
      if (!E->isUnimportable())
663
4.52M
        VisitModule({E, &V});
664
4.53M
    }
665
666
1.35M
    for (auto &C : V.M->Conflicts) {
667
2
      if (isVisible(C.Other)) {
668
2
        llvm::SmallVector<Module*, 8> Path;
669
4
        for (Visiting *I = &V; I; 
I = I->ExportedBy2
)
670
2
          Path.push_back(I->M);
671
2
        Cb(Path, C.Other, C.Message);
672
2
      }
673
2
    }
674
1.35M
  };
675
48.3k
  VisitModule({M, nullptr});
676
48.3k
}
677
678
ASTSourceDescriptor::ASTSourceDescriptor(Module &M)
679
607k
    : Signature(M.Signature), ClangModule(&M) {
680
607k
  if (M.Directory)
681
447k
    Path = M.Directory->getName();
682
607k
  if (auto File = M.getASTFile())
683
38.3k
    ASTFile = File->getName();
684
607k
}
685
686
27.1k
std::string ASTSourceDescriptor::getModuleName() const {
687
27.1k
  if (ClangModule)
688
27.0k
    return ClangModule->Name;
689
113
  else
690
113
    return std::string(PCHModuleName);
691
27.1k
}