Skip to content

[6.2] ClangImporter: Introduce the #ClangDeclarationImport diagnostic group #81307

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
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
3 changes: 2 additions & 1 deletion include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
GROUP(no_group, "")

GROUP(ActorIsolatedCall, "actor-isolated-call")
GROUP(NonisolatedNonsendingByDefault, "nonisolated-nonsending-by-default")
GROUP(ClangDeclarationImport, "clang-declaration-import")
GROUP(ConformanceIsolation, "conformance-isolation")
GROUP(DeprecatedDeclaration, "deprecated-declaration")
GROUP(DynamicCallable, "dynamic-callable-requirements")
Expand All @@ -52,6 +52,7 @@ GROUP(IsolatedConformances, "isolated-conformances")
GROUP(MultipleInheritance, "multiple-inheritance")
GROUP(MutableGlobalVariable, "mutable-global-variable")
GROUP(NominalTypes, "nominal-types")
GROUP(NonisolatedNonsendingByDefault, "nonisolated-nonsending-by-default")
GROUP(OpaqueTypeInference, "opaque-type-inference")
GROUP(PreconcurrencyImport, "preconcurrency-import")
GROUP(PropertyWrappers, "property-wrapper-requirements")
Expand Down
42 changes: 23 additions & 19 deletions include/swift/AST/DiagnosticsClangImporter.def
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ ERROR(emit_pcm_error,Fatal,
ERROR(dump_pcm_error,Fatal,
"failed to dump precompiled module '%0'", (StringRef))

WARNING(invalid_swift_name_method,none,
GROUPED_WARNING(invalid_swift_name_method, ClangDeclarationImport, none,
"too %select{few|many}0 parameters in swift_name attribute (expected %1; "
"got %2)", (bool, unsigned, unsigned))
WARNING(invalid_swift_name_for_decl,none,
GROUPED_WARNING(invalid_swift_name_for_decl, ClangDeclarationImport, none,
"custom Swift name '%0' ignored because it is not valid for %kindonly1; "
"imported as %1 instead",
(StringRef, ValueDecl *))
Expand All @@ -67,35 +67,35 @@ ERROR(swift_name_protocol_static, none, "swift_name cannot be used to define "
ERROR(swift_name_no_prototype, none,
"swift_name cannot be used on a non-prototyped function declaration", ())

WARNING(inconsistent_swift_name,none,
GROUPED_WARNING(inconsistent_swift_name, ClangDeclarationImport, none,
"inconsistent Swift name for Objective-C %select{method|property}0 "
"'%1' in '%2' (%3 in '%4' vs. %5 in '%6')",
(bool, StringRef, StringRef, DeclName, StringRef, DeclName,
StringRef))

WARNING(swift_name_circular_context_import,none,
GROUPED_WARNING(swift_name_circular_context_import, ClangDeclarationImport, none,
"cycle detected while resolving '%0' in swift_name attribute for '%1'",
(StringRef, StringRef))
NOTE(swift_name_circular_context_import_other,none,
"while resolving '%0' in swift_name attribute for '%1'",
(StringRef, StringRef))

WARNING(unresolvable_clang_decl,none,
GROUPED_WARNING(unresolvable_clang_decl, ClangDeclarationImport, none,
"imported declaration '%0' could not be mapped to '%1'",
(StringRef, StringRef))

NOTE(unresolvable_clang_decl_is_a_framework_bug,none,
"please report this issue to the owners of '%0'",
(StringRef))

WARNING(clang_swift_attr_unhandled,none,
GROUPED_WARNING(clang_swift_attr_unhandled, ClangDeclarationImport, none,
"ignoring unknown Swift attribute or modifier '%0'", (StringRef))

WARNING(clang_error_code_must_be_sendable,none,
GROUPED_WARNING(clang_error_code_must_be_sendable, ClangDeclarationImport, none,
"cannot make error code type '%0' non-sendable because Swift errors "
"are always sendable", (StringRef))

WARNING(clang_ignored_sendable_attr,none,
GROUPED_WARNING(clang_ignored_sendable_attr, ClangDeclarationImport, none,
"cannot make type %0 sendable because '@Sendable' and '& Sendable' "
"cannot be added to it",
(Type))
Expand All @@ -108,18 +108,18 @@ WARNING(implicit_bridging_header_imported_from_module,none,
"is deprecated and will be removed in a later version of Swift",
(StringRef, Identifier))

WARNING(import_multiple_mainactor_attr,none,
GROUPED_WARNING(import_multiple_mainactor_attr, ClangDeclarationImport, none,
"this attribute for global actor '%0' is invalid; the declaration already has attribute for global actor '%1'",
(StringRef, StringRef))

WARNING(contradicting_mutation_attrs,none,
GROUPED_WARNING(contradicting_mutation_attrs, ClangDeclarationImport, none,
"attribute '%0' is ignored when combined with attribute '%1'",
(StringRef, StringRef))

WARNING(nonmutating_without_const,none,
GROUPED_WARNING(nonmutating_without_const, ClangDeclarationImport, none,
"attribute 'nonmutating' has no effect on non-const method", ())

WARNING(nonmutating_without_mutable_fields,none,
GROUPED_WARNING(nonmutating_without_mutable_fields, ClangDeclarationImport, none,
"attribute 'nonmutating' has no effect without any mutable fields", ())

ERROR(module_map_not_found, none, "module map file '%0' not found", (StringRef))
Expand All @@ -135,7 +135,7 @@ WARNING(libstdcxx_modulemap_not_found, none,
"module map for libstdc++ not found for '%0'; C++ stdlib may be unavailable",
(StringRef))

WARNING(api_pattern_attr_ignored, none,
GROUPED_WARNING(api_pattern_attr_ignored, ClangDeclarationImport, none,
"'%0' Swift attribute ignored on type '%1': type is not copyable or destructible",
(StringRef, StringRef))

Expand Down Expand Up @@ -226,7 +226,7 @@ ERROR(private_fileid_attr_repeated, none,
NOTE(private_fileid_attr_here, none,
"SWIFT_PRIVATE_FILEID annotation found here", ())

WARNING(private_fileid_attr_format_invalid, none,
GROUPED_WARNING(private_fileid_attr_format_invalid, ClangDeclarationImport, none,
"SWIFT_PRIVATE_FILEID annotation on '%0' does not have a valid file ID",
(StringRef))
REMARK(private_fileid_attr_format_specification, none,
Expand Down Expand Up @@ -302,12 +302,12 @@ ERROR(returns_retained_or_returns_unretained_for_non_cxx_frt_values, none,
// TODO: In the next C++ interop version, convert this warning into an error and
// stop importing unannotated C++ APIs that return SWIFT_SHARED_REFERENCE.
// rdar://138806722
WARNING(no_returns_retained_returns_unretained, none,
GROUPED_WARNING(no_returns_retained_returns_unretained, ClangDeclarationImport, none,
"%0 should be annotated with either SWIFT_RETURNS_RETAINED or "
"SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE",
(const clang::NamedDecl *))

WARNING(returns_retained_returns_unretained_on_overloaded_operator, none,
GROUPED_WARNING(returns_retained_returns_unretained_on_overloaded_operator, ClangDeclarationImport, none,
"SWIFT_RETURNS_RETAINED and SWIFT_RETURNS_UNRETAINED is not supported "
"yet for overloaded C++ %0. Overloaded C++ operators always "
"return "
Expand All @@ -318,7 +318,7 @@ WARNING(returns_retained_returns_unretained_on_overloaded_operator, none,
// stop importing C++ types that inherit from SWIFT_SHARED_REFERENCE if the
// Swift compiler cannot find unique retain/release functions.
// rdar://145194375
WARNING(cant_infer_frt_in_cxx_inheritance, none,
GROUPED_WARNING(cant_infer_frt_in_cxx_inheritance, ClangDeclarationImport, none,
"unable to infer SWIFT_SHARED_REFERENCE for %0, although one of its "
"transitive base types is marked as SWIFT_SHARED_REFERENCE",
(const clang::NamedDecl *))
Expand Down Expand Up @@ -351,8 +351,12 @@ NOTE(forward_declared_protocol_clashes_with_imported_objc_Swift_protocol, none,
"its name conflicts with a %1 in module %2",
(const clang::NamedDecl*, StringRef, StringRef))

WARNING(return_escapable_with_lifetimebound, none, "the returned type '%0' is annotated as escapable; it cannot have lifetime dependencies", (StringRef))
WARNING(return_nonescapable_without_lifetimebound, none, "the returned type '%0' is annotated as non-escapable; its lifetime dependencies must be annotated", (StringRef))
GROUPED_WARNING(return_escapable_with_lifetimebound, ClangDeclarationImport, none,
"the returned type '%0' is annotated as escapable; it cannot have lifetime dependencies",
(StringRef))
GROUPED_WARNING(return_nonescapable_without_lifetimebound, ClangDeclarationImport, none,
"the returned type '%0' is annotated as non-escapable; its lifetime dependencies must be annotated",
(StringRef))

ERROR(unknown_template_parameter,none,
"template parameter '%0' does not exist", (StringRef))
Expand Down
4 changes: 2 additions & 2 deletions test/ClangImporter/Inputs/custom-modules/ImportAsMember.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ typedef float MNInnerFloat __attribute__((swift_name("IAMMultipleNested.Inner"))

typedef int IAMBadInnerInt
__attribute__((swift_name("IAMNonexistent.Inner")));
// CHECK: ImportAsMember.h:[[@LINE-1]]:{{[0-9]+}}: warning: imported declaration 'IAMBadInnerInt' could not be mapped to 'IAMNonexistent.Inner'
// CHECK: note: please report this issue to the owners of 'ImportAsMember'
// CHECK: ImportAsMember.h:[[@LINE-1]]:{{[0-9]+}}: warning: imported declaration 'IAMBadInnerInt' could not be mapped to 'IAMNonexistent.Inner' [#ClangDeclarationImport]
// CHECK: note: please report this issue to the owners of 'ImportAsMember' [#ClangDeclarationImport]
typedef int IAMBadInnerIntAPINotes;
// CHECK: ImportAsMember.h:[[@LINE-1]]:{{[0-9]+}}: warning: imported declaration 'IAMBadInnerIntAPINotes' could not be mapped to 'IAMNonexistent.Inner2'
// CHECK: note: please report this issue to the owners of 'ImportAsMember'
Expand Down
6 changes: 3 additions & 3 deletions test/ClangImporter/attr-swift_name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ func test(_ i: Int) {
// We only see these five warnings because Clang can catch the other invalid
// cases, and marks the attribute as invalid ahead of time.

// CHECK: warning: too few parameters in swift_name attribute (expected 2; got 1)
// CHECK: warning: too few parameters in swift_name attribute (expected 2; got 1) [#ClangDeclarationImport]
// CHECK: + (instancetype)g:(id)x outParam:(int *)foo SWIFT_NAME(init(g:));
// CHECK-NOT: warning:
// CHECK: note: please report this issue to the owners of 'ObjCIRExtras'
// CHECK-NOT: warning:

// CHECK: warning: cycle detected while resolving 'CircularName' in swift_name attribute for 'CircularName'
// CHECK: warning: cycle detected while resolving 'CircularName' in swift_name attribute for 'CircularName' [#ClangDeclarationImport]
// CHECK: SWIFT_NAME(CircularName.Inner) @interface CircularName : NSObject @end
// CHECK-NOT: {{warning|note}}:
// CHECK: note: please report this issue to the owners of 'ObjCIRExtras'
// CHECK-NOT: warning:

// CHECK: warning: cycle detected while resolving 'MutuallyCircularNameB' in swift_name attribute for 'MutuallyCircularNameA'
// CHECK: warning: cycle detected while resolving 'MutuallyCircularNameB' in swift_name attribute for 'MutuallyCircularNameA' [#ClangDeclarationImport]
// CHECK: SWIFT_NAME(MutuallyCircularNameB.Inner) @interface MutuallyCircularNameA : NSObject @end
// CHECK-NOT: {{warning|note}}:
// CHECK: note: while resolving 'MutuallyCircularNameA' in swift_name attribute for 'MutuallyCircularNameB'
Expand Down
4 changes: 2 additions & 2 deletions test/ClangImporter/duplicate_mainactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// REQUIRES: concurrency
// REQUIRES: objc_interop

// CHECK: DoubleMainActor.h:{{[0-9]+}}:{{[0-9]+}}: warning: this attribute for global actor '@MainActor' is invalid; the declaration already has attribute for global actor '@UIActor'
// CHECK: DoubleMainActor.h:{{[0-9]+}}:{{[0-9]+}}: warning: this attribute for global actor '@MainActor' is invalid; the declaration already has attribute for global actor '@UIActor' [#ClangDeclarationImport]

protocol P : DoubleMainActor {}
protocol P : DoubleMainActor {}
2 changes: 1 addition & 1 deletion test/IDE/print_omit_needless_words.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
// CHECK-OMIT-NEEDLESS-WORDS-NEXT: @available(swift, obsoleted: 3, renamed: "veryCarefullyBurn()")
// CHECK-OMIT-NEEDLESS-WORDS-NEXT: func veryCarefullyBurnGarbage4DTypeRefMask_t()

// CHECK-OMIT-NEEDLESS-WORDS-DIAGS: inconsistent Swift name for Objective-C property 'conflictingProp1' in 'OMWSub' ('waggleProp1' in 'OMWWaggle' vs. 'wiggleProp1' in 'OMWSuper')
// CHECK-OMIT-NEEDLESS-WORDS-DIAGS: inconsistent Swift name for Objective-C property 'conflictingProp1' in 'OMWSub' ('waggleProp1' in 'OMWWaggle' vs. 'wiggleProp1' in 'OMWSuper') [#ClangDeclarationImport]

// Don't drop the 'error'.
// CHECK-ERRORS: func tryAndReturnError(_: ()) throws
12 changes: 6 additions & 6 deletions test/Interop/Cxx/class/nonescapable-errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ public func importInvalid(_ x: Invalid) {
// CHECK: error: a function with a ~Escapable result needs a parameter to depend on
// CHECK-NO-LIFETIMES: test.swift:11:32: error: a function with a ~Escapable result requires '-enable-experimental-feature LifetimeDependence'
public func noAnnotations() -> View {
// CHECK: nonescapable.h:16:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies
// CHECK-NO-LIFETIMES: nonescapable.h:16:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies
// CHECK: nonescapable.h:16:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
// CHECK-NO-LIFETIMES: nonescapable.h:16:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
f(nil)
// CHECK: nonescapable.h:20:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies
// CHECK-NO-LIFETIMES: nonescapable.h:20:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies
// CHECK: nonescapable.h:20:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
// CHECK-NO-LIFETIMES: nonescapable.h:20:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
// No duplicate warning for f2:
// CHECK-NOT: nonescapable.h:20
f2(nil, nil)
// CHECK: nonescapable.h:24:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated
// CHECK-NO-LIFETIMES: nonescapable.h:24:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated
// CHECK: nonescapable.h:24:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated [#ClangDeclarationImport]
// CHECK-NO-LIFETIMES: nonescapable.h:24:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated [#ClangDeclarationImport]
// CHECK-NO-LIFETIMES: nonescapable.h:24:6: error: a function with a ~Escapable result requires '-enable-experimental-feature LifetimeDependence'
g(nil)
h1(nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ __attribute__((swift_attr("unsafe")));

// C++ APIs returning cxx frts (for testing diagnostics)
struct StructWithAPIsReturningCxxFrt {
static FRTStruct *_Nonnull StaticMethodReturningCxxFrt(); // expected-warning {{'StaticMethodReturningCxxFrt' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
static FRTStruct *_Nonnull StaticMethodReturningCxxFrt(); // expected-warning {{'StaticMethodReturningCxxFrt' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
static FRTStruct *_Nonnull StaticMethodReturningCxxFrtWithAnnotation()
__attribute__((swift_attr("returns_retained")));
};
Expand Down
17 changes: 17 additions & 0 deletions userdocs/diagnostics/clang-declaration-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Imported Clang Declaration Warnings (`ClangDeclarationImport`)


This diagnostic group covers all warnings related to malformed APIs that are imported into Swift from C, C++, and Obj-C headers.

As one example of a potential malformed API diagnostic, suppose a Clang module dependency contained the following declaration:

```
typedef int NotificationIdentifier
__attribute__((swift_name("Notification.Identifier")))
```

The Swift compiler would emit the following warning if no type named `Notification` could be found:

```
warning: imported declaration 'NotificationIdentifier' could not be mapped to 'Notification.Identifier’
```