Skip to content

Commit

Permalink
[clang-tidy] Lift common matchers to utils namespace
Browse files Browse the repository at this point in the history
Summary:
This patch is lifting matchers used by more than one checkers
to the common namespace.

Reviewers: aaron.ballman, alexfh

Subscribers: aaron.ballman, cfe-commits

Differential Revision: http://reviews.llvm.org/D19841

llvm-svn: 269804
  • Loading branch information
bergeret committed May 17, 2016
1 parent de96f39 commit e15ef2f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 43 deletions.
Expand Up @@ -15,14 +15,6 @@ namespace clang {
namespace tidy {
namespace misc {

namespace {

AST_MATCHER(CastExpr, isPointerToBoolean) {
return Node.getCastKind() == CK_PointerToBoolean;
}

} // namespace

void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
// Look for ifs that have an implicit bool* to bool conversion in the
// condition. Filter negations.
Expand All @@ -32,7 +24,7 @@ void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
hasSourceExpression(expr(
hasType(pointerType(pointee(booleanType()))),
ignoringParenImpCasts(declRefExpr().bind("expr")))),
isPointerToBoolean())))),
hasCastKind(CK_PointerToBoolean))))),
unless(isInTemplateInstantiation())).bind("if"),
this);
}
Expand Down
17 changes: 2 additions & 15 deletions clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
Expand Up @@ -24,20 +24,6 @@ namespace {

const char CastSequence[] = "sequence";

/// \brief Matches cast expressions that have a cast kind of CK_NullToPointer
/// or CK_NullToMemberPointer.
///
/// Given
/// \code
/// int *p = 0;
/// \endcode
/// implicitCastExpr(isNullToPointer()) matches the implicit cast clang adds
/// around \c 0.
AST_MATCHER(CastExpr, isNullToPointer) {
return Node.getCastKind() == CK_NullToPointer ||
Node.getCastKind() == CK_NullToMemberPointer;
}

AST_MATCHER(Type, sugaredNullptrType) {
const Type *DesugaredType = Node.getUnqualifiedDesugaredType();
if (const BuiltinType *BT = dyn_cast<BuiltinType>(DesugaredType))
Expand All @@ -52,7 +38,8 @@ AST_MATCHER(Type, sugaredNullptrType) {
/// can be replaced instead of just the inner-most implicit cast.
StatementMatcher makeCastSequenceMatcher() {
StatementMatcher ImplicitCastToNull = implicitCastExpr(
isNullToPointer(),
anyOf(hasCastKind(CK_NullToPointer),
hasCastKind(CK_NullToMemberPointer)),
unless(hasSourceExpression(hasType(sugaredNullptrType()))));

return castExpr(anyOf(ImplicitCastToNull,
Expand Down
Expand Up @@ -38,8 +38,6 @@ llvm::Optional<std::string> MakeCharacterLiteral(const StringLiteral *Literal) {
return Result;
}

AST_MATCHER(StringLiteral, lengthIsOne) { return Node.getLength() == 1; }

AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<Expr>,
hasSubstitutedType) {
return hasType(qualType(anyOf(substTemplateTypeParmType(),
Expand All @@ -65,7 +63,7 @@ void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) {
return;

const auto SingleChar =
expr(ignoringParenCasts(stringLiteral(lengthIsOne()).bind("literal")));
expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")));

const auto StringFindFunctions =
anyOf(hasName("find"), hasName("rfind"), hasName("find_first_of"),
Expand Down
Expand Up @@ -20,10 +20,6 @@ namespace readability {

namespace {

AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) {
return Node.getCastKind() == Kind;
}

AST_MATCHER(Stmt, isMacroExpansion) {
SourceManager &SM = Finder->getASTContext().getSourceManager();
SourceLocation Loc = Node.getLocStart();
Expand Down
Expand Up @@ -8,25 +8,16 @@
//===----------------------------------------------------------------------===//

#include "RedundantStringInitCheck.h"
#include "../utils/Matchers.h"
#include "clang/ASTMatchers/ASTMatchers.h"

using namespace clang::ast_matchers;
using namespace clang::tidy::matchers;

namespace clang {
namespace tidy {
namespace readability {

namespace {

AST_MATCHER(StringLiteral, lengthIsZero) { return Node.getLength() == 0; }

AST_MATCHER_P(Expr, ignoringImplicit,
ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
}

} // namespace

void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
Expand All @@ -45,7 +36,7 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
const auto EmptyStringCtorExpr =
cxxConstructExpr(StringConstructorExpr,
hasArgument(0, ignoringParenImpCasts(
stringLiteral(lengthIsZero()))));
stringLiteral(hasSize(0)))));

const auto EmptyStringCtorExprWithTemporaries =
expr(ignoringImplicit(
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clang-tidy/utils/Matchers.h
Expand Up @@ -17,6 +17,11 @@ namespace clang {
namespace tidy {
namespace matchers {

AST_MATCHER_P(Expr, ignoringImplicit,
ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
}

AST_MATCHER(BinaryOperator, isRelationalOperator) {
return Node.isRelationalOp();
}
Expand Down

0 comments on commit e15ef2f

Please sign in to comment.