Skip to content

Commit 8f4fd06

Browse files
author
David Ungar
committed
Use Optional for fingerprint
1 parent 0a3be75 commit 8f4fd06

File tree

7 files changed

+29
-30
lines changed

7 files changed

+29
-30
lines changed

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,9 @@ class IterableDeclContext {
802802
// Some Decls are IterableDeclContexts, but not all.
803803
static bool classof(const Decl *D);
804804

805-
/// Return a hash of all tokens in the body for dependency analysis.
806-
/// Empty string means no hash available.
807-
StringRef getBodyFingerprint() const;
805+
/// Return a hash of all tokens in the body for dependency analysis, if
806+
/// available.
807+
Optional<std::string> getBodyFingerprint() const;
808808

809809
bool areDependenciesUsingTokenHashesForTypeBodies() const;
810810

include/swift/AST/ParseRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ void reportEvaluatedRequest(UnifiedStatsReporter &stats,
2828
const Request &request);
2929

3030
struct FingerprintAndMembers {
31-
std::string fingerprint;
32-
ArrayRef<Decl *> members;
31+
Optional<std::string> fingerprint = None;
32+
ArrayRef<Decl *> members = {};
3333
bool operator==(const FingerprintAndMembers &x) const {
3434
return fingerprint == x.fingerprint && members == x.members;
3535
}

include/swift/Parse/Parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ class Parser {
927927
bool IsAtStartOfLineOrPreviousHadSemi,
928928
llvm::function_ref<void(Decl*)> Handler);
929929

930-
std::pair<std::vector<Decl *>, llvm::SmallString<32>>
930+
std::pair<std::vector<Decl *>, Optional<std::string>>
931931
parseDeclListDelayed(IterableDeclContext *IDC);
932932

933933
bool parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
@@ -1066,7 +1066,7 @@ class Parser {
10661066
ParserStatus parseDeclItem(bool &PreviousHadSemi,
10671067
Parser::ParseDeclOptions Options,
10681068
llvm::function_ref<void(Decl*)> handler);
1069-
std::pair<std::vector<Decl *>, llvm::SmallString<32>>
1069+
std::pair<std::vector<Decl *>, Optional<std::string>>
10701070
parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
10711071
ParseDeclOptions Options, IterableDeclContext *IDC,
10721072
bool &hadError);

lib/AST/DeclContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,10 @@ IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
901901
}
902902
}
903903

