diff options
| author | Akira Hatanaka <ahatanaka@apple.com> | 2019-03-08 04:45:37 +0000 |
|---|---|---|
| committer | Akira Hatanaka <ahatanaka@apple.com> | 2019-03-08 04:45:37 +0000 |
| commit | 1488ee4bd5418a5536856676c350c9ee64488334 (patch) | |
| tree | e8000965c5d7f7ab08b07c2600482d03b1436fd8 /clang/test/SemaObjC | |
| parent | 988332a54a705496b7a2e2bab5d032bf7ad2fce0 (diff) | |
| download | bcm5719-llvm-1488ee4bd5418a5536856676c350c9ee64488334.tar.gz bcm5719-llvm-1488ee4bd5418a5536856676c350c9ee64488334.zip | |
[ObjC] Emit a boxed expression as a compile-time constant if the
expression inside the parentheses is a valid UTF-8 string literal.
Previously clang emitted an expression like @("abc") as a message send
to stringWithUTF8String. This commit makes clang emit the boxed
expression as a compile-time constant instead.
This commit also has the effect of silencing the nullable-to-nonnull
conversion warning clang started emitting after r317727, which
originally motivated this commit (see https://oleb.net/2018/@keypath).
rdar://problem/42684601
Differential Revision: https://reviews.llvm.org/D58729
llvm-svn: 355662
Diffstat (limited to 'clang/test/SemaObjC')
| -rw-r--r-- | clang/test/SemaObjC/boxing-illegal.m | 15 | ||||
| -rw-r--r-- | clang/test/SemaObjC/objc-literal-sig.m | 6 | ||||
| -rw-r--r-- | clang/test/SemaObjC/transfer-boxed-string-nullability.m | 18 |
3 files changed, 32 insertions, 7 deletions
diff --git a/clang/test/SemaObjC/boxing-illegal.m b/clang/test/SemaObjC/boxing-illegal.m index 59b5c8b710d..329873ac6ad 100644 --- a/clang/test/SemaObjC/boxing-illegal.m +++ b/clang/test/SemaObjC/boxing-illegal.m @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -Wattributes %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wattributes -fpascal-strings %s typedef long NSInteger; typedef unsigned long NSUInteger; @@ -57,6 +57,19 @@ void testEnum(void *p) { box = @(*(enum ForwE*)p); // expected-error {{incomplete type 'enum ForwE' used in a boxed expression}} } +@interface NSString +@end + +void testStringLiteral() { + NSString *s; + s = @("abc"); + s = @(u8"abc"); + s = @(u"abc"); // expected-error {{illegal type 'unsigned short *' used in a boxed expression}} + s = @(U"abc"); // expected-error {{illegal type 'unsigned int *' used in a boxed expression}} + s = @(L"abc"); // expected-error {{illegal type 'int *' used in a boxed expression}} + s = @("\pabc"); // expected-error {{illegal type 'unsigned char *' used in a boxed expression}} +} + // rdar://13333205 @class NSMutableDictionary; diff --git a/clang/test/SemaObjC/objc-literal-sig.m b/clang/test/SemaObjC/objc-literal-sig.m index 86f42d3abca..48f69165efb 100644 --- a/clang/test/SemaObjC/objc-literal-sig.m +++ b/clang/test/SemaObjC/objc-literal-sig.m @@ -39,6 +39,8 @@ typedef _Bool BOOL; // All tests are doubled to make sure that a bad method is not saved // and then used un-checked. +const char *getStr(void); + void test_sig() { (void)@__objc_yes; // expected-error{{literal construction method 'numberWithBool:' has incompatible signature}} (void)@__objc_yes; // expected-error{{literal construction method 'numberWithBool:' has incompatible signature}} @@ -46,6 +48,6 @@ void test_sig() { id array2 = @[ @17 ]; // expected-error{{literal construction method 'arrayWithObjects:count:' has incompatible signature}} id dict = @{ @"hello" : @17 }; // expected-error{{literal construction method 'dictionaryWithObjects:forKeys:count:' has incompatible signature}} id dict2 = @{ @"hello" : @17 }; // expected-error{{literal construction method 'dictionaryWithObjects:forKeys:count:' has incompatible signature}} - id str = @("hello"); // expected-error{{literal construction method 'stringWithUTF8String:' has incompatible signature}} - id str2 = @("hello"); // expected-error{{literal construction method 'stringWithUTF8String:' has incompatible signature}} + id str = @(getStr()); // expected-error{{literal construction method 'stringWithUTF8String:' has incompatible signature}} + id str2 = @(getStr()); // expected-error{{literal construction method 'stringWithUTF8String:' has incompatible signature}} } diff --git a/clang/test/SemaObjC/transfer-boxed-string-nullability.m b/clang/test/SemaObjC/transfer-boxed-string-nullability.m index 42604ef4a6a..a3b6832be46 100644 --- a/clang/test/SemaObjC/transfer-boxed-string-nullability.m +++ b/clang/test/SemaObjC/transfer-boxed-string-nullability.m @@ -16,13 +16,23 @@ void takesNonNull(NSString * _Nonnull ptr); void testBoxedString() { + // No diagnostic emitted as this doesn't need a stringWithUTF8String message + // send. + takesNonNull(@("hey")); + takesNonNull(@(u8"hey")); + + // If the string isn't a valid UTF-8 string, a diagnostic is emitted since the + // boxed expression turns into a message send. + takesNonNull(@(u8"\xFF")); // expected-warning {{string is ill-formed as UTF-8}} + takesNonNull(@(u8"\xC0\x80")); // expected-warning {{string is ill-formed as UTF-8}} + const char *str = "hey"; takesNonNull([NSString stringWithUTF8String:str]); takesNonNull(@(str)); #ifndef NOWARN - // expected-warning@-3 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}} - // expected-warning@-3 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}} -#else - // expected-no-diagnostics + // expected-warning@-7 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}} + // expected-warning@-7 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}} + // expected-warning@-5 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}} + // expected-warning@-5 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}} #endif } |

