From 5fc313fcdc5a7acb5d6b12535ed3d7e85657705b Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Fri, 24 Jul 2015 12:35:41 +0000 Subject: [PATCH] Add an AST matcher, isFinal(), for testing whether a method or class declaration are marked final. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243107 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 31 +++++++++++++++++++++++ include/clang/ASTMatchers/ASTMatchers.h | 21 +++++++++++++++ lib/ASTMatchers/Dynamic/Registry.cpp | 1 + unittests/ASTMatchers/ASTMatchersTest.cpp | 9 +++++++ 4 files changed, 62 insertions(+) diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 5e34d65f2f..c977872f38 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -1488,6 +1488,25 @@ methodDecl(isConst()) matches A::foo() but not A::bar() +Matcher<CXXMethodDecl>isFinal +
Matches if the given method declaration is final.
+
+Given:
+
+struct A {
+  virtual void foo();
+  virtual void bar();
+};
+
+struct B : A {
+  void foo() final;
+  void bar();
+};
+
+methodDecl(isFinal()) matches B::foo() but not B::bar(), A::foo(), or A::bar()
+
+ + Matcher<CXXMethodDecl>isOverride
Matches if the given method declaration overrides another method.
 
@@ -1573,6 +1592,18 @@ isSameOrDerivedFrom(hasName(...)).
 
+Matcher<CXXRecordDecl>isFinal +
Matches if the given class declaration is final.
+
+Given:
+
+struct A {};
+
+struct B final : A {};
+
+recordDecl(isFinal()) matches B but not A.
+
+ Matcher<CXXRecordDecl>isTemplateInstantiation
Matches template instantiations of function, class, or static
 member variable template instantiations.
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 88f9f07885..0cd90d929e 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -3147,6 +3147,27 @@ AST_MATCHER(CXXMethodDecl, isVirtual) {
   return Node.isVirtual();
 }
 
+/// \brief Matches if the given method or class declaration is final.
+///
+/// Given:
+/// \code
+///   class A final {};
+///
+///   struct B {
+///     virtual void f();
+///   };
+///
+///   struct C : B {
+///     void f() final;
+///   };
+/// \endcode
+/// matches A and C::f, but not B, C, or B::f
+AST_POLYMORPHIC_MATCHER(isFinal,
+                        AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl,
+                                                        CXXMethodDecl)) {
+  return Node.hasAttr();
+}
+
 /// \brief Matches if the given method declaration is pure.
 ///
 /// Given
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 2af4f0002b..8c0832d169 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -249,6 +249,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isExplicitTemplateSpecialization);
   REGISTER_MATCHER(isExpr);
   REGISTER_MATCHER(isExternC);
+  REGISTER_MATCHER(isFinal);
   REGISTER_MATCHER(isImplicit);
   REGISTER_MATCHER(isExpansionInFileMatching);
   REGISTER_MATCHER(isExpansionInMainFile);
diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp
index 5ac28e5d3e..edeebde81e 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1784,6 +1784,15 @@ TEST(Matcher, MatchesAccessSpecDecls) {
   EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
 }
 
+TEST(Matcher, MatchesFinal) {
+  EXPECT_TRUE(matches("class X final {};", recordDecl(isFinal())));
+  EXPECT_TRUE(matches("class X { virtual void f() final; };",
+                      methodDecl(isFinal())));
+  EXPECT_TRUE(notMatches("class X {};", recordDecl(isFinal())));
+  EXPECT_TRUE(notMatches("class X { virtual void f(); };",
+                         methodDecl(isFinal())));
+}
+
 TEST(Matcher, MatchesVirtualMethod) {
   EXPECT_TRUE(matches("class X { virtual int f(); };",
       methodDecl(isVirtual(), hasName("::X::f"))));
-- 
2.40.0