Skip to content

Commit 912ed27

Browse files
committed
[NFC] Use Fingerprints When Computing the Interface Hash
1 parent 27d2926 commit 912ed27

7 files changed

+17
-38
lines changed

include/swift/AST/AbstractSourceFileDepGraphFactory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AbstractSourceFileDepGraphFactory {
3232
const std::string swiftDeps;
3333

3434
/// The fingerprint of the whole file
35-
const std::string fileFingerprint;
35+
Fingerprint fileFingerprint;
3636

3737
/// For debugging
3838
const bool emitDotFileAfterConstruction;
@@ -47,7 +47,7 @@ class AbstractSourceFileDepGraphFactory {
4747
/// See the instance variable comments for explanation.
4848
AbstractSourceFileDepGraphFactory(bool hadCompilationError,
4949
StringRef swiftDeps,
50-
StringRef fileFingerprint,
50+
Fingerprint fileFingerprint,
5151
bool emitDotFileAfterConstruction,
5252
DiagnosticEngine &diags);
5353

include/swift/AST/SourceFile.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,10 @@ class SourceFile final : public FileUnit {
525525
}
526526

527527
/// Output this file's interface hash into the provided string buffer.
528-
void getInterfaceHash(llvm::SmallString<32> &str) const;
528+
Fingerprint getInterfaceHash() const;
529529

530530
void dumpInterfaceHash(llvm::raw_ostream &out) {
531-
llvm::SmallString<32> str;
532-
getInterfaceHash(str);
533-
out << str << '\n';
531+
out << getInterfaceHash() << '\n';
534532
}
535533

536534
/// If this source file has been told to collect its parsed tokens, retrieve

lib/AST/AbstractSourceFileDepGraphFactory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ using namespace fine_grained_dependencies;
3434

3535
AbstractSourceFileDepGraphFactory::AbstractSourceFileDepGraphFactory(
3636
bool hadCompilationError, StringRef swiftDeps,
37-
StringRef fileFingerprint, bool emitDotFileAfterConstruction,
37+
Fingerprint fileFingerprint, bool emitDotFileAfterConstruction,
3838
DiagnosticEngine &diags)
3939
: hadCompilationError(hadCompilationError), swiftDeps(swiftDeps.str()),
40-
fileFingerprint(fileFingerprint.str()),
40+
fileFingerprint(fileFingerprint),
4141
emitDotFileAfterConstruction(emitDotFileAfterConstruction), diags(diags) {
4242
}
4343

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,24 +218,10 @@ FrontendSourceFileDepGraphFactory::FrontendSourceFileDepGraphFactory(
218218
const SourceFile *SF, StringRef outputPath,
219219
const DependencyTracker &depTracker, const bool alsoEmitDotFile)
220220
: AbstractSourceFileDepGraphFactory(
221-
SF->getASTContext().hadError(), outputPath, getInterfaceHash(SF),
221+
SF->getASTContext().hadError(), outputPath, SF->getInterfaceHash(),
222222
alsoEmitDotFile, SF->getASTContext().Diags),
223223
SF(SF), depTracker(depTracker) {}
224224

225-
/// Centralize the invariant that the fingerprint of the whole file is the
226-
/// interface hash
227-
std::string
228-
FrontendSourceFileDepGraphFactory::getFingerprint(const SourceFile *SF) {
229-
return getInterfaceHash(SF);
230-
}
231-
232-
std::string
233-
FrontendSourceFileDepGraphFactory::getInterfaceHash(const SourceFile *SF) {
234-
llvm::SmallString<32> interfaceHash;
235-
SF->getInterfaceHash(interfaceHash);
236-
return interfaceHash.str().str();
237-
}
238-
239225
//==============================================================================
240226
// MARK: FrontendSourceFileDepGraphFactory - adding collections of defined Decls
241227
//==============================================================================
@@ -520,7 +506,9 @@ void FrontendSourceFileDepGraphFactory::addAllUsedDecls() {
520506
ModuleDepGraphFactory::ModuleDepGraphFactory(const ModuleDecl *Mod,
521507
bool emitDot)
522508
: AbstractSourceFileDepGraphFactory(Mod->getASTContext().hadError(),
523-
Mod->getNameStr(), "0xBADBEEF", emitDot,
509+
Mod->getNameStr(),
510+
Fingerprint(std::string{Fingerprint::DIGEST_LENGTH, '0'}),
511+
emitDot,
524512
Mod->getASTContext().Diags),
525513
Mod(Mod) {}
526514

lib/AST/FrontendSourceFileDepGraphFactory.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ class FrontendSourceFileDepGraphFactory
3434
~FrontendSourceFileDepGraphFactory() override = default;
3535

3636
private:
37-
static std::string getFingerprint(const SourceFile *SF);
38-
static std::string getInterfaceHash(const SourceFile *SF);
39-
4037
void addAllDefinedDecls() override;
4138
void addAllUsedDecls() override;
4239
};

lib/AST/Module.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,15 +1074,15 @@ LookupConformanceInModuleRequest::evaluate(
10741074
return ProtocolConformanceRef(conformance);
10751075
}
10761076

1077-
void SourceFile::getInterfaceHash(llvm::SmallString<32> &str) const {
1077+
Fingerprint SourceFile::getInterfaceHash() const {
10781078
assert(hasInterfaceHash() && "Interface hash not enabled");
10791079
auto &eval = getASTContext().evaluator;
10801080
auto *mutableThis = const_cast<SourceFile *>(this);
10811081
auto md5 = *evaluateOrDefault(eval, ParseSourceFileRequest{mutableThis}, {})
10821082
.InterfaceHash;
10831083
llvm::MD5::MD5Result result;
10841084
md5.final(result);
1085-
llvm::MD5::stringifyResult(result, str);
1085+
return Fingerprint{std::move(result)};
10861086
}
10871087

10881088
syntax::SourceFileSyntax SourceFile::getSyntaxRoot() const {

lib/IDE/CompletionInstance.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,10 @@ static bool areAnyDependentFilesInvalidated(
295295
/// Note that we don't care about local types (i.e. type declarations inside
296296
/// function bodies, closures, or top level statement bodies) because they are
297297
/// not visible from other functions where the completion is happening.
298-
void getInterfaceHashIncludingTypeMembers(SourceFile *SF,
299-
llvm::SmallString<32> &str) {
298+
static Fingerprint getInterfaceHashIncludingTypeMembers(const SourceFile *SF) {
300299
/// FIXME: Gross. Hashing multiple "hash" values.
301300
llvm::MD5 hash;
302-
SF->getInterfaceHash(str);
303-
hash.update(str);
301+
hash.update(SF->getInterfaceHash().getRawValue());
304302

305303
std::function<void(IterableDeclContext *)> hashTypeBodyFingerprints =
306304
[&](IterableDeclContext *IDC) {
@@ -318,7 +316,7 @@ void getInterfaceHashIncludingTypeMembers(SourceFile *SF,
318316

319317
llvm::MD5::MD5Result result;
320318
hash.final(result);
321-
str = result.digest();
319+
return Fingerprint{std::move(result)};
322320
}
323321

324322
} // namespace
@@ -397,10 +395,8 @@ bool CompletionInstance::performCachedOperationIfPossible(
397395
switch (newInfo.Kind) {
398396
case CodeCompletionDelayedDeclKind::FunctionBody: {
399397
// If the interface has changed, AST must be refreshed.
400-
llvm::SmallString<32> oldInterfaceHash{};
401-
llvm::SmallString<32> newInterfaceHash{};
402-
getInterfaceHashIncludingTypeMembers(oldSF, oldInterfaceHash);
403-
getInterfaceHashIncludingTypeMembers(tmpSF, newInterfaceHash);
398+
const auto oldInterfaceHash = getInterfaceHashIncludingTypeMembers(oldSF);
399+
const auto newInterfaceHash = getInterfaceHashIncludingTypeMembers(tmpSF);
404400
if (oldInterfaceHash != newInterfaceHash)
405401
return false;
406402

0 commit comments

Comments
 (0)