From: Dave Lee Date: Sat, 11 Nov 2017 22:46:15 +0000 (+0000) Subject: Add ObjC exception statement AST matchers X-Git-Tag: llvmorg-6.0.0-rc1~3599 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0934fdc3a5c11bcfc539ee5ac992235a43b33b23;p=platform%2Fupstream%2Fllvm.git Add ObjC exception statement AST matchers Summary: Add AST matchers for Objective-C @throw, @try, @catch and @finally. Reviewers: aaron.ballman, malcolm.parsons, alexshap, compnerd Reviewed By: aaron.ballman Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D39940 llvm-svn: 317992 --- diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 63ae085..4cddea8 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -1225,6 +1225,24 @@ nullStmt() +Matcher<Stmt>objcCatchStmtMatcher<ObjCAtCatchStmt>... +
Matches Objective-C @catch statements.
+
+Example matches @catch
+  @try {}
+  @catch (...) {}
+
+ + +Matcher<Stmt>objcFinallyStmtMatcher<ObjCAtFinallyStmt>... +
Matches Objective-C @finally statements.
+
+Example matches @finally
+  @try {}
+  @finally {}
+
+ + Matcher<Stmt>objcMessageExprMatcher<ObjCMessageExpr>...
Matches ObjectiveC Message invocation expressions.
 
@@ -1236,6 +1254,23 @@ NSString's "alloc". This matcher should match both message sends.
 
+Matcher<Stmt>objcThrowStmtMatcher<ObjCAtThrowStmt>... +
Matches Objective-C @try statements.
+
+Example matches @throw
+  @throw obj;
+
+ + +Matcher<Stmt>objcTryStmtMatcher<ObjCAtTryStmt>... +
Matches Objective-C @try statements.
+
+Example matches @try
+  @try {}
+  @catch (...) {}
+
+ + Matcher<Stmt>opaqueValueExprMatcher<OpaqueValueExpr>...
Matches opaque value expressions. They are used as helpers
 to reference another expressions and can be met
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index d1a5825..34e6b42 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1265,6 +1265,49 @@ const internal::VariadicDynCastAllOfMatcher<
   Decl,
   ObjCPropertyDecl> objcPropertyDecl;
 
+/// \brief Matches Objective-C @try statements.
+///
+/// Example matches @throw
+/// \code
+///   @throw obj;
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  ObjCAtThrowStmt> objcThrowStmt;
+
+/// \brief Matches Objective-C @try statements.
+///
+/// Example matches @try
+/// \code
+///   @try {}
+///   @catch (...) {}
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  ObjCAtTryStmt> objcTryStmt;
+
+/// \brief Matches Objective-C @catch statements.
+///
+/// Example matches @catch
+/// \code
+///   @try {}
+///   @catch (...) {}
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  ObjCAtCatchStmt> objcCatchStmt;
+
+/// \brief Matches Objective-C @finally statements.
+///
+/// Example matches @finally
+/// \code
+///   @try {}
+///   @finally {}
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  ObjCAtFinallyStmt> objcFinallyStmt;
+
 /// \brief Matches expressions that introduce cleanups to be run at the end
 /// of the sub-expression's evaluation.
 ///
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index f171900..be2e1b7 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -388,8 +388,10 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(nullStmt);
   REGISTER_MATCHER(numSelectorArgs);
   REGISTER_MATCHER(ofClass);
+  REGISTER_MATCHER(objcCatchStmt);
   REGISTER_MATCHER(objcCategoryDecl);
   REGISTER_MATCHER(objcCategoryImplDecl);
+  REGISTER_MATCHER(objcFinallyStmt);
   REGISTER_MATCHER(objcImplementationDecl);
   REGISTER_MATCHER(objcInterfaceDecl);
   REGISTER_MATCHER(objcIvarDecl);
@@ -398,6 +400,8 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(objcObjectPointerType);
   REGISTER_MATCHER(objcPropertyDecl);
   REGISTER_MATCHER(objcProtocolDecl);
+  REGISTER_MATCHER(objcThrowStmt);
+  REGISTER_MATCHER(objcTryStmt);
   REGISTER_MATCHER(on);
   REGISTER_MATCHER(onImplicitObjectArgument);
   REGISTER_MATCHER(opaqueValueExpr);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index a24d8d3..59fadad 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1639,5 +1639,28 @@ TEST(ObjCDeclMatcher, CoreDecls) {
     objcPropertyDecl(hasName("enabled"))));
 }
 
+TEST(ObjCStmtMatcher, ExceptionStmts) {
+  std::string ObjCString =
+    "void f(id obj) {"
+    "  @try {"
+    "    @throw obj;"
+    "  } @catch (...) {"
+    "  } @finally {}"
+    "}";
+
+  EXPECT_TRUE(matchesObjC(
+    ObjCString,
+    objcTryStmt()));
+  EXPECT_TRUE(matchesObjC(
+    ObjCString,
+    objcThrowStmt()));
+  EXPECT_TRUE(matchesObjC(
+    ObjCString,
+    objcCatchStmt()));
+  EXPECT_TRUE(matchesObjC(
+    ObjCString,
+    objcFinallyStmt()));
+}
+
 } // namespace ast_matchers
 } // namespace clang