diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-10 03:03:00 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-10 03:03:00 +0000 |
commit | 81814bce7954f38311b39c488ba076a297458534 (patch) | |
tree | cd97745b8cf60bfa960af62726a13920c7449e1f /base | |
parent | 704a8539764c9095a317400e43bb5b090c8c8d9d (diff) | |
download | chromium_src-81814bce7954f38311b39c488ba076a297458534.zip chromium_src-81814bce7954f38311b39c488ba076a297458534.tar.gz chromium_src-81814bce7954f38311b39c488ba076a297458534.tar.bz2 |
Create a "no compile" drivers script in python to unittest compile time asserts.
BUG=87341
TEST=enable some of the existing no-compile tests and run on try bots.
Review URL: http://codereview.chromium.org/7458012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@100564 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 6 | ||||
-rw-r--r-- | base/bind_unittest.cc | 108 | ||||
-rw-r--r-- | base/bind_unittest.nc | 223 |
3 files changed, 235 insertions, 102 deletions
diff --git a/base/base.gyp b/base/base.gyp index e8da279..e78db0a 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -116,6 +116,7 @@ 'atomicops_unittest.cc', 'base64_unittest.cc', 'bind_unittest.cc', + 'bind_unittest.nc', 'bits_unittest.cc', 'callback_unittest.cc', 'command_line_unittest.cc', @@ -245,6 +246,11 @@ '../third_party/icu/icu.gyp:icui18n', '../third_party/icu/icu.gyp:icuuc', ], + 'includes': ['../build/nocompile.gypi'], + 'variables': { + # TODO(ajwong): Is there a way to autodetect this? + 'module_dir': 'base' + }, 'conditions': [ ['toolkit_uses_gtk==1', { 'sources!': [ diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc index 333512a..8718d1d 100644 --- a/base/bind_unittest.cc +++ b/base/bind_unittest.cc @@ -251,6 +251,7 @@ TEST_F(BindTest, ArityTest) { // Function type support. // - Normal function. +// - Normal function bound with non-refcounted first argument. // - Method bound to non-const object. // - Const method bound to non-const object. // - Const method bound to const object. @@ -265,12 +266,16 @@ TEST_F(BindTest, FunctionTypeSupport) { EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2); Closure normal_cb = Bind(&VoidFunc0); + Callback<NoRef*(void)> normal_non_refcounted_cb = + Bind(&PolymorphicIdentity<NoRef*>, &no_ref_); + normal_cb.Run(); + EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run()); + Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_); Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0, &has_ref_); Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0, const_has_ref_ptr_); - normal_cb.Run(); method_cb.Run(); const_method_nonconst_obj_cb.Run(); const_method_const_obj_cb.Run(); @@ -580,107 +585,6 @@ TEST_F(BindTest, ArgumentCopies) { // // TODO(ajwong): Is there actually a way to test this? -// No-compile tests. These should not compile. If they do, we are allowing -// error-prone, or incorrect behavior in the callback system. Uncomment the -// tests to check. -TEST_F(BindTest, NoCompile) { - // - Method bound to const-object. - // - // Only const methods should be allowed to work with const objects. - // - // Callback<void(void)> method_to_const_cb = - // Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); - // method_to_const_cb.Run(); - - // - Method bound to non-refcounted object. - // - Const Method bound to non-refcounted object. - // - // We require refcounts unless you have Unretained(). - // - // Callback<void(void)> no_ref_cb = - // Bind(&NoRef::VoidMethod0, &no_ref_); - // no_ref_cb.Run(); - // Callback<void(void)> no_ref_const_cb = - // Bind(&NoRef::VoidConstMethod0, &no_ref_); - // no_ref_const_cb.Run(); - - // - Unretained() used with a refcounted object. - // - // If the object supports refcounts, unretaining it in the callback is a - // memory management contract break. - // Callback<void(void)> unretained_cb = - // Bind(&HasRef::VoidConstMethod0, Unretained(&has_ref_)); - // unretained_cb.Run(); - - // - Const argument used with non-const pointer parameter of same type. - // - Const argument used with non-const pointer parameter of super type. - // - // This is just a const-correctness check. - // - // const Parent* const_parent_ptr; - // const Child* const_child_ptr; - // Callback<Parent*(void)> pointer_same_cb = - // Bind(&PolymorphicIdentity<Parent*>, const_parent_ptr); - // pointer_same_cb.Run(); - // Callback<Parent*(void)> pointer_super_cb = - // Bind(&PolymorphicIdentity<Parent*>, const_child_ptr); - // pointer_super_cb.Run(); - - // - Construction of Callback<A> from Callback<B> if A is supertype of B. - // Specific example: Callback<void(void)> a; Callback<int(void)> b; a = b; - // - // While this is technically safe, most people aren't used to it when coding - // C++ so if this is happening, it is almost certainly an error. - // - // Callback<int(void)> cb_a0 = Bind(&Identity, 1); - // Callback<void(void)> cb_b0 = cb_a0; - - // - Assignment of Callback<A> from Callback<B> if A is supertype of B. - // See explanation above. - // - // Callback<int(void)> cb_a1 = Bind(&Identity, 1); - // Callback<void(void)> cb_b1; - // cb_a1 = cb_b1; - - // - Functions with reference parameters, unsupported. - // - // First, non-const reference parameters are disallowed by the Google - // style guide. Seconds, since we are doing argument forwarding it becomes - // very tricky to avoid copies, maintain const correctness, and not - // accidentally have the function be modifying a temporary, or a copy. - // - // NoRefParent p; - // Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapNoRefParentRef); - // ref_arg_cb.Run(p); - // Callback<int(void)> ref_cb = Bind(&UnwrapNoRefParentRef, p); - // ref_cb.Run(); - - // - A method should not be bindable with an array of objects. - // - // This is likely not wanted behavior. We specifically check for it though - // because it is possible, depending on how you implement prebinding, to - // implicitly convert an array type to a pointer type. - // - // HasRef p[10]; - // Callback<void(void)> method_bound_to_array_cb = - // Bind(&HasRef::VoidConstMethod0, p); - // method_bound_to_array_cb.Run(); - - // - Refcounted types should not be bound as a raw pointer. - // HasRef for_raw_ptr; - // Callback<void(void)> ref_count_as_raw_ptr = - // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); - - // - WeakPtrs cannot be bound to methods with return types. - // HasRef for_raw_ptr; - // WeakPtrFactory<NoRef> weak_factory(&no_ref_); - // Callback<int(void)> weak_ptr_with_non_void_return_type = - // Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); - - // - Bind result cannot be assigned to Callbacks with a mismatching type. - // Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); -} - #if defined(OS_WIN) int __fastcall FastCallFunc(int n) { return n; diff --git a/base/bind_unittest.nc b/base/bind_unittest.nc new file mode 100644 index 0000000..2d07cbb --- /dev/null +++ b/base/bind_unittest.nc @@ -0,0 +1,223 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/callback.h" +#include "base/bind.h" + +namespace base { + +// Do not put everything inside an anonymous namespace. If you do, many of the +// helper function declarations will generate unused definition warnings unless +// unused definition warnings. + +static const int kParentValue = 1; +static const int kChildValue = 2; + +class NoRef { + public: + void VoidMethod0() {} + void VoidConstMethod0() const {} + int IntMethod0() { return 1; } +}; + +class HasRef : public NoRef { + public: + void AddRef(void) const {} + bool Release(void) const { return true; } +}; + +class Parent { + public: + void AddRef(void) const {} + void Release(void) const {} + virtual void VirtualSet() { value = kParentValue; } + void NonVirtualSet() { value = kParentValue; } + int value; +}; + +class Child : public Parent { + public: + virtual void VirtualSet() { value = kChildValue; } + void NonVirtualSet() { value = kChildValue; } +}; + +class NoRefParent { + public: + virtual void VirtualSet() { value = kParentValue; } + void NonVirtualSet() { value = kParentValue; } + int value; +}; + +class NoRefChild : public NoRefParent { + virtual void VirtualSet() { value = kChildValue; } + void NonVirtualSet() { value = kChildValue; } +}; + +template <typename T> +T PolymorphicIdentity(T t) { + return t; +} + +int UnwrapParentRef(Parent& p) { + return p.value; +} + +template <typename T> +void VoidPolymorphic1(T t) { +} + +#if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"invalid conversion from 'const base::NoRef\*' to 'base::NoRef\*'"] + +// Method bound to const-object. +// +// Only const methods should be allowed to work with const objects. +void WontCompile() { + HasRef has_ref; + const HasRef* const_has_ref_ptr_ = &has_ref; + Callback<void(void)> method_to_const_cb = + Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); + method_to_const_cb.Run(); +} + +#elif defined(NCTEST_CONSTRUCTION_FROM_SUBTYPE) // [r"conversion from 'base::Callback<int\(\)>' to non-scalar type 'base::Callback<void\(\)>'"] + +// Construction of Callback<A> from Callback<B> if A is supertype of B. +// +// While this is technically safe, most people aren't used to it when coding +// C++ so if this is happening, it is almost certainly an error. +void WontCompile() { + Callback<int(void)> cb_a = Bind(&PolymorphicIdentity<int>, 1); + Callback<void(void)> cb_b = cb_a; +} + +#elif defined(NCTEST_ASSIGNMENT_FROM_SUBTYPE) // [r"no match for 'operator=' in 'cb_a = cb_b'"] + +// Assignment of Callback<A> from Callback<B> if A is supertype of B. +// See explanation for NCTEST_CONSTRUCTION_FROM_SUBTYPE +void WontCompile() { + Callback<int(void)> cb_a = Bind(&PolymorphicIdentity<int>, 1); + Callback<void(void)> cb_b; + cb_a = cb_b; +} + +#elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"has no member named 'AddRef'"] + +// Method bound to non-refcounted object. +// +// We require refcounts unless you have Unretained(). +void WontCompile() { + NoRef no_ref; + Callback<void(void)> no_ref_cb = + Bind(&NoRef::VoidMethod0, &no_ref); + no_ref_cb.Run(); +} + +#elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"has no member named 'AddRef'"] + +// Const Method bound to non-refcounted object. +// +// We require refcounts unless you have Unretained(). +void WontCompile() { + NoRef no_ref; + Callback<void(void)> no_ref_const_cb = + Bind(&NoRef::VoidConstMethod0, &no_ref); + no_ref_const_cb.Run(); +} + +#elif defined(NCTEST_CONST_POINTER) // [r"invalid conversion from 'const base::NoRef\*' to 'base::NoRef\*'"] + +// Const argument used with non-const pointer parameter of same type. +// +// This is just a const-correctness check. +void WontCompile() { + const NoRef* const_no_ref_ptr; + Callback<NoRef*(void)> pointer_same_cb = + Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); + pointer_same_cb.Run(); +} + +#elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"'const base::NoRefParent\*' to 'base::NoRefParent\*'"] + +// Const argument used with non-const pointer parameter of super type. +// +// This is just a const-correctness check. +void WontCompile() { + const NoRefChild* const_child_ptr; + Callback<NoRefParent*(void)> pointer_super_cb = + Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); + pointer_super_cb.Run(); +} + +#elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"badstring"] +// I think there's a type safety promotion issue here where we can pass a const +// ref to a non const-ref function, or vice versa accidentally. Or we make a +// copy accidentally. Check. + +// Functions with reference parameters, unsupported. +// +// First, non-const reference parameters are disallowed by the Google +// style guide. Second, since we are doing argument forwarding it becomes +// very tricky to avoid copies, maintain const correctness, and not +// accidentally have the function be modifying a temporary, or a copy. +void WontCompile() { + Parent p; + Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef); + ref_arg_cb.Run(p); +} + +#elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"creating array with negative size"] + +// Binding functions with reference parameters, unsupported. +// +// See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM +void WontCompile() { + Parent p; + Callback<int(void)> ref_cb = Bind(&UnwrapParentRef, p); + ref_cb.Run(); +} + +#elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"creating array with negative size"] + +// A method should not be bindable with an array of objects. +// +// This is likely not wanted behavior. We specifically check for it though +// because it is possible, depending on how you implement prebinding, to +// implicitly convert an array type to a pointer type. +void WontCompile() { + HasRef p[10]; + Callback<void(void)> method_bound_to_array_cb = + Bind(&HasRef::VoidMethod0, p); + method_bound_to_array_cb.Run(); +} + +#elif defined(NCTEST_NO_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"creating array with negative size"] + +// Refcounted types should not be bound as a raw pointer. +void WontCompile() { + HasRef for_raw_ptr; + Callback<void(void)> ref_count_as_raw_ptr = + Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); +} + +#elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"creating array with negative size"] + +// WeakPtrs cannot be bound to methods with return types. +void WontCompile() { + NoRef no_ref; + WeakPtrFactory<NoRef> weak_factory(&no_ref); + Callback<int(void)> weak_ptr_with_non_void_return_type = + Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); + weak_ptr_with_non_void_return_type.Run(); +} + +#elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERINT_TYPES) // [r"creating array with negative size"] + +// Bind result cannot be assigned to Callbacks with a mismatching type. +void WontCompile() { + Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); +} + +#endif + +} // namespace base |