2016-08-31 07:07:14 +08:00
|
|
|
// RUN: %clang_cc1 -fblocks -x objective-c-header -emit-pch -o %t.pch %S/Inputs/localization-pch.h
|
|
|
|
|
2018-11-02 23:48:10 +08:00
|
|
|
// 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
|
2015-08-15 04:22:22 +08:00
|
|
|
|
|
|
|
// 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
|
2015-09-24 05:43:21 +08:00
|
|
|
@protocol UIAccessibility
|
2015-09-23 08:17:52 +08:00
|
|
|
- (void)accessibilitySetIdentification:(NSString *)ident;
|
2015-09-24 05:43:21 +08:00
|
|
|
- (void)setAccessibilityLabel:(NSString *)label;
|
|
|
|
@end
|
|
|
|
@interface UILabel : NSObject <UIAccessibility>
|
|
|
|
@property(nullable, nonatomic, copy) NSString *text;
|
2015-08-15 04:22:22 +08:00
|
|
|
@end
|
|
|
|
@interface TestObject : NSObject
|
|
|
|
@property(strong) NSString *text;
|
|
|
|
@end
|
2015-09-24 05:43:21 +08:00
|
|
|
@interface NSView : NSObject
|
|
|
|
@property (strong) NSString *toolTip;
|
|
|
|
@end
|
|
|
|
@interface NSViewSubclass : NSView
|
|
|
|
@end
|
2015-08-15 04:22:22 +08:00
|
|
|
|
|
|
|
@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")));
|
2017-07-14 18:24:36 +08:00
|
|
|
+ (NSString *)takesLocalizedString:
|
|
|
|
(NSString *)__attribute__((annotate("takes_localized_nsstring")))str;
|
2015-08-15 04:22:22 +08:00
|
|
|
@end
|
|
|
|
|
2017-07-14 18:24:36 +08:00
|
|
|
NSString *
|
|
|
|
takesLocalizedString(NSString *str
|
|
|
|
__attribute__((annotate("takes_localized_nsstring")))) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-08-15 04:22:22 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:24:36 +08:00
|
|
|
+ (NSString *) takesLocalizedString:(NSString *)str { return str; }
|
|
|
|
|
2015-08-15 04:22:22 +08:00
|
|
|
// 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";
|
|
|
|
}
|
|
|
|
|
2015-09-24 05:43:21 +08:00
|
|
|
[testLabel setText:bar]; // expected-warning {{User-facing text should use localized string macro}}
|
2015-08-15 04:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testLocalizationErrorDetectedOnNSString {
|
|
|
|
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
|
|
|
|
|
|
|
|
if (random()) {
|
|
|
|
bar = @"Unlocalized string";
|
|
|
|
}
|
|
|
|
|
2015-09-24 05:43:21 +08:00
|
|
|
[bar drawAtPoint:CGPointMake(0, 0) withAttributes:nil]; // expected-warning {{User-facing text should use localized string macro}}
|
2015-08-15 04:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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];
|
2015-09-24 05:43:21 +08:00
|
|
|
[testLabel setText:@"Hello"]; // expected-warning {{User-facing text should use localized string macro}}
|
2015-08-15 04:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
2015-09-24 05:43:21 +08:00
|
|
|
[testLabel setText:argumentString]; // expected-warning {{User-facing text should use localized string macro}}
|
2015-08-15 04:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// [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");
|
|
|
|
|
2015-09-24 05:43:21 +08:00
|
|
|
[testLabel setText:[LocalizationTestSuite unLocalizedStringMethod]]; // expected-warning {{User-facing text should use localized string macro}}
|
2015-08-15 04:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-09-24 05:43:21 +08:00
|
|
|
// 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}}
|
|
|
|
}
|
|
|
|
|
2015-08-15 04:22:22 +08:00
|
|
|
// 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}}
|
|
|
|
}
|
|
|
|
|
2016-08-31 07:07:14 +08:00
|
|
|
- (void)testMacroExpansionDefinedInPCH {
|
|
|
|
NSString *string = MyLocalizedStringInPCH(@"Hello"); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
|
|
|
|
}
|
|
|
|
|
2015-08-15 04:22:22 +08:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:24:36 +08:00
|
|
|
- (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}}
|
|
|
|
}
|
2015-08-15 04:22:22 +08:00
|
|
|
@end
|
2019-12-12 03:03:38 +08:00
|
|
|
|
|
|
|
@interface SynthesizedAccessors : NSObject
|
|
|
|
@property (assign) NSObject *obj;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SynthesizedAccessors
|
|
|
|
// no-crash
|
|
|
|
@end
|