1 //===--- ForbiddenSubclassingCheck.cpp - clang-tidy -----------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
16 using namespace clang::ast_matchers
;
24 constexpr char DefaultForbiddenSuperClassNames
[] =
25 "ABNewPersonViewController;"
26 "ABPeoplePickerNavigationController;"
27 "ABPersonViewController;"
28 "ABUnknownPersonViewController;"
36 "UIImagePickerController;"
42 ForbiddenSubclassingCheck::ForbiddenSubclassingCheck(
44 ClangTidyContext
*Context
)
45 : ClangTidyCheck(Name
, Context
),
46 ForbiddenSuperClassNames(
47 utils::options::parseStringList(
48 Options
.get("ClassNames", DefaultForbiddenSuperClassNames
))) {
51 void ForbiddenSubclassingCheck::registerMatchers(MatchFinder
*Finder
) {
54 isDerivedFrom(objcInterfaceDecl(hasAnyName(ForbiddenSuperClassNames
))
60 void ForbiddenSubclassingCheck::check(
61 const MatchFinder::MatchResult
&Result
) {
62 const auto *SubClass
= Result
.Nodes
.getNodeAs
<ObjCInterfaceDecl
>(
64 assert(SubClass
!= nullptr);
65 const auto *SuperClass
= Result
.Nodes
.getNodeAs
<ObjCInterfaceDecl
>(
67 assert(SuperClass
!= nullptr);
68 diag(SubClass
->getLocation(),
69 "Objective-C interface %0 subclasses %1, which is not "
70 "intended to be subclassed")
75 void ForbiddenSubclassingCheck::storeOptions(
76 ClangTidyOptions::OptionMap
&Opts
) {
79 "ForbiddenSuperClassNames",
80 utils::options::serializeStringList(ForbiddenSuperClassNames
));