Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
115k
      NameVisibility(Hidden) {
48
115k
  if (Parent) {
49
41.0k
    if (!Parent->isAvailable())
50
3.12k
      IsAvailable = false;
51
41.0k
    if (Parent->IsSystem)
52
4.23k
      IsSystem = true;
53
41.0k
    if (Parent->IsExternC)
54
3.94k
      IsExternC = true;
55
41.0k
    if (Parent->NoUndeclaredIncludes)
56
1.11k
      NoUndeclaredIncludes = true;
57
41.0k
    if (Parent->ModuleMapIsPrivate)
58
115
      ModuleMapIsPrivate = true;
59
41.0k
    IsMissingRequirement = Parent->IsMissingRequirement;
60
41.0k
61
41.0k
    Parent->SubModuleIndex[Name] = Parent->SubModules.size();
62
41.0k
    Parent->SubModules.push_back(this);
63
41.0k
  }
64
115k
}
65
66
114k
Module::~Module() {
67
114k
  for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
68
154k
       I != IEnd; 
++I40.3k
) {
69
40.3k
    delete *I;
70
40.3k
  }
71
114k
}
72
73
8.74k
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
74
8.74k
  StringRef Platform = Target.getPlatformName();
75
8.74k
  StringRef Env = Target.getTriple().getEnvironmentName();
76
8.74k
77
8.74k
  // Attempt to match platform and environment.
78
8.74k
  if (Platform == Feature || 
Target.getTriple().getOSName() == Feature8.72k
||
79
8.74k
      
Env == Feature8.70k
)
80
58
    return true;
81
8.68k
82
8.68k
  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
8.68k
91
8.68k
  SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
92
8.68k
  // Darwin has different but equivalent variants for simulators, example:
93
8.68k
  //   1. x86_64-apple-ios-simulator
94
8.68k
  //   2. x86_64-apple-iossimulator
95
8.68k
  // where both are valid examples of the same platform+environment but in the
96
8.68k
  // variant (2) the simulator is hardcoded as part of the platform name. Both
97
8.68k
  // forms above should match for "iossimulator" requirement.
98
8.68k
  if (Target.getTriple().isOSDarwin() && 
PlatformEnv.endswith("simulator")7.86k
)
99
3
    return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
100
8.67k
101
8.67k
  return PlatformEnv == Feature;
102
8.67k
}
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
10.0k
                       const TargetInfo &Target) {
108
10.0k
  bool HasFeature = llvm::StringSwitch<bool>(Feature)
109
10.0k
                        .Case("altivec", LangOpts.AltiVec)
110
10.0k
                        .Case("blocks", LangOpts.Blocks)
111
10.0k
                        .Case("coroutines", LangOpts.Coroutines)
112
10.0k
                        .Case("cplusplus", LangOpts.CPlusPlus)
113
10.0k
                        .Case("cplusplus11", LangOpts.CPlusPlus11)
114
10.0k
                        .Case("cplusplus14", LangOpts.CPlusPlus14)
115
10.0k
                        .Case("cplusplus17", LangOpts.CPlusPlus17)
116
10.0k
                        .Case("c99", LangOpts.C99)
117
10.0k
                        .Case("c11", LangOpts.C11)
118
10.0k
                        .Case("c17", LangOpts.C17)
119
10.0k
                        .Case("freestanding", LangOpts.Freestanding)
120
10.0k
                        .Case("gnuinlineasm", LangOpts.GNUAsm)
121
10.0k
                        .Case("objc", LangOpts.ObjC)
122
10.0k
                        .Case("objc_arc", LangOpts.ObjCAutoRefCount)
123
10.0k
                        .Case("opencl", LangOpts.OpenCL)
124
10.0k
                        .Case("tls", Target.isTLSSupported())
125
10.0k
                        .Case("zvector", LangOpts.ZVector)
126
10.0k
                        .Default(Target.hasFeature(Feature) ||
127
10.0k
                                 
isPlatformEnvironment(Target, Feature)8.74k
);
128
10.0k
  if (!HasFeature)
129
7.89k
    HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
130
7.89k
                           LangOpts.ModuleFeatures.end(),
131
7.89k
                           Feature) != LangOpts.ModuleFeatures.end();
132
10.0k
  return HasFeature;
133
10.0k
}
134
135
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
136
                         Requirement &Req,
137
                         UnresolvedHeaderDirective &MissingHeader,
138
5.41k
                         Module *&ShadowingModule) const {
139
5.41k
  if (IsAvailable)
140
5.34k
    return true;
141
65
142
66
  
for (const Module *Current = this; 65
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
65
160
65
  
llvm_unreachable0
("could not find a reason why module is unavailable");
161
65
}
162
163
618
bool Module::isSubModuleOf(const Module *Other) const {
164
618
  const Module *This = this;
165
894
  do {
166
894
    if (This == Other)
167
465
      return true;
168
429
169
429
    This = This->Parent;
170
429
  } while (This);
171
618
172
618
  
return false153
;
173
618
}
174
175
105k
const Module *Module::getTopLevelModule() const {
176
105k
  const Module *Result = this;
177
141k
  while (Result->Parent)
178
35.5k
    Result = Result->Parent;
179
105k
180
105k
  return Result;
181
105k
}
182
183
static StringRef getModuleNameFromComponent(
184
3
    const std::pair<std::string, SourceLocation> &IdComponent) {
185
3
  return IdComponent.first;
186
3
}
187
188
4.17k
static StringRef getModuleNameFromComponent(StringRef R) { return R; }
189
190
template<typename InputIter>
191
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
192
2.84k
                          bool AllowStringLiterals = true) {
193
7.01k
  for (InputIter It = Begin; It != End; 
++It4.17k
) {
194
4.17k
    if (It != Begin)
195
1.34k
      OS << ".";
196
4.17k
197
4.17k
    StringRef Name = getModuleNameFromComponent(*It);
198
4.17k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)438
)
199
4.16k
      OS << Name;
200
10
    else {
201
10
      OS << '"';
202
10
      OS.write_escaped(Name);
203
10
      OS << '"';
204
10
    }
205
4.17k
  }
206
2.84k
}
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
2.65k
                          bool AllowStringLiterals = true) {
193
6.64k
  for (InputIter It = Begin; It != End; 
++It3.99k
) {
194
3.99k
    if (It != Begin)
195
1.34k
      OS << ".";
196
3.99k
197
3.99k
    StringRef Name = getModuleNameFromComponent(*It);
198
3.99k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)261
)
199
3.98k
      OS << Name;
200
8
    else {
201
8
      OS << '"';
202
8
      OS.write_escaped(Name);
203
8
      OS << '"';
204
8
    }
205
3.99k
  }
206
2.65k
}
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
174
                          bool AllowStringLiterals = true) {
193
348
  for (InputIter It = Begin; It != End; 
++It174
) {
194
174
    if (It != Begin)
195
0
      OS << ".";
196
174
197
174
    StringRef Name = getModuleNameFromComponent(*It);
198
174
    if (!AllowStringLiterals || isValidIdentifier(Name))
199
172
      OS << Name;
200
2
    else {
201
2
      OS << '"';
202
2
      OS.write_escaped(Name);
203
2
      OS << '"';
204
2
    }
205
174
  }
206
174
}
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
2.65k
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
214
2.65k
  SmallVector<StringRef, 2> Names;
215
2.65k
216
2.65k
  // Build up the set of module names (from innermost to outermost).
217
6.64k
  for (const Module *M = this; M; 
M = M->Parent3.99k
)
218
3.99k
    Names.push_back(M->Name);
219
2.65k
220
2.65k
  std::string Result;
221
2.65k
222
2.65k
  llvm::raw_string_ostream Out(Result);
223
2.65k
  printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
224
2.65k
  Out.flush();
225
2.65k
226
2.65k
  return Result;
227
2.65k
}
228
229
1.10k
bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
230
1.32k
  for (const Module *M = this; M; 
M = M->Parent224
) {
231
1.22k
    if (nameParts.empty() || M->Name != nameParts.back())
232
1.00k
      return false;
233
224
    nameParts = nameParts.drop_back();
234
224
  }
