Coverage Report

Created: 2021-06-15 06: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), 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.35M
      NameVisibility(Hidden) {
48
1.35M
  if (Parent) {
49
1.23M
    IsAvailable = Parent->isAvailable();
50
1.23M
    IsUnimportable = Parent->isUnimportable();
51
1.23M
    IsSystem = Parent->IsSystem;
52
1.23M
    IsExternC = Parent->IsExternC;
53
1.23M
    NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
54
1.23M
    ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
55
56
1.23M
    Parent->SubModuleIndex[Name] = Parent->SubModules.size();
57
1.23M
    Parent->SubModules.push_back(this);
58
1.23M
  }
59
1.35M
}
60
61
923k
Module::~Module() {
62
923k
  for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
63
1.74M
       I != IEnd; 
++I818k
) {
64
818k
    delete *I;
65
818k
  }
66
923k
}
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.8k
||
74
57.9k
      
Env == Feature57.8k
)
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
63
  
llvm_unreachable0
("could not find a reason why module is unimportable");
151
63
}
152
153
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
154
                         Requirement &Req,
155
                         UnresolvedHeaderDirective &MissingHeader,
156
25.5k
                         Module *&ShadowingModule) const {
157
25.5k
  if (IsAvailable)
158
25.5k
    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
2
  
llvm_unreachable0
("could not find a reason why module is unavailable");
173
2
}
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.37M
const Module *Module::getTopLevelModule() const {
184
6.37M
  const Module *Result = this;
185
16.8M
  while (Result->Parent)
186
10.4M
    Result = Result->Parent;
187
188
6.37M
  return Result;
189
6.37M
}
190
191
static StringRef getModuleNameFromComponent(
192
3
    const std::pair<std::string, SourceLocation> &IdComponent) {
193
3
  return IdComponent.first;
194
3
}
195
196
10.1k
static StringRef getModuleNameFromComponent(StringRef R) { return R; }
197
198
template<typename InputIter>
199
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
200
5.13k
                          bool AllowStringLiterals = true) {
201
15.2k
  for (InputIter It = Begin; It != End; 
++It10.1k
) {
202
10.1k
    if (It != Begin)
203
5.00k
      OS << ".";
204
205
10.1k
    StringRef Name = getModuleNameFromComponent(*It);
206
10.1k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)497
)
207
10.1k
      OS << Name;
208
10
    else {
209
10
      OS << '"';
210
10
      OS.write_escaped(Name);
211
10
      OS << '"';
212
10
    }
213
10.1k
  }
214
5.13k
}
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.88k
                          bool AllowStringLiterals = true) {
201
14.7k
  for (InputIter It = Begin; It != End; 
++It9.89k
) {
202
9.89k
    if (It != Begin)
203
5.00k
      OS << ".";
204
205
9.89k
    StringRef Name = getModuleNameFromComponent(*It);
206
9.89k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)261
)
207
9.88k
      OS << Name;
208
8
    else {
209
8
      OS << '"';
210
8
      OS.write_escaped(Name);
211
8
      OS << '"';
212
8
    }
213
9.89k
  }
214
4.88k
}
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
233
                          bool AllowStringLiterals = true) {
201
466
  for (InputIter It = Begin; It != End; 
++It233
) {
202
233
    if (It != Begin)
203
0
      OS << ".";
204
205
233
    StringRef Name = getModuleNameFromComponent(*It);
206
233
    if (!AllowStringLiterals || isValidIdentifier(Name))
207
231
      OS << Name;
208
2
    else {
209
2
      OS << '"';
210
2
      OS.write_escaped(Name);
211
2
      OS << '"';
212
2
    }
213
233
  }
214
233
}
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 || isValidIdentifier(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.88k
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
222
4.88k
  SmallVector<StringRef, 2> Names;
223
224
  // Build up the set of module names (from innermost to outermost).
225
14.7k
  for (const Module *M = this; M; 
M = M->Parent9.89k
)
226
9.89k
    Names.push_back(M->Name);
227
228
4.88k
  std::string Result;
229
230
4.88k
  llvm::raw_string_ostream Out(Result);
231
4.88k
  printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
232
4.88k
  Out.flush();
233
234
4.88k
  return Result;
235
4.88k
}
236
237
8.85k
bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
238
9.17k
  for (const Module *M = this; M; 
M = M->Parent322
) {
239
9.03k
    if (nameParts.empty() || M->Name != nameParts.back())
240
8.71k
      return false;
241
322
    nameParts = nameParts.drop_back();
242
322
  }
243
138
  return nameParts.empty();
244
8.85k
}
245
246
88.7k
Module::DirectoryName Module::getUmbrellaDir() const {
247
88.7k
  if (Header U = getUmbrellaHeader())
248
11.2k
    return {"", "", U.Entry->getDir()};
249
250
77.4k
  return {UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
251
77.4k
          Umbrella.dyn_cast<const DirectoryEntry *>()};
252
88.7k
}
253
254
22.3k
void Module::addTopHeader(const FileEntry *File) {
255
22.3k
  assert(File);
256
0
  TopHeaders.insert(File);
257
22.3k
}
258
259
20.1k
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
260
20.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
20.1k
  return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
