1 //===--- SuperSelfCheck.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 "SuperSelfCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::objc
{
19 /// Matches Objective-C methods in the initializer family.
21 /// Example matches -init and -initWithInt:.
22 /// (matcher = objcMethodDecl(isInitializer()))
25 /// - (instancetype)init;
26 /// - (instancetype)initWithInt:(int)i;
27 /// + (instancetype)init;
31 AST_MATCHER(ObjCMethodDecl
, isInitializer
) {
32 return Node
.getMethodFamily() == OMF_init
;
35 /// Matches Objective-C implementations with interfaces that match
38 /// Example matches implementation declarations for X.
39 /// (matcher = objcImplementationDecl(hasInterface(hasName("X"))))
50 AST_MATCHER_P(ObjCImplementationDecl
, hasInterface
,
51 ast_matchers::internal::Matcher
<ObjCInterfaceDecl
>, Base
) {
52 const ObjCInterfaceDecl
*InterfaceDecl
= Node
.getClassInterface();
53 return Base
.matches(*InterfaceDecl
, Finder
, Builder
);
56 /// Matches Objective-C message expressions where the receiver is the
59 /// Example matches the invocations of -banana and -orange.
60 /// (matcher = objcMessageExpr(isMessagingSuperInstance()))
68 AST_MATCHER(ObjCMessageExpr
, isMessagingSuperInstance
) {
69 return Node
.getReceiverKind() == ObjCMessageExpr::SuperInstance
;
74 void SuperSelfCheck::registerMatchers(MatchFinder
*Finder
) {
76 objcMessageExpr(hasSelector("self"), isMessagingSuperInstance(),
77 hasAncestor(objcMethodDecl(
79 hasDeclContext(objcImplementationDecl(hasInterface(
80 isDerivedFrom(hasName("NSObject"))))))))
85 void SuperSelfCheck::check(const MatchFinder::MatchResult
&Result
) {
86 const auto *Message
= Result
.Nodes
.getNodeAs
<ObjCMessageExpr
>("message");
88 auto Diag
= diag(Message
->getExprLoc(), "suspicious invocation of %0 in "
89 "initializer; did you mean to "
90 "invoke a superclass initializer?")
91 << Message
->getMethodDecl();
93 SourceLocation ReceiverLoc
= Message
->getReceiverRange().getBegin();
94 if (ReceiverLoc
.isMacroID() || ReceiverLoc
.isInvalid())
97 SourceLocation SelectorLoc
= Message
->getSelectorStartLoc();
98 if (SelectorLoc
.isMacroID() || SelectorLoc
.isInvalid())
101 Diag
<< FixItHint::CreateReplacement(Message
->getSourceRange(),
102 StringRef("[super init]"));
105 } // namespace clang::tidy::objc