Coverage Report

Created: 2020-02-18 08:44

/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), IsMissingRequirement(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.77M
      NameVisibility(Hidden) {
48
1.77M
  if (Parent) {
49
1.64M
    if (!Parent->isAvailable())
50
130k
      IsAvailable = false;
51
1.64M
    if (Parent->IsSystem)
52
1.53M
      IsSystem = true;
53
1.64M
    if (Parent->IsExternC)
54
1.36M
      IsExternC = true;
55
1.64M
    if (Parent->NoUndeclaredIncludes)
56
902k
      NoUndeclaredIncludes = true;
57
1.64M
    if (Parent->ModuleMapIsPrivate)
58
115
      ModuleMapIsPrivate = true;
59
1.64M
    IsMissingRequirement = Parent->IsMissingRequirement;
60
1.64M
61
1.64M
    Parent->SubModuleIndex[Name] = Parent->SubModules.size();
62
1.64M
    Parent->SubModules.push_back(this);
63
1.64M
  }
64
1.77M
}
65
66
807k
Module::~Module() {
67
807k
  for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
68
1.51M
       I != IEnd; 
++I709k
) {
69
709k
    delete *I;
70
709k
  }
71
807k
}
72
73
64.6k
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
74
64.6k
  StringRef Platform = Target.getPlatformName();
75
64.6k
  StringRef Env = Target.getTriple().getEnvironmentName();
76
64.6k
77
64.6k
  // Attempt to match platform and environment.
78
64.6k
  if (Platform == Feature || 
Target.getTriple().getOSName() == Feature64.6k
||
79
64.6k
      
Env == Feature64.6k
)
80
58
    return true;
81
64.5k
82
64.5k
  auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
83
3
    auto Pos = LHS.find("-");
84
3
    if (Pos == StringRef::npos)
85
0
      return false;
86
3
    SmallString<128> NewLHS = LHS.slice(0, Pos);
87
3
    NewLHS += LHS.slice(Pos+1, LHS.size());
88
3
    return NewLHS == RHS;
89
3
  };
90
64.5k
91
64.5k
  SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
92
64.5k
  // Darwin has different but equivalent variants for simulators, example:
93
64.5k
  //   1. x86_64-apple-ios-simulator
94
64.5k
  //   2. x86_64-apple-iossimulator
95
64.5k
  // where both are valid examples of the same platform+environment but in the
96
64.5k
  // variant (2) the simulator is hardcoded as part of the platform name. Both
97
64.5k
  // forms above should match for "iossimulator" requirement.
98
64.5k
  if (Target.getTriple().isOSDarwin() && 
PlatformEnv.endswith("simulator")63.7k
)
99
3
    return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
100
64.5k
101
64.5k
  return PlatformEnv == Feature;
102
64.5k
}
103
104
/// Determine whether a translation unit built using the current
105
/// language options has the given feature.
106
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
107
68.3k
                       const TargetInfo &Target) {
108
68.3k
  bool HasFeature = llvm::StringSwitch<bool>(Feature)
109
68.3k
                        .Case("altivec", LangOpts.AltiVec)
110
68.3k
                        .Case("blocks", LangOpts.Blocks)
111
68.3k
                        .Case("coroutines", LangOpts.Coroutines)
112
68.3k
                        .Case("cplusplus", LangOpts.CPlusPlus)
113
68.3k
                        .Case("cplusplus11", LangOpts.CPlusPlus11)
114
68.3k
                        .Case("cplusplus14", LangOpts.CPlusPlus14)
115
68.3k
                        .Case("cplusplus17", LangOpts.CPlusPlus17)
116
68.3k
                        .Case("c99", LangOpts.C99)
117
68.3k
                        .Case("c11", LangOpts.C11)
118
68.3k
                        .Case("c17", LangOpts.C17)
119
68.3k
                        .Case("freestanding", LangOpts.Freestanding)
120
68.3k
                        .Case("gnuinlineasm", LangOpts.GNUAsm)
121
68.3k
                        .Case("objc", LangOpts.ObjC)
122
68.3k
                        .Case("objc_arc", LangOpts.ObjCAutoRefCount)
123
68.3k
                        .Case("opencl", LangOpts.OpenCL)
124
68.3k
                        .Case("tls", Target.isTLSSupported())
125
68.3k
                        .Case("zvector", LangOpts.ZVector)
126
68.3k
                        .Default(Target.hasFeature(Feature) ||
127
68.3k
                                 
isPlatformEnvironment(Target, Feature)64.6k
);
128
68.3k
  if (!HasFeature)
129
43.5k
    HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
130
43.5k
                           LangOpts.ModuleFeatures.end(),
131
43.5k
                           Feature) != LangOpts.ModuleFeatures.end();
