Coverage Report

Created: 2020-11-24 06:42

/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.76M
      NameVisibility(Hidden) {
48
1.76M
  if (Parent) {
49
1.62M
    IsAvailable = Parent->isAvailable();
50
1.62M
    IsUnimportable = Parent->isUnimportable();
51
1.62M
    IsSystem = Parent->IsSystem;
52
1.62M
    IsExternC = Parent->IsExternC;
53
1.62M
    NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
54
1.62M
    ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
55
56
1.62M
    Parent->SubModuleIndex[Name] = Parent->SubModules.size();
57
1.62M
    Parent->SubModules.push_back(this);
58
1.62M
  }
59
1.76M
}
60
61
865k
Module::~Module() {
62
865k
  for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
63
1.62M
       I != IEnd; 
++I760k
) {
64
760k
    delete *I;
65
760k
  }
66
865k
}
67
68
76.4k
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
69
76.4k
  StringRef Platform = Target.getPlatformName();
70
76.4k
  StringRef Env = Target.getTriple().getEnvironmentName();
71
72
  // Attempt to match platform and environment.
73
76.4k
  if (Platform == Feature || 
Target.getTriple().getOSName() == Feature76.4k
||
74
76.4k
      Env == Feature)
75
58
    return true;
76
77
76.4k
  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
76.4k
  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
76.4k
  if (Target.getTriple().isOSDarwin() && 
PlatformEnv.endswith("simulator")75.5k
)
94
3
    return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
95
96
76.4k
  return PlatformEnv == Feature;
97
76.4k
}
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
80.3k
                       const TargetInfo &Target) {
103
80.3k
  bool HasFeature = llvm::StringSwitch<bool>(Feature)
104
80.3k
                        .Case("altivec", LangOpts.AltiVec)
105
80.3k
                        .Case("blocks", LangOpts.Blocks)
106
80.3k
                        .Case("coroutines", LangOpts.Coroutines)
107
80.3k
                        .Case("cplusplus", LangOpts.CPlusPlus)
108
80.3k
                        .Case("cplusplus11", LangOpts.CPlusPlus11)
109
80.3k
                        .Case("cplusplus14", LangOpts.CPlusPlus14)
110
80.3k
                        .Case("cplusplus17", LangOpts.CPlusPlus17)
111
80.3k
                        .Case("c99", LangOpts.C99)
112
80.3k
                        .Case("c11", LangOpts.C11)
113
80.3k
                        .Case("c17", LangOpts.C17)
114
80.3k
                        .Case("freestanding", LangOpts.Freestanding)
115
80.3k
                        .Case("gnuinlineasm", LangOpts.GNUAsm)
116
80.3k
                        .Case("objc", LangOpts.ObjC)
117
80.3k
                        .Case("objc_arc", LangOpts.ObjCAutoRefCount)
118
80.3k
                        .Case("opencl", LangOpts.OpenCL)
119
80.3k
                        .Case("tls", Target.isTLSSupported())
120
80.3k
                        .Case("zvector", LangOpts.ZVector)
121
80.3k
                        .Default(Target.hasFeature(Feature) ||
122
76.4k
                                 isPlatformEnvironment(Target, Feature));
123
80.3k
  if (!HasFeature)
124
54.6k
    HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
125
54.6k
                           LangOpts.ModuleFeatures.end(),
126
54.6k
                           Feature) != LangOpts.ModuleFeatures.end();
127
80.3k
  return HasFeature;
128
80.3k
}
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
29.5k
                         Module *&ShadowingModule) const {
157
29.5k
  if (IsAvailable)
158
29.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.74M
const Module *Module::getTopLevelModule() const {
184
6.74M
  const Module *Result = this;
185
17.6M
  while (Result->Parent)
186
10.9M
    Result = Result->Parent;
187
188
6.74M
  return Result;
189
6.74M
}
190
191
static StringRef getModuleNameFromComponent(
192
3
    const std::pair<std::string, SourceLocation> &IdComponent) {
193
3
  return IdComponent.first;
194
3
}
195
196
9.39k
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.75k
                          bool AllowStringLiterals = true) {
201
14.1k
  for (InputIter It = Begin; It != End; 
++It9.39k
) {
202
9.39k
    if (It != Begin)
203
4.65k
      OS << ".";
204
205
9.39k
    StringRef Name = getModuleNameFromComponent(*It);
206
9.39k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)505
)
207
9.38k
      OS << Name;
