Skip to content

Allow identification of OSLogMessage by a @_semantics attribute instead of just name #39455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/SemanticAttrs.def
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_OWNED2GUARANTEE_NEVER,
"optimize.sil.specialize.owned2guarantee.never")

SEMANTICS_ATTR(OSLOG_MESSAGE_TYPE, "oslog.message.type")
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_INTERPOLATION, "oslog.message.init_interpolation")
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_STRING_LITERAL, "oslog.message.init_stringliteral")
SEMANTICS_ATTR(OSLOG_REQUIRES_CONSTANT_ARGUMENTS, "oslog.requires_constant_arguments")
Expand Down
7 changes: 4 additions & 3 deletions lib/SILOptimizer/Mandatory/OSLogOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,8 +1510,8 @@ static ApplyInst *getAsOSLogMessageInit(SILInstruction *inst) {
}

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

class OSLogOptimization : public SILFunctionTransform {
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/ConstantnessSemaDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ static void diagnoseError(Expr *errorExpr, const ASTContext &astContext,
}
// If this is OSLogMessage, it should be a string-interpolation literal.
Identifier declName = nominalDecl->getName();
if (declName == astContext.Id_OSLogMessage) {
if (declName == astContext.Id_OSLogMessage ||
nominalDecl->hasSemanticsAttr(semantics::OSLOG_MESSAGE_TYPE)) {
diags.diagnose(errorLoc, diag::oslog_message_must_be_string_interpolation);
return;
}
Expand Down
5 changes: 3 additions & 2 deletions stdlib/private/OSLog/OSLogTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ internal func _os_log_impl_test(
/// on the special case of animation begin signposts.
@_transparent
public func _osSignpostAnimationBeginTestHelper(
_ format: AnimationFormatString.OSLogMessage,
_ format: AnimationFormatString.MyLogMessage,
_ arguments: CVarArg...
) {
_animationBeginSignpostHelper(formatStringPointer: format.formatStringPointer,
Expand Down Expand Up @@ -149,7 +149,8 @@ public enum AnimationFormatString {
}

@frozen
public struct OSLogMessage : ExpressibleByStringLiteral {
@_semantics("oslog.message.type")
public struct MyLogMessage : ExpressibleByStringLiteral {
@usableFromInline
var formatStringPointer: UnsafePointer<CChar>

Expand Down