Skip to content

Commit fa8185c

Browse files
committedApr 8, 2019
Clean up ObjCPropertyDecl printing
Summary: - `@property(attr, attr2)` instead of `@property ( attr,attr2 )`. - Change priority of attributes (see code/comments inline). - Support for printing weak and unsafe_unretained attributes. Subscribers: arphaman, jfb, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D57965 llvm-svn: 357937
1 parent 63b97d2 commit fa8185c

File tree

5 files changed

+80
-41
lines changed

5 files changed

+80
-41
lines changed
 

Diff for: ‎clang/lib/AST/DeclPrinter.cpp

+53-36
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,13 @@ void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
13911391

13921392
/// PrintObjCPropertyDecl - print a property declaration.
13931393
///
1394+
/// Print attributes in the following order:
1395+
/// - class
1396+
/// - nonatomic | atomic
1397+
/// - assign | retain | strong | copy | weak | unsafe_unretained
1398+
/// - readwrite | readonly
1399+
/// - getter & setter
1400+
/// - nullability
13941401
void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
13951402
if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
13961403
Out << "@required\n";
@@ -1402,58 +1409,69 @@ void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
14021409
Out << "@property";
14031410
if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
14041411
bool first = true;
1405-
Out << " (";
1406-
if (PDecl->getPropertyAttributes() &
1407-
ObjCPropertyDecl::OBJC_PR_readonly) {
1408-
Out << (first ? ' ' : ',') << "readonly";
1412+
Out << "(";
1413+
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1414+
Out << (first ? "" : ", ") << "class";
14091415
first = false;
14101416
}
14111417

1412-
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1413-
Out << (first ? ' ' : ',') << "getter = ";
1414-
PDecl->getGetterName().print(Out);
1418+
if (PDecl->getPropertyAttributes() &
1419+
ObjCPropertyDecl::OBJC_PR_nonatomic) {
1420+
Out << (first ? "" : ", ") << "nonatomic";
14151421
first = false;
14161422
}
1417-
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1418-
Out << (first ? ' ' : ',') << "setter = ";
1419-
PDecl->getSetterName().print(Out);
1423+
if (PDecl->getPropertyAttributes() &
1424+
ObjCPropertyDecl::OBJC_PR_atomic) {
1425+
Out << (first ? "" : ", ") << "atomic";
14201426
first = false;
14211427
}
14221428

14231429
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1424-
Out << (first ? ' ' : ',') << "assign";
1425-
first = false;
1426-
}
1427-
1428-
if (PDecl->getPropertyAttributes() &
1429-
ObjCPropertyDecl::OBJC_PR_readwrite) {
1430-
Out << (first ? ' ' : ',') << "readwrite";
1430+
Out << (first ? "" : ", ") << "assign";
14311431
first = false;
14321432
}
1433-
14341433
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1435-
Out << (first ? ' ' : ',') << "retain";
1434+
Out << (first ? "" : ", ") << "retain";
14361435
first = false;
14371436
}
14381437

14391438
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1440-
Out << (first ? ' ' : ',') << "strong";
1439+
Out << (first ? "" : ", ") << "strong";
14411440
first = false;
14421441
}
1443-
14441442
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1445-
Out << (first ? ' ' : ',') << "copy";
1443+
Out << (first ? "" : ", ") << "copy";
1444+
first = false;
1445+
}
1446+
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
1447+
Out << (first ? "" : ", ") << "weak";
1448+
first = false;
1449+
}
1450+
if (PDecl->getPropertyAttributes()
1451+
& ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
1452+
Out << (first ? "" : ", ") << "unsafe_unretained";
14461453
first = false;
14471454
}
14481455

14491456
if (PDecl->getPropertyAttributes() &
1450-
ObjCPropertyDecl::OBJC_PR_nonatomic) {
1451-
Out << (first ? ' ' : ',') << "nonatomic";
1457+
ObjCPropertyDecl::OBJC_PR_readwrite) {
1458+
Out << (first ? "" : ", ") << "readwrite";
14521459
first = false;
14531460
}
14541461
if (PDecl->getPropertyAttributes() &
1455-
ObjCPropertyDecl::OBJC_PR_atomic) {
1456-
Out << (first ? ' ' : ',') << "atomic";
1462+
ObjCPropertyDecl::OBJC_PR_readonly) {
1463+
Out << (first ? "" : ", ") << "readonly";
1464+
first = false;
1465+
}
1466+
1467+
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1468+
Out << (first ? "" : ", ") << "getter = ";
1469+
PDecl->getGetterName().print(Out);
1470+
first = false;
1471+
}
1472+
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1473+
Out << (first ? "" : ", ") << "setter = ";
1474+
PDecl->getSetterName().print(Out);
14571475
first = false;
14581476
}
14591477

@@ -1463,25 +1481,24 @@ void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
14631481
if (*nullability == NullabilityKind::Unspecified &&
14641482
(PDecl->getPropertyAttributes() &
14651483
ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1466-
Out << (first ? ' ' : ',') << "null_resettable";
1484+
Out << (first ? "" : ", ") << "null_resettable";
14671485
} else {
1468-
Out << (first ? ' ' : ',')
1486+
Out << (first ? "" : ", ")
14691487
<< getNullabilitySpelling(*nullability, true);
14701488
}
14711489
first = false;
14721490
}
14731491
}
14741492