208
10
    else {
209
10
      OS << '"';
210
10
      OS.write_escaped(Name);
211
10
      OS << '"';
212
10
    }
213
9.39k
  }
214
4.75k
}
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.49k
                          bool AllowStringLiterals = true) {
201
13.6k
  for (InputIter It = Begin; It != End; 
++It9.15k
) {
202
9.15k
    if (It != Begin)
203
4.65k
      OS << ".";
204
205
9.15k
    StringRef Name = getModuleNameFromComponent(*It);
206
9.15k
    if (!AllowStringLiterals || 
isValidIdentifier(Name)261
)
207
9.14k
      OS << Name;
208
8
    else {
209
8
      OS << '"';
210
8
      OS.write_escaped(Name);
211
8
      OS << '"';
212
8
    }
213
9.15k
  }
214
4.49k
}
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
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
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.49k
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
222
4.49k
  SmallVector<StringRef, 2> Names;
223
224
  // Build up the set of module names (from innermost to outermost).
225
13.6k
  for (const Module *M = this; M; 
M = M->Parent9.15k
)
226
9.15k
    Names.push_back(M->Name);
227
228
4.49k
  std::string Result;
229
230
4.49k
  llvm::raw_string_ostream Out(Result);
231
4.49k
  printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
232
4.49k
  Out.flush();
233
234
4.49k
  return Result;
235
4.49k
}
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
108k
Module::DirectoryName Module::getUmbrellaDir() const {
247
108k
  if (Header U = getUmbrellaHeader())
248
16.4k
    return {"", U.Entry->getDir()};
249
250
92.2k
  return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
251
92.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
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
274
  // A top-level module implicitly uses itself.
275
44.1k
  if (Requested->isSubModuleOf(Top))
276
43.8k
    return true;
277
278
350
  for (auto *Use : Top->DirectUses)
279
86
    if (Requested->isSubModuleOf(Use))
280
26
      return true;
281
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
286
176
  return false;
287
176
}
288
289
void Module::addRequirement(StringRef Feature, bool RequiredState,
290
                            const LangOptions &LangOpts,
291
80.2k
                            const TargetInfo &Target) {
292
80.2k
  Requirements.push_back(Requirement(std::string(Feature), RequiredState));
293
294
  // If this feature is currently available, we're done.
295
80.2k
  if (hasFeature(Feature, LangOpts, Target) == RequiredState)
296
31.2k
    return;
297
298
49.0k
  markUnavailable(/*Unimportable*/true);
299
49.0k
}
300
301
50.7k
void Module::markUnavailable(bool Unimportable) {
302
86.5k
  auto needUpdate = [Unimportable](Module *M) {
303
86.5k
    return M->IsAvailable || 
(20.2k
!M->IsUnimportable20.2k
&&
Unimportable1.43k
);
304
86.5k
  };
305
306
50.7k
  if (!needUpdate(this))
307
16.9k
    return;
308
309
33.7k
  SmallVector<Module *, 2> Stack;
310
33.7k
  Stack.push_back(this);
311
67.5k
  while (!Stack.empty()) {
312
33.8k
    Module *Current = Stack.back();
313
33.8k
    Stack.pop_back();
314
315
33.8k
    if (!needUpdate(Current))
316
0
      continue;
317
318
33.8k
    Current->IsAvailable = false;
319
33.8k
    Current->IsUnimportable |= Unimportable;
320
33.8k
    for (submodule_iterator Sub = Current->submodule_begin(),
321
33.8k
                         SubEnd = Current->submodule_end();
322
35.8k
         Sub != SubEnd; 
++Sub2.07k
) {
323
2.07k
      if (needUpdate(*Sub))
324
14
        Stack.push_back(*Sub);
325
2.07k
    }
326
33.8k
  }
327
33.7k
}
328
329
3.66M
Module *Module::findSubmodule(StringRef Name) const {
330
3.66M
  llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
331
3.66M
  if (Pos == SubModuleIndex.end())
332
2.77M
    return nullptr;
333
334
888k
  return SubModules[Pos->getValue()];
335
888k
}
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.19M
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
353
  // All non-explicit submodules are exported.
354
2.19M
  for (std::vector<Module *>::const_iterator I = SubModules.begin(),
355
2.19M
                                             E = SubModules.end();
356
3.96M
       I != E; 
++I1.76M
) {
357
1.76M
    Module *Mod = *I;
358
1.76M
    if (!Mod->IsExplicit)
359
1.72M
      Exported.push_back(Mod);
360
1.76M
  }
