Skip to content

[Index/SourceKit] Remove the code related to calculating a module hash from the indexing walker #25672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions include/swift/Index/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ class DeclContext;
namespace index {

void indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer);
void indexSourceFile(SourceFile *SF, StringRef hash,
IndexDataConsumer &consumer);
void indexModule(ModuleDecl *module, StringRef hash,
IndexDataConsumer &consumer);
void indexSourceFile(SourceFile *SF, IndexDataConsumer &consumer);
void indexModule(ModuleDecl *module, IndexDataConsumer &consumer);

} // end namespace index
} // end namespace swift
Expand Down
3 changes: 1 addition & 2 deletions include/swift/Index/IndexDataConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ class IndexDataConsumer {
virtual void failed(StringRef error) = 0;
virtual void warning(StringRef warning) {}

virtual bool recordHash(StringRef hash, bool isKnown) = 0;
virtual bool startDependency(StringRef name, StringRef path, bool isClangModule,
bool isSystem, StringRef hash) = 0;
bool isSystem) = 0;
virtual bool finishDependency(bool isClangModule) = 0;
virtual Action startSourceEntity(const IndexSymbol &symbol) = 0;
virtual bool finishSourceEntity(SymbolInfo symInfo, SymbolRoleSet roles) = 0;
Expand Down
4 changes: 1 addition & 3 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,7 @@ class RenameRangeCollector : public IndexDataConsumer {
private:
bool indexLocals() override { return true; }
void failed(StringRef error) override {}
bool recordHash(StringRef hash, bool isKnown) override { return true; }
bool startDependency(StringRef name, StringRef path, bool isClangModule,
bool isSystem, StringRef hash) override {
bool startDependency(StringRef name, StringRef path, bool isClangModule, bool isSystem) override {
return true;
}
bool finishDependency(bool isClangModule) override { return true; }
Expand Down
99 changes: 11 additions & 88 deletions lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,14 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
assert(Cancelled || ManuallyVisitedAccessorStack.empty());
}

void visitModule(ModuleDecl &Mod, StringRef Hash);
void visitModule(ModuleDecl &Mod);
void visitDeclContext(DeclContext *DC);

private:
bool visitImports(SourceFileOrModule Mod,
llvm::SmallPtrSetImpl<ModuleDecl *> &Visited);

bool handleSourceOrModuleFile(SourceFileOrModule SFOrMod, StringRef KnownHash,
bool &HashIsKnown);
bool handleSourceOrModuleFile(SourceFileOrModule SFOrMod);

bool walkToDeclPre(Decl *D, CharSourceRange Range) override {
// Do not handle unavailable decls.
Expand Down Expand Up @@ -565,10 +564,6 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
/// \returns false if AST visitation should stop.
bool handleWitnesses(Decl *D, SmallVectorImpl<IndexedWitness> &explicitWitnesses);

void getModuleHash(SourceFileOrModule SFOrMod, llvm::raw_ostream &OS);
llvm::hash_code hashModule(llvm::hash_code code, SourceFileOrModule SFOrMod);
llvm::hash_code hashFileReference(llvm::hash_code code,
SourceFileOrModule SFOrMod);
void getRecursiveModuleImports(ModuleDecl &Mod,
SmallVectorImpl<ModuleDecl *> &Imports);
void
Expand Down Expand Up @@ -601,7 +596,7 @@ void IndexSwiftASTWalker::visitDeclContext(DeclContext *Context) {
ManuallyVisitedAccessorStack.pop_back();
}

void IndexSwiftASTWalker::visitModule(ModuleDecl &Mod, StringRef KnownHash) {
void IndexSwiftASTWalker::visitModule(ModuleDecl &Mod) {
SourceFile *SrcFile = nullptr;
for (auto File : Mod.getFiles()) {
if (auto SF = dyn_cast<SourceFile>(File)) {
Expand All @@ -613,41 +608,23 @@ void IndexSwiftASTWalker::visitModule(ModuleDecl &Mod, StringRef KnownHash) {
}
}

bool HashIsKnown;
if (SrcFile != nullptr) {
IsModuleFile = false;
if (!handleSourceOrModuleFile(*SrcFile, KnownHash, HashIsKnown))
if (!handleSourceOrModuleFile(*SrcFile))
return;
if (HashIsKnown)
return; // No need to report symbols.
walk(*SrcFile);
} else {
IsModuleFile = true;
isSystemModule = Mod.isSystemModule();
if (!handleSourceOrModuleFile(Mod, KnownHash, HashIsKnown))
if (!handleSourceOrModuleFile(Mod))
return;
if (HashIsKnown)
return; // No need to report symbols.
walk(Mod);
}
}

bool IndexSwiftASTWalker::handleSourceOrModuleFile(SourceFileOrModule SFOrMod,
StringRef KnownHash,
bool &HashIsKnown) {
bool IndexSwiftASTWalker::handleSourceOrModuleFile(SourceFileOrModule SFOrMod) {
// Common reporting for TU/module file.

SmallString<32> HashBuf;
{
llvm::raw_svector_ostream HashOS(HashBuf);
getModuleHash(SFOrMod, HashOS);
StringRef Hash = HashOS.str();
HashIsKnown = Hash == KnownHash;
if (!IdxConsumer.recordHash(Hash, HashIsKnown))
return false;
}

// We always report the dependencies, even if the hash is known.
llvm::SmallPtrSet<ModuleDecl *, 16> Visited;
return visitImports(SFOrMod, Visited);
}
Expand Down Expand Up @@ -703,16 +680,8 @@ bool IndexSwiftASTWalker::visitImports(
continue;
bool IsClangModule = *IsClangModuleOpt;

StringRef Hash;
SmallString<32> HashBuf;
if (!IsClangModule) {
llvm::raw_svector_ostream HashOS(HashBuf);
getModuleHash(*Mod, HashOS);
Hash = HashOS.str();
}

if (!IdxConsumer.startDependency(Mod->getName().str(), Path, IsClangModule,
Mod->isSystemModule(), Hash))
Mod->isSystemModule()))
return false;
if (!IsClangModule)
if (!visitImports(*Mod, Visited))
Expand Down Expand Up @@ -1441,43 +1410,6 @@ bool IndexSwiftASTWalker::indexComment(const Decl *D) {
return !Cancelled;
}

llvm::hash_code
IndexSwiftASTWalker::hashFileReference(llvm::hash_code code,
SourceFileOrModule SFOrMod) {
StringRef Filename = SFOrMod.getFilename();
if (Filename.empty())
return code;

// FIXME: FileManager for swift ?

llvm::sys::fs::file_status Status;
if (std::error_code Ret = llvm::sys::fs::status(Filename, Status)) {
// Failure to read the file, just use filename to recover.
warn([&](llvm::raw_ostream &OS) {
OS << "failed to stat file: " << Filename << " (" << Ret.message() << ')';
});
return hash_combine(code, Filename);
}

// Don't use inode because it can easily change when you update the repository
// even though the file is supposed to be the same (same size/time).
code = hash_combine(code, Filename);
auto mtime = Status.getLastModificationTime().time_since_epoch().count();
return hash_combine(code, Status.getSize(), mtime);
}

llvm::hash_code IndexSwiftASTWalker::hashModule(llvm::hash_code code,
SourceFileOrModule SFOrMod) {
code = hashFileReference(code, SFOrMod);

SmallVector<ModuleDecl *, 16> Imports;
getRecursiveModuleImports(SFOrMod.getModule(), Imports);
for (auto Import : Imports)
code = hashFileReference(code, *Import);

return code;
}

void IndexSwiftASTWalker::getRecursiveModuleImports(
ModuleDecl &Mod, SmallVectorImpl<ModuleDecl *> &Imports) {
auto It = ImportsMap.find(&Mod);
Expand Down Expand Up @@ -1575,13 +1507,6 @@ void IndexSwiftASTWalker::collectRecursiveModuleImports(
}
}

void IndexSwiftASTWalker::getModuleHash(SourceFileOrModule Mod,
llvm::raw_ostream &OS) {
// FIXME: Use a longer hash string to minimize possibility for conflicts.
llvm::hash_code code = hashModule(0, Mod);
OS << llvm::APInt(64, code).toString(36, /*Signed=*/false);
}

static Type getContextFreeInterfaceType(ValueDecl *VD) {
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
return AFD->getMethodInterfaceType();
Expand Down Expand Up @@ -1661,19 +1586,17 @@ void index::indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer) {
consumer.finish();
}

void index::indexSourceFile(SourceFile *SF, StringRef hash,
IndexDataConsumer &consumer) {
void index::indexSourceFile(SourceFile *SF, IndexDataConsumer &consumer) {
assert(SF);
unsigned bufferID = SF->getBufferID().getValue();
IndexSwiftASTWalker walker(consumer, SF->getASTContext(), bufferID);
walker.visitModule(*SF->getParentModule(), hash);
walker.visitModule(*SF->getParentModule());
consumer.finish();
}

void index::indexModule(ModuleDecl *module, StringRef hash,
IndexDataConsumer &consumer) {
void index::indexModule(ModuleDecl *module, IndexDataConsumer &consumer) {
assert(module);
IndexSwiftASTWalker walker(consumer, module->getASTContext());
walker.visitModule(*module, hash);
walker.visitModule(*module);
consumer.finish();
}
14 changes: 5 additions & 9 deletions lib/Index/IndexRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ class IndexRecordingConsumer : public IndexDataConsumer {
// FIXME: expose errors?
}

bool recordHash(StringRef hash, bool isKnown) override { return true; }
bool startDependency(StringRef name, StringRef path, bool isClangModule,
bool isSystem, StringRef hash) override {
bool startDependency(StringRef name, StringRef path, bool isClangModule, bool isSystem) override {
return true;
}
bool finishDependency(bool isClangModule) override { return true; }
Expand Down Expand Up @@ -203,9 +201,7 @@ class StdlibGroupsIndexRecordingConsumer : public IndexDataConsumer {
// FIXME: expose errors?
}

bool recordHash(StringRef hash, bool isKnown) override { return true; }
bool startDependency(StringRef name, StringRef path, bool isClangModule,
bool isSystem, StringRef hash) override {
bool startDependency(StringRef name, StringRef path, bool isClangModule, bool isSystem) override {
return true;
}
bool finishDependency(bool isClangModule) override { return true; }
Expand Down Expand Up @@ -342,7 +338,7 @@ recordSourceFile(SourceFile *SF, StringRef indexStorePath,
bool failed = false;
auto consumer = makeRecordingConsumer(SF->getFilename(), indexStorePath,
&diags, &recordFile, &failed);
indexSourceFile(SF, /*Hash=*/"", *consumer);
indexSourceFile(SF, *consumer);

if (!failed && !recordFile.empty())
callback(recordFile, SF->getFilename());
Expand Down Expand Up @@ -482,7 +478,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
bool failed = false;
auto consumer = makeRecordingConsumer(filename, indexStorePath,
&diags, &recordFile, &failed);
indexModule(module, /*Hash=*/"", *consumer);
indexModule(module, *consumer);

if (failed)
return true;
Expand Down Expand Up @@ -531,7 +527,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
records.emplace_back(outRecordFile, moduleName.str());
return true;
});
indexModule(module, /*Hash=*/"", groupIndexConsumer);
indexModule(module, groupIndexConsumer);
if (failed)
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions test/SourceKit/Indexing/Inputs/implicit-vis/a.index.response
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
key.hash: <hash>,
key.dependencies: [
{
key.kind: source.lang.swift.import.module.swift,
key.name: "Swift",
key.filepath: Swift.swiftmodule,
key.hash: <hash>,
key.is_system: 1
}
],
Expand Down
2 changes: 0 additions & 2 deletions test/SourceKit/Indexing/Inputs/implicit-vis/b.index.response
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
key.hash: <hash>,
key.dependencies: [
{
key.kind: source.lang.swift.import.module.swift,
key.name: "Swift",
key.filepath: Swift.swiftmodule,
key.hash: <hash>,
key.is_system: 1
}
],
Expand Down
4 changes: 0 additions & 4 deletions test/SourceKit/Indexing/Inputs/test_module.index.response
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
{
key.hash: <hash>,
key.dependencies: [
{
key.kind: source.lang.swift.import.module.swift,
key.name: "Swift",
key.filepath: Swift.swiftmodule,
key.hash: <hash>,
key.is_system: 1
},
{
key.kind: source.lang.swift.import.module.swift,
key.name: "SwiftOnoneSupport",
key.filepath: SwiftOnoneSupport.swiftmodule,
key.hash: <hash>,
key.is_system: 1,
key.dependencies: [
{
key.kind: source.lang.swift.import.module.swift,
key.name: "Swift",
key.filepath: Swift.swiftmodule,
key.hash: <hash>,
key.is_system: 1
}
]
Expand Down
6 changes: 1 addition & 5 deletions test/SourceKit/Indexing/index.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// FIXME(integers): %t.response content is non-deterministic with the new
// integer protocols
// XFAIL: *

// RUN: %sourcekitd-test -req=index %s -- -Xfrontend -serialize-diagnostics-path -Xfrontend %t.dia %s | %sed_clean > %t.response
// RUN: %sourcekitd-test -req=index %s -- -Xfrontend -serialize-diagnostics-path -Xfrontend %t.dia %s | %sed_clean | sed -e 's/key.usr: \".*\"/key.usr: <usr>/g' > %t.response
// RUN: diff -u %s.response %t.response

var globV: Int
Expand Down
Loading