Skip to content

Commit a83c7e8

Browse files
committed
[SourceKit] Calculate CompilerInvocation hash
1 parent c1530ee commit a83c7e8

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

lib/IDE/CompletionInstance.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Parse/PersistentParserState.h"
2424
#include "swift/Subsystems.h"
2525
#include "llvm/Support/MemoryBuffer.h"
26+
#include "llvm/ADT/Hashing.h"
2627

2728
using namespace swift;
2829
using namespace ide;
@@ -124,6 +125,73 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
124125

125126
return newDC;
126127
}
128+
129+
/// Calculate the hash value of the \c CompilerInvocation.
130+
/// This should take all options affecting completion results into account.
131+
/// If the hashes between multiple completion invocation are different, we
132+
/// cannot reuse the CompilerInstance.
133+
static llvm::hash_code calculateInvocationHash(const CompilerInvocation &Inv) {
134+
llvm::hash_code hash(0);
135+
136+
auto &frontendOpts = Inv.getFrontendOptions();
137+
for (const InputFile &Input : frontendOpts.InputsAndOutputs.getAllInputs())
138+
hash = llvm::hash_combine(hash, Input.file());
139+
hash = llvm::hash_combine(
140+
hash,
141+
frontendOpts.InputKind,
142+
llvm::makeArrayRef(frontendOpts.ImplicitImportModuleNames),
143+
frontendOpts.ImplicitObjCHeaderPath,
144+
frontendOpts.ModuleName,
145+
frontendOpts.PrebuiltModuleCachePath,
146+
llvm::makeArrayRef(frontendOpts.PreferInterfaceForModules),
147+
frontendOpts.ParseStdlib,
148+
frontendOpts.EnableSourceImport,
149+
frontendOpts.ImportUnderlyingModule);
150+
151+
auto &langOpts = Inv.getLangOptions();
152+
hash = llvm::hash_combine(
153+
hash,
154+
langOpts.Target.str(),
155+
llvm::VersionTuple(langOpts.EffectiveLanguageVersion).getAsString(),
156+
llvm::VersionTuple(langOpts.PackageDescriptionVersion).getAsString(),
157+
langOpts.DisableAvailabilityChecking,
158+
langOpts.EnableAccessControl,
159+
langOpts.EnableAppExtensionRestrictions,
160+
langOpts.RequireExplicitAvailability,
161+
langOpts.RequireExplicitAvailabilityTarget,
162+
langOpts.EnableObjCInterop,
163+
langOpts.EnableCXXInterop,
164+
langOpts.InferImportAsMember,
165+
langOpts.EnableSwift3ObjCInference);
166+
167+
auto &searchPathOpts = Inv.getSearchPathOptions();
168+
hash = llvm::hash_combine(
169+
hash,
170+
searchPathOpts.SDKPath,
171+
llvm::makeArrayRef(searchPathOpts.ImportSearchPaths),
172+
llvm::makeArrayRef(searchPathOpts.VFSOverlayFiles),
173+
searchPathOpts.RuntimeResourcePath,
174+
llvm::makeArrayRef(searchPathOpts.RuntimeLibraryImportPaths),
175+
llvm::makeArrayRef(searchPathOpts.SkipRuntimeLibraryImportPaths));
176+
for (auto &P : searchPathOpts.FrameworkSearchPaths)
177+
hash = llvm::hash_combine(hash, P.IsSystem, P.Path);
178+
179+
auto &clangOpts = Inv.getClangImporterOptions();
180+
hash = llvm::hash_combine(
181+
hash,
182+
clangOpts.ModuleCachePath,
183+
llvm::makeArrayRef(clangOpts.ExtraArgs),
184+
clangOpts.OverrideResourceDir,
185+
clangOpts.TargetCPU,
186+
static_cast<uint8_t>(clangOpts.Mode),
187+
clangOpts.ImportForwardDeclarations,
188+
clangOpts.InferImportAsMember,
189+
clangOpts.DisableSwiftBridgeAttr,
190+
clangOpts.DisableOverlayModules);
191+
192+
return hash;
193+
}
194+
127195
} // namespace
128196

129197
CompilerInstance *CompletionInstance::getReusingCompilerInstance(
@@ -136,7 +204,8 @@ CompilerInstance *CompletionInstance::getReusingCompilerInstance(
136204
if (!CachedCI)
137205
return nullptr;
138206

139-
if (Invocation.getPCHHash() != CachedCI->getInvocation().getPCHHash())
207+
if (calculateInvocationHash(Invocation) !=
208+
calculateInvocationHash(CachedCI->getInvocation()))
140209
return nullptr;
141210

142211
auto &oldState = CachedCI->getPersistentParserState();

0 commit comments

Comments
 (0)