@@ -320,13 +320,13 @@ SourceLookupCache::SourceLookupCache(const ModuleDecl &M) {
320320 FrontendStatsTracer tracer (M.getASTContext ().Stats ,
321321 " module-populate-cache" );
322322 for (const FileUnit *file : M.getFiles ()) {
323- if (auto *SFU = dyn_cast<SynthesizedFileUnit>(file)) {
324- addToUnqualifiedLookupCache (SFU->getTopLevelDecls (), false );
325- continue ;
326- }
327323 auto *SF = cast<SourceFile>(file);
328324 addToUnqualifiedLookupCache (SF->getTopLevelDecls (), false );
329325 addToUnqualifiedLookupCache (SF->getHoistedDecls (), false );
326+
327+ if (auto *SFU = file->getSynthesizedFile ()) {
328+ addToUnqualifiedLookupCache (SFU->getTopLevelDecls (), false );
329+ }
330330 }
331331}
332332
@@ -548,9 +548,13 @@ SourceFile *CodeCompletionFileRequest::evaluate(Evaluator &evaluator,
548548 llvm_unreachable (" Couldn't find the completion file?" );
549549}
550550
551- #define FORWARD (name, args ) \
552- for (const FileUnit *file : getFiles()) \
553- file->name args;
551+ #define FORWARD (name, args ) \
552+ for (const FileUnit *file : getFiles()) { \
553+ file->name args; \
554+ if (auto *synth = file->getSynthesizedFile ()) { \
555+ synth->name args; \
556+ } \
557+ }
554558
555559SourceLookupCache &ModuleDecl::getSourceLookupCache () const {
556560 if (!Cache) {
@@ -3064,16 +3068,9 @@ SynthesizedFileUnit &FileUnit::getOrCreateSynthesizedFile() {
30643068 return *thisSynth;
30653069 SynthesizedFile = new (getASTContext ()) SynthesizedFileUnit (*this );
30663070 SynthesizedFileAndKind.setPointer (SynthesizedFile);
3067- // FIXME: Mutating the module in-flight is not a good idea. Any
3068- // callers above us in the stack that are iterating over
3069- // the module's files will have their iterators invalidated. There's
3070- // a strong chance that whatever analysis led to this function being
3071- // called is doing just that!
3072- //
3073- // Instead we ought to just call ModuleDecl::clearLookupCache() here
3074- // and patch out the places looking for synthesized files hanging off of
3075- // source files.
3076- getParentModule ()->addFile (*SynthesizedFile);
3071+ // Rebuild the source lookup caches now that we have a synthesized file
3072+ // full of declarations to look into.
3073+ getParentModule ()->clearLookupCache ();
30773074 }
30783075 return *SynthesizedFile;
30793076}
0 commit comments