From 19eed2939e10fdd431ae6774fde6f8dad9be4fbf Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Wed, 15 Jul 2015 17:11:21 +0000 Subject: [PATCH] Add the ability to AST match a variable declaration that is an exception variable. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@242303 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 18 +++++++++++++++--- include/clang/ASTMatchers/ASTMatchers.h | 15 +++++++++++++++ lib/ASTMatchers/Dynamic/Registry.cpp | 1 + unittests/ASTMatchers/ASTMatchersTest.cpp | 4 ++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index a555bb339f..f4471364f2 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -1754,7 +1754,7 @@ Given private: int c; }; fieldDecl(isPrivate()) - matches 'int c;' + matches 'int c;' @@ -1768,7 +1768,7 @@ Given private: int c; }; fieldDecl(isProtected()) - matches 'int b;' + matches 'int b;' @@ -1782,7 +1782,7 @@ Given private: int c; }; fieldDecl(isPublic()) - matches 'int a;' + matches 'int a;' @@ -2293,6 +2293,18 @@ Usable as: Matcher<VarDecl>isExceptionVariable +
Matches if a variable declaration is for a C++ catch handler or Objective-C @catch variable.
+
+Example matches x (matcher = varDecl(isExceptionVariable())
+  void f(int y) {
+    try {
+    } catch (int x) {
+    }
+  }
+
+ + Matcher<VarDecl>isExplicitTemplateSpecialization
Matches explicit template specializations of function, class, or
 static member variable template instantiations.
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 281d6370e5..4e7625c1c2 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -2450,6 +2450,21 @@ AST_MATCHER(VarDecl, hasGlobalStorage) {
   return Node.hasGlobalStorage();
 }
 
+/// \brief Matches a variable declaration that is an exception variable from
+/// a C++ catch block, or an Objective-C \@catch statement.
+///
+/// Example matches x (matcher = varDecl(isExceptionVariable())
+/// \code
+/// void f(int y) {
+///   try {
+///   } catch (int x) {
+///   }
+/// }
+/// \endcode
+AST_MATCHER(VarDecl, isExceptionVariable) {
+  return Node.isExceptionVariable();
+}
+
 /// \brief Checks that a call expression or a constructor call expression has
 /// a specific number of arguments (including absent default arguments).
 ///
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 72713dda03..2af4f0002b 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -245,6 +245,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isConstQualified);
   REGISTER_MATCHER(isDefinition);
   REGISTER_MATCHER(isDeleted);
+  REGISTER_MATCHER(isExceptionVariable);
   REGISTER_MATCHER(isExplicitTemplateSpecialization);
   REGISTER_MATCHER(isExpr);
   REGISTER_MATCHER(isExternC);
diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp
index 8ef3f15e4c..54aed8f3fc 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -3329,6 +3329,10 @@ TEST(ExceptionHandling, SimpleCases) {
                       catchStmt(isCatchAll())));
   EXPECT_TRUE(notMatches("void foo() try { throw; } catch(int) { }",
                          catchStmt(isCatchAll())));
+  EXPECT_TRUE(matches("void foo() try {} catch(int X) { }",
+                      varDecl(isExceptionVariable())));
+  EXPECT_TRUE(notMatches("void foo() try { int X; } catch (...) { }",
+                         varDecl(isExceptionVariable())));
 }
 
 TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
-- 
2.40.0