23
23
#include " swift/Frontend/Frontend.h"
24
24
#include " swift/Parse/PersistentParserState.h"
25
25
#include " swift/Subsystems.h"
26
- #include " llvm/Support/MemoryBuffer.h"
27
26
#include " llvm/ADT/Hashing.h"
27
+ #include " llvm/Support/MemoryBuffer.h"
28
28
29
29
using namespace swift ;
30
30
using namespace ide ;
@@ -127,76 +127,10 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
127
127
return newDC;
128
128
}
129
129
130
- // / Calculate the hash value of the \c CompilerInvocation.
131
- // / This should take all options affecting completion results into account.
132
- // / If the hashes between multiple completion invocation are different, we
133
- // / cannot reuse the CompilerInstance.
134
- static llvm::hash_code calculateInvocationHash (const CompilerInvocation &Inv) {
135
- llvm::hash_code hash (0 );
136
-
137
- auto &frontendOpts = Inv.getFrontendOptions ();
138
- for (const InputFile &Input : frontendOpts.InputsAndOutputs .getAllInputs ())
139
- hash = llvm::hash_combine (hash, Input.file ());
140
- hash = llvm::hash_combine (
141
- hash,
142
- frontendOpts.InputKind ,
143
- llvm::makeArrayRef (frontendOpts.ImplicitImportModuleNames ),
144
- frontendOpts.ImplicitObjCHeaderPath ,
145
- frontendOpts.ModuleName ,
146
- frontendOpts.PrebuiltModuleCachePath ,
147
- llvm::makeArrayRef (frontendOpts.PreferInterfaceForModules ),
148
- frontendOpts.ParseStdlib ,
149
- frontendOpts.EnableSourceImport ,
150
- frontendOpts.ImportUnderlyingModule );
151
-
152
- auto &langOpts = Inv.getLangOptions ();
153
- hash = llvm::hash_combine (
154
- hash,
155
- langOpts.Target .str (),
156
- llvm::VersionTuple (langOpts.EffectiveLanguageVersion ).getAsString (),
157
- llvm::VersionTuple (langOpts.PackageDescriptionVersion ).getAsString (),
158
- langOpts.DisableAvailabilityChecking ,
159
- langOpts.EnableAccessControl ,
160
- langOpts.EnableAppExtensionRestrictions ,
161
- langOpts.RequireExplicitAvailability ,
162
- langOpts.RequireExplicitAvailabilityTarget ,
163
- langOpts.EnableObjCInterop ,
164
- langOpts.EnableCXXInterop ,
165
- langOpts.InferImportAsMember ,
166
- langOpts.EnableSwift3ObjCInference );
167
-
168
- auto &searchPathOpts = Inv.getSearchPathOptions ();
169
- hash = llvm::hash_combine (
170
- hash,
171
- searchPathOpts.SDKPath ,
172
- llvm::makeArrayRef (searchPathOpts.ImportSearchPaths ),
173
- llvm::makeArrayRef (searchPathOpts.VFSOverlayFiles ),
174
- searchPathOpts.RuntimeResourcePath ,
175
- llvm::makeArrayRef (searchPathOpts.RuntimeLibraryImportPaths ),
176
- llvm::makeArrayRef (searchPathOpts.SkipRuntimeLibraryImportPaths ));
177
- for (auto &P : searchPathOpts.FrameworkSearchPaths )
178
- hash = llvm::hash_combine (hash, P.IsSystem , P.Path );
179
-
180
- auto &clangOpts = Inv.getClangImporterOptions ();
181
- hash = llvm::hash_combine (
182
- hash,
183
- clangOpts.ModuleCachePath ,
184
- llvm::makeArrayRef (clangOpts.ExtraArgs ),
185
- clangOpts.OverrideResourceDir ,
186
- clangOpts.TargetCPU ,
187
- static_cast <uint8_t >(clangOpts.Mode ),
188
- clangOpts.ImportForwardDeclarations ,
189
- clangOpts.InferImportAsMember ,
190
- clangOpts.DisableSwiftBridgeAttr ,
191
- clangOpts.DisableOverlayModules );
192
-
193
- return hash;
194
- }
195
-
196
130
} // namespace
197
131
198
132
CompilerInstance *CompletionInstance::getCachedCompilerInstance (
199
- const swift::CompilerInvocation &Invocation,
133
+ const swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
200
134
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
201
135
DiagnosticConsumer *DiagC) {
202
136
if (!EnableASTCaching)
@@ -210,8 +144,7 @@ CompilerInstance *CompletionInstance::getCachedCompilerInstance(
210
144
if (!CachedCI)
211
145
return nullptr ;
212
146
213
- if (calculateInvocationHash (Invocation) !=
214
- calculateInvocationHash (CachedCI->getInvocation ()))
147
+ if (ArgsHash != CachedArgsHash)
215
148
return nullptr ;
216
149
217
150
auto &oldState = CachedCI->getPersistentParserState ();
@@ -307,12 +240,13 @@ CompilerInstance *CompletionInstance::getCachedCompilerInstance(
307
240
}
308
241
309
242
CompilerInstance *CompletionInstance::renewCompilerInstance (
310
- swift::CompilerInvocation &Invocation,
243
+ swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
311
244
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
312
245
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
313
246
std::string &Error, DiagnosticConsumer *DiagC) {
314
247
CachedCI.reset ();
315
248
CachedCI = std::make_unique<CompilerInstance>();
249
+ CachedArgsHash = ArgsHash;
316
250
auto *CI = CachedCI.get ();
317
251
if (DiagC)
318
252
CachedCI->addDiagnosticConsumer (DiagC);
@@ -332,7 +266,7 @@ CompilerInstance *CompletionInstance::renewCompilerInstance(
332
266
}
333
267
334
268
CompilerInstance *swift::ide::CompletionInstance::getCompilerInstance (
335
- swift::CompilerInvocation &Invocation,
269
+ swift::CompilerInvocation &Invocation, llvm::ArrayRef< const char *> Args,
336
270
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
337
271
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
338
272
std::string &Error, DiagnosticConsumer *DiagC) {
@@ -348,12 +282,18 @@ CompilerInstance *swift::ide::CompletionInstance::getCompilerInstance(
348
282
// FIXME: ASTScopeLookup doesn't support code completion yet.
349
283
Invocation.disableASTScopeLookup ();
350
284
351
- if (auto *cached = getCachedCompilerInstance (Invocation, completionBuffer,
352
- Offset, DiagC))
285
+ // Compute the signature of the invocation.
286
+ llvm::hash_code ArgsHash (0 );
287
+ for (auto arg : Args)
288
+ ArgsHash = llvm::hash_combine (ArgsHash, StringRef (arg));
289
+
290
+ if (auto *cached = getCachedCompilerInstance (Invocation, ArgsHash,
291
+ completionBuffer, Offset, DiagC))
353
292
return cached;
354
293
355
- if (auto *renewed = renewCompilerInstance (
356
- Invocation, FileSystem, completionBuffer, Offset, Error, DiagC))
294
+ if (auto *renewed =
295
+ renewCompilerInstance (Invocation, ArgsHash, FileSystem,
296
+ completionBuffer, Offset, Error, DiagC))
357
297
return renewed;
358
298
359
299
assert (!Error.empty ());
0 commit comments