clang-tools  11.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  Finder->addMatcher(
79  objcMessageExpr(hasSelector("self"), isMessagingSuperInstance(),
80  hasAncestor(objcMethodDecl(
81  isInitializer(),
82  hasDeclContext(objcImplementationDecl(hasInterface(
83  isDerivedFrom(hasName("NSObject"))))))))
84  .bind("message"),
85  this);
86 }
87 
88 void SuperSelfCheck::check(const MatchFinder::MatchResult &Result) {
89  const auto *Message = Result.Nodes.getNodeAs<ObjCMessageExpr>("message");
90 
91  auto Diag = diag(Message->getExprLoc(), "suspicious invocation of %0 in "
92  "initializer; did you mean to "
93  "invoke a superclass initializer?")
94  << Message->getMethodDecl();
95 
96  SourceLocation ReceiverLoc = Message->getReceiverRange().getBegin();
97  if (ReceiverLoc.isMacroID() || ReceiverLoc.isInvalid())
98  return;
99 
100  SourceLocation SelectorLoc = Message->getSelectorStartLoc();
101  if (SelectorLoc.isMacroID() || SelectorLoc.isInvalid())
102  return;
103 
104  Diag << FixItHint::CreateReplacement(Message->getSourceRange(),
105  StringRef("[super init]"));
106 }
107 
108 } // namespace objc
109 } // namespace tidy
110 } // namespace clang
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:85
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
clang::ast_matchers
Definition: AbseilMatcher.h:14
SuperSelfCheck.h
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:53
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27