235
1.10k
  
return nameParts.empty()96
;
236
1.10k
}
237
238
7.00k
Module::DirectoryName Module::getUmbrellaDir() const {
239
7.00k
  if (Header U = getUmbrellaHeader())
240
1.20k
    return {"", U.Entry->getDir()};
241
5.80k
242
5.80k
  return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
243
5.80k
}
244
245
3.45k
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
246
3.45k
  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 (const FileEntry *FE = FileMgr.getFile(*I))
250
2
        TopHeaders.insert(FE);
251
2
    }
252
1
    TopHeaderNames.clear();
253
1
  }
254
3.45k
255
3.45k
  return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
256
3.45k
}
257
258
112
bool Module::directlyUses(const Module *Requested) const {
259
112
  auto *Top = getTopLevelModule();
260
112
261
112
  // A top-level module implicitly uses itself.
262
112
  if (Requested->isSubModuleOf(Top))
263
65
    return true;
264
47
265
47
  for (auto *Use : Top->DirectUses)
266
86
    if (Requested->isSubModuleOf(Use))
267
26
      return true;
268
47
269
47
  // Anyone is allowed to use our builtin stddef.h and its accompanying module.
270
47
  
if (21
!Requested->Parent21
&&
Requested->Name == "_Builtin_stddef_max_align_t"13
)
271
1
    return true;
272
20
273
20
  return false;
274
20
}
275
276
void Module::addRequirement(StringRef Feature, bool RequiredState,
277
                            const LangOptions &LangOpts,
278
9.98k
                            const TargetInfo &Target) {
279
9.98k
  Requirements.push_back(Requirement(Feature, RequiredState));
280
9.98k
281
9.98k
  // If this feature is currently available, we're done.
282
9.98k
  if (hasFeature(Feature, LangOpts, Target) == RequiredState)
283
2.31k
    return;
284
7.66k
285
7.66k
  markUnavailable(/*MissingRequirement*/true);
286
7.66k
}
287
288
9.55k
void Module::markUnavailable(bool MissingRequirement) {
289
15.4k
  auto needUpdate = [MissingRequirement](Module *M) {
290
15.4k
    return M->IsAvailable || 
(5.15k
!M->IsMissingRequirement5.15k
&&
MissingRequirement1.64k
);
291
15.4k
  };
292
9.55k
293
9.55k
  if (!needUpdate(this))
294
3.79k
    return;
295
5.75k
296
5.75k
  SmallVector<Module *, 2> Stack;
297
5.75k
  Stack.push_back(this);
298
11.5k
  while (!Stack.empty()) {
299
5.76k
    Module *Current = Stack.back();
300
5.76k
    Stack.pop_back();
301
5.76k
302
5.76k
    if (!needUpdate(Current))
303
0
      continue;
304
5.76k
305
5.76k
    Current->IsAvailable = false;
306
5.76k
    Current->IsMissingRequirement |= MissingRequirement;
307
5.76k
    for (submodule_iterator Sub = Current->submodule_begin(),
308
5.76k
                         SubEnd = Current->submodule_end();
309
5.90k
         Sub != SubEnd; 
++Sub142
) {
310
142
      if (needUpdate(*Sub))
311
6
        Stack.push_back(*Sub);
312
142
    }
313
5.76k
  }
314
5.75k
}
315
316
84.3k
Module *Module::findSubmodule(StringRef Name) const {
317
84.3k
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
318
84.3k
  if (Pos == SubModuleIndex.end())
319
80.9k
    return nullptr;
320
3.44k
321
3.44k
  return SubModules[Pos->getValue()];
322
3.44k
}
323
324
47
Module *Module::findOrInferSubmodule(StringRef Name) {
325
47
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
326
47
  if (Pos != SubModuleIndex.end())
327
46
    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
19.9k
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
340
19.9k
  // All non-explicit submodules are exported.
341
19.9k
  for (std::vector<Module *>::const_iterator I = SubModules.begin(),
342
19.9k
                                             E = SubModules.end();
343
26.5k
       I != E; 
++I6.56k
) {
344
6.56k
    Module *Mod = *I;
345
6.56k
    if (!Mod->IsExplicit)
346
2.42k
      Exported.push_back(Mod);
347
6.56k
  }
348
19.9k
349
19.9k
  // Find re-exported modules by filtering the list of imported modules.
350
19.9k
  bool AnyWildcard = false;
351
19.9k
  bool UnrestrictedWildcard = false;
352
19.9k
  SmallVector<Module *, 4> WildcardRestrictions;
353
29.8k
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I9.89k
) {
354
9.89k
    Module *Mod = Exports[I].getPointer();
355
9.89k
    if (!Exports[I].getInt()) {
356
279
      // Export a named module directly; no wildcards involved.
357
279
      Exported.push_back(Mod);
358
279
359
279
      continue;
360
279
    }
361
9.61k
362
9.61k
    // Wildcard export: export all of the imported modules that match
363
9.61k
    // the given pattern.
364
9.61k
    AnyWildcard = true;
365
9.61k
    if (UnrestrictedWildcard)
366
573
      continue;
367
9.04k
368
9.04k
    if (Module *Restriction = Exports[I].getPointer())
369
3
      WildcardRestrictions.push_back(Restriction);
370
9.03k
    else {
371
9.03k
      WildcardRestrictions.clear();
372
9.03k
      UnrestrictedWildcard = true;
373
9.03k
    }
374
9.04k
  }
