Skip to content

[Module printing] Don't print attached macros in generated interfaces #66131

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 2 commits into from
May 26, 2023
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
4 changes: 4 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ struct PrintOptions {
/// prevent printing from doing "extra" work.
bool PrintCurrentMembersOnly = false;

/// Whether to suppress printing of custom attributes that are expanded macros.
bool SuppressExpandedMacros = true;

/// List of attribute kinds that should not be printed.
std::vector<AnyAttrKind> ExcludeAttrList = {DAK_Transparent, DAK_Effects,
DAK_FixedLayout,
Expand Down Expand Up @@ -653,6 +656,7 @@ struct PrintOptions {
result.EnumRawValues = EnumRawValueMode::PrintObjCOnly;
result.MapCrossImportOverlaysToDeclaringModule = true;
result.PrintCurrentMembersOnly = false;
result.SuppressExpandedMacros = true;
return result;
}

Expand Down
9 changes: 9 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,15 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
if (Options.excludeAttrKind(DA->getKind()))
continue;

// If we're supposed to suppress expanded macros, check whether this is
// a macro.
if (Options.SuppressExpandedMacros) {
if (auto customAttr = dyn_cast<CustomAttr>(DA)) {
if (D->getResolvedMacro(const_cast<CustomAttr *>(customAttr)))
continue;
}
}

// If this attribute is only allowed because this is a Clang decl, don't
// print it.
if (D && D->hasClangNode()
Expand Down
1 change: 1 addition & 0 deletions test/Macros/macro_expand_conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public struct PublicEquatable {
public init() { }
}

// INTERFACE-NOT: @Equatable
// INTERFACE: public struct PublicEquatable
// INTERFACE: extension ModuleWithEquatable.PublicEquatable : Swift.Equatable

Expand Down
24 changes: 24 additions & 0 deletions test/ModuleInterface/Observable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -module-name Library -plugin-path %swift-host-lib-dir/plugins -disable-availability-checking
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library -disable-availability-checking
// RUN: %FileCheck %s < %t/Library.swiftinterface

// REQUIRES: swift_swift_parser

import _Observation

// CHECK-NOT: @Observable
// CHECK-NOT: @ObservationIgnored
// CHECK-NOT: @ObservationTracked

@Observable
public class SomeClass {
public var field = 3
@ObservationIgnored public var ignored = 4
}

public func requiresObservable<T: Observable>(_: T) { }

@inlinable func useObservable(sc: SomeClass) {
requiresObservable(sc)
}