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
;
18 namespace clang::tidy::objc
{
22 constexpr char DefaultForbiddenSuperClassNames
[] =
23 "ABNewPersonViewController;"
24 "ABPeoplePickerNavigationController;"
25 "ABPersonViewController;"
26 "ABUnknownPersonViewController;"
34 "UIImagePickerController;"
40 ForbiddenSubclassingCheck::ForbiddenSubclassingCheck(
42 ClangTidyContext
*Context
)
43 : ClangTidyCheck(Name
, Context
),
44 ForbiddenSuperClassNames(
45 utils::options::parseStringList(
46 Options
.get("ClassNames", DefaultForbiddenSuperClassNames
))) {
49 void ForbiddenSubclassingCheck::registerMatchers(MatchFinder
*Finder
) {
52 isDerivedFrom(objcInterfaceDecl(hasAnyName(ForbiddenSuperClassNames
))
58 void ForbiddenSubclassingCheck::check(
59 const MatchFinder::MatchResult
&Result
) {
60 const auto *SubClass
= Result
.Nodes
.getNodeAs
<ObjCInterfaceDecl
>(
62 assert(SubClass
!= nullptr);
63 const auto *SuperClass
= Result
.Nodes
.getNodeAs
<ObjCInterfaceDecl
>(
65 assert(SuperClass
!= nullptr);
66 diag(SubClass
->getLocation(),
67 "Objective-C interface %0 subclasses %1, which is not "
68 "intended to be subclassed")
73 void ForbiddenSubclassingCheck::storeOptions(
74 ClangTidyOptions::OptionMap
&Opts
) {
77 "ForbiddenSuperClassNames",
78 utils::options::serializeStringList(ForbiddenSuperClassNames
));
81 } // namespace clang::tidy::objc