From fc3d72eeea942e71a4dc5174e925a5e7d52a7e5a Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Mon, 23 Jul 2018 22:29:35 +0000 Subject: [PATCH] [ASTMatchers] Add an isMain() matcher Differential Revision: https://reviews.llvm.org/D49615 llvm-svn: 337761 --- clang/docs/LibASTMatchersReference.html | 6 ++++++ clang/include/clang/ASTMatchers/ASTMatchers.h | 6 ++++++ clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 + clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 3a32c6ca6c27..49899c30865e 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -2919,6 +2919,12 @@ namespaceDecl(isInline()) will match n::m. +Matcher<FunctionDecl>isMain +
Determines whether the function is "main", which is the entry point
+into an executable program.
+
+ + Matcher<FunctionDecl>isNoReturn
Matches FunctionDecls that have a noreturn attribute.
 
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 850caf63c7b0..75dc654905fd 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -616,6 +616,12 @@ AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher,
           InnerMatcher.matches(*Initializer, Finder, Builder));
 }
 
+/// Determines whether the function is "main", which is the entry point
+/// into an executable program.
+AST_MATCHER(FunctionDecl, isMain) {
+  return Node.isMain();
+}
+
 /// Matches the specialized template of a specialization declaration.
 ///
 /// Given
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 5a01c5d1f94a..982156f6dc4a 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -359,6 +359,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isInTemplateInstantiation);
   REGISTER_MATCHER(isLambda);
   REGISTER_MATCHER(isListInitialization);
+  REGISTER_MATCHER(isMain);
   REGISTER_MATCHER(isMemberInitializer);
   REGISTER_MATCHER(isMoveAssignmentOperator);
   REGISTER_MATCHER(isMoveConstructor);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index ef385e7f186c..7426dd926d8e 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2158,5 +2158,13 @@ TEST(IsAssignmentOperator, Basic) {
       notMatches("void x() { int a; if(a == 0) return; }", BinAsgmtOperator));
 }
 
+TEST(Matcher, isMain) {
+  EXPECT_TRUE(
+    matches("int main() {}", functionDecl(isMain())));
+
+  EXPECT_TRUE(
+    notMatches("int main2() {}", functionDecl(isMain())));
+}
+
 } // namespace ast_matchers
 } // namespace clang