361
362
  // Find re-exported modules by filtering the list of imported modules.
363
2.19M
  bool AnyWildcard = false;
364
2.19M
  bool UnrestrictedWildcard = false;
365
2.19M
  SmallVector<Module *, 4> WildcardRestrictions;
366
4.42M
  for (unsigned I = 0, N = Exports.size(); I != N; 
++I2.22M
) {
367
2.22M
    Module *Mod = Exports[I].getPointer();
368
2.22M
    if (!Exports[I].getInt()) {
369
      // Export a named module directly; no wildcards involved.
370
106k
      Exported.push_back(Mod);
371
372
106k
      continue;
373
106k
    }
374
375
    // Wildcard export: export all of the imported modules that match
376
    // the given pattern.
377
2.11M
    AnyWildcard = true;
378
2.11M
    if (UnrestrictedWildcard)
379
9.25k
      continue;
380
381
2.11M
    if (Module *Restriction = Exports[I].getPointer())
382
3
      WildcardRestrictions.push_back(Restriction);
383
2.11M
    else {
384
2.11M
      WildcardRestrictions.clear();
385
2.11M
      UnrestrictedWildcard = true;
386
2.11M
    }
387
2.11M
  }
388
389
  // If there were any wildcards, push any imported modules that were
390
  // re-exported by the wildcard restriction.
391
2.19M
  if (!AnyWildcard)
392
84.2k
    return;
393
394
8.91M
  
for (unsigned I = 0, N = Imports.size(); 2.11M
I != N;
++I6.80M
) {
395
6.80M
    Module *Mod = Imports[I];
396
6.80M
    bool Acceptable = UnrestrictedWildcard;
397
6.80M
    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
408
6.80M
    if (!Acceptable)
409
3
      continue;
410
411
6.80M
    Exported.push_back(Mod);
412
6.80M
  }
413
2.11M
}
414
415
218
void Module::buildVisibleModulesCache() const {
416
218
  assert(VisibleModulesCache.empty() && "cache does not need building");
417
418
  // This module is visible to itself.
419
218
  VisibleModulesCache.insert(this);
420
421
  // Every imported module is visible.
422
218
  SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
423
89.7k
  while (!Stack.empty()) {
424
89.4k
    Module *CurrModule = Stack.pop_back_val();
425
426
    // Every module transitively exported by an imported module is visible.
427
89.4k
    if (VisibleModulesCache.insert(CurrModule).second)
428
30.9k
      CurrModule->getExportedModules(Stack);
429
89.4k
  }
430
218
}
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
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
449
241
  OS << " {\n";
450
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
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
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
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
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
526
241
  if (!ExportAsModule.empty()) {
527
0
    OS.indent(Indent + 2);
528
0
    OS << "export_as" << ExportAsModule << "\n";
529
0
  }
530
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
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
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
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
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
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
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
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
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
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
230k
                                  VisibleCallback Vis, ConflictCallback Cb) {
627
230k
  assert(Loc.isValid() && "setVisible expects a valid import location");
628
230k
  if (isVisible(M))
629
181k
    return;
630
631
48.4k
  ++Generation;
632
633
48.4k
  struct Visiting {
634
48.4k
    Module *M;
635
48.4k
    Visiting *ExportedBy;
636
48.4k
  };
637
638
4.10M
  std::function<void(Visiting)> VisitModule = [&](Visiting V) {
639
    // Nothing to do for a module that's already visible.
640
4.10M
    unsigned ID = V.M->getVisibilityID();
641
4.10M
    if (ImportLocs.size() <= ID)
642
334k
      ImportLocs.resize(ID + 1);
643
3.77M
    else if (ImportLocs[ID].isValid())
644
3.04M
      return;
645
646
1.06M
    ImportLocs[ID] = Loc;
647
1.06M
    Vis(M);
648
649
    // Make any exported modules visible.
650
1.06M
    SmallVector<Module *, 16> Exports;
651
1.06M
    V.M->getExportedModules(Exports);
652
4.06M
    for (Module *E : Exports) {
653
      // Don't import non-importable modules.
654
4.06M
      if (!E->isUnimportable())
655
4.05M
        VisitModule({E, &V});
656
4.06M
    }
657
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
1.06M
  };
667
48.4k
  VisitModule({M, nullptr});
668
48.4k
}
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
}