270
20.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.0k
                            const TargetInfo &Target) {
293
61.0k
  Requirements.push_back(Requirement(std::string(Feature), RequiredState));
294
295
  // If this feature is currently available, we're done.
296
61.0k
  if (hasFeature(Feature, LangOpts, Target) == RequiredState)
297
22.6k
    return;
298
299
38.4k
  markUnavailable(/*Unimportable*/true);
300
38.4k
}
301
302
40.0k
void Module::markUnavailable(bool Unimportable) {
303
67.8k
  auto needUpdate = [Unimportable](Module *M) {
304
67.8k
    return M->IsAvailable || 
(15.9k
!M->IsUnimportable15.9k
&&
Unimportable1.43k
);
305
67.8k
  };
306
307
40.0k
  if (!needUpdate(this))
308
13.4k
    return;
309
310
26.5k
  SmallVector<Module *, 2> Stack;
311
26.5k
  Stack.push_back(this);
312
53.2k
  while (!Stack.empty()) {
313
26.6k
    Module *Current = Stack.back();
314
26.6k
    Stack.pop_back();
315
316
26.6k
    if (!needUpdate(Current))
317
0
      continue;
318
319
26.6k
    Current->IsAvailable = false;
320
26.6k
    Current->IsUnimportable |= Unimportable;
321
26.6k
    for (submodule_iterator Sub = Current->submodule_begin(),
322
26.6k
                         SubEnd = Current->submodule_end();
323
27.7k
         Sub != SubEnd; 
++Sub1.16k
) {
324
1.16k
      if (needUpdate(*Sub))
325
14
        Stack.push_back(*Sub);
326
1.16k
    }
327
26.6k
  }
328
26.5k
}
329
330
2.85M
Module *Module::findSubmodule(StringRef Name) const {
331
2.85M
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
332
2.85M
  if (Pos == SubModuleIndex.end())
333
2.14M
    return nullptr;
334
335
708k
  return SubModules[Pos->getValue()];
336
2.85M
}
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.03M
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
354
  // All non-explicit submodules are exported.
355
2.03M
  for (std::vector<Module *>::const_iterator I = SubModules.begin(),
356
2.03M
                                             E = SubModules.end();
357
3.65M
       I != E; 
++I1.62M
) {
358
1.62M
    Module *Mod = *I;
359
1.62M
    if (!Mod->IsExplicit)
360
1.59M
      Exported.push_back(Mod);
361
1.62M
  }
362
363
  // Find re-exported modules by filtering the list of imported modules.
364
2.03M
  bool AnyWildcard = false;
365
2.03M
  bool UnrestrictedWildcard = false;
366
2.03M
  SmallVector<Module *, 4> WildcardRestrictions;
367
4.11M
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I2.07M
) {
368
2.07M
    Module *Mod = Exports[I].getPointer();
369
2.07M
    if (!Exports[I].getInt()) {
370
      // Export a named module directly; no wildcards involved.
371
123k
      Exported.push_back(Mod);
372
373
123k
      continue;
374
123k
    }
375
376
    // Wildcard export: export all of the imported modules that match
377
    // the given pattern.
378
1.95M
    AnyWildcard = true;
379
1.95M
    if (UnrestrictedWildcard)
380
7.21k
      continue;
381
382
1.94M
    if (Module *Restriction = Exports[I].getPointer())
383
3
      WildcardRestrictions.push_back(Restriction);
384
1.94M
    else {
385
1.94M
      WildcardRestrictions.clear();
386
1.94M
      UnrestrictedWildcard = true;
387
1.94M
    }
388
1.94M
  }
389
390
  // If there were any wildcards, push any imported modules that were
391
  // re-exported by the wildcard restriction.
392
2.03M
  if (!AnyWildcard)
393
84.9k
    return;
394
395
8.41M
  
for (unsigned I = 0, N = Imports.size(); 1.94M
I != N;
++I6.47M
) {
396
6.47M
    Module *Mod = Imports[I];
397
6.47M
    bool Acceptable = UnrestrictedWildcard;
398
6.47M
    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
6.47M
    if (!Acceptable)
410
3
      continue;
411
412
6.47M
    Exported.push_back(Mod);
413
6.47M
  }