904-
StringRef IterableDeclContext::getBodyFingerprint() const {
904+
Optional<std::string> IterableDeclContext::getBodyFingerprint() const {
905905
// Only makes sense for contexts in a source file
906906
if (!getDecl()->getDeclContext()->getParentSourceFile())
907-
return "";
907+
return None;
908908
auto mutableThis = const_cast<IterableDeclContext *>(this);
909909
return evaluateOrDefault(getASTContext().evaluator,
910910
ParseMembersRequest{mutableThis},

lib/AST/FineGrainedDependenciesSourceFileDepGraphConstructor.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,8 @@ class SourceFileDepGraphConstructor {
669669
return None;
670670
}
671671
static Optional<std::string> getFingerprintIfAny(const Decl *d) {
672-
if (const auto *idc = dyn_cast<IterableDeclContext>(d)) {
673-
StringRef fp = idc->getBodyFingerprint();
674-
if (!fp.empty())
675-
return fp.str();
676-
}
672+
if (const auto *idc = dyn_cast<IterableDeclContext>(d))
673+
return idc->getBodyFingerprint();
677674
return None;
678675
}
679676

lib/Parse/ParseDecl.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,7 +3912,7 @@ static ScopeKind getMemberParseScopeKind(IterableDeclContext *idc) {
39123912
}
39133913
}
39143914

3915-
std::pair<std::vector<Decl *>, llvm::SmallString<32>>
3915+
std::pair<std::vector<Decl *>, Optional<std::string>>
39163916
Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
39173917
Decl *D = const_cast<Decl*>(IDC->getDecl());
39183918
DeclContext *DC = cast<DeclContext>(D);
@@ -3926,7 +3926,7 @@ Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
39263926

39273927
if (BodyRange.isInvalid()) {
39283928
assert(D->isImplicit());
3929-
return {std::vector<Decl *>(), llvm::SmallString<32>()};
3929+
return {std::vector<Decl *>(), None};
39303930
}
39313931

39323932
auto BeginParserPosition = getParserPosition({BodyRange.Start, SourceLoc()});
@@ -3952,7 +3952,7 @@ Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
39523952
// If there is no left brace, then return an empty list of declarations;
39533953
// we will have already diagnosed this.
39543954
if (!Tok.is(tok::l_brace))
3955-
return {std::vector<Decl *>(), llvm::SmallString<32>()};
3955+
return {std::vector<Decl *>(), None};
39563956

39573957
// Re-enter the lexical scope. The top-level scope is needed because
39583958
// delayed parsing of members happens with a fresh parser, where there is
@@ -4337,7 +4337,7 @@ bool Parser::parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
43374337
Context.evaluator.cacheOutput(
43384338
ParseMembersRequest{IDC},
43394339
FingerprintAndMembers{
4340-
membersAndHash.second.str().str(),
4340+
membersAndHash.second,
43414341
Context.AllocateCopy(llvm::makeArrayRef(membersAndHash.first))});
43424342

43434343
if (hadError)
@@ -4352,7 +4352,7 @@ bool Parser::parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
43524352
/// \verbatim
43534353
/// decl* '}'
43544354
/// \endverbatim
4355-
std::pair<std::vector<Decl *>, llvm::SmallString<32>>
4355+
std::pair<std::vector<Decl *>, Optional<std::string>>
43564356
Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
43574357
ParseDeclOptions Options, IterableDeclContext *IDC,
43584358
bool &hadError) {
@@ -4397,15 +4397,14 @@ Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
43974397
if (RBLoc.isInvalid())
43984398
hadError = true;
43994399

4400-
llvm::SmallString<32> tokenHashString;
4401-
// Must return an empty string if there is really no fingerprint
4402-
if (Context.LangOpts.EnableTypeFingerprints) {
4403-
llvm::MD5::MD5Result result;
4404-
tokenHashForThisDeclList.final(result);
4405-
llvm::MD5::stringifyResult(result, tokenHashString);
4406-
}
4400+
if (!Context.LangOpts.EnableTypeFingerprints)
4401+
return std::make_pair(decls, None);
44074402

4408-
return std::make_pair(decls, tokenHashString);
4403+
llvm::MD5::MD5Result result;
4404+
tokenHashForThisDeclList.final(result);
4405+
llvm::SmallString<32> tokenHashString;
4406+
llvm::MD5::stringifyResult(result, tokenHashString);
4407+
return std::make_pair(decls, tokenHashString.str().str());
44094408
}
44104409

44114410
bool Parser::canDelayMemberDeclParsing(bool &HasOperatorDeclarations,

lib/Parse/ParseRequests.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ namespace swift {
3535

3636
void swift::simple_display(llvm::raw_ostream &out,
3737
const FingerprintAndMembers &value) {
38-
simple_display(out, value.fingerprint);
38+
if (value.fingerprint)
39+
simple_display(out, value.fingerprint.getValue());
40+
else
41+
out << "<no fingerprint>";
3942
out << ", ";
4043
simple_display(out, value.members);
4144
}
@@ -53,8 +56,8 @@ ParseMembersRequest::evaluate(Evaluator &evaluator,
5356
parser.SyntaxContext->disable();
5457
ASTContext &ctx = idc->getDecl()->getASTContext();
5558
auto declsAndHash = parser.parseDeclListDelayed(idc);
56-
FingerprintAndMembers fingerprintAndMembers = {
57-
declsAndHash.second.str().str(), declsAndHash.first};
59+
FingerprintAndMembers fingerprintAndMembers = {declsAndHash.second,
60+
declsAndHash.first};
5861
return FingerprintAndMembers{
5962
fingerprintAndMembers.fingerprint,
6063
ctx.AllocateCopy(llvm::makeArrayRef(fingerprintAndMembers.members))};

0 commit comments

Comments
 (0)