Skip to content

Commit 182ba1b

Browse files
committed
[AST] Make it possible to find what Swift version was used to build a module
For imported modules the version is empty because they don't carry this information.
1 parent b98d7a5 commit 182ba1b

File tree

7 files changed

+34
-2
lines changed

7 files changed

+34
-2
lines changed

include/swift/AST/FileUnit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/RawComment.h"
1818
#include "swift/Basic/BasicSourceInfo.h"
1919
#include "swift/Basic/Debug.h"
20+
#include "swift/Basic/Version.h"
2021

2122
#include "llvm/ADT/PointerIntPair.h"
2223

@@ -417,6 +418,10 @@ class LoadedFile : public FileUnit {
417418
assert(classof(this) && "invalid kind");
418419
}
419420
public:
421+
/// Returns the language version that was used to compile the contents of this
422+
/// file. An empty `Version` is returned if the information is not available.
423+
virtual version::Version getLanguageVersionBuiltWith() const = 0;
424+
420425
/// Returns an arbitrary string representing the storage backing this file.
421426
///
422427
/// This is usually a filesystem path.

include/swift/AST/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,10 @@ class ModuleDecl
11471147

11481148
SourceRange getSourceRange() const { return SourceRange(); }
11491149

1150+
/// Returns the language version that was used to compile this module.
1151+
/// An empty `Version` is returned if the information is not available.
1152+
version::Version getLanguageVersionBuiltWith() const;
1153+
11501154
static bool classof(const DeclContext *DC) {
11511155
if (auto D = DC->getAsDecl())
11521156
return classof(D);

include/swift/ClangImporter/ClangModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define SWIFT_CLANGIMPORTER_CLANGMODULE_H
1818

1919
#include "swift/AST/FileUnit.h"
20+
#include "swift/Basic/Version.h"
2021
#include "swift/ClangImporter/ClangImporter.h"
2122
#include "clang/AST/ExternalASTSource.h"
2223
#include "clang/Basic/Module.h"
@@ -109,6 +110,10 @@ class ClangModuleUnit final : public LoadedFile {
109110
llvm_unreachable("no private decls in Clang modules");
110111
}
111112

113+
virtual version::Version getLanguageVersionBuiltWith() const override {
114+
return version::Version();
115+
}
116+
112117
virtual StringRef getFilename() const override;
113118

114119
virtual StringRef getLoadedFilename() const override;

include/swift/Serialization/SerializedModuleLoader.h

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

401401
/// Returns the language version that was used to compile the contents of this
402402
/// file.
403-
const version::Version &getLanguageVersionBuiltWith() const;
403+
virtual version::Version getLanguageVersionBuiltWith() const override;
404404

405405
virtual bool hadLoadError() const override;
406406

lib/AST/Module.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,3 +3935,17 @@ bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) con
39353935
return (!runtimePath.empty() && pathStartsWith(runtimePath, modulePath)) ||
39363936
(!sdkPath.empty() && pathStartsWith(sdkPath, modulePath));
39373937
}
3938+
3939+
version::Version ModuleDecl::getLanguageVersionBuiltWith() const {
3940+
for (auto *F : getFiles()) {
3941+
auto *LD = dyn_cast<LoadedFile>(F);
3942+
if (!LD)
3943+
continue;
3944+
3945+
auto version = LD->getLanguageVersionBuiltWith();
3946+
if (!version.empty())
3947+
return version;
3948+
}
3949+
3950+
return version::Version();
3951+
}

lib/ClangImporter/DWARFImporter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class DWARFModuleUnit final : public LoadedFile {
8282
llvm_unreachable("no private decls in Clang modules");
8383
}
8484

85+
virtual version::Version getLanguageVersionBuiltWith() const override {
86+
return version::Version();
87+
}
88+
8589
virtual StringRef getFilename() const override { return ""; }
8690

8791
virtual const clang::Module *getUnderlyingClangModule() const override {

lib/Serialization/ModuleFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ ValueDecl *SerializedASTFile::getMainDecl() const {
13931393
return cast_or_null<ValueDecl>(File.getDecl(File.getEntryPointDeclID()));
13941394
}
13951395

1396-
const version::Version &SerializedASTFile::getLanguageVersionBuiltWith() const {
1396+
version::Version SerializedASTFile::getLanguageVersionBuiltWith() const {
13971397
return File.getCompatibilityVersion();
13981398
}
13991399

0 commit comments

Comments
 (0)