132
68.3k
  return HasFeature;
133
68.3k
}
134
135
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
136
                         Requirement &Req,
137
                         UnresolvedHeaderDirective &MissingHeader,
138
31.5k
                         Module *&ShadowingModule) const {
139
31.5k
  if (IsAvailable)
140
31.4k
    return true;
141
67
142
68
  
for (const Module *Current = this; 67
Current;
Current = Current->Parent1
) {
143
66
    if (Current->ShadowingModule) {
144
1
      ShadowingModule = Current->ShadowingModule;
145
1
      return false;
146
1
    }
147
65
    for (unsigned I = 0, N = Current->Requirements.size(); I != N; 
++I0
) {
148
62
      if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
149
62
              Current->Requirements[I].second) {
150
62
        Req = Current->Requirements[I];
151
62
        return false;
152
62
      }
153
62
    }
154
65
    
if (3
!Current->MissingHeaders.empty()3
) {
155
2
      MissingHeader = Current->MissingHeaders.front();
156
2
      return false;
157
2
    }
158
3
  }
159
67
160
67
  
llvm_unreachable2
("could not find a reason why module is unavailable");
161
67
}
162
163
57.9k
bool Module::isSubModuleOf(const Module *Other) const {
164
57.9k
  const Module *This = this;
165
193k
  do {
166
193k
    if (This == Other)
167
57.3k
      return true;
168
136k
169
136k
    This = This->Parent;
170
136k
  } while (This);
171
57.9k
172
57.9k
  
return false601
;
173
57.9k
}
174
175
6.98M
const Module *Module::getTopLevelModule() const {
176
6.98M
  const Module *Result = this;
177
18.4M
  while (Result->Parent)
178
11.4M
    Result = Result->Parent;
179
6.98M
180
6.98M
  return Result;
181
6.98M
}
182
183
static StringRef getModuleNameFromComponent(
184
3
    const std::pair<std::string, SourceLocation> &IdComponent) {
185
3
  return IdComponent.first;
186
3
}
187
188
10.9k
static StringRef getModuleNameFromComponent(StringRef R) { return R; }
189
190
template<typename InputIter>
191
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
192
5.19k
                          bool AllowStringLiterals = true) {
193
16.1k
  for (InputIter It = Begin; It != End; 
++It10.9k
) {
194
10.9k
    if (It != Begin)
195
5.74k
      OS << ".";
196
10.9k
197
10.9k
    StringRef Name = getModuleNameFromComponent(*It);
198
10.9k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)507
)
199
10.9k
      OS << Name;
200
10
    else {
201
10
      OS << '"';
202
10
      OS.write_escaped(Name);
203
10
      OS << '"';
204
10
    }
205
10.9k
  }
206
5.19k
}
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
192
4.94k
                          bool AllowStringLiterals = true) {
193
15.6k
  for (InputIter It = Begin; It != End; 
++It10.6k
) {
194
10.6k
    if (It != Begin)
195
5.74k
      OS << ".";
196
10.6k
197
10.6k
    StringRef Name = getModuleNameFromComponent(*It);
198
10.6k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)261
)
199
10.6k
      OS << Name;
200
8
    else {
201
8
      OS << '"';
202
8
      OS.write_escaped(Name);
203
8
      OS << '"';
204
8
    }
