Skip to content

Commit 013b692

Browse files
Add isTrivial() and isTriviallyCopyable() AST matchers
1 parent 1b942ae commit 013b692

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5442,6 +5442,43 @@ AST_MATCHER(FunctionDecl, isDefaulted) {
54425442
return Node.isDefaulted();
54435443
}
54445444

5445+
/// Matches trivial methods and types.
5446+
///
5447+
/// Given:
5448+
/// \code
5449+
/// class A { A(); };
5450+
/// A::A() = default;
5451+
/// class B { B() = default; };
5452+
/// \endcode
5453+
/// cxxMethodDecl(isTrivial())
5454+
/// matches the declaration of B, but not A.
5455+
AST_POLYMORPHIC_MATCHER(isTrivial,
5456+
AST_POLYMORPHIC_SUPPORTED_TYPES(CXXMethodDecl,
5457+
CXXRecordDecl)) {
5458+
if (const auto *E = dyn_cast<CXXMethodDecl>(&Node))
5459+
return E->isTrivial();
5460+
if (const auto *E = dyn_cast<CXXRecordDecl>(&Node)) {
5461+
const auto *Def = Node.getDefinition();
5462+
return Def && Def->isTrivial();
5463+
}
5464+
return false;
5465+
}
5466+
5467+
/// Matches trivially copyable types.
5468+
///
5469+
/// Given:
5470+
/// \code
5471+
/// class A { A(const A &); };
5472+
/// A::A(const A &) = default;
5473+
/// class B { B(const B &) = default; };
5474+
/// \endcode
5475+
/// cxxMethodDecl(isTriviallyCopyable())
5476+
/// matches the declaration of B, but not A.
5477+
AST_MATCHER(CXXRecordDecl, isTriviallyCopyable) {
5478+
CXXRecordDecl *Def = Node.getDefinition();
5479+
return Def && Def->isTriviallyCopyable();
5480+
}
5481+
54455482
/// Matches weak function declarations.
54465483
///
54475484
/// Given:

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,33 @@ TEST_P(ASTMatchersTest, IsDeleted) {
18491849
functionDecl(hasName("Func"), isDeleted())));
18501850
}
18511851

1852+
TEST_P(ASTMatchersTest, IsTrivial) {
1853+
if (!GetParam().isCXX()) {
1854+
return;
1855+
}
1856+
1857+
EXPECT_TRUE(notMatches("class A { A(); };",
1858+
cxxRecordDecl(hasName("A"), isTrivial())));
1859+
EXPECT_TRUE(matches("class B { B() = default; };",
1860+
cxxRecordDecl(hasName("B"), isTrivial())));
1861+
1862+
EXPECT_TRUE(notMatches("class A { ~A(); }; A::~A() = default;",
1863+
cxxMethodDecl(hasName("~A"), isTrivial())));
1864+
EXPECT_TRUE(matches("class B { ~B() = default; };",
1865+
cxxMethodDecl(hasName("~B"), isTrivial())));
1866+
}
1867+
1868+
TEST_P(ASTMatchersTest, IsTriviallyCopyable) {
1869+
if (!GetParam().isCXX()) {
1870+
return;
1871+
}
1872+
1873+
EXPECT_TRUE(notMatches("class A { ~A(); }; A::~A() = default;",
1874+
cxxRecordDecl(hasName("A"), isTriviallyCopyable())));
1875+
EXPECT_TRUE(matches("class B { ~B() = default; };",
1876+
cxxRecordDecl(hasName("B"), isTriviallyCopyable())));
1877+
}
1878+
18521879
TEST_P(ASTMatchersTest, IsNoThrow_DynamicExceptionSpec) {
18531880
if (!GetParam().supportsCXXDynamicExceptionSpecification()) {
18541881
return;

0 commit comments

Comments
 (0)