@@ -54,18 +54,18 @@ static std::vector<std::string> splitString(std::string S, char Separator) {
54
54
return Result;
55
55
}
56
56
57
- void ModuleDepCollector::addOutputPaths (CompilerInvocation &CI,
57
+ void ModuleDepCollector::addOutputPaths (CowCompilerInvocation &CI,
58
58
ModuleDeps &Deps) {
59
- CI.getFrontendOpts ().OutputFile =
59
+ CI.getMutFrontendOpts ().OutputFile =
60
60
Controller.lookupModuleOutput (Deps.ID , ModuleOutputKind::ModuleFile);
61
61
if (!CI.getDiagnosticOpts ().DiagnosticSerializationFile .empty ())
62
- CI.getDiagnosticOpts ().DiagnosticSerializationFile =
62
+ CI.getMutDiagnosticOpts ().DiagnosticSerializationFile =
63
63
Controller.lookupModuleOutput (
64
64
Deps.ID , ModuleOutputKind::DiagnosticSerializationFile);
65
65
if (!CI.getDependencyOutputOpts ().OutputFile .empty ()) {
66
- CI.getDependencyOutputOpts ().OutputFile = Controller.lookupModuleOutput (
66
+ CI.getMutDependencyOutputOpts ().OutputFile = Controller.lookupModuleOutput (
67
67
Deps.ID , ModuleOutputKind::DependencyFile);
68
- CI.getDependencyOutputOpts ().Targets =
68
+ CI.getMutDependencyOutputOpts ().Targets =
69
69
splitString (Controller.lookupModuleOutput (
70
70
Deps.ID , ModuleOutputKind::DependencyTargets),
71
71
' \0 ' );
@@ -74,18 +74,13 @@ void ModuleDepCollector::addOutputPaths(CompilerInvocation &CI,
74
74
// Fallback to -o as dependency target, as in the driver.
75
75
SmallString<128 > Target;
76
76
quoteMakeTarget (CI.getFrontendOpts ().OutputFile , Target);
77
- CI.getDependencyOutputOpts ().Targets .push_back (std::string (Target));
77
+ CI.getMutDependencyOutputOpts ().Targets .push_back (std::string (Target));
78
78
}
79
79
}
80
80
}
81
81
82
- CompilerInvocation
83
- ModuleDepCollector::makeInvocationForModuleBuildWithoutOutputs (
84
- const ModuleDeps &Deps,
85
- llvm::function_ref<void (CompilerInvocation &)> Optimize) const {
86
- // Make a deep copy of the original Clang invocation.
87
- CompilerInvocation CI (OriginalInvocation);
88
-
82
+ static CowCompilerInvocation
83
+ makeCommonInvocationForModuleBuild (CompilerInvocation CI) {
89
84
CI.resetNonModularOptions ();
90
85
CI.clearImplicitModuleBuildOptions ();
91
86
@@ -117,14 +112,38 @@ ModuleDepCollector::makeInvocationForModuleBuildWithoutOutputs(
117
112
CI.getFrontendOpts ().ARCMTAction = FrontendOptions::ARCMT_None;
118
113
CI.getFrontendOpts ().ObjCMTAction = FrontendOptions::ObjCMT_None;
119
114
CI.getFrontendOpts ().MTMigrateDir .clear ();
120
- CI.getLangOpts ().ModuleName = Deps.ID .ModuleName ;
121
- CI.getFrontendOpts ().IsSystemModule = Deps.IsSystem ;
115
+ CI.getLangOpts ().ModuleName .clear ();
116
+
117
+ // Remove any macro definitions that are explicitly ignored.
118
+ if (!CI.getHeaderSearchOpts ().ModulesIgnoreMacros .empty ()) {
119
+ llvm::erase_if (
120
+ CI.getPreprocessorOpts ().Macros ,
121
+ [&CI](const std::pair<std::string, bool > &Def) {
122
+ StringRef MacroDef = Def.first ;
123
+ return CI.getHeaderSearchOpts ().ModulesIgnoreMacros .contains (
124
+ llvm::CachedHashString (MacroDef.split (' =' ).first ));
125
+ });
126
+ // Remove the now unused option.
127
+ CI.getHeaderSearchOpts ().ModulesIgnoreMacros .clear ();
128
+ }
129
+
130
+ return CI;
131
+ }
132
+
133
+ CowCompilerInvocation
134
+ ModuleDepCollector::getInvocationAdjustedForModuleBuildWithoutOutputs (
135
+ const ModuleDeps &Deps,
136
+ llvm::function_ref<void (CowCompilerInvocation &)> Optimize) const {
137
+ CowCompilerInvocation CI = CommonInvocation;
138
+
139
+ CI.getMutLangOpts ().ModuleName = Deps.ID .ModuleName ;
140
+ CI.getMutFrontendOpts ().IsSystemModule = Deps.IsSystem ;
122
141
123
142
// Inputs
124
143
InputKind ModuleMapInputKind (CI.getFrontendOpts ().DashX .getLanguage (),
125
144
InputKind::Format::ModuleMap);
126
- CI.getFrontendOpts ().Inputs .emplace_back (Deps.ClangModuleMapFile ,
127
- ModuleMapInputKind);
145
+ CI.getMutFrontendOpts ().Inputs .emplace_back (Deps.ClangModuleMapFile ,
146
+ ModuleMapInputKind);
128
147
129
148
auto CurrentModuleMapEntry =
130
149
ScanInstance.getFileManager ().getFile (Deps.ClangModuleMapFile );
@@ -150,36 +169,25 @@ ModuleDepCollector::makeInvocationForModuleBuildWithoutOutputs(
150
169
!DepModuleMapFiles.contains (*ModuleMapEntry))
151
170
continue ;
152
171
153
- CI.getFrontendOpts ().ModuleMapFiles .emplace_back (ModuleMapFile);
172
+ CI.getMutFrontendOpts ().ModuleMapFiles .emplace_back (ModuleMapFile);
154
173
}
155
174
156
175
// Report the prebuilt modules this module uses.
157
176
for (const auto &PrebuiltModule : Deps.PrebuiltModuleDeps )
158
- CI.getFrontendOpts ().ModuleFiles .push_back (PrebuiltModule.PCMFile );
177
+ CI.getMutFrontendOpts ().ModuleFiles .push_back (PrebuiltModule.PCMFile );
159
178
160
179
// Add module file inputs from dependencies.
161
180
addModuleFiles (CI, Deps.ClangModuleDeps );
162
181
163
- // Remove any macro definitions that are explicitly ignored.
164
- if (!CI.getHeaderSearchOpts ().ModulesIgnoreMacros .empty ()) {
165
- llvm::erase_if (
166
- CI.getPreprocessorOpts ().Macros ,
167
- [&CI](const std::pair<std::string, bool > &Def) {
168
- StringRef MacroDef = Def.first ;
169
- return CI.getHeaderSearchOpts ().ModulesIgnoreMacros .contains (
170
- llvm::CachedHashString (MacroDef.split (' =' ).first ));
171
- });
172
- // Remove the now unused option.
173
- CI.getHeaderSearchOpts ().ModulesIgnoreMacros .clear ();
182
+ if (!CI.getDiagnosticOpts ().SystemHeaderWarningsModules .empty ()) {
183
+ // Apply -Wsystem-headers-in-module for the current module.
184
+ if (llvm::is_contained (CI.getDiagnosticOpts ().SystemHeaderWarningsModules ,
185
+ Deps.ID .ModuleName ))
186
+ CI.getMutDiagnosticOpts ().Warnings .push_back (" system-headers" );
187
+ // Remove the now unused option(s).
188
+ CI.getMutDiagnosticOpts ().SystemHeaderWarningsModules .clear ();
174
189
}
175
190
176
- // Apply -Wsystem-headers-in-module for the current module.
177
- if (llvm::is_contained (CI.getDiagnosticOpts ().SystemHeaderWarningsModules ,
178
- Deps.ID .ModuleName ))
179
- CI.getDiagnosticOpts ().Warnings .push_back (" system-headers" );
180
- // Remove the now unused option(s).
181
- CI.getDiagnosticOpts ().SystemHeaderWarningsModules .clear ();
182
-
183
191
Optimize (CI);
184
192
185
193
return CI;
@@ -224,6 +232,19 @@ void ModuleDepCollector::addModuleFiles(
224
232
}
225
233
}
226
234
235
+ void ModuleDepCollector::addModuleFiles (
236
+ CowCompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
237
+ for (const ModuleID &MID : ClangModuleDeps) {
238
+ std::string PCMPath =
239
+ Controller.lookupModuleOutput (MID, ModuleOutputKind::ModuleFile);
240
+ if (EagerLoadModules)
241
+ CI.getMutFrontendOpts ().ModuleFiles .push_back (std::move (PCMPath));
242
+ else
243
+ CI.getMutHeaderSearchOpts ().PrebuiltModuleFiles .insert (
244
+ {MID.ModuleName , std::move (PCMPath)});
245
+ }
246
+ }
247
+
227
248
static bool needsModules (FrontendInputFile FIF) {
228
249
switch (FIF.getKind ().getLanguage ()) {
229
250
case Language::Unknown:
@@ -264,7 +285,7 @@ void ModuleDepCollector::applyDiscoveredDependencies(CompilerInvocation &CI) {
264
285
}
265
286
266
287
static std::string getModuleContextHash (const ModuleDeps &MD,
267
- const CompilerInvocation &CI,
288
+ const CowCompilerInvocation &CI,
268
289
bool EagerLoadModules) {
269
290
llvm::HashBuilder<llvm::TruncatedBLAKE3<16 >,
270
291
llvm::support::endianness::native>
@@ -304,8 +325,8 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
304
325
return toString (llvm::APInt (sizeof (Words) * 8 , Words), 36 , /* Signed=*/ false );
305
326
}
306
327
307
- void ModuleDepCollector::associateWithContextHash (const CompilerInvocation &CI,
308
- ModuleDeps &Deps) {
328
+ void ModuleDepCollector::associateWithContextHash (
329
+ const CowCompilerInvocation &CI, ModuleDeps &Deps) {
309
330
Deps.ID .ContextHash = getModuleContextHash (Deps, CI, EagerLoadModules);
310
331
bool Inserted = ModuleDepsByID.insert ({Deps.ID , &Deps}).second ;
311
332
(void )Inserted;
@@ -498,12 +519,13 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
498
519
MD.ModuleMapFileDeps .emplace_back (IFI.FilenameAsRequested );
499
520
});
500
521
501
- CompilerInvocation CI = MDC.makeInvocationForModuleBuildWithoutOutputs (
502
- MD, [&](CompilerInvocation &BuildInvocation) {
503
- if (MDC.OptimizeArgs )
504
- optimizeHeaderSearchOpts (BuildInvocation.getHeaderSearchOpts (),
505
- *MDC.ScanInstance .getASTReader (), *MF);
506
- });
522
+ CowCompilerInvocation CI =
523
+ MDC.getInvocationAdjustedForModuleBuildWithoutOutputs (
524
+ MD, [&](CowCompilerInvocation &BuildInvocation) {
525
+ if (MDC.OptimizeArgs )
526
+ optimizeHeaderSearchOpts (BuildInvocation.getMutHeaderSearchOpts (),
527
+ *MDC.ScanInstance .getASTReader (), *MF);
528
+ });
507
529
508
530
MDC.associateWithContextHash (CI, MD);
509
531
@@ -601,7 +623,9 @@ ModuleDepCollector::ModuleDepCollector(
601
623
DependencyActionController &Controller, CompilerInvocation OriginalCI,
602
624
bool OptimizeArgs, bool EagerLoadModules, bool IsStdModuleP1689Format)
603
625
: ScanInstance(ScanInstance), Consumer(C), Controller(Controller),
604
- Opts(std::move(Opts)), OriginalInvocation(std::move(OriginalCI)),
626
+ Opts(std::move(Opts)),
627
+ CommonInvocation(
628
+ makeCommonInvocationForModuleBuild (std::move(OriginalCI))),
605
629
OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules),
606
630
IsStdModuleP1689Format(IsStdModuleP1689Format) {}
607
631
0 commit comments