Skip to content

Commit d536696

Browse files
committed
support_for_target_directive_clang_unittests
1 parent 0a5012f commit d536696

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8723,6 +8723,21 @@ AST_MATCHER_P(OMPExecutableDirective, hasAnyClause,
87238723
Builder) != Clauses.end();
87248724
}
87258725

8726+
/// Matches any ``#pragma omp target update`` executable directive.
8727+
///
8728+
/// Given
8729+
///
8730+
/// \code
8731+
/// #pragma omp target update from(a)
8732+
/// #pragma omp target update to(b)
8733+
/// \endcode
8734+
///
8735+
/// ``ompTargetUpdateDirective()`` matches both ``omp target update from(a)``
8736+
/// and ``omp target update to(b)``.
8737+
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
8738+
OMPTargetUpdateDirective>
8739+
ompTargetUpdateDirective;
8740+
87268741
/// Matches OpenMP ``default`` clause.
87278742
///
87288743
/// Given
@@ -8836,6 +8851,18 @@ AST_MATCHER_P(OMPExecutableDirective, isAllowedToContainClauseKind,
88368851
Finder->getASTContext().getLangOpts().OpenMP);
88378852
}
88388853

8854+
/// Matches OpenMP ``from`` clause.
8855+
///
8856+
/// Given
8857+
///
8858+
/// \code
8859+
/// #pragma omp target update from(a)
8860+
/// \endcode
8861+
///
8862+
/// ``ompFromClause()`` matches ``from(a)``.
8863+
extern const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPFromClause>
8864+
ompFromClause;
8865+
88398866
//----------------------------------------------------------------------------//
88408867
// End OpenMP handling.
88418868
//----------------------------------------------------------------------------//

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,12 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(
11241124

11251125
const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
11261126
ompExecutableDirective;
1127+
const internal::VariadicDynCastAllOfMatcher<Stmt, OMPTargetUpdateDirective>
1128+
ompTargetUpdateDirective;
11271129
const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPDefaultClause>
11281130
ompDefaultClause;
1131+
const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPFromClause>
1132+
ompFromClause;
11291133
const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl>
11301134
cxxDeductionGuideDecl;
11311135

clang/lib/ASTMatchers/Dynamic/Registry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ RegistryMaps::RegistryMaps() {
528528
REGISTER_MATCHER(ofClass);
529529
REGISTER_MATCHER(ofKind);
530530
REGISTER_MATCHER(ompDefaultClause);
531+
REGISTER_MATCHER(ompFromClause);
531532
REGISTER_MATCHER(ompExecutableDirective);
533+
REGISTER_MATCHER(ompTargetUpdateDirective);
532534
REGISTER_MATCHER(on);
533535
REGISTER_MATCHER(onImplicitObjectArgument);
534536
REGISTER_MATCHER(opaqueValueExpr);

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,6 +4734,105 @@ void x() {
47344734
EXPECT_TRUE(matchesWithOpenMP(Source8, Matcher));
47354735
}
47364736

4737+
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsStandaloneDirective) {
4738+
auto Matcher = ompTargetUpdateDirective(isStandaloneDirective());
4739+
4740+
StringRef Source0 = R"(
4741+
void foo() {
4742+
int arr[8];
4743+
#pragma omp target update from(arr[0:8:2])
4744+
;
4745+
}
4746+
)";
4747+
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
4748+
}
4749+
4750+
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_HasStructuredBlock) {
4751+
StringRef Source0 = R"(
4752+
void foo() {
4753+
int arr[8];
4754+
#pragma omp target update from(arr[0:8:2])
4755+
;
4756+
}
4757+
)";
4758+
EXPECT_TRUE(notMatchesWithOpenMP(
4759+
Source0, ompTargetUpdateDirective(hasStructuredBlock(nullStmt()))));
4760+
}
4761+
4762+
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_HasClause) {
4763+
auto Matcher = ompTargetUpdateDirective(hasAnyClause(ompFromClause()));
4764+
4765+
StringRef Source0 = R"(
4766+
void foo() {
4767+
int arr[8];
4768+
#pragma omp target update from(arr[0:8:2])
4769+
;
4770+
}
4771+
)";
4772+
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
4773+
4774+
auto astUnit = tooling::buildASTFromCodeWithArgs(Source0, {"-fopenmp"});
4775+
ASSERT_TRUE(astUnit);
4776+
4777+
auto Results = match(ompTargetUpdateDirective().bind("directive"),
4778+
astUnit->getASTContext());
4779+
ASSERT_FALSE(Results.empty());
4780+
4781+
const auto *Directive =
4782+
Results[0].getNodeAs<OMPTargetUpdateDirective>("directive");
4783+
ASSERT_TRUE(Directive);
4784+
4785+
OMPFromClause *FromClause = nullptr;
4786+
for (auto *Clause : Directive->clauses()) {
4787+
if (FromClause = dyn_cast<OMPFromClause>(Clause)) {
4788+
break;
4789+
}
4790+
}
4791+
ASSERT_TRUE(FromClause);
4792+
4793+
for (const auto *VarExpr : FromClause->varlist()) {
4794+
const auto *ArraySection = dyn_cast<ArraySectionExpr>(VarExpr);
4795+
if (!ArraySection)
4796+
continue;
4797+
// base (arr)
4798+
const Expr *Base = ArraySection->getBase();
4799+
ASSERT_TRUE(Base);
4800+
4801+
// lower bound (0)
4802+
const Expr *LowerBound = ArraySection->getLowerBound();
4803+
ASSERT_TRUE(LowerBound);
4804+
4805+
// length (8)
4806+
const Expr *Length = ArraySection->getLength();
4807+
ASSERT_TRUE(Length);
4808+
4809+
// stride (2)
4810+
const Expr *Stride = ArraySection->getStride();
4811+
ASSERT_TRUE(Stride);
4812+
}
4813+
}
4814+
4815+
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsAllowedToContainClauseKind) {
4816+
auto Matcher = ompTargetUpdateDirective(
4817+
isAllowedToContainClauseKind(llvm::omp::OMPC_from));
4818+
4819+
StringRef Source0 = R"(
4820+
void x() {
4821+
;
4822+
}
4823+
)";
4824+
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher));
4825+
4826+
StringRef Source1 = R"(
4827+
void foo() {
4828+
int arr[8];
4829+
#pragma omp target update from(arr[0:8:2])
4830+
;
4831+
}
4832+
)";
4833+
EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher));
4834+
}
4835+
47374836
TEST_P(ASTMatchersTest, HasAnyBase_DirectBase) {
47384837
if (!GetParam().isCXX()) {
47394838
return;

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,73 @@ void x() {
27422742
EXPECT_TRUE(notMatchesWithOpenMP(Source2, Matcher));
27432743
}
27442744

2745+
TEST(ASTMatchersTestOpenMP, OMPTargetUpdateDirective) {
2746+
auto Matcher = stmt(ompTargetUpdateDirective());
2747+
2748+
StringRef Source0 = R"(
2749+
void foo() {
2750+
int arr[8];
2751+
#pragma omp target update from(arr[0:8:2])
2752+
;
2753+
}
2754+
)";
2755+
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
2756+
}
2757+
2758+
TEST(ASTMatchersTestOpenMP, OMPFromClause) {
2759+
auto Matcher = ompTargetUpdateDirective(hasAnyClause(ompFromClause()));
2760+
2761+
StringRef Source0 = R"(
2762+
void foo() {
2763+
int arr[8];
2764+
#pragma omp target update from(arr[0:8:2])
2765+
;
2766+
}
2767+
)";
2768+
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher));
2769+
2770+
auto astUnit = tooling::buildASTFromCodeWithArgs(Source0, {"-fopenmp"});
2771+
ASSERT_TRUE(astUnit);
2772+
2773+
auto Results = match(ompTargetUpdateDirective().bind("directive"),
2774+
astUnit->getASTContext());
2775+
ASSERT_FALSE(Results.empty());
2776+
2777+
const auto *Directive =
2778+
Results[0].getNodeAs<OMPTargetUpdateDirective>("directive");
2779+
ASSERT_TRUE(Directive);
2780+
2781+
OMPFromClause *FromClause = nullptr;
2782+
for (auto *Clause : Directive->clauses()) {
2783+
if (FromClause = dyn_cast<OMPFromClause>(Clause)) {
2784+
break;
2785+
}
2786+
}
2787+
ASSERT_TRUE(FromClause);
2788+
2789+
for (const auto *VarExpr : FromClause->varlist()) {
2790+
const auto *ArraySection = dyn_cast<ArraySectionExpr>(VarExpr);
2791+
if (!ArraySection)
2792+
continue;
2793+
2794+
// base (arr)
2795+
const Expr *Base = ArraySection->getBase();
2796+
ASSERT_TRUE(Base);
2797+
2798+
// lower bound (0)
2799+
const Expr *LowerBound = ArraySection->getLowerBound();
2800+
ASSERT_TRUE(LowerBound);
2801+
2802+
// length (8)
2803+
const Expr *Length = ArraySection->getLength();
2804+
ASSERT_TRUE(Length);
2805+
2806+
// stride (2)
2807+
const Expr *Stride = ArraySection->getStride();
2808+
ASSERT_TRUE(Stride);
2809+
}
2810+
}
2811+
27452812
TEST(ASTMatchersTestOpenMP, OMPDefaultClause) {
27462813
auto Matcher = ompExecutableDirective(hasAnyClause(ompDefaultClause()));
27472814

0 commit comments

Comments
 (0)