[clang-tidy] Cleanup some ast-matchers and lift some to utils.

Summary:
Little cleanup to lift-out and to remove some frequently used
ast-matchers.

Some of theses matchers are candidates to be lifted to ASTMatchers.h.

Reviewers: alexfh

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@267003 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Etienne Bergeron 2016-04-21 16:57:56 +00:00
Родитель d1f3b6cd34
Коммит 4ad9607fb2
10 изменённых файлов: 36 добавлений и 81 удалений

Просмотреть файл

@ -17,7 +17,6 @@ namespace {
AST_MATCHER(CastExpr, isPointerToBoolean) {
return Node.getCastKind() == CK_PointerToBoolean;
}
AST_MATCHER(QualType, isBoolean) { return Node->isBooleanType(); }
} // namespace
@ -31,7 +30,7 @@ void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
ifStmt(hasCondition(findAll(implicitCastExpr(
allOf(unless(hasParent(unaryOperator(hasOperatorName("!")))),
hasSourceExpression(expr(
hasType(pointerType(pointee(isBoolean()))),
hasType(pointerType(pointee(booleanType()))),
ignoringParenImpCasts(declRefExpr().bind("expr")))),
isPointerToBoolean())))),
unless(isInTemplateInstantiation())).bind("if"),

Просмотреть файл

