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 | |
14 | using namespace clang::ast_matchers; |
15 | |
16 | namespace clang::tidy::objc { |
17 | |
18 | namespace { |
19 | |
20 | constexpr char DefaultForbiddenSuperClassNames[] = |
21 | "ABNewPersonViewController;" |
22 | "ABPeoplePickerNavigationController;" |
23 | "ABPersonViewController;" |
24 | "ABUnknownPersonViewController;" |
25 | "NSHashTable;" |
26 | "NSMapTable;" |
27 | "NSPointerArray;" |
28 | "NSPointerFunctions;" |
29 | "NSTimer;" |
30 | "UIActionSheet;" |
31 | "UIAlertView;" |
32 | "UIImagePickerController;" |
33 | "UITextInputMode;" |
34 | "UIWebView" ; |
35 | |
36 | } // namespace |
37 | |
38 | ForbiddenSubclassingCheck::ForbiddenSubclassingCheck(StringRef Name, |
39 | ClangTidyContext *Context) |
40 | : ClangTidyCheck(Name, Context), |
41 | ForbiddenSuperClassNames(utils::options::parseStringList( |
42 | Option: Options.get(LocalName: "ClassNames" , Default: DefaultForbiddenSuperClassNames))) {} |
43 | |
44 | void ForbiddenSubclassingCheck::registerMatchers(MatchFinder *Finder) { |
45 | Finder->addMatcher( |
46 | NodeMatch: objcInterfaceDecl( |
47 | isDerivedFrom(Base: objcInterfaceDecl(hasAnyName(ForbiddenSuperClassNames)) |
48 | .bind(ID: "superclass" ))) |
49 | .bind(ID: "subclass" ), |
50 | Action: this); |
51 | } |
52 | |
53 | void ForbiddenSubclassingCheck::check(const MatchFinder::MatchResult &Result) { |
54 | const auto *SubClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(ID: "subclass" ); |
55 | assert(SubClass != nullptr); |
56 | const auto *SuperClass = |
57 | Result.Nodes.getNodeAs<ObjCInterfaceDecl>(ID: "superclass" ); |
58 | assert(SuperClass != nullptr); |
59 | diag(SubClass->getLocation(), |
60 | "Objective-C interface %0 subclasses %1, which is not " |
61 | "intended to be subclassed" ) |
62 | << SubClass << SuperClass; |
63 | } |
64 | |
65 | void ForbiddenSubclassingCheck::storeOptions( |
66 | ClangTidyOptions::OptionMap &Opts) { |
67 | Options.store(Options&: Opts, LocalName: "ForbiddenSuperClassNames" , |
68 | Value: utils::options::serializeStringList(Strings: ForbiddenSuperClassNames)); |
69 | } |
70 | |
71 | } // namespace clang::tidy::objc |
72 | |