205
10.6k
  }
206
4.94k
}
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
192
243
                          bool AllowStringLiterals = true) {
193
486
  for (InputIter It = Begin; It != End; 
++It243
) {
194
243
    if (It != Begin)
195
0
      OS << ".";
196
243
197
243
    StringRef Name = getModuleNameFromComponent(*It);
198
243
    if (!AllowStringLiterals || isValidIdentifier(Name))
199
241
      OS << Name;
200
2
    else {
201
2
      OS << '"';
202
2
      OS.write_escaped(Name);
203
2
      OS << '"';
204
2
    }
205
243
  }
206
243
}
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
192
14
                          bool AllowStringLiterals = true) {
193
17
  for (InputIter It = Begin; It != End; 
++It3
) {
194
3
    if (It != Begin)
195
0
      OS << ".";
196
3
197
3
    StringRef Name = getModuleNameFromComponent(*It);
198
3
    if (!AllowStringLiterals || isValidIdentifier(Name))
199
3
      OS << Name;
200
0
    else {
201
0
      OS << '"';
202
0
      OS.write_escaped(Name);
203
0
      OS << '"';
204
0
    }
205
3
  }
206
14
}
207
208
template<typename Container>
209
14
static void printModuleId(raw_ostream &OS, const Container &C) {
210
14
  return printModuleId(OS, C.begin(), C.end());
211
14
}
212
213
4.94k
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
214
4.94k
  SmallVector<StringRef, 2> Names;
215
4.94k
216
4.94k
  // Build up the set of module names (from innermost to outermost).
217
15.6k
  for (const Module *M = this; M; 
M = M->Parent10.6k
)
218
10.6k
    Names.push_back(M->Name);
219
4.94k
220
4.94k
  std::string Result;
221
4.94k
222
4.94k
  llvm::raw_string_ostream Out(Result);
223
4.94k
  printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
224
4.94k
  Out.flush();
225
4.94k
226
4.94k
  return Result;
227
4.94k
}
228
229
15.6k
bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
230
15.9k
  for (const Module *M = this; M; 
M = M->Parent280
) {
231
15.8k
    if (nameParts.empty() || M->Name != nameParts.back())
232
15.5k
      return false;
233
280
    nameParts = nameParts.drop_back();
234
280
  }
235
15.6k
  
return nameParts.empty()120
;
236
15.6k
}
237
238
115k
Module::DirectoryName Module::getUmbrellaDir() const {
239
115k
  if (Header U = getUmbrellaHeader())
240
16.4k
    return {"", U.Entry->getDir()};
241
98.8k
242
98.8k
  return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
243
98.8k
}
244
245
25.3k
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
246
25.3k
  if (!TopHeaderNames.empty()) {
247
1
    for (std::vector<std::string>::iterator
248
3
           I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; 
++I2
) {
249
2
      if (auto FE = FileMgr.getFile(*I))
250
2
        TopHeaders.insert(*FE);
251
2
    }
252
1
    TopHeaderNames.clear();
253
1
  }
254
25.3k
255
25.3k
  return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
