George Karpenkov | ead0162 | 2017-10-11 18:39:40 +0000 | [diff] [blame^] | 1 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s |
| 2 | |
| 3 | // Nullability of const string-like globals, testing |
| 4 | // NonnullStringConstantsChecker. |
| 5 | |
| 6 | void clang_analyzer_eval(bool); |
| 7 | |
| 8 | @class NSString; |
| 9 | typedef const struct __CFString *CFStringRef; |
| 10 | |
| 11 | // Global NSString* is non-null. |
| 12 | extern NSString *const StringConstGlobal; |
| 13 | void stringConstGlobal() { |
| 14 | clang_analyzer_eval(StringConstGlobal); // expected-warning{{TRUE}} |
| 15 | } |
| 16 | |
| 17 | // The logic does not apply to local variables though. |
| 18 | extern NSString *stringGetter(); |
| 19 | void stringConstLocal() { |
| 20 | NSString *const local = stringGetter(); |
| 21 | clang_analyzer_eval(local); // expected-warning{{UNKNOWN}} |
| 22 | } |
| 23 | |
| 24 | // Global const CFStringRef's are also assumed to be non-null. |
| 25 | extern const CFStringRef CFStringConstGlobal; |
| 26 | void cfStringCheckGlobal() { |
| 27 | clang_analyzer_eval(CFStringConstGlobal); // expected-warning{{TRUE}} |
| 28 | } |
| 29 | |
| 30 | // But only "const" ones. |
| 31 | extern CFStringRef CFStringNonConstGlobal; |
| 32 | void cfStringCheckMutableGlobal() { |
| 33 | clang_analyzer_eval(CFStringNonConstGlobal); // expected-warning{{UNKNOWN}} |
| 34 | } |
| 35 | |
| 36 | // char* const is also assumed to be non-null. |
| 37 | extern const char *const ConstCharStarConst; |
| 38 | void constCharStarCheckGlobal() { |
| 39 | clang_analyzer_eval(ConstCharStarConst); // expected-warning{{TRUE}} |
| 40 | } |
| 41 | |
| 42 | // Pointer value can be mutable. |
| 43 | extern char *const CharStarConst; |
| 44 | void charStarCheckGlobal() { |
| 45 | clang_analyzer_eval(CharStarConst); // expected-warning{{TRUE}} |
| 46 | } |
| 47 | |
| 48 | // But the pointer itself should be immutable. |
| 49 | extern char *CharStar; |
| 50 | void charStartCheckMutableGlobal() { |
| 51 | clang_analyzer_eval(CharStar); // expected-warning{{UNKNOWN}} |
| 52 | } |
| 53 | |
| 54 | // Type definitions should also work across typedefs, for pointers: |
| 55 | typedef char *const str; |
| 56 | extern str globalStr; |
| 57 | void charStarCheckTypedef() { |
| 58 | clang_analyzer_eval(globalStr); // expected-warning{{TRUE}} |
| 59 | } |
| 60 | |
| 61 | // And for types. |
| 62 | typedef NSString *const NStr; |
| 63 | extern NStr globalNSString; |
| 64 | void NSStringCheckTypedef() { |
| 65 | clang_analyzer_eval(globalNSString); // expected-warning{{TRUE}} |
| 66 | } |
| 67 | |
| 68 | // Note that constness could be either inside |
| 69 | // the var declaration, or in a typedef. |
| 70 | typedef NSString *NStr2; |
| 71 | extern const NStr2 globalNSString2; |
| 72 | void NSStringCheckConstTypedef() { |
| 73 | clang_analyzer_eval(globalNSString2); // expected-warning{{TRUE}} |
| 74 | } |
| 75 | |
| 76 | // Nested typedefs should work as well. |
| 77 | typedef const CFStringRef str1; |
| 78 | typedef str1 str2; |
| 79 | extern str2 globalStr2; |
| 80 | void testNestedTypedefs() { |
| 81 | clang_analyzer_eval(globalStr2); // expected-warning{{TRUE}} |
| 82 | } |
| 83 | |
| 84 | // And for NSString *. |
| 85 | typedef NSString *const nstr1; |
| 86 | typedef nstr1 nstr2; |
| 87 | extern nstr2 nglobalStr2; |
| 88 | void testNestedTypedefsForNSString() { |
| 89 | clang_analyzer_eval(nglobalStr2); // expected-warning{{TRUE}} |
| 90 | } |