Skip to content

Commit 4a9d20d

Browse files
authored
Merge pull request #77684 from xedin/switch-swiftinterface-version-to-use-Version
[Frontend] Switch `-interface-compiler-version` to `Version`
2 parents 67acddd + a44f42e commit 4a9d20d

File tree

14 files changed

+60
-32
lines changed

14 files changed

+60
-32
lines changed

include/swift/AST/FileUnit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
347347

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

include/swift/AST/Module.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class ModuleDecl
257257

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

262262
public:
263263
/// Produces the components of a given module's full name in reverse order.
@@ -524,11 +524,11 @@ class ModuleDecl
524524
}
525525

526526
/// See \c InterfaceCompilerVersion
527-
llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
527+
version::Version getSwiftInterfaceCompilerVersion() const {
528528
return InterfaceCompilerVersion;
529529
}
530530

531-
void setSwiftInterfaceCompilerVersion(llvm::VersionTuple version) {
531+
void setSwiftInterfaceCompilerVersion(version::Version version) {
532532
InterfaceCompilerVersion = version;
533533
}
534534

include/swift/Frontend/FrontendOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class FrontendOptions {
112112

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

117117
/// A set of modules allowed to import this module.
118118
std::set<std::string> AllowableClients;

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ class SerializedASTFile final : public LoadedFile {
539539

540540
virtual StringRef getPublicModuleName() const override;
541541

542-
virtual llvm::VersionTuple getSwiftInterfaceCompilerVersion() const override;
542+
virtual version::Version getSwiftInterfaceCompilerVersion() const override;
543543

544544
ValueDecl *getMainDecl() const override;
545545

include/swift/Serialization/Validation.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include "swift/AST/Identifier.h"
1717
#include "swift/Basic/CXXStdlibKind.h"
1818
#include "swift/Basic/LLVM.h"
19+
#include "swift/Basic/SourceLoc.h"
1920
#include "swift/Basic/Version.h"
21+
#include "swift/Parse/ParseVersion.h"
2022
#include "swift/Serialization/SerializationOptions.h"
2123
#include "llvm/ADT/ArrayRef.h"
2224
#include "llvm/ADT/SmallVector.h"
@@ -131,7 +133,7 @@ class ExtendedValidationInfo {
131133
StringRef ExportAsName;
132134
StringRef PublicModuleName;
133135
CXXStdlibKind CXXStdlib;
134-
llvm::VersionTuple SwiftInterfaceCompilerVersion;
136+
version::Version SwiftInterfaceCompilerVersion;
135137
struct {
136138
unsigned ArePrivateImportsEnabled : 1;
137139
unsigned IsSIB : 1;
@@ -252,14 +254,13 @@ class ExtendedValidationInfo {
252254
CXXStdlibKind getCXXStdlibKind() const { return CXXStdlib; }
253255
void setCXXStdlibKind(CXXStdlibKind kind) { CXXStdlib = kind; }
254256

255-
llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
257+
version::Version getSwiftInterfaceCompilerVersion() const {
256258
return SwiftInterfaceCompilerVersion;
257259
}
258260
void setSwiftInterfaceCompilerVersion(StringRef version) {
259-
llvm::VersionTuple compilerVersion;
260-
if (compilerVersion.tryParse(version))
261-
return;
262-
SwiftInterfaceCompilerVersion = compilerVersion;
261+
if (auto genericVersion = VersionParser::parseVersionString(
262+
version, SourceLoc(), /*Diags=*/nullptr))
263+
SwiftInterfaceCompilerVersion = genericVersion.value();
263264
}
264265
};
265266

lib/Basic/Version.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,11 @@ std::string getCompilerVersion() {
343343
std::string buf;
344344
llvm::raw_string_ostream OS(buf);
345345

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

351352
return OS.str();
352353
}

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Option/Options.h"
2323
#include "swift/Option/SanitizerOptions.h"
2424
#include "swift/Parse/Lexer.h"
25+
#include "swift/Parse/ParseVersion.h"
2526
#include "swift/Strings.h"
2627
#include "llvm/ADT/STLExtras.h"
2728
#include "llvm/TargetParser/Triple.h"
@@ -302,10 +303,15 @@ bool ArgsToFrontendOptionsConverter::convert(
302303
Opts.PublicModuleName = A->getValue();
303304

304305
if (auto A = Args.getLastArg(OPT_swiftinterface_compiler_version)) {
305-
if (Opts.SwiftInterfaceCompilerVersion.tryParse(A->getValue())) {
306+
if (auto version = VersionParser::parseVersionString(
307+
A->getValue(), SourceLoc(), /*Diags=*/nullptr)) {
308+
Opts.SwiftInterfaceCompilerVersion = version.value();
309+
}
310+
311+
if (Opts.SwiftInterfaceCompilerVersion.empty() ||
312+
Opts.SwiftInterfaceCompilerVersion.size() > 5)
306313
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
307314
A->getAsString(Args), A->getValue());
308-
}
309315
}
310316

311317
// This must be called after computing module name, module abi name,

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14931493
if (Invocation.getSILOptions().EnableSerializePackage)
14941494
MainModule->setSerializePackageEnabled();
14951495

1496-
if (auto compilerVersion =
1497-
Invocation.getFrontendOptions().SwiftInterfaceCompilerVersion) {
1496+
if (!Invocation.getFrontendOptions()
1497+
.SwiftInterfaceCompilerVersion.empty()) {
1498+
auto compilerVersion =
1499+
Invocation.getFrontendOptions().SwiftInterfaceCompilerVersion;
14981500
MainModule->setSwiftInterfaceCompilerVersion(compilerVersion);
14991501
}
15001502

lib/Sema/CSFix.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/RequirementSignature.h"
2828
#include "swift/Basic/Assertions.h"
2929
#include "swift/Basic/SourceManager.h"
30+
#include "swift/Basic/Version.h"
3031
#include "swift/Sema/ConstraintLocator.h"
3132
#include "swift/Sema/ConstraintSystem.h"
3233
#include "swift/Sema/CSFix.h"
@@ -1300,11 +1301,11 @@ AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
13001301
// are built with 6.1+ compilers, libraries produced by earlier
13011302
// compilers don't have required symbols.
13021303
if (auto *module = member->getDeclContext()->getParentModule()) {
1303-
if (module->isBuiltFromInterface() &&
1304-
module->getSwiftInterfaceCompilerVersion() <
1305-
llvm::VersionTuple(6, 1)) {
1306-
return AllowInvalidRefInKeyPath::create(
1307-
cs, baseType, RefKind::UnsupportedStaticMember, member, locator);
1304+
if (module->isBuiltFromInterface()) {
1305+
auto compilerVersion = module->getSwiftInterfaceCompilerVersion();
1306+
if (!compilerVersion.isVersionAtLeast(6, 1))
1307+
return AllowInvalidRefInKeyPath::create(
1308+
cs, baseType, RefKind::UnsupportedStaticMember, member, locator);
13081309
}
13091310
}
13101311

lib/Serialization/ModuleFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,6 @@ StringRef SerializedASTFile::getPublicModuleName() const {
14171417
return File.getPublicModuleName();
14181418
}
14191419

1420-
llvm::VersionTuple SerializedASTFile::getSwiftInterfaceCompilerVersion() const {
1420+
version::Version SerializedASTFile::getSwiftInterfaceCompilerVersion() const {
14211421
return File.getSwiftInterfaceCompilerVersion();
14221422
}

lib/Serialization/ModuleFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class ModuleFile
603603
return Core->UserModuleVersion;
604604
}
605605

606-
llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
606+
version::Version getSwiftInterfaceCompilerVersion() const {
607607
return Core->SwiftInterfaceCompilerVersion;
608608
}
609609

lib/Serialization/ModuleFileSharedCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ModuleFileSharedCore {
106106
/// The version of the Swift compiler used to produce swiftinterface
107107
/// this module is based on. This is the most precise version possible
108108
/// - a compiler tag or version if this is a development compiler.
109-
llvm::VersionTuple SwiftInterfaceCompilerVersion;
109+
version::Version SwiftInterfaceCompilerVersion;
110110

111111
/// \c true if this module has incremental dependency information.
112112
bool HasIncrementalInfo = false;

lib/Serialization/Serialization.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,11 +1142,16 @@ void Serializer::writeHeader() {
11421142
PublicModuleName.emit(ScratchRecord, publicModuleName.str());
11431143
}
11441144

1145-
llvm::VersionTuple compilerVersion =
1146-
M->getSwiftInterfaceCompilerVersion();
1147-
if (compilerVersion) {
1145+
version::Version compilerVersion = M->getSwiftInterfaceCompilerVersion();
1146+
if (!compilerVersion.empty()) {
11481147
options_block::SwiftInterfaceCompilerVersionLayout Version(Out);
1149-
Version.emit(ScratchRecord, compilerVersion.getAsString());
1148+
1149+
SmallString<32> versionBuf;
1150+
llvm::raw_svector_ostream OS(versionBuf);
1151+
1152+
OS << compilerVersion;
1153+
1154+
Version.emit(ScratchRecord, OS.str());
11501155
}
11511156

11521157
if (M->isConcurrencyChecked()) {

test/ModuleInterface/swiftinterface-compiler-version-option.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// RUN: %empty-directory(%t)
22

3+
// Test some invalid uses
4+
// RUN: not %target-swift-frontend -typecheck %s -interface-compiler-version A 2>&1 | %FileCheck %s --check-prefix=INVALID
5+
// RUN: not %target-swift-frontend -typecheck %s -interface-compiler-version 6.0.0.0.1.6 2>&1 | %FileCheck %s --check-prefix=INVALID
6+
// RUN: not %target-swift-frontend -typecheck %s -interface-compiler-version 6.xx 2>&1 | %FileCheck %s --check-prefix=INVALID
7+
8+
// INVALID: <unknown>:0: error: invalid value '{{.*}}' in '-interface-compiler-version {{.*}}'
9+
10+
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6
11+
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6.1
12+
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6.1.0.0
13+
// RUN: %target-typecheck-verify-swift %s -interface-compiler-version 6.1.0.0.0
14+
315
/// Build the libraries.
416
// RUN: %target-swift-frontend %s \
517
// RUN: -module-name Lib \

0 commit comments

Comments
 (0)