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 
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 
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 
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 
86  Options.store(
87  Opts,
88  "ForbiddenSuperClassNames",
89  utils::options::serializeStringList(ForbiddenSuperClassNames));
90 }
91 
92 } // namespace objc
93 } // namespace tidy
94 } // namespace clang
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
const LangOptions & getLangOpts() const
Returns the language options from the context.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.