256
25.3k
}
257
258
57.4k
bool Module::directlyUses(const Module *Requested) const {
259
57.4k
  auto *Top = getTopLevelModule();
260
57.4k
261
57.4k
  // A top-level module implicitly uses itself.
262
57.4k
  if (Requested->isSubModuleOf(Top))
263
56.9k
    return true;
264
495
265
495
  for (auto *Use : Top->DirectUses)
266
86
    if (Requested->isSubModuleOf(Use))
267
26
      return true;
268
495
269
495
  // Anyone is allowed to use our builtin stddef.h and its accompanying module.
270
495
  
if (469
!Requested->Parent469
&&
Requested->Name == "_Builtin_stddef_max_align_t"209
)
271
197
    return true;
272
272
273
272
  return false;
274
272
}
275
276
void Module::addRequirement(StringRef Feature, bool RequiredState,
277
                            const LangOptions &LangOpts,
278
68.3k
                            const TargetInfo &Target) {
279
68.3k
  Requirements.push_back(Requirement(std::string(Feature), RequiredState));
280
68.3k
281
68.3k
  // If this feature is currently available, we're done.
282
68.3k
  if (hasFeature(Feature, LangOpts, Target) == RequiredState)
283
30.2k
    return;
284
38.0k
285
38.0k
  markUnavailable(/*MissingRequirement*/true);
286
38.0k
}
287
288
222k
void Module::markUnavailable(bool MissingRequirement) {
289
255k
  auto needUpdate = [MissingRequirement](Module *M) {
290
255k
    return M->IsAvailable || 
(197k
!M->IsMissingRequirement197k
&&
MissingRequirement182k
);
291
255k
  };
292
222k
293
222k
  if (!needUpdate(this))
294
192k
    return;
295
29.7k
296
29.7k
  SmallVector<Module *, 2> Stack;
297
29.7k
  Stack.push_back(this);
298
59.5k
  while (!Stack.empty()) {
299
29.7k
    Module *Current = Stack.back();
300
29.7k
    Stack.pop_back();
301
29.7k
302
29.7k
    if (!needUpdate(Current))
303
0
      continue;
304
29.7k
305
29.7k
    Current->IsAvailable = false;
306
29.7k
    Current->IsMissingRequirement |= MissingRequirement;
307
29.7k
    for (submodule_iterator Sub = Current->submodule_begin(),
308
29.7k
                         SubEnd = Current->submodule_end();
309
33.4k
         Sub != SubEnd; 
++Sub3.65k
) {
310
3.65k
      if (needUpdate(*Sub))
311
6
        Stack.push_back(*Sub);
312
3.65k
    }
313
29.7k
  }
314
29.7k
}
315
316
3.65M
Module *Module::findSubmodule(StringRef Name) const {
317
3.65M
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
318
3.65M
  if (Pos == SubModuleIndex.end())
319
2.82M
    return nullptr;
320
825k
321
825k
  return SubModules[Pos->getValue()];
322
825k
}
323
324
48
Module *Module::findOrInferSubmodule(StringRef Name) {
325
48
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
326
48
  if (Pos != SubModuleIndex.end())
327
47
    return SubModules[Pos->getValue()];
328
1
  if (!InferSubmodules)
329
0
    return nullptr;
330
1
  Module *Result = new Module(Name, SourceLocation(), this, false, InferExplicitSubmodules, 0);
331
1
  Result->InferExplicitSubmodules = InferExplicitSubmodules;
332
1
  Result->InferSubmodules = InferSubmodules;
333
1
  Result->InferExportWildcard = InferExportWildcard;
334
1
  if (Result->InferExportWildcard)
335
1
    Result->Exports.push_back(Module::ExportDecl(nullptr, true));
336
1
  return Result;
337
1
}
338
339
2.01M
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
340
2.01M
  // All non-explicit submodules are exported.
341
2.01M
  for (std::vector<Module *>::const_iterator I = SubModules.begin(),
342
2.01M
                                             E = SubModules.end();
343
3.59M
       I != E; 
++I1.57M
) {
344
1.57M
    Module *Mod = *I;
345
1.57M
    if (!Mod->IsExplicit)
346
1.53M
      Exported.push_back(Mod);
347
1.57M
  }
348
2.01M
349
2.01M
  // Find re-exported modules by filtering the list of imported modules.
350
2.01M
  bool AnyWildcard = false;
351
2.01M
  bool UnrestrictedWildcard = false;
352
2.01M
  SmallVector<Module *, 4> WildcardRestrictions;
