Skip to content

Commit 6cb4b59

Browse files
authored
Merge pull request #77216 from xedin/add-swift-compiler-version
[Frontend/AST] Add `-interface-compiler-version` option to frontend/modules
2 parents 1499229 + 84a62fc commit 6cb4b59

20 files changed

+151
-8
lines changed

include/swift/AST/FileUnit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
345345
return {};
346346
}
347347

348+
/// Returns the version of the Swift compiler used to create generate
349+
/// .swiftinterface file if this file is produced from one.
350+
virtual llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
351+
return {};
352+
}
353+
348354
SWIFT_DEBUG_DUMPER(dumpDisplayDecls());
349355
SWIFT_DEBUG_DUMPER(dumpTopLevelDecls());
350356

include/swift/AST/Module.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ class ModuleDecl
254254

255255
mutable Identifier PublicModuleName;
256256

257+
/// Indicates a version of the Swift compiler used to generate
258+
/// .swiftinterface file that this module was produced from (if any).
259+
mutable llvm::VersionTuple InterfaceCompilerVersion;
260+
257261
public:
258262
/// Produces the components of a given module's full name in reverse order.
259263
///
@@ -518,11 +522,20 @@ class ModuleDecl
518522
PublicModuleName = name;
519523
}
520524

525+
/// See \c InterfaceCompilerVersion
526+
llvm::VersionTuple getSwiftInterfaceCompilerVersion() const {
527+
return InterfaceCompilerVersion;
528+
}
529+
530+
void setSwiftInterfaceCompilerVersion(llvm::VersionTuple version) {
531+
InterfaceCompilerVersion = version;
532+
}
533+
521534
/// Retrieve the actual module name of an alias used for this module (if any).
522535
///
523-
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,
524-
/// and this module is (a) not the main module and (b) is named Foo, then it returns
525-
/// the real (physically on-disk) module name Bar.
536+
/// For example, if '-module-alias Foo=Bar' is passed in when building the
537+
/// main module, and this module is (a) not the main module and (b) is named
538+
/// Foo, then it returns the real (physically on-disk) module name Bar.
526539
///
527540
/// If no module aliasing is set, it will return getName(), i.e. Foo.
528541
Identifier getRealName() const;

include/swift/Basic/Version.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ StringRef getCurrentCompilerChannel();
187187
/// users.
188188
unsigned getUpcomingCxxInteropCompatVersion();
189189

190+
/// Retrieves the version of the running compiler. It could be a tag or
191+
/// a "development" version that only has major/minor.
192+
std::string getCompilerVersion();
193+
190194
} // end namespace version
191195
} // end namespace swift
192196

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ class FrontendOptions {
110110
/// User-defined module version number.
111111
llvm::VersionTuple UserModuleVersion;
112112

113+
/// The Swift compiler version number that would be used to synthesize
114+
/// swiftinterface files and subsequently their swiftmodules.
115+
llvm::VersionTuple SwiftInterfaceCompilerVersion;
116+
113117
/// A set of modules allowed to import this module.
114118
std::set<std::string> AllowableClients;
115119

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ def package_description_version: Separate<["-"], "package-description-version">,
303303
HelpText<"The version number to be applied on the input for the PackageDescription availability kind">,
304304
MetaVarName<"<vers>">;
305305

306+
def swiftinterface_compiler_version : Separate<["-"], "interface-compiler-version">,
307+
Flags<[FrontendOption, HelpHidden]>,
308+
HelpText<"The version of the Swift compiler used to generate a .swiftinterface file">,
309+
MetaVarName<"<intcvers>">;
310+
306311
def tools_directory : Separate<["-"], "tools-directory">,
307312
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
308313
ArgumentIsPath]>,

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 2 additions & 0 deletions
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 getSwiftInterfaceCompilerVersion() const override;
543+
542544
ValueDecl *getMainDecl() const override;
543545

544546
bool hasEntryPoint() const override;

include/swift/Serialization/Validation.h

Lines changed: 11 additions & 0 deletions
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 SwiftInterfaceCompilerVersion;
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 getSwiftInterfaceCompilerVersion() const {
256+
return SwiftInterfaceCompilerVersion;
257+
}
258+
void setSwiftInterfaceCompilerVersion(StringRef version) {
259+
llvm::VersionTuple compilerVersion;
260+
if (compilerVersion.tryParse(version))
261+
return;
262+
SwiftInterfaceCompilerVersion = compilerVersion;
263+
}
253264
};
254265

255266
struct SearchPath {

lib/Basic/Version.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,5 +339,18 @@ unsigned getUpcomingCxxInteropCompatVersion() {
339339
return SWIFT_VERSION_MAJOR + 1;
340340
}
341341

342+
std::string getCompilerVersion() {
343+
std::string buf;
344+
llvm::raw_string_ostream OS(buf);
345+
346+
#if defined(SWIFT_COMPILER_VERSION)
347+
OS << SWIFT_COMPILER_VERSION;
348+
#else
349+
OS << SWIFT_VERSION_STRING;
350+
#endif
351+
352+
return OS.str();
353+
}
354+
342355
} // end namespace version
343356
} // end namespace swift

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ bool ArgsToFrontendOptionsConverter::convert(
299299
if (const Arg *A = Args.getLastArg(OPT_public_module_name))
300300
Opts.PublicModuleName = A->getValue();
301301

302+
if (auto A = Args.getLastArg(OPT_swiftinterface_compiler_version)) {
303+
if (Opts.SwiftInterfaceCompilerVersion.tryParse(A->getValue())) {
304+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
305+
A->getAsString(Args), A->getValue());
306+
}
307+
}
308+
302309
// This must be called after computing module name, module abi name,
303310
// and module link name. If computing module aliases is unsuccessful,
304311
// return early.

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,11 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14911491
if (Invocation.getSILOptions().EnableSerializePackage)
14921492
MainModule->setSerializePackageEnabled();
14931493

1494+
if (auto compilerVersion =
1495+
Invocation.getFrontendOptions().SwiftInterfaceCompilerVersion) {
1496+
MainModule->setSwiftInterfaceCompilerVersion(compilerVersion);
1497+
}
1498+
14941499
// Register the main module with the AST context.
14951500
Context->addLoadedModule(MainModule);
14961501
Context->MainModule = MainModule;

0 commit comments

Comments
 (0)