1475-
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1476-
Out << (first ? ' ' : ',') << "class";
1477-
first = false;
1478-
}
1479-
14801493
(void) first; // Silence dead store warning due to idiomatic code.
1481-
Out << " )";
1494+
Out << ")";
14821495
}
1483-
Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1484-
getAsString(Policy) << ' ' << *PDecl;
1496+
std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1497+
getAsString(Policy);
1498+
Out << ' ' << TypeStr;
1499+
if (!StringRef(TypeStr).endswith("*"))
1500+
Out << ' ';
1501+
Out << *PDecl;
14851502
if (Policy.PolishForDeclaration)
14861503
Out << ';';
14871504
}

Diff for: ‎clang/test/AST/ast-print-objc-property.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -ast-print %s -o - | FileCheck %s
2+
3+
@interface NSObject
4+
@end
5+
6+
@interface Properties : NSObject
7+
@property(class) int classFoo;
8+
@property(nonatomic) int atomicBar;
9+
@property(readonly) int readonlyConstant;
10+
@property(retain, nonatomic, setter=my_setter:, getter=my_getter) id __crazy_name;
11+
@property(nonatomic, strong, nullable) NSObject * objProperty;
12+
@property(nonatomic, weak, null_resettable) NSObject * weakObj;
13+
@property(nonatomic, copy, nonnull) NSObject * copyObj;
14+
@end
15+
16+
// CHECK: @property(class, atomic, assign, unsafe_unretained, readwrite) int classFoo;
17+
// CHECK: @property(nonatomic, assign, unsafe_unretained, readwrite) int atomicBar;
18+
// CHECK: @property(atomic, readonly) int readonlyConstant;
19+
// CHECK: @property(nonatomic, retain, readwrite, getter = my_getter, setter = my_setter:) id __crazy_name;
20+
// CHECK: @property(nonatomic, strong, readwrite, nullable) NSObject *objProperty;
21+
// CHECK: @property(nonatomic, weak, readwrite, null_resettable) NSObject *weakObj;
22+
// CHECK: @property(nonatomic, copy, readwrite, nonnull) NSObject *copyObj;

Diff for: ‎clang/test/Index/comment-objc-decls.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ - (unsigned int)MethodMyProto:(nullable id)anObject inRange:(unsigned int)range;
3232
@end
3333
// CHECK: <Declaration>@protocol MyProto\n@end</Declaration>
3434
// CHECK: <Declaration>- (unsigned int)MethodMyProto:(nullable id)anObject inRange:(unsigned int)range;</Declaration>
35-
// CHECK: <Declaration>@optional\n@property(readwrite, copy, atomic, nonnull) id PropertyMyProto;</Declaration>
35+
// CHECK: <Declaration>@optional\n@property(atomic, copy, readwrite, nonnull) id PropertyMyProto;</Declaration>
3636
// CHECK: <Declaration>+ (id)ClassMethodMyProto;</Declaration>
3737

3838
/**
@@ -77,7 +77,7 @@ @interface MyClass : NSObject<MyProto>
7777
// CHECK: <Declaration>id IvarMyClass</Declaration>
7878
// CHECK: <Declaration>- (id)MethodMyClass;</Declaration>
7979
// CHECK: <Declaration>+ (id)ClassMethodMyClass;</Declaration>
80-
// CHECK: <Declaration>@property(readwrite, copy, atomic) id PropertyMyClass;</Declaration
80+
// CHECK: <Declaration>@property(atomic, copy, readwrite) id PropertyMyClass;</Declaration
8181

8282
/**
8383
* \brief - This is class extension of MyClass
@@ -110,7 +110,7 @@ - (void)MethodMyClassCategory;
110110
@end
111111
// CHECK: <Declaration>@interface MyClass (Category)\n@end</Declaration>
112112
// CHECK: <Declaration>- (void)MethodMyClassCategory;</Declaration>
113-
// CHECK: <Declaration>@property(readwrite, copy, atomic) id PropertyMyClassCategory;</Declaration>
113+
// CHECK: <Declaration>@property(atomic, copy, readwrite) id PropertyMyClassCategory;</Declaration>
114114
// CHECK: <Declaration>- (id)PropertyMyClassCategory;</Declaration>
115115
// CHECK: <Declaration>- (void)setPropertyMyClassCategory:(id)arg;</Declaration>
116116

Diff for: ‎clang/test/Index/comment-unqualified-objc-pointer.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ - (NSString *)WithLabel:(NSString * const)label;
1919

2020
//! This is a property to get the Name.
2121
@property (copy) NSString *Name;
22-
// CHECK: <Declaration>@property(readwrite, copy, atomic) NSString *Name;</Declaration>
22+
// CHECK: <Declaration>@property(atomic, copy, readwrite) NSString *Name;</Declaration>
2323
@end
2424

2525
@implementation NSMutableArray

Diff for: ‎clang/test/PCH/chain-remap-types.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// CHECK: @class X;
88
// CHECK: struct Y
9-
// CHECK: @property ( assign,readwrite,atomic ) X * prop
9+
// CHECK: @property(atomic, assign, unsafe_unretained, readwrite) X *prop
1010
// CHECK: void h(X *);
1111
// CHECK: @interface X(Blah)
1212
// CHECK: void g(X *);

0 commit comments

Comments
 (0)
Please sign in to comment.