Skip to content

Commit 7c968c6

Browse files
committed
AST: support availability on Windows
Enable Windows specific availability annotations in Swift.
1 parent cdfff19 commit 7c968c6

File tree

9 files changed

+53
-0
lines changed

9 files changed

+53
-0
lines changed

include/swift/AST/PlatformKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ AVAILABILITY_PLATFORM(macOSApplicationExtension, "application extensions for mac
3333
AVAILABILITY_PLATFORM(macCatalyst, "Mac Catalyst")
3434
AVAILABILITY_PLATFORM(macCatalystApplicationExtension, "application extensions for Mac Catalyst")
3535
AVAILABILITY_PLATFORM(OpenBSD, "OpenBSD")
36+
AVAILABILITY_PLATFORM(Windows, "Windows")
3637

3738
#undef AVAILABILITY_PLATFORM

lib/AST/PlatformKind.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
8989
return Target.isWatchOS();
9090
case PlatformKind::OpenBSD:
9191
return Target.isOSOpenBSD();
92+
case PlatformKind::Windows:
93+
return Target.isOSWindows();
9294
case PlatformKind::none:
9395
llvm_unreachable("handled above");
9496
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,10 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
19341934
deprecatedAsUnavailableMessage = "";
19351935
break;
19361936

1937+
case PlatformKind::Windows:
1938+
deprecatedAsUnavailableMessage = "";
1939+
break;
1940+
19371941
case PlatformKind::none:
19381942
break;
19391943
}
@@ -1969,6 +1973,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
19691973
case PlatformKind::OpenBSD:
19701974
return name == "openbsd";
19711975

1976+
case PlatformKind::Windows:
1977+
return name == "windows";
1978+
19721979
case PlatformKind::none:
19731980
return false;
19741981
}
@@ -2012,6 +2019,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
20122019
case PlatformKind::OpenBSD:
20132020
// No deprecation filter on OpenBSD
20142021
return false;
2022+
2023+
case PlatformKind::Windows:
2024+
// No deprecation filter on Windows
2025+
return false;
20152026
}
20162027

20172028
llvm_unreachable("Unexpected platform");

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,9 @@ class DeclAndTypePrinter::Implementation
881881
case PlatformKind::OpenBSD:
882882
plat = "openbsd";
883883
break;
884+
case PlatformKind::Windows:
885+
plat = "windows";
886+
break;
884887
case PlatformKind::none:
885888
llvm_unreachable("handled above");
886889
}

lib/SymbolGraphGen/AvailabilityMixin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ StringRef getDomain(const AvailableAttr &AvAttr) {
5858
return { "watchOSAppExtension" };
5959
case swift::PlatformKind::OpenBSD:
6060
return { "OpenBSD" };
61+
case swift::PlatformKind::Windows:
62+
return { "Windows" };
6163
case swift::PlatformKind::none:
6264
return { "*" };
6365
}

lib/TBDGen/TBDGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver) {
251251
llvm_unreachable("cannot find platform kind");
252252
case swift::PlatformKind::OpenBSD:
253253
llvm_unreachable("not used for this platform");
254+
case swift::PlatformKind::Windows:
255+
llvm_unreachable("not used for this platform");
254256
case swift::PlatformKind::iOS:
255257
case swift::PlatformKind::iOSApplicationExtension:
256258
return Ver.IsSimulator ? LinkerPlatformId::iOS_sim:

test/IDE/complete_decl_attribute.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct MyStruct {}
4040
// AVAILABILITY1-NEXT: Keyword/None: macCatalyst[#Platform#]; name=macCatalyst
4141
// AVAILABILITY1-NEXT: Keyword/None: macCatalystApplicationExtension[#Platform#]; name=macCatalystApplicationExtension
4242
// AVAILABILITY1-NEXT: Keyword/None: OpenBSD[#Platform#]; name=OpenBSD{{$}}
43+
// AVAILABILITY1-NEXT: Keyword/None: Windows[#Platform#]; name=Windows{{$}}
4344
// AVAILABILITY1-NEXT: End completions
4445

4546
@available(*, #^AVAILABILITY2^#)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %target-typecheck-verify-swift
2+
// REQUIRES: OS=windows-msvc
3+
4+
// expected-note@+2{{'unavailable()' has been explicitly marked unavailable here}}
5+
@available(Windows, unavailable, message: "unsupported")
6+
func unavailable() {}
7+
8+
// expected-error@+1 {{'unavailable()' is unavailable in Windows: unsupported}}
9+
unavailable()
10+
11+
@available(Windows, introduced: 10.0.17763, deprecated: 10.0.19140)
12+
func introduced_deprecated() {}
13+
14+
// expected-error@+1 {{'introduced_deprecated()' is only available in * 10.0.17763 or newe}}
15+
introduced_deprecated()
16+
// expected-note@-1 {{add 'if #available' version check}}
17+
18+
@available(Windows 10, *)
19+
func windows10() {}
20+
21+
// expected-error@+1 {{'windows10()' is only available in * 10 or newer}}
22+
windows10()
23+
// expected-note@-1 {{add 'if #available' version check}}
24+
25+
func coniditonal_compilation() {
26+
if #available(Windows 10, *) {
27+
}
28+
}

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ static void reportAttributes(ASTContext &Ctx,
660660
static UIdent PlatformtvOSAppExt("source.availability.platform.tvos_app_extension");
661661
static UIdent PlatformWatchOSAppExt("source.availability.platform.watchos_app_extension");
662662
static UIdent PlatformOpenBSD("source.availability.platform.openbsd");
663+
static UIdent PlatformWindows("source.availability.platform.windows");
663664
std::vector<const DeclAttribute*> Scratch;
664665

665666
for (auto Attr : getDeclAttributes(D, Scratch)) {
@@ -690,6 +691,8 @@ static void reportAttributes(ASTContext &Ctx,
690691
PlatformUID = PlatformWatchOSAppExt; break;
691692
case PlatformKind::OpenBSD:
692693
PlatformUID = PlatformOpenBSD; break;
694+
case PlatformKind::Windows:
695+
PlatformUID = PlatformWindows; break;
693696
}
694697

695698
AvailableAttrInfo Info;

0 commit comments

Comments
 (0)