Skip to content

[Frontend] Switch -interface-compiler-version to Version #77684

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
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
2 changes: 1 addition & 1 deletion include/swift/AST/FileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {

/// Returns the version of the Swift compiler used to create generate
/// .swiftinterface file if this file is produced from one.
virtual llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
virtual version::Version getSwiftInterfaceCompilerVersion() const {
return {};
}

Expand Down
6 changes: 3 additions & 3 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class ModuleDecl

/// Indicates a version of the Swift compiler used to generate
/// .swiftinterface file that this module was produced from (if any).
mutable llvm::VersionTuple InterfaceCompilerVersion;
mutable version::Version InterfaceCompilerVersion;

public:
/// Produces the components of a given module's full name in reverse order.
Expand Down Expand Up @@ -524,11 +524,11 @@ class ModuleDecl
}

/// See \c InterfaceCompilerVersion
llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
version::Version getSwiftInterfaceCompilerVersion() const {
return InterfaceCompilerVersion;
}

void setSwiftInterfaceCompilerVersion(llvm::VersionTuple version) {
void setSwiftInterfaceCompilerVersion(version::Version version) {
InterfaceCompilerVersion = version;
}

Expand Down
2 changes: 1 addition & 1 deletion include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class FrontendOptions {

/// The Swift compiler version number that would be used to synthesize
/// swiftinterface files and subsequently their swiftmodules.
llvm::VersionTuple SwiftInterfaceCompilerVersion;
version::Version SwiftInterfaceCompilerVersion;

/// A set of modules allowed to import this module.
std::set<std::string> AllowableClients;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ class SerializedASTFile final : public LoadedFile {

virtual StringRef getPublicModuleName() const override;

virtual llvm::VersionTuple getSwiftInterfaceCompilerVersion() const override;
virtual version::Version getSwiftInterfaceCompilerVersion() const override;

ValueDecl *getMainDecl() const override;

Expand Down
13 changes: 7 additions & 6 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#include "swift/AST/Identifier.h"
#include "swift/Basic/CXXStdlibKind.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceLoc.h"
#include "swift/Basic/Version.h"
#include "swift/Parse/ParseVersion.h"
#include "swift/Serialization/SerializationOptions.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -131,7 +133,7 @@ class ExtendedValidationInfo {
StringRef ExportAsName;
StringRef PublicModuleName;
CXXStdlibKind CXXStdlib;
llvm::VersionTuple SwiftInterfaceCompilerVersion;
version::Version SwiftInterfaceCompilerVersion;
struct {
unsigned ArePrivateImportsEnabled : 1;
unsigned IsSIB : 1;
Expand Down Expand Up @@ -252,14 +254,13 @@ class ExtendedValidationInfo {
CXXStdlibKind getCXXStdlibKind() const { return CXXStdlib; }
void setCXXStdlibKind(CXXStdlibKind kind) { CXXStdlib = kind; }

llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
version::Version getSwiftInterfaceCompilerVersion() const {
return SwiftInterfaceCompilerVersion;
}
void setSwiftInterfaceCompilerVersion(StringRef version) {
llvm::VersionTuple compilerVersion;
if (compilerVersion.tryParse(version))
return;
SwiftInterfaceCompilerVersion = compilerVersion;
if (auto genericVersion = VersionParser::parseVersionString(
version, SourceLoc(), /*Diags=*/nullptr))
SwiftInterfaceCompilerVersion = genericVersion.value();
}
};

Expand Down
9 changes: 5 additions & 4 deletions lib/Basic/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,11 @@ std::string getCompilerVersion() {
std::string buf;
llvm::raw_string_ostream OS(buf);

// TODO: This should print SWIFT_COMPILER_VERSION when
// available, but to do that we need to switch from
// llvm::VersionTuple to swift::Version.
OS << SWIFT_VERSION_STRING;
#if defined(SWIFT_COMPILER_VERSION)
OS << SWIFT_COMPILER_VERSION;
#else
OS << SWIFT_VERSION_STRING;
#endif

return OS.str();
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/Option/Options.h"
#include "swift/Option/SanitizerOptions.h"
#include "swift/Parse/Lexer.h"
#include "swift/Parse/ParseVersion.h"
#include "swift/Strings.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/TargetParser/Triple.h"
Expand Down Expand Up @@ -302,10 +303,15 @@ bool ArgsToFrontendOptionsConverter::convert(
Opts.PublicModuleName = A->getValue();

if (auto A = Args.getLastArg(OPT_swiftinterface_compiler_version)) {
if (Opts.SwiftInterfaceCompilerVersion.tryParse(A->getValue())) {
if (auto version = VersionParser::parseVersionString(
A->getValue(), SourceLoc(), /*Diags=*/nullptr)) {
Opts.SwiftInterfaceCompilerVersion = version.value();
}

if (Opts.SwiftInterfaceCompilerVersion.empty() ||
Opts.SwiftInterfaceCompilerVersion.size() > 5)
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
}
}

// This must be called after computing module name, module abi name,
Expand Down
6 changes: 4 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,8 +1493,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
if (Invocation.getSILOptions().EnableSerializePackage)
MainModule->setSerializePackageEnabled();

if (auto compilerVersion =
Invocation.getFrontendOptions().SwiftInterfaceCompilerVersion) {
if (!Invocation.getFrontendOptions()
.SwiftInterfaceCompilerVersion.empty()) {
auto compilerVersion =
Invocation.getFrontendOptions().SwiftInterfaceCompilerVersion;
MainModule->setSwiftInterfaceCompilerVersion(compilerVersion);
}

Expand Down
11 changes: 6 additions & 5 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "swift/AST/RequirementSignature.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Version.h"
#include "swift/Sema/ConstraintLocator.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/CSFix.h"
Expand Down Expand Up @@ -1300,11 +1301,11 @@ AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
// are built with 6.1+ compilers, libraries produced by earlier
// compilers don't have required symbols.
if (auto *module = member->getDeclContext()->getParentModule()) {
if (module->isBuiltFromInterface() &&
module->getSwiftInterfaceCompilerVersion() <
llvm::VersionTuple(6, 1)) {
return AllowInvalidRefInKeyPath::create(
cs, baseType, RefKind::UnsupportedStaticMember, member, locator);
if (module->isBuiltFromInterface()) {
auto compilerVersion = module->getSwiftInterfaceCompilerVersion();
if (!compilerVersion.isVersionAtLeast(6, 1))
return AllowInvalidRefInKeyPath::create(
cs, baseType, RefKind::UnsupportedStaticMember, member, locator);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,6 @@ StringRef SerializedASTFile::getPublicModuleName() const {
return File.getPublicModuleName();
}

llvm::VersionTuple SerializedASTFile::getSwiftInterfaceCompilerVersion() const {
version::Version SerializedASTFile::getSwiftInterfaceCompilerVersion() const {
return File.getSwiftInterfaceCompilerVersion();
}
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ class ModuleFile
return Core->UserModuleVersion;
}

llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
version::Version getSwiftInterfaceCompilerVersion() const {
return Core->SwiftInterfaceCompilerVersion;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFileSharedCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ModuleFileSharedCore {
/// The version of the Swift compiler used to produce swiftinterface
/// this module is based on. This is the most precise version possible
/// - a compiler tag or version if this is a development compiler.
llvm::VersionTuple SwiftInterfaceCompilerVersion;
version::Version SwiftInterfaceCompilerVersion;

/// \c true if this module has incremental dependency information.
bool HasIncrementalInfo = false;
Expand Down
13 changes: 9 additions & 4 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,11 +1142,16 @@ void Serializer::writeHeader() {
PublicModuleName.emit(ScratchRecord, publicModuleName.str());
}

llvm::VersionTuple compilerVersion =
M->getSwiftInterfaceCompilerVersion();
if (compilerVersion) {
version::Version compilerVersion = M->getSwiftInterfaceCompilerVersion();
if (!compilerVersion.empty()) {
options_block::SwiftInterfaceCompilerVersionLayout Version(Out);
Version.emit(ScratchRecord, compilerVersion.getAsString());

SmallString<32> versionBuf;
llvm::raw_svector_ostream OS(versionBuf);

OS << compilerVersion;

Version.emit(ScratchRecord, OS.str());
}

if (M->isConcurrencyChecked()) {
Expand Down
12 changes: 12 additions & 0 deletions test/ModuleInterface/swiftinterface-compiler-version-option.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// RUN: %empty-directory(%t)

// Test some invalid uses
// RUN: not %target-swift-frontend -typecheck %s -interface-compiler-version A 2>&1 | %FileCheck %s --check-prefix=INVALID
// RUN: not %target-swift-frontend -typecheck %s -interface-compiler-version 6.0.0.0.1.6 2>&1 | %FileCheck %s --check-prefix=INVALID
// RUN: not %target-swift-frontend -typecheck %s -interface-compiler-version 6.xx 2>&1 | %FileCheck %s --check-prefix=INVALID

// INVALID: <unknown>:0: error: invalid value '{{.*}}' in '-interface-compiler-version {{.*}}'

// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6.1
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6.1.0.0
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6.1.0.0.0

/// Build the libraries.
// RUN: %target-swift-frontend %s \
// RUN: -module-name Lib \
Expand Down