Skip to content
This repository has been archived by the owner on Jan 10, 2020. It is now read-only.

Commit

Permalink
Fixes boredzo#65: Add nullability annotations, and fix issues caused …
Browse files Browse the repository at this point in the history
…thereby.

Also turns on some static analyzer settings and the analyzer itself.
  • Loading branch information
boredzo committed Apr 17, 2016
1 parent 0a19dd4 commit 40ba01d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
30 changes: 19 additions & 11 deletions ISO8601DateFormatter.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/*ISO8601DateFormatter.h
*
*Created by Peter Hosey on 2009-04-11.
*Copyright 2009–2013 Peter Hosey. All rights reserved.
*Copyright 2009–2016 Peter Hosey. All rights reserved.
*/

#import <Foundation/Foundation.h>

#if __has_feature(nullability)
#define ISO8601_NULLABLE _Nullable
#define ISO8601_NONNULL _Nonnull
#else
#define ISO8601_NULLABLE /*can be NULL*/
#define ISO8601_NONNULL /*never NULL*/
#endif

///Which of ISO 8601's three date formats the formatter should produce.
typedef NS_ENUM(NSUInteger, ISO8601DateFormat) {
///YYYY-MM-DD.
Expand Down Expand Up @@ -53,7 +61,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
BOOL parsesStrictly;
}

@property(nonatomic, retain) NSTimeZone *defaultTimeZone;
@property(nonatomic, retain) NSTimeZone *ISO8601_NULLABLE defaultTimeZone;

#pragma mark Parsing
/*!
Expand Down Expand Up @@ -92,7 +100,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @sa dateComponentsFromString:timeZone:
* @sa dateComponentsFromString:timeZone:range:fractionOfSecond:
*/
- (NSDateComponents *) dateComponentsFromString:(NSString *)string;
- (NSDateComponents *ISO8601_NULLABLE) dateComponentsFromString:(NSString *ISO8601_NONNULL)string;
/*!
* @brief Parse a string into individual date components.
*
Expand All @@ -102,7 +110,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @sa dateComponentsFromString:
* @sa dateComponentsFromString:timeZone:range:fractionOfSecond:
*/
- (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone;
- (NSDateComponents *ISO8601_NULLABLE) dateComponentsFromString:(NSString *ISO8601_NONNULL)string timeZone:(out NSTimeZone *ISO8601_NULLABLE *ISO8601_NULLABLE)outTimeZone;
/*!
* @brief Parse a string into individual date components.
*
Expand All @@ -114,7 +122,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @sa dateComponentsFromString:
* @sa dateComponentsFromString:timeZone:
*/
- (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone range:(out NSRange *)outRange fractionOfSecond:(NSTimeInterval *)outFractionOfSecond;
- (NSDateComponents *ISO8601_NULLABLE) dateComponentsFromString:(NSString *ISO8601_NONNULL)string timeZone:(out NSTimeZone *ISO8601_NULLABLE *ISO8601_NULLABLE)outTimeZone range:(out NSRange *ISO8601_NULLABLE)outRange fractionOfSecond:(NSTimeInterval *ISO8601_NULLABLE)outFractionOfSecond;

/*!
* @brief Parse a string.
Expand All @@ -126,7 +134,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @sa dateFromString:timeZone:
* @sa dateFromString:timeZone:range:
*/
- (NSDate *) dateFromString:(NSString *)string;
- (NSDate *ISO8601_NULLABLE) dateFromString:(NSString *ISO8601_NONNULL)string;
/*!
* @brief Parse a string.
*
Expand All @@ -138,7 +146,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @sa dateFromString:
* @sa dateFromString:timeZone:range:
*/
- (NSDate *) dateFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone;
- (NSDate *ISO8601_NULLABLE) dateFromString:(NSString *ISO8601_NONNULL)string timeZone:(out NSTimeZone *ISO8601_NULLABLE *ISO8601_NULLABLE)outTimeZone;
/*!
* @brief Parse a string into a single date, identified by an NSDate object.
*
Expand All @@ -151,7 +159,7 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @sa dateFromString:
* @sa dateFromString:timeZone:
*/
- (NSDate *) dateFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone range:(out NSRange *)outRange;
- (NSDate *ISO8601_NULLABLE) dateFromString:(NSString *ISO8601_NONNULL)string timeZone:(out NSTimeZone *ISO8601_NULLABLE *ISO8601_NULLABLE)outTimeZone range:(out NSRange *ISO8601_NULLABLE)outRange;

#pragma mark Unparsing
/*!
Expand Down Expand Up @@ -211,10 +219,10 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
* @brief Produce a string that represents a date in UTC.
*
* @param date The string to parse. Must represent a date in one of the ISO 8601 formats.
* @result A string that represents the date in UTC.
* @result A string that represents the date in UTC. Can be `nil` in some cases; see `stringFromDate:timeZone:` for details.
* @sa stringFromDate:timeZone:
*/
- (NSString *) stringFromDate:(NSDate *)date;
- (NSString *ISO8601_NULLABLE) stringFromDate:(NSDate *ISO8601_NONNULL)date;
/*!
* @brief Produce a string that represents a date.
*
Expand All @@ -228,6 +236,6 @@ extern const unichar ISO8601DefaultTimeSeparatorCharacter;
*
* @sa stringFromDate:
*/
- (NSString *) stringFromDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone;
- (NSString *ISO8601_NULLABLE) stringFromDate:(NSDate *ISO8601_NONNULL)date timeZone:(NSTimeZone *ISO8601_NONNULL)timeZone;

@end
9 changes: 6 additions & 3 deletions ISO8601DateFormatter.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*ISO8601DateFormatter.m
*
*Created by Peter Hosey on 2009-04-11.
*Copyright 2009–2013 Peter Hosey. All rights reserved.
*Copyright 2009–2016 Peter Hosey. All rights reserved.
*/

#import <float.h>
Expand Down Expand Up @@ -737,6 +737,9 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
unparsingFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
}

NSTimeZone *_Nonnull const savedCalendarTimeZone = unparsingCalendar.timeZone;
NSTimeZone *_Nonnull const savedFormatterTimeZone = unparsingFormatter.timeZone;

unparsingCalendar.timeZone = timeZone;
unparsingFormatter.timeZone = timeZone;
NSString *str = [unparsingFormatter stringForObjectValue:date];
Expand All @@ -762,8 +765,8 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
}

