14
14
#include " swift/AST/ASTContext.h"
15
15
#include " swift/AST/Decl.h"
16
16
#include " swift/AST/DiagnosticsFrontend.h"
17
+ #include " swift/AST/DiagnosticsSema.h"
17
18
#include " swift/AST/ExistentialLayout.h"
18
19
#include " swift/AST/FileSystem.h"
19
20
#include " swift/AST/Module.h"
@@ -47,15 +48,15 @@ using FileDependency = SerializationOptions::FileDependency;
47
48
static swift::version::Version InterfaceFormatVersion ({1 , 0 });
48
49
49
50
static bool
50
- extractSwiftInterfaceVersionAndArgs (DiagnosticEngine &Diags,
51
+ extractSwiftInterfaceVersionAndArgs (DiagnosticEngine &Diags, SourceLoc DiagLoc,
51
52
clang::vfs::FileSystem &FS,
52
53
StringRef SwiftInterfacePathIn,
53
54
swift::version::Version &Vers,
54
55
llvm::StringSaver &SubArgSaver,
55
56
SmallVectorImpl<const char *> &SubArgs) {
56
57
auto FileOrError = swift::vfs::getFileOrSTDIN (FS, SwiftInterfacePathIn);
57
58
if (!FileOrError) {
58
- Diags.diagnose (SourceLoc () , diag::error_open_input_file,
59
+ Diags.diagnose (DiagLoc , diag::error_open_input_file,
59
60
SwiftInterfacePathIn, FileOrError.getError ().message ());
60
61
return true ;
61
62
}
@@ -64,12 +65,12 @@ extractSwiftInterfaceVersionAndArgs(DiagnosticEngine &Diags,
64
65
auto FlagRe = getSwiftInterfaceModuleFlagsRegex ();
65
66
SmallVector<StringRef, 1 > VersMatches, FlagMatches;
66
67
if (!VersRe.match (SB, &VersMatches)) {
67
- Diags.diagnose (SourceLoc () ,
68
+ Diags.diagnose (DiagLoc ,
68
69
diag::error_extracting_version_from_parseable_interface);
69
70
return true ;
70
71
}
71
72
if (!FlagRe.match (SB, &FlagMatches)) {
72
- Diags.diagnose (SourceLoc () ,
73
+ Diags.diagnose (DiagLoc ,
73
74
diag::error_extracting_flags_from_parseable_interface);
74
75
return true ;
75
76
}
@@ -83,11 +84,11 @@ extractSwiftInterfaceVersionAndArgs(DiagnosticEngine &Diags,
83
84
static std::unique_ptr<llvm::MemoryBuffer>
84
85
getBufferOfDependency (clang::vfs::FileSystem &FS,
85
86
StringRef ModulePath, StringRef DepPath,
86
- DiagnosticEngine &Diags) {
87
+ DiagnosticEngine &Diags, SourceLoc DiagLoc ) {
87
88
auto DepBuf = FS.getBufferForFile (DepPath, /* FileSize=*/ -1 ,
88
89
/* RequiresNullTerminator=*/ false );
89
90
if (!DepBuf) {
90
- Diags.diagnose (SourceLoc () ,
91
+ Diags.diagnose (DiagLoc ,
91
92
diag::missing_dependency_of_parseable_module_interface,
92
93
DepPath, ModulePath, DepBuf.getError ().message ());
93
94
return nullptr ;
@@ -130,6 +131,7 @@ static std::string getCacheHash(ASTContext &Ctx,
130
131
void
131
132
ParseableInterfaceModuleLoader::configureSubInvocationAndOutputPaths (
132
133
CompilerInvocation &SubInvocation,
134
+ Identifier ModuleName,
133
135
StringRef InPath,
134
136
llvm::SmallString<128 > &OutPath) {
135
137
@@ -145,37 +147,42 @@ ParseableInterfaceModuleLoader::configureSubInvocationAndOutputPaths(
145
147
SubInvocation.setRuntimeResourcePath (SearchPathOpts.RuntimeResourcePath );
146
148
SubInvocation.setTargetTriple (LangOpts.Target );
147
149
SubInvocation.setClangModuleCachePath (CacheDir);
150
+ SubInvocation.setModuleName (ModuleName.str ());
148
151
149
152
// Inhibit warnings from the SubInvocation since we are assuming the user
150
153
// is not in a position to fix them.
151
154
SubInvocation.getDiagnosticOptions ().SuppressWarnings = true ;
152
155
156
+ // Inherit this setting down so that it can affect error diagnostics (mostly
157
+ // by making them non-fatal).
158
+ SubInvocation.getLangOptions ().DebuggerSupport = LangOpts.DebuggerSupport ;
159
+
153
160
// Calculate an output filename that includes a hash of relevant key data, and
154
161
// wire up the SubInvocation's InputsAndOutputs to contain both input and
155
162
// output filenames.
156
163
OutPath = CacheDir;
157
- llvm::sys::path::append (OutPath, llvm::sys::path::stem (InPath ));
164
+ llvm::sys::path::append (OutPath, ModuleName. str ( ));
158
165
OutPath.append (" -" );
159
166
OutPath.append (getCacheHash (Ctx, SubInvocation, InPath));
160
167
OutPath.append (" ." );
161
168
auto OutExt = file_types::getExtension (file_types::TY_SwiftModuleFile);
162
169
OutPath.append (OutExt);
163
170
164
- auto &FEOpts = SubInvocation.getFrontendOptions ();
165
- FEOpts .RequestedAction = FrontendOptions::ActionType::EmitModuleOnly;
166
- FEOpts .EnableParseableModuleInterface = true ;
167
- FEOpts .InputsAndOutputs .addPrimaryInputFile (InPath);
171
+ auto &SubFEOpts = SubInvocation.getFrontendOptions ();
172
+ SubFEOpts .RequestedAction = FrontendOptions::ActionType::EmitModuleOnly;
173
+ SubFEOpts .EnableParseableModuleInterface = true ;
174
+ SubFEOpts .InputsAndOutputs .addPrimaryInputFile (InPath);
168
175
SupplementaryOutputPaths SOPs;
169
176
SOPs.ModuleOutputPath = OutPath.str ();
170
177
StringRef MainOut = " /dev/null" ;
171
- FEOpts .InputsAndOutputs .setMainAndSupplementaryOutputs ({MainOut}, {SOPs});
178
+ SubFEOpts .InputsAndOutputs .setMainAndSupplementaryOutputs ({MainOut}, {SOPs});
172
179
}
173
180
174
181
// Check that the output .swiftmodule file is at least as new as all the
175
182
// dependencies it read when it was built last time.
176
183
static bool
177
184
swiftModuleIsUpToDate (clang::vfs::FileSystem &FS,
178
- StringRef ModuleCachePath ,
185
+ std::pair<Identifier, SourceLoc> ModuleID ,
179
186
StringRef OutPath,
180
187
DiagnosticEngine &Diags,
181
188
DependencyTracker *OuterTracker) {
@@ -193,10 +200,14 @@ swiftModuleIsUpToDate(clang::vfs::FileSystem &FS,
193
200
if (VI.status != serialization::Status::Valid)
194
201
return false ;
195
202
203
+ assert (VI.name == ModuleID.first .str () &&
204
+ " we built a module at this path with a different name?" );
205
+
196
206
for (auto In : AllDeps) {
197
207
if (OuterTracker)
198
208
OuterTracker->addDependency (In.Path , /* IsSystem=*/ false );
199
- auto DepBuf = getBufferOfDependency (FS, OutPath, In.Path , Diags);
209
+ auto DepBuf = getBufferOfDependency (FS, OutPath, In.Path , Diags,
210
+ ModuleID.second );
200
211
if (!DepBuf ||
201
212
DepBuf->getBufferSize () != In.Size ||
202
213
xxHash64 (DepBuf->getBuffer ()) != In.Hash ) {
@@ -225,7 +236,7 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
225
236
CompilerInstance &SubInstance,
226
237
StringRef InPath, StringRef ModuleCachePath,
227
238
SmallVectorImpl<FileDependency> &Deps,
228
- DiagnosticEngine &Diags,
239
+ DiagnosticEngine &Diags, SourceLoc DiagLoc,
229
240
DependencyTracker *OuterTracker) {
230
241
auto DTDeps = SubInstance.getDependencyTracker ()->getDependencies ();
231
242
SmallVector<StringRef, 16 > InitialDepNames (DTDeps.begin (), DTDeps.end ());
@@ -235,7 +246,7 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
235
246
if (AllDepNames.insert (DepName).second && OuterTracker) {
236
247
OuterTracker->addDependency (DepName, /* IsSystem=*/ false );
237
248
}
238
- auto DepBuf = getBufferOfDependency (FS, InPath, DepName, Diags);
249
+ auto DepBuf = getBufferOfDependency (FS, InPath, DepName, Diags, DiagLoc );
239
250
if (!DepBuf) {
240
251
return true ;
241
252
}
@@ -256,7 +267,7 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
256
267
DepBuf->getBuffer (),
257
268
/* ExtendedValidationInfo=*/ nullptr , &SubDeps);
258
269
if (VI.status != serialization::Status::Valid) {
259
- Diags.diagnose (SourceLoc () ,
270
+ Diags.diagnose (DiagLoc ,
260
271
diag::error_extracting_dependencies_from_cached_module,
261
272
DepName);
262
273
return true ;
@@ -274,7 +285,7 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
274
285
}
275
286
276
287
static bool buildSwiftModuleFromSwiftInterface (
277
- clang::vfs::FileSystem &FS, DiagnosticEngine &Diags,
288
+ clang::vfs::FileSystem &FS, DiagnosticEngine &Diags, SourceLoc DiagLoc,
278
289
CompilerInvocation &SubInvocation, StringRef InPath, StringRef OutPath,
279
290
StringRef ModuleCachePath, DependencyTracker *OuterTracker) {
280
291
bool SubError = false ;
@@ -285,7 +296,7 @@ static bool buildSwiftModuleFromSwiftInterface(
285
296
llvm::StringSaver SubArgSaver (SubArgsAlloc);
286
297
SmallVector<const char *, 16 > SubArgs;
287
298
swift::version::Version Vers;
288
- if (extractSwiftInterfaceVersionAndArgs (Diags, FS, InPath, Vers,
299
+ if (extractSwiftInterfaceVersionAndArgs (Diags, DiagLoc, FS, InPath, Vers,
289
300
SubArgSaver, SubArgs)) {
290
301
SubError = true ;
291
302
return ;
@@ -295,18 +306,29 @@ static bool buildSwiftModuleFromSwiftInterface(
295
306
// minor versions might be interesting for debugging, or special-casing a
296
307
// compatible field variant.
297
308
if (Vers.asMajorVersion () != InterfaceFormatVersion.asMajorVersion ()) {
298
- Diags.diagnose (SourceLoc () ,
309
+ Diags.diagnose (DiagLoc ,
299
310
diag::unsupported_version_of_parseable_interface,
300
311
InPath, Vers);
301
312
SubError = true ;
302
313
return ;
303
314
}
304
315
316
+ SmallString<32 > ExpectedModuleName = SubInvocation.getModuleName ();
305
317
if (SubInvocation.parseArgs (SubArgs, Diags)) {
306
318
SubError = true ;
307
319
return ;
308
320
}
309
321
322
+ if (SubInvocation.getModuleName () != ExpectedModuleName) {
323
+ auto DiagKind = diag::serialization_name_mismatch;
324
+ if (SubInvocation.getLangOptions ().DebuggerSupport )
325
+ DiagKind = diag::serialization_name_mismatch_repl;
326
+ Diags.diagnose (DiagLoc, DiagKind, SubInvocation.getModuleName (),
327
+ ExpectedModuleName);
328
+ SubError = true ;
329
+ return ;
330
+ }
331
+
310
332
// Optimize emitted modules. This has to happen after we parse arguments,
311
333
// because parseSILOpts would override the current optimization mode.
312
334
SubInvocation.getSILOptions ().OptMode = OptimizationMode::ForSpeed;
@@ -353,7 +375,7 @@ static bool buildSwiftModuleFromSwiftInterface(
353
375
SerializationOpts.ModuleLinkName = FEOpts.ModuleLinkName ;
354
376
SmallVector<FileDependency, 16 > Deps;
355
377
if (collectDepsForSerialization (FS, SubInstance, InPath, ModuleCachePath,
356
- Deps, Diags, OuterTracker)) {
378
+ Deps, Diags, DiagLoc, OuterTracker)) {
357
379
SubError = true ;
358
380
return ;
359
381
}
@@ -389,7 +411,8 @@ static bool serializedASTLooksValidOrCannotBeRead(clang::vfs::FileSystem &FS,
389
411
// / cache or by converting it in a subordinate \c CompilerInstance, caching
390
412
// / the results.
391
413
std::error_code ParseableInterfaceModuleLoader::openModuleFiles (
392
- StringRef DirName, StringRef ModuleFilename, StringRef ModuleDocFilename,
414
+ AccessPathElem ModuleID, StringRef DirName, StringRef ModuleFilename,
415
+ StringRef ModuleDocFilename,
393
416
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
394
417
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
395
418
llvm::SmallVectorImpl<char > &Scratch) {
@@ -430,12 +453,14 @@ std::error_code ParseableInterfaceModuleLoader::openModuleFiles(
430
453
// Set up a _potential_ sub-invocation to consume the .swiftinterface and emit
431
454
// the .swiftmodule.
432
455
CompilerInvocation SubInvocation;
433
- configureSubInvocationAndOutputPaths (SubInvocation, InPath, OutPath);
456
+ configureSubInvocationAndOutputPaths (SubInvocation, ModuleID.first , InPath,
457
+ OutPath);
434
458
435
459
// Evaluate if we need to run this sub-invocation, and if so run it.
436
- if (!swiftModuleIsUpToDate (FS, CacheDir, OutPath, Diags, dependencyTracker)) {
437
- if (buildSwiftModuleFromSwiftInterface (FS, Diags, SubInvocation, InPath,
438
- OutPath, CacheDir, dependencyTracker))
460
+ if (!swiftModuleIsUpToDate (FS, ModuleID, OutPath, Diags, dependencyTracker)) {
461
+ if (buildSwiftModuleFromSwiftInterface (FS, Diags, ModuleID.second ,
462
+ SubInvocation, InPath, OutPath,
463
+ CacheDir, dependencyTracker))
439
464
return std::make_error_code (std::errc::invalid_argument);
440
465
}
441
466
@@ -444,8 +469,8 @@ std::error_code ParseableInterfaceModuleLoader::openModuleFiles(
444
469
LLVM_DEBUG (llvm::dbgs () << " Loading " << OutPath
445
470
<< " via normal module loader\n " );
446
471
auto ErrorCode = SerializedModuleLoaderBase::openModuleFiles (
447
- CacheDir, llvm::sys::path::filename (OutPath), ModuleDocFilename ,
448
- ModuleBuffer, ModuleDocBuffer, Scratch);
472
+ ModuleID, CacheDir, llvm::sys::path::filename (OutPath),
473
+ ModuleDocFilename, ModuleBuffer, ModuleDocBuffer, Scratch);
449
474
LLVM_DEBUG (llvm::dbgs () << " Loaded " << OutPath
450
475
<< " via normal module loader" );
451
476
if (ErrorCode) {
0 commit comments