Coverage Report

Created: 2022-01-25 06:29

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