1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// RUN: %clang_cc1 -fblocks -x objective-c-header -emit-pch -o %t.pch %S/Inputs/localization-pch.h
// RUN: %clang_analyze_cc1 -fblocks -analyzer-store=region \
// RUN: -analyzer-config optin.osx.cocoa.localizability.NonLocalizedStringChecker:AggressiveReport=true \
// RUN: -analyzer-checker=optin.osx.cocoa.localizability.NonLocalizedStringChecker \
// RUN: -analyzer-checker=optin.osx.cocoa.localizability.EmptyLocalizationContextChecker \
// RUN: -include-pch %t.pch -verify %s
// These declarations were reduced using Delta-Debugging from Foundation.h
// on Mac OS X.
#define nil ((id)0)
#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
[bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
[bundle localizedStringForKey:(key) value:(val) table:(tbl)]
#define CGFLOAT_TYPE double
typedef CGFLOAT_TYPE CGFloat;
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
@interface NSObject
+ (id)alloc;
- (id)init;
@end
@class NSDictionary;
@interface NSString : NSObject
- (void)drawAtPoint:(CGPoint)point withAttributes:(NSDictionary *)attrs;
+ (instancetype)localizedStringWithFormat:(NSString *)format, ...;
@end
@interface NSBundle : NSObject
+ (NSBundle *)mainBundle;
- (NSString *)localizedStringForKey:(NSString *)key
value:(NSString *)value
table:(NSString *)tableName;
@end
@protocol UIAccessibility
- (void)accessibilitySetIdentification:(NSString *)ident;
- (void)setAccessibilityLabel:(NSString *)label;
@end
@interface UILabel : NSObject <UIAccessibility>
@property(nullable, nonatomic, copy) NSString *text;
@end
@interface TestObject : NSObject
@property(strong) NSString *text;
@end
@interface NSView : NSObject
@property (strong) NSString *toolTip;
@end
@interface NSViewSubclass : NSView
@end
@interface LocalizationTestSuite : NSObject
NSString *ForceLocalized(NSString *str)
__attribute__((annotate("returns_localized_nsstring")));
CGPoint CGPointMake(CGFloat x, CGFloat y);
int random();
// This next one is a made up API
NSString *CFNumberFormatterCreateStringWithNumber(float x);
+ (NSString *)forceLocalized:(NSString *)str
__attribute__((annotate("returns_localized_nsstring")));
+ (NSString *)takesLocalizedString:
(NSString *)__attribute__((annotate("takes_localized_nsstring")))str;
@end
NSString *
takesLocalizedString(NSString *str
__attribute__((annotate("takes_localized_nsstring")))) {
return str;
}
// Test cases begin here
@implementation LocalizationTestSuite
// A C-Funtion that returns a localized string because it has the
// "returns_localized_nsstring" annotation
NSString *ForceLocalized(NSString *str) { return str; }
// An ObjC method that returns a localized string because it has the
// "returns_localized_nsstring" annotation
+ (NSString *)forceLocalized:(NSString *)str {
return str;
}
+ (NSString *) takesLocalizedString:(NSString *)str { return str; }
// An ObjC method that returns a localized string
+ (NSString *)unLocalizedStringMethod {
return @"UnlocalizedString";
}
- (void)testLocalizationErrorDetectedOnPathway {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[testLabel setText:bar]; // expected-warning {{User-facing text should use localized string macro}}
}
- (void)testLocalizationErrorDetectedOnNSString {
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[bar drawAtPoint:CGPointMake(0, 0) withAttributes:nil]; // expected-warning {{User-facing text should use localized string macro}}
}
- (void)testNoLocalizationErrorDetectedFromCFunction {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = CFNumberFormatterCreateStringWithNumber(1);
[testLabel setText:bar]; // no-warning
}
- (void)testAnnotationAddsLocalizedStateForCFunction {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[testLabel setText:ForceLocalized(bar)]; // no-warning
}
- (void)testAnnotationAddsLocalizedStateForObjCMethod {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[testLabel setText:[LocalizationTestSuite forceLocalized:bar]]; // no-warning
}
// An empty string literal @"" should not raise an error
- (void)testEmptyStringLiteralHasLocalizedState {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = @"";
[testLabel setText:bar]; // no-warning
}
// An empty string literal @"" inline should not raise an error
- (void)testInlineEmptyStringLiteralHasLocalizedState {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:@""]; // no-warning
}
// An string literal @"Hello" inline should raise an error
- (void)testInlineStringLiteralHasLocalizedState {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:@"Hello"]; // expected-warning {{User-facing text should use localized string macro}}
}
// A nil string should not raise an error
- (void)testNilStringIsNotMarkedAsUnlocalized {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:nil]; // no-warning
}
// A method that takes in a localized string and returns a string
// most likely that string is localized.
- (void)testLocalizedStringArgument {
UILabel *testLabel = [[UILabel alloc] init];
NSString *localizedString = NSLocalizedString(@"Hello", @"Comment");
NSString *combinedString =
[NSString localizedStringWithFormat:@"%@", localizedString];
[testLabel setText:combinedString]; // no-warning
}
// A String passed in as a an parameter should not be considered
// unlocalized
- (void)testLocalizedStringAsArgument:(NSString *)argumentString {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:argumentString]; // no-warning
}
// The warning is expected to be seen in localizedStringAsArgument: body
- (void)testLocalizedStringAsArgumentOtherMethod:(NSString *)argumentString {
[self localizedStringAsArgument:@"UnlocalizedString"];
}
// A String passed into another method that calls a method that
// requires a localized string should give an error
- (void)localizedStringAsArgument:(NSString *)argumentString {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:argumentString]; // expected-warning {{User-facing text should use localized string macro}}
}
// [LocalizationTestSuite unLocalizedStringMethod] returns an unlocalized string
// so we expect an error. Unfrtunately, it probably doesn't make a difference
// what [LocalizationTestSuite unLocalizedStringMethod] returns since all
// string values returned are marked as Unlocalized in aggressive reporting.
- (void)testUnLocalizedStringMethod {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
[testLabel setText:[LocalizationTestSuite unLocalizedStringMethod]]; // expected-warning {{User-facing text should use localized string macro}}
}
// This is the reverse situation: accessibilitySetIdentification: doesn't care
// about localization so we don't expect a warning
- (void)testMethodNotInRequiresLocalizedStringMethods {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel accessibilitySetIdentification:@"UnlocalizedString"]; // no-warning
}
// An NSView subclass should raise a warning for methods in NSView that
// require localized strings
- (void)testRequiresLocalizationMethodFromSuperclass {
NSViewSubclass *s = [[NSViewSubclass alloc] init];
NSString *bar = @"UnlocalizedString";
[s setToolTip:bar]; // expected-warning {{User-facing text should use localized string macro}}
}
- (void)testRequiresLocalizationMethodFromProtocol {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setAccessibilityLabel:@"UnlocalizedString"]; // expected-warning {{User-facing text should use localized string macro}}
}
// EmptyLocalizationContextChecker tests
#define HOM(s) YOLOC(s)
#define YOLOC(x) NSLocalizedString(x, nil)
- (void)testNilLocalizationContext {
NSString *string = NSLocalizedString(@"LocalizedString", nil); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string2 = NSLocalizedString(@"LocalizedString", nil); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string3 = NSLocalizedString(@"LocalizedString", nil); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
}
- (void)testEmptyLocalizationContext {
NSString *string = NSLocalizedString(@"LocalizedString", @""); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string2 = NSLocalizedString(@"LocalizedString", @" "); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string3 = NSLocalizedString(@"LocalizedString", @" "); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
}
- (void)testNSLocalizedStringVariants {
NSString *string = NSLocalizedStringFromTable(@"LocalizedString", nil, @""); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string2 = NSLocalizedStringFromTableInBundle(@"LocalizedString", nil, [[NSBundle alloc] init],@""); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string3 = NSLocalizedStringWithDefaultValue(@"LocalizedString", nil, [[NSBundle alloc] init], nil,@""); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
}
- (void)testMacroExpansionNilString {
NSString *string = YOLOC(@"Hello"); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string2 = HOM(@"Hello"); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
NSString *string3 = NSLocalizedString((0 ? @"Critical" : @"Current"),nil); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
}
- (void)testMacroExpansionDefinedInPCH {
NSString *string = MyLocalizedStringInPCH(@"Hello"); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
}
#define KCLocalizedString(x,comment) NSLocalizedString(x, comment)
#define POSSIBLE_FALSE_POSITIVE(s,other) KCLocalizedString(s,@"Comment")
- (void)testNoWarningForNilCommentPassedIntoOtherMacro {
NSString *string = KCLocalizedString(@"Hello",@""); // no-warning
NSString *string2 = KCLocalizedString(@"Hello",nil); // no-warning
NSString *string3 = KCLocalizedString(@"Hello",@"Comment"); // no-warning
}
- (void)testPossibleFalsePositiveSituationAbove {
NSString *string = POSSIBLE_FALSE_POSITIVE(@"Hello", nil); // no-warning
NSString *string2 = POSSIBLE_FALSE_POSITIVE(@"Hello", @"Hello"); // no-warning
}
- (void)testTakesLocalizedString {
NSString *localized = NSLocalizedString(@"Hello", @"World");
NSString *alsoLocalized = [LocalizationTestSuite takesLocalizedString:localized]; // no-warning
NSString *stillLocalized = [LocalizationTestSuite takesLocalizedString:alsoLocalized]; // no-warning
takesLocalizedString(stillLocalized); // no-warning
[LocalizationTestSuite takesLocalizedString:@"not localized"]; // expected-warning {{User-facing text should use localized string macro}}
takesLocalizedString(@"not localized"); // expected-warning {{User-facing text should use localized string macro}}
}
@end
@interface SynthesizedAccessors : NSObject
@property (assign) NSObject *obj;
@end
@implementation SynthesizedAccessors
// no-crash
@end
|