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
;
21 /// Matches Objective-C methods in the initializer family.
23 /// Example matches -init and -initWithInt:.
24 /// (matcher = objcMethodDecl(isInitializer()))
27 /// - (instancetype)init;
28 /// - (instancetype)initWithInt:(int)i;
29 /// + (instancetype)init;
33 AST_MATCHER(ObjCMethodDecl
, isInitializer
) {
34 return Node
.getMethodFamily() == OMF_init
;
37 /// Matches Objective-C implementations with interfaces that match
40 /// Example matches implementation declarations for X.
41 /// (matcher = objcImplementationDecl(hasInterface(hasName("X"))))
52 AST_MATCHER_P(ObjCImplementationDecl
, hasInterface
,
53 ast_matchers::internal::Matcher
<ObjCInterfaceDecl
>, Base
) {
54 const ObjCInterfaceDecl
*InterfaceDecl
= Node
.getClassInterface();
55 return Base
.matches(*InterfaceDecl
, Finder
, Builder
);
58 /// Matches Objective-C message expressions where the receiver is the
61 /// Example matches the invocations of -banana and -orange.
62 /// (matcher = objcMessageExpr(isMessagingSuperInstance()))
70 AST_MATCHER(ObjCMessageExpr
, isMessagingSuperInstance
) {
71 return Node
.getReceiverKind() == ObjCMessageExpr::SuperInstance
;
76 void SuperSelfCheck::registerMatchers(MatchFinder
*Finder
) {
78 objcMessageExpr(hasSelector("self"), isMessagingSuperInstance(),
79 hasAncestor(objcMethodDecl(
81 hasDeclContext(objcImplementationDecl(hasInterface(
82 isDerivedFrom(hasName("NSObject"))))))))
87 void SuperSelfCheck::check(const MatchFinder::MatchResult
&Result
) {
88 const auto *Message
= Result
.Nodes
.getNodeAs
<ObjCMessageExpr
>("message");
90 auto Diag
= diag(Message
->getExprLoc(), "suspicious invocation of %0 in "
91 "initializer; did you mean to "
92 "invoke a superclass initializer?")
93 << Message
->getMethodDecl();
95 SourceLocation ReceiverLoc
= Message
->getReceiverRange().getBegin();
96 if (ReceiverLoc
.isMacroID() || ReceiverLoc
.isInvalid())
99 SourceLocation SelectorLoc
= Message
->getSelectorStartLoc();
100 if (SelectorLoc
.isMacroID() || SelectorLoc
.isInvalid())
103 Diag
<< FixItHint::CreateReplacement(Message
->getSourceRange(),
104 StringRef("[super init]"));