Skip to content

Commit 3ca3e05

Browse files
committed
Share string literal for spi_available attribute name
1 parent af0d4ef commit 3ca3e05

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

include/swift/Strings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ constexpr static const StringLiteral BUILTIN_TYPE_NAME_PREFIX = "Builtin.";
6464
constexpr static const StringLiteral CLANG_MODULE_DEFUALT_SPI_GROUP_NAME =
6565
"OBJC_DEFAULT_SPI_GROUP";
6666

67+
/// The attribute name for @_spi_available
68+
constexpr static const StringLiteral SPI_AVAILABLE_ATTRNAME =
69+
"_spi_available";
70+
6771
/// A composition class containing a StringLiteral for the names of
6872
/// Swift builtins. The reason we use this is to ensure that we when
6973
/// necessary slice off the "Builtin." prefix from these names in a

lib/AST/Attr.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/Types.h"
3030
#include "swift/Basic/Defer.h"
3131
#include "swift/Basic/QuotedString.h"
32+
#include "swift/Strings.h"
3233
#include "llvm/ADT/SmallString.h"
3334
#include "llvm/ADT/StringSwitch.h"
3435
#include "llvm/Support/ErrorHandling.h"
@@ -126,7 +127,7 @@ DeclAttrKind DeclAttribute::getAttrKindFromString(StringRef Str) {
126127
#define DECL_ATTR(X, CLASS, ...) .Case(#X, DAK_##CLASS)
127128
#define DECL_ATTR_ALIAS(X, CLASS) .Case(#X, DAK_##CLASS)
128129
#include "swift/AST/Attr.def"
129-
.Case("_spi_available", DAK_Available)
130+
.Case(SPI_AVAILABLE_ATTRNAME, DAK_Available)
130131
.Default(DAK_Count);
131132
}
132133

@@ -982,7 +983,12 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
982983
Printer << ", unavailable)";
983984
break;
984985
}
985-
Printer.printAttrName(Attr->IsSPI ? "@_spi_available": "@available");
986+
if (Attr->IsSPI) {
987+
std::string atSPI = (llvm::Twine("@") + SPI_AVAILABLE_ATTRNAME).str();
988+
Printer.printAttrName(atSPI);
989+
} else {
990+
Printer.printAttrName("@available");
991+
}
986992
Printer << "(";
987993
printAvailableAttr(Attr, Printer, Options);
988994
Printer << ")";

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Parse/SyntaxParsingContext.h"
2222
#include "swift/Syntax/SyntaxKind.h"
2323
#include "swift/Subsystems.h"
24+
#include "swift/Strings.h"
2425
#include "swift/AST/ASTWalker.h"
2526
#include "swift/AST/Attr.h"
2627
#include "swift/AST/GenericParamList.h"
@@ -574,7 +575,7 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
574575
Obsoleted.Version, Obsoleted.Range,
575576
PlatformAgnostic,
576577
/*Implicit=*/false,
577-
AttrName == "_spi_available");
578+
AttrName == SPI_AVAILABLE_ATTRNAME);
578579
return makeParserResult(Attr);
579580

580581
}
@@ -860,7 +861,7 @@ bool Parser::parseAvailability(
860861
/*Obsoleted=*/llvm::VersionTuple(),
861862
/*ObsoletedRange=*/SourceRange(), PlatformAgnostic,
862863
/*Implicit=*/false,
863-
AttrName == "_spi_available"));
864+
AttrName == SPI_AVAILABLE_ATTRNAME));
864865
}
865866

866867
return true;

0 commit comments

Comments
 (0)