Skip to content

Commit 0b22d91

Browse files
committed
[unittests] Extend Sema testing fixture to load stdlib (+ shims)
Setup module importers, load stdlib, establish separate testing module and load a single main file there which imports standard library.
1 parent 9239692 commit 0b22d91

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

unittests/Sema/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ add_swift_unittest(swiftSemaTests
44

55
target_link_libraries(swiftSemaTests
66
PRIVATE
7-
swiftSema)
7+
swiftAST
8+
swiftSema
9+
swiftSerialization)
10+
11+
target_compile_definitions(swiftSemaTests PRIVATE
12+
SWIFTLIB_DIR=\"${SWIFTLIB_DIR}\")

unittests/Sema/SemaFixture.cpp

+25-7
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,42 @@
1313
#include "SemaFixture.h"
1414
#include "swift/AST/Module.h"
1515
#include "swift/AST/ParseRequests.h"
16-
#include "swift/Strings.h"
16+
#include "swift/AST/SourceFile.h"
17+
#include "swift/Basic/LLVMInitialize.h"
18+
#include "swift/ClangImporter/ClangImporter.h"
19+
#include "swift/Serialization/SerializedModuleLoader.h"
1720
#include "swift/Subsystems.h"
1821

1922
using namespace swift;
2023
using namespace swift::unittest;
2124

25+
using ModuleDecl = SourceFile::ImportedModuleDesc;
26+
2227
SemaTest::SemaTest()
2328
: Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts,
2429
ClangImporterOpts, SourceMgr, Diags)) {
30+
INITIALIZE_LLVM();
31+
2532
registerParseRequestFunctions(Context.evaluator);
2633
registerTypeCheckerRequestFunctions(Context.evaluator);
27-
auto stdlibID = Context.getIdentifier(STDLIB_NAME);
28-
auto *module = ModuleDecl::create(stdlibID, Context);
29-
Context.addLoadedModule(module);
3034

31-
FileForLookups = new (Context) SourceFile(*module, SourceFileKind::Library,
32-
/*buffer*/ None);
33-
module->addFile(*FileForLookups);
35+
Context.addModuleLoader(ImplicitSerializedModuleLoader::create(Context));
36+
Context.addModuleLoader(ClangImporter::create(Context), /*isClang=*/true);
37+
38+
auto *stdlib = Context.getStdlibModule(/*loadIfAbsent=*/true);
39+
assert(stdlib && "Failed to load standard library");
40+
41+
auto *module =
42+
ModuleDecl::create(Context.getIdentifier("SemaTests"), Context);
43+
44+
MainFile = new (Context) SourceFile(*module, SourceFileKind::Main,
45+
/*buffer=*/None);
46+
47+
auto stdlibImport =
48+
ModuleDesc({ImportPath::Access(), stdlib}, /*options=*/{});
49+
50+
MainFile->setImports(stdlibImport);
51+
module->addFile(*MainFile);
3452

3553
DC = module;
3654
}

unittests/Sema/SemaFixture.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
#include "swift/AST/Module.h"
1616
#include "swift/AST/SourceFile.h"
1717
#include "swift/Basic/LangOptions.h"
18+
#include "swift/Basic/Platform.h"
1819
#include "swift/Basic/SourceManager.h"
20+
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/ADT/SmallString.h"
1922
#include "llvm/Support/Host.h"
23+
#include "llvm/Support/Path.h"
2024
#include "gtest/gtest.h"
25+
#include <string>
2126

2227
namespace swift {
2328
namespace unittest {
@@ -33,12 +38,20 @@ class SemaTestBase : public ::testing::Test {
3338

3439
SemaTestBase() : Diags(SourceMgr) {
3540
LangOpts.Target = llvm::Triple(llvm::sys::getProcessTriple());
41+
42+
llvm::SmallString<128> libDir(SWIFTLIB_DIR);
43+
llvm::sys::path::append(libDir, getPlatformNameForTriple(LangOpts.Target));
44+
45+
SearchPathOpts.RuntimeResourcePath = SWIFTLIB_DIR;
46+
SearchPathOpts.RuntimeLibraryPaths.push_back(std::string(libDir.str()));
47+
SearchPathOpts.RuntimeLibraryImportPaths.push_back(
48+
std::string(libDir.str()));
3649
}
3750
};
3851

3952
/// Owns an ASTContext and the associated types.
4053
class SemaTest : public SemaTestBase {
41-
SourceFile *FileForLookups;
54+
SourceFile *MainFile;
4255

4356
public:
4457
ASTContext &Context;

0 commit comments

Comments
 (0)