375
19.9k
376
19.9k
  // If there were any wildcards, push any imported modules that were
377
19.9k
  // re-exported by the wildcard restriction.
378
19.9k
  if (!AnyWildcard)
379
10.9k
    return;
380
9.04k
381
16.4k
  
for (unsigned I = 0, N = Imports.size(); 9.04k
I != N;
++I7.40k
) {
382
7.40k
    Module *Mod = Imports[I];
383
7.40k
    bool Acceptable = UnrestrictedWildcard;
384
7.40k
    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
7.40k
395
7.40k
    if (!Acceptable)
396
3
      continue;
397
7.39k
398
7.39k
    Exported.push_back(Mod);
399
7.39k
  }
400
9.04k
}
401
402
23
void Module::buildVisibleModulesCache() const {
403
23
  assert(VisibleModulesCache.empty() && "cache does not need building");
404
23
405
23
  // This module is visible to itself.
406
23
  VisibleModulesCache.insert(this);
407
23
408
23
  // Every imported module is visible.
409
23
  SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
410
54
  while (!Stack.empty()) {
411
31
    Module *CurrModule = Stack.pop_back_val();
412
31
413
31
    // Every module transitively exported by an imported module is visible.
414
31
    if (VisibleModulesCache.insert(CurrModule).second)
415
31
      CurrModule->getExportedModules(Stack);
416
31
  }
417
23
}
418
419
174
void Module::print(raw_ostream &OS, unsigned Indent) const {
420
174
  OS.indent(Indent);
421
174
  if (IsFramework)
422
149
    OS << "framework ";
423
174
  if (IsExplicit)
424
0
    OS << "explicit ";
425
174
  OS << "module ";
426
174
  printModuleId(OS, &Name, &Name + 1);
427
174
428
174
  if (IsSystem || 
IsExternC172
) {
429
7
    OS.indent(Indent + 2);
430
7
    if (IsSystem)
431
2
      OS << " [system]";
432
7
    if (IsExternC)
433
5
      OS << " [extern_c]";
434
7
  }
435
174
436
174
  OS << " {\n";
437
174
438
174
  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
174
451
174
  if (Header H = getUmbrellaHeader()) {
452
149
    OS.indent(Indent + 2);
453
149
    OS << "umbrella header \"";
454
149
    OS.write_escaped(H.NameAsWritten);
455
149
    OS << "\"\n";
456
149
  } 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
174
463
174
  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
174
476
174
  struct {
477
174
    StringRef Prefix;
478
174
    HeaderKind Kind;
479
174
  } Kinds[] = {{"", HK_Normal},
480
174
               {"textual ", HK_Textual},
481
174
               {"private ", HK_Private},
482
174
               {"private textual ", HK_PrivateTextual},
483
174
               {"exclude ", HK_Excluded}};
484
174
485
870
  for (auto &K : Kinds) {
486
870
    assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
487
870
    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
870
  }
495
348
  for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
496
348
    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
348
  }
