| 1 | //===--- ForbiddenSubclassingCheck.cpp - clang-tidy -----------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ForbiddenSubclassingCheck.h" |
| 10 | #include "../utils/OptionsUtils.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "llvm/ADT/Hashing.h" |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | |
| 16 | using namespace clang::ast_matchers; |
| 17 | |
| 18 | namespace clang::tidy::objc { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | constexpr char DefaultForbiddenSuperClassNames[] = |
| 23 | "ABNewPersonViewController;" |
| 24 | "ABPeoplePickerNavigationController;" |
| 25 | "ABPersonViewController;" |
| 26 | "ABUnknownPersonViewController;" |
| 27 | "NSHashTable;" |
| 28 | "NSMapTable;" |
| 29 | "NSPointerArray;" |
| 30 | "NSPointerFunctions;" |
| 31 | "NSTimer;" |
| 32 | "UIActionSheet;" |
| 33 | "UIAlertView;" |
| 34 | "UIImagePickerController;" |
| 35 | "UITextInputMode;" |
| 36 | "UIWebView"; |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | ForbiddenSubclassingCheck::ForbiddenSubclassingCheck( |
| 41 | StringRef Name, |
| 42 | ClangTidyContext *Context) |
| 43 | : ClangTidyCheck(Name, Context), |
| 44 | ForbiddenSuperClassNames( |
| 45 | utils::options::parseStringList( |
| 46 | Options.get("ClassNames", DefaultForbiddenSuperClassNames))) { |
| 47 | } |
| 48 | |
| 49 | void ForbiddenSubclassingCheck::registerMatchers(MatchFinder *Finder) { |
| 50 | Finder->addMatcher( |
| 51 | objcInterfaceDecl( |
| 52 | isDerivedFrom(objcInterfaceDecl(hasAnyName(ForbiddenSuperClassNames)) |
| 53 | .bind("superclass"))) |
| 54 | .bind("subclass"), |
| 55 | this); |
| 56 | } |
| 57 | |
| 58 | void ForbiddenSubclassingCheck::check( |
| 59 | const MatchFinder::MatchResult &Result) { |
| 60 | const auto *SubClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>( |
| 61 | "subclass"); |
| 62 | assert(SubClass != nullptr); |
| 63 | const auto *SuperClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>( |
| 64 | "superclass"); |
| 65 | assert(SuperClass != nullptr); |
| 66 | diag(SubClass->getLocation(), |
| 67 | "Objective-C interface %0 subclasses %1, which is not " |
| 68 | "intended to be subclassed") |
| 69 | << SubClass |
| 70 | << SuperClass; |
| 71 | } |
| 72 | |
| 73 | void ForbiddenSubclassingCheck::storeOptions( |
| 74 | ClangTidyOptions::OptionMap &Opts) { |
| 75 | Options.store( |
| 76 | Opts, |
| 77 | "ForbiddenSuperClassNames", |
| 78 | utils::options::serializeStringList(ForbiddenSuperClassNames)); |
| 79 | } |
| 80 | |
| 81 | } // namespace clang::tidy::objc |