353
4.04M
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I2.02M
) {
354
2.02M
    Module *Mod = Exports[I].getPointer();
355
2.02M
    if (!Exports[I].getInt()) {
356
89.9k
      // Export a named module directly; no wildcards involved.
357
89.9k
      Exported.push_back(Mod);
358
89.9k
359
89.9k
      continue;
360
89.9k
    }
361
1.93M
362
1.93M
    // Wildcard export: export all of the imported modules that match
363
1.93M
    // the given pattern.
364
1.93M
    AnyWildcard = true;
365
1.93M
    if (UnrestrictedWildcard)
366
8.92k
      continue;
367
1.92M
368
1.92M
    if (Module *Restriction = Exports[I].getPointer())
369
3
      WildcardRestrictions.push_back(Restriction);
370
1.92M
    else {
371
1.92M
      WildcardRestrictions.clear();
372
1.92M
      UnrestrictedWildcard = true;
373
1.92M
    }
374
1.92M
  }
375
2.01M
376
2.01M
  // If there were any wildcards, push any imported modules that were
377
2.01M
  // re-exported by the wildcard restriction.
378
2.01M
  if (!AnyWildcard)
379
90.2k
    return;
380
1.92M
381
7.93M
  
for (unsigned I = 0, N = Imports.size(); 1.92M
I != N;
++I6.00M
) {
382
6.00M
    Module *Mod = Imports[I];
383
6.00M
    bool Acceptable = UnrestrictedWildcard;
384
6.00M
    if (!Acceptable) {
385
6
      // Check whether this module meets one of the restrictions.
386
9
      for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; 
++R3
) {
387
6
        Module *Restriction = WildcardRestrictions[R];
388
6
        if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
389
3
          Acceptable = true;
390
3
          break;
391
3
        }
392
6
      }
393
6
    }
394
6.00M
395
6.00M
    if (!Acceptable)
396
3
      continue;
397
6.00M
398
6.00M
    Exported.push_back(Mod);
399
6.00M
  }
400
1.92M
}
401
402
225
void Module::buildVisibleModulesCache() const {
403
225
  assert(VisibleModulesCache.empty() && "cache does not need building");
404
225
405
225
  // This module is visible to itself.
406
225
  VisibleModulesCache.insert(this);
407
225
408
225
  // Every imported module is visible.
409
225
  SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
410
98.1k
  while (!Stack.empty()) {
411
97.9k
    Module *CurrModule = Stack.pop_back_val();
412
97.9k
413
97.9k
    // Every module transitively exported by an imported module is visible.
414
97.9k
    if (VisibleModulesCache.insert(CurrModule).second)
415
32.5k
      CurrModule->getExportedModules(Stack);
416
97.9k
  }
417
225
}
418
419
243
void Module::print(raw_ostream &OS, unsigned Indent) const {
420
243
  OS.indent(Indent);
421
243
  if (IsFramework)
422
218
    OS << "framework ";
423
243
  if (IsExplicit)
424
0
    OS << "explicit ";
425
243
  OS << "module ";
426
243
  printModuleId(OS, &Name, &Name + 1);
427
243
428
243
  if (IsSystem || 
IsExternC172
) {
429
76
    OS.indent(Indent + 2);
430
76
    if (IsSystem)
431
71
      OS << " [system]";
432
76
    if (IsExternC)
433
74
      OS << " [extern_c]";
434
76
  }
435
243
436
243
  OS << " {\n";
437
243
438
243
  if (!Requirements.empty()) {
439
0
    OS.indent(Indent + 2);
440
0
    OS << "requires ";
441
0
    for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
442
0
      if (I)
443
0
        OS << ", ";
444
0
      if (!Requirements[I].second)
445
0
        OS << "!";
446
0
      OS << Requirements[I].first;
447
0
    }
448
0
    OS << "\n";
449
0
  }
450
243
451
243
  if (Header H = getUmbrellaHeader()) {
452
218
    OS.indent(Indent + 2);
453
218
    OS << "umbrella header \"";
454
218
    OS.write_escaped(H.NameAsWritten);
455
218
    OS << "\"\n";
456
218
  } else 
if (DirectoryName 25
D25
= getUmbrellaDir()) {
457
0
    OS.indent(Indent + 2);
458
0
    OS << "umbrella \"";
459
0
    OS.write_escaped(D.NameAsWritten);
460
0
    OS << "\"\n";
461
0
  }
462
243
463
243
  if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
464
0
    OS.indent(Indent + 2);
