forked from OSchip/llvm-project
Add hasAttr matcher for declarations.
Delete special-case CUDA attribute matchers. Patch by Jacques Pienaar. llvm-svn: 216379
This commit is contained in:
parent
4b3ba214d0
commit
3fe8a38110
|
@ -1612,30 +1612,13 @@ and reference to that variable declaration within a compound statement.
|
||||||
</pre></td></tr>
|
</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('hasCudaDeviceAttr0')"><a name="hasCudaDeviceAttr0Anchor">hasCudaDeviceAttr</a></td><td></td></tr>
|
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('hasAttr0')"><a name="hasAttr0Anchor">hasAttr</a></td><td>attr::Kind AttrKind</td></tr>
|
||||||
<tr><td colspan="4" class="doc" id="hasCudaDeviceAttr0"><pre>Matches declaration that has CUDA device attribute.
|
<tr><td colspan="4" class="doc" id="hasAttr0"><pre>Matches declaration that has a given attribute.
|
||||||
|
|
||||||
Given
|
Given
|
||||||
__attribute__((device)) void f() { ... }
|
__attribute__((device)) void f() { ... }
|
||||||
matches the function declaration of f.
|
decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of
|
||||||
</pre></td></tr>
|
f.
|
||||||
|
|
||||||
|
|
||||||
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('hasCudaGlobalAttr0')"><a name="hasCudaGlobalAttr0Anchor">hasCudaGlobalAttr</a></td><td></td></tr>
|
|
||||||
<tr><td colspan="4" class="doc" id="hasCudaGlobalAttr0"><pre>Matches declaration that has CUDA global attribute.
|
|
||||||
|
|
||||||
Given
|
|
||||||
__attribute__((global)) void f() { ... }
|
|
||||||
matches the function declaration of f.
|
|
||||||
</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('hasCudaHostAttr0')"><a name="hasCudaHostAttr0Anchor">hasCudaHostAttr</a></td><td></td></tr>
|
|
||||||
<tr><td colspan="4" class="doc" id="hasCudaHostAttr0"><pre>Matches declaration that has CUDA host attribute.
|
|
||||||
|
|
||||||
Given
|
|
||||||
__attribute__((host)) void f() { ... }
|
|
||||||
matches the function declaration of f.
|
|
||||||
</pre></td></tr>
|
</pre></td></tr>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3658,6 +3658,22 @@ AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>,
|
||||||
return InnerMatcher.matches(*Node.getLHS(), Finder, Builder);
|
return InnerMatcher.matches(*Node.getLHS(), Finder, Builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// \brief Matches declaration that has a given attribute.
|
||||||
|
///
|
||||||
|
/// Given
|
||||||
|
/// \code
|
||||||
|
/// __attribute__((device)) void f() { ... }
|
||||||
|
/// \endcode
|
||||||
|
/// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of
|
||||||
|
/// f.
|
||||||
|
AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) {
|
||||||
|
for (const auto *Attr : Node.attrs()) {
|
||||||
|
if (Attr->getKind() == AttrKind)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// \brief Matches CUDA kernel call expression.
|
/// \brief Matches CUDA kernel call expression.
|
||||||
///
|
///
|
||||||
/// Example matches,
|
/// Example matches,
|
||||||
|
@ -3667,39 +3683,6 @@ AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>,
|
||||||
const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr>
|
const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr>
|
||||||
CUDAKernelCallExpr;
|
CUDAKernelCallExpr;
|
||||||
|
|
||||||
/// \brief Matches declaration that has CUDA device attribute.
|
|
||||||
///
|
|
||||||
/// Given
|
|
||||||
/// \code
|
|
||||||
/// __attribute__((device)) void f() { ... }
|
|
||||||
/// \endcode
|
|
||||||
/// matches the function declaration of f.
|
|
||||||
AST_MATCHER(Decl, hasCudaDeviceAttr) {
|
|
||||||
return Node.hasAttr<clang::CUDADeviceAttr>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Matches declaration that has CUDA host attribute.
|
|
||||||
///
|
|
||||||
/// Given
|
|
||||||
/// \code
|
|
||||||
/// __attribute__((host)) void f() { ... }
|
|
||||||
/// \endcode
|
|
||||||
/// matches the function declaration of f.
|
|
||||||
AST_MATCHER(Decl, hasCudaHostAttr) {
|
|
||||||
return Node.hasAttr<clang::CUDAHostAttr>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Matches declaration that has CUDA global attribute.
|
|
||||||
///
|
|
||||||
/// Given
|
|
||||||
/// \code
|
|
||||||
/// __attribute__((global)) void f() { ... }
|
|
||||||
/// \endcode
|
|
||||||
/// matches the function declaration of f.
|
|
||||||
AST_MATCHER(Decl, hasCudaGlobalAttr) {
|
|
||||||
return Node.hasAttr<clang::CUDAGlobalAttr>();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // end namespace ast_matchers
|
} // end namespace ast_matchers
|
||||||
} // end namespace clang
|
} // end namespace clang
|
||||||
|
|
||||||
|
|
|
@ -76,6 +76,27 @@ template <> struct ArgTypeTraits<unsigned> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <> struct ArgTypeTraits<attr::Kind> {
|
||||||
|
private:
|
||||||
|
static attr::Kind getAttrKind(llvm::StringRef AttrKind) {
|
||||||
|
return llvm::StringSwitch<attr::Kind>(AttrKind)
|
||||||
|
#define ATTR(X) .Case("attr::" #X, attr:: X)
|
||||||
|
#include "clang/Basic/AttrList.inc"
|
||||||
|
.Default(attr::Kind(-1));
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
static bool is(const VariantValue &Value) {
|
||||||
|
return Value.isString() &&
|
||||||
|
getAttrKind(Value.getString()) != attr::Kind(-1);
|
||||||
|
}
|
||||||
|
static attr::Kind get(const VariantValue &Value) {
|
||||||
|
return getAttrKind(Value.getString());
|
||||||
|
}
|
||||||
|
static ArgKind getKind() {
|
||||||
|
return ArgKind(ArgKind::AK_String);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// \brief Matcher descriptor interface.
|
/// \brief Matcher descriptor interface.
|
||||||
///
|
///
|
||||||
/// Provides a \c create() method that constructs the matcher from the provided
|
/// Provides a \c create() method that constructs the matcher from the provided
|
||||||
|
|
|
@ -176,15 +176,13 @@ RegistryMaps::RegistryMaps() {
|
||||||
REGISTER_MATCHER(hasAnyUsingShadowDecl);
|
REGISTER_MATCHER(hasAnyUsingShadowDecl);
|
||||||
REGISTER_MATCHER(hasArgument);
|
REGISTER_MATCHER(hasArgument);
|
||||||
REGISTER_MATCHER(hasArgumentOfType);
|
REGISTER_MATCHER(hasArgumentOfType);
|
||||||
|
REGISTER_MATCHER(hasAttr);
|
||||||
REGISTER_MATCHER(hasBase);
|
REGISTER_MATCHER(hasBase);
|
||||||
REGISTER_MATCHER(hasBody);
|
REGISTER_MATCHER(hasBody);
|
||||||
REGISTER_MATCHER(hasCanonicalType);
|
REGISTER_MATCHER(hasCanonicalType);
|
||||||
REGISTER_MATCHER(hasCaseConstant);
|
REGISTER_MATCHER(hasCaseConstant);
|
||||||
REGISTER_MATCHER(hasCondition);
|
REGISTER_MATCHER(hasCondition);
|
||||||
REGISTER_MATCHER(hasConditionVariableStatement);
|
REGISTER_MATCHER(hasConditionVariableStatement);
|
||||||
REGISTER_MATCHER(hasCudaDeviceAttr);
|
|
||||||
REGISTER_MATCHER(hasCudaGlobalAttr);
|
|
||||||
REGISTER_MATCHER(hasCudaHostAttr);
|
|
||||||
REGISTER_MATCHER(hasDeclContext);
|
REGISTER_MATCHER(hasDeclContext);
|
||||||
REGISTER_MATCHER(hasDeclaration);
|
REGISTER_MATCHER(hasDeclaration);
|
||||||
REGISTER_MATCHER(hasDeducedType);
|
REGISTER_MATCHER(hasDeducedType);
|
||||||
|
|
|
@ -649,22 +649,23 @@ TEST(DeclarationMatcher, HasDescendantMemoization) {
|
||||||
EXPECT_TRUE(matches("void f() { int i; }", CannotMemoize));
|
EXPECT_TRUE(matches("void f() { int i; }", CannotMemoize));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(DeclarationMatcher, HasAttr) {
|
||||||
|
EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};",
|
||||||
|
decl(hasAttr(clang::attr::WarnUnused))));
|
||||||
|
EXPECT_FALSE(matches("struct X {};",
|
||||||
|
decl(hasAttr(clang::attr::WarnUnused))));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(DeclarationMatcher, MatchCudaDecl) {
|
TEST(DeclarationMatcher, MatchCudaDecl) {
|
||||||
EXPECT_TRUE(matchesWithCuda("__global__ void f() { }"
|
EXPECT_TRUE(matchesWithCuda("__global__ void f() { }"
|
||||||
"void g() { f<<<1, 2>>>(); }",
|
"void g() { f<<<1, 2>>>(); }",
|
||||||
CUDAKernelCallExpr()));
|
CUDAKernelCallExpr()));
|
||||||
EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}",
|
EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}",
|
||||||
hasCudaDeviceAttr()));
|
hasAttr(clang::attr::CUDADevice)));
|
||||||
EXPECT_TRUE(matchesWithCuda("__attribute__((host)) void f() {}",
|
|
||||||
hasCudaHostAttr()));
|
|
||||||
EXPECT_TRUE(matchesWithCuda("__attribute__((global)) void f() {}",
|
|
||||||
hasCudaGlobalAttr()));
|
|
||||||
EXPECT_FALSE(matchesWithCuda("void f() {}",
|
|
||||||
hasCudaGlobalAttr()));
|
|
||||||
EXPECT_TRUE(notMatchesWithCuda("void f() {}",
|
EXPECT_TRUE(notMatchesWithCuda("void f() {}",
|
||||||
hasCudaGlobalAttr()));
|
CUDAKernelCallExpr()));
|
||||||
EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}",
|
EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}",
|
||||||
hasCudaGlobalAttr()));
|
hasAttr(clang::attr::CUDAGlobal)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements a run method that returns whether BoundNodes contains a
|
// Implements a run method that returns whether BoundNodes contains a
|
||||||
|
|
|
@ -472,6 +472,14 @@ TEST_F(RegistryTest, Completion) {
|
||||||
"Matcher<NamedDecl> hasName(string)"));
|
"Matcher<NamedDecl> hasName(string)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(RegistryTest, HasArgs) {
|
||||||
|
Matcher<Decl> Value = constructMatcher(
|
||||||
|
"decl", constructMatcher("hasAttr", std::string("attr::WarnUnused")))
|
||||||
|
.getTypedMatcher<Decl>();
|
||||||
|
EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};", Value));
|
||||||
|
EXPECT_FALSE(matches("struct X {};", Value));
|
||||||
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
} // end namespace dynamic
|
} // end namespace dynamic
|
||||||
} // end namespace ast_matchers
|
} // end namespace ast_matchers
|
||||||
|
|
Loading…
Reference in New Issue