414
1.94M
}
415
416
218
void Module::buildVisibleModulesCache() const {
417
218
  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
218
  SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
424
93.7k
  while (!Stack.empty()) {
425
93.5k
    Module *CurrModule = Stack.pop_back_val();
426
427
    // Every module transitively exported by an imported module is visible.
428
93.5k
    if (VisibleModulesCache.insert(CurrModule).second)
429
31.4k
      CurrModule->getExportedModules(Stack);
430
93.5k
  }
431
218
}
432
433
233
void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) const {
434
233
  OS.indent(Indent);
435
233
  if (IsFramework)
436
208
    OS << "framework ";
437
233
  if (IsExplicit)
438
0
    OS << "explicit ";
439
233
  OS << "module ";
440
233
  printModuleId(OS, &Name, &Name + 1);
441
442
233
  if (IsSystem || 
IsExternC178
) {
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
233
  OS << " {\n";
451
452
233
  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
233
  if (Header H = getUmbrellaHeader()) {
466
208
    OS.indent(Indent + 2);
467
208
    OS << "umbrella header \"";
468
208
    OS.write_escaped(H.NameAsWritten);
469
208
    OS << "\"\n";
470
208
  } 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
233
  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
233
  struct {
491
233
    StringRef Prefix;
492
233
    HeaderKind Kind;
493
233
  } Kinds[] = {{"", HK_Normal},
494
233
               {"textual ", HK_Textual},
495
233
               {"private ", HK_Private},
496
233
               {"private textual ", HK_PrivateTextual},
497
233
               {"exclude ", HK_Excluded}};
498
499
1.16k
  for (auto &K : Kinds) {
500
1.16k
    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.16k
  }
509
466
  for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
510
466
    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
466
  }
526
527
233
  if (!ExportAsModule.empty()) {
528
0
    OS.indent(Indent + 2);
529
0
    OS << "export_as" << ExportAsModule << "\n";
530
0
  }
531
532
233
  for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
533
351
       MI != MIEnd; 
++MI118
)
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
118
    if (!(*MI)->IsInferred || 
(*MI)->IsFramework112
)
539
117
      (*MI)->print(OS, Indent + 2, Dump);
540
541
441
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I208
) {
542
208
    OS.indent(Indent + 2);
543
208
    OS << "export ";
544
208
    if (Module *Restriction = Exports[I].getPointer()) {
545
0
      OS << Restriction->getFullModuleName(true);
546
0
      if (Exports[I].getInt())
547
0
        OS << ".*";
548
208
    } else {
549
208
      OS << "*";
550
208
    }
551
208
    OS << "\n";
552
208
  }
553
554
246
  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
233
  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
233
  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
234
  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
301
  for (unsigned I = 0, N = LinkLibraries.size(); I != N; 
++I68
) {
585
68
    OS.indent(Indent + 2);
586
68
    OS << "link ";
587
68
    if (LinkLibraries[I].IsFramework)
588
68
      OS << "framework ";
589
68
    OS << "\"";
590
68
    OS.write_escaped(LinkLibraries[I].Library);
591
68
    OS << "\"";
592
68
  }
593
594
233
  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
233
  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
233
  if (InferSubmodules) {
613
208
    OS.indent(Indent + 2);
614
208
    if (InferExplicitSubmodules)
615
0
      OS << "explicit ";
616
208
    OS << "module * {\n";
617
208
    if (InferExportWildcard) {
618
208
      OS.indent(Indent + 4);
619
208
      OS << "export *\n";
620
208
    }
621
208
    OS.indent(Indent + 2);
622
208
    OS << "}\n";
623
208
  }
624
625
233
  OS.indent(Indent);
626
233
  OS << "}\n";
627
233
}
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
224k
                                  VisibleCallback Vis, ConflictCallback Cb) {
635
224k
  assert(Loc.isValid() && "setVisible expects a valid import location");
636
224k
  if (isVisible(M))
637
178k
    return;
638
639
45.1k
  ++Generation;
640
641
45.1k
  struct Visiting {
642
45.1k
    Module *M;
643
45.1k
    Visiting *ExportedBy;
644
45.1k
  };
645
646
3.66M
  std::function<void(Visiting)> VisitModule = [&](Visiting V) {
647
    // Nothing to do for a module that's already visible.
648
3.66M
    unsigned ID = V.M->getVisibilityID();
649
3.66M
    if (ImportLocs.size() <= ID)
650
281k
      ImportLocs.resize(ID + 1);
651
3.38M
    else if (ImportLocs[ID].isValid())
652
2.75M
      return;
653
654
914k
    ImportLocs[ID] = Loc;
655
914k
    Vis(M);
656
657
    // Make any exported modules visible.
658
914k
    SmallVector<Module *, 16> Exports;
659
914k
    V.M->getExportedModules(Exports);
660
3.62M
    for (Module *E : Exports) {
661
      // Don't import non-importable modules.
662
3.62M
      if (!E->isUnimportable())
663
3.62M
        VisitModule({E, &V});
664
3.62M
    }
665
666
914k
    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
914k
  };
675
45.1k
  VisitModule({M, nullptr});
676
45.1k
}
677
678
ASTSourceDescriptor::ASTSourceDescriptor(Module &M)
679
565k
    : Signature(M.Signature), ClangModule(&M) {
680
565k
  if (M.Directory)
681
409k
    Path = M.Directory->getName();
682
565k
  if (auto File = M.getASTFile())
683
34.0k
    ASTFile = File->getName();
684
565k
}
685
686
23.2k
std::string ASTSourceDescriptor::getModuleName() const {
687
23.2k
  if (ClangModule)
688
23.1k
    return ClangModule->Name;
689
111
  else
690
111
    return std::string(PCHModuleName);
691
23.2k
}