forked from OSchip/llvm-project
Fixed HasDeclarationMatcher to properly convert all types into decls where possible. Added objcObjectPointerType(), objcInterfaceDecl(), templateTypeParmType(), injectedClassNameType(), and unresolvedUsingTypenameDecl(). Updated documentation for pointerType() to call out that it does not match ObjCObjectPointerType types. Changed pointsTo() to handle ObjCObjectPointerType as well as PointerType.
While this may seem like a lot of unrelated changes, they all relate back to fixing HasDeclarationMatcher. This now allows us to write a matcher like: varDecl(hasType(namedDecl(hasName("Foo")))) that matches code using typedefs, objc interfaces, template type parameters, injected class names, or unresolved using typenames. llvm-svn: 247404
This commit is contained in:
parent
13ce0fd140
commit
b85be665b0
|
@ -307,6 +307,15 @@ namespaceDecl()
|
|||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('objcInterfaceDecl0')"><a name="objcInterfaceDecl0Anchor">objcInterfaceDecl</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ObjCInterfaceDecl.html">ObjCInterfaceDecl</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="objcInterfaceDecl0"><pre>Matches Objective-C interface declarations.
|
||||
|
||||
Example matches Foo
|
||||
@interface Foo
|
||||
@end
|
||||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('parmVarDecl0')"><a name="parmVarDecl0Anchor">parmVarDecl</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="parmVarDecl0"><pre>Matches parameter variable declarations.
|
||||
|
||||
|
@ -364,6 +373,22 @@ typedefDecl()
|
|||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('unresolvedUsingTypenameDecl0')"><a name="unresolvedUsingTypenameDecl0Anchor">unresolvedUsingTypenameDecl</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1UnresolvedUsingTypenameDecl.html">UnresolvedUsingTypenameDecl</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="unresolvedUsingTypenameDecl0"><pre>Matches unresolved using value declarations that involve the
|
||||
typename.
|
||||
|
||||
Given
|
||||
template <typename T>
|
||||
struct Base { typedef T Foo; };
|
||||
|
||||
template<typename T>
|
||||
struct S : private Base<T> {
|
||||
using typename Base<T>::Foo;
|
||||
};
|
||||
unresolvedUsingTypenameDecl()
|
||||
matches using Base<T>::Foo </pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('unresolvedUsingValueDecl0')"><a name="unresolvedUsingValueDecl0Anchor">unresolvedUsingValueDecl</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1UnresolvedUsingValueDecl.html">UnresolvedUsingValueDecl</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="unresolvedUsingValueDecl0"><pre>Matches unresolved using value declarations.
|
||||
|
||||
|
@ -1254,6 +1279,18 @@ incompleteArrayType()
|
|||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('injectedClassNameType0')"><a name="injectedClassNameType0Anchor">injectedClassNameType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1InjectedClassNameType.html">InjectedClassNameType</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="injectedClassNameType0"><pre>Matches injected class name types.
|
||||
|
||||
Example matches S s, but not S<T> s.
|
||||
(matcher = parmVarDecl(hasType(injectedClassNameType())))
|
||||
template <typename T> struct S {
|
||||
void f(S s);
|
||||
void g(S<T> s);
|
||||
};
|
||||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('lValueReferenceType0')"><a name="lValueReferenceType0Anchor">lValueReferenceType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1LValueReferenceType.html">LValueReferenceType</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="lValueReferenceType0"><pre>Matches lvalue reference types.
|
||||
|
||||
|
@ -1281,6 +1318,21 @@ memberPointerType()
|
|||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('objcObjectPointerType0')"><a name="objcObjectPointerType0Anchor">objcObjectPointerType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ObjCObjectPointerType.html">ObjCObjectPointerType</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="objcObjectPointerType0"><pre>Matches an Objective-C object pointer type, which is different from
|
||||
a pointer type, despite being syntactically similar.
|
||||
|
||||
Given
|
||||
int *a;
|
||||
|
||||
@interface Foo
|
||||
@end
|
||||
Foo *f;
|
||||
pointerType()
|
||||
matches "Foo *f", but does not match "int *a".
|
||||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('parenType0')"><a name="parenType0Anchor">parenType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParenType.html">ParenType</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="parenType0"><pre>Matches ParenType nodes.
|
||||
|
||||
|
@ -1294,14 +1346,19 @@ array_of_ptrs.
|
|||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('pointerType0')"><a name="pointerType0Anchor">pointerType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1PointerType.html">PointerType</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="pointerType0"><pre>Matches pointer types.
|
||||
<tr><td colspan="4" class="doc" id="pointerType0"><pre>Matches pointer types, but does not match Objective-C object pointer
|
||||
types.
|
||||
|
||||
Given
|
||||
int *a;
|
||||
int &b = *a;
|
||||
int c = 5;
|
||||
|
||||
@interface Foo
|
||||
@end
|
||||
Foo *f;
|
||||
pointerType()
|
||||
matches "int *a"
|
||||
matches "int *a", but does not match "Foo *f".
|
||||
</pre></td></tr>
|
||||
|
||||
|
||||
|
@ -1382,6 +1439,15 @@ instantiation in A and the type of the variable declaration in B.
|
|||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('templateTypeParmType0')"><a name="templateTypeParmType0Anchor">templateTypeParmType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateTypeParmType.html">TemplateTypeParmType</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="templateTypeParmType0"><pre>Matches template type parameter types.
|
||||
|
||||
Example matches T, but not int.
|
||||
(matcher = templateTypeParmType())
|
||||
template <typename T> void f(int i);
|
||||
</pre></td></tr>
|
||||
|
||||
|
||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('type0')"><a name="type0Anchor">type</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>>...</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="type0"><pre>Matches Types in the clang AST.
|
||||
</pre></td></tr>
|
||||
|
@ -2280,7 +2346,7 @@ a substring matched by the given RegExp.
|
|||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ObjCMessageExpr.html">ObjCMessageExpr</a>></td><td class="name" onclick="toggle('numSelectorArgs0')"><a name="numSelectorArgs0Anchor">numSelectorArgs</a></td><td>unsigned N</td></tr>
|
||||
<tr><td colspan="4" class="doc" id="numSelectorArgs0"><pre>Matches when the selector has the specified number of arguments
|
||||
|
||||
matcher = objCMessageExpr(numSelectorArgs(1));
|
||||
matcher = objCMessageExpr(numSelectorArgs(0));
|
||||
matches self.bodyView in the code below
|
||||
|
||||
matcher = objCMessageExpr(numSelectorArgs(2));
|
||||
|
|
|
@ -904,6 +904,16 @@ const internal::VariadicDynCastAllOfMatcher<
|
|||
Stmt,
|
||||
ObjCMessageExpr> objcMessageExpr;
|
||||
|
||||
/// \brief Matches Objective-C interface declarations.
|
||||
///
|
||||
/// Example matches Foo
|
||||
/// \code
|
||||
/// @interface Foo
|
||||
/// @end
|
||||
/// \endcode
|
||||
const internal::VariadicDynCastAllOfMatcher<
|
||||
Decl,
|
||||
ObjCInterfaceDecl> objcInterfaceDecl;
|
||||
|
||||
/// \brief Matches expressions that introduce cleanups to be run at the end
|
||||
/// of the sub-expression's evaluation.
|
||||
|
@ -978,6 +988,25 @@ const internal::VariadicDynCastAllOfMatcher<
|
|||
Decl,
|
||||
UnresolvedUsingValueDecl> unresolvedUsingValueDecl;
|
||||
|
||||
/// \brief Matches unresolved using value declarations that involve the
|
||||
/// typename.
|
||||
///
|
||||
/// Given
|
||||
/// \code
|
||||
/// template <typename T>
|
||||
/// struct Base { typedef T Foo; };
|
||||
///
|
||||
/// template<typename T>
|
||||
/// struct S : private Base<T> {
|
||||
/// using typename Base<T>::Foo;
|
||||
/// };
|
||||
/// \endcode
|
||||
/// unresolvedUsingTypenameDecl()
|
||||
/// matches \code using Base<T>::Foo \endcode
|
||||
const internal::VariadicDynCastAllOfMatcher<
|
||||
Decl,
|
||||
UnresolvedUsingTypenameDecl> unresolvedUsingTypenameDecl;
|
||||
|
||||
/// \brief Matches constructor call expressions (including implicit ones).
|
||||
///
|
||||
/// Example matches string(ptr, n) and ptr within arguments of f
|
||||
|
@ -2282,7 +2311,7 @@ AST_MATCHER_P(QualType, asString, std::string, Name) {
|
|||
AST_MATCHER_P(
|
||||
QualType, pointsTo, internal::Matcher<QualType>,
|
||||
InnerMatcher) {
|
||||
return (!Node.isNull() && Node->isPointerType() &&
|
||||
return (!Node.isNull() && Node->isAnyPointerType() &&
|
||||
InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
|
||||
}
|
||||
|
||||
|
@ -3754,18 +3783,38 @@ AST_TYPE_MATCHER(BlockPointerType, blockPointerType);
|
|||
/// matches "A::* ptr"
|
||||
AST_TYPE_MATCHER(MemberPointerType, memberPointerType);
|
||||
|
||||
/// \brief Matches pointer types.
|
||||
/// \brief Matches pointer types, but does not match Objective-C object pointer
|
||||
/// types.
|
||||
///
|
||||
/// Given
|
||||
/// \code
|
||||
/// int *a;
|
||||
/// int &b = *a;
|
||||
/// int c = 5;
|
||||
///
|
||||
/// @interface Foo
|
||||
/// @end
|
||||
/// Foo *f;
|
||||
/// \endcode
|
||||
/// pointerType()
|
||||
/// matches "int *a"
|
||||
/// matches "int *a", but does not match "Foo *f".
|
||||
AST_TYPE_MATCHER(PointerType, pointerType);
|
||||
|
||||
/// \brief Matches an Objective-C object pointer type, which is different from
|
||||
/// a pointer type, despite being syntactically similar.
|
||||
///
|
||||
/// Given
|
||||
/// \code
|
||||
/// int *a;
|
||||
///
|
||||
/// @interface Foo
|
||||
/// @end
|
||||
/// Foo *f;
|
||||
/// \endcode
|
||||
/// pointerType()
|
||||
/// matches "Foo *f", but does not match "int *a".
|
||||
AST_TYPE_MATCHER(ObjCObjectPointerType, objcObjectPointerType);
|
||||
|
||||
/// \brief Matches both lvalue and rvalue reference types.
|
||||
///
|
||||
/// Given
|
||||
|
@ -3963,6 +4012,27 @@ AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>,
|
|||
/// \c substTemplateTypeParmType() matches the type of 't' but not '1'
|
||||
AST_TYPE_MATCHER(SubstTemplateTypeParmType, substTemplateTypeParmType);
|
||||
|
||||
/// \brief Matches template type parameter types.
|
||||
///
|
||||
/// Example matches T, but not int.
|
||||
/// (matcher = templateTypeParmType())
|
||||
/// \code
|
||||
/// template <typename T> void f(int i);
|
||||
/// \endcode
|
||||
AST_TYPE_MATCHER(TemplateTypeParmType, templateTypeParmType);
|
||||
|
||||
/// \brief Matches injected class name types.
|
||||
///
|
||||
/// Example matches S s, but not S<T> s.
|
||||
/// (matcher = parmVarDecl(hasType(injectedClassNameType())))
|
||||
/// \code
|
||||
/// template <typename T> struct S {
|
||||
/// void f(S s);
|
||||
/// void g(S<T> s);
|
||||
/// };
|
||||
/// \endcode
|
||||
AST_TYPE_MATCHER(InjectedClassNameType, injectedClassNameType);
|
||||
|
||||
/// \brief Matches declarations whose declaration context, interpreted as a
|
||||
/// Decl, matches \c InnerMatcher.
|
||||
///
|
||||
|
|
|
@ -672,10 +672,26 @@ private:
|
|||
/// matcher matches on it.
|
||||
bool matchesSpecialized(const QualType &Node, ASTMatchFinder *Finder,
|
||||
BoundNodesTreeBuilder *Builder) const {
|
||||
/// FIXME: Add other ways to convert...
|
||||
if (Node.isNull())
|
||||
return false;
|
||||
return matchesDecl(Node->getAsTagDecl(), Finder, Builder);
|
||||
|
||||
if (auto *TD = Node->getAsTagDecl())
|
||||
return matchesDecl(TD, Finder, Builder);
|
||||
else if (auto *TT = Node->getAs<TypedefType>())
|
||||
return matchesDecl(TT->getDecl(), Finder, Builder);
|
||||
// Do not use getAs<TemplateTypeParmType> instead of the direct dyn_cast.
|
||||
// Calling getAs will return the canonical type, but that type does not
|
||||
// store a TemplateTypeParmDecl. We *need* the uncanonical type, if it is
|
||||
// available, and using dyn_cast ensures that.
|
||||
else if (auto *TTP = dyn_cast<TemplateTypeParmType>(Node.getTypePtr()))
|
||||
return matchesDecl(TTP->getDecl(), Finder, Builder);
|
||||
else if (auto *OCIT = Node->getAs<ObjCInterfaceType>())
|
||||
return matchesDecl(OCIT->getDecl(), Finder, Builder);
|
||||
else if (auto *UUT = Node->getAs<UnresolvedUsingType>())
|
||||
return matchesDecl(UUT->getDecl(), Finder, Builder);
|
||||
else if (auto *ICNT = Node->getAs<InjectedClassNameType>())
|
||||
return matchesDecl(ICNT->getDecl(), Finder, Builder);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// \brief Gets the TemplateDecl from a TemplateSpecializationType
|
||||
|
|
|
@ -238,6 +238,7 @@ RegistryMaps::RegistryMaps() {
|
|||
REGISTER_MATCHER(implicitCastExpr);
|
||||
REGISTER_MATCHER(incompleteArrayType);
|
||||
REGISTER_MATCHER(initListExpr);
|
||||
REGISTER_MATCHER(injectedClassNameType);
|
||||
REGISTER_MATCHER(innerType);
|
||||
REGISTER_MATCHER(integerLiteral);
|
||||
REGISTER_MATCHER(isAnonymous);
|
||||
|
@ -298,7 +299,9 @@ RegistryMaps::RegistryMaps() {
|
|||
REGISTER_MATCHER(nullStmt);
|
||||
REGISTER_MATCHER(numSelectorArgs);
|
||||
REGISTER_MATCHER(ofClass);
|
||||
REGISTER_MATCHER(objcInterfaceDecl);
|
||||
REGISTER_MATCHER(objcMessageExpr);
|
||||
REGISTER_MATCHER(objcObjectPointerType);
|
||||
REGISTER_MATCHER(on);
|
||||
REGISTER_MATCHER(onImplicitObjectArgument);
|
||||
REGISTER_MATCHER(operatorCallExpr);
|
||||
|
@ -334,6 +337,7 @@ RegistryMaps::RegistryMaps() {
|
|||
REGISTER_MATCHER(templateArgument);
|
||||
REGISTER_MATCHER(templateArgumentCountIs);
|
||||
REGISTER_MATCHER(templateSpecializationType);
|
||||
REGISTER_MATCHER(templateTypeParmType);
|
||||
REGISTER_MATCHER(temporaryObjectExpr);
|
||||
REGISTER_MATCHER(thisExpr);
|
||||
REGISTER_MATCHER(throughUsingDecl);
|
||||
|
@ -350,6 +354,7 @@ RegistryMaps::RegistryMaps() {
|
|||
REGISTER_MATCHER(unaryTransformType);
|
||||
REGISTER_MATCHER(unless);
|
||||
REGISTER_MATCHER(unresolvedConstructExpr);
|
||||
REGISTER_MATCHER(unresolvedUsingTypenameDecl);
|
||||
REGISTER_MATCHER(unresolvedUsingValueDecl);
|
||||
REGISTER_MATCHER(userDefinedLiteral);
|
||||
REGISTER_MATCHER(usingDecl);
|
||||
|
|
|
@ -924,6 +924,54 @@ TEST(TypeMatcher, MatchesClassType) {
|
|||
varDecl(hasType(namedDecl(hasName("S"))))));
|
||||
}
|
||||
|
||||
TEST(TypeMatcher, MatchesDeclTypes) {
|
||||
// TypedefType -> TypedefNameDecl
|
||||
EXPECT_TRUE(matches("typedef int I; void f(I i);",
|
||||
parmVarDecl(hasType(namedDecl(hasName("I"))))));
|
||||
// ObjCObjectPointerType
|
||||
EXPECT_TRUE(matchesObjC("@interface Foo @end void f(Foo *f);",
|
||||
parmVarDecl(hasType(objcObjectPointerType()))));
|
||||
// ObjCObjectPointerType -> ObjCInterfaceType -> ObjCInterfaceDecl
|
||||
EXPECT_TRUE(matchesObjC(
|
||||
"@interface Foo @end void f(Foo *f);",
|
||||
parmVarDecl(hasType(pointsTo(objcInterfaceDecl(hasName("Foo")))))));
|
||||
// TemplateTypeParmType
|
||||
EXPECT_TRUE(matches("template <typename T> void f(T t);",
|
||||
parmVarDecl(hasType(templateTypeParmType()))));
|
||||
// TemplateTypeParmType -> TemplateTypeParmDecl
|
||||
EXPECT_TRUE(matches("template <typename T> void f(T t);",
|
||||
parmVarDecl(hasType(namedDecl(hasName("T"))))));
|
||||
// InjectedClassNameType
|
||||
EXPECT_TRUE(matches("template <typename T> struct S {"
|
||||
" void f(S s);"
|
||||
"};",
|
||||
parmVarDecl(hasType(injectedClassNameType()))));
|
||||
EXPECT_TRUE(notMatches("template <typename T> struct S {"
|
||||
" void g(S<T> s);"
|
||||
"};",
|
||||
parmVarDecl(hasType(injectedClassNameType()))));
|
||||
// InjectedClassNameType -> CXXRecordDecl
|
||||
EXPECT_TRUE(matches("template <typename T> struct S {"
|
||||
" void f(S s);"
|
||||
"};",
|
||||
parmVarDecl(hasType(namedDecl(hasName("S"))))));
|
||||
|
||||
static const char Using[] = "template <typename T>"
|
||||
"struct Base {"
|
||||
" typedef T Foo;"
|
||||
"};"
|
||||
""
|
||||
"template <typename T>"
|
||||
"struct S : private Base<T> {"
|
||||
" using typename Base<T>::Foo;"
|
||||
" void f(Foo);"
|
||||
"};";
|
||||
// UnresolvedUsingTypenameDecl
|
||||
EXPECT_TRUE(matches(Using, unresolvedUsingTypenameDecl(hasName("Foo"))));
|
||||
// UnresolvedUsingTypenameType -> UnresolvedUsingTypenameDecl
|
||||
EXPECT_TRUE(matches(Using, parmVarDecl(hasType(namedDecl(hasName("Foo"))))));
|
||||
}
|
||||
|
||||
TEST(Matcher, BindMatchedNodes) {
|
||||
DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
|
||||
|
||||
|
|
Loading…
Reference in New Issue