clang-tools 19.0.0git
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 "../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
41 StringRef Name,
42 ClangTidyContext *Context)
43 : ClangTidyCheck(Name, Context),
44 ForbiddenSuperClassNames(
45 utils::options::parseStringList(
46 Options.get("ClassNames", DefaultForbiddenSuperClassNames))) {
47}
48
50 Finder->addMatcher(
51 objcInterfaceDecl(
52 isDerivedFrom(objcInterfaceDecl(hasAnyName(ForbiddenSuperClassNames))
53 .bind("superclass")))
54 .bind("subclass"),
55 this);
56}
57
59 const MatchFinder::MatchResult &Result) {
60 const auto *SubClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(
61 "subclass");
62 assert(SubClass != nullptr);
63 const auto *SuperClass = Result.Nodes.getNodeAs<ObjCInterfaceDecl>(
64 "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
76 Opts,
77 "ForbiddenSuperClassNames",
78 utils::options::serializeStringList(ForbiddenSuperClassNames));
79}
80
81} // namespace clang::tidy::objc
llvm::SmallString< 256U > Name
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ForbiddenSubclassingCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap