Add ModuleDecl::ReverseFullNameIterator

Package up the logic that generates a full Clang module name, so that
(a) we don't have to deal with clang::Module in quite as many places
in the /Swift/ compiler, and (b) we can avoid the cost of a temporary
string in a few places.

The main places where this is /not/ adopted is where we don't just
want to know the parent module name, but actually the module itself.
This is mostly indexing-related queries, which use the very similar
ModuleEntity class also defined in Module.h. I didn't quite see an
obvious way to unify these, but that might be where we want to go.

No functionality change.
This commit is contained in:
Jordan Rose
2018-09-06 19:47:25 -07:00
parent d6133f408d
commit 37ec248823
7 changed files with 113 additions and 61 deletions

View File

@@ -37,9 +37,6 @@
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Serialization/SerializationOptions.h"
#include "swift/Strings.h"
#include "clang/Basic/Module.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitcode/BitstreamWriter.h"
@@ -1032,22 +1029,12 @@ void Serializer::writeDocHeader() {
using ImportPathBlob = llvm::SmallString<64>;
static void flattenImportPath(const ModuleDecl::ImportedModule &import,
ImportPathBlob &out) {
ArrayRef<FileUnit *> files = import.second->getFiles();
if (auto clangModule = dyn_cast<ClangModuleUnit>(files.front())) {
// FIXME: This is an awful hack to handle Clang submodules.
// Once Swift has a native notion of submodules, this can go away.
const clang::Module *submodule = clangModule->getClangModule();
SmallVector<StringRef, 4> submoduleNames;
do {
submoduleNames.push_back(submodule->Name);
submodule = submodule->Parent;
} while (submodule);
interleave(submoduleNames.rbegin(), submoduleNames.rend(),
[&out](StringRef next) { out.append(next); },
[&out] { out.push_back('\0'); });
} else {
out.append(import.second->getName().str());
}
SmallVector<StringRef, 4> reverseSubmoduleNames(
import.second->getReverseFullModuleName(), {});
interleave(reverseSubmoduleNames.rbegin(), reverseSubmoduleNames.rend(),
[&out](StringRef next) { out.append(next); },
[&out] { out.push_back('\0'); });
if (import.first.empty())
return;