Skip to content

Commit

Permalink
Fix check for AppleClang in src/native/libs and fix warnings
Browse files Browse the repository at this point in the history
While looking at dotnet#101781 I noticed that we weren't catching AppleClang in the CMAKE_C_COMPILER_ID in src/native/libs/CMakeLists.txt because we used  STREQUAL instead of MATCHES like we do everywhere else.

The result was that we didn't enable `-Weverything` on Apple platforms for the libs.native subset and a bunch of warnings didn't get enabled, this PR fixes that.
  • Loading branch information
akoeplinger committed May 4, 2024
1 parent 15e98e5 commit 8a5ed08
Show file tree
Hide file tree
Showing 35 changed files with 183 additions and 154 deletions.
2 changes: 1 addition & 1 deletion src/native/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI)
add_subdirectory(System.IO.Ports.Native)
endif ()

if (CMAKE_C_COMPILER_ID STREQUAL Clang)
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
add_compile_options(-Weverything)
add_compile_options(-Wno-format-nonliteral)
add_compile_options(-Wno-disabled-macro-expansion)
Expand Down
25 changes: 13 additions & 12 deletions src/native/libs/System.Globalization.Native/pal_calendarData.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static CalendarId GetCalendarId(const char* calendarName)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSString *locName = [[NSString alloc] initWithUTF8String:localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];

if (dataType == CalendarData_MonthDay)
Expand Down Expand Up @@ -162,8 +162,8 @@ static CalendarId GetCalendarId(const char* calendarName)
assert(false);
return NULL;
}

NSString *arrayToString = [[result valueForKey:@"description"] componentsJoinedByString:@"||"];
NSArray *descriptionsArray = [result valueForKey:@"description"];
NSString *arrayToString = [descriptionsArray componentsJoinedByString:@"||"];
return arrayToString ? strdup([arrayToString UTF8String]) : NULL;
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ int32_t GlobalizationNative_GetJapaneseEraStartDateNative(int32_t era, int32_t*
for (int month = 0; month <= 12; month++)
{
NSDateComponents *eraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:date];
currentEra = [eraComponents era];
currentEra = (int32_t)[eraComponents era];
if (currentEra == era)
{
for (int day = 0; day < 31; day++)
Expand All @@ -220,17 +220,17 @@ int32_t GlobalizationNative_GetJapaneseEraStartDateNative(int32_t era, int32_t*
startDateComponents.day = startDateComponents.day - 1;
date = [japaneseCalendar dateFromComponents:startDateComponents];
eraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:date];
currentEra = [eraComponents era];
currentEra = (int32_t)[eraComponents era];
if (currentEra != era)
{
// add back 1 day to get back into the specified Era
startDateComponents.day = startDateComponents.day + 1;
date = [japaneseCalendar dateFromComponents:startDateComponents];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
*startYear = [components year];
*startMonth = [components month];
*startDay = [components day];
*startYear = (int32_t)[components year];
*startMonth = (int32_t)[components month];
*startDay = (int32_t)[components day];
return 1;
}
}
Expand All @@ -239,7 +239,7 @@ int32_t GlobalizationNative_GetJapaneseEraStartDateNative(int32_t era, int32_t*
startDateComponents.month = startDateComponents.month + 1;
date = [japaneseCalendar dateFromComponents:startDateComponents];
eraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:date];
currentEra = [eraComponents era];
currentEra = (int32_t)[eraComponents era];
}

return 0;
Expand Down Expand Up @@ -267,17 +267,18 @@ int32_t GlobalizationNative_GetCalendarsNative(const char* localeName, CalendarI
NSCalendarIdentifierRepublicOfChina,
];

NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSString *locName = [[NSString alloc] initWithUTF8String:localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSString *defaultCalendarIdentifier = [currentLocale calendarIdentifier];
int32_t calendarCount = MIN(calendarIdentifiers.count, calendarsCapacity);
int32_t calendarCount = (int32_t)calendarIdentifiers.count < calendarsCapacity ? (int32_t)calendarIdentifiers.count : calendarsCapacity;
int32_t calendarIndex = 0;
CalendarId defaultCalendarId = GetCalendarId([defaultCalendarIdentifier UTF8String]);
// If the default calendar is not supported, return the Gregorian calendar as the default.
calendars[calendarIndex++] = defaultCalendarId == UNINITIALIZED_VALUE ? GREGORIAN : defaultCalendarId;
for (int i = 0; i < calendarCount; i++)
{
CalendarId calendarId = GetCalendarId([calendarIdentifiers[i] UTF8String]);
NSCalendarIdentifier calendarIdentifier = calendarIdentifiers[(NSUInteger)i];
CalendarId calendarId = GetCalendarId([calendarIdentifier UTF8String]);
if (calendarId == UNINITIALIZED_VALUE || calendarId == defaultCalendarId)
continue;
calendars[calendarIndex++] = calendarId;
Expand Down
10 changes: 5 additions & 5 deletions src/native/libs/System.Globalization.Native/pal_casing.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int32_t GlobalizationNative_ChangeCaseNative(const uint16_t* localeName, int32_t
}
else
{
NSString *locName = [NSString stringWithCharacters: localeName length: lNameLength];
NSString *locName = [NSString stringWithCharacters: localeName length: (NSUInteger)lNameLength];
currentLocale = [NSLocale localeWithLocaleIdentifier:locName];
}

Expand All @@ -89,14 +89,14 @@ int32_t GlobalizationNative_ChangeCaseNative(const uint16_t* localeName, int32_t
int32_t startIndex = srcIdx;
NEXTOFFSET(lpSrc, srcIdx, cwSrcLength);
int32_t srcLength = srcIdx - startIndex;
NSString *src = [NSString stringWithCharacters: lpSrc + startIndex length: srcLength];
NSString *src = [NSString stringWithCharacters: lpSrc + startIndex length: (NSUInteger)srcLength];
NSString *dst = bToUpper ? [src uppercaseStringWithLocale:currentLocale] : [src lowercaseStringWithLocale:currentLocale];
int32_t index = 0;
// iterate over all code points of a surrogate pair character
while (index < srcLength)
{
// the dst.length > srcLength is to prevent code point expansions
dstCodepoint = dst.length > srcLength ? [src characterAtIndex: index] : [dst characterAtIndex: index];
dstCodepoint = (int32_t)dst.length > srcLength ? [src characterAtIndex: (NSUInteger)index] : [dst characterAtIndex: (NSUInteger)index];
Append(lpDst, dstIdx, cwDstLength, dstCodepoint, isError);
index++;
}
Expand Down Expand Up @@ -130,14 +130,14 @@ int32_t GlobalizationNative_ChangeCaseInvariantNative(const uint16_t* lpSrc, int
int32_t startIndex = srcIdx;
NEXTOFFSET(lpSrc, srcIdx, cwSrcLength);
int32_t srcLength = srcIdx - startIndex;
NSString *src = [NSString stringWithCharacters: lpSrc + startIndex length: srcLength];
NSString *src = [NSString stringWithCharacters: lpSrc + startIndex length: (NSUInteger)srcLength];
NSString *dst = bToUpper ? src.uppercaseString : src.lowercaseString;
int32_t index = 0;
// iterate over all code points of a surrogate pair character
while (index < srcLength)
{
// the dst.length > srcLength is to prevent code point expansions
dstCodepoint = dst.length > srcLength ? [src characterAtIndex: index] : [dst characterAtIndex: index];
dstCodepoint = (int32_t)dst.length > srcLength ? [src characterAtIndex: (NSUInteger)index] : [dst characterAtIndex: (NSUInteger)index];
Append(lpDst, dstIdx, cwDstLength, dstCodepoint, isError);
index++;
}
Expand Down
43 changes: 22 additions & 21 deletions src/native/libs/System.Globalization.Native/pal_collation.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}
else
{
NSString *locName = [NSString stringWithCharacters: localeName length: lNameLength];
NSString *locName = [NSString stringWithCharacters: localeName length: (NSUInteger)lNameLength];
currentLocale = [NSLocale localeWithLocaleIdentifier:locName];
}
return currentLocale;
Expand Down Expand Up @@ -90,9 +90,9 @@ int32_t GlobalizationNative_CompareStringNative(const uint16_t* localeName, int3
if (!IsComparisonOptionSupported(comparisonOptions))
return ERROR_COMPARISON_OPTIONS_NOT_FOUND;
NSLocale *currentLocale = GetCurrentLocale(localeName, lNameLength);
NSString *sourceString = [NSString stringWithCharacters: lpSource length: cwSourceLength];
NSString *sourceString = [NSString stringWithCharacters: lpSource length: (NSUInteger)cwSourceLength];
NSString *sourceStrPrecomposed = sourceString.precomposedStringWithCanonicalMapping;
NSString *targetString = [NSString stringWithCharacters: lpTarget length: cwTargetLength];
NSString *targetString = [NSString stringWithCharacters: lpTarget length: (NSUInteger)cwTargetLength];
NSString *targetStrPrecomposed = targetString.precomposedStringWithCanonicalMapping;

if (comparisonOptions & IgnoreKanaType)
Expand All @@ -110,7 +110,7 @@ int32_t GlobalizationNative_CompareStringNative(const uint16_t* localeName, int3

NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions, true);
NSRange comparisonRange = NSMakeRange(0, sourceStrPrecomposed.length);
return [sourceStrPrecomposed compare:targetStrPrecomposed
return (int32_t)[sourceStrPrecomposed compare:targetStrPrecomposed
options:options
range:comparisonRange
locale:currentLocale];
Expand Down Expand Up @@ -158,9 +158,9 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam
return result;
}
NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions, true);
NSString *searchString = [NSString stringWithCharacters: lpTarget length: cwTargetLength];
NSString *searchString = [NSString stringWithCharacters: lpTarget length: (NSUInteger)cwTargetLength];
NSString *searchStrCleaned = RemoveWeightlessCharacters(searchString);
NSString *sourceString = [NSString stringWithCharacters: lpSource length: cwSourceLength];
NSString *sourceString = [NSString stringWithCharacters: lpSource length: (NSUInteger)cwSourceLength];
NSString *sourceStrCleaned = RemoveWeightlessCharacters(sourceString);
if (comparisonOptions & IgnoreKanaType)
{
Expand All @@ -170,7 +170,7 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam

if (sourceStrCleaned.length == 0 || searchStrCleaned.length == 0)
{
result.location = fromBeginning ? 0 : sourceString.length;
result.location = fromBeginning ? 0 : (int32_t)sourceString.length;
return result;
}

Expand Down Expand Up @@ -204,8 +204,8 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam

if (nsRange.location != NSNotFound)
{
result.location = nsRange.location;
result.length = nsRange.length;
result.location = (int32_t)nsRange.location;
result.length = (int32_t)nsRange.length;
// in case of CompareOptions.IgnoreCase if letters have different representations in source and search strings
// and case insensitive search appears more than one time in source string take last index for LastIndexOf and first index for IndexOf
// e.g. new CultureInfo().CompareInfo.LastIndexOf("Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?", "U\u0308", 25,18, CompareOptions.IgnoreCase);
Expand All @@ -230,8 +230,8 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam
if ((comparisonOptions & IgnoreCase) && IsIndexFound(fromBeginning, (int32_t)result.location, (int32_t)precomposedRange.location))
return result;

result.location = precomposedRange.location;
result.length = precomposedRange.length;
result.location = (int32_t)precomposedRange.location;
result.length = (int32_t)precomposedRange.length;
if (!(comparisonOptions & IgnoreCase))
return result;
}
Expand All @@ -249,8 +249,8 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam
if ((comparisonOptions & IgnoreCase) && IsIndexFound(fromBeginning, (int32_t)result.location, (int32_t)decomposedRange.location))
return result;

result.location = decomposedRange.location;
result.length = decomposedRange.length;
result.location = (int32_t)decomposedRange.location;
result.length = (int32_t)decomposedRange.length;
return result;
}

Expand All @@ -270,9 +270,9 @@ int32_t GlobalizationNative_StartsWithNative(const uint16_t* localeName, int32_t
return ERROR_COMPARISON_OPTIONS_NOT_FOUND;
NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions, true);
NSLocale *currentLocale = GetCurrentLocale(localeName, lNameLength);
NSString *prefixString = [NSString stringWithCharacters: lpPrefix length: cwPrefixLength];
NSString *prefixString = [NSString stringWithCharacters: lpPrefix length: (NSUInteger)cwPrefixLength];
NSString *prefixStrComposed = RemoveWeightlessCharacters(prefixString.precomposedStringWithCanonicalMapping);
NSString *sourceString = [NSString stringWithCharacters: lpSource length: cwSourceLength];
NSString *sourceString = [NSString stringWithCharacters: lpSource length: (NSUInteger)cwSourceLength];
NSString *sourceStrComposed = RemoveWeightlessCharacters(sourceString.precomposedStringWithCanonicalMapping);
if (comparisonOptions & IgnoreKanaType)
{
Expand All @@ -282,7 +282,7 @@ int32_t GlobalizationNative_StartsWithNative(const uint16_t* localeName, int32_t

NSRange sourceRange = NSMakeRange(0, prefixStrComposed.length > sourceStrComposed.length ? sourceStrComposed.length : prefixStrComposed.length);

int32_t result = [sourceStrComposed compare:prefixStrComposed
int32_t result = (int32_t)[sourceStrComposed compare:prefixStrComposed
options:options
range:sourceRange
locale:currentLocale];
Expand All @@ -302,19 +302,19 @@ int32_t GlobalizationNative_EndsWithNative(const uint16_t* localeName, int32_t l
return ERROR_COMPARISON_OPTIONS_NOT_FOUND;
NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions, true);
NSLocale *currentLocale = GetCurrentLocale(localeName, lNameLength);
NSString *suffixString = [NSString stringWithCharacters: lpSuffix length: cwSuffixLength];
NSString *suffixString = [NSString stringWithCharacters: lpSuffix length: (NSUInteger)cwSuffixLength];
NSString *suffixStrComposed = RemoveWeightlessCharacters(suffixString.precomposedStringWithCanonicalMapping);
NSString *sourceString = [NSString stringWithCharacters: lpSource length: cwSourceLength];
NSString *sourceString = [NSString stringWithCharacters: lpSource length: (NSUInteger)cwSourceLength];
NSString *sourceStrComposed = RemoveWeightlessCharacters(sourceString.precomposedStringWithCanonicalMapping);
if (comparisonOptions & IgnoreKanaType)
{
suffixStrComposed = ConvertToKatakana(suffixStrComposed);
sourceStrComposed = ConvertToKatakana(sourceStrComposed);
}
int32_t startIndex = suffixStrComposed.length > sourceStrComposed.length ? 0 : sourceStrComposed.length - suffixStrComposed.length;
NSUInteger startIndex = suffixStrComposed.length > sourceStrComposed.length ? 0 : sourceStrComposed.length - suffixStrComposed.length;
NSRange sourceRange = NSMakeRange(startIndex, sourceStrComposed.length - startIndex);

int32_t result = [sourceStrComposed compare:suffixStrComposed
int32_t result = (int32_t)[sourceStrComposed compare:suffixStrComposed
options:options
range:sourceRange
locale:currentLocale];
Expand All @@ -334,7 +334,7 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t
}
if (!IsComparisonOptionSupported(options))
return 0;
NSString *sourceString = [NSString stringWithCharacters: lpStr length: cwStrLength];
NSString *sourceString = [NSString stringWithCharacters: lpStr length: (NSUInteger)cwStrLength];
if (options & IgnoreKanaType)
{
sourceString = ConvertToKatakana(sourceString);
Expand Down Expand Up @@ -363,6 +363,7 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t
if (result)
return (int32_t)usedLength;

(void)cbSortKeyLength; // ignore unused parameter
return 0;
}
}
Expand Down
Loading

0 comments on commit 8a5ed08

Please sign in to comment.