465
0
    OS << "config_macros ";
466
0
    if (ConfigMacrosExhaustive)
467
0
      OS << "[exhaustive]";
468
0
    for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
469
0
      if (I)
470
0
        OS << ", ";
471
0
      OS << ConfigMacros[I];
472
0
    }
473
0
    OS << "\n";
474
0
  }
475
243
476
243
  struct {
477
243
    StringRef Prefix;
478
243
    HeaderKind Kind;
479
243
  } Kinds[] = {{"", HK_Normal},
480
243
               {"textual ", HK_Textual},
481
243
               {"private ", HK_Private},
482
243
               {"private textual ", HK_PrivateTextual},
483
243
               {"exclude ", HK_Excluded}};
484
243
485
1.21k
  for (auto &K : Kinds) {
486
1.21k
    assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
487
1.21k
    for (auto &H : Headers[K.Kind]) {
488
41
      OS.indent(Indent + 2);
489
41
      OS << K.Prefix << "header \"";
490
41
      OS.write_escaped(H.NameAsWritten);
491
41
      OS << "\" { size " << H.Entry->getSize()
492
41
         << " mtime " << H.Entry->getModificationTime() << " }\n";
493
41
    }
494
1.21k
  }
495
486
  for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
496
486
    for (auto &U : *Unresolved) {
497
0
      OS.indent(Indent + 2);
498
0
      OS << Kinds[U.Kind].Prefix << "header \"";
499
0
      OS.write_escaped(U.FileName);
500
0
      OS << "\"";
501
0
      if (U.Size || U.ModTime) {
502
0
        OS << " {";
503
0
        if (U.Size)
504
0
          OS << " size " << *U.Size;
505
0
        if (U.ModTime)
506
0
          OS << " mtime " << *U.ModTime;
507
0
        OS << " }";
508
0
      }
509
0
      OS << "\n";
510
0
    }
511
486
  }
512
243
513
243
  if (!ExportAsModule.empty()) {
514
0
    OS.indent(Indent + 2);
515
0
    OS << "export_as" << ExportAsModule << "\n";
516
0
  }
517
243
518
243
  for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
519
372
       MI != MIEnd; 
++MI129
)
520
129
    // Print inferred subframework modules so that we don't need to re-infer
521
129
    // them (requires expensive directory iteration + stat calls) when we build
522
129
    // the module. Regular inferred submodules are OK, as we need to look at all
523
129
    // those header files anyway.
524
129
    if (!(*MI)->IsInferred || 
(*MI)->IsFramework123
)
525
128
      (*MI)->print(OS, Indent + 2);
526
243
527
461
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I218
) {
528
218
    OS.indent(Indent + 2);
529
218
    OS << "export ";
530
218
    if (Module *Restriction = Exports[I].getPointer()) {
531
0
      OS << Restriction->getFullModuleName(true);
532
0
      if (Exports[I].getInt())
533
0
        OS << ".*";
534
218
    } else {
535
218
      OS << "*";
536
218
    }
537
218
    OS << "\n";
538
218
  }
539
243
540
256
  for (unsigned I = 0, N = UnresolvedExports.size(); I != N; 
++I13
) {
541
13
    OS.indent(Indent + 2);
542
13
    OS << "export ";
543
13
    printModuleId(OS, UnresolvedExports[I].Id);
544
13
    if (UnresolvedExports[I].Wildcard)
545
11
      OS << (UnresolvedExports[I].Id.empty() ? "*" : 
".*"0
);
546
13
    OS << "\n";
547
13
  }
548
243
549
243
  for (unsigned I = 0, N = DirectUses.size(); I != N; 
++I0
) {
550
0
    OS.indent(Indent + 2);
551
0
    OS << "use ";
552
0
    OS << DirectUses[I]->getFullModuleName(true);
553
0
    OS << "\n";
554
0
  }
555
243
556
244
  for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; 
++I1
) {
557
1
    OS.indent(Indent + 2);
558
1
    OS << "use ";
559
1
    printModuleId(OS, UnresolvedDirectUses[I]);
560
1
    OS << "\n";
561
1
  }