512
174
513
174
  if (!ExportAsModule.empty()) {
514
0
    OS.indent(Indent + 2);
515
0
    OS << "export_as" << ExportAsModule << "\n";
516
0
  }
517
174
518
174
  for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
519
243
       MI != MIEnd; 
++MI69
)
520
69
    // Print inferred subframework modules so that we don't need to re-infer
521
69
    // them (requires expensive directory iteration + stat calls) when we build
522
69
    // the module. Regular inferred submodules are OK, as we need to look at all
523
69
    // those header files anyway.
524
69
    if (!(*MI)->IsInferred || 
(*MI)->IsFramework63
)
525
68
      (*MI)->print(OS, Indent + 2);
526
174
527
323
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I149
) {
528
149
    OS.indent(Indent + 2);
529
149
    OS << "export ";
530
149
    if (Module *Restriction = Exports[I].getPointer()) {
531
0
      OS << Restriction->getFullModuleName(true);
532
0
      if (Exports[I].getInt())
533
0
        OS << ".*";
534
149
    } else {
535
149
      OS << "*";
536
149
    }
537
149
    OS << "\n";
538
149
  }
539
174
540
187
  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
174
549
174
  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
174
556
175
  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
174
563
234
  for (unsigned I = 0, N = LinkLibraries.size(); I != N; 
++I60
) {
564
60
    OS.indent(Indent + 2);
565
60
    OS << "link ";
566
60
    if (LinkLibraries[I].IsFramework)
567
60
      OS << "framework ";
568
60
    OS << "\"";
569
60
    OS.write_escaped(LinkLibraries[I].Library);
570
60
    OS << "\"";
571
60
  }
572
174
573
174
  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
174
582
174
  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
174
591
174
  if (InferSubmodules) {
592
149
    OS.indent(Indent + 2);
593
149
    if (InferExplicitSubmodules)
594
0
      OS << "explicit ";
595
149
    OS << "module * {\n";
596
149
    if (InferExportWildcard) {
597
149
      OS.indent(Indent + 4);
598
149
      OS << "export *\n";
599
149
    }
600
149
    OS.indent(Indent + 2);
601
149
    OS << "}\n";
602
149
  }
603
174
604
174
  OS.indent(Indent);
605
174
  OS << "}\n";
606
174
}
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
13.7k
                                  VisibleCallback Vis, ConflictCallback Cb) {
614
13.7k
  assert(Loc.isValid() && "setVisible expects a valid import location");
615
13.7k
  if (isVisible(M))
616
3.88k
    return;
617
9.91k
618
9.91k
  ++Generation;
619
9.91k
620
9.91k
  struct Visiting {
621
9.91k
    Module *M;
622
9.91k
    Visiting *ExportedBy;
623
9.91k
  };
624
9.91k
625
16.5k
  std::function<void(Visiting)> VisitModule = [&](Visiting V) {
626
16.5k
    // Nothing to do for a module that's already visible.
627
16.5k
    unsigned ID = V.M->getVisibilityID();
628
16.5k
    if (ImportLocs.size() <= ID)
629
9.52k
      ImportLocs.resize(ID + 1);
630
6.98k
    else if (ImportLocs[ID].isValid())
631
2.87k
      return;
632
13.6k
633
13.6k
    ImportLocs[ID] = Loc;
634
13.6k
    Vis(M);
635
13.6k
636
13.6k
    // Make any exported modules visible.
637
13.6k
    SmallVector<Module *, 16> Exports;
638
13.6k
    V.M->getExportedModules(Exports);
639
13.6k
    for (Module *E : Exports) {
640
6.65k
      // Don't recurse to unavailable submodules.
641
6.65k
      if (E->isAvailable())
642
6.59k
        VisitModule({E, &V});
643
6.65k
    }
644
13.6k
645
13.6k
    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
13.6k
  };
654
9.91k
  VisitModule({M, nullptr});
655
9.91k
}