summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Karpenkov <ekarpenkov@apple.com>2018-07-23 22:29:35 +0000
committerGeorge Karpenkov <ekarpenkov@apple.com>2018-07-23 22:29:35 +0000
commitbcd3d176cb4d5d3e31749a571cc6573259edec02 (patch)
tree5234c034816691b720e90a48308cee15c8c69885
parent3787b2b6af82e095ea3d2123db225566abf957d0 (diff)
[ASTMatchers] Add an isMain() matcher
Differential Revision: https://reviews.llvm.org/D49615 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@337761 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--docs/LibASTMatchersReference.html6
-rw-r--r--include/clang/ASTMatchers/ASTMatchers.h6
-rw-r--r--lib/ASTMatchers/Dynamic/Registry.cpp1
-rw-r--r--unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp8
4 files changed, 21 insertions, 0 deletions
diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html
index 3a32c6ca6c..49899c3086 100644
--- a/docs/LibASTMatchersReference.html
+++ b/docs/LibASTMatchersReference.html
@@ -2919,6 +2919,12 @@ namespaceDecl(isInline()) will match n::m.
</pre></td></tr>
+<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;</td><td class="name" onclick="toggle('isMain0')"><a name="isMain0Anchor">isMain</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isMain0"><pre>Determines whether the function is "main", which is the entry point
+into an executable program.
+</pre></td></tr>
+
+
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;</td><td class="name" onclick="toggle('isNoReturn0')"><a name="isNoReturn0Anchor">isNoReturn</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isNoReturn0"><pre>Matches FunctionDecls that have a noreturn attribute.
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 850caf63c7..75dc654905 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -616,6 +616,12 @@ AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>,
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/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 5a01c5d1f9..982156f6dc 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/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/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index ef385e7f18..7426dd926d 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/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