Coverage Report

Created: 2020-09-15 12:33

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