Skip to content

Commit 87cc490

Browse files
authored
Allow identification of OSLogMessage by a @_semantics attribute instead of just name (#39455)
1 parent b1236e9 commit 87cc490

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

include/swift/AST/SemanticAttrs.def

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
7575
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_OWNED2GUARANTEE_NEVER,
7676
"optimize.sil.specialize.owned2guarantee.never")
7777

78+
SEMANTICS_ATTR(OSLOG_MESSAGE_TYPE, "oslog.message.type")
7879
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_INTERPOLATION, "oslog.message.init_interpolation")
7980
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_STRING_LITERAL, "oslog.message.init_stringliteral")
8081
SEMANTICS_ATTR(OSLOG_REQUIRES_CONSTANT_ARGUMENTS, "oslog.requires_constant_arguments")

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1510,8 +1510,8 @@ static ApplyInst *getAsOSLogMessageInit(SILInstruction *inst) {
15101510
}
15111511

15121512
/// Return true iff the SIL function \c fun is a method of the \c OSLogMessage
1513-
/// type.
1514-
bool isMethodOfOSLogMessage(SILFunction &fun) {
1513+
/// type or a type that has the @_semantics("oslog.message.type") annotation.
1514+
static bool isMethodOfOSLogMessage(SILFunction &fun) {
15151515
DeclContext *declContext = fun.getDeclContext();
15161516
if (!declContext)
15171517
return false;
@@ -1527,7 +1527,8 @@ bool isMethodOfOSLogMessage(SILFunction &fun) {
15271527
NominalTypeDecl *typeDecl = parentContext->getSelfNominalTypeDecl();
15281528
if (!typeDecl)
15291529
return false;
1530-
return typeDecl->getName() == fun.getASTContext().Id_OSLogMessage;
1530+
return typeDecl->getName() == fun.getASTContext().Id_OSLogMessage
1531+
|| typeDecl->hasSemanticsAttr(semantics::OSLOG_MESSAGE_TYPE);
15311532
}
15321533

15331534
class OSLogOptimization : public SILFunctionTransform {

lib/Sema/ConstantnessSemaDiagnostics.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ static void diagnoseError(Expr *errorExpr, const ASTContext &astContext,
284284
}
285285
// If this is OSLogMessage, it should be a string-interpolation literal.
286286
Identifier declName = nominalDecl->getName();
287-
if (declName == astContext.Id_OSLogMessage) {
287+
if (declName == astContext.Id_OSLogMessage ||
288+
nominalDecl->hasSemanticsAttr(semantics::OSLOG_MESSAGE_TYPE)) {
288289
diags.diagnose(errorLoc, diag::oslog_message_must_be_string_interpolation);
289290
return;
290291
}

stdlib/private/OSLog/OSLogTestHelper.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal func _os_log_impl_test(
121121
/// on the special case of animation begin signposts.
122122
@_transparent
123123
public func _osSignpostAnimationBeginTestHelper(
124-
_ format: AnimationFormatString.OSLogMessage,
124+
_ format: AnimationFormatString.MyLogMessage,
125125
_ arguments: CVarArg...
126126
) {
127127
_animationBeginSignpostHelper(formatStringPointer: format.formatStringPointer,
@@ -149,7 +149,8 @@ public enum AnimationFormatString {
149149
}
150150

151151
@frozen
152-
public struct OSLogMessage : ExpressibleByStringLiteral {
152+
@_semantics("oslog.message.type")
153+
public struct MyLogMessage : ExpressibleByStringLiteral {
153154
@usableFromInline
154155
var formatStringPointer: UnsafePointer<CChar>
155156

0 commit comments

Comments
 (0)