clang-tools 10.0.0

SuperSelfCheck.cpp
Go to the documentation of this file.
1 //===--- SuperSelfCheck.cpp - clang-tidy ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "SuperSelfCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace objc {
19
20 namespace {
21
22 /// Matches Objective-C methods in the initializer family.
23 ///
24 /// Example matches -init and -initWithInt:.
25 /// (matcher = objcMethodDecl(isInitializer()))
26 /// \code
27 /// @interface Foo
28 /// - (instancetype)init;
29 /// - (instancetype)initWithInt:(int)i;
30 /// + (instancetype)init;
31 /// - (void)bar;
32 /// @end
33 /// \endcode
34 AST_MATCHER(ObjCMethodDecl, isInitializer) {
35 return Node.getMethodFamily() == OMF_init;
36 }
37
38 /// Matches Objective-C implementations with interfaces that match
39 /// \c Base.
40 ///
41 /// Example matches implementation declarations for X.
42 /// (matcher = objcImplementationDecl(hasInterface(hasName("X"))))
43 /// \code
44 /// @interface X
45 /// @end
46 /// @implementation X
47 /// @end
48 /// @interface Y
49 // @end
50 /// @implementation Y
51 /// @end
52 /// \endcode
53 AST_MATCHER_P(ObjCImplementationDecl, hasInterface,
54 ast_matchers::internal::Matcher<ObjCInterfaceDecl>, Base) {
55 const ObjCInterfaceDecl *InterfaceDecl = Node.getClassInterface();
56 return Base.matches(*InterfaceDecl, Finder, Builder);
57 }
58
59 /// Matches Objective-C message expressions where the receiver is the
60 /// super instance.
61 ///
62 /// Example matches the invocations of -banana and -orange.
63 /// (matcher = objcMessageExpr(isMessagingSuperInstance()))
64 /// \code
65 /// - (void)banana {
66 /// [self apple]
67 /// [super banana];
68 /// [super orange];
69 /// }
70 /// \endcode
71 AST_MATCHER(ObjCMessageExpr, isMessagingSuperInstance) {
72 return Node.getReceiverKind() == ObjCMessageExpr::SuperInstance;
73 }
74
75 } // namespace
76
77 void SuperSelfCheck::registerMatchers(MatchFinder *Finder) {
78 // This check should only be applied to Objective-C sources.
79 if (!getLangOpts().ObjC)
80 return;
81
82 Finder->addMatcher(
83 objcMessageExpr(hasSelector("self"), isMessagingSuperInstance(),
84 hasAncestor(objcMethodDecl(
85 isInitializer(),
86 hasDeclContext(objcImplementationDecl(hasInterface(
87 isDerivedFrom(hasName("NSObject"))))))))
88 .bind("message"),
89 this);
90 }
91
92 void SuperSelfCheck::check(const MatchFinder::MatchResult &Result) {
93 const auto *Message = Result.Nodes.getNodeAs<ObjCMessageExpr>("message");
94
95 auto Diag = diag(Message->getExprLoc(), "suspicious invocation of %0 in "
96 "initializer; did you mean to "
97 "invoke a superclass initializer?")
98 << Message->getMethodDecl();
99
100 SourceLocation ReceiverLoc = Message->getReceiverRange().getBegin();
101 if (ReceiverLoc.isMacroID() || ReceiverLoc.isInvalid())
102 return;
103
104 SourceLocation SelectorLoc = Message->getSelectorStartLoc();
105 if (SelectorLoc.isMacroID() || SelectorLoc.isInvalid())
106 return;
107
108 Diag << FixItHint::CreateReplacement(Message->getSourceRange(),
109 StringRef("[super init]"));
110 }
111
112 } // namespace objc
113 } // namespace tidy
114 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:18
clang::tidy::modernize::Message
constexpr llvm::StringLiteral Message
Definition: UseTrailingReturnTypeCheck.cpp:101
SuperSelfCheck.h
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:83
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:52