clang-tools 10.0.0

ForbiddenSubclassingCheck.cpp
Go to the documentation of this file.
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 "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "../utils/OptionsUtils.h"
15
16 using namespace clang::ast_matchers;
17
18 namespace clang {
19 namespace tidy {
20 namespace objc {
21
22 namespace {
23
24 constexpr char DefaultForbiddenSuperClassNames[] =
25 "ABNewPersonViewController;"
26 "ABPeoplePickerNavigationController;"
27 "ABPersonViewController;"
28 "ABUnknownPersonViewController;"
29 "NSHashTable;"
30 "NSMapTable;"
31 "NSPointerArray;"
32 "NSPointerFunctions;"
33 "NSTimer;"
34 "UIActionSheet;"
35 "UIAlertView;"
36 "UIImagePickerController;"
37 "UITextInputMode;"
38 "UIWebView";
39
40 } // namespace
41
42 ForbiddenSubclassingCheck::ForbiddenSubclassingCheck(
43 StringRef Name,
44 ClangTidyContext *Context)
45 : ClangTidyCheck(Name, Context),
46 ForbiddenSuperClassNames(
47 utils::options::parseStringList(
48 Options.get("ClassNames", DefaultForbiddenSuperClassNames))) {
49 }
50
51 void ForbiddenSubclassingCheck::registerMatchers(MatchFinder *Finder) {
52 // this check should only be applied to ObjC sources.
53 if (!getLangOpts().ObjC)
54 return;
55
56 Finder->addMatcher(
57 objcInterfaceDecl(
58 isDerivedFrom(
59 objcInterfaceDecl(
60 hasAnyName(
61 std::vector<StringRef>(
62 ForbiddenSuperClassNames.begin(),
63 ForbiddenSuperClassNames.end())))
64 .bind("superclass")))
65 .bind("subclass"),
66 this);
67 }
68
69 void ForbiddenSubclassingCheck::check(
70 const MatchFinder::MatchResult &Result) {
71 const auto *SubClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(
72 "subclass");
73 assert(SubClass != nullptr);
74 const auto *SuperClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(
75 "superclass");
76 assert(SuperClass != nullptr);
77 diag(SubClass->getLocation(),
78 "Objective-C interface %0 subclasses %1, which is not "
79 "intended to be subclassed")
80 << SubClass
81 << SuperClass;
82 }
83
84 void ForbiddenSubclassingCheck::storeOptions(
85 ClangTidyOptions::OptionMap &Opts) {
86 Options.store(
87 Opts,
88 "ForbiddenSuperClassNames",
89 utils::options::serializeStringList(ForbiddenSuperClassNames));
90 }
91
92 } // namespace objc
93 } // namespace tidy
94 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::options::serializeStringList
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Definition: OptionsUtils.cpp:30
clang::tidy::objc::ForbiddenSubclassingCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ForbiddenSubclassingCheck.cpp:51
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::objc::ForbiddenSubclassingCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
Definition: ForbiddenSubclassingCheck.cpp:84
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::objc::ForbiddenSubclassingCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ForbiddenSubclassingCheck.cpp:69
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
ForbiddenSubclassingCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21