@ -24,11 +24,6 @@ AST_MATCHER(FloatingLiteral, floatHalf) {
return literal.convertToDouble() == 0.5;
return false;
}
// TODO(hokein): Moving it to ASTMatchers.h
AST_MATCHER(BuiltinType, isFloatingPoint) {
return Node.isFloatingPoint();
}
} // namespace ast_matchers
} // namespace clang
@ -42,7 +37,7 @@ void IncorrectRoundings::registerMatchers(MatchFinder *MatchFinder) {
auto FloatHalf = floatLiteral(floatHalf());
// Match a floating point expression.
auto FloatType = expr(hasType(builtinType(isFloatingPoint())));
auto FloatType = expr(hasType(realFloatingPointType()));
// Match a floating literal of 0.5 or a floating literal of 0.5 implicitly.
// cast to floating type.

Просмотреть файл

@ -18,8 +18,8 @@ namespace tidy {
namespace {
class MacroParenthesesPPCallbacks : public PPCallbacks {
public:
explicit MacroParenthesesPPCallbacks(Preprocessor *PP,
MacroParenthesesCheck *Check)
MacroParenthesesPPCallbacks(Preprocessor *PP,
MacroParenthesesCheck *Check)
: PP(PP), Check(Check) {}
void MacroDefined(const Token &MacroNameTok,

Просмотреть файл

@ -10,6 +10,7 @@
#include "MisplacedWideningCastCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "../utils/Matchers.h"
using namespace clang::ast_matchers;
@ -47,9 +48,7 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(callExpr(hasAnyArgument(Cast)), this);
Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this);
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!="),
hasOperatorName("<"), hasOperatorName("<="),
hasOperatorName(">"), hasOperatorName(">=")),
binaryOperator(matchers::isComparisonOperator(),
hasEitherOperand(Cast)),
this);
}

Просмотреть файл

@ -10,6 +10,7 @@
#include "PointerAndIntegralOperationCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "../utils/Matchers.h"
using namespace clang::ast_matchers;
@ -30,8 +31,7 @@ void PointerAndIntegralOperationCheck::registerMatchers(MatchFinder *Finder) {
binaryOperator(hasOperatorName("="), hasLHS(PointerExpr));
const auto CompareToPointerExpr =
binaryOperator(anyOf(hasOperatorName("<"), hasOperatorName("<="),
hasOperatorName(">"), hasOperatorName(">=")),
binaryOperator(matchers::isRelationalOperator(),
hasEitherOperand(PointerExpr));
// Detect expression like: ptr = (x != y);

Просмотреть файл

@ -10,6 +10,7 @@
#include "SizeofExpressionCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "../utils/Matchers.h"
using namespace clang::ast_matchers;
@ -19,10 +20,6 @@ namespace misc {
namespace {
AST_MATCHER(BinaryOperator, isRelationalOperator) {
return Node.isRelationalOp();
}
AST_MATCHER_P(IntegerLiteral, isBiggerThan, unsigned, N) {
return Node.getValue().getZExtValue() > N;
}
@ -139,7 +136,7 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
// Detect expression like: sizeof(epxr) <= k for a suspicious constant 'k'.
if (WarnOnSizeOfCompareToConstant) {
Finder->addMatcher(
binaryOperator(isRelationalOperator(),
binaryOperator(matchers::isRelationalOperator(),
hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
hasEitherOperand(ignoringParenImpCasts(
anyOf(integerLiteral(equals(0)),

Просмотреть файл

@ -16,22 +16,6 @@
using namespace clang::ast_matchers;
namespace clang {
namespace {
bool isShrinkableContainer(llvm::StringRef ClassName) {
static const char *const Shrinkables[] = {
"std::basic_string",
"std::deque",
"std::vector"
};
return std::binary_search(std::begin(Shrinkables), std::end(Shrinkables),
ClassName);
}
AST_MATCHER(NamedDecl, stlShrinkableContainer) {
return isShrinkableContainer(Node.getQualifiedNameAsString());
}
} // namespace
namespace tidy {
namespace modernize {
@ -54,7 +38,8 @@ void ShrinkToFitCheck::registerMatchers(MatchFinder *Finder) {
has(declRefExpr(hasDeclaration(equalsBoundNode("ContainerDecl")))))));
Finder->addMatcher(
cxxMemberCallExpr(on(hasType(namedDecl(stlShrinkableContainer()))),
cxxMemberCallExpr(on(hasType(namedDecl(
hasAnyName("std::basic_string", "std::deque", "std::vector")))),
callee(cxxMethodDecl(hasName("swap"))),
has(memberExpr(hasDescendant(CopyCtorCall))),
hasArgument(0, SwapParam.bind("ContainerToShrink")),

Просмотреть файл

@ -11,41 +11,11 @@
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/StringRef.h"
#include "../utils/Matchers.h"
using namespace clang::ast_matchers;
static bool isContainerName(llvm::StringRef ClassName) {
static const char *const ContainerNames[] = {"array",
"basic_string",
"deque",
"forward_list",
"list",
"map",
"multimap",
"multiset",
"priority_queue",
"queue",
"set",
"stack",
"unordered_map",
"unordered_multimap",
"unordered_multiset",
"unordered_set",
"vector"};
return std::binary_search(std::begin(ContainerNames),
std::end(ContainerNames), ClassName);
}
namespace clang {
namespace {
AST_MATCHER(NamedDecl, stlContainer) {
if (!isContainerName(Node.getName()))
return false;
return StringRef(Node.getQualifiedNameAsString()).startswith("std::");
}
} // namespace
namespace tidy {
namespace readability {
@ -59,11 +29,15 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
const auto stlContainer = hasAnyName(
"array", "basic_string", "deque", "forward_list", "list", "map",
"multimap", "multiset", "priority_queue", "queue", "set", "stack",
"unordered_map", "unordered_multimap", "unordered_multiset",
"unordered_set", "vector");
const auto WrongUse = anyOf(
hasParent(binaryOperator(
anyOf(hasOperatorName("<"), hasOperatorName(">="),
hasOperatorName(">"), hasOperatorName("<="),
hasOperatorName("=="), hasOperatorName("!=")),
matchers::isComparisonOperator(),
hasEitherOperand(ignoringImpCasts(anyOf(
integerLiteral(equals(1)), integerLiteral(equals(0))))))
.bind("SizeBinaryOp")),
@ -76,9 +50,9 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxMemberCallExpr(
on(expr(anyOf(hasType(namedDecl(stlContainer())),
hasType(pointsTo(namedDecl(stlContainer()))),
hasType(references(namedDecl(stlContainer())))))
on(expr(anyOf(hasType(namedDecl(stlContainer)),
hasType(pointsTo(namedDecl(stlContainer))),
hasType(references(namedDecl(stlContainer)))))
.bind("STLObject")),
callee(cxxMethodDecl(hasName("size"))), WrongUse)
.bind("SizeCallExpr"),

Просмотреть файл

@ -19,16 +19,10 @@ namespace tidy {
namespace {
const internal::VariadicDynCastAllOfMatcher<Stmt, ParenExpr> parenExpr;
AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) {
return Node.getCastKind() == Kind;
}
AST_MATCHER(QualType, isBool) {
return !Node.isNull() && Node->isBooleanType();
}
AST_MATCHER(Stmt, isMacroExpansion) {
SourceManager &SM = Finder->getASTContext().getSourceManager();
SourceLocation Loc = Node.getLocStart();
@ -62,7 +56,7 @@ StatementMatcher createImplicitCastFromBoolMatcher() {
allOf(anyOf(hasCastKind(CK_NullToPointer),
hasCastKind(CK_NullToMemberPointer)),
hasSourceExpression(cxxBoolLiteral()))),
hasSourceExpression(expr(hasType(qualType(isBool())))));
hasSourceExpression(expr(hasType(qualType(booleanType())))));
}
StringRef

Просмотреть файл

@ -17,6 +17,18 @@ namespace clang {
namespace tidy {
namespace matchers {
AST_MATCHER(BinaryOperator, isRelationalOperator) {
return Node.isRelationalOp();
}
AST_MATCHER(BinaryOperator, isEqualityOperator) {
return Node.isEqualityOp();
}
AST_MATCHER(BinaryOperator, isComparisonOperator) {
return Node.isComparisonOp();
}
AST_MATCHER(QualType, isExpensiveToCopy) {
llvm::Optional<bool> IsExpensive =
type_traits::isExpensiveToCopy(Node, Finder->getASTContext());