Skip to content

Commit f56a299

Browse files
committedJun 5, 2014
Add hasLocalStorage/hasGlobalStorage matchers.
Summary: Add hasLocalStorage/hasGlobalStorage matchers for VarDecl nodes. Update the doc. Also add them to the dynamic registry. Reviewers: klimek Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D4034 llvm-svn: 210278
1 parent 0d2f580 commit f56a299

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
 

‎clang/docs/LibASTMatchersReference.html

+25
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,31 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
19661966
</pre></td></tr>
19671967

19681968

1969+
<tr><td>Matcher&lt<a href="http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('hasGlobalStorage0')"><a name="hasGlobalStorage0Anchor">hasGlobalStorage</a></td><td></td></tr>
1970+
<tr><td colspan="4" class="doc" id="hasGlobalStorage0"><pre>Matches a variable declaration that does not have local storage.
1971+
1972+
Example matches y and z (matcher = varDecl(hasGlobalStorage())
1973+
void f() {
1974+
int x;
1975+
static int y;
1976+
}
1977+
int z;
1978+
</pre></td></tr>
1979+
1980+
1981+
<tr><td>Matcher&lt<a href="http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('hasLocalStorage0')"><a name="hasLocalStorage0Anchor">hasLocalStorage</a></td><td></td></tr>
1982+
<tr><td colspan="4" class="doc" id="hasLocalStorage0"><pre>Matches a variable declaration that has function scope and is a
1983+
non-static local variable.
1984+
1985+
Example matches x (matcher = varDecl(hasLocalStorage())
1986+
void f() {
1987+
int x;
1988+
static int y;
1989+
}
1990+
int z;
1991+
</pre></td></tr>
1992+
1993+
19691994
<tr><td>Matcher&lt<a href="http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('isDefinition1')"><a name="isDefinition1Anchor">isDefinition</a></td><td></td></tr>
19701995
<tr><td colspan="4" class="doc" id="isDefinition1"><pre>Matches if a declaration has a body attached.
19711996

‎clang/include/clang/ASTMatchers/ASTMatchers.h

+29
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,35 @@ AST_MATCHER_P(
20582058
InnerMatcher.matches(*Initializer, Finder, Builder));
20592059
}
20602060

2061+
/// \brief Matches a variable declaration that has function scope and is a
2062+
/// non-static local variable.
2063+
///
2064+
/// Example matches x (matcher = varDecl(hasLocalStorage())
2065+
/// \code
2066+
/// void f() {
2067+
/// int x;
2068+
/// static int y;
2069+
/// }
2070+
/// int z;
2071+
/// \endcode
2072+
AST_MATCHER(VarDecl, hasLocalStorage) {
2073+
return Node.hasLocalStorage();
2074+
}
2075+
2076+
/// \brief Matches a variable declaration that does not have local storage.
2077+
///
2078+
/// Example matches y and z (matcher = varDecl(hasGlobalStorage())
2079+
/// \code
2080+
/// void f() {
2081+
/// int x;
2082+
/// static int y;
2083+
/// }
2084+
/// int z;
2085+
/// \endcode
2086+
AST_MATCHER(VarDecl, hasGlobalStorage) {
2087+
return Node.hasGlobalStorage();
2088+
}
2089+
20612090
/// \brief Checks that a call expression or a constructor call expression has
20622091
/// a specific number of arguments (including absent default arguments).
20632092
///

‎clang/lib/ASTMatchers/Dynamic/Registry.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ RegistryMaps::RegistryMaps() {
189189
REGISTER_MATCHER(hasEitherOperand);
190190
REGISTER_MATCHER(hasElementType);
191191
REGISTER_MATCHER(hasFalseExpression);
192+
REGISTER_MATCHER(hasGlobalStorage);
192193
REGISTER_MATCHER(hasImplicitDestinationType);
193194
REGISTER_MATCHER(hasIncrement);
194195
REGISTER_MATCHER(hasIndex);
195196
REGISTER_MATCHER(hasInitializer);
196197
REGISTER_MATCHER(hasLHS);
197198
REGISTER_MATCHER(hasLocalQualifiers);
199+
REGISTER_MATCHER(hasLocalStorage);
198200
REGISTER_MATCHER(hasLoopInit);
199201
REGISTER_MATCHER(hasMethod);
200202
REGISTER_MATCHER(hasName);

‎clang/unittests/ASTMatchers/ASTMatchersTest.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,18 @@ TEST(Matcher, VariableUsage) {
11651165
"}", Reference));
11661166
}
11671167

1168+
TEST(Matcher, VarDecl_Storage) {
1169+
auto M = varDecl(hasName("X"), hasLocalStorage());
1170+
EXPECT_TRUE(matches("void f() { int X; }", M));
1171+
EXPECT_TRUE(notMatches("int X;", M));
1172+
EXPECT_TRUE(notMatches("void f() { static int X; }", M));
1173+
1174+
M = varDecl(hasName("X"), hasGlobalStorage());
1175+
EXPECT_TRUE(notMatches("void f() { int X; }", M));
1176+
EXPECT_TRUE(matches("int X;", M));
1177+
EXPECT_TRUE(matches("void f() { static int X; }", M));
1178+
}
1179+
11681180
TEST(Matcher, FindsVarDeclInFunctionParameter) {
11691181
EXPECT_TRUE(matches(
11701182
"void f(int i) {}",

0 commit comments

Comments
 (0)
Please sign in to comment.