2019-04-18 06:29:06 +08:00
|
|
|
//===--- SuperSelfCheck.cpp - clang-tidy ----------------------------------===//
|
|
|
|
//
|
2021-08-05 11:16:17 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-04-18 06:29:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SuperSelfCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace objc {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Matches Objective-C methods in the initializer family.
|
2019-04-18 06:29:06 +08:00
|
|
|
///
|
|
|
|
/// Example matches -init and -initWithInt:.
|
|
|
|
/// (matcher = objcMethodDecl(isInitializer()))
|
|
|
|
/// \code
|
|
|
|
/// @interface Foo
|
|
|
|
/// - (instancetype)init;
|
|
|
|
/// - (instancetype)initWithInt:(int)i;
|
|
|
|
/// + (instancetype)init;
|
|
|
|
/// - (void)bar;
|
|
|
|
/// @end
|
|
|
|
/// \endcode
|
|
|
|
AST_MATCHER(ObjCMethodDecl, isInitializer) {
|
|
|
|
return Node.getMethodFamily() == OMF_init;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Matches Objective-C implementations with interfaces that match
|
2019-08-16 10:27:58 +08:00
|
|
|
/// \c Base.
|
2019-04-18 06:29:06 +08:00
|
|
|
///
|
2019-08-16 10:27:58 +08:00
|
|
|
/// Example matches implementation declarations for X.
|
|
|
|
/// (matcher = objcImplementationDecl(hasInterface(hasName("X"))))
|
2019-04-18 06:29:06 +08:00
|
|
|
/// \code
|
|
|
|
/// @interface X
|
|
|
|
/// @end
|
2019-08-16 10:27:58 +08:00
|
|
|
/// @implementation X
|
2019-04-18 06:29:06 +08:00
|
|
|
/// @end
|
2019-08-16 10:27:58 +08:00
|
|
|
/// @interface Y
|
|
|
|
// @end
|
|
|
|
/// @implementation Y
|
2019-04-18 06:29:06 +08:00
|
|
|
/// @end
|
|
|
|
/// \endcode
|
2019-08-16 10:27:58 +08:00
|
|
|
AST_MATCHER_P(ObjCImplementationDecl, hasInterface,
|
|
|
|
ast_matchers::internal::Matcher<ObjCInterfaceDecl>, Base) {
|
|
|
|
const ObjCInterfaceDecl *InterfaceDecl = Node.getClassInterface();
|
|
|
|
return Base.matches(*InterfaceDecl, Finder, Builder);
|
2019-04-18 06:29:06 +08:00
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Matches Objective-C message expressions where the receiver is the
|
2019-04-18 06:29:06 +08:00
|
|
|
/// super instance.
|
|
|
|
///
|
|
|
|
/// Example matches the invocations of -banana and -orange.
|
|
|
|
/// (matcher = objcMessageExpr(isMessagingSuperInstance()))
|
|
|
|
/// \code
|
|
|
|
/// - (void)banana {
|
|
|
|
/// [self apple]
|
|
|
|
/// [super banana];
|
|
|
|
/// [super orange];
|
|
|
|
/// }
|
|
|
|
/// \endcode
|
|
|
|
AST_MATCHER(ObjCMessageExpr, isMessagingSuperInstance) {
|
|
|
|
return Node.getReceiverKind() == ObjCMessageExpr::SuperInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void SuperSelfCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(
|
2019-08-16 10:27:58 +08:00
|
|
|
objcMessageExpr(hasSelector("self"), isMessagingSuperInstance(),
|
|
|
|
hasAncestor(objcMethodDecl(
|
|
|
|
isInitializer(),
|
|
|
|
hasDeclContext(objcImplementationDecl(hasInterface(
|
|
|
|
isDerivedFrom(hasName("NSObject"))))))))
|
2019-04-18 06:29:06 +08:00
|
|
|
.bind("message"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuperSelfCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Message = Result.Nodes.getNodeAs<ObjCMessageExpr>("message");
|
|
|
|
|
|
|
|
auto Diag = diag(Message->getExprLoc(), "suspicious invocation of %0 in "
|
|
|
|
"initializer; did you mean to "
|
|
|
|
"invoke a superclass initializer?")
|
|
|
|
<< Message->getMethodDecl();
|
|
|
|
|
|
|
|
SourceLocation ReceiverLoc = Message->getReceiverRange().getBegin();
|
|
|
|
if (ReceiverLoc.isMacroID() || ReceiverLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceLocation SelectorLoc = Message->getSelectorStartLoc();
|
|
|
|
if (SelectorLoc.isMacroID() || SelectorLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Diag << FixItHint::CreateReplacement(Message->getSourceRange(),
|
|
|
|
StringRef("[super init]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace objc
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|