summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsdefresne <sdefresne@chromium.org>2015-02-25 08:32:19 -0800
committerCommit bot <commit-bot@chromium.org>2015-02-25 16:32:45 +0000
commit85b90ad128c95c9328f1ef883818908f910b7fd5 (patch)
tree118ef36deda287e2202d2eb06f05141569312752
parentbdf542901eb439619dfc221fda500d4876fad3f4 (diff)
downloadchromium_src-85b90ad128c95c9328f1ef883818908f910b7fd5.zip
chromium_src-85b90ad128c95c9328f1ef883818908f910b7fd5.tar.gz
chromium_src-85b90ad128c95c9328f1ef883818908f910b7fd5.tar.bz2
Fix comparison of forward-declared and fully visible types
Patch in 65ee89744bc1fbb9461f26d83e08243068cb212b that fixes comparison between opaque and non-opaque type (i.e. whether or not the contents of the structure is known at the point of compilation). https://github.com/carllindberg/ocmock/commit/65ee89744bc1fbb9461f26d83e08243068cb212b BUG=459013 Review URL: https://codereview.chromium.org/958613003 Cr-Commit-Position: refs/heads/master@{#318059}
-rw-r--r--third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.m228
-rw-r--r--third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.mm145
-rw-r--r--third_party/ocmock/README.chromium7
-rw-r--r--third_party/ocmock/ocmock.gyp2
4 files changed, 231 insertions, 151 deletions
diff --git a/third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.m b/third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.m
new file mode 100644
index 0000000..c316b4c
--- /dev/null
+++ b/third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.m
@@ -0,0 +1,228 @@
+//---------------------------------------------------------------------------------------
+// $Id$
+// Copyright (c) 2009 by Mulle Kybernetik. See License file for details.
+//---------------------------------------------------------------------------------------
+
+#import "OCMBoxedReturnValueProvider.h"
+
+static const char* OCMTypeWithoutQualifiers(const char* objCType)
+{
+ while(strchr("rnNoORV", objCType[0]) != NULL)
+ objCType += 1;
+ return objCType;
+}
+
+/*
+ * Sometimes an external type is an opaque struct (which will have an @encode of "{structName}"
+ * or "{structName=}") but the actual method return type, or property type, will know the contents
+ * of the struct (so will have an objcType of say "{structName=iiSS}". This function will determine
+ * those are equal provided they have the same structure name, otherwise everything else will be
+ * compared textually. This can happen particularly for pointers to such structures, which still
+ * encode what is being pointed to.
+ */
+static BOOL OCMTypesEqualAllowOpaqueStructs(const char *type1, const char *type2)
+{
+ type1 = OCMTypeWithoutQualifiers(type1);
+ type2 = OCMTypeWithoutQualifiers(type2);
+
+ switch (type1[0])
+ {
+ case '{':
+ case '(':
+ {
+ if (type2[0] != type1[0])
+ return NO;
+ char endChar = type1[0] == '{'? '}' : ')';
+
+ const char *type1End = strchr(type1, endChar);
+ const char *type2End = strchr(type2, endChar);
+ const char *type1Equals = strchr(type1, '=');
+ const char *type2Equals = strchr(type2, '=');
+
+ /* Opaque types either don't have an equals sign (just the name and the end brace), or
+ * empty content after the equals sign.
+ * We want that to compare the same as a type of the same name but with the content.
+ */
+ BOOL type1Opaque = (type1Equals == NULL || (type1End < type1Equals) || type1Equals[1] == endChar);
+ BOOL type2Opaque = (type2Equals == NULL || (type2End < type2Equals) || type2Equals[1] == endChar);
+ const char *type1NameEnd = (type1Equals == NULL || (type1End < type1Equals)) ? type1End : type1Equals;
+ const char *type2NameEnd = (type2Equals == NULL || (type2End < type2Equals)) ? type2End : type2Equals;
+ intptr_t type1NameLen = type1NameEnd - type1;
+ intptr_t type2NameLen = type2NameEnd - type2;
+
+ /* If the names are not equal, return NO */
+ if (type1NameLen != type2NameLen || strncmp(type1, type2, type1NameLen))
+ return NO;
+
+ /* If the same name, and at least one is opaque, that is close enough. */
+ if (type1Opaque || type2Opaque)
+ return YES;
+
+ /* Otherwise, compare all the elements. Use NSGetSizeAndAlignment to walk through the struct elements. */
+ type1 = type1Equals + 1;
+ type2 = type2Equals + 1;
+ while (type1[0] != endChar && type1[0] != '\0')
+ {
+ if (!OCMTypesEqualAllowOpaqueStructs(type1, type2))
+ return NO;
+ type1 = NSGetSizeAndAlignment(type1, NULL, NULL);
+ type2 = NSGetSizeAndAlignment(type2, NULL, NULL);
+ }
+ return YES;
+ }
+ case '^':
+ /* for a pointer, make sure the other is a pointer, then recursively compare the rest */
+ if (type2[0] != type1[0])
+ return NO;
+ return OCMTypesEqualAllowOpaqueStructs(type1+1, type2+1);
+
+ case '\0':
+ return type2[0] == '\0';
+
+ default:
+ {
+ // Move the type pointers past the current types, then compare that region
+ const char *afterType1 = NSGetSizeAndAlignment(type1, NULL, NULL);
+ const char *afterType2 = NSGetSizeAndAlignment(type2, NULL, NULL);
+ intptr_t type1Len = afterType1 - type1;
+ intptr_t type2Len = afterType2 - type2;
+
+ return (type1Len == type2Len && (strncmp(type1, type2, type1Len) == 0));
+ }
+ }
+}
+
+static CFNumberType OCMNumberTypeForObjCType(const char *objcType)
+{
+ switch (objcType[0])
+ {
+ case 'c': return kCFNumberCharType;
+ case 'C': return kCFNumberCharType;
+ case 'B': return kCFNumberCharType;
+ case 's': return kCFNumberShortType;
+ case 'S': return kCFNumberShortType;
+ case 'i': return kCFNumberIntType;
+ case 'I': return kCFNumberIntType;
+ case 'l': return kCFNumberLongType;
+ case 'L': return kCFNumberLongType;
+ case 'q': return kCFNumberLongLongType;
+ case 'Q': return kCFNumberLongLongType;
+ case 'f': return kCFNumberFloatType;
+ case 'd': return kCFNumberDoubleType;
+ }
+
+ return 0;
+}
+
+static NSNumber *OCMNumberForValue(NSValue *value)
+{
+#define CREATE_NUM(_type, _meth) ({ _type _v; [value getValue:&_v]; [NSNumber _meth _v]; })
+ switch ([value objCType][0])
+ {
+ case 'c': return CREATE_NUM(char, numberWithChar:);
+ case 'C': return CREATE_NUM(unsigned char, numberWithUnsignedChar:);
+ case 'B': return CREATE_NUM(bool, numberWithBool:);
+ case 's': return CREATE_NUM(short, numberWithShort:);
+ case 'S': return CREATE_NUM(unsigned short, numberWithUnsignedShort:);
+ case 'i': return CREATE_NUM(int, numberWithInt:);
+ case 'I': return CREATE_NUM(unsigned int, numberWithUnsignedInt:);
+ case 'l': return CREATE_NUM(long, numberWithLong:);
+ case 'L': return CREATE_NUM(unsigned long, numberWithUnsignedLong:);
+ case 'q': return CREATE_NUM(long long, numberWithLongLong:);
+ case 'Q': return CREATE_NUM(unsigned long long, numberWithUnsignedLongLong:);
+ case 'f': return CREATE_NUM(float, numberWithFloat:);
+ case 'd': return CREATE_NUM(double, numberWithDouble:);
+ }
+
+ return nil;
+}
+
+@implementation OCMBoxedReturnValueProvider
+
+- (void)handleInvocation:(NSInvocation *)anInvocation
+{
+ const char *returnType = [[anInvocation methodSignature] methodReturnType];
+ NSUInteger returnTypeSize = [[anInvocation methodSignature] methodReturnLength];
+ char valueBuffer[returnTypeSize];
+ NSValue *value = (NSValue *)returnValue;
+
+ if ([self getBytes:valueBuffer forValue:value compatibleWithType:returnType])
+ {
+ [anInvocation setReturnValue:valueBuffer];
+ }
+ else
+ {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Return value does not match method signature; signature declares '%s' but value is '%s'.", returnType, [value objCType]];
+ }
+}
+
+- (BOOL)isMethodReturnType:(const char *)returnType compatibleWithValueType:(const char *)valueType
+{
+ /* Allow void* for methods that return id, mainly to be able to handle nil */
+ if(strcmp(returnType, @encode(id)) == 0 && strcmp(valueType, @encode(void *)) == 0)
+ return YES;
+
+ /* Same types are obviously compatible */
+ if(strcmp(returnType, valueType) == 0)
+ return YES;
+
+ @try {
+ if(OCMTypesEqualAllowOpaqueStructs(returnType, valueType))
+ return YES;
+ }
+ @catch (NSException *e) {
+ /* Probably a bitfield or something that NSGetSizeAndAlignment chokes on, oh well */
+ return NO;
+ }
+
+ return NO;
+}
+
+- (BOOL)getBytes:(void *)outputBuf forValue:(NSValue *)inputValue compatibleWithType:(const char *)targetType
+{
+ /* If the types are directly compatible, use it */
+ if ([self isMethodReturnType:targetType compatibleWithValueType:[inputValue objCType]])
+ {
+ [inputValue getValue:outputBuf];
+ return YES;
+ }
+
+ /*
+ * See if they are similar number types, and if we can convert losslessly between them.
+ * For the most part, we set things up to use CFNumberGetValue, which returns false if
+ * conversion will be lossy.
+ */
+ CFNumberType inputType = OCMNumberTypeForObjCType([inputValue objCType]);
+ CFNumberType outputType = OCMNumberTypeForObjCType(targetType);
+
+ if (inputType == 0 || outputType == 0) // one or both are non-number types
+ return NO;
+
+
+ NSNumber *inputNumber = [inputValue isKindOfClass:[NSNumber class]]? (id)inputValue : OCMNumberForValue(inputValue);
+
+ /*
+ * Due to some legacy, back-compatible requirements in CFNumber.c, CFNumberGetValue can return true for
+ * some conversions which should not be allowed (by reading source, conversions from integer types to
+ * 8-bit or 16-bit integer types). So, check ourselves.
+ */
+ long long min = LLONG_MIN;
+ long long max = LLONG_MAX;
+ long long val = [inputNumber longLongValue];
+ switch (targetType[0])
+ {
+ case 'B':
+ case 'c': min = CHAR_MIN; max = CHAR_MAX; break;
+ case 'C': min = 0; max = UCHAR_MAX; break;
+ case 's': min = SHRT_MIN; max = SHRT_MAX; break;
+ case 'S': min = 0; max = USHRT_MAX; break;
+ }
+ if (val < min || val > max)
+ return NO;
+
+ /* Get the number, and return NO if the value was out of range or conversion was lossy */
+ return CFNumberGetValue((CFNumberRef)inputNumber, outputType, outputBuf);
+}
+
+@end
diff --git a/third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.mm b/third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.mm
deleted file mode 100644
index cf59610..0000000
--- a/third_party/ocmock/OCMock/OCMBoxedReturnValueProvider.mm
+++ /dev/null
@@ -1,145 +0,0 @@
-//---------------------------------------------------------------------------------------
-// $Id$
-// Copyright (c) 2009 by Mulle Kybernetik. See License file for details.
-//---------------------------------------------------------------------------------------
-
-#import "OCMBoxedReturnValueProvider.h"
-#import <objc/runtime.h>
-
-#if defined(__clang__)
-#include <vector> // for _LIBCPP_ABI_VERSION to detect if using libc++
-#endif
-
-#if defined(__clang__) && defined(_LIBCPP_ABI_VERSION)
-namespace {
-// Default stack size to use when checking for matching opening and closing
-// characters (<> and {}). This is used to reduce the number of allocations
-// in AdvanceTypeDescriptionPointer function.
-const size_t kDefaultStackSize = 32;
-
-// Move to the next pertinent character in a type description. This skips
-// all the field expansion that clang includes in the type description when
-// compiling with libc++.
-//
-// See inner comment of -isValueTypeCompatibleWithInvocation: for more details.
-// Returns true if the pointer was advanced, false if the type description was
-// not correctly parsed.
-bool AdvanceTypeDescriptionPointer(const char *&typeDescription) {
- if (!*typeDescription)
- return true;
-
- ++typeDescription;
- if (*typeDescription != '=')
- return true;
-
- ++typeDescription;
- std::vector<char> stack;
- stack.reserve(kDefaultStackSize);
- while (*typeDescription) {
- const char current = *typeDescription;
- if (current == '<' || current == '{') {
- stack.push_back(current);
- } else if (current == '>' || current == '}') {
- if (!stack.empty()) {
- const char opening = stack.back();
- if ((opening == '<' && current != '>') ||
- (opening == '{' && current != '}')) {
- return false;
- }
- stack.pop_back();
- } else {
- return current == '}';
- }
- } else if (current == ',' && stack.empty()) {
- return true;
- }
- ++typeDescription;
- }
- return true;
-}
-}
-#endif // defined(__clang__) && defined(_LIBCPP_ABI_VERSION)
-
-@interface OCMBoxedReturnValueProvider ()
-
-- (BOOL)isValueTypeCompatibleWithInvocation:(NSInvocation *)anInvocation;
-
-@end
-
-@implementation OCMBoxedReturnValueProvider
-
-- (BOOL)isValueTypeCompatibleWithInvocation:(NSInvocation *)anInvocation {
- const char *returnType = [[anInvocation methodSignature] methodReturnType];
- const char *valueType = [(NSValue *)returnValue objCType];
-
-#if defined(__aarch64__) || defined(__x86_64__)
- // ARM64 uses 'B' for BOOLs in method signature but 'c' in NSValue. That case
- // should match.
- if (strcmp(returnType, "B") == 0 && strcmp(valueType, "c") == 0)
- return YES;
-#endif // defined(__aarch64__) || defined(__x86_64__)
-
-#if defined(__clang__) && defined(_LIBCPP_ABI_VERSION)
- // The type representation of the return type of the invocation, and the
- // type representation passed to NSValue are not the same for C++ objects
- // when compiling with libc++ with clang.
- //
- // In that configuration, the C++ class are expanded to list the types of
- // the fields, but the depth of the expansion for templated types is larger
- // for the value stored in the NSValue.
- //
- // For example, when creating a OCMOCK_VALUE with a GURL object (from the
- // Chromium project), then the two types representations are:
- //
- // r^{GURL={basic_string<char, std::__1::char_traits<char>, std::__1::alloca
- // tor<char> >={__compressed_pair<std::__1::basic_string<char, std::__1::cha
- // r_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<c
- // har> >={__rep}}}B{Parsed={Component=ii}{Component=ii}{Component=ii}{Compo
- // nent=ii}{Component=ii}{Component=ii}{Component=ii}{Component=ii}^{Parsed}
- // }{scoped_ptr<GURL, base::DefaultDeleter<GURL> >={scoped_ptr_impl<GURL, ba
- // se::DefaultDeleter<GURL> >={Data=^{GURL}}}}}
- //
- // r^{GURL={basic_string<char, std::__1::char_traits<char>, std::__1::alloca
- // tor<char> >={__compressed_pair<std::__1::basic_string<char, std::__1::cha
- // r_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<c
- // har> >={__rep=(?={__long=II*}{__short=(?=Cc)[11c]}{__raw=[3L]})}}}B{Parse
- // d={Component=ii}{Component=ii}{Component=ii}{Component=ii}{Component=ii}{
- // Component=ii}{Component=ii}{Component=ii}^{Parsed}}{scoped_ptr<GURL, base
- // ::DefaultDeleter<GURL> >={scoped_ptr_impl<GURL, base::DefaultDeleter<GURL
- // > >={Data=^{GURL}}}}}
- //
- // Since those types should be considered equals, we un-expand them during
- // the comparison. For that, we remove everything following an "=" until we
- // meet a non-matched "}" or a ",".
-
- while (*returnType && *valueType) {
- if (*returnType != *valueType)
- return NO;
-
- if (!AdvanceTypeDescriptionPointer(returnType))
- return NO;
-
- if (!AdvanceTypeDescriptionPointer(valueType))
- return NO;
- }
-
- return !*returnType && !*valueType;
-#else
- return strcmp(returnType, valueType) == 0;
-#endif // defined(__clang__) && defined(_LIBCPP_ABI_VERSION)
-}
-
-- (void)handleInvocation:(NSInvocation *)anInvocation
-{
- if (![self isValueTypeCompatibleWithInvocation:anInvocation]) {
- const char *returnType = [[anInvocation methodSignature] methodReturnType];
- const char *valueType = [(NSValue *)returnValue objCType];
- @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"Return value does not match method signature; signature declares '%s' but value is '%s'.", returnType, valueType] userInfo:nil];
- }
- void *buffer = malloc([[anInvocation methodSignature] methodReturnLength]);
- [returnValue getValue:buffer];
- [anInvocation setReturnValue:buffer];
- free(buffer);
-}
-
-@end
diff --git a/third_party/ocmock/README.chromium b/third_party/ocmock/README.chromium
index dec968a..65d4126 100644
--- a/third_party/ocmock/README.chromium
+++ b/third_party/ocmock/README.chromium
@@ -32,8 +32,5 @@ the comparison of types between invocation return type description (which
changed from "c" to "B" on arm64) and NSValue objCType (which didn't change)
on arm64 for BOOL values.
-Chromium does patch OCMBoxedReturnValueProvider.m to handle comparison of
-return type and value type for C++ objects when compiling with clang and
-using libc++ STL library. The file is renamed OCMBoxedReturnValueProvider.mm
-to be compiled as an Objective-C++ class to allow detection of the C++ STL
-library used. Pull request: https://github.com/erikdoe/ocmock/pull/97
+Chromium also patches in 65ee89744bc1fbb9461f26d83e08243068cb212b that fixes
+the comparison between opaque (forward-declared) and non-opaque types.
diff --git a/third_party/ocmock/ocmock.gyp b/third_party/ocmock/ocmock.gyp
index 7d6539f..04a4a88 100644
--- a/third_party/ocmock/ocmock.gyp
+++ b/third_party/ocmock/ocmock.gyp
@@ -42,7 +42,7 @@
'OCMock/OCMBlockCaller.h',
'OCMock/OCMBlockCaller.m',
'OCMock/OCMBoxedReturnValueProvider.h',
- 'OCMock/OCMBoxedReturnValueProvider.mm',
+ 'OCMock/OCMBoxedReturnValueProvider.m',
'OCMock/OCMConstraint.h',
'OCMock/OCMConstraint.m',
'OCMock/OCMExceptionReturnValueProvider.h',