summaryrefslogtreecommitdiffstats
path: root/clang/test/Analysis/localization.m
diff options
context:
space:
mode:
authorAnna Zaks <ganna@apple.com>2015-08-14 20:22:22 +0000
committerAnna Zaks <ganna@apple.com>2015-08-14 20:22:22 +0000
commite5d74caf2a5826ba2fcf3449554b5d03b24c6cf1 (patch)
treeed17c9fc33f9fce417e8cb66be0d42fd8af25120 /clang/test/Analysis/localization.m
parent78a2e4720db3f31c0c0a0668b10e8f50d704a76c (diff)
downloadbcm5719-llvm-e5d74caf2a5826ba2fcf3449554b5d03b24c6cf1.tar.gz
bcm5719-llvm-e5d74caf2a5826ba2fcf3449554b5d03b24c6cf1.zip
[analyzer] Add checkers for OS X / iOS localizability issues
Add checkers that detect code-level localizability issues for OS X / iOS: - A path sensitive checker that warns about uses of non-localized NSStrings passed to UI methods expecting localized strings. - A syntax checker that warns against not including a comment in NSLocalizedString macros. A patch by Kulpreet Chilana! (This is the second attempt with the compilation issue on Windows and the random test failures resolved.) llvm-svn: 245093
Diffstat (limited to 'clang/test/Analysis/localization.m')
-rw-r--r--clang/test/Analysis/localization.m86
1 files changed, 86 insertions, 0 deletions
diff --git a/clang/test/Analysis/localization.m b/clang/test/Analysis/localization.m
new file mode 100644
index 00000000000..429b0d79747
--- /dev/null
+++ b/clang/test/Analysis/localization.m
@@ -0,0 +1,86 @@
+// RUN: %clang_cc1 -analyze -fblocks -analyzer-store=region -analyzer-checker=alpha.osx.cocoa.NonLocalizedStringChecker -analyzer-checker=alpha.osx.cocoa.EmptyLocalizationContextChecker -verify %s
+
+// The larger set of tests in located in localization.m. These are tests
+// specific for non-aggressive reporting.
+
+// 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)]
+@interface NSObject
++ (id)alloc;
+- (id)init;
+@end
+@interface NSString : NSObject
+@end
+@interface NSBundle : NSObject
++ (NSBundle *)mainBundle;
+- (NSString *)localizedStringForKey:(NSString *)key
+ value:(NSString *)value
+ table:(NSString *)tableName;
+@end
+@interface UILabel : NSObject
+@property(nullable, nonatomic, copy) NSString *text;
+@end
+@interface TestObject : NSObject
+@property(strong) NSString *text;
+@end
+
+@interface LocalizationTestSuite : NSObject
+int random();
+@end
+
+// Test cases begin here
+@implementation LocalizationTestSuite
+
+// An object passed in as an parameter's string member
+// should not be considered unlocalized
+- (void)testObjectAsArgument:(TestObject *)argumentObject {
+ UILabel *testLabel = [[UILabel alloc] init];
+
+ [testLabel setText:[argumentObject text]]; // no-warning
+ [testLabel setText:argumentObject.text]; // no-warning
+}
+
+- (void)testLocalizationErrorDetectedOnPathway {
+ UILabel *testLabel = [[UILabel alloc] init];
+ NSString *bar = NSLocalizedString(@"Hello", @"Comment");
+
+ if (random()) {
+ bar = @"Unlocalized string";
+ }
+
+ [testLabel setText:bar]; // expected-warning {{String should be localized}}
+}
+
+- (void)testOneCharacterStringsDoNotGiveAWarning {
+ UILabel *testLabel = [[UILabel alloc] init];
+ NSString *bar = NSLocalizedString(@"Hello", @"Comment");
+
+ if (random()) {
+ bar = @"-";
+ }
+
+ [testLabel setText:bar]; // no-warning
+}
+
+- (void)testOneCharacterUTFStringsDoNotGiveAWarning {
+ UILabel *testLabel = [[UILabel alloc] init];
+ NSString *bar = NSLocalizedString(@"Hello", @"Comment");
+
+ if (random()) {
+ bar = @"\u2014";
+ }
+
+ [testLabel setText:bar]; // no-warning
+}
+
+@end
OpenPOWER on IntegriCloud