forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleLoadingTests.cpp
145 lines (117 loc) · 5.12 KB
/
ModuleLoadingTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//===--- ModuleLoadingTests.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "swift/AST/ASTContext.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/ParseableInterfaceModuleLoader.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/Serialization/Validation.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/VirtualFileSystem.h"
using namespace swift;
static std::string createFilename(StringRef base, StringRef name) {
SmallString<256> path = base;
llvm::sys::path::append(path, name);
return llvm::Twine(path).str();
}
static bool emitFileWithContents(StringRef path, StringRef contents,
std::string *pathOut = nullptr) {
int fd;
if (llvm::sys::fs::openFileForWrite(path, fd))
return true;
if (pathOut)
*pathOut = path;
llvm::raw_fd_ostream file(fd, /*shouldClose=*/true);
file << contents;
return false;
}
static bool emitFileWithContents(StringRef base, StringRef name,
StringRef contents,
std::string *pathOut = nullptr) {
return emitFileWithContents(createFilename(base, name), contents, pathOut);
}
namespace unittest {
class OpenTrackingFileSystem : public llvm::vfs::ProxyFileSystem {
llvm::StringMap<unsigned> numberOfOpensPerFile;
public:
OpenTrackingFileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
: llvm::vfs::ProxyFileSystem(fs) {}
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
openFileForRead(const Twine &Path) override {
numberOfOpensPerFile[Path.str()] += 1;
return ProxyFileSystem::openFileForRead(Path);
}
unsigned numberOfOpens(StringRef path) {
return numberOfOpensPerFile[path];
}
};
class ParseableInterfaceModuleLoaderTest : public testing::Test {
protected:
void setupAndLoadParseableModule() {
SmallString<256> tempDir;
ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory(
"ParseableModuleBufferTests.emitModuleInMemory", tempDir));
SWIFT_DEFER { llvm::sys::fs::remove_directories(tempDir); };
auto cacheDir = createFilename(tempDir, "ModuleCache");
ASSERT_FALSE(llvm::sys::fs::create_directory(cacheDir));
auto prebuiltCacheDir = createFilename(tempDir, "PrebuiltModuleCache");
ASSERT_FALSE(llvm::sys::fs::create_directory(prebuiltCacheDir));
// Emit an interface file that we can attempt to compile.
ASSERT_FALSE(emitFileWithContents(tempDir, "Library.swiftinterface",
"// swift-interface-format-version: 1.0\n"
"// swift-module-flags: -module-name TestModule -parse-stdlib\n"
"public func foo()\n"));
SourceManager sourceMgr;
// Create a file system that tracks how many times a file has been opened.
llvm::IntrusiveRefCntPtr<OpenTrackingFileSystem> fs(
new OpenTrackingFileSystem(sourceMgr.getFileSystem()));
sourceMgr.setFileSystem(fs);
PrintingDiagnosticConsumer printingConsumer;
DiagnosticEngine diags(sourceMgr);
diags.addConsumer(printingConsumer);
LangOptions langOpts;
langOpts.Target = llvm::Triple(llvm::sys::getDefaultTargetTriple());
SearchPathOptions searchPathOpts;
auto ctx = ASTContext::get(langOpts, searchPathOpts, sourceMgr, diags);
auto loader = ParseableInterfaceModuleLoader::create(
*ctx, cacheDir, prebuiltCacheDir,
/*dependencyTracker*/nullptr,
ModuleLoadingMode::PreferSerialized);
Identifier moduleName = ctx->getIdentifier("TestModule");
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
std::unique_ptr<llvm::MemoryBuffer> moduleDocBuffer;
auto error =
loader->findModuleFilesInDirectory({moduleName, SourceLoc()}, tempDir,
"Library.swiftmodule", "Library.swiftdoc",
&moduleBuffer, &moduleDocBuffer);
ASSERT_FALSE(error);
ASSERT_FALSE(diags.hadAnyError());
ASSERT_NE(nullptr, moduleBuffer);
// We should not have written a module doc file.
ASSERT_EQ(nullptr, moduleDocBuffer);
// Make sure the buffer identifier points to the written module.
StringRef cachedModulePath = moduleBuffer->getBufferIdentifier();
ASSERT_TRUE(fs->exists(cachedModulePath));
// Assert that we've only opened this file once, to write it.
ASSERT_EQ((unsigned)1, fs->numberOfOpens(cachedModulePath));
auto bufOrErr = fs->getBufferForFile(cachedModulePath);
ASSERT_TRUE(bufOrErr);
auto bufData = (*bufOrErr)->getBuffer();
auto validationInfo = serialization::validateSerializedAST(bufData);
ASSERT_EQ(serialization::Status::Valid, validationInfo.status);
ASSERT_EQ(bufData, moduleBuffer->getBuffer());
}
};
TEST_F(ParseableInterfaceModuleLoaderTest, LoadModuleFromBuffer) {
setupAndLoadParseableModule();
}
} // end namespace unittest