Skip to content

Commit ab4d8f6

Browse files
committed
[Serialization] Add -swift-compiler-version option to swiftmodules
1 parent 39fb638 commit ab4d8f6

12 files changed

+78
-5
lines changed

Diff for: include/swift/AST/FileUnit.h

+5
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
345345
return {};
346346
}
347347

348+
/// Returns the version of the Swift compiler used to create this module.
349+
virtual llvm::VersionTuple getSwiftCompilerVersion() const {
350+
return {};
351+
}
352+
348353
SWIFT_DEBUG_DUMPER(dumpDisplayDecls());
349354
SWIFT_DEBUG_DUMPER(dumpTopLevelDecls());
350355

Diff for: include/swift/Serialization/SerializedModuleLoader.h

+2
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ class SerializedASTFile final : public LoadedFile {
539539

540540
virtual StringRef getPublicModuleName() const override;
541541

542+
virtual llvm::VersionTuple getSwiftCompilerVersion() const override;
543+
542544
ValueDecl *getMainDecl() const override;
543545

544546
bool hasEntryPoint() const override;

Diff for: include/swift/Serialization/Validation.h

+11
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ExtendedValidationInfo {
131131
StringRef ExportAsName;
132132
StringRef PublicModuleName;
133133
CXXStdlibKind CXXStdlib;
134+
llvm::VersionTuple SwiftCompilerVersion;
134135
struct {
135136
unsigned ArePrivateImportsEnabled : 1;
136137
unsigned IsSIB : 1;
@@ -250,6 +251,16 @@ class ExtendedValidationInfo {
250251

251252
CXXStdlibKind getCXXStdlibKind() const { return CXXStdlib; }
252253
void setCXXStdlibKind(CXXStdlibKind kind) { CXXStdlib = kind; }
254+
255+
llvm::VersionTuple getSwiftCompilerVersion() const {
256+
return SwiftCompilerVersion;
257+
}
258+
void setSwiftCompilerVersion(StringRef version) {
259+
llvm::VersionTuple compilerVersion;
260+
if (compilerVersion.tryParse(version))
261+
return;
262+
SwiftCompilerVersion = compilerVersion;
263+
}
253264
};
254265

255266
struct SearchPath {

Diff for: lib/Serialization/ModuleFile.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1409,3 +1409,7 @@ StringRef SerializedASTFile::getExportedModuleName() const {
14091409
StringRef SerializedASTFile::getPublicModuleName() const {
14101410
return File.getPublicModuleName();
14111411
}
1412+
1413+
llvm::VersionTuple SerializedASTFile::getSwiftCompilerVersion() const {
1414+
return File.getSwiftCompilerVersion();
1415+
}

Diff for: lib/Serialization/ModuleFile.h

+4
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,10 @@ class ModuleFile
603603
return Core->UserModuleVersion;
604604
}
605605

606+
llvm::VersionTuple getSwiftCompilerVersion() const {
607+
return Core->SwiftCompilerVersion;
608+
}
609+
606610
ArrayRef<StringRef> getAllowableClientNames() const {
607611
return Core->AllowableClientNames;
608612
}

Diff for: lib/Serialization/ModuleFileSharedCore.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
214214
case options_block::PUBLIC_MODULE_NAME:
215215
extendedInfo.setPublicModuleName(blobData);
216216
break;
217+
case options_block::SWIFT_COMPILER_VERSION:
218+
extendedInfo.setSwiftCompilerVersion(blobData);
219+
break;
217220
default:
218221
// Unknown options record, possibly for use by a future version of the
219222
// module format.
@@ -1496,6 +1499,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
14961499
ModulePackageName = extInfo.getModulePackageName();
14971500
ModuleExportAsName = extInfo.getExportAsName();
14981501
PublicModuleName = extInfo.getPublicModuleName();
1502+
SwiftCompilerVersion = extInfo.getSwiftCompilerVersion();
14991503

15001504
hasValidControlBlock = true;
15011505
break;

Diff for: lib/Serialization/ModuleFileSharedCore.h

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ class ModuleFileSharedCore {
103103
/// Name to use in public facing diagnostics and documentation.
104104
StringRef PublicModuleName;
105105

106+
/// The version of the Swift compiler used to produce swiftinterface
107+
/// this module is based on or build the module itself. This is
108+
/// the most precise version possible - a compiler tag or version
109+
/// if this is a development compiler.
110+
llvm::VersionTuple SwiftCompilerVersion;
111+
106112
/// \c true if this module has incremental dependency information.
107113
bool HasIncrementalInfo = false;
108114

Diff for: lib/Serialization/ModuleFormat.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 897; // Builtin.FixedArray
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 898; // swift-compiler-version
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -966,6 +966,7 @@ namespace options_block {
966966
SERIALIZE_PACKAGE_ENABLED,
967967
CXX_STDLIB_KIND,
968968
PUBLIC_MODULE_NAME,
969+
SWIFT_COMPILER_VERSION,
969970
};
970971

971972
using SDKPathLayout = BCRecordLayout<
@@ -1066,6 +1067,11 @@ namespace options_block {
10661067
PUBLIC_MODULE_NAME,
10671068
BCBlob
10681069
>;
1070+
1071+
using SwiftCompilerVersionLayout = BCRecordLayout<
1072+
SWIFT_COMPILER_VERSION,
1073+
BCBlob // version tuple
1074+
>;
10691075
}
10701076

10711077
/// The record types within the input block.

Diff for: lib/Serialization/Serialization.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ void Serializer::writeBlockInfoBlock() {
864864
BLOCK_RECORD(options_block, SERIALIZE_PACKAGE_ENABLED);
865865
BLOCK_RECORD(options_block, CXX_STDLIB_KIND);
866866
BLOCK_RECORD(options_block, PUBLIC_MODULE_NAME);
867+
BLOCK_RECORD(options_block, SWIFT_COMPILER_VERSION);
867868

868869
BLOCK(INPUT_BLOCK);
869870
BLOCK_RECORD(input_block, IMPORTED_MODULE);
@@ -1139,6 +1140,10 @@ void Serializer::writeHeader() {
11391140
PublicModuleName.emit(ScratchRecord, publicModuleName.str());
11401141
}
11411142

1143+
llvm::VersionTuple compilerVersion = M->getSwiftCompilerVersion();
1144+
options_block::SwiftCompilerVersionLayout SwiftCompilerVersion(Out);
1145+
SwiftCompilerVersion.emit(ScratchRecord, compilerVersion.getAsString());
1146+
11421147
if (M->isConcurrencyChecked()) {
11431148
options_block::IsConcurrencyCheckedLayout IsConcurrencyChecked(Out);
11441149
IsConcurrencyChecked.emit(ScratchRecord);

Diff for: lib/Serialization/SerializedModuleLoader.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
10351035
M.setPackageName(Ctx.getIdentifier(loadedModuleFile->getModulePackageName()));
10361036
}
10371037
M.setUserModuleVersion(loadedModuleFile->getUserModuleVersion());
1038+
M.setSwiftCompilerVersion(loadedModuleFile->getSwiftCompilerVersion());
10381039
for (auto name: loadedModuleFile->getAllowableClientNames()) {
10391040
M.addAllowableClientName(Ctx.getIdentifier(name));
10401041
}

Diff for: test/CAS/embedded-Xcc.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
// RUN: llvm-bcanalyzer --dump %t/Test.swiftmodule | %FileCheck %s
2222

23-
// CHECK: <XCC abbrevid=6/> blob data = '-cc1'
24-
// CHECK: <XCC abbrevid=6/> blob data = '-D'
25-
// CHECK: <XCC abbrevid=6/> blob data = 'TEST=1'
26-
// CHECK-NOT: <XCC abbrevid=6/> blob data = '--target=
23+
// CHECK: <XCC abbrevid=7/> blob data = '-cc1'
24+
// CHECK: <XCC abbrevid=7/> blob data = '-D'
25+
// CHECK: <XCC abbrevid=7/> blob data = 'TEST=1'
26+
// CHECK-NOT: <XCC abbrevid=7/> blob data = '--target=
2727

2828
//--- main.swift
2929
public func test() {}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %empty-directory(%t)
2+
3+
/// Build the libraries.
4+
// RUN: %target-swift-frontend %s \
5+
// RUN: -emit-module-path %t/Lib.swiftmodule \
6+
// RUN: -emit-module-interface-path %t/Lib.swiftinterface \
7+
// RUN: -enable-library-evolution -swift-version 6 \
8+
// RUN: -swift-compiler-version 6.0.0.1
9+
10+
// RUN: %target-swift-typecheck-module-from-interface(%t/Lib.swiftinterface)
11+
12+
/// Check option in swiftinterface
13+
// RUN: cat %t/Lib.swiftinterface | %FileCheck --check-prefix=CHECK-OPTION %s
14+
// CHECK-OPTION: swift-module-flags:
15+
// CHECK-SAME-OPTION: -swift-compiler-version 6.0.0.1
16+
17+
/// Check option in swiftmodule
18+
// RUN: llvm-bcanalyzer --dump %t/Lib.swiftmodule | %FileCheck --check-prefix=CHECK-MODULE-OPTION %s
19+
// CHECK-MODULE-OPTION: <OPTIONS_BLOCK
20+
// CHECK-MODULE-OPTION: <SWIFT_COMPILER_VERSION abbrevid={{.*}}/> blob data = '6.0.0.1'
21+
// CHECK-MODULE-OPTION: </OPTIONS_BLOCK>
22+
23+
public struct S {
24+
public var test: Int = 42
25+
}

0 commit comments

Comments
 (0)