-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[unittests] Add a fixture for Sema unit tests #34286
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9239692
[unittests] Add a fixture for Sema unit tests
xedin 0b22d91
[unittests] Extend Sema testing fixture to load stdlib (+ shims)
xedin b2c31c3
[unittests/Sema] Add an ability to retrieve stdlib types by name
xedin dc7c9c2
[unittests/Sema] Add a simple integer literal type inference test
xedin 6e470f5
[unittest/Sema] NFC: Switch to use `AttributedImport` instead of depr…
xedin fde1af7
[unittest/Sema] Use default target triple to fix Windows build
xedin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===--- BindingInferenceTests.cpp ----------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 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 "SemaFixture.h" | ||
#include "swift/AST/Expr.h" | ||
#include "swift/Sema/ConstraintSystem.h" | ||
|
||
using namespace swift; | ||
using namespace swift::unittest; | ||
using namespace swift::constraints; | ||
|
||
TEST_F(SemaTest, TestIntLiteralBindingInference) { | ||
ConstraintSystemOptions options; | ||
options |= ConstraintSystemFlags::AllowUnresolvedTypeVariables; | ||
|
||
ConstraintSystem cs(DC, options); | ||
|
||
auto *intLiteral = IntegerLiteralExpr::createFromUnsigned(Context, 42); | ||
|
||
auto *literalTy = cs.createTypeVariable(cs.getConstraintLocator(intLiteral), | ||
/*options=*/0); | ||
|
||
cs.addConstraint( | ||
ConstraintKind::LiteralConformsTo, literalTy, | ||
Context.getProtocol(KnownProtocolKind::ExpressibleByIntegerLiteral) | ||
->getDeclaredInterfaceType(), | ||
cs.getConstraintLocator(intLiteral)); | ||
|
||
auto bindings = cs.inferBindingsFor(literalTy); | ||
|
||
ASSERT_EQ(bindings.Bindings.size(), (unsigned)1); | ||
|
||
const auto &binding = bindings.Bindings.front(); | ||
|
||
ASSERT_TRUE(binding.BindingType->isEqual(getStdlibType("Int"))); | ||
ASSERT_TRUE(binding.hasDefaultedLiteralProtocol()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
add_swift_unittest(swiftSemaTests | ||
SemaFixture.cpp | ||
BindingInferenceTests.cpp) | ||
|
||
target_link_libraries(swiftSemaTests | ||
PRIVATE | ||
swiftAST | ||
swiftSema | ||
swiftSerialization) | ||
|
||
target_compile_definitions(swiftSemaTests PRIVATE | ||
SWIFTLIB_DIR=\"${SWIFTLIB_DIR}\") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//===--- SemaFixture.cpp - Helper for setting up Sema context --------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 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 "SemaFixture.h" | ||
#include "swift/AST/Decl.h" | ||
#include "swift/AST/Import.h" | ||
#include "swift/AST/Module.h" | ||
#include "swift/AST/ParseRequests.h" | ||
#include "swift/AST/SourceFile.h" | ||
#include "swift/AST/Type.h" | ||
#include "swift/AST/Types.h" | ||
#include "swift/Basic/LLVMInitialize.h" | ||
#include "swift/ClangImporter/ClangImporter.h" | ||
#include "swift/Serialization/SerializedModuleLoader.h" | ||
#include "swift/Subsystems.h" | ||
|
||
using namespace swift; | ||
using namespace swift::unittest; | ||
|
||
SemaTest::SemaTest() | ||
: Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts, | ||
ClangImporterOpts, SourceMgr, Diags)) { | ||
INITIALIZE_LLVM(); | ||
|
||
registerParseRequestFunctions(Context.evaluator); | ||
registerTypeCheckerRequestFunctions(Context.evaluator); | ||
|
||
Context.addModuleLoader(ImplicitSerializedModuleLoader::create(Context)); | ||
Context.addModuleLoader(ClangImporter::create(Context), /*isClang=*/true); | ||
|
||
auto *stdlib = Context.getStdlibModule(/*loadIfAbsent=*/true); | ||
assert(stdlib && "Failed to load standard library"); | ||
|
||
auto *module = | ||
ModuleDecl::create(Context.getIdentifier("SemaTests"), Context); | ||
|
||
MainFile = new (Context) SourceFile(*module, SourceFileKind::Main, | ||
/*buffer=*/None); | ||
|
||
AttributedImport<ImportedModule> stdlibImport{{ImportPath::Access(), stdlib}, | ||
/*options=*/{}}; | ||
|
||
MainFile->setImports(stdlibImport); | ||
module->addFile(*MainFile); | ||
|
||
DC = module; | ||
} | ||
|
||
Type SemaTest::getStdlibType(StringRef name) const { | ||
auto typeName = Context.getIdentifier(name); | ||
|
||
auto *stdlib = Context.getStdlibModule(); | ||
|
||
llvm::SmallVector<ValueDecl *, 4> results; | ||
stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results); | ||
|
||
if (results.size() != 1) | ||
return Type(); | ||
|
||
if (auto *decl = dyn_cast<TypeDecl>(results.front())) { | ||
if (auto *NTD = dyn_cast<NominalTypeDecl>(decl)) | ||
return NTD->getDeclaredType(); | ||
return decl->getDeclaredInterfaceType(); | ||
} | ||
|
||
return Type(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===--- SemaFixture.h - Helper for setting up Sema context -----*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 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 "swift/AST/ASTContext.h" | ||
#include "swift/AST/DiagnosticEngine.h" | ||
#include "swift/AST/Module.h" | ||
#include "swift/AST/SourceFile.h" | ||
#include "swift/AST/Type.h" | ||
#include "swift/Basic/LangOptions.h" | ||
#include "swift/Basic/Platform.h" | ||
#include "swift/Basic/SourceManager.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/Support/Host.h" | ||
#include "llvm/Support/Path.h" | ||
#include "gtest/gtest.h" | ||
#include <string> | ||
|
||
namespace swift { | ||
namespace unittest { | ||
|
||
class SemaTestBase : public ::testing::Test { | ||
public: | ||
LangOptions LangOpts; | ||
TypeCheckerOptions TypeCheckerOpts; | ||
SearchPathOptions SearchPathOpts; | ||
ClangImporterOptions ClangImporterOpts; | ||
SourceManager SourceMgr; | ||
DiagnosticEngine Diags; | ||
|
||
SemaTestBase() : Diags(SourceMgr) { | ||
LangOpts.Target = llvm::Triple(llvm::sys::getDefaultTargetTriple()); | ||
|
||
llvm::SmallString<128> libDir(SWIFTLIB_DIR); | ||
llvm::sys::path::append(libDir, getPlatformNameForTriple(LangOpts.Target)); | ||
|
||
SearchPathOpts.RuntimeResourcePath = SWIFTLIB_DIR; | ||
SearchPathOpts.RuntimeLibraryPaths.push_back(std::string(libDir.str())); | ||
SearchPathOpts.RuntimeLibraryImportPaths.push_back( | ||
std::string(libDir.str())); | ||
} | ||
}; | ||
|
||
/// Owns an ASTContext and the associated types. | ||
class SemaTest : public SemaTestBase { | ||
SourceFile *MainFile; | ||
|
||
public: | ||
ASTContext &Context; | ||
DeclContext *DC; | ||
|
||
SemaTest(); | ||
|
||
protected: | ||
Type getStdlibType(StringRef name) const; | ||
}; | ||
|
||
} // end namespace unittest | ||
} // end namespace swift |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.