Skip to content

Commit f981f44

Browse files
Add isTrivial() and isTriviallyCopyable() AST matchers
1 parent ddb9869 commit f981f44

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
@@ -5444,6 +5444,43 @@ AST_MATCHER(FunctionDecl, isDefaulted) {
54445444
return Node.isDefaulted();
54455445
}
54465446

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

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

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

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

0 commit comments

Comments
 (0)