Files
clang-p2996/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrMatchers.cpp
Ariel J. Bernal f78debd7d2 Refactor Usenullptr matcher to avoid duplication
Previously UseNullptr matched separately implicit and explicit casts to nullptr,
now it matches casts that either are implict casts to nullptr or have an
implicit cast to nullptr within.

Also fixes PR15572 since the same macro replacement logic is applied to implicit
and explicit casts.

llvm-svn: 178494
2013-04-01 20:09:29 +00:00

68 lines
1.9 KiB
C++

//===-- nullptr-convert/Matchers.cpp - Matchers for null casts ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file contains the definitions for matcher-generating functions
/// and a custom AST_MATCHER for identifying casts of type CK_NullTo*.
///
//===----------------------------------------------------------------------===//
#include "NullptrMatchers.h"
#include "clang/AST/ASTContext.h"
using namespace clang::ast_matchers;
using namespace clang;
const char *CastSequence = "sequence";
namespace clang {
namespace ast_matchers {
/// \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))
return BT->getKind() == BuiltinType::NullPtr;
return false;
}
} // end namespace ast_matchers
} // end namespace clang
StatementMatcher makeCastSequenceMatcher() {
StatementMatcher ImplicitCastToNull =
implicitCastExpr(
isNullToPointer(),
unless(
hasSourceExpression(
hasType(sugaredNullptrType())
)
)
);
return castExpr(
unless(hasAncestor(explicitCastExpr())),
anyOf(
hasDescendant(ImplicitCastToNull),
ImplicitCastToNull
)
).bind(CastSequence);
}