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
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::objc {
19
20namespace {
21
22constexpr 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
40ForbiddenSubclassingCheck::ForbiddenSubclassingCheck(
41 StringRef Name,
42 ClangTidyContext *Context)
43 : ClangTidyCheck(Name, Context),
44 ForbiddenSuperClassNames(
45 utils::options::parseStringList(
46 Option: Options.get(LocalName: "ClassNames", Default: DefaultForbiddenSuperClassNames))) {
47}
48
49void ForbiddenSubclassingCheck::registerMatchers(MatchFinder *Finder) {
50 Finder->addMatcher(
51 NodeMatch: objcInterfaceDecl(
52 isDerivedFrom(Base: objcInterfaceDecl(hasAnyName(ForbiddenSuperClassNames))
53 .bind(ID: "superclass")))
54 .bind(ID: "subclass"),
55 Action: this);
56}
57
58void ForbiddenSubclassingCheck::check(
59 const MatchFinder::MatchResult &Result) {
60 const auto *SubClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(
61 ID: "subclass");
62 assert(SubClass != nullptr);
63 const auto *SuperClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(
64 ID: "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
73void ForbiddenSubclassingCheck::storeOptions(
74 ClangTidyOptions::OptionMap &Opts) {
75 Options.store(
76 Options&: Opts,
77 LocalName: "ForbiddenSuperClassNames",
78 Value: utils::options::serializeStringList(Strings: ForbiddenSuperClassNames));
79}
80
81} // namespace clang::tidy::objc
82

source code of clang-tools-extra/clang-tidy/objc/ForbiddenSubclassingCheck.cpp