//Undo the change we made earlier
unparsingCalendar.timeZone = self.defaultTimeZone;
unparsingFormatter.timeZone = self.defaultTimeZone;
unparsingCalendar.timeZone = self.defaultTimeZone ?: savedCalendarTimeZone;
unparsingFormatter.timeZone = self.defaultTimeZone ?: savedFormatterTimeZone;

return str;
}
Expand Down
12 changes: 12 additions & 0 deletions ISO8601ForCocoa/ISO8601ForCocoa.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,11 @@
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD)";
"ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD_64_BIT)";
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_STRCPY = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
Expand All @@ -748,6 +753,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
ONLY_ACTIVE_ARCH = YES;
RUN_CLANG_STATIC_ANALYZER = YES;
};
name = Debug;
};
Expand All @@ -757,6 +763,11 @@
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD)";
"ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD_64_BIT)";
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_STRCPY = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
Expand All @@ -774,6 +785,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
RUN_CLANG_STATIC_ANALYZER = YES;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,18 @@ - (void) testStrictModeRejectsSlashyDates {
}

- (void) testParseNilIntoDateComponents {
NSDateComponents *components = [_iso8601DateFormatter dateComponentsFromString:nil];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
NSDateComponents *_Nullable const components = [_iso8601DateFormatter dateComponentsFromString:nil];
#pragma clang diagnostic pop
XCTAssertNil(components, @"dateComponentsFromString:nil should have returned nil, but returned %@", components);
}

- (void) testParseNilIntoDate {
NSDate *date = [_iso8601DateFormatter dateFromString:nil];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
NSDate *_Nullable const date = [_iso8601DateFormatter dateFromString:nil];
#pragma clang diagnostic pop
XCTAssertNil(date, @"dateFromString:nil returned should have returned nil, but returned %@", date);
}

Expand Down

0 comments on commit 40ba01d

Please sign in to comment.