xref: /llvm-project/clang/test/SemaObjC/format-arg-attribute.m (revision 12ab3e6c8402078f58959847277858eb47a43a19)
1// RUN: %clang_cc1 -verify -fsyntax-only %s
2
3@interface NSString
4+(instancetype)stringWithCString:(const char *)cstr __attribute__((format_arg(1)));
5-(instancetype)initWithString:(NSString *)str __attribute__((format_arg(1)));
6
7+(instancetype _Nonnull)nonNullableString:(NSString *)str __attribute__((format_arg(1)));
8+(instancetype _Nullable)nullableString:(NSString *)str __attribute__((format_arg(1)));
9@end
10
11@protocol MaybeString
12-(instancetype)maybeString:(const char *)cstr __attribute__((format_arg(1))); // expected-error {{function does not return string type}}
13@end
14
15@class NSAttributedString;
16
17extern NSString *fa2 (const NSString *) __attribute__((format_arg(1)));
18extern NSString *fa3 (NSString *) __attribute__((format_arg(1)));
19
20extern void fc1 (const NSString *) __attribute__((format_arg));  // expected-error {{'format_arg' attribute takes one argument}}
21extern void fc2 (const NSString *) __attribute__((format_arg())); // expected-error {{'format_arg' attribute takes one argument}}
22extern void fc3 (const NSString *) __attribute__((format_arg(1, 2))); // expected-error {{'format_arg' attribute takes one argument}}
23
24struct s1 { int i; } __attribute__((format_arg(1)));  // expected-warning {{'format_arg' attribute only applies to Objective-C methods and non-K&R-style functions}}
25union u1 { int i; } __attribute__((format_arg(1)));  // expected-warning {{'format_arg' attribute only applies to}}
26enum e1 { E1V0 } __attribute__((format_arg(1))); // expected-warning {{'format_arg' attribute only applies to}}
27
28extern NSString *ff3 (const NSString *) __attribute__((format_arg(3-2)));
29extern NSString *ff4 (const NSString *) __attribute__((format_arg(foo))); // expected-error {{use of undeclared identifier 'foo'}}
30
31/* format_arg formats must take and return a string.  */
32extern NSString *fi0 (int) __attribute__((format_arg(1)));  // expected-error {{format argument not a string type}}
33extern NSString *fi1 (NSString *) __attribute__((format_arg(1)));
34
35extern NSString *fi2 (NSString *) __attribute__((format_arg(1)));
36
37extern int fi3 (const NSString *) __attribute__((format_arg(1)));  // expected-error {{function does not return NSString}}
38extern NSString *fi4 (const NSString *) __attribute__((format_arg(1)));
39extern NSString *fi5 (const NSString *) __attribute__((format_arg(1)));
40
41extern NSAttributedString *fattrs (const NSString *) __attribute__((format_arg(1)));
42