562
243
563
312
  for (unsigned I = 0, N = LinkLibraries.size(); I != N; 
++I69
) {
564
69
    OS.indent(Indent + 2);
565
69
    OS << "link ";
566
69
    if (LinkLibraries[I].IsFramework)
567
69
      OS << "framework ";
568
69
    OS << "\"";
569
69
    OS.write_escaped(LinkLibraries[I].Library);
570
69
    OS << "\"";
571
69
  }
572
243
573
243
  for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; 
++I0
) {
574
0
    OS.indent(Indent + 2);
575
0
    OS << "conflict ";
576
0
    printModuleId(OS, UnresolvedConflicts[I].Id);
577
0
    OS << ", \"";
578
0
    OS.write_escaped(UnresolvedConflicts[I].Message);
579
0
    OS << "\"\n";
580
0
  }
581
243
582
243
  for (unsigned I = 0, N = Conflicts.size(); I != N; 
++I0
) {
583
0
    OS.indent(Indent + 2);
584
0
    OS << "conflict ";
585
0
    OS << Conflicts[I].Other->getFullModuleName(true);
586
0
    OS << ", \"";
587
0
    OS.write_escaped(Conflicts[I].Message);
588
0
    OS << "\"\n";
589
0
  }
590
243
591
243
  if (InferSubmodules) {
592
218
    OS.indent(Indent + 2);
593
218
    if (InferExplicitSubmodules)
594
0
      OS << "explicit ";
595
218
    OS << "module * {\n";
596
218
    if (InferExportWildcard) {
597
218
      OS.indent(Indent + 4);
598
218
      OS << "export *\n";
599
218
    }
600
218
    OS.indent(Indent + 2);
601
218
    OS << "}\n";
602
218
  }
603
243
604
243
  OS.indent(Indent);
605
243
  OS << "}\n";
606
243
}
607
608
0
LLVM_DUMP_METHOD void Module::dump() const {
609
0
  print(llvm::errs());
610
0
}
611
612
void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
613
285k
                                  VisibleCallback Vis, ConflictCallback Cb) {
614
285k
  assert(Loc.isValid() && "setVisible expects a valid import location");
615
285k
  if (isVisible(M))
616
230k
    return;
617
55.1k
618
55.1k
  ++Generation;
619
55.1k
620
55.1k
  struct Visiting {
621
55.1k
    Module *M;
622
55.1k
    Visiting *ExportedBy;
623
55.1k
  };
624
55.1k
625
3.60M
  std::function<void(Visiting)> VisitModule = [&](Visiting V) {
626
3.60M
    // Nothing to do for a module that's already visible.
627
3.60M
    unsigned ID = V.M->getVisibilityID();
628
3.60M
    if (ImportLocs.size() <= ID)
629
320k
      ImportLocs.resize(ID + 1);
630
3.28M
    else if (ImportLocs[ID].isValid())
631
2.63M
      return;
632
971k
633
971k
    ImportLocs[ID] = Loc;
634
971k
    Vis(M);
635
971k
636
971k
    // Make any exported modules visible.
637
971k
    SmallVector<Module *, 16> Exports;
638
971k
    V.M->getExportedModules(Exports);
639
3.55M
    for (Module *E : Exports) {
640
3.55M
      // Don't recurse to unavailable submodules.
641
3.55M
      if (E->isAvailable())
642
3.54M
        VisitModule({E, &V});
643
3.55M
    }
644
971k
645
971k
    for (auto &C : V.M->Conflicts) {
646
2
      if (isVisible(C.Other)) {
647
2
        llvm::SmallVector<Module*, 8> Path;
648
4
        for (Visiting *I = &V; I; 
I = I->ExportedBy2
)
649
2
          Path.push_back(I->M);
650
2
        Cb(Path, C.Other, C.Message);
651
2
      }
652
2
    }
653
971k
  };
654
55.1k
  VisitModule({M, nullptr});
655
55.1k
}