diff options
author | Haojian Wu <hokein@google.com> | 2017-10-27 07:41:36 +0000 |
---|---|---|
committer | Haojian Wu <hokein@google.com> | 2017-10-27 07:41:36 +0000 |
commit | e010406e28074f954336106ac624181b24923a41 (patch) | |
tree | d6b514a886d5d82480261daf4bc6a2da40cc1a32 /clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp | |
parent | 363afa3b25ac975e0c5ab3a13ab4e4aea6a18316 (diff) | |
download | bcm5719-llvm-e010406e28074f954336106ac624181b24923a41.tar.gz bcm5719-llvm-e010406e28074f954336106ac624181b24923a41.zip |
[clang-tidy ObjC] [3/3] New check objc-forbidden-subclassing
Summary:
This is part 3 of 3 of a series of changes to improve Objective-C
linting in clang-tidy.
This adds a new clang-tidy check `objc-forbidden-subclassing` which
ensures clients do not create subclasses of Objective-C classes which
are not designed to be subclassed.
(Note that for code under your control, you should use
__attribute__((objc_subclassing_restricted)) instead -- this
is intended for third-party APIs which cannot be modified.)
By default, the following classes (which are publicly documented
as not supporting subclassing) are forbidden from subclassing:
ABNewPersonViewController
ABPeoplePickerNavigationController
ABPersonViewController
ABUnknownPersonViewController
NSHashTable
NSMapTable
NSPointerArray
NSPointerFunctions
NSTimer
UIActionSheet
UIAlertView
UIImagePickerController
UITextInputMode
UIWebView
Clients can set a CheckOption
`objc-forbidden-subclassing.ClassNames` to a semicolon-separated
list of class names, which overrides this list.
Test Plan: `ninja check-clang-tools`
Patch by Ben Hamilton!
Reviewers: hokein, alexfh
Reviewed By: hokein
Subscribers: saidinwot, Wizard, srhines, mgorny, xazax.hun
Differential Revision: https://reviews.llvm.org/D39142
llvm-svn: 316744
Diffstat (limited to 'clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp')
-rw-r--r-- | clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp index c50480a65fc..92ae8e1038f 100644 --- a/clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp +++ b/clang-tools-extra/unittests/clang-tidy/ObjCModuleTest.cpp @@ -9,12 +9,40 @@ #include "ClangTidyTest.h" #include "gtest/gtest.h" +#include "objc/ForbiddenSubclassingCheck.h" + +using namespace clang::tidy::objc; namespace clang { namespace tidy { namespace test { -// TODO(D39142) Add unit tests for the ObjC module here once a check lands. +TEST(ObjCForbiddenSubclassing, AllowedSubclass) { + std::vector<ClangTidyError> Errors; + runCheckOnCode<ForbiddenSubclassingCheck>( + "@interface Foo\n" + "@end\n" + "@interface Bar : Foo\n" + "@end\n", + &Errors, + "input.m"); + EXPECT_EQ(0ul, Errors.size()); +} + +TEST(ObjCForbiddenSubclassing, ForbiddenSubclass) { + std::vector<ClangTidyError> Errors; + runCheckOnCode<ForbiddenSubclassingCheck>( + "@interface UIImagePickerController\n" + "@end\n" + "@interface Foo : UIImagePickerController\n" + "@end\n", + &Errors, + "input.m"); + EXPECT_EQ(1ul, Errors.size()); + EXPECT_EQ( + "Objective-C interface 'Foo' subclasses 'UIImagePickerController', which is not intended to be subclassed", + Errors[0].Message.Message); +} } // namespace test } // namespace tidy |