AST Matchers
------------
-- hasAnyArgument: Matcher no longer ignores parentheses and implicit casts on
- the argument before applying the inner matcher. The fix was done to allow for
- greater control by the user. In all existing checkers that use this matcher
- all instances of code ``hasAnyArgument(<inner matcher>)`` must be changed to
- ``hasAnyArgument(ignoringParenImpCasts(<inner matcher>))``.
+- has and hasAnyArgument: Matchers no longer ignores parentheses and implicit
+ casts on the argument before applying the inner matcher. The fix was done to
+ allow for greater control by the user. In all existing checkers that use this
+ matcher all instances of code ``hasAnyArgument(<inner matcher>)`` or
+ ``has(<inner matcher>)`` must be changed to
+ ``hasAnyArgument(ignoringParenImpCasts(<inner matcher>))`` or
+ ``has(ignoringParenImpCasts(<inner matcher>))``.
...
/// ChildT must be an AST base type.
///
/// Usable as: Any Matcher
+/// Note that has is direct matcher, so it also matches things like implicit
+/// casts and paren casts. If you are matching with expr then you should
+/// probably consider using ignoringParenImpCasts like:
+/// has(ignoringParenImpCasts(expr())).
const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher>
LLVM_ATTRIBUTE_UNUSED has = {};
/// ChildT must be an AST base type.
template <typename T, typename ChildT>
class HasMatcher : public WrapperMatcherInterface<T> {
- static_assert(IsBaseType<ChildT>::value,
- "has only accepts base type matcher");
public:
explicit HasMatcher(const Matcher<ChildT> &ChildMatcher)
bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override {
- return Finder->matchesChildOf(
- Node, this->InnerMatcher, Builder,
- ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
- ASTMatchFinder::BK_First);
+ return Finder->matchesChildOf(Node, this->InnerMatcher, Builder,
+ ASTMatchFinder::TK_AsIs,
+ ASTMatchFinder::BK_First);
}
};
add_subdirectory(Sema)
add_subdirectory(Serialization)
add_subdirectory(StaticAnalyzer/Checkers)
+add_subdirectory(ASTMatchers)
TEST(ImportExpr, ImportAtomicExpr) {
MatchVerifier<Decl> Verifier;
- EXPECT_TRUE(
- testImport(
- "void declToImport() { int *ptr; __atomic_load_n(ptr, 1); }",
- Lang_CXX, "", Lang_CXX, Verifier,
- functionDecl(
- hasBody(
- compoundStmt(
- has(
- atomicExpr(
- has(declRefExpr(
- hasDeclaration(varDecl(hasName("ptr"))),
- hasType(asString("int *")))),
- has(integerLiteral(equals(1), hasType(asString("int"))))
- )))))));
+ EXPECT_TRUE(testImport(
+ "void declToImport() { int *ptr; __atomic_load_n(ptr, 1); }", Lang_CXX,
+ "", Lang_CXX, Verifier,
+ functionDecl(hasBody(compoundStmt(has(atomicExpr(
+ has(ignoringParenImpCasts(
+ declRefExpr(hasDeclaration(varDecl(hasName("ptr"))),
+ hasType(asString("int *"))))),
+ has(integerLiteral(equals(1), hasType(asString("int")))))))))));
}
TEST(ImportExpr, ImportLabelDeclAndAddrLabelExpr) {
TEST(StatementMatcher, Has) {
StatementMatcher HasVariableI =
- expr(hasType(pointsTo(recordDecl(hasName("X")))),
- has(declRefExpr(to(varDecl(hasName("i"))))));
+ expr(hasType(pointsTo(recordDecl(hasName("X")))),
+ has(ignoringParenImpCasts(declRefExpr(to(varDecl(hasName("i")))))));
EXPECT_TRUE(matches(
"class X; X *x(int); void c() { int i; x(i); }", HasVariableI));