Skip to content

Commit fc3d72e

Browse files
author
George Karpenkov
committed
[ASTMatchers] Add an isMain() matcher
Differential Revision: https://reviews.llvm.org/D49615 llvm-svn: 337761
1 parent daac52c commit fc3d72e

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

clang/docs/LibASTMatchersReference.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,12 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
29192919
</pre></td></tr>
29202920

29212921

2922+
<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>
2923+
<tr><td colspan="4" class="doc" id="isMain0"><pre>Determines whether the function is "main", which is the entry point
2924+
into an executable program.
2925+
</pre></td></tr>
2926+
2927+
29222928
<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>
29232929
<tr><td colspan="4" class="doc" id="isNoReturn0"><pre>Matches FunctionDecls that have a noreturn attribute.
29242930

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,12 @@ AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>,
616616
InnerMatcher.matches(*Initializer, Finder, Builder));
617617
}
618618

619+
/// Determines whether the function is "main", which is the entry point
620+
/// into an executable program.
621+
AST_MATCHER(FunctionDecl, isMain) {
622+
return Node.isMain();
623+
}
624+
619625
/// Matches the specialized template of a specialization declaration.
620626
///
621627
/// Given

clang/lib/ASTMatchers/Dynamic/Registry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ RegistryMaps::RegistryMaps() {
359359
REGISTER_MATCHER(isInTemplateInstantiation);
360360
REGISTER_MATCHER(isLambda);
361361
REGISTER_MATCHER(isListInitialization);
362+
REGISTER_MATCHER(isMain);
362363
REGISTER_MATCHER(isMemberInitializer);
363364
REGISTER_MATCHER(isMoveAssignmentOperator);
364365
REGISTER_MATCHER(isMoveConstructor);

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,5 +2158,13 @@ TEST(IsAssignmentOperator, Basic) {
21582158
notMatches("void x() { int a; if(a == 0) return; }", BinAsgmtOperator));
21592159
}
21602160

2161+
TEST(Matcher, isMain) {
2162+
EXPECT_TRUE(
2163+
matches("int main() {}", functionDecl(isMain())));
2164+
2165+
EXPECT_TRUE(
2166+
notMatches("int main2() {}", functionDecl(isMain())));
2167+
}
2168+
21612169
} // namespace ast_matchers
21622170
} // namespace clang

0 commit comments

Comments
 (0)