[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.
This commit is contained in:
Pavel Yaskevich
2024-01-22 15:32:00 -08:00
parent b98d7a5679
commit 182ba1bb2d
7 changed files with 34 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
#include "swift/AST/RawComment.h"
#include "swift/Basic/BasicSourceInfo.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/Version.h"
#include "llvm/ADT/PointerIntPair.h"
@@ -417,6 +418,10 @@ protected:
assert(classof(this) && "invalid kind");
}
public:
/// Returns the language version that was used to compile the contents of this
/// file. An empty `Version` is returned if the information is not available.
virtual version::Version getLanguageVersionBuiltWith() const = 0;
/// Returns an arbitrary string representing the storage backing this file.
///
/// This is usually a filesystem path.

View File

@@ -1147,6 +1147,10 @@ public:
SourceRange getSourceRange() const { return SourceRange(); }
/// Returns the language version that was used to compile this module.
/// An empty `Version` is returned if the information is not available.
version::Version getLanguageVersionBuiltWith() const;
static bool classof(const DeclContext *DC) {
if (auto D = DC->getAsDecl())
return classof(D);

View File

@@ -17,6 +17,7 @@
#define SWIFT_CLANGIMPORTER_CLANGMODULE_H
#include "swift/AST/FileUnit.h"
#include "swift/Basic/Version.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "clang/AST/ExternalASTSource.h"
#include "clang/Basic/Module.h"
@@ -109,6 +110,10 @@ public:
llvm_unreachable("no private decls in Clang modules");
}
virtual version::Version getLanguageVersionBuiltWith() const override {
return version::Version();
}
virtual StringRef getFilename() const override;
virtual StringRef getLoadedFilename() const override;

View File

@@ -400,7 +400,7 @@ public:
/// Returns the language version that was used to compile the contents of this
/// file.
const version::Version &getLanguageVersionBuiltWith() const;
virtual version::Version getLanguageVersionBuiltWith() const override;
virtual bool hadLoadError() const override;

View File

@@ -3935,3 +3935,17 @@ bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) con
return (!runtimePath.empty() && pathStartsWith(runtimePath, modulePath)) ||
(!sdkPath.empty() && pathStartsWith(sdkPath, modulePath));
}
version::Version ModuleDecl::getLanguageVersionBuiltWith() const {
for (auto *F : getFiles()) {
auto *LD = dyn_cast<LoadedFile>(F);
if (!LD)
continue;
auto version = LD->getLanguageVersionBuiltWith();
if (!version.empty())
return version;
}
return version::Version();
}

View File

@@ -82,6 +82,10 @@ public:
llvm_unreachable("no private decls in Clang modules");
}
virtual version::Version getLanguageVersionBuiltWith() const override {
return version::Version();
}
virtual StringRef getFilename() const override { return ""; }
virtual const clang::Module *getUnderlyingClangModule() const override {

View File

@@ -1393,7 +1393,7 @@ ValueDecl *SerializedASTFile::getMainDecl() const {
return cast_or_null<ValueDecl>(File.getDecl(File.getEntryPointDeclID()));
}
const version::Version &SerializedASTFile::getLanguageVersionBuiltWith() const {
version::Version SerializedASTFile::getLanguageVersionBuiltWith() const {
return File.getCompatibilityVersion();
}