Skip to content

Commit 932949d

Browse files
authored
[APINotes] Upstream the remaining API Notes fixes and tests
This upstreams the last bits of Clang API Notes functionality that is currently implemented in the Apple fork: https://github.com/apple/llvm-project/tree/next/clang/lib/APINotes
1 parent 26464f2 commit 932949d

File tree

96 files changed

+1829
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1829
-20
lines changed

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,54 @@ static void applyNullability(Sema &S, Decl *D, NullabilityKind Nullability,
5252
if (!Metadata.IsActive)
5353
return;
5454

55-
auto IsModified = [&](Decl *D, QualType QT,
56-
NullabilityKind Nullability) -> bool {
55+
auto GetModified =
56+
[&](Decl *D, QualType QT,
57+
NullabilityKind Nullability) -> std::optional<QualType> {
5758
QualType Original = QT;
5859
S.CheckImplicitNullabilityTypeSpecifier(QT, Nullability, D->getLocation(),
5960
isa<ParmVarDecl>(D),
6061
/*OverrideExisting=*/true);
61-
return QT.getTypePtr() != Original.getTypePtr();
62+
return (QT.getTypePtr() != Original.getTypePtr()) ? std::optional(QT)
63+
: std::nullopt;
6264
};
6365

6466
if (auto Function = dyn_cast<FunctionDecl>(D)) {
65-
if (IsModified(D, Function->getReturnType(), Nullability)) {
66-
QualType FnType = Function->getType();
67-
Function->setType(FnType);
67+
if (auto Modified =
68+
GetModified(D, Function->getReturnType(), Nullability)) {
69+
const FunctionType *FnType = Function->getType()->castAs<FunctionType>();
70+
if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(FnType))
71+
Function->setType(S.Context.getFunctionType(
72+
*Modified, proto->getParamTypes(), proto->getExtProtoInfo()));
73+
else
74+
Function->setType(
75+
S.Context.getFunctionNoProtoType(*Modified, FnType->getExtInfo()));
6876
}
6977
} else if (auto Method = dyn_cast<ObjCMethodDecl>(D)) {
70-
QualType Type = Method->getReturnType();
71-
if (IsModified(D, Type, Nullability)) {
72-
Method->setReturnType(Type);
78+
if (auto Modified = GetModified(D, Method->getReturnType(), Nullability)) {
79+
Method->setReturnType(*Modified);
7380

7481
// Make it a context-sensitive keyword if we can.
75-
if (!isIndirectPointerType(Type))
82+
if (!isIndirectPointerType(*Modified))
7683
Method->setObjCDeclQualifier(Decl::ObjCDeclQualifier(
7784
Method->getObjCDeclQualifier() | Decl::OBJC_TQ_CSNullability));
7885
}
7986
} else if (auto Value = dyn_cast<ValueDecl>(D)) {
80-
QualType Type = Value->getType();
81-
if (IsModified(D, Type, Nullability)) {
82-
Value->setType(Type);
87+
if (auto Modified = GetModified(D, Value->getType(), Nullability)) {
88+
Value->setType(*Modified);
8389

8490
// Make it a context-sensitive keyword if we can.
8591
if (auto Parm = dyn_cast<ParmVarDecl>(D)) {
86-
if (Parm->isObjCMethodParameter() && !isIndirectPointerType(Type))
92+
if (Parm->isObjCMethodParameter() && !isIndirectPointerType(*Modified))
8793
Parm->setObjCDeclQualifier(Decl::ObjCDeclQualifier(
8894
Parm->getObjCDeclQualifier() | Decl::OBJC_TQ_CSNullability));
8995
}
9096
}
9197
} else if (auto Property = dyn_cast<ObjCPropertyDecl>(D)) {
92-
QualType Type = Property->getType();
93-
if (IsModified(D, Type, Nullability)) {
94-
Property->setType(Type, Property->getTypeSourceInfo());
98+
if (auto Modified = GetModified(D, Property->getType(), Nullability)) {
99+
Property->setType(*Modified, Property->getTypeSourceInfo());
95100

96101
// Make it a property attribute if we can.
97-
if (!isIndirectPointerType(Type))
102+
if (!isIndirectPointerType(*Modified))
98103
Property->setPropertyAttributes(
99104
ObjCPropertyAttribute::kind_null_resettable);
100105
}

clang/lib/Sema/SemaObjCProperty.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,15 +638,15 @@ ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
638638
PDecl->setInvalidDecl();
639639
}
640640

641-
ProcessDeclAttributes(S, PDecl, FD.D);
642-
643641
// Regardless of setter/getter attribute, we save the default getter/setter
644642
// selector names in anticipation of declaration of setter/getter methods.
645643
PDecl->setGetterName(GetterSel, GetterNameLoc);
646644
PDecl->setSetterName(SetterSel, SetterNameLoc);
647645
PDecl->setPropertyAttributesAsWritten(
648646
makePropertyAttributesAsWritten(AttributesAsWritten));
649647

648+
ProcessDeclAttributes(S, PDecl, FD.D);
649+
650650
if (Attributes & ObjCPropertyAttribute::kind_readonly)
651651
PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readonly);
652652

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Name: SomeOtherKit
2+
Classes:
3+
- Name: A
4+
Methods:
5+
- Selector: "methodB"
6+
MethodKind: Instance
7+
Availability: none
8+
AvailabilityMsg: "anything but this"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Name: SomeBrokenLib
2+
Functions:
3+
- Name: do_something_with_pointers
4+
Nu llabilityOfRet: O
5+
# the space is intentional, to make sure we don't crash on malformed API Notes
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef SOME_BROKEN_LIB_H
2+
#define SOME_BROKEN_LIB_H
3+
4+
void do_something_with_pointers(int *ptr1, int *ptr2);
5+
6+
#endif // SOME_BROKEN_LIB_H
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Name: SomeBrokenLib
2+
Functions:
3+
- Name: do_something_with_pointers
4+
NullabilityOfRet: O
5+
- Name: do_something_with_pointers
6+
NullabilityOfRet: O
7+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef SOME_BROKEN_LIB_H
2+
#define SOME_BROKEN_LIB_H
3+
4+
void do_something_with_pointers(int *ptr1, int *ptr2);
5+
6+
#endif // SOME_BROKEN_LIB_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern int FrameworkWithActualPrivateModule;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module FrameworkWithActualPrivateModule {
2+
umbrella header "FrameworkWithActualPrivateModule.h"
3+
export *
4+
module * { export * }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module FrameworkWithActualPrivateModule_Private {
2+
umbrella header "FrameworkWithActualPrivateModule_Private.h"
3+
export *
4+
module * { export * }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Name: FrameworkWithActualPrivateModule_Private
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include <FrameworkWithActualPrivateModule/FrameworkWithActualPrivateModule.h>
2+
extern int FrameworkWithActualPrivateModule_Private;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern int FrameworkWithWrongCase;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module FrameworkWithWrongCase {
2+
umbrella header "FrameworkWithWrongCase.h"
3+
export *
4+
module * { export * }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Name: FrameworkWithWrongCase
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern int FrameworkWithWrongCasePrivate;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module FrameworkWithWrongCasePrivate {
2+
umbrella header "FrameworkWithWrongCasePrivate.h"
3+
export *
4+
module * { export * }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module FrameworkWithWrongCasePrivate.Inner {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Name: FrameworkWithWrongCasePrivate
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import LayeredKitImpl;
2+
3+
// @interface declarations already don't inherit attributes from forward
4+
// declarations, so in order to test this properly we have to /not/ define
5+
// UpwardClass anywhere.
6+
7+
// @interface UpwardClass
8+
// @end
9+
10+
@protocol UpwardProto
11+
@end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module LayeredKit {
2+
umbrella header "LayeredKit.h"
3+
export *
4+
module * { export * }
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Name: LayeredKitImpl
2+
Classes:
3+
- Name: PerfectlyNormalClass
4+
Availability: none
5+
- Name: UpwardClass
6+
Availability: none
7+
Protocols:
8+
- Name: UpwardProto
9+
Availability: none
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@protocol UpwardProto;
2+
@class UpwardClass;
3+
4+
@interface PerfectlyNormalClass
5+
@end
6+
7+
void doImplementationThings(UpwardClass *first, id <UpwardProto> second) __attribute((unavailable));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module LayeredKitImpl {
2+
umbrella header "LayeredKitImpl.h"
3+
export *
4+
module * { export * }
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module SimpleKit {
2+
umbrella header "SimpleKit.h"
3+
export *
4+
module * { export * }
5+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Name: SomeKit
2+
Classes:
3+
- Name: A
4+
Methods:
5+
- Selector: "transform:"
6+
MethodKind: Instance
7+
Availability: none
8+
AvailabilityMsg: "anything but this"
9+
- Selector: "transform:integer:"
10+
MethodKind: Instance
11+
NullabilityOfRet: N
12+
Nullability: [ N, S ]
13+
Properties:
14+
- Name: intValue
15+
PropertyKind: Instance
16+
Availability: none
17+
AvailabilityMsg: "wouldn't work anyway"
18+
- Name: nonnullAInstance
19+
PropertyKind: Instance
20+
Nullability: N
21+
- Name: nonnullAClass
22+
PropertyKind: Class
23+
Nullability: N
24+
- Name: nonnullABoth
25+
Nullability: N
26+
- Name: B
27+
Availability: none
28+
AvailabilityMsg: "just don't"
29+
- Name: C
30+
Methods:
31+
- Selector: "initWithA:"
32+
MethodKind: Instance
33+
DesignatedInit: true
34+
- Name: OverriddenTypes
35+
Methods:
36+
- Selector: "methodToMangle:second:"
37+
MethodKind: Instance
38+
ResultType: 'char *'
39+
Parameters:
40+
- Position: 0
41+
Type: 'SOMEKIT_DOUBLE *'
42+
- Position: 1
43+
Type: 'float *'
44+
Properties:
45+
- Name: intPropertyToMangle
46+
PropertyKind: Instance
47+
Type: 'double *'
48+
Functions:
49+
- Name: global_int_fun
50+
ResultType: 'char *'
51+
Parameters:
52+
- Position: 0
53+
Type: 'double *'
54+
- Position: 1
55+
Type: 'float *'
56+
Globals:
57+
- Name: global_int_ptr
58+
Type: 'double *'
59+
SwiftVersions:
60+
- Version: 3.0
61+
Classes:
62+
- Name: A
63+
Methods:
64+
- Selector: "transform:integer:"
65+
MethodKind: Instance
66+
NullabilityOfRet: O
67+
Nullability: [ O, S ]
68+
Properties:
69+
- Name: explicitNonnullInstance
70+
PropertyKind: Instance
71+
Nullability: O
72+
- Name: explicitNullableInstance
73+
PropertyKind: Instance
74+
Nullability: N
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Name: SomeKit
2+
Classes:
3+
- Name: A
4+
Methods:
5+
- Selector: "privateTransform:input:"
6+
MethodKind: Instance
7+
NullabilityOfRet: N
8+
Nullability: [ N, S ]
9+
Properties:
10+
- Name: internalProperty
11+
Nullability: N
12+
Protocols:
13+
- Name: InternalProtocol
14+
Availability: none
15+
AvailabilityMsg: "not for you"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef SOMEKIT_H
2+
#define SOMEKIT_H
3+
4+
#define ROOT_CLASS __attribute__((objc_root_class))
5+
6+
ROOT_CLASS
7+
@interface A
8+
-(A*)transform:(A*)input;
9+
-(A*)transform:(A*)input integer:(int)integer;
10+
11+
@property (nonatomic, readonly, retain) A* someA;
12+
@property (nonatomic, retain) A* someOtherA;
13+
14+
@property (nonatomic) int intValue;
15+
@end
16+
17+
@interface B : A
18+
@end
19+
20+
@interface C : A
21+
- (instancetype)init;
22+
- (instancetype)initWithA:(A*)a;
23+
@end
24+
25+
26+
@interface MyClass : A
27+
- Inst;
28+
+ Clas;
29+
@end
30+
31+
struct CGRect {
32+
float origin;
33+
float size;
34+
};
35+
typedef struct CGRect NSRect;
36+
37+
@interface I
38+
- (void) Meth : (NSRect[4])exposedRects;
39+
- (void) Meth1 : (const I*)exposedRects;
40+
- (void) Meth2 : (const I*)exposedRects;
41+
- (void) Meth3 : (I*)exposedRects;
42+
- (const I*) Meth4;
43+
- (const I*) Meth5 : (int) Arg1 : (const I*)Arg2 : (double)Arg3 : (const I*) Arg4 :(const volatile id) Arg5;
44+
- (volatile const I*) Meth6 : (const char *)Arg1 : (const char *)Arg2 : (double)Arg3 : (const I*) Arg4 :(const volatile id) Arg5;
45+
@end
46+
47+
@class NSURL, NSArray, NSError;
48+
@interface INTF_BLOCKS
49+
+ (void)getNonLocalVersionsOfItemAtURL:(NSURL *)url completionHandler:(void (^)(NSArray *nonLocalFileVersions, NSError *error))completionHandler;
50+
+ (void *)getNonLocalVersionsOfItemAtURL2:(NSURL *)url completionHandler:(void (^)(NSArray *nonLocalFileVersions, NSError *error))completionHandler;
51+
+ (NSError **)getNonLocalVersionsOfItemAtURL3:(int)url completionHandler:(void (^)(NSArray *nonLocalFileVersions, NSError *error))completionHandler;
52+
+ (id)getNonLocalVersionsOfItemAtURL4:(NSURL *)url completionHandler:(void (^)(int nonLocalFileVersions, NSError *error, NSURL*))completionHandler;
53+
@end
54+
55+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework module SomeKit {
2+
umbrella header "SomeKit.h"
3+
export *
4+
module * { export * }
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module SomeKit.Private {
2+
header "SomeKit_Private.h"
3+
export *
4+
5+
explicit module NullAnnotation {
6+
header "SomeKit_PrivateForNullAnnotation.h"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
explicit framework module SomeKit.Private {
2+
header "SomeKit_Private.h"
3+
explicit NullAnnotation { header "SomeKit_PrivateForNullAnnotation.h" }
4+
export *
5+
module * { export * }
6+
syntax error
7+
8+
}

0